26 #define buffsize_mb 0.5
27 #define primary_hdr_size 6
29 #define basename(s) (strrchr((s), '/') == NULL ? (s) : strrchr((s), '/') + 1)
31 int main(
int argc,
char *argv[]) {
35 int packet_length = 0;
40 int last_pkt_in_buffer;
45 int packet_written = 0;
48 static int pkt_len_off = 4;
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];
63 double gran_length = 300;
65 char utc_str[28], utc_str2[28];
66 unsigned char *buffer, outname[384];
71 printf(
"%s %s (%s %s)\n\n", argv[0],
"1.x", __DATE__, __TIME__);
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");
87 stream = fopen(argv[1],
"r");
89 printf(
"%s not found.\n", argv[1]);
92 fseek(stream, (
long) 0, SEEK_SET);
95 buffer = (
unsigned char *) malloc(buffsize *
sizeof (
unsigned char));
97 fseek(stream, (
long) 0, SEEK_SET);
101 if (argc >= 3) gran_length = atof(argv[2]);
102 printf(
"Granule Length: %f\n", gran_length);
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);
110 if (argc == 4)
strcpy(utc_str2, argv[3]);
111 printf(
"UTC start time: %s\n", utc_str2);
113 PGS_TD_UTCtoTAI(utc_str2, &taitime0);
116 strcpy((
char*) outname, cptr3);
129 PGS_TD_ASCIItime_AtoB(utc_str2, utc_str);
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]);
140 outfp = fopen((
char*) outname,
"a");
141 fseek(outfp, 0, SEEK_SET);
143 last_pkt_in_file = L0_false;
144 found_start_time = L0_false;
146 while (last_pkt_in_file == L0_false) {
147 last_pkt_in_buffer = L0_false;
149 read_cnt = fread(buffer,
sizeof (
char), buffsize, stream);
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;
158 }
else if ((bytes_left == night_pkt_size) ||
159 (bytes_left == 2 * night_pkt_size)) {
161 last_pkt_in_buffer = L0_true;
162 fseek(stream, strm_pos, SEEK_SET);
167 packet_length = buffer[buf_pos + (pkt_len_off)]*256 +
168 buffer[buf_pos + pkt_len_off + 1];
173 if ((packet_length != 642) && (packet_length != 276)) {
175 printf(
"Packet with invalid length found %d %d\n",
176 n_packets, packet_length);
186 if (packet_length == 642) n_day++;
187 if (packet_length == 276) n_night++;
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;
193 memcpy(stop_time_tag, &buffer[buf_pos_last + time_off], time_cnt);
194 PGS_TD_EOSAMtoTAI(stop_time_tag, &taitime_stop);
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) {
204 fwrite(&buffer[buf_pos], packet_length, 1, outfp);
209 if (taitime >= taitime0 + gran_length) {
210 if (packet_written == 1) printf(
"Writing L0 file: %s\n", outname);
213 taitime0 += gran_length;
216 strcpy((
char*) outname, cptr3);
229 PGS_TD_TAItoUTC(taitime0 + 1, utc_str2);
230 PGS_TD_ASCIItime_AtoB(utc_str2, utc_str);
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]);
241 outfp = fopen((
char*) outname,
"a");
242 fseek(outfp, 0, SEEK_SET);
247 strm_pos += (long) packet_length;
248 buf_pos_last = buf_pos;
249 buf_pos += packet_length;
251 if ((n_packets % 100000) == 0) {
252 printf(
"%10d packets read %f %f\n", n_packets, taitime, taitime0);
256 if (feof(stream) != 0) {
257 last_pkt_in_file = L0_true;
260 if (ferror(stream) != 0) {
262 fseek(stream, (
long) 0, SEEK_SET);