OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
CivilDateTime.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // "Copyright Centre National d'Etudes Spatiales"
4 //
5 // License: LGPL
6 //
7 // See LICENSE.txt file in the top level directory for more details.
8 //
9 //----------------------------------------------------------------------------
10 // $Id$
11 
12 #include <otb/JulianDate.h>
13 #include <otb/CivilDateTime.h>
14 #include <otb/JSDDateTime.h>
15 #include <otb/GMSTDateTime.h>
16 
17 #include <cmath>
18 #include <cstring>
19 #include <cstdlib>
20 #include <cstdio>
21 #include <ostream>
22 
23 namespace ossimplugins
24 {
25 
26 
27 const double CivilDateTime::JOURCIVIL_LENGTH = 86400.0 ;
28 
30  _year(0),
31  _month(0),
32  _day(0),
33  _second(0),
34  _decimal(0.0)
35 {
36 }
37 
38 CivilDateTime::CivilDateTime(int year, int month, int day, int second, double decimal):
39  _year(year),
40  _month(month),
41  _day(day),
42  _second(second),
43  _decimal(decimal)
44 {
45 }
46 
48 {
49  SetUtcDateTime(Utc);
50 }
51 
53 {
54  SetUtcDateTime(Utc);
55 }
56 
58 {
59 }
60 
62  _year(rhs._year),
63  _month(rhs._month),
64  _day(rhs._day),
65  _second(rhs._second),
66  _decimal(rhs._decimal)
67 {
68 
69 }
70 
72 {
73  _year = rhs._year;
74  _month = rhs._month;
75  _day = rhs._day;
76  _second = rhs._second;
77  _decimal = rhs._decimal;
78 
79  return *this;
80 }
81 
83 {
84  int etat , test ;
85  int an , mois , i , j ;
86  double b , d ;
87 
88  if (_month > 2)
89  {
90  an = _year ;
91  mois = _month ;
92  }
93  else
94  {
95  an = _year - 1 ;
96  mois = _month + 12 ;
97  }
98 
99  test = _year * 10000 + _month * 100 + _day ;
100  if ((test > 15821004) && (test < 15821015))
101  etat = 1 ;
102  else
103  {
104  etat = 0 ;
105  d = (an < 0) ? 0.75 : 0.0 ;
106  if (test >= 15821015)
107  {
108  i = an / 100 ;
109  j = 2 - i + i/4 ;
110  b = (double)j + 1720994.5 ;
111  }
112  else
113  b = 1720994.5 ;
114 
115  i = (int) (365.25 * (double)an - d) ;
116  j = (int) (30.6001 * (double)(mois + 1)) ;
117  double jourJulien = (double)(i + j) + (double)_day
118  + ((double)_second + _decimal) / JOURCIVIL_LENGTH + b ;
119 
120  julianDate->set_julianDate(jourJulien);
121  }
122 
123  return etat;
124 }
125 
127 {
128  char str_an[5];
129  char str_day[3];
130  char str_month[4];
131  char str_hour[3];
132  char str_min[3];
133  char str_sec[3];
134  char str_micro[7];
135  int i;
136  double exposant;
137 
138  /*
139  * year extraction
140  */
141  for (i = 0; i < 4; i++)
142  {
143  str_an[i] = Utc[i+7];
144  }
145 
146  str_an[4] = '\0';
147  _year = atoi(str_an);
148  if ( (_year > 2050) || (_year < 1950) )
149  {
150  return 1;
151  }
152 
153  /*
154  * month extraction
155  */
156  for (i = 0; i < 3; i++)
157  {
158  str_month[i] = Utc[i+3];
159  }
160 
161  str_month[3] = '\0';
162  if (!strcmp(str_month, "JAN")) _month = 1;
163  else if (!strcmp(str_month, "FEV")) _month = 2;
164  else if (!strcmp(str_month, "MAR")) _month = 3;
165  else if (!strcmp(str_month, "APR")) _month = 4;
166  else if (!strcmp(str_month, "MAY")) _month = 5;
167  else if (!strcmp(str_month, "JUN")) _month = 6;
168  else if (!strcmp(str_month, "JUL")) _month = 7;
169  else if (!strcmp(str_month, "AUG")) _month = 8;
170  else if (!strcmp(str_month, "SEP")) _month = 9;
171  else if (!strcmp(str_month, "OCT")) _month = 10;
172  else if (!strcmp(str_month, "NOV")) _month = 11;
173  else if (!strcmp(str_month, "DEC")) _month = 12;
174  else
175  {
176  return 1;
177  }
178 
179  /*
180  * day extraction
181  */
182  for (i = 0; i < 2; i++)
183  {
184  str_day[i] = Utc[i];
185  }
186 
187  str_day[2] = '\0';
188  _day = atoi(str_day);
189  if ( (_day < 1) || (_day > 31) )
190  {
191  return 1;
192  }
193 
194  /*
195  * seconds extraction
196  */
197  for (i = 0; i < 2; i++)
198  {
199  str_hour[i] = Utc[i+12];
200  str_min[i] = Utc[i+15];
201  str_sec[i] = Utc[i+18];
202  }
203 
204  str_hour[2] = '\0';if ( (atoi(str_hour) < 0) || (atoi(str_hour) > 23) )
205  {
206  return 1;
207  }
208 
209  str_min[2] = '\0';if ( (atoi(str_min) < 0) || (atoi(str_hour) > 59) )
210  {
211  return 1;
212  }
213  str_sec[2] = '\0';if ( (atoi(str_sec) < 0) || (atoi(str_hour) > 59) )
214  {
215  return 1;
216  }
217  _second = 3600*atoi(str_hour) + 60*atoi(str_min) + atoi(str_sec);
218 
219  /*
220  * microseconds extraction
221  */
222  for (i = 0; i < 6; i++)
223  {
224  str_micro[i] = Utc[i+21];
225  }
226 
227  str_micro[6] = '\0';
228  i = 0;
229  exposant = 10e-6;
230  while ((unsigned int)i < strlen(str_micro) )
231  {
232  if (str_micro[i] == '0') exposant = exposant * 0.1;
233  i++;
234  }
235 
236  _decimal = exposant * (double) atoi(str_micro);
237  if ( (_decimal < 0) || (_decimal >= 1) )
238  {
239  return 1;
240  }
241 
242  return 0;
243 
244 }
245 
246 int CivilDateTime::SetUtcDateTime(const char* Utc)
247 {
248  char str_an[5];
249  char str_day[3];
250  char str_month[4];
251  char str_hour[3];
252  char str_min[3];
253  char str_sec[3];
254  char str_micro[7];
255  int i;
256  double exposant;
257 
258  /*
259  * year extraction
260  */
261  for (i = 0; i < 4; i++)
262  {
263  str_an[i] = Utc[i+7];
264  }
265 
266  str_an[4] = '\0';
267  _year = atoi(str_an);
268  if ( (_year > 2050) || (_year < 1950) )
269  {
270  return 1;
271  }
272 
273  /*
274  * month extraction
275  */
276  for (i = 0; i < 3; i++)
277  {
278  str_month[i] = Utc[i+3];
279  }
280 
281  str_month[3] = '\0';
282  if (!strcmp(str_month, "JAN")) _month = 1;
283  else if (!strcmp(str_month, "FEV")) _month = 2;
284  else if (!strcmp(str_month, "MAR")) _month = 3;
285  else if (!strcmp(str_month, "APR")) _month = 4;
286  else if (!strcmp(str_month, "MAY")) _month = 5;
287  else if (!strcmp(str_month, "JUN")) _month = 6;
288  else if (!strcmp(str_month, "JUL")) _month = 7;
289  else if (!strcmp(str_month, "AUG")) _month = 8;
290  else if (!strcmp(str_month, "SEP")) _month = 9;
291  else if (!strcmp(str_month, "OCT")) _month = 10;
292  else if (!strcmp(str_month, "NOV")) _month = 11;
293  else if (!strcmp(str_month, "DEC")) _month = 12;
294  else
295  {
296  return 1;
297  }
298 
299  /*
300  * day extraction
301  */
302  for (i = 0; i < 2; i++)
303  {
304  str_day[i] = Utc[i];
305  }
306 
307  str_day[2] = '\0';
308  _day = atoi(str_day);
309  if ( (_day < 1) || (_day > 31) )
310  {
311  return 1;
312  }
313 
314  /*
315  * seconds extraction
316  */
317  for (i = 0; i < 2; i++)
318  {
319  str_hour[i] = Utc[i+12];
320  str_min[i] = Utc[i+15];
321  str_sec[i] = Utc[i+18];
322  }
323 
324  str_hour[2] = '\0';if ( (atoi(str_hour) < 0) || (atoi(str_hour) > 23) )
325  {
326  return 1;
327  }
328 
329  str_min[2] = '\0';if ( (atoi(str_min) < 0) || (atoi(str_hour) > 59) )
330  {
331  return 1;
332  }
333  str_sec[2] = '\0';if ( (atoi(str_sec) < 0) || (atoi(str_hour) > 59) )
334  {
335  return 1;
336  }
337  _second = 3600*atoi(str_hour) + 60*atoi(str_min) + atoi(str_sec);
338 
339  /*
340  * microseconds extraction
341  */
342  for (i = 0; i < 6; i++)
343  {
344  str_micro[i] = Utc[i+21];
345  }
346 
347  str_micro[6] = '\0';
348  i = 0;
349  exposant = 10e-6;
350  while ((unsigned int)i < strlen(str_micro) )
351  {
352  if (str_micro[i] == '0') exposant = exposant * 0.1;
353  i++;
354  }
355 
356  _decimal = exposant * (double) atoi(str_micro);
357  if ( (_decimal < 0) || (_decimal >= 1) )
358  {
359  return 1;
360  }
361 
362  return 0;
363 
364 }
365 
366 
368 {
369  int etat , k ;
370  JulianDate jourJulien;
371  double r ;
372 
373  etat = AsJulianDate(&jourJulien );
374  if (etat == 0)
375  {
376  k = (int)(jourJulien.get_julianDate() - 0.5) ;
377 
378  double day0hTU = (double)k + 0.5;
379  JSDdate->set_day0hTU(JulianDate(day0hTU));
380 
381  r = (jourJulien.get_julianDate() - day0hTU) * JOURCIVIL_LENGTH ;
382  JSDdate->set_second(floor (r)) ;
383  JSDdate->set_decimal(r - JSDdate->get_second()) ;
384  }
385 
386  return etat ;
387 }
388 
390 {
391  const double TWOPI = 6.28318530717958647693 ;
392  const double OMEGATERRE = 6.28318530717958647693 / 86164.09054 ;
393  int etat ;
394  JulianDate jourJulien;
395  double t=0.0;
396  double tsm0hTU=0.0;
397  double tsm;
398  CivilDateTime date0hTU(_year, _month, _day, 0, 0.0);
399 
400 
401  etat = date0hTU.AsJulianDate(&jourJulien );
402  if (etat == 0)
403  {
404  if (GMST->get_origine() == GMSTDateTime::AN1900)
405  {
406  t = (jourJulien.get_julianDate() - 2415020.0) / 36525.0 ;
407  tsm0hTU = 23925.836 + t * (8640184.542 + t * 0.0929) ;
408  tsm0hTU = tsm0hTU * TWOPI / JOURCIVIL_LENGTH ;
409  }
410  else if (GMST->get_origine() == GMSTDateTime::AN1950)
411  {
412  t = jourJulien.get_julianDate() - 2433281.5 ;
413  tsm0hTU = 1.72944494 + t * 6.3003880987 ;
414  }
415  else if (GMST->get_origine() == GMSTDateTime::AN2000)
416  {
417  t = (jourJulien.get_julianDate() - 2451545.0) / 36525.0 ;
418  tsm0hTU = 24110.54841 + t * (8640184.812866 + t * (0.093103 - t * 6.210e-6)) ;
419  tsm0hTU = tsm0hTU * TWOPI / JOURCIVIL_LENGTH ;
420  }
421  t = tsm0hTU + ((double)_second + _decimal) * OMEGATERRE ;
422  tsm = fmod (t, TWOPI) ;
423 
424  if (tsm < 0.0)
425  {
426  tsm = tsm + TWOPI ;
427  }
428  GMST->set_tms(tsm);
429  }
430 
431  return etat ;
432 }
433 
434 
435 CivilDateTime::operator JulianDate()
436 {
437  JulianDate date;
438  AsJulianDate(&date);
439  return date;
440 }
441 
442 CivilDateTime::operator JSDDateTime()
443 {
444  JSDDateTime date;
445  AsJSDDateTime(&date);
446  return date;
447 }
448 
449 CivilDateTime::operator GMSTDateTime()
450 {
451  GMSTDateTime date;
452  AsGMSTDateTime(&date);
453  return date;
454 }
455 
457 {
458  out << "CivilDateTime:"
459  << "\nyear: " << _year
460  << "\nmonth: " << _month
461  << "\nday: " << _day
462  << "\nsecond: " << _second
463  << "\ndecimal: " << _decimal
464  << std::endl;
465 
466  return out;
467 }
468 
470 {
471  return obj.print(out);
472 }
473 }
void set_tms(double tms)
Definition: GMSTDateTime.h:68
This class represents a date.
Definition: GMSTDateTime.h:29
This class represents a date and time in the civil format.
Definition: CivilDateTime.h:30
double get_julianDate() const
Definition: JulianDate.h:70
int AsJulianDate(JulianDate *julianDate)
This function converts the current CivilDateTime into Julian Date.
void set_day0hTU(JulianDate day)
Definition: JSDDateTime.h:86
static const double JOURCIVIL_LENGTH
int AsGMSTDateTime(GMSTDateTime *GMSTdate)
This function converts the current CivilDateTime into GMSTDateTime.
std::ostream & print(std::ostream &out) const
int AsJSDDateTime(JSDDateTime *JSDdate)
This function converts the current CivilDateTime into JSDDateTime.
void set_second(double second)
Definition: JSDDateTime.h:91
This class represents a date in the julian referential.
Definition: JulianDate.h:27
std::ostream & operator<<(std::ostream &os, const AlosPalsarData &data)
int SetUtcDateTime(char *Utc)
This function initialize the date and the the time of the class using an utc string.
void set_decimal(double decimal)
Definition: JSDDateTime.h:96
double get_second() const
Definition: JSDDateTime.h:76
void set_julianDate(double julianDate)
Definition: JulianDate.h:65
CivilDateTime & operator=(const CivilDateTime &rhs)
Affectation operator.
int _second
Number of secondes since the beginning of the day.
Ori_TSM_Green get_origine()
Definition: GMSTDateTime.h:73
This class represents a date.
Definition: JSDDateTime.h:30
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23