MISR Toolkit  1.5.1
MtkUtcJdToUtc.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkUtcJdToUtc =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2006, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrUtil.h"
18 #include "MisrError.h"
19 #include <math.h>
20 
21 static double* JulianDateSplit(
22  double inputJD[2],
23  double outputJD[2] )
24 {
25  double remainder;
26  double temp;
27 
28  outputJD[0] = inputJD[0];
29  outputJD[1] = inputJD[1];
30 
31  /* the Julian date must be of the form:
32  outputJD[0] = (Julian Day #) + 0.5
33  outputJD[1] = Julian Day fraction (0 <= outputJD[1] < 1) */
34 
35  /* Make sure outputJD[0] is greater in magnitude than outputJD[1] */
36 
37  if (fabs(outputJD[1]) > fabs(outputJD[0]))
38  {
39  temp = outputJD[0];
40  outputJD[0] = outputJD[1];
41  outputJD[1] = temp;
42  }
43 
44  /* Make sure outputJD[0] is half integral */
45 
46  if ((remainder=fmod(outputJD[0],1.0)) != 0.5)
47  {
48  outputJD[0] = outputJD[0] - remainder + 0.5;
49  outputJD[1] = outputJD[1] + remainder - 0.5;
50  }
51 
52  /* Make sure magnitude of outputJD[1] is less than 1.0 */
53  if (fabs(outputJD[1]) >= 1.0)
54  {
55  remainder=fmod(outputJD[1],1.0);
56  outputJD[0] += outputJD[1] - remainder;
57  outputJD[1] = remainder;
58  }
59 
60  /* Make sure outputJD[1] is greater than or equal to 0.0 */
61 
62  if (outputJD[1] < 0)
63  {
64  if ((outputJD[1] + 1.0) < 1.0)
65  {
66  outputJD[0] -= 1.0;
67  outputJD[1] += 1.0;
68  }
69  else
70  outputJD[1] = 0.0;
71  }
72 
73  return outputJD;
74 }
75 
76 static void calday( /* converts Julian day to calendar components */
77  int julianDayNum, /* Julian day */
78  int *year, /* calendar year */
79  int *month, /* calendar month */
80  int *day) /* calendar day (of month) */
81 {
82  long l; /* intermediate variable */
83  long n; /* intermediate variable */
84 
85  l = julianDayNum + 68569;
86  n = 4 * l / 146097;
87  l = l - (146097 * n + 3) / 4;
88  *year = (int)(4000 * (l + 1) / 1461001);
89  l = l - 1461 * (*year) / 4 + 31;
90  *month = (int)(80 * l / 2447L);
91  *day = (int)(l - 2447 * (*month) / 80);
92  l = *month / 11;
93  *month = (int)(*month + 2 - 12 * l);
94  *year = (int)(100 * (n - 49) + *year + l);
95 
96  return;
97 }
98 
99 
113  double jdUTCin[2],
114  char utc_datetime[MTKd_DATETIME_LEN] )
115 {
116  double jdUTC[2];
117  int year; /* year portion of date */
118  int month; /* month portion of date */
119  int day; /* day portion of date */
120  int hours; /* hour of day */
121  int minutes; /* minute of hour */
122  double seconds; /* seconds of minute */
123  int intSecs; /* integer number of UTC seconds of latest minute */
124  int fracSecs; /* integer number of microseconds of latest second */
125  double dayFractionSecs;
126 
127  JulianDateSplit(jdUTCin,jdUTC);
128 
129  calday((int)(jdUTC[0] + 0.5), &year, &month, &day);
130 
131  dayFractionSecs = jdUTC[1] * SECONDSperDAY + 5.0e-7;
132 
133  hours = (int) (dayFractionSecs / 3600.0);
134  minutes = (int) ((dayFractionSecs - hours * 3600.0) / 60.0);
135  seconds = (dayFractionSecs - hours * 3600.0 - minutes * 60.0);
136  intSecs = (int) seconds;
137  fracSecs = (int) (fmod(dayFractionSecs,1.0) * 1.0e6);
138 
139  /* sometimes jdUTC[1] is right on the edge and is just shy of 1 when it
140  should be 1 and "tip over" into the next day */
141 
142  if (hours == 24)
143  {
144  hours = 0;
145  calday((int)(jdUTC[0] + 1.5), &year, &month, &day);
146  }
147 
148  sprintf(utc_datetime,"%04d-%02d-%02dT%02d:%02d:%02d.%06dZ", (int)year,
149  (int)month, (int)day, hours, minutes, intSecs, fracSecs);
150 
151  return MTK_SUCCESS;
152 }
#define SECONDSperDAY
Definition: MisrUtil.h:145
static void calday(int julianDayNum, int *year, int *month, int *day)
Definition: MtkUtcJdToUtc.c:76
HDFFCLIBAPI _fcd _fcd intf * n
MTKt_status
Definition: MisrError.h:11
MTKt_status MtkUtcJdToUtc(double jdUTCin[2], char utc_datetime[MTKd_DATETIME_LEN])
Convert calendar date to Julian date.
#define MTKd_DATETIME_LEN
Definition: MisrUtil.h:149
static double * JulianDateSplit(double inputJD[2], double outputJD[2])
Definition: MtkUtcJdToUtc.c:21

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:52