OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l0fix_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 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 
29 #define buffsize_mb 0.5
30 #define primary_hdr_size 6
31 
32 int main(int argc, char *argv[]) {
33  int n_packets = 0;
34  int read_cnt;
35  long strm_pos = 0;
36  int packet_length = 0;
37  int buf_pos = 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 write;
44  int n_night = 0;
45  int n_day = 0;
46  struct stat filestat;
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 time_tag[8];
56 
57  double taitime_start;
58  double taitime_stop;
59  double taitime_tag;
60  double taitime_good;
61  double eps = 1e-6;
62  size_t status;
63 
64  unsigned char *buffer;
65  FILE *stream, *stream_w = NULL;
66 
67  /*
68  1) Get start and stop times
69 
70  ./fix_L0 L0file -1 -1
71 
72  2) fix L0
73 
74  ./fix_L0 L0file starttime stoptime outputL0
75  */
76 
77  printf("fix_L0: Version as of 07/16/07\n\n");
78 
79  stream = fopen(argv[1], "r");
80  if (stream == NULL) {
81  printf("%s not found.\n", argv[1]);
82  exit(-1);
83  }
84  fseek(stream, (long) 0, SEEK_SET);
85 
86  taitime_start = atof(argv[2]);
87  taitime_stop = atof(argv[3]);
88 
89  if (taitime_stop == -1) {
90  taitime_stop = 1e30;
91  } else {
92  stream_w = fopen(argv[4], "wb");
93  }
94 
95  buffsize = buffsize_mb * 1000000;
96  buffer = (unsigned char *) malloc(buffsize * sizeof (unsigned char));
97  if (buffer == NULL) {
98  fseek(stream, (long) 0, SEEK_SET);
99  return 1;
100  }
101 
102  last_pkt_in_file = L0_false;
103  found_start_time = L0_false;
104 
105  while (last_pkt_in_file == L0_false) {
106  last_pkt_in_buffer = L0_false;
107 
108  read_cnt = fread(buffer, sizeof (char), buffsize, stream);
109 
110  buf_pos = 0;
111  while (last_pkt_in_buffer == L0_false) {
112  bytes_left = read_cnt - buf_pos;
113  if (bytes_left < day_pkt_size) {
114  if (bytes_left == 0) {
115  last_pkt_in_buffer = L0_true;
116  continue;
117  } else if ((bytes_left == night_pkt_size) ||
118  (bytes_left == 2 * night_pkt_size)) {
119  } else if (feof(stream) == 0) {
120  last_pkt_in_buffer = L0_true;
121  fseek(stream, strm_pos, SEEK_SET);
122  continue;
123  } else {
124  free(buffer);
125 
126  printf("%d Total packets read\n", n_packets);
127  printf("taitime_stop: %f\n", taitime_good);
128 
129  if (stream_w != NULL) fclose(stream_w);
130  fclose(stream);
131 
132  if (atof(argv[2]) != -1) {
133  stat(argv[4], &filestat);
134  if (n_day * 642 + n_night * 276 != filestat.st_size) {
135  printf("output filesize discrepency.\n");
136  printf("Computed filesize: %d\n", n_day * 642 + n_night * 276);
137  printf("Actual filesize: %d\n", (int) filestat.st_size);
138  return 3;
139  }
140  }
141 
142  return 0;
143  }
144  }
145 
146  packet_length = buffer[buf_pos + (pkt_len_off)]*256 +
147  buffer[buf_pos + pkt_len_off + 1];
148  packet_length += 1;
149  packet_length += primary_hdr_size;
150 
151  write = 1;
152  if ((packet_length != 642) && (packet_length != 276)) {
153  if (n_packets > 0) {
154  printf("Packet with invalid length found %d %d\n",
155  n_packets, packet_length);
156  write = 0;
157  }
158  }
159 
160 
161  memcpy(time_tag, &buffer[buf_pos + time_off], time_cnt);
162  PGS_TD_EOSAMtoTAI(time_tag, &taitime_tag);
163 
164  // if (n_packets == 300000) printf("%f\n", taitime_tag);
165 
166  if (taitime_tag < taitime_start - eps || taitime_tag > taitime_stop + eps) {
167  printf("Bad L0 packet: %d %d\n",
168  taitime_tag < taitime_start,
169  taitime_tag > taitime_stop);
170  printf("Bad L0 packet: %f %f %f\n",
171  taitime_start, taitime_tag, taitime_stop);
172  write = 0;
173  }
174 
175  if (taitime_start == -1 && found_start_time == L0_false) {
176  printf("taitime_start: %f\n", taitime_tag);
177  found_start_time = L0_true;
178  }
179 
180  if (write && stream_w != NULL) {
181  status = fwrite(&buffer[buf_pos], packet_length, 1, stream_w);
182  if (status != 1) {
183  exit(4);
184  }
185  }
186 
187  if (write)
188  taitime_good = taitime_tag;
189 
190  strm_pos += (long) packet_length;
191  buf_pos += packet_length;
192  n_packets++;
193 
194  if (write) {
195  if (packet_length == 642) n_day++;
196  if (packet_length == 276) n_night++;
197  }
198 
199  if ((n_packets % 100000) == 0) {
200  printf("%10d packets read\n", n_packets);
201  }
202 
203  }
204 
205  if (feof(stream) != 0) {
206  last_pkt_in_file = L0_true;
207  }
208 
209  if (ferror(stream) != 0) {
210  free(buffer);
211  fseek(stream, (long) 0, SEEK_SET);
212  return 2;
213  }
214  }
215  free(buffer);
216 
217  printf("%d Total packets read\n", n_packets);
218  printf("taitime_stop: %f\n", taitime_good);
219 
220  if (stream_w != NULL) fclose(stream_w);
221  fclose(stream);
222 
223  if (atof(argv[2]) != -1) {
224  stat(argv[4], &filestat);
225  if (n_day * 642 + n_night * 276 != filestat.st_size) {
226  printf("output filesize discrepency.\n");
227  printf("Computed filesize: %d\n", n_day * 642 + n_night * 276);
228  printf("Actual filesize: %d\n", (int) filestat.st_size);
229  return 3;
230  }
231  }
232 
233  return 0;
234 }
int status
Definition: l1_czcs_hdf.c:32
#define NULL
Definition: decode_rs.h:63
#define buffsize_mb
Definition: l0fix_modis.c:29
int main(int argc, char *argv[])
Definition: l0fix_modis.c:32
#define primary_hdr_size
Definition: l0fix_modis.c:30