1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.time;
18
19 import org.orekit.errors.OrekitException;
20 import org.orekit.errors.OrekitMessages;
21
22
23
24
25
26
27
28 class CcsdsSegmentedTimeCode extends AbstractCcsdsTimeCode {
29
30
31 private final DateComponents date;
32
33
34 private final TimeComponents time;
35
36
37 private final double subSecond;
38
39
40
41
42
43
44
45
46
47
48
49
50
51 CcsdsSegmentedTimeCode(final byte preambleField, final byte[] timeField,
52 final DateComponents agencyDefinedEpoch) {
53
54
55 if ((preambleField & 0xF0) != 0x40) {
56 throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
57 formatByte(preambleField));
58 }
59
60
61 final DateComponents epoch;
62 if ((preambleField & 0x08) == 0x00) {
63
64 epoch = DateComponents.CCSDS_EPOCH;
65 } else {
66
67 if (agencyDefinedEpoch == null) {
68 throw new OrekitException(OrekitMessages.CCSDS_DATE_MISSING_AGENCY_EPOCH);
69 }
70 epoch = agencyDefinedEpoch;
71 }
72
73
74 final int daySegmentLength = ((preambleField & 0x04) == 0x0) ? 2 : 3;
75 final int subMillisecondLength = (preambleField & 0x03) << 1;
76 if (subMillisecondLength == 6) {
77 throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
78 formatByte(preambleField));
79 }
80 if (timeField.length != daySegmentLength + 4 + subMillisecondLength) {
81 throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_LENGTH_TIME_FIELD,
82 timeField.length, daySegmentLength + 4 + subMillisecondLength);
83 }
84
85
86 int i = 0;
87 int day = 0;
88 while (i < daySegmentLength) {
89 day = day * 256 + toUnsigned(timeField[i++]);
90 }
91
92 long milliInDay = 0L;
93 while (i < daySegmentLength + 4) {
94 milliInDay = milliInDay * 256 + toUnsigned(timeField[i++]);
95 }
96 final int milli = (int) (milliInDay % 1000L);
97 final int seconds = (int) ((milliInDay - milli) / 1000L);
98
99 double subMilli = 0;
100 double divisor = 1;
101 while (i < timeField.length) {
102 subMilli = subMilli * 256 + toUnsigned(timeField[i++]);
103 divisor *= 1000;
104 }
105
106 this.date = new DateComponents(epoch, day);
107 this.time = new TimeComponents(seconds);
108 this.subSecond = milli * 1.0e-3 + subMilli / divisor;
109
110 }
111
112
113
114
115
116
117
118
119
120
121 CcsdsSegmentedTimeCode(final byte preambleField, final byte[] timeField) {
122
123
124 if ((preambleField & 0xF0) != 0x50) {
125 throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
126 formatByte(preambleField));
127 }
128
129
130 final int length = 7 + (preambleField & 0x07);
131 if (length == 14) {
132 throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
133 formatByte(preambleField));
134 }
135 if (timeField.length != length) {
136 throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_LENGTH_TIME_FIELD,
137 timeField.length, length);
138 }
139
140
141 if ((preambleField & 0x08) == 0x00) {
142
143 this.date = new DateComponents(toUnsigned(timeField[0]) * 256 + toUnsigned(timeField[1]),
144 toUnsigned(timeField[2]),
145 toUnsigned(timeField[3]));
146 } else {
147
148 this.date = new DateComponents(toUnsigned(timeField[0]) * 256 + toUnsigned(timeField[1]),
149 toUnsigned(timeField[2]) * 256 + toUnsigned(timeField[3]));
150 }
151
152
153 this.time = new TimeComponents(toUnsigned(timeField[4]),
154 toUnsigned(timeField[5]),
155 toUnsigned(timeField[6]));
156
157 double sub = 0;
158 double divisor = 1;
159 for (int i = 7; i < length; ++i) {
160 sub = sub * 100 + toUnsigned(timeField[i]);
161 divisor *= 100;
162 }
163
164 this.subSecond = sub / divisor;
165
166 }
167
168
169
170
171 public DateComponents getDate() {
172 return date;
173 }
174
175
176
177
178 public TimeComponents getTime() {
179 return time;
180 }
181
182
183
184
185 public double getSubSecond() {
186 return subSecond;
187 }
188
189 }