OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l0chunk_modis.c
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 Copyright (C) 2000, Space Science and Engineering Center, University
3 of Wisconsin-Madison, Madison WI.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ------------------------------------------------------------------------*/
19 
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <PGS_TD.h>
25 
26 #define buffsize_mb 0.5
27 #define primary_hdr_size 6
28 
29 #define basename(s) (strrchr((s), '/') == NULL ? (s) : strrchr((s), '/') + 1)
30 
31 int main(int argc, char *argv[]) {
32  int n_packets = 0;
33  int read_cnt;
34  long strm_pos = 0;
35  int packet_length = 0;
36  int buf_pos = 0;
37  int buf_pos_last = 0;
38  int bytes_left;
39  int last_pkt_in_file;
40  int last_pkt_in_buffer;
41  int found_start_time;
42  int buffsize;
43  int n_night = 0;
44  int n_day = 0;
45  int packet_written = 0;
46  int write;
47 
48  static int pkt_len_off = 4;
49  static int time_off = primary_hdr_size;
50  static int time_cnt = 8;
51  static int L0_true = 0;
52  static int L0_false = 1;
53  static int day_pkt_size = 642;
54  static int night_pkt_size = 276;
55  static unsigned char start_time_tag[8];
56  static unsigned char stop_time_tag[8];
57  static unsigned char time_tag[8];
58  char *cptr3;
59 
60  double taitime_stop;
61  double taitime0;
62  double taitime;
63  double gran_length = 300;
64 
65  char utc_str[28], utc_str2[28];
66  unsigned char *buffer, outname[384];
67  FILE *stream, *outfp;
68 
69 
70 
71  printf("%s %s (%s %s)\n\n", argv[0], "1.x", __DATE__, __TIME__);
72 
73  if (argc == 1) {
74  printf("USAGE: %s MODIS_L0_PDS_file [granule_length] [zulu_start_time]\n\n", argv[0]);
75  printf("granule_length: Time in seconds of each generated granule. (default = 300 sec)\n\n");
76  printf("zulu_start_time: Time of the first packet included in the granule(s). All packets\n");
77  printf(" before this time will be ignored. All packets after this time\n");
78  printf(" will be included in the output L0 granule(s). If zulu_start_time\n");
79  printf(" is not set, the first output granule's filename will be set to the\n");
80  printf(" rounded 5 minute interval preceding the time of the first packet.\n");
81  printf(" zulu_start_time example: 2006-06-12T16:50:00.00000\n");
82  printf(" (default = start time of first packet in L0 granule)\n\n");
83 
84  exit(1);
85  }
86 
87  stream = fopen(argv[1], "r");
88  if (stream == NULL) {
89  printf("%s not found.\n", argv[1]);
90  exit(1);
91  }
92  fseek(stream, (long) 0, SEEK_SET);
93 
94  buffsize = buffsize_mb * 1000000;
95  buffer = (unsigned char *) malloc(buffsize * sizeof (unsigned char));
96  if (buffer == NULL) {
97  fseek(stream, (long) 0, SEEK_SET);
98  return -1;
99  }
100 
101  if (argc >= 3) gran_length = atof(argv[2]);
102  printf("Granule Length: %f\n", gran_length);
103 
104  read_cnt = fread(buffer, sizeof (char), time_off + time_cnt, stream);
105  PGS_TD_EOSAMtoTAI(&buffer[time_off], &taitime0);
106  PGS_TD_TAItoUTC(taitime0, utc_str2);
107  strcpy(&utc_str2[15], "0:00.000000Z");
108  fseek(stream, (long) 0, SEEK_SET);
109 
110  if (argc == 4) strcpy(utc_str2, argv[3]);
111  printf("UTC start time: %s\n", utc_str2);
112 
113  PGS_TD_UTCtoTAI(utc_str2, &taitime0);
114 
115  cptr3 = basename(argv[1]);
116  strcpy((char*) outname, cptr3);
117  /*
118  cptr3++;
119  if (*cptr3 == '1')
120  strcpy(outname, "MOD00.P");
121  else if (*cptr3 == '0')
122  strcpy(outname, "MOD00.A");
123  else {
124  printf("Satellite Type cannot be determined\n");
125  return -1;
126  }
127  */
128 
129  PGS_TD_ASCIItime_AtoB(utc_str2, utc_str);
130  utc_str[4] = 0;
131  utc_str[8] = 0;
132  utc_str[11] = 0;
133  utc_str[14] = 0;
134  strcat((char*) outname, "_");
135  strcat((char*) outname, &utc_str[0]);
136  strcat((char*) outname, &utc_str[5]);
137  strcat((char*) outname, &utc_str[9]);
138  strcat((char*) outname, &utc_str[12]);
139  /* strcat(outname, ".pds"); */
140  outfp = fopen((char*) outname, "a");
141  fseek(outfp, 0, SEEK_SET);
142 
143  last_pkt_in_file = L0_false;
144  found_start_time = L0_false;
145 
146  while (last_pkt_in_file == L0_false) {
147  last_pkt_in_buffer = L0_false;
148 
149  read_cnt = fread(buffer, sizeof (char), buffsize, stream);
150 
151  buf_pos = 0;
152  while (last_pkt_in_buffer == L0_false) {
153  bytes_left = read_cnt - buf_pos;
154  if (bytes_left < day_pkt_size) {
155  if (bytes_left == 0) {
156  last_pkt_in_buffer = L0_true;
157  continue;
158  } else if ((bytes_left == night_pkt_size) ||
159  (bytes_left == 2 * night_pkt_size)) {
160  } else {
161  last_pkt_in_buffer = L0_true;
162  fseek(stream, strm_pos, SEEK_SET);
163  continue;
164  }
165  }
166 
167  packet_length = buffer[buf_pos + (pkt_len_off)]*256 +
168  buffer[buf_pos + pkt_len_off + 1];
169  packet_length += 1;
170  packet_length += primary_hdr_size;
171 
172  write = 1;
173  if ((packet_length != 642) && (packet_length != 276)) {
174  if (n_packets > 0) {
175  printf("Packet with invalid length found %d %d\n",
176  n_packets, packet_length);
177  write = 0;
178  }
179  /*
180  free( buffer );
181  fseek( stream, (long) 0, SEEK_SET);
182  return -3;
183  */
184  }
185 
186  if (packet_length == 642) n_day++;
187  if (packet_length == 276) n_night++;
188 
189  if (found_start_time == L0_false) {
190  memcpy(start_time_tag, &buffer[buf_pos + time_off], time_cnt);
191  found_start_time = L0_true;
192  } else {
193  memcpy(stop_time_tag, &buffer[buf_pos_last + time_off], time_cnt);
194  PGS_TD_EOSAMtoTAI(stop_time_tag, &taitime_stop);
195  /* PGS_TD_TAItoUTC( taitime_stop, outname);*/
196  /*printf("stoptime =%s\n", outname);*/
197  }
198 
199  /* write packet if within time period */
200  memcpy(time_tag, &buffer[buf_pos + time_off], time_cnt);
201  PGS_TD_EOSAMtoTAI(time_tag, &taitime);
202  if (taitime >= taitime0 && taitime < taitime0 + gran_length) {
203  if (write) {
204  fwrite(&buffer[buf_pos], packet_length, 1, outfp);
205  packet_written = 1;
206  }
207  }
208 
209  if (taitime >= taitime0 + gran_length) {
210  if (packet_written == 1) printf("Writing L0 file: %s\n", outname);
211  fclose(outfp);
212 
213  taitime0 += gran_length;
214 
215  cptr3 = basename(argv[1]);
216  strcpy((char*) outname, cptr3);
217  /*
218  cptr3++;
219  if (*cptr3 == '1')
220  strcpy(outname, "MOD00.P");
221  else if (*cptr3 == '0')
222  strcpy(outname, "MOD00.A");
223  else {
224  printf("Satellite Type cannot be determined\n");
225  return -1;
226  }
227  */
228 
229  PGS_TD_TAItoUTC(taitime0 + 1, utc_str2);
230  PGS_TD_ASCIItime_AtoB(utc_str2, utc_str);
231  utc_str[4] = 0;
232  utc_str[8] = 0;
233  utc_str[11] = 0;
234  utc_str[14] = 0;
235  strcat((char*) outname, "_");
236  strcat((char*) outname, &utc_str[0]);
237  strcat((char*) outname, &utc_str[5]);
238  strcat((char*) outname, &utc_str[9]);
239  strcat((char*) outname, &utc_str[12]);
240  /* strcat(outname, ".pds"); */
241  outfp = fopen((char*) outname, "a");
242  fseek(outfp, 0, SEEK_SET);
243 
244  packet_written = 0;
245  }
246 
247  strm_pos += (long) packet_length;
248  buf_pos_last = buf_pos;
249  buf_pos += packet_length;
250  n_packets++;
251  if ((n_packets % 100000) == 0) {
252  printf("%10d packets read %f %f\n", n_packets, taitime, taitime0);
253  }
254  }
255 
256  if (feof(stream) != 0) {
257  last_pkt_in_file = L0_true;
258  }
259 
260  if (ferror(stream) != 0) {
261  free(buffer);
262  fseek(stream, (long) 0, SEEK_SET);
263  return -2;
264  }
265  }
266 
267  free(buffer);
268 
269  fclose(stream);
270  fclose(outfp);
271 
272  return 0;
273 }
#define primary_hdr_size
Definition: l0chunk_modis.c:27
int main(int argc, char *argv[])
Definition: l0chunk_modis.c:31
#define buffsize_mb
Definition: l0chunk_modis.c:26
#define NULL
Definition: decode_rs.h:63
#define basename(s)
Definition: l0chunk_modis.c:29
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")