NASA Logo
Ocean Color Science Software

ocssw V2022
main_l2extract.c
Go to the documentation of this file.
1 /*
2  Modification history:
3  Programmer Organization Date Description of change
4  -------------- ------------ -------- ---------------------
5  Joel Gales Futuretech 08/10/03 Original Development
6 
7  */
8 
9 /*
10  Revision 2.0.2 09/13/22
11  Add support for CLO
12  B. Yang
13 
14  Revision 2.0.2 12/06/17
15  Add support for prodlist for netcdf4 files
16  J. Gales
17 
18  Revision 1.000 01/08/13
19  Don't read control points if lonlatinterp == 0
20  J. Gales
21 
22  Revision 0.993 05/21/10
23  Fix east/westmost lon for extracts crossing dateline
24  J. Gales
25 
26  Revision 0.992 09/26/08
27  Only print prodlist (arg 9) if it exists
28  J. Gales
29 
30  Revision 0.991 06/18/08
31  Don't extract on "pxls" dimention for OCTS
32  J. Gales
33 
34  Revision 0.990 12/05/06
35  Fix slat,clat,...,slon,clon,...,& lon/lat boundary metadata
36  J. Gales
37 
38  Revision 0.983 08/08/06
39  Make cntl_pt_cols 1-based
40  J. Gales
41 
42  Revision 0.982 06/15/06
43  Set octs only if Level 1
44  J. Gales
45 
46  Revision 0.981 06/01/06
47  Make sure sscan is odd and escan is even if OCTS
48  J. Gales
49 
50  Revision 0.98 05/30/06
51  Fix problem with OCTS L1A extraction
52  J. Gales
53  */
54 
55 
56 #include <stdio.h>
57 #include <math.h>
58 #include <time.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include "mfhdf.h"
62 #include <genutils.h>
63 
64 #include "l2extract.h"
65 #include "hdf.h"
66 #include <timeutils.h>
67 
69 
70 int main(int argc, char *argv[]) {
71  int32 i;
72  int32 j;
73  int32 k;
74  int32 n;
75  int32 jds;
76  int32 sds_id;
77  int32 dims[8];
78  int32 dims_w[256][8];
79  int32 dims_lonlat[2];
80  int32 rank;
81  int32 nelem;
82  int32 dtype;
83  int32 nattrs;
84  int32 status;
85  int32 LAC_start;
86  int32 i32;
87  int32 lonlatinterp = 1;
88 
89  int32 zero = 0;
90  // int32 one = 1;
91 
92  int32 vg_ref;
93  int32 vgid_r;
94  int32 vgid_w;
95  int32 tag;
96  int32 ref;
97  // int32 listlen = 0;
98  int32 npixl;
99  int32 nscans;
100  // int32 nread;
101  // int32 nwrite;
102  int32 sscan, escan, nscan;
103  int32 spix, epix;
104  // int32 maskout;
105  // int32 fldsize;
106  int32 octs = 0;
107  int32 seawifs_l1a = 0;
108 
109  int32 startyear = 0, cenyear = 0, endyear = 0;
110  int32 startday = 0, cenday = 0, endday = 0;
111  int32 startmsec = 0, cenmsec = 0, endmsec = 0;
112  float64 utime;
113 
114  float32 northern_lat = -90.0;
115  float32 southern_lat = +90.0;
116  float32 western_lon = +180.0;
117  float32 eastern_lon = -180.0;
118 
119  static char buffer[2048];
120  static char buf2[2048];
121  static char prodlist[2048];
122  static char geophyslist[2048];
123  static char wavelist[2048];
124 
125  static char fieldname[FIELDNAMELENMAX];
126  static char attrname[FIELDNAMELENMAX];
127  char *infile, *outfile; // , parm_list[1024];
128  char *prodlist_str = "";
129  char *waves_str = "";
130 
131  char *data;
132  char *data2;
133 
134  int32 HDFfid_r;
135  int32 sd_id_r;
136  int32 HDFfid_w;
137  int32 sd_id_w;
138  int32 sds_id_w;
139  int32 ndatasets;
140  int32 nglobal_attr;
141  int32 numPositionOptions;
142  int32 count;
143  int32 dim_id_r;
144  int32 dim_id_w;
145  int32 start[8] = {0, 0, 0, 0, 0, 0, 0, 0};
146  int32 edge[8] = {0, 0, 0, 0, 0, 0, 0, 0};
147  int32 start_w[8] = {0, 0, 0, 0, 0, 0, 0, 0};
148  // int32 maxsize;
149  int32 n_cntl_pnts;
150  int32 pix_sub, sc_sub;
151  float32 f32, delta;
152 
153  int32 ntilts;
154  int16 old_tilt_flags[20];
155  int16 old_tilt_ranges[20][2];
156  int16 new_tilt_flags[20];
157  int16 new_tilt_ranges[20][2];
158  int16 stilt, etilt;
159 
160  int32 nvgrp = 3;
161  char *vgrpname[] = {"Scan-Line Attributes", "Geophysical Data", "Navigation Data"};
162 
163  float32 lllon, lllat, lrlon, lrlat, ullon, ullat, urlon, urlat;
164  float32 sclon, sclat, cclon, cclat, eclon, eclat;
165  float32 csolz;
166 
167  char cntl_pt_cols_buf[4 * 4096];
168  char *ver = "3.1";
169  // char proc_con[2048];
170  char title[128];
171 
172  float32 f32_buf[4096];
173  float32 spline_arr[4096];
174 
175  uint8 datelinecross = 0;
176 
177  printf("This is version %s of %s (compiled on %s %s)\n", ver, "l2extract", __DATE__, __TIME__);
178 
179  /*** load input parameters into local variables */
180 
182 
184 
186  if (argc == 1) {
188  exit(EXIT_FAILURE);
189  }
190 
192  numPositionOptions = clo_getPositionNumOptions(optionList);
193 
194  if (numPositionOptions == 0) {
195  infile = clo_getString(optionList, "ifile");
196  outfile = clo_getString(optionList, "ofile");
197  spix = clo_getInt(optionList, "spix");
198  epix = clo_getInt(optionList, "epix");
199  sscan = clo_getInt(optionList, "sline");
200  escan = clo_getInt(optionList, "eline");
201  // pix_sub = clo_getInt(optionList, "pix_sub");
202  // sc_sub = clo_getInt(optionList, "sc_sub");
203  pix_sub = 1;
204  sc_sub = 1;
205 
206  if (clo_isSet(optionList, "product")) {
207  prodlist_str = clo_getRawString(optionList, "product");
208  }
209  if (clo_isSet(optionList, "wavelist")) {
210  waves_str = clo_getRawString(optionList, "wavelist");
211  }
212  } else if (numPositionOptions == 8) {
216  sscan = atoi(clo_getPositionString(optionList, 3));
217  escan = atoi(clo_getPositionString(optionList, 4));
218  pix_sub = atoi(clo_getPositionString(optionList, 5));
219  sc_sub = atoi(clo_getPositionString(optionList, 6));
221  } else if (numPositionOptions == 9) {
225  sscan = atoi(clo_getPositionString(optionList, 3));
226  escan = atoi(clo_getPositionString(optionList, 4));
227  pix_sub = atoi(clo_getPositionString(optionList, 5));
228  sc_sub = atoi(clo_getPositionString(optionList, 6));
230  prodlist_str = clo_getPositionString(optionList, 8);
231  } else {
232  printf("\nNote: Number of Positional Arguments should be 0, 8 or 9!\n");
233  exit(EXIT_FAILURE);
234  }
235 
236  if (pix_sub != 1 || sc_sub != 1) {
237  printf("Subsampling not yet implemented.\n");
238  exit(EXIT_FAILURE);
239  }
240 
241  prodlist[0] = 0;
242  if (prodlist_str[0] != 0) {
243  strcpy(prodlist, ",l2_flags,");
244  strcat(prodlist, prodlist_str);
245  strcat(prodlist, ",");
246  printf("product: %s\n", prodlist_str);
247  }
248 
249  if (clo_getBool(optionList, "verbose")) {
250  want_verbose = 1;
251  }
252  if (waves_str[0] != 0) {
253  strcat(wavelist, waves_str);
254  if (want_verbose)
255  printf("Wavelength requested by the user: %s\n", waves_str);
256  }
257  // if it is not HDF4 then try netCDF
258  if (!Hishdf(infile)) {
259  return extractNetCDF(infile, outfile, spix, epix, sscan, escan, prodlist, wavelist);
260  }
261 
262  HDFfid_r = Hopen(infile, DFACC_READ, 0);
263  status = Vstart(HDFfid_r);
264  sd_id_r = SDstart(infile, DFACC_RDONLY);
265 
266  /* Get number of scan lines */
267  /* ------------------------ */
268  status = SDreadattr(sd_id_r, SDfindattr(sd_id_r, "Number of Scan Lines"), (VOIDP)&i32);
269 
270  /* Read Title, Check if OCTS L1 */
271  /* ---------------------------- */
272  status = SDreadattr(sd_id_r, SDfindattr(sd_id_r, "Title"), (VOIDP)title);
273  if (strncmp(title, "OCTS", 4) == 0)
274  octs = 1;
275  if (strstr(title, "Level-2") != 0)
276  octs = 0;
277  if (strcmp(title, "SeaWiFS Level-1A Data") == 0)
278  seawifs_l1a = 1;
279 
280  /* If OCTS make sure sscan is odd and escan is even */
281  /* ------------------------------------------------ */
282  if (octs == 1) {
283  if ((sscan % 2) == 0)
284  sscan -= 1;
285  if ((escan % 2) == 1)
286  escan -= 1;
287  printf("Actual OCTS sscan: %d\n", sscan);
288  printf("Actual OCTS escan: %d\n", escan);
289  }
290 
291  /* Determine number of datasets in input files */
292  SDfileinfo(sd_id_r, &ndatasets, &nglobal_attr);
293 
294  if (sscan == -1 && spix == -1)
295  lonlatinterp = 0;
296 
297  if (sscan == -1) {
298  sscan = 1;
299  escan = i32;
300  }
301 
302  if (escan > i32 * (octs + 1)) {
303  printf("escan: %d greater than # of scan lines: %d\n", escan, i32);
304  exit(BOUNDS_ERROR);
305  }
306 
307  status = SDreadattr(sd_id_r, SDfindattr(sd_id_r, "Pixels per Scan Line"), (VOIDP)&i32);
308 
309  if (spix == -1) {
310  spix = 1;
311  epix = i32;
312  }
313 
314  if (epix > i32) {
315  printf("epix: %d greater than # of pixels per scan: %d\n", epix, i32);
316  exit(BOUNDS_ERROR);
317  }
318 
319  printf("sscan: %d escan: %d\n", sscan, escan);
320  nscan = escan - sscan + 1;
321 
322  /* Create output HDF file */
323  /* ---------------------- */
324  HDFfid_w = Hopen(outfile, DFACC_CREATE, 0);
325  status = Vstart(HDFfid_w);
326  sd_id_w = SDstart(outfile, DFACC_RDWR);
327 
328  /* For each dataset (CREATE) ... */
329  /* ----------------------------- */
330  vg_ref = Vfind(HDFfid_r, "Geophysical Data");
331  vgid_r = Vattach(HDFfid_r, vg_ref, "r");
332  strcpy(geophyslist, ",");
333  for (i = 0; i < Vntagrefs(vgid_r); i++) {
334  status = Vgettagref(vgid_r, i, &tag, &ref);
335  sds_id = SDselect(sd_id_r, SDreftoindex(sd_id_r, ref));
336  status = SDgetinfo(sds_id, buffer, &rank, dims, &dtype, &nattrs);
337  SDendaccess(sds_id);
338  strcat(geophyslist, buffer);
339  strcat(geophyslist, ",");
340  }
341  Vdetach(vgid_r);
342 
343  for (jds = 0; jds < ndatasets; jds++) {
344  sds_id = SDselect(sd_id_r, jds);
345  status = SDgetinfo(sds_id, fieldname, &rank, dims, &dtype, &nattrs);
346 
347  printf("Name: %s rank: %d type: %d\n", fieldname, rank, dtype);
348 
349  /* Check if L2 product field & in prodlist */
350  /* --------------------------------------- */
351  if (prodlist[0] != 0) {
352  strcpy(buffer, ",");
353  strcat(buffer, fieldname);
354  strcat(buffer, ",");
355 
356  if (strstr(geophyslist, buffer) == NULL)
357  goto LBL1;
358  if (strstr(prodlist, buffer) == NULL)
359  continue;
360  }
361 
362  LBL1:
363 
364  /* Determine extract dimensions */
365  /* ---------------------------- */
366  for (i = 0; i < rank; i++) {
367  dim_id_r = SDgetdimid(sds_id, i);
368  dim_id_w = SDgetdimid(sds_id_w, i);
369  SDdiminfo(dim_id_r, buffer, &count, &i32, &i32);
370 
371  dims_w[jds][i] = dims[i];
372 
373  if (octs == 1) {
374  /* OCTS */
375 
376  if (strcmp(fieldname, "samp_table") == 0)
377  continue;
378 
379  if (strcmp(buffer, "rec") == 0) {
380  dims_w[jds][i] = (escan - sscan + 1) / 2;
381  }
382 
383  if (strcmp(buffer, "lines") == 0) {
384  dims_w[jds][i] = escan - sscan + 1;
385  }
386 
387  if (strcmp(buffer, "blines") == 0) {
388  dims_w[jds][i] = 5 * (escan - sscan + 1);
389  }
390 
391  if (strcmp(buffer, "nsamp") == 0) {
392  dims_w[jds][i] = epix - spix + 1;
393  }
394 
395  // if (strcmp(buffer, "pxls") == 0) {
396  // dims_w[jds][i] = (epix - spix + 1) / 20 + 1;
397  //}
398  } else {
399  if (strcmp(buffer, "Number of Scan Lines") == 0) {
400  dims_w[jds][i] = escan - sscan + 1;
401  }
402 
403  if (strcmp(buffer, "Pixels per Scan Line") == 0) {
404  dims_w[jds][i] = epix - spix + 1;
405  }
406 
407  if (strcmp(buffer, "Number of Pixel Control Points") == 0) {
408  if (lonlatinterp) {
409  n_cntl_pnts = count;
410  dims_w[jds][i] = epix - spix + 1;
411  }
412  }
413  }
414  }
415 
416  for (k = 0; k < rank; k++) printf("%d\n", dims_w[jds][k]);
417  printf("---------------\n");
418 
419  /* Create extract SDS */
420  /* ------------------ */
421  sds_id_w = SDcreate(sd_id_w, fieldname, dtype, rank, dims_w[jds]);
422  if (sds_id_w == -1) {
423  printf("Field: %s cannot be created\n", fieldname);
424  exit(EXIT_FAILURE);
425  }
426 
427  /* Set dimension names */
428  /* ------------------- */
429  for (i = 0; i < rank; i++) {
430  dim_id_r = SDgetdimid(sds_id, i);
431  dim_id_w = SDgetdimid(sds_id_w, i);
432  SDdiminfo(dim_id_r, buffer, &count, &i32, &i32);
433  SDsetdimname(dim_id_w, buffer);
434  }
435 
436  /* Read & Write SDS attributes */
437  /* --------------------------- */
438  for (i = 0; i < nattrs; i++) {
439  status = SDreadattr(sds_id, i, (VOIDP)buf2);
440  status = SDattrinfo(sds_id, i, attrname, &dtype, &count);
441  status = SDsetattr(sds_id_w, attrname, dtype, count, (VOIDP)buf2);
442  }
443 
444  /* Get lon/lat control points */
445  /* -------------------------- */
446  if (strcmp(fieldname, "cntl_pt_cols") == 0 && lonlatinterp == 1) {
447  status = SDreaddata(sds_id, start, NULL, dims, (VOIDP)cntl_pt_cols_buf);
448 
449  /* Convert cntl pnts from I32 to F32 */
450  /* --------------------------------- */
451  for (i = 0; i < n_cntl_pnts; i++) {
452  memcpy(&i32, &cntl_pt_cols_buf[4 * i], sizeof(int32));
453  f32 = (float32)i32;
454  memcpy(&cntl_pt_cols_buf[4 * i], &f32, sizeof(float32));
455  }
456  }
457 
458  SDendaccess(sds_id_w);
459  SDendaccess(sds_id);
460  } /* End Create SD */
461  /* printf("-----------------------------\n");*/
462 
463  /* Get #scan lines, #pixels per scan */
464  /* --------------------------------- */
465  for (i = 0; i < nglobal_attr; i++) {
466  status = SDattrinfo(sd_id_r, i, buffer, &dtype, &count);
467  data = (char *)calloc(count, DFKNTsize(dtype));
468  status = SDreadattr(sd_id_r, i, (VOIDP)data);
469 
470  if (strcmp(buffer, "Number of Scan Lines") == 0) {
471  memcpy(&nscans, data, 4);
472  }
473 
474  if (strcmp(buffer, "Pixels per Scan Line") == 0) {
475  memcpy(&npixl, data, 4);
476  }
477  free(data);
478  }
479 
480  /* Copy Datasets */
481  /* ------------- */
482  n = 0;
483 
484  for (jds = 0; jds < ndatasets; jds++) {
485  sds_id = SDselect(sd_id_r, jds);
486  status = SDgetinfo(sds_id, fieldname, &rank, dims, &dtype, &nattrs);
487 
488  /* Check if L2 product field & in prodlist */
489  /* --------------------------------------- */
490  if (prodlist[0] != 0) {
491  strcpy(buffer, ",");
492  strcat(buffer, fieldname);
493  strcat(buffer, ",");
494 
495  if (strstr(geophyslist, buffer) == NULL)
496  goto LBL2;
497  if (strstr(prodlist, buffer) == NULL)
498  continue;
499  }
500 
501  LBL2:
502 
503  printf("[Copy] %s\n", fieldname);
504 
505  sds_id_w = SDselect(sd_id_w, n++);
506 
507  /* Determine start & nelem */
508  /* ----------------------- */
509  for (i = 0; i < rank; i++) {
510  start[i] = 0;
511 
512  dim_id_w = SDgetdimid(sds_id_w, i);
513  SDdiminfo(dim_id_w, buffer, &count, &i32, &nattrs);
514  SDsetdimname(dim_id_w, buffer);
515 
516  if (octs == 1) {
517  /* OCTS */
518  if (strcmp(fieldname, "samp_table_") != 0) {
519  if (strcmp(buffer, "rec") == 0) {
520  start[i] = (sscan - 1) / 2;
521  }
522 
523  if (strcmp(buffer, "lines") == 0) {
524  start[i] = sscan - 1;
525  }
526 
527  if (strcmp(buffer, "blines") == 0) {
528  start[i] = 5 * (sscan - 1);
529  }
530 
531  if (strcmp(buffer, "nsamp") == 0) {
532  start[i] = spix - 1;
533  }
534 
535  // if (strcmp(buffer, "pxls") == 0) {
536  // start[i] = (spix - 1) / 20;
537  //}
538  }
539  } else {
540  if (strcmp(buffer, "Number of Scan Lines") == 0) {
541  start[i] = sscan - 1;
542  }
543 
544  if (strcmp(buffer, "Pixels per Scan Line") == 0) {
545  start[i] = spix - 1;
546  }
547  }
548 
549  if (i == 0)
550  nelem = dims_w[jds][i];
551  else
552  nelem *= dims_w[jds][i];
553  }
554 
555  if (seawifs_l1a == 0) {
556  if (strcmp(fieldname, "slat") == 0 || strcmp(fieldname, "slon") == 0 ||
557  strcmp(fieldname, "elon") == 0 || strcmp(fieldname, "elat") == 0 ||
558  strcmp(fieldname, "clon") == 0 || strcmp(fieldname, "clat") == 0) {
559  continue;
560  }
561  }
562 
563  /* Allocate data buffer */
564  /* -------------------- */
565  if (strcmp(fieldname, "latitude") == 0 || strcmp(fieldname, "longitude") == 0)
566  nelem = nscans * npixl;
567 
568  data = (char *)calloc(nelem, DFKNTsize(dtype));
569 
570  if (strcmp(fieldname, "cntl_pt_cols") == 0 && lonlatinterp == 1) {
571  for (i = 0; i < dims_w[jds][0]; i++) {
572  i32 = i + 1;
573  memcpy(&data[i * 4], &i32, 4);
574  }
575  /* Latitude */
576  /* -------- */
577  } else if (strcmp(fieldname, "latitude") == 0 && lonlatinterp == 1) {
578  dims_lonlat[0] = dims_w[jds][0];
579  dims_lonlat[1] = n_cntl_pnts;
580 
581  for (i = 0; i < dims_w[jds][0]; i++) {
582  start[0] = i + sscan - 1;
583  edge[0] = 1;
584  edge[1] = dims_lonlat[1];
585  status = SDreaddata(sds_id, start, NULL, edge, (VOIDP)&data[4 * i * npixl]);
586  }
587 
588  /* Interpolate Latitude */
589  /* -------------------- */
590  for (j = 0; j < dims_w[jds][0]; j++) {
591  spline((float *)cntl_pt_cols_buf, (float *)&data[j * 4 * npixl], n_cntl_pnts, 1e30, 1e30,
592  spline_arr);
593  for (i = spix - 1; i < epix; i++) {
594  splint((float *)cntl_pt_cols_buf, (float *)&data[j * 4 * npixl], spline_arr, n_cntl_pnts,
595  i + 1.0, &f32_buf[i - spix + 1]);
596 
597  /* Determine northmost & southmost lat */
598  /* ----------------------------------- */
599  if (f32_buf[i - spix + 1] > northern_lat)
600  northern_lat = f32_buf[i - spix + 1];
601  if (f32_buf[i - spix + 1] < southern_lat)
602  southern_lat = f32_buf[i - spix + 1];
603  }
604 
605  memcpy(&data[j * 4 * dims_w[jds][1]], f32_buf, 4 * dims_w[jds][1]);
606  }
607 
608  /* Longitude */
609  /* --------- */
610  } else if (strcmp(fieldname, "longitude") == 0 && lonlatinterp == 1) {
611  dims_lonlat[0] = dims_w[jds][0];
612  dims_lonlat[1] = n_cntl_pnts;
613 
614  for (i = 0; i < dims_w[jds][0]; i++) {
615  start[0] = i + sscan - 1;
616  edge[0] = 1;
617  edge[1] = dims_lonlat[1];
618  status = SDreaddata(sds_id, start, NULL, edge, (VOIDP)&data[4 * i * npixl]);
619  }
620 
621  /* Interpolate Longitude */
622  /* --------------------- */
623  for (j = 0; j < dims_w[jds][0]; j++) {
624  /* Remove any dateline discontinuity in the longitudes */
625  /* --------------------------------------------------- */
626  for (i = 1; i < dims_lonlat[1]; i++) {
627  memcpy(&f32_buf[1], &data[4 * (j * npixl + i)], 4);
628  memcpy(&f32_buf[0], &data[4 * (j * npixl + i - 1)], 4);
629  delta = f32_buf[1] - f32_buf[0];
630  if (delta < -180) {
631  f32_buf[1] += 360;
632  datelinecross = 1;
633  } else if (delta > 180) {
634  f32_buf[1] -= 360;
635  datelinecross = 1;
636  }
637  memcpy(&data[4 * (j * npixl + i)], &f32_buf[1], 4);
638  }
639 
640  spline((float *)cntl_pt_cols_buf, (float *)&data[j * 4 * npixl], n_cntl_pnts, 1e30, 1e30,
641  spline_arr);
642  for (i = spix - 1; i < epix; i++) {
643  splint((float *)cntl_pt_cols_buf, (float *)&data[j * 4 * npixl], spline_arr, n_cntl_pnts,
644  i + 1.0, &f32_buf[i - spix + 1]);
645  if (f32_buf[i - spix + 1] > +180)
646  f32_buf[i - spix + 1] -= 360;
647  if (f32_buf[i - spix + 1] < -180)
648  f32_buf[i - spix + 1] += 360;
649 
650  /* Determine eastmost & westmost lon */
651  /* --------------------------------- */
652  if (f32_buf[i - spix + 1] > eastern_lon)
653  eastern_lon = f32_buf[i - spix + 1];
654  if (f32_buf[i - spix + 1] < western_lon)
655  western_lon = f32_buf[i - spix + 1];
656  }
657  memcpy(&data[j * 4 * dims_w[jds][1]], f32_buf, 4 * dims_w[jds][1]);
658  }
659  } else {
660  status = SDreaddata(sds_id, start, NULL, dims_w[jds], (VOIDP)data);
661  if (status == -1) {
662  printf("read status: %d for %s\n\n", status, fieldname);
663  exit(EXIT_FAILURE);
664  }
665  }
666 
667  /* Modify time/date fields */
668  /* ----------------------- */
669  if (strcmp(fieldname, "year") == 0) {
670  memcpy(&startyear, &data[0], 4);
671  memcpy(&cenyear, &data[4 * (dims_w[jds][0] / 2)], 4);
672  memcpy(&endyear, &data[4 * (dims_w[jds][0] - 1)], 4);
673  }
674 
675  if (strcmp(fieldname, "day") == 0) {
676  memcpy(&startday, &data[0], 4);
677  memcpy(&cenday, &data[4 * (dims_w[jds][0] / 2)], 4);
678  memcpy(&endday, &data[4 * (dims_w[jds][0] - 1)], 4);
679  }
680 
681  if (strcmp(fieldname, "msec") == 0) {
682  memcpy(&startmsec, &data[0], 4);
683  memcpy(&cenmsec, &data[4 * (dims_w[jds][0] / 2)], 4);
684  memcpy(&endmsec, &data[4 * (dims_w[jds][0] - 1)], 4);
685  }
686 
687  if (strcmp(fieldname, "longitude") == 0) {
688  memcpy(&ullon, &data[0], 4);
689  memcpy(&lllon, &data[4 * (dims_w[jds][1] * (dims_w[jds][0] - 1))], 4);
690 
691  memcpy(&urlon, &data[4 * (dims_w[jds][1] - 1)], 4);
692  memcpy(&lrlon, &data[4 * (dims_w[jds][0] * dims_w[jds][1] - 1)], 4);
693 
694  memcpy(&cclon, &data[4 * (dims_w[jds][1] * dims_w[jds][0] / 2 - dims_w[jds][1] / 2)], 4);
695  memcpy(&sclon, &data[4 * (dims_w[jds][1] / 2)], 4);
696  memcpy(&eclon, &data[4 * (dims_w[jds][1] * dims_w[jds][0] - dims_w[jds][1] / 2)], 4);
697  }
698 
699  if (strcmp(fieldname, "latitude") == 0) {
700  memcpy(&ullat, &data[0], 4);
701  memcpy(&lllat, &data[4 * (dims_w[jds][1] * (dims_w[jds][0] - 1))], 4);
702 
703  memcpy(&urlat, &data[4 * (dims_w[jds][1] - 1)], 4);
704  memcpy(&lrlat, &data[4 * (dims_w[jds][0] * dims_w[jds][1] - 1)], 4);
705 
706  memcpy(&cclat, &data[4 * (dims_w[jds][1] * dims_w[jds][0] / 2 - dims_w[jds][1] / 2)], 4);
707  memcpy(&sclat, &data[4 * (dims_w[jds][1] / 2)], 4);
708  memcpy(&eclat, &data[4 * (dims_w[jds][1] * dims_w[jds][0] - dims_w[jds][1] / 2)], 4);
709  }
710 
711  if (octs == 1) {
712  if (strcmp(fieldname, "lon") == 0) {
713  memcpy(&ullon, &data[0], 4);
714  memcpy(&lllon, &data[4 * (dims_w[jds][0] - 1) * dims_w[jds][1]], 4);
715 
716  memcpy(&urlon, &data[4 * (dims_w[jds][1] - 1)], 4);
717  memcpy(&lrlon, &data[4 * ((dims_w[jds][0] - 1) * dims_w[jds][1] + dims_w[jds][1] - 1)], 4);
718 
719  memcpy(&cclon, &data[4 * ((dims_w[jds][0] / 2) * dims_w[jds][1] + dims_w[jds][1] / 2)], 4);
720  memcpy(&sclon, &data[4 * (dims_w[jds][1] / 2)], 4);
721  memcpy(&eclon, &data[4 * ((dims_w[jds][0] - 1) * dims_w[jds][1] + dims_w[jds][1] / 2)], 4);
722  }
723 
724  if (strcmp(fieldname, "lat") == 0) {
725  memcpy(&ullat, &data[0], 4);
726  memcpy(&lllat, &data[4 * (dims_w[jds][0] - 1) * dims_w[jds][1]], 4);
727 
728  memcpy(&urlat, &data[4 * (dims_w[jds][1] - 1)], 4);
729  memcpy(&lrlat, &data[4 * ((dims_w[jds][0] - 1) * dims_w[jds][1] + dims_w[jds][1] - 1)], 4);
730 
731  memcpy(&cclat, &data[4 * ((dims_w[jds][0] / 2) * dims_w[jds][1] + dims_w[jds][1] / 2)], 4);
732  memcpy(&sclat, &data[4 * (dims_w[jds][1] / 2)], 4);
733  memcpy(&eclat, &data[4 * ((dims_w[jds][0] - 1) * dims_w[jds][1] + dims_w[jds][1] / 2)], 4);
734  }
735  }
736 
737  if (strcmp(fieldname, "csol_z") == 0) {
738  memcpy(&csolz, &data[4 * (dims_w[jds][0] / 2)], 4);
739  }
740 
741  if (strcmp(fieldname, "ntilts") == 0) {
742  memcpy(&ntilts, data, 4);
743  }
744 
745  if (strcmp(fieldname, "tilt_flags") == 0) {
746  memcpy(old_tilt_flags, data, 2 * 20);
747  }
748 
749  if (strcmp(fieldname, "tilt_ranges") == 0) {
750  memset(new_tilt_flags, 0, 2 * 20);
751  memset(new_tilt_ranges, 0, 2 * 2 * 20);
752  memcpy(old_tilt_ranges, data, 2 * 2 * 20);
753 
754  for (i = 0; i < ntilts; i++) {
755  if (sscan >= old_tilt_ranges[i][0] && sscan <= old_tilt_ranges[i][1]) {
756  stilt = i;
757  }
758 
759  if (escan >= old_tilt_ranges[i][0] && escan <= old_tilt_ranges[i][1]) {
760  etilt = i;
761  }
762  }
763  ntilts = etilt - stilt + 1;
764 
765  for (i = 0; i < ntilts; i++) {
766  new_tilt_ranges[i][0] = old_tilt_ranges[stilt + i][0] - sscan + 1;
767  new_tilt_ranges[i][1] = old_tilt_ranges[stilt + i][1] - sscan + 1;
768 
769  if (new_tilt_ranges[i][0] < 1)
770  new_tilt_ranges[i][0] = 1;
771  if (new_tilt_ranges[i][1] < 1)
772  new_tilt_ranges[i][0] = 1;
773 
774  if (new_tilt_ranges[i][0] > nscan)
775  new_tilt_ranges[i][0] = nscan;
776  if (new_tilt_ranges[i][1] > nscan)
777  new_tilt_ranges[i][1] = nscan;
778 
779  new_tilt_flags[i] = old_tilt_flags[stilt + i];
780  new_tilt_flags[i] = old_tilt_flags[stilt + i];
781  }
782 
783  memcpy(data, new_tilt_ranges, 2 * 2 * 20);
784  }
785 
786  /* Write extract field */
787  /* ------------------- */
788  status = SDwritedata(sds_id_w, start_w, NULL, dims_w[jds], (VOIDP)data);
789  if (status == -1) {
790  printf("write status: %d\n\n", status);
791  exit(EXIT_FAILURE);
792  }
793 
794  /* Write slon,elon,clon */
795  /* -------------------- */
796  if (strcmp(fieldname, "longitude") == 0) {
797  data2 = (char *)calloc(dims_w[jds][0], DFKNTsize(dtype));
798 
799  /* slon */
800  for (j = 0; j < dims_w[jds][0]; j++) {
801  k = DFKNTsize(dtype);
802  memcpy(&data2[k * j], &data[k * j * dims_w[jds][1]], DFKNTsize(dtype));
803  }
804  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "slon"));
805  status = SDwritedata(sds_id_w, start_w, NULL, dims_w[jds], (VOIDP)data2);
806  if (status == -1) {
807  printf("write status: %d\n\n", status);
808  exit(EXIT_FAILURE);
809  }
810 
811  /* elon */
812  for (j = 0; j < dims_w[jds][0]; j++) {
813  k = DFKNTsize(dtype);
814  memcpy(&data2[k * j], &data[k * (j * dims_w[jds][1] + dims_w[jds][1] - 1)], DFKNTsize(dtype));
815  }
816  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "elon"));
817  status = SDwritedata(sds_id_w, start_w, NULL, dims_w[jds], (VOIDP)data2);
818  if (status == -1) {
819  printf("write status: %d\n\n", status);
820  exit(EXIT_FAILURE);
821  }
822 
823  /* clon */
824  for (j = 0; j < dims_w[jds][0]; j++) {
825  k = DFKNTsize(dtype);
826  memcpy(&data2[k * j], &data[k * (j * dims_w[jds][1] + dims_w[jds][1] / 2)], DFKNTsize(dtype));
827  }
828  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "clon"));
829  status = SDwritedata(sds_id_w, start_w, NULL, dims_w[jds], (VOIDP)data2);
830  if (status == -1) {
831  printf("write status: %d\n\n", status);
832  exit(EXIT_FAILURE);
833  }
834  free(data2);
835  }
836 
837  /* Write slat,elat,clat */
838  /* -------------------- */
839  if (strcmp(fieldname, "latitude") == 0) {
840  data2 = (char *)calloc(dims_w[jds][0], DFKNTsize(dtype));
841 
842  /* slat */
843  for (j = 0; j < dims_w[jds][0]; j++) {
844  k = DFKNTsize(dtype);
845  memcpy(&data2[k * j], &data[k * j * dims_w[jds][1]], DFKNTsize(dtype));
846  }
847  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "slat"));
848  status = SDwritedata(sds_id_w, start_w, NULL, dims_w[jds], (VOIDP)data2);
849  if (status == -1) {
850  printf("write status: %d\n\n", status);
851  exit(EXIT_FAILURE);
852  }
853 
854  /* elat */
855  for (j = 0; j < dims_w[jds][0]; j++) {
856  k = DFKNTsize(dtype);
857  memcpy(&data2[k * j], &data[k * (j * dims_w[jds][1] + dims_w[jds][1] - 1)], DFKNTsize(dtype));
858  }
859  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "elat"));
860  status = SDwritedata(sds_id_w, start_w, NULL, dims_w[jds], (VOIDP)data2);
861  if (status == -1) {
862  printf("write status: %d\n\n", status);
863  exit(EXIT_FAILURE);
864  }
865 
866  /* clat */
867  for (j = 0; j < dims_w[jds][0]; j++) {
868  k = DFKNTsize(dtype);
869  memcpy(&data2[k * j], &data[k * (j * dims_w[jds][1] + dims_w[jds][1] / 2)], DFKNTsize(dtype));
870  }
871  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "clat"));
872  status = SDwritedata(sds_id_w, start_w, NULL, dims_w[jds], (VOIDP)data2);
873  if (status == -1) {
874  printf("write status: %d\n\n", status);
875  exit(EXIT_FAILURE);
876  }
877  free(data2);
878  }
879 
880  free(data);
881 
882  SDendaccess(sds_id);
883  SDendaccess(sds_id_w);
884 
885  } /* Dataset Loop */
886 
887  /* Correct "tilt" fields */
888  /* --------------------- */
889  sds_id = SDselect(sd_id_w, SDnametoindex(sd_id_w, "ntilts"));
890  if (sds_id != -1) {
891  status = SDgetinfo(sds_id, fieldname, &rank, dims, &dtype, &nattrs);
892  status = SDwritedata(sds_id, &zero, NULL, dims, (VOIDP)&ntilts);
893  if (status == -1) {
894  printf("write ntilts status: %d\n\n", status);
895  exit(EXIT_FAILURE);
896  }
897  }
898 
899  sds_id = SDselect(sd_id_w, SDnametoindex(sd_id_w, "tilt_flags"));
900  if (sds_id != -1) {
901  status = SDgetinfo(sds_id, fieldname, &rank, dims, &dtype, &nattrs);
902  status = SDwritedata(sds_id, &zero, NULL, dims, (VOIDP)new_tilt_flags);
903  if (status == -1) {
904  printf("write tilt_flags status: %d for %d\n\n", status, jds);
905  exit(EXIT_FAILURE);
906  }
907  }
908 
909  /* If Seawifs L1A determine geo limits */
910  /* ----------------------------------- */
911  if (seawifs_l1a) {
912  start_w[0] = 0;
913  dims_w[0][0] = epix - spix + 1;
914  data2 = (char *)calloc(dims_w[0][0], sizeof(float));
915 
916  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "slon"));
917  status = SDreaddata(sds_id_w, start_w, NULL, dims_w[0], (VOIDP)data2);
918 
919  memcpy(&ullon, &data2[0], sizeof(float));
920  memcpy(&lllon, &data2[sizeof(float) * (epix - spix)], sizeof(float));
921 
922  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "clon"));
923  status = SDreaddata(sds_id_w, start_w, NULL, dims_w[0], (VOIDP)data2);
924 
925  memcpy(&sclon, &data2[0], sizeof(float));
926  memcpy(&eclon, &data2[sizeof(float) * (epix - spix)], sizeof(float));
927  memcpy(&cclon, &data2[sizeof(float) * (epix - spix) / 2], sizeof(float));
928 
929  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "elon"));
930  status = SDreaddata(sds_id_w, start_w, NULL, dims_w[0], (VOIDP)data2);
931 
932  memcpy(&urlon, &data2[0], sizeof(float));
933  memcpy(&lrlon, &data2[sizeof(float) * (epix - spix)], sizeof(float));
934 
935  eastern_lon = -180.0;
936  western_lon = +180.0;
937  if (ullon > eastern_lon)
938  eastern_lon = ullon;
939  if (lllon > eastern_lon)
940  eastern_lon = lllon;
941  if (sclon > eastern_lon)
942  eastern_lon = sclon;
943  if (eclon > eastern_lon)
944  eastern_lon = eclon;
945  if (urlon > eastern_lon)
946  eastern_lon = urlon;
947  if (lrlon > eastern_lon)
948  eastern_lon = lrlon;
949 
950  if (ullon < western_lon)
951  western_lon = ullon;
952  if (lllon < western_lon)
953  western_lon = lllon;
954  if (sclon < western_lon)
955  western_lon = sclon;
956  if (eclon < western_lon)
957  western_lon = eclon;
958  if (urlon < western_lon)
959  western_lon = urlon;
960  if (lrlon < western_lon)
961  western_lon = lrlon;
962 
963  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "slat"));
964  status = SDreaddata(sds_id_w, start_w, NULL, dims_w[0], (VOIDP)data2);
965 
966  memcpy(&ullat, &data2[0], sizeof(float));
967  memcpy(&lllat, &data2[sizeof(float) * (epix - spix)], sizeof(float));
968 
969  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "clat"));
970  status = SDreaddata(sds_id_w, start_w, NULL, dims_w[0], (VOIDP)data2);
971 
972  memcpy(&sclat, &data2[0], sizeof(float));
973  memcpy(&eclat, &data2[sizeof(float) * (epix - spix)], sizeof(float));
974  memcpy(&cclat, &data2[sizeof(float) * (epix - spix) / 2], sizeof(float));
975 
976  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, "elat"));
977  status = SDreaddata(sds_id_w, start_w, NULL, dims_w[0], (VOIDP)data2);
978 
979  memcpy(&urlat, &data2[0], sizeof(float));
980  memcpy(&lrlat, &data2[sizeof(float) * (epix - spix)], sizeof(float));
981 
982  northern_lat = -90.0;
983  southern_lat = +90.0;
984  if (ullat > northern_lat)
985  northern_lat = ullat;
986  if (lllat > northern_lat)
987  northern_lat = lllat;
988  if (sclat > northern_lat)
989  northern_lat = sclat;
990  if (eclat > northern_lat)
991  northern_lat = eclat;
992  if (urlat > northern_lat)
993  northern_lat = urlat;
994  if (lrlat > northern_lat)
995  northern_lat = lrlat;
996 
997  if (ullat < southern_lat)
998  southern_lat = ullat;
999  if (lllat < southern_lat)
1000  southern_lat = lllat;
1001  if (sclat < southern_lat)
1002  southern_lat = sclat;
1003  if (eclat < southern_lat)
1004  southern_lat = eclat;
1005  if (urlat < southern_lat)
1006  southern_lat = urlat;
1007  if (lrlat < southern_lat)
1008  southern_lat = lrlat;
1009 
1010  /*
1011  if (strcmp(buffer, "Scene Center Solar Zenith") == 0) {
1012  memcpy(data, &csolz, 4);
1013  }
1014 
1015  if (strcmp(buffer, "Easternmost Longitude") == 0 && lonlatinterp == 1) {
1016  memcpy(data, &eastern_lon, 4);
1017  }
1018  if (strcmp(buffer, "Westernmost Longitude") == 0 && lonlatinterp == 1) {
1019  memcpy(data, &western_lon, 4);
1020  }
1021  */
1022  } else {
1023  if (datelinecross == 1) {
1024  eastern_lon = lllon;
1025  western_lon = lrlon;
1026  }
1027  }
1028 
1029  /* Write Global Attributes */
1030  /* ----------------------- */
1031  for (i = 0; i < nglobal_attr; i++) {
1032  status = SDattrinfo(sd_id_r, i, buffer, &dtype, &count);
1033 
1034  data = (char *)calloc(count, DFKNTsize(dtype));
1035 
1036  status = SDreadattr(sd_id_r, i, (VOIDP)data);
1037 
1038  if (strcmp(buffer, "Number of Scan Lines") == 0) {
1039  if (octs == 1)
1040  i32 = (escan - sscan + 1) / 2;
1041  else
1042  i32 = escan - sscan + 1;
1043  memcpy(data, &i32, 4);
1044  }
1045 
1046  if (strcmp(buffer, "Pixels per Scan Line") == 0) {
1047  i32 = epix - spix + 1;
1048  memcpy(data, &i32, 4);
1049  }
1050 
1051  if (strcmp(buffer, "Number of Scan Control Points") == 0 && lonlatinterp == 1) {
1052  i32 = escan - sscan + 1;
1053  memcpy(data, &i32, 4);
1054  }
1055 
1056  if (strcmp(buffer, "Number of Pixel Control Points") == 0 && lonlatinterp == 1) {
1057  i32 = epix - spix + 1;
1058  memcpy(data, &i32, 4);
1059  }
1060 
1061  if (strcmp(buffer, "Scene Center Scan Line") == 0) {
1062  i32 = (escan - sscan + 1) / 2 + 1; /*((escan - sscan + 1) % 2);*/
1063  memcpy(data, &i32, 4);
1064  }
1065 
1066  if (strcmp(buffer, "Start Time") == 0) {
1067  if (startyear != 0) {
1068  utime = yds2unix(startyear, startday, startmsec / ((double)1000.0));
1069  memcpy(data, ydhmsf(utime, 'G'), 17);
1070  }
1071  }
1072  if (strcmp(buffer, "Start Year") == 0) {
1073  if (startyear != 0)
1074  memcpy(data, &startyear, 4);
1075  }
1076  if (strcmp(buffer, "Start Day") == 0) {
1077  if (startday != 0)
1078  memcpy(data, &startday, 4);
1079  }
1080  if (strcmp(buffer, "Start Millisec") == 0) {
1081  if (startmsec != 0)
1082  memcpy(data, &startmsec, 4);
1083  }
1084 
1085  if (strcmp(buffer, "End Time") == 0) {
1086  if (endyear != 0) {
1087  utime = yds2unix(endyear, endday, endmsec / ((double)1000.0));
1088  memcpy(data, ydhmsf(utime, 'G'), 17);
1089  }
1090  }
1091  if (strcmp(buffer, "End Year") == 0) {
1092  if (endyear != 0)
1093  memcpy(data, &endyear, 4);
1094  }
1095  if (strcmp(buffer, "End Day") == 0) {
1096  if (endday != 0)
1097  memcpy(data, &endday, 4);
1098  }
1099  if (strcmp(buffer, "End Millisec") == 0) {
1100  if (endmsec != 0)
1101  memcpy(data, &endmsec, 4);
1102  }
1103 
1104  if (strcmp(buffer, "Scene Center Time") == 0) {
1105  if (cenyear != 0) {
1106  utime = yds2unix(cenyear, cenday, cenmsec / ((double)1000.0));
1107  memcpy(data, ydhmsf(utime, 'G'), 17);
1108  }
1109  }
1110 
1111  if (strcmp(buffer, "Upper Left Longitude") == 0) {
1112  memcpy(data, &ullon, 4);
1113  }
1114  if (strcmp(buffer, "Upper Right Longitude") == 0) {
1115  memcpy(data, &urlon, 4);
1116  }
1117  if (strcmp(buffer, "Upper Left Latitude") == 0) {
1118  memcpy(data, &ullat, 4);
1119  }
1120  if (strcmp(buffer, "Upper Right Latitude") == 0) {
1121  memcpy(data, &urlat, 4);
1122  }
1123  if (strcmp(buffer, "Start Center Longitude") == 0) {
1124  memcpy(data, &sclon, 4);
1125  }
1126  if (strcmp(buffer, "Start Center Latitude") == 0) {
1127  memcpy(data, &sclat, 4);
1128  }
1129  if (strcmp(buffer, "Scene Center Longitude") == 0) {
1130  memcpy(data, &cclon, 4);
1131  }
1132  if (strcmp(buffer, "Scene Center Latitude") == 0) {
1133  memcpy(data, &cclat, 4);
1134  }
1135  if (strcmp(buffer, "Scene Center Solar Zenith") == 0) {
1136  memcpy(data, &csolz, 4);
1137  }
1138  if (strcmp(buffer, "Lower Left Longitude") == 0) {
1139  memcpy(data, &lllon, 4);
1140  }
1141  if (strcmp(buffer, "Lower Right Longitude") == 0) {
1142  memcpy(data, &lrlon, 4);
1143  }
1144  if (strcmp(buffer, "Lower Left Latitude") == 0) {
1145  memcpy(data, &lllat, 4);
1146  }
1147  if (strcmp(buffer, "Lower Right Latitude") == 0) {
1148  memcpy(data, &lrlat, 4);
1149  }
1150  if (strcmp(buffer, "End Center Longitude") == 0) {
1151  memcpy(data, &eclon, 4);
1152  }
1153  if (strcmp(buffer, "End Center Latitude") == 0) {
1154  memcpy(data, &eclat, 4);
1155  }
1156 
1157  if (strcmp(buffer, "Northernmost Latitude") == 0 && lonlatinterp == 1) {
1158  memcpy(data, &northern_lat, 4);
1159  }
1160  if (strcmp(buffer, "Southernmost Latitude") == 0 && lonlatinterp == 1) {
1161  memcpy(data, &southern_lat, 4);
1162  }
1163  if (strcmp(buffer, "Easternmost Longitude") == 0 && lonlatinterp == 1) {
1164  memcpy(data, &eastern_lon, 4);
1165  }
1166  if (strcmp(buffer, "Westernmost Longitude") == 0 && lonlatinterp == 1) {
1167  memcpy(data, &western_lon, 4);
1168  }
1169 
1170  if (strcmp(buffer, "LAC Pixel Start Number") == 0) {
1171  status = SDreadattr(sd_id_r, i + 1, (VOIDP)&i32);
1172  memcpy(&LAC_start, data, 4);
1173  LAC_start += i32 * (spix - 1);
1174  memcpy(data, &LAC_start, 4);
1175  }
1176 
1177  status = SDsetattr(sd_id_w, buffer, dtype, count, (VOIDP)data);
1178 
1179  /* Set Creation Time to current time
1180  ("Processing Time" is copied from input file */
1181  if (strcmp(buffer, "Processing Time") == 0) {
1182  get_time(data);
1183  status = SDsetattr(sd_id_w, "Creation Time", dtype, count, (VOIDP)data);
1184  }
1185 
1186  /* printf("%d %d\n", i, status);*/
1187  free(data);
1188  }
1189 
1190  /* Write extract attributes if Level 1 */
1191  /* ----------------------------------- */
1192  for (i = 0; i < nglobal_attr; i++) {
1193  status = SDattrinfo(sd_id_r, i, buffer, &dtype, &count);
1194 
1195  data = (char *)calloc(count, DFKNTsize(dtype));
1196 
1197  status = SDreadattr(sd_id_r, i, (VOIDP)data);
1198 
1199  if (strcmp(buffer, "Title") == 0) {
1200  if (strstr(data, "Level-1A") != NULL) {
1201  /* Set Extract Pixel Offset Attribute */
1202  /* ---------------------------------- */
1203  i32 = spix - 1;
1204  status = SDsetattr(sd_id_w, "Extract Pixel Offset", DFNT_INT32, 1, (VOIDP)&i32);
1205 
1206  /* Set Extract Pixel Count Attribute */
1207  /* --------------------------------- */
1208  i32 = epix - spix + 1;
1209  status = SDsetattr(sd_id_w, "Extract Pixel Count", DFNT_INT32, 1, (VOIDP)&i32);
1210 
1211  /* Set Extract Line Offset Attribute */
1212  /* --------------------------------- */
1213  i32 = sscan - 1;
1214  status = SDsetattr(sd_id_w, "Extract Line Offset", DFNT_INT32, 1, (VOIDP)&i32);
1215 
1216  /* Set Extract Line Count Attribute */
1217  /* -------------------------------- */
1218  i32 = escan - sscan + 1;
1219  status = SDsetattr(sd_id_w, "Extract Line Count", DFNT_INT32, 1, (VOIDP)&i32);
1220  }
1221  free(data);
1222  break;
1223  }
1224  }
1225 
1226  /* Build and fill Vgroups */
1227  /* ---------------------- */
1228  for (j = 0; j < nvgrp; j++) {
1229  vg_ref = Vfind(HDFfid_r, vgrpname[j]);
1230  vgid_r = Vattach(HDFfid_r, vg_ref, "r");
1231  vgid_w = Vattach(HDFfid_w, -1, "w");
1232  Vgetname(vgid_r, buffer);
1233  Vsetname(vgid_w, buffer);
1234  Vgetclass(vgid_r, buffer);
1235  Vsetclass(vgid_w, buffer);
1236 
1237  for (i = 0; i < Vntagrefs(vgid_r); i++) {
1238  status = Vgettagref(vgid_r, i, &tag, &ref);
1239  sds_id = SDselect(sd_id_r, SDreftoindex(sd_id_r, ref));
1240  status = SDgetinfo(sds_id, fieldname, &rank, dims, &dtype, &nattrs);
1241 
1242  /* Check if L2 product field & in prodlist */
1243  /* --------------------------------------- */
1244  if (prodlist[0] != 0 && strcmp(vgrpname[j], "Geophysical Data") == 0) {
1245  strcpy(buf2, ",");
1246  strcat(buf2, fieldname);
1247  strcat(buf2, ",");
1248 
1249  if (strstr(prodlist, buf2) == NULL)
1250  goto LBL3;
1251  }
1252 
1253  sds_id_w = SDselect(sd_id_w, SDnametoindex(sd_id_w, fieldname));
1254  Vaddtagref(vgid_w, DFTAG_NDG, SDidtoref(sds_id_w));
1255 
1256  SDendaccess(sds_id_w);
1257  LBL3:
1258  SDendaccess(sds_id);
1259  }
1260  Vdetach(vgid_r);
1261  Vdetach(vgid_w);
1262  }
1263 
1264  SDend(sd_id_r);
1265  Hclose(HDFfid_r);
1266  Vend(HDFfid_r);
1267 
1268  SDend(sd_id_w);
1269  Hclose(HDFfid_w);
1270  Vend(HDFfid_w);
1271 
1272  return 0;
1273 }
char * ydhmsf(double dtime, char zone)
Definition: ydhmsf.c:12
integer, parameter int16
Definition: cubeio.f90:3
int j
Definition: decode_rs.h:73
char * clo_getString(clo_optionList_t *list, const char *key)
Definition: clo.c:1357
int status
Definition: l1_czcs_hdf.c:32
double yds2unix(int16_t year, int16_t day, double secs)
Definition: yds2unix.c:7
void clo_setEnablePositionOptions(int val)
Definition: clo.c:1685
int l2extract_init_options(clo_optionList_t *list, const char *softwareVersion)
#define NULL
Definition: decode_rs.h:63
int extractNetCDF(const char *infile, const char *outfile, int spix, int epix, int sscan, int escan, const char *prodlist, const char *wavelist)
subroutine spline(s, x, y, n, in, t, il, iu, vl, vu, e, u)
Definition: phs.f:1348
int clo_isSet(clo_optionList_t *list, const char *key)
Definition: clo.c:2270
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT32
int32 nscan
Definition: l1_czcs_hdf.c:19
int main(int argc, char *argv[])
int clo_getInt(clo_optionList_t *list, const char *key)
Definition: clo.c:1393
int32_t prodlist(int32_t sensorID, int32_t evalmask, const char *inprod, const char *defprod, char outprod[L1_MAXPROD][32])
clo_optionList_t * clo_createList()
Definition: clo.c:532
int l2extract_read_options(clo_optionList_t *list, int argc, char *argv[])
void get_time(char *pr_time)
Definition: get_time.c:28
char * clo_getPositionString(clo_optionList_t *list, int pos)
Definition: clo.c:1723
void clo_printUsage(clo_optionList_t *list)
Definition: clo.c:1988
const double delta
int want_verbose
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
#define BOUNDS_ERROR
dtype
Definition: DDataset.hpp:31
int32 spix
Definition: l1_czcs_hdf.c:21
Extra metadata that will be written to the HDF4 file l2prod rank
subroutine splint(xa, ya, y2a, n, x, y)
int clo_getPositionNumOptions(clo_optionList_t *list)
Definition: clo.c:1704
int32 epix
Definition: l1_czcs_hdf.c:23
char * clo_getRawString(clo_optionList_t *list, const char *key)
Definition: clo.c:1339
int clo_getBool(clo_optionList_t *list, const char *key)
Definition: clo.c:1375
int i
Definition: decode_rs.h:71
clo_optionList_t * optionList
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int k
Definition: decode_rs.h:73
float32 f32
Definition: l2bin.cpp:98
int count
Definition: decode_rs.h:79