MISR Toolkit  1.5.1
MtkJulianToCal.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkJulianToCal =
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 #include <string.h>
21 
22 /* LEAP_GREGORIAN -- Is a given year in the Gregorian calendar a leap year ? */
23 
24 static int leap_gregorian(int year)
25 {
26  return ((year % 4) == 0) &&
27  (!(((year % 100) == 0) && ((year % 400) != 0)));
28 }
29 
30 /* GREGORIAN_TO_JD -- Determine Julian day number from Gregorian calendar date */
31 
32 #define GREGORIAN_EPOCH 1721425.5
33 
34 static double gregorian_to_jd(int year, int month, int day)
35 {
36  return (GREGORIAN_EPOCH - 1.0) +
37  (365.0 * (year - 1.0)) +
38  floor((year - 1.0) / 4.0) +
39  (-floor((year - 1.0) / 100.0)) +
40  floor((year - 1.0) / 400.0) +
41  floor((((367.0 * month) - 362.0) / 12.0) +
42  ((month <= 2) ? 0 :
43  (leap_gregorian(year) ? -1 : -2)
44  ) +
45  day);
46 }
47 
48 /* MOD -- Modulus function which works for non-integers. */
49 
50 static double mod(double a, double b)
51 {
52  return a - (b * floor(a / b));
53 }
54 
55 
56 /* JD_TO_GREGORIAN -- Calculate Gregorian calendar date from Julian day */
57 
58 static void jd_to_gregorian(double jd, int *y, int *m, int *d)
59 {
60  double wjd;
61  double depoch;
62  double quadricent;
63  double dqc;
64  int cent;
65  double dcent;
66  double quad;
67  double dquad;
68  int yindex;
69  int year;
70  int month;
71  int day;
72  double yearday;
73  int leapadj;
74 
75  wjd = floor(jd - 0.5) + 0.5;
76  depoch = wjd - GREGORIAN_EPOCH;
77  quadricent = floor(depoch / 146097);
78  dqc = mod(depoch, 146097);
79  cent = (int)floor(dqc / 36524);
80  dcent = mod(dqc, 36524);
81  quad = floor(dcent / 1461);
82  dquad = mod(dcent, 1461);
83  yindex = (int)floor(dquad / 365);
84  year = (int)((quadricent * 400) + (cent * 100) + (quad * 4) + yindex);
85  if (!((cent == 4) || (yindex == 4))) {
86  year++;
87  }
88  yearday = wjd - gregorian_to_jd(year, 1, 1);
89  leapadj = ((wjd < gregorian_to_jd(year, 3, 1)) ? 0 :
90  (leap_gregorian(year) ? 1 : 2));
91  month = (int)floor((((yearday + leapadj) * 12) + 373) / 367);
92  day = (int)((wjd - gregorian_to_jd(year, month, 1)) + 1);
93 
94  *y = year;
95  *m = month;
96  *d = day;
97 }
98 
99 /* JHMS -- Convert Julian time to hour, minutes, and seconds. */
100 
101 static void jhms(double j, int *hour, int *min, int *sec)
102 {
103  int ij;
104 
105  j += 0.5; /* Astronomical to civil */
106  ij = (int)(((j - floor(j)) * 86400.0) + 0.5);
107  *hour = (int)floor(ij / 3600);
108  *min = (int)floor((ij / 60) % 60);
109  *sec = (int)floor(ij % 60);
110 }
111 
127  double jd,
128  int *year,
129  int *month,
130  int *day,
131  int *hour,
132  int *min,
133  int *sec )
134 {
135  MTKt_status status_code; /* Return code of this function */
136 
137  if (year == NULL || month == NULL || day == NULL ||
138  hour == NULL || min == NULL || sec == NULL)
140 
141  if (jd < 1721119.5)
143 
144  jd_to_gregorian(jd,year,month,day);
145  jhms(jd,hour,min,sec);
146 
147  return MTK_SUCCESS;
148 
149 ERROR_HANDLE:
150  return status_code;
151 }
#define GREGORIAN_EPOCH
static void jd_to_gregorian(double jd, int *y, int *m, int *d)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
long b
Definition: jpegint.h:371
static double mod(double a, double b)
static int leap_gregorian(int year)
MTKt_status MtkJulianToCal(double jd, int *year, int *month, int *day, int *hour, int *min, int *sec)
Convert Julian date to calendar date.
static double gregorian_to_jd(int year, int month, int day)
static void jhms(double j, int *hour, int *min, int *sec)
MTKt_status
Definition: MisrError.h:11
HDFFCLIBAPI void * min

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