OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
modis_timestamp.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <string.h>
6 
7 int main(int argc, char *argv[]) {
8  int len;
9  char *ifile = NULL;
10  FILE *fp = NULL;
11  FILE *fptr = NULL;
12  FILE *testptr = NULL;
13  char ncdump[500000] = "";
14  char tmpstr[1000] = "";
15  char cmd_string[250] = "";
16  char *ptr = NULL;
17  char range_date[11] = "";
18  char range_time[9] = "";
19  char platform[4] = "";
20  char first_letter;
21  char year[5] = "";
22  char month[3] = "";
23  char day[3] = "";
24  int year_int;
25  int day_int;
26  int mod;
27  int add_leap_day;
28  int basedays = 0;
29  int julian_days;
30  char julian_str[4] = "";
31  char jul_str[4] = "";
32  char hour[3] = "";
33  char min[3] = "";
34  char sec[3] = "";
35  char date_stamp[15] = "";
36  char *seadas = NULL;
37 
38  if (argc == 1) {
39  printf("\nUSAGE: %s MODIS_L1A_L1B_or_GEO_file [start|stop]\n", argv[0]);
40  exit(1);
41  }
42  if (!(fptr = fopen(argv[1], "r"))) {
43  perror(argv[1]);
44  exit(1);
45  }
46  fclose(fptr);
47 
48  len = strlen(argv[1]);
49  ifile = malloc(1 + (len * sizeof (char)));
50  strncpy(ifile, argv[1], len);
51 
52  seadas = getenv("OCSSWROOT");
53  if (seadas == NULL) {
54  strcat(cmd_string, "ncdump");
55  } else {
56  strcpy(cmd_string, seadas);
57  strcat(cmd_string, "/run/bin3/ncdump");
58  if (!(testptr = fopen(cmd_string, "r"))) {
59  strcpy(cmd_string, seadas);
60  strcat(cmd_string, "/bin/ncdump");
61  if (!(testptr = fopen(cmd_string, "r"))) {
62  printf("modis_timestamp: Error - ncdump must exist in $SEADAS/run/bin3/ or $SEADAS/bin/ to run this program.\n");
63  exit(1);
64  }
65  }
66  }
67 
68  strcat(cmd_string, " -h ");
69  strcat(cmd_string, ifile);
70 
71  fp = popen(cmd_string, "r");
72  while (fgets(tmpstr, sizeof tmpstr, fp)) {
73  strcat(ncdump, tmpstr);
74  }
75  pclose(fp);
76 
77  /* find the date */
78  if (argc == 2) argv[2] = argv[1]; /* HACK - do this to avoid bus error */
79  if (strcmp(argv[2], "stop") == 0) ptr = strstr(ncdump, "RANGEENDINGDATE");
80  else ptr = strstr(ncdump, "RANGEBEGINNINGDATE");
81  if (ptr == NULL) {
82  printf("modis_timestamp: Error - File doesn't appear to have a valid MODIS HDF header.\n");
83  exit(1);
84  }
85  ptr = strstr(ptr, "VALUE");
86  if (ptr == NULL) {
87  printf("modis_timestamp: Error - File doesn't appear to have a valid MODIS HDF header.\n");
88  exit(1);
89  }
90  strncpy(range_date, ptr + 25, 10);
91  range_date[10] = '\0';
92 
93  /* find the time */
94  if (strcmp(argv[2], "stop") == 0) ptr = strstr(ncdump, "RANGEENDINGTIME");
95  else ptr = strstr(ncdump, "RANGEBEGINNINGTIME");
96  if (ptr == NULL) {
97  printf("modis_timestamp: Error - File doesn't appear to have a valid MODIS HDF header.\n");
98  exit(1);
99  }
100  ptr = strstr(ptr, "VALUE");
101  if (ptr == NULL) {
102  printf("modis_timestamp: Error - File doesn't appear to have a valid MODIS HDF header.\n");
103  exit(1);
104  }
105  strncpy(range_time, ptr + 25, 8);
106  range_time[8] = '\0';
107 
108  /* find out if it's aqua or terra */
109  ptr = strstr(ncdump, " SHORTNAME");
110  if (ptr == NULL) {
111  printf("modis_timestamp: Error - File doesn't appear to have a valid MODIS HDF header.\n");
112  exit(1);
113  }
114  ptr = strstr(ptr, "VALUE");
115  if (ptr == NULL) {
116  printf("modis_timestamp: Error - File doesn't appear to have a valid MODIS HDF header.\n");
117  exit(1);
118  }
119  strncpy(platform, ptr + 25, 3);
120  platform[3] = '\0';
121 
122  /* build the return string, like AYYYYDDDHHMMSS */
123  if (strcmp(platform, "MYD") == 0) {
124  first_letter = 'A';
125  } else if (strcmp(platform, "MOD") == 0) {
126  first_letter = 'T';
127  } else {
128  printf("modis_timestamp: Error - Could not determine platform.\n");
129  exit(1);
130  }
131 
132  strncpy(year, &range_date[0], 4);
133  year[4] = '\0';
134  strncpy(month, &range_date[5], 2);
135  month[2] = '\0';
136  strncpy(day, &range_date[8], 2);
137  day[2] = '\0';
138 
139  /* convert year string into integer for leap calc */
140  sscanf(year, "%d", &year_int);
141  mod = year_int % 4;
142  if (mod == 0) {
143  add_leap_day = 1;
144  } else {
145  add_leap_day = 0;
146  }
147 
148  sscanf(day, "%d", &day_int);
149 
150  if (strcmp(month, "01") == 0)
151  basedays = 0;
152  else if (strcmp(month, "02") == 0)
153  basedays = 31;
154  else if (strcmp(month, "03") == 0)
155  basedays = 59 + add_leap_day;
156  else if (strcmp(month, "04") == 0)
157  basedays = 90 + add_leap_day;
158  else if (strcmp(month, "05") == 0)
159  basedays = 120 + add_leap_day;
160  else if (strcmp(month, "06") == 0)
161  basedays = 151 + add_leap_day;
162  else if (strcmp(month, "07") == 0)
163  basedays = 181 + add_leap_day;
164  else if (strcmp(month, "08") == 0)
165  basedays = 212 + add_leap_day;
166  else if (strcmp(month, "09") == 0)
167  basedays = 243 + add_leap_day;
168  else if (strcmp(month, "10") == 0)
169  basedays = 273 + add_leap_day;
170  else if (strcmp(month, "11") == 0)
171  basedays = 304 + add_leap_day;
172  else if (strcmp(month, "12") == 0)
173  basedays = 334 + add_leap_day;
174 
175  julian_days = basedays + day_int;
176 
177  if (julian_days < 10) {
178  strcpy(julian_str, "00\0");
179  } else if (julian_days < 100) {
180  strcpy(julian_str, "0\0");
181  } else {
182  strcpy(julian_str, "\0");
183  }
184 
185  sprintf(jul_str, "%d", julian_days);
186  strcat(julian_str, jul_str);
187 
188  strncpy(hour, range_time, 2);
189  hour[2] = '\0';
190  ptr = &range_time[3];
191  strncpy(min, ptr, 2);
192  min[2] = '\0';
193  ptr = &range_time[6];
194  strncpy(sec, ptr, 2);
195  sec[2] = '\0';
196 
197  /* put it all together */
198  date_stamp[0] = first_letter;
199  date_stamp[1] = '\0';
200  strcat(date_stamp, year);
201  strcat(date_stamp, julian_str);
202  strcat(date_stamp, hour);
203  strcat(date_stamp, min);
204  strcat(date_stamp, sec);
205  date_stamp[14] = '\0';
206 
207  printf("%s", date_stamp);
208 
209  exit(0);
210 }
int32_t day
These are used to scale the SD before writing it to the HDF4 file The default is and which means the product is not scaled at all Since the product is usually stored as a float inside of this is a way to write the float out as a integer l2prod min
int main(int argc, char *argv[])
FILE * popen(const char *, const char *)
#define NULL
Definition: decode_rs.h:63
int pclose(FILE *)
PARAM_TYPE_NONE Default value No parameter is buried in the product name name_prefix is case insensitive string compared to the product name PARAM_TYPE_VIS_WAVE The visible wavelength bands from the sensor are buried in the product name The product name is compared by appending and name_suffix ie aph_412_giop where prod_ix will be set to PARAM_TYPE_IR_WAVE same search method as PARAM_TYPE_VIS_WAVE except only wavelength above are looped through but prod_ix is still based ie aph_2_giop for the second and prod_ix set to PARAM_TYPE_INT name_prefix is compared with the beginning of the product name If name_suffix is not empty the it must match the end of the product name The characters right after the prefix are read as an integer and prod_ix is set to that number strncpy(l2prod->name_prefix, "myprod", UNITLEN)
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")