46 static float bad_float =
BAD_FLT;
50 static int32 numScans;
51 static int32 numPixels;
52 static int32 numBands;
53 static int32 numLvlProf;
54 static int32 n_refl_loc;
55 static int32 numBandsIR;
64 static FILE *fp_meta =
NULL;
66 #define GEOBOX_INC 20.0
72 int32
get_ctl(int32_t ctl_pt_fact, int32 ictl[], int32 jctl[]) {
77 if (ctl_pt_fact <= 0) {
81 for (
i = 0;
i < numPixels;
i++) {
86 dctl = (int32_t) numPixels /
RFACTOR;
87 nctl =
MIN((int32_t) numPixels / dctl + 1, numPixels);
88 for (
i = 0;
i < (nctl - 1);
i++) {
89 ictl[
i] =
i * dctl + 1;
91 ictl[nctl - 1] = numPixels;
96 nctl =
MIN((int32_t) numPixels / dctl + 1, numPixels);
98 for (
i = 0;
i < (nctl - 1);
i++) {
99 ictl[
i] =
i * dctl + 1;
101 ictl[nctl - 1] = numPixels;
104 for (
i = 0;
i < numScans;
i++)
119 int32 sd_id =
l2file->sd_id;
123 int seawifs_meta = 0;
126 if (
sd_select(sd_id,
"scan_ell", &sds_id) == 0)
130 h_id = Hopen(
l2file->name, DFACC_RDWR, 0);
132 fprintf(
stderr,
"-E- %s line %d: Hopen() failed for file, %s.\n",
133 __FILE__, __LINE__,
l2file->name);
140 Vsetclass(v_id,
"Per File Data");
141 Vsetname(v_id,
"Sensor Band Parameters");
154 Vsetclass(v_id,
"Per File Data");
155 Vsetname(v_id,
"Sensor Tilt");
165 Vsetclass(v_id,
"Per Scan Data");
166 Vsetname(v_id,
"Scan-Line Attributes");
181 Vsetclass(v_id,
"Per Scan Data");
182 Vsetname(v_id,
"Geophysical Data");
190 Vsetclass(v_id,
"Per Scan Data");
191 Vsetname(v_id,
"Navigation Data");
209 if (Hclose(h_id) != SUCCEED) {
210 fprintf(
stderr,
"-E- %s line %d: Hclose(%d) failed for file, %s .\n",
211 __FILE__, __LINE__, h_id,
l2file->name);
233 int16_t sstflagbits[16];
234 uint8_t byteflagbits[8];
240 const char dm_name[3][80];
245 char* calibrationDataStr;
246 char* equatorCrossingLonStr;
250 char* orbitNumberStr;
251 char* pixelControlPointsStr;
252 char* processingVersionStr;
253 char* productNameStr;
254 char* scanControlPointsStr;
255 char* softwareNameStr;
256 char* softwareVersionStr;
259 char* numberOfScanLinesStr;
260 char* pixelsPerScanLineStr;
261 char* bandsPerPixelStr;
262 char* n_refl_loc_str;
263 char* profLvlPerPixelStr;
264 char* totalBandNumberStr;
267 int profileProductsExist = 0;
269 if (strcmp(
input->metafile,
"") != 0) {
270 fp_meta = fopen(
input->metafile,
"w");
271 if (fp_meta ==
NULL) {
273 "-E- %s line %d: Unable to open specified meta-data file, %s .\n",
274 __FILE__, __LINE__,
input->metafile);
281 int32 nt_chr, nt_u8, nt_i16, nt_i32, nt_f32;
290 calibrationDataStr =
"Calibration Data";
291 equatorCrossingLonStr =
"Orbit Node Longitude";
292 historyStr =
"Processing Control";
293 inputFilesStr =
"Input Files";
294 maskNamesStr =
"Mask Names";
295 orbitNumberStr =
"Orbit Number";
296 pixelControlPointsStr =
"Number of Pixel Control Points";
297 processingVersionStr =
"Processing Version";
298 productNameStr =
"Product Name";
299 scanControlPointsStr =
"Number of Scan Control Points";
300 softwareNameStr =
"Software Name";
301 softwareVersionStr =
"Software Version";
303 numberOfScanLinesStr =
"Number of Scan Lines";
304 pixelsPerScanLineStr =
"Pixels per Scan Line";
305 bandsPerPixelStr =
"Bands per Pixel";
306 n_refl_loc_str =
"Number of Reflectance Location Values";
307 profLvlPerPixelStr =
"Profile Levels per Pixel";
308 totalBandNumberStr =
"total band number";
309 bandNumberStr =
"band number";
320 calibrationDataStr =
"calibration_data";
321 equatorCrossingLonStr =
"equatorCrossingLongitude";
322 historyStr =
"history";
323 inputFilesStr =
"input_sources";
324 maskNamesStr =
"mask_names";
325 orbitNumberStr =
"orbit_number";
326 pixelControlPointsStr =
"pixel_control_points";
327 processingVersionStr =
"processing_version";
328 productNameStr =
"product_name";
329 scanControlPointsStr =
"scan_control_points";
330 softwareNameStr =
"software_name";
331 softwareVersionStr =
"software_version";
333 numberOfScanLinesStr =
"number_of_lines";
334 pixelsPerScanLineStr =
"pixels_per_line";
335 bandsPerPixelStr =
"bands_per_pixel";
336 profLvlPerPixelStr =
"profile_levels_per_pixel";
337 n_refl_loc_str =
"number_of_reflectance_location_values";
338 totalBandNumberStr =
"number_of_bands";
339 bandNumberStr =
"number_of_reflective_bands";
343 priv->
fp = fopen(
l2file->name,
"w");
347 numBands =
l2file->nbands;
348 numBandsIR =
l2file->nbandsir;
358 printf(
"\n\nThe following products will be included in %s.\n",
l2file->name);
359 for (
i=0;
i<tot_prod;
i++)
360 printf(
"%d %s\n",
i,
l2file->l2_prod_names[
i]);
362 l2file->tot_prod = tot_prod;
365 l2file->productInfos = (productInfo_t**)
allocateMemory(tot_prod *
sizeof(productInfo_t*),
"l2file->productInfos");
368 for (
i=0;
i<tot_prod;
i++) {
371 fprintf(
stderr,
"-E- %s line %d: product %s not found.\n", __FILE__,__LINE__,
l2file->l2_prod_names[
i]);
383 numBands =
l2file->nbands;
384 numBandsIR =
l2file->nbandsir;
385 numLvlProf =
l2file->nlvl;
386 n_refl_loc =
l2file->n_refl_loc;
388 cpix = numPixels / 2;
389 epix = numPixels - 1;
390 cscan = numScans / 2;
393 if ((ictl = calloc(numPixels,
sizeof (int32))) ==
NULL) {
395 "-E- %s line %d: Unable to allocate control-point array.\n",
399 if ((jctl = calloc(numScans,
sizeof (int32))) ==
NULL) {
401 "-E- %s line %d: Unable to allocate %d scan control-point array.\n",
402 __FILE__, __LINE__, numScans);
449 printf(
"\n\nThe following products will be included in %s.\n",
l2file->name);
450 for (
i = 0;
i < tot_prod;
i++)
451 printf(
"%d %s\n",
i,
l2file->l2_prod_names[
i]);
453 l2file->tot_prod = tot_prod;
456 l2file->productInfos = (productInfo_t**)
allocateMemory(tot_prod *
sizeof (productInfo_t*),
"l2file->productInfos");
458 l2file->prodptr = (l2prodstr*)
allocateMemory(tot_prod *
sizeof (l2prodstr),
"l2file->prodptr");
459 for (
i = 0;
i < tot_prod;
i++) {
463 "-E- %s line %d: product %s not found.\n",
464 __FILE__, __LINE__,
l2file->l2_prod_names[
i]);
469 numBands + numBandsIR, numPixels, numScans,
l2file->iwave)) ==
NULL) {
471 "-E- %s line %d: product index failure.\n",
479 for (
i = 0;
i < tot_prod;
i++) {
480 if( strcmp(
l2file->productInfos[
i]->category,
"Anc_profile" ) == 0 ) {
481 profileProductsExist = 1;
488 if (nc_def_dim(ds_id.
fid, numberOfScanLinesStr, numScans, &dumdim)
489 != NC_NOERR) exit(1);
490 if (nc_def_dim(ds_id.
fid, pixelsPerScanLineStr, numPixels, &dumdim)
491 != NC_NOERR) exit(1);
492 if ( nc_def_dim(ds_id.
fid, bandsPerPixelStr, numBands, &dumdim)
493 != NC_NOERR) exit(1);
494 if (profileProductsExist) {
495 if ( nc_def_dim(ds_id.
fid, profLvlPerPixelStr, numLvlProf, &dumdim)
496 != NC_NOERR) exit(1);
498 if (nc_def_dim(ds_id.
fid, n_refl_loc_str, n_refl_loc, &dumdim)
499 != NC_NOERR) exit(1);
500 if (nc_def_dim(ds_id.
fid, pixelControlPointsStr, nctl, &dumdim)
501 != NC_NOERR) exit(1);
502 if (nc_def_dim(ds_id.
fid, totalBandNumberStr, numBands + numBandsIR, &dumdim)
503 != NC_NOERR) exit(1);
504 if (nc_def_dim(ds_id.
fid, bandNumberStr, numBands, &dumdim)
505 != NC_NOERR) exit(1);
507 nc_def_grp(ds_id.
fid,
"sensor_band_parameters", &
l2file->grp_id[0]);
508 nc_def_grp(ds_id.
fid,
"scan_line_attributes", &
l2file->grp_id[2]);
509 nc_def_grp(ds_id.
fid,
"geophysical_data", &
l2file->grp_id[3]);
510 nc_def_grp(ds_id.
fid,
"navigation_data", &
l2file->grp_id[4]);
511 nc_def_grp(ds_id.
fid,
"processing_control", &
l2file->grp_id[5]);
522 strcpy((
char *) dm_name[0], numberOfScanLinesStr);
540 strcpy((
char *) dm_name[1], pixelControlPointsStr);
546 strcpy((
char *) dm_name[0], pixelControlPointsStr);
550 strcpy((
char *) dm_name[0], numberOfScanLinesStr);
561 strcpy((
char *) dm_name[0], numberOfScanLinesStr);
563 strcpy((
char *) dm_name[1], pixelsPerScanLineStr);
565 for (
i = 0;
i < tot_prod;
i++) {
567 if (!strcmp(
l2file->l2_prod_names[
i],
"detnum") ||
568 !strcmp(
l2file->l2_prod_names[
i],
"mside") ||
569 !strcmp(
l2file->l2_prod_names[
i],
"year") ||
570 !strcmp(
l2file->l2_prod_names[
i],
"day") ||
571 !strcmp(
l2file->l2_prod_names[
i],
"msec") ||
572 !strcmp(
l2file->l2_prod_names[
i],
"slon") ||
573 !strcmp(
l2file->l2_prod_names[
i],
"clon") ||
574 !strcmp(
l2file->l2_prod_names[
i],
"elon") ||
575 !strcmp(
l2file->l2_prod_names[
i],
"slat") ||
576 !strcmp(
l2file->l2_prod_names[
i],
"clat") ||
577 !strcmp(
l2file->l2_prod_names[
i],
"elat") ||
578 !strcmp(
l2file->l2_prod_names[
i],
"csol_z") ||
579 !strcmp(
l2file->l2_prod_names[
i],
"latitude") ||
580 !strcmp(
l2file->l2_prod_names[
i],
"longitude") ||
581 !strcmp(
l2file->l2_prod_names[
i],
"tilt")
586 if( strcmp(
l2file->productInfos[
i]->category,
"Anc_profile" ) == 0 ) {
588 strcpy((
char *) dm_name[2], profLvlPerPixelStr );
589 }
else if( strcmp(
l2file->productInfos[
i]->category,
590 "Reflectance_loc" ) == 0 ) {
592 strcpy((
char *) dm_name[2], n_refl_loc_str );
595 strcpy((
char *) dm_name[2], bandsPerPixelStr);
606 if ((strcmp(
l2file->l2_prod_names[
i],
"l2_flags") == 0) ||
607 (strcmp(
l2file->l2_prod_names[
i],
"flags_sst") == 0) ||
608 (strcmp(
l2file->l2_prod_names[
i],
"flags_sst3") == 0) ||
609 (strcmp(
l2file->l2_prod_names[
i],
"flags_sst4") == 0) ||
610 (strcmp(
l2file->l2_prod_names[
i],
"qual_sst") == 0) ||
611 (strcmp(
l2file->l2_prod_names[
i],
"qual_sst3") == 0) ||
612 (strcmp(
l2file->l2_prod_names[
i],
"qual_sst4") == 0) ||
613 (strcmp(
l2file->l2_prod_names[
i],
"flags_habs") == 0) ||
614 (strcmp(
l2file->l2_prod_names[
i],
"flags_mph") == 0)) {
616 if ((strcmp(
l2file->l2_prod_names[
i],
"l2_flags") == 0)) {
620 strcat(tmp_str, l2_flag_lname[
k]);
624 strcat(tmp_str,
" ");
630 strlen(l2_flag_lname[
k]) + 1, (VOIDP) l2_flag_lname[
k]));
635 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_sst") == 0) ||
636 (strcmp(
l2file->l2_prod_names[
i],
"flags_sst3") == 0) ||
637 (strcmp(
l2file->l2_prod_names[
i],
"flags_sst4") == 0)) {
641 sstflagbits[
k] =
val;
644 strcat(tmp_str, avhrr_sst_flag_lname[
k]);
646 strcat(tmp_str, viirs_sst_flag_lname[
k]);
648 strcat(tmp_str, sst_flag_lname[
k]);
651 strcat(tmp_str,
" ");
655 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"qual_sst") == 0) ||
656 (strcmp(
l2file->l2_prod_names[
i],
"qual_sst3") == 0) ||
657 (strcmp(
l2file->l2_prod_names[
i],
"qual_sst4") == 0)) {
661 strcat(tmp_str, qual_sst_flag_lname[
k]);
663 strcat(tmp_str,
" ");
667 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_mph") == 0)) {
671 byteflagbits[
k] =
val;
673 strcat(tmp_str, mph_flag_lname[
k]);
675 strcat(tmp_str,
" ");
678 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_habs") == 0)) {
682 byteflagbits[
k] =
val;
684 strcat(tmp_str, habs_flag_lname[
k]);
686 strcat(tmp_str,
" ");
690 PTB(
setAttr(ds_id,
"flag_meanings", nt_chr, strlen(tmp_str) + 1, (VOIDP) tmp_str));
700 tmpFloat =
l2file->Fonom[
p->prod_ix] * 10.0;
701 PTB(
setAttr(ds_id,
"solar_irradiance", nt_f32, 1, &tmpFloat));
707 (strcmp(
l2file->l2_prod_names[
i],
"flags_sst") != 0) ||
708 (strcmp(
l2file->l2_prod_names[
i],
"flags_sst3") != 0) ||
709 (strcmp(
l2file->l2_prod_names[
i],
"flags_sst4") != 0))) {
711 switch (
p->datatype) {
714 setAttr(ds_id,
"bad_value_scaled", nt_chr, 1,
pbuf);
716 setAttr(ds_id,
"bad_value_unscaled", nt_f32, 1,
pbuf);
720 setAttr(ds_id,
"bad_value_scaled", nt_i16, 1,
pbuf);
722 setAttr(ds_id,
"bad_value_unscaled", nt_f32, 1,
pbuf);
727 setAttr(ds_id,
"bad_value_scaled", nt_f32, 1, &bad_float);
728 setAttr(ds_id,
"bad_value_unscaled", nt_f32, 1, &bad_float);
744 dm[0] = numBands + numBandsIR;
745 strcpy((
char *) dm_name[0], totalBandNumberStr);
749 strcpy((
char *) dm_name[0], bandNumberStr);
774 if (
l2file->orbit_node_lon > -180.0 &&
l2file->orbit_node_lon < 180.0)
776 if (
l2file->orbit_number > 0)
780 PTB(
SetI32GA(ds_id, scanControlPointsStr, numScans));
792 if ((calfile_str = malloc(strlen(
l1_input->calfile) + 1)) ==
NULL) {
793 fprintf(
stderr,
"-E- %s line %d: Unable to copy calfile string.\n",
797 strcpy(calfile_str,
"\0");
800 fprintf(
stderr,
"-E- %s line %d: Unable to copy calfile string.\n",
805 char *token = strtok_r(tmp_calfile,
",", &end_str);
806 while (token !=
NULL) {
809 strcat(calfile_str,
basename(tmp_str));
810 token = strtok_r(
NULL,
",", &end_str);
812 strcat(calfile_str,
", ");
816 PTB(
SetChrGA(ds_id, calibrationDataStr, calfile_str));
828 strcpy(avhrrbird,
"NOAA-");
834 PTB(
SetChrGA(ds_id,
"Conventions",
"CF-1.6 ACDD-1.3"));
838 if (strcmp(
l1_input->pversion,
"Unspecified") != 0) {
840 strcat(buf1,
"/L2/");
873 if(strcmp(
l2file->spatialResolution,
"300 m") == 0)
874 strcat(buf1,
"/FRS");
878 if(strcmp(
l2file->spatialResolution,
"300 m") == 0)
879 strcat(buf1,
"/EFR");
881 strcat(buf1,
"/ERR");
886 PTB(
SetChrGA(ds_id,
"identifier_product_doi_authority",
"http://dx.doi.org"));
887 PTB(
SetChrGA(ds_id,
"identifier_product_doi", doiStr));
892 if (
l2file->node_crossing_time > 0) {
895 PTB(
SetChrGA(ds_id,
"equatorCrossingDateTime", buf1));
912 int32_t grp_id_input_parms;
913 nc_def_grp(
l2file->grp_id[5],
"input_parameters", &grp_id_input_parms);
914 ds_id.
fid = grp_id_input_parms;
917 char *token = strtok_r(tmp_parms,
"\n", &end_str);
918 while (token !=
NULL) {
920 char *
name = strtok_r(token,
"=", &end_token);
922 char *
value = strtok_r(
NULL,
";", &end_token);
924 if (
name[0] !=
'#') {
927 token = strtok_r(
NULL,
"\n", &end_str);
937 if(profileProductsExist)
938 PTB(
SetI32GA(ds_id,
"Number of Profile Levels", numLvlProf));
939 PTB(
SetI32GA(ds_id,
"Number of Scan Lines", numScans));
940 PTB(
SetI32GA(ds_id,
"Pixels per Scan Line", numPixels));
941 PTB(
SetI32GA(ds_id,
"Scene Center Scan Line", cscan));
954 PTB(
writeDS(ds_id,
"wavelength",
l2file->iwave, 0, 0, 0, numBands + numBandsIR, 0, 0));
961 float tmpFobar[numBands];
962 for (
i = 0;
i < numBands;
i++)
963 tmpFobar[
i] =
l2file->Fobar[
i] * 10.0;
964 PTB(
writeDS(ds_id,
"F0", tmpFobar, 0, 0, 0, numBands, 0, 0));
973 PTB(
writeDS(ds_id,
"cntl_pt_cols", ictl, 0, 0, 0, nctl, 0, 0));
974 PTB(
writeDS(ds_id,
"cntl_pt_rows", jctl, 0, 0, 0, numScans, 0, 0));
991 static int32 *buf =
NULL;
992 static float fsol = -1.0;
1000 static int32_t sst3_flag_cnt[
NSSTFLAGS];
1001 static int32_t sst4_flag_cnt[
NSSTFLAGS];
1004 static int32_t qualsst3_flag_cnt[
NQSSTFLAGS];
1005 static int32_t qualsst4_flag_cnt[
NQSSTFLAGS];
1006 static int32_t qaa_flag_cnt[1] = {0};
1007 static int32_t carder_flag_cnt[1] = {0};
1008 static int32_t niwa_flag_cnt[1] = {0};
1009 static const char *flag_lname[1] = {
"PRODFAIL"};
1011 static uint8_t first = 1;
1012 static float last_lat;
1013 static float geobox[4][100];
1014 static int32 geobox_cnt = 0;
1015 float gring_fval[100];
1016 int32 gring_ival[100];
1023 priv->
fp = fopen(l2rec->l1rec->l1file->name,
"r");
1025 fgets(buffer, 2047, priv->
fp);
1027 int32_t fld_cnt = 0;
1032 if (strncmp(buffer,
"/fields=", 8) == 0) {
1033 char *token = strtok(&buffer[8],
",");
1034 memcpy(
value, buffer, 8);
1039 while (token !=
NULL) {
1044 (
char *) (l2rec->l1rec->l1file->private_data+
count*
sizeof(int32_t));
1046 memcpy(&fld_idx, ptr,
sizeof(int32_t));
1049 if (fld_cnt == fld_idx) {
1063 fputs(
"\n", priv->
fp);
1067 token = strtok(
NULL,
",");
1070 }
else if (strncmp(buffer,
"/units=", 7) == 0) {
1071 char *token = strtok(&buffer[8],
",");
1072 memcpy(
value, buffer, 7);
1077 while (token !=
NULL) {
1081 char *ptr = (
char *) (l2rec->l1rec->l1file->private_data+
count*
sizeof(int32_t));
1083 memcpy(&fld_idx, ptr,
sizeof(int32_t));
1086 if (fld_cnt == fld_idx) {
1098 fputs(
"\n", priv->
fp);
1102 token = strtok(
NULL,
",");
1105 }
else if (strncmp(buffer,
"/end_header", 11) == 0){
1106 fputs(buffer, priv->
fp);
1109 fputs(buffer, priv->
fp);
1122 if (
p->slope != 1.0 ||
p->offset != 0.0) {
1129 if (strcmp(
l2file->productInfos[
i]->productName,
"chlor_a") == 0) {
1131 sprintf(buffer,
"%15.6e", l2rec->Rrs[
j]);
1132 fputs(buffer, priv->
fp);
1138 memcpy(&
f,
pbuf,
sizeof(
float));
1139 sprintf(buffer,
"%15.6f\n",
f);
1140 fputs(buffer, priv->
fp);
1152 if (
recnum >= numScans) {
1153 fprintf(
stderr,
"-W- %s line %d: ", __FILE__, __LINE__);
1154 fprintf(
stderr,
"attempt to write rec %d of %d\n",
recnum, numScans);
1158 if (
recnum >= cscan && fsol < 0.0) {
1159 fsol = l2rec->l1rec->fsol;
1164 if ((buf = calloc(numPixels,
sizeof (int32))) ==
NULL) {
1166 "-E- %s line %d: Unable to allocate buffer space.\n",
1167 __FILE__, __LINE__);
1172 if ((
lon = calloc(numPixels,
sizeof (float32))) ==
NULL) {
1174 "-E- %s line %d: Unable to allocate buffer space.\n",
1175 __FILE__, __LINE__);
1180 if ((
lat = calloc(numPixels,
sizeof (float32))) ==
NULL) {
1182 "-E- %s line %d: Unable to allocate buffer space.\n",
1183 __FILE__, __LINE__);
1190 for (
i = 0;
i < nctl;
i++) {
1191 lon [
i] = l2rec->l1rec->lon[ictl[
i] - 1];
1192 lat [
i] = l2rec->l1rec->lat[ictl[
i] - 1];
1201 int32_t year32 = (int32_t)
year;
1202 int32_t day32 = (int32_t)
day;
1203 int32_t msec32 = (int32_t) round(sec * 1.e3);
1206 int32_t good_spix = spix;
1207 int32_t good_epix =
epix;
1209 for(
int i=spix;
i<
epix;
i++) {
1210 if(!l2rec->l1rec->navfail[
i]) {
1215 for(
int i=
epix;
i>=spix;
i--) {
1216 if(!l2rec->l1rec->navfail[
i]) {
1226 PTB(
writeDS(ds_id,
"detnum", &l2rec->l1rec->detnum,
recnum, 0, 0, 1, 1, 1));
1227 PTB(
writeDS(ds_id,
"slon", &(l2rec->l1rec->lon[good_spix]),
recnum, 0, 0, 1, 1, 1));
1228 PTB(
writeDS(ds_id,
"clon", &(l2rec->l1rec->lon[cpix]),
recnum, 0, 0, 1, 1, 1));
1229 PTB(
writeDS(ds_id,
"elon", &(l2rec->l1rec->lon[good_epix]),
recnum, 0, 0, 1, 1, 1));
1230 PTB(
writeDS(ds_id,
"slat", &(l2rec->l1rec->lat[good_spix]),
recnum, 0, 0, 1, 1, 1));
1231 PTB(
writeDS(ds_id,
"clat", &(l2rec->l1rec->lat[cpix]),
recnum, 0, 0, 1, 1, 1));
1232 PTB(
writeDS(ds_id,
"elat", &(l2rec->l1rec->lat[good_epix]),
recnum, 0, 0, 1, 1, 1));
1233 PTB(
writeDS(ds_id,
"csol_z", &(l2rec->l1rec->solz[cpix]),
recnum, 0, 0, 1, 1, 1));
1240 if (outfile_number == 0) {
1241 if ((first == 1) || (
fabs(last_lat - l2rec->l1rec->lat[cpix]) >
GEOBOX_INC)
1242 || (
recnum == (numScans - 1))) {
1244 if (!(l2rec->l1rec->flags[cpix] &
NAVFAIL)) {
1246 if ((!(l2rec->l1rec->flags[good_spix] &
NAVFAIL)
1247 && !(l2rec->l1rec->flags[good_epix] &
NAVFAIL))) {
1250 geobox[0][geobox_cnt] = l2rec->l1rec->lon[good_spix];
1251 geobox[1][geobox_cnt] = l2rec->l1rec->lat[good_spix];
1252 geobox[2][geobox_cnt] = l2rec->l1rec->lon[good_epix];
1253 geobox[3][geobox_cnt] = l2rec->l1rec->lat[good_epix];
1254 last_lat = l2rec->l1rec->lat[cpix];
1260 if ((first == 0) && (
recnum < (numScans - 1))
1261 && (l2rec->l1rec->lat[cpix] != last_lat)) {
1262 if (!(l2rec->l1rec->flags[cpix] &
NAVFAIL)) {
1264 if ((!(l2rec->l1rec->flags[good_spix] &
NAVFAIL)
1265 && !(l2rec->l1rec->flags[good_epix] &
NAVFAIL))) {
1266 geobox[0][geobox_cnt] = l2rec->l1rec->lon[good_spix];
1267 geobox[1][geobox_cnt] = l2rec->l1rec->lat[good_spix];
1268 geobox[2][geobox_cnt] = l2rec->l1rec->lon[good_epix];
1269 geobox[3][geobox_cnt] = l2rec->l1rec->lat[good_epix];
1273 if (
recnum == (numScans - 1) && geobox_cnt == 1) geobox_cnt++;
1280 for (
i = 0;
i <
l2file->tot_prod;
i++) {
1282 if (!strcmp(
l2file->l2_prod_names[
i],
"detnum") ||
1283 !strcmp(
l2file->l2_prod_names[
i],
"mside") ||
1284 !strcmp(
l2file->l2_prod_names[
i],
"year") ||
1285 !strcmp(
l2file->l2_prod_names[
i],
"day") ||
1286 !strcmp(
l2file->l2_prod_names[
i],
"msec") ||
1287 !strcmp(
l2file->l2_prod_names[
i],
"slon") ||
1288 !strcmp(
l2file->l2_prod_names[
i],
"clon") ||
1289 !strcmp(
l2file->l2_prod_names[
i],
"elon") ||
1290 !strcmp(
l2file->l2_prod_names[
i],
"slat") ||
1291 !strcmp(
l2file->l2_prod_names[
i],
"clat") ||
1292 !strcmp(
l2file->l2_prod_names[
i],
"elat") ||
1293 !strcmp(
l2file->l2_prod_names[
i],
"csol_z") ||
1294 !strcmp(
l2file->l2_prod_names[
i],
"latitude") ||
1295 !strcmp(
l2file->l2_prod_names[
i],
"longitude") ||
1296 !strcmp(
l2file->l2_prod_names[
i],
"tilt")
1304 if (
p->slope != 1.0 ||
p->offset != 0.0) {
1308 if ((strcmp(
l2file->l2_prod_names[
i],
"flags_sst") == 0)) {
1310 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_sst4") == 0)) {
1312 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_sst3") == 0)) {
1314 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_giop") == 0)) {
1316 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_qaa") == 0)) {
1318 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_carder") == 0)) {
1320 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_niwa") == 0)) {
1322 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"qual_sst") == 0)) {
1324 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"qual_sst3") == 0)) {
1326 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"qual_sst4") == 0)) {
1334 num_3d = ( strcmp(
l2file->productInfos[
i]->category,
1335 "Anc_profile" ) == 0 ) ? numLvlProf : numBands;
1337 num_3d = ( strcmp(
l2file->productInfos[
i]->category,
1338 "Reflectance_loc" ) == 0 ) ? n_refl_loc : num_3d;
1340 recnum,0,0,1,numPixels,num_3d) );
1341 }
else if (
p->rank == 2) {
1343 recnum, 0, 0, 1, numPixels, 1));
1358 if (
recnum == (numScans - 1)) {
1367 PTB(
SetF64GA(ds_id,
"earth_sun_distance_correction", fsol));
1370 int32_t grp_id_flag_percentages;
1371 ds_id.
sid = NC_GLOBAL;
1375 for (
i = 0;
i <
l2file->tot_prod;
i++) {
1376 if ((strcmp(
l2file->l2_prod_names[
i],
"flags_sst") == 0)) {
1377 nc_def_grp(
l2file->grp_id[5],
"sst_flag_percentages", &grp_id_flag_percentages);
1378 ds_id.
fid = grp_id_flag_percentages;
1379 printf(
"\nSST: Percentage of pixels flagged:\n");
1386 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_sst4") == 0)) {
1387 nc_def_grp(
l2file->grp_id[5],
"sst4_flag_percentages", &grp_id_flag_percentages);
1388 ds_id.
fid = grp_id_flag_percentages;
1389 printf(
"\nSST4: Percentage of pixels flagged:\n");
1391 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_sst3") == 0)) {
1392 nc_def_grp(
l2file->grp_id[5],
"sst3_flag_percentages", &grp_id_flag_percentages);
1393 ds_id.
fid = grp_id_flag_percentages;
1394 printf(
"\nSST3: Percentage of pixels flagged:\n");
1396 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_giop") == 0)) {
1397 nc_def_grp(
l2file->grp_id[5],
"giop_flag_percentages", &grp_id_flag_percentages);
1398 ds_id.
fid = grp_id_flag_percentages;
1399 printf(
"\nGIOP: Percentage of pixels flagged:\n");
1401 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_qaa") == 0)) {
1402 nc_def_grp(
l2file->grp_id[5],
"qaa_flag_percentages", &grp_id_flag_percentages);
1403 ds_id.
fid = grp_id_flag_percentages;
1404 printf(
"\nQAA: Percentage of pixels flagged:\n");
1405 write_flag_pcnts(ds_id, fp_meta, qaa_flag_cnt, 1, flag_lname, numScans, numPixels);
1406 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_carder") == 0)) {
1407 nc_def_grp(
l2file->grp_id[5],
"carder_flag_percentages", &grp_id_flag_percentages);
1408 ds_id.
fid = grp_id_flag_percentages;
1409 printf(
"\nCARDER: Percentage of pixels flagged:\n");
1410 write_flag_pcnts(ds_id, fp_meta, carder_flag_cnt, 1, flag_lname, numScans, numPixels);
1411 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"flags_niwa") == 0)) {
1412 nc_def_grp(
l2file->grp_id[5],
"niwa_flag_percentages", &grp_id_flag_percentages);
1413 ds_id.
fid = grp_id_flag_percentages;
1414 printf(
"\nNIWA: Percentage of pixels flagged:\n");
1415 write_flag_pcnts(ds_id, fp_meta, niwa_flag_cnt, 1, flag_lname, numScans, numPixels);
1416 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"qual_sst") == 0)) {
1417 nc_def_grp(
l2file->grp_id[5],
"qual_sst_percentages", &grp_id_flag_percentages);
1418 ds_id.
fid = grp_id_flag_percentages;
1419 printf(
"\nQUAL_SST: Percentage of pixels flagged:\n");
1421 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"qual_sst4") == 0)) {
1422 nc_def_grp(
l2file->grp_id[5],
"qual_sst4_percentages", &grp_id_flag_percentages);
1423 ds_id.
fid = grp_id_flag_percentages;
1424 printf(
"\nQUAL_SST4: Percentage of pixels flagged:\n");
1426 }
else if ((strcmp(
l2file->l2_prod_names[
i],
"qual_sst3") == 0)) {
1427 nc_def_grp(
l2file->grp_id[5],
"qual_sst3_percentages", &grp_id_flag_percentages);
1428 ds_id.
fid = grp_id_flag_percentages;
1429 printf(
"\nQUAL_SST3: Percentage of pixels flagged:\n");
1434 nc_def_grp(
l2file->grp_id[5],
"flag_percentages", &grp_id_flag_percentages);
1435 ds_id.
fid = grp_id_flag_percentages;
1436 printf(
"\nPercentage of pixels flagged:\n");
1443 gring_fval[0] = geobox[0][0];
1444 for (
i = 0;
i < geobox_cnt;
i++) {
1445 gring_fval[
j++] = geobox[2][
i];
1447 for (
i = 0;
i < geobox_cnt - 1;
i++) {
1448 gring_fval[
j++] = geobox[0][geobox_cnt - 1 -
i];
1451 PTB(
setAttr(ds_id,
"gringpointlongitude", NC_FLOAT,
j, (VOIDP) gring_fval));
1454 gring_fval[0] = geobox[1][0];
1456 for (
i = 0;
i < geobox_cnt;
i++) {
1457 gring_ival[
j] =
j + 1;
1458 gring_fval[
j++] = geobox[3][
i];
1460 for (
i = 0;
i < geobox_cnt - 1;
i++) {
1461 gring_ival[
j] =
j + 1;
1462 gring_fval[
j++] = geobox[1][geobox_cnt - 1 -
i];
1465 PTB(
setAttr(ds_id,
"gringpointlatitude", NC_FLOAT,
j, (VOIDP) gring_fval));
1466 PTB(
setAttr(ds_id,
"gringpointsequence", NC_INT,
j, (VOIDP) gring_ival));
1473 PTB(
SetF64GA(ds_id,
"Earth-Sun Distance Correction", fsol));
1476 printf(
"\nPercentage of pixels flagged:\n");
1478 flag_perc[
i] = ((
float) l2_flag_cnt[
i]) / numScans / numPixels * 100.0;
1479 printf(
"Flag #%2d: %16s %10d %8.4f\n",
1480 i + 1, l2_flag_lname[
i], l2_flag_cnt[
i], flag_perc[
i]);
1481 if (fp_meta !=
NULL)
1482 fprintf(fp_meta,
"Flag #%2d: %16s %10d %8.4f\n",
1483 i + 1, l2_flag_lname[
i], l2_flag_cnt[
i], flag_perc[
i]);
1519 fprintf(
stderr,
"-E- %s line %d: endDS(%d) failed for file, %s.\n",
1520 __FILE__, __LINE__, ds_id.
fid,
l2file->name);
1534 free(
l2file->productInfos);
1539 if (fp_meta !=
NULL)
1551 for (ip = 0; ip <
npix; ip++) {
1553 for (
i = 0;
i < nflags;
i++) {
1566 for (ip = 0; ip <
npix; ip++) {
1568 for (
i = 0;
i < nflags;
i++) {
1581 for (ip = 0; ip <
npix; ip++) {
1583 for (
i = 0;
i < nflags;
i++) {
1591 int write_flag_pcnts(
idDS ds_id, FILE *fpmeta, int32_t *flag_cnt, int32_t nflags,
const char *
const flag_lname[], int32_t numScans, int32_t numPixels) {
1595 flag_perc = (
float *) calloc(nflags,
sizeof (
float));
1598 for (
i = 0;
i < nflags;
i++) {
1599 flag_perc[
i] = ((
float) flag_cnt[
i]) / numScans / numPixels * 100.0;
1600 printf(
"Flag #%2d: %16s %10d %8.4f\n",
i + 1, flag_lname[
i], flag_cnt[
i], flag_perc[
i]);
1601 if (fp_meta !=
NULL)
1602 fprintf(fpmeta,
"Flag #%2d: %16s %10d %8.4f\n",
i + 1, flag_lname[
i], flag_cnt[
i], flag_perc[
i]);
1604 PTB(
setAttr(ds_id, flag_lname[
i], NC_FLOAT, 1, (VOIDP) (flag_perc +
i)));
1612 int32_t
i, sumflags = 0;
1615 flag_perc = (
float *) calloc(nflags,
sizeof (
float));
1617 for (
i = 0;
i < nflags;
i++) sumflags += flag_cnt[
i];
1620 for (
i = 0;
i < nflags;
i++) {
1621 flag_perc[
i] = ((
float) flag_cnt[
i]) / sumflags * 100.0;
1622 printf(
"Flag #%2d: %16s %10d %8.4f\n",
i + 1, flag_lname[
i], flag_cnt[
i], flag_perc[
i]);
1623 if (fp_meta !=
NULL)
1624 fprintf(fpmeta,
"Flag #%2d: %16s %10d %8.4f\n",
i + 1, flag_lname[
i], flag_cnt[
i], flag_perc[
i]);
1626 PTB(
setAttr(ds_id, flag_lname[
i], NC_FLOAT, 1, (VOIDP) (flag_perc +
i)));