OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
main_gpsextract.c
Go to the documentation of this file.
1 /* ==================================================================== */
2 /* */
3 /* cut_elements - clip time-range of elements.dat file of SWl01. */
4 /* */
5 /* Synopsis: */
6 /* */
7 /* cut_elements [-n num_days] infile outfile */
8 /* */
9 /* Description: */
10 /* */
11 /* */
12 /* Written By: */
13 /* */
14 /* Bryan A. Franz */
15 /* General Sciences Corp. */
16 /* 8 March 2000 */
17 /* */
18 /* =====================================================================*/
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include "genutils.h"
25 #include "elements.h"
26 #include <string.h>
27 #include <timeutils.h>
28 
29 #define CMD_ARGS "y:d:n:"
30 
31 void usage(char *file) {
32  printf("Usage: %s [-%s] input-elements-file output-elements-file\n",
33  file, CMD_ARGS);
34  printf(" -n n : number of days to include. [default=1]\n");
35 
36  exit(1);
37 }
38 
39 
40 /* -------------------------------------------------------------------- */
41 /* main */
42 
43 /* -------------------------------------------------------------------- */
44 int main(int argc, char* argv[]) {
45 
46  INT32 ndays = 1; /* Number of days to retain */
47  char *ifile = NULL; /* Input elements file */
48  char *ofile = NULL; /* Output elements file */
49  FILE *ifp;
50  FILE *ofp;
51  hdrstr ihdr;
52  hdrstr ohdr;
53  elmstr selm;
54  elmstr elm;
55  INT32 pos;
56  FLOAT64 utime;
57  FLOAT64 stime;
58  INT32 irec;
59  INT32 orec = 0;
60  INT32 srec = -1;
61 
62  INT32 nrecs;
63  INT32 year;
64  INT32 day;
65  FLOAT64 sec;
66 
67  /* Parameters for getopt() */
68  extern int opterr;
69  extern int optind;
70  extern char *optarg;
71  int c;
72 
73 
74  /* */
75  /* Process command-line arguments */
76  /* */
77  while ((c = getopt(argc, argv, CMD_ARGS)) != EOF) {
78  switch (c) {
79  case 'n':
80  ndays = atoi(optarg);
81  break;
82  default:
83  usage(argv[0]);
84  break;
85  }
86  }
87  switch (argc - optind + 1) {
88  case 3:
89  ifile = argv[optind + 0];
90  ofile = argv[optind + 1];
91  break;
92  default:
93  usage(argv[0]);
94  break;
95  }
96 
97  /* */
98  /* Open file for reading */
99  /* */
100  if ((ifp = fopen(ifile, "r")) == NULL) {
101  fprintf(stderr,
102  "-E- %s line %d: unable to open %s for reading\n",
103  __FILE__, __LINE__, ifile);
104  exit(1);
105  }
106 
107  /* */
108  /* Open file for writing */
109  /* */
110  if ((ofp = fopen(ofile, "w")) == NULL) {
111  fprintf(stderr,
112  "-E- %s line %d: unable to open %s for writing\n",
113  __FILE__, __LINE__, ofile);
114  exit(1);
115  }
116 
117  /* */
118  /* Read input header */
119  /* */
120  if (fread(&ihdr, sizeof (ihdr), 1, ifp) != 1) {
121  fprintf(stderr,
122  "-E- %s line %d: error reading %s\n",
123  __FILE__, __LINE__, ifile);
124  exit(1);
125  }
126 
127  /* */
128  /* Now, we read the last record, determine the end date, and from */
129  /* that and ndays we compute the start date. */
130  /* */
131  nrecs = ihdr.nrecs;
132  if (endianess() == 1)
133  swapc_bytes((char *) &nrecs, 4, 1);
134  pos = sizeof (elm)*(nrecs - 1);
135  if (fseek(ifp, pos, SEEK_SET) != 0) {
136  fprintf(stderr,
137  "-E- %s line %d: error reading %s\n",
138  __FILE__, __LINE__, ifile);
139  exit(1);
140  }
141  if (fread(&elm, sizeof (elm), 1, ifp) != 1) {
142  fprintf(stderr,
143  "-E- %s line %d: error reading %s\n",
144  __FILE__, __LINE__, ifile);
145  exit(1);
146  }
147 
148  year = elm.year;
149  day = elm.day;
150  sec = elm.sec;
151  if (endianess() == 1) {
152  swapc_bytes((char *) &year, 4, 1);
153  swapc_bytes((char *) &day, 4, 1);
154  swapc_bytes((char *) &sec, 8, 1);
155  }
156 
157  utime = yds2unix(year, day, sec);
158  stime = utime - ndays * 3600.0 * 24.0;
159 
160  /* */
161  /* Write a dummy header, then read input and write desired output */
162  /* */
163  if (fwrite(&ohdr, sizeof (ohdr), 1, ofp) != 1) {
164  fprintf(stderr,
165  "-E- %s line %d: error writing %s\n",
166  __FILE__, __LINE__, ofile);
167  exit(1);
168  }
169  orec++;
170 
171  if (fseek(ifp, sizeof (ihdr), SEEK_SET) != 0) {
172  fprintf(stderr,
173  "-E- %s line %d: error reading %s\n",
174  __FILE__, __LINE__, ifile);
175  exit(1);
176  }
177 
178  for (irec = 1; irec < nrecs; irec++) {
179  if (fread(&elm, sizeof (elm), 1, ifp) != 1) {
180  fprintf(stderr,
181  "-E- %s line %d: error reading %s\n",
182  __FILE__, __LINE__, ifile);
183  exit(1);
184  }
185 
186  year = elm.year;
187  day = elm.day;
188  sec = elm.sec;
189  if (endianess() == 1) {
190  swapc_bytes((char *) &year, 4, 1);
191  swapc_bytes((char *) &day, 4, 1);
192  swapc_bytes((char *) &sec, 8, 1);
193  }
194  utime = yds2unix(year, day, sec);
195  if (utime > stime) {
196  if (fwrite(&elm, sizeof (elm), 1, ofp) != 1) {
197  fprintf(stderr,
198  "-E- %s line %d: error writing %s\n",
199  __FILE__, __LINE__, ofile);
200  exit(1);
201  }
202  orec++;
203  if (srec < 0) {
204  srec = irec;
205  memcpy(&selm, &elm, sizeof (elm));
206  }
207  }
208  }
209 
210  /* */
211  /* Write final header */
212  /* */
213  nrecs = orec;
214  if (endianess() == 1) {
215  swapc_bytes((char *) &orec, 4, 1);
216  }
217  ohdr.nrecs = orec;
218  if (fseek(ofp, 0, SEEK_SET) != 0) {
219  fprintf(stderr,
220  "-E- %s line %d: error reading %s\n",
221  __FILE__, __LINE__, ifile);
222  exit(1);
223  }
224  if (fwrite(&ohdr, sizeof (ohdr), 1, ofp) != 1) {
225  fprintf(stderr,
226  "-E- %s line %d: error writing %s\n",
227  __FILE__, __LINE__, ofile);
228  exit(1);
229  }
230 
231 
232  printf("A total of %d records written to %s\n", nrecs, ofile);
233  year = selm.year;
234  day = selm.day;
235  sec = selm.sec;
236  if (endianess() == 1) {
237  swapc_bytes((char *) &year, 4, 1);
238  swapc_bytes((char *) &day, 4, 1);
239  swapc_bytes((char *) &sec, 8, 1);
240  }
241  printf("Start year, day, sec = %d, %d, %lf\n",
242  year, day, sec);
243  year = elm.year;
244  day = elm.day;
245  sec = elm.sec;
246  if (endianess() == 1) {
247  swapc_bytes((char *) &year, 4, 1);
248  swapc_bytes((char *) &day, 4, 1);
249  swapc_bytes((char *) &sec, 8, 1);
250  }
251  printf("End year, day, sec = %d, %d, %lf\n",
252  year, day, sec);
253 
254  exit(0);
255 
256 }
double FLOAT64
Definition: elements.h:8
int32_t day
double yds2unix(int16_t year, int16_t day, double secs)
Definition: yds2unix.c:7
#define NULL
Definition: decode_rs.h:63
int32_t INT32
Definition: elements.h:6
float32 * pos
Definition: l1_czcs_hdf.c:35
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed file
Definition: HISTORY.txt:413
int endianess(void)
determine endianess
Definition: endianess.c:10
int swapc_bytes(char *in, int nbyte, int ntime)
Definition: swapc_bytes.c:4
int main(int argc, char *argv[])
void usage(char *file)
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed as required for compatibility with version of the SDP toolkit Corrected test output file names to end in per delivery and then split off a new MYD_PR03 pcf file for Aqua Added AssociatedPlatformInstrumentSensor to the inventory metadata in MOD01 mcf and MOD03 mcf Created new versions named MYD01 mcf and MYD03 where AssociatedPlatformShortName is rather than Terra The program itself has been changed to read the Satellite Instrument validate it against the input L1A and LUT and to use it determine the correct files to retrieve the ephemeris and attitude data from Changed to produce a LocalGranuleID starting with MYD03 if run on Aqua data Added the Scan Type file attribute to the Geolocation copied from the L1A and attitude_angels to radians rather than degrees The accumulation of Cumulated gflags was moved from GEO_validate_earth_location c to GEO_locate_one_scan c
Definition: HISTORY.txt:464
#define CMD_ARGS