14 #define SGLI_NVNIR_LG 2
15 #define SGLI_NVNIR_GEOM 2
20 static int32_t *scan_times, swirir_exist, resqkm, resample, resamp_1km;
31 static uint16_t *ui16_buf_1km;
32 static int32_t
npix, npix_1km,
nscan, npix_tie, nscan_tie;
33 static int32_t npix_tie_1km, nscan_tie_1km;
34 static h5io_str vnir_fid, swir_ir_fid, vnir_hg_dsid[
SGLI_NVNIR];
37 static double st_day_unix;
38 static char *geo_q, *swir_q, *sen_q, *sol_q;
39 static gsl_interp_accel *accel_x, *accel_y;
40 static gsl_spline2d *geo_int_id[3];
41 static double *xa, *ya, *xa_1km, *ya_1km;
42 static double *geo_x, *geo_y, *geo_z;
43 static float *tie_el, *tie_az;
44 static grid_res_str grid_res[2];
45 static band_geom_str band_geom_vnir[
SGLI_NVNIR];
47 static band_geom_str band_geom_swir[
SGLI_NSWIR];
48 static band_geom_str band_geom_nominal[2];
52 sgli_t*
data = (sgli_t*) calloc(1,
sizeof (sgli_t));
54 fprintf(
stderr,
"-E- %s line %d: unable to allocate private data for sgli\n",
83 int32_t stage_pass = 0;
94 if (
h5io_set_grp(fid,
"Global_attributes", &g_id) == 0) {
96 if (
h5io_rd_attr(&g_id,
"Satellite", (
void *) str_store) == 0) {
97 if (strncmp(str_store,
"Global Change Observation Mission - Climate (GCOM-C)", 52) == 0) {
104 if (
h5io_rd_attr(&g_id,
"Sensor", (
void *) str_store) == 0) {
105 if (strncmp(str_store,
"Second-generation Global Imager (SGLI)", 38) == 0) {
110 if ((stage_pass == 1) && (
h5io_attr_exist(&g_id,
"Product_level") == 0)) {
112 if (
h5io_rd_attr(&g_id,
"Product_level", (
void *) str_store) == 0) {
113 if (strncmp(str_store,
"Level-1B", 8) == 0) {
118 if ((stage_pass == 1) && (
h5io_attr_exist(&g_id,
"Product_name") == 0)) {
120 if (
h5io_rd_attr(&g_id,
"Product_name", (
void *) str_store) == 0) {
121 if (strncmp(str_store,
"Top of atmosphere radiance (reflectance)", 40) == 0) {
128 return ( stage_pass == 0) ? 1 : 0;
132 int *dim_siz, uint16_t *mx_dn,
float *
scale,
float *off,
float *sat)
157 H5T_class_t h5_class;
162 fprintf(
stderr,
"%s, %d, E - Cannot open dataset: %s\n", __FILE__,
167 if (
h5io_info(dsid,
NULL, &h5_class, &h5_native_typ, &ndim, dim_siz,
169 fprintf(
stderr,
"%s, %d, E - h5io_info failure, dataset: %s\n",
170 __FILE__, __LINE__, ds_nam);
174 if (
h5io_rd_attr(dsid,
"Maximum_valid_DN", (uint16_t *) mx_dn) != 0) {
175 fprintf(
stderr,
"%s, %d, E - Cannot read Maximum_valid_DN for %s\n",
176 __FILE__, __LINE__, ds_nam);
180 fprintf(
stderr,
"%s, %d, E - Cannot read Slope for %s\n",
181 __FILE__, __LINE__, ds_nam);
185 fprintf(
stderr,
"%s, %d, E - Cannot read Offset for %s\n",
186 __FILE__, __LINE__, ds_nam);
189 if (
h5io_rd_attr(dsid,
"Saturation_radiance", (
float *) sat) != 0) {
190 fprintf(
stderr,
"%s, %d, E - Cannot read Saturation_radiance for %s\n",
191 __FILE__, __LINE__, ds_nam);
219 int32_t ib, ip, il, ipl, npix_tie, nlin_tie;
220 int16_t *tie_el_16, *tie_az_16;
222 double *geo[3], el_cvt, az_cvt, xy_mod, *xa, *ya;
227 tie_el_16 = (int16_t *) tie_el;
228 tie_az_16 = (int16_t *) tie_az;
233 for (ib = 0; ib <
nbands; ib++) {
234 npix_tie = band_geom[ib].grd_desc->npix_tie;
235 nlin_tie = band_geom[ib].grd_desc->nscn_sub_tie;
236 xa = band_geom[ib].grd_desc->xa;
237 ya = band_geom[ib].grd_desc->ya;
238 if ((band_geom[ib].
qual =
239 (
char *) calloc(npix_tie * nlin_tie,
sizeof (
char))) ==
NULL) {
241 "%s, %d, E - Failed to allocate vnir sensor, solar quality storage\n",
248 start[0] = band_geom[ib].grd_desc->tie_st_lin;
254 (
void *) tie_el_16) != 0) ||
256 (
void *) tie_az_16) != 0)) {
257 fprintf(
stderr,
"%s, %d, E Error reading vnir view angle tie points\n",
262 for (il = 0; il < nlin_tie; il++) {
263 for (ip = 0; ip < npix_tie; ip++) {
264 ipl = ip + il * npix_tie;
266 el_cvt = band_geom[ib].offset[1] +
267 band_geom[ib].scale[1] * tie_el_16[ipl];
268 az_cvt = band_geom[ib].offset[0] +
269 band_geom[ib].scale[0] * tie_az_16[ipl];
271 if ((el_cvt < 0.) || (el_cvt > 180.) ||
272 (az_cvt < -180.) || (az_cvt > 180.)) {
275 band_geom[ib].qual[ipl] = 1;
278 geo_x[ipl] = xy_mod * cos(
deg2rad(az_cvt));
279 geo_y[ipl] = xy_mod * sin(
deg2rad(az_cvt));
280 geo_z[ipl] = cos(
deg2rad(el_cvt));
287 for (ip = 0; ip < 3; ip++) {
288 band_geom[ib].int_id_sen[ip] =
289 gsl_spline2d_alloc(gsl_interp2d_bicubic, npix_tie, nlin_tie);
290 if ((gsl_spline2d_init(band_geom[ib].int_id_sen[ip], xa,
291 ya, geo[ip], npix_tie, nlin_tie)) != 0) {
292 fprintf(
stderr,
"%s, %d, E vnir gsl_spline2d_init error\n",
305 float *azi_v,
float *zen_v,
char *
qual)
330 double xvec, yvec, zvec;
332 int32_t ipb, scan_tie, tie_en_line, npix_tie, nscan_tie;
333 size_t ip_tie, il_tie;
335 npix_tie = band_geom->grd_desc->npix_tie;
336 nscan_tie = band_geom->grd_desc->nscn_sub_tie;
337 tie_en_line = band_geom->grd_desc->tie_st_lin + nscan_tie - 1;
338 scan_tie = ( (
scan >= band_geom->grd_desc->resamp * tie_en_line)) ?
339 band_geom->grd_desc->resamp * tie_en_line - 1 :
scan;
341 if ((gsl_spline2d_eval_e(band_geom->int_id_sen[0], ip,
342 scan, accel_x, accel_y, &xvec) != 0) ||
343 (gsl_spline2d_eval_e(band_geom->int_id_sen[1], ip,
344 scan, accel_x, accel_y, &yvec) != 0) ||
345 (gsl_spline2d_eval_e(band_geom->int_id_sen[2], ip,
346 scan, accel_x, accel_y, &zvec) != 0)) {
347 fprintf(
stderr,
"%s, %d, E Error in vnir_lg gsl_spline2d_eval\n",
354 ip_tie = gsl_interp_accel_find(accel_x, band_geom->grd_desc->xa, npix_tie, ip);
355 il_tie = gsl_interp_accel_find(accel_y, band_geom->grd_desc->ya, nscan_tie, scan_tie);
356 ipb = ip_tie + il_tie * npix_tie;
358 if ((band_geom->qual[ipb] != 0) ||
359 (band_geom->qual[ ipb + 1 ] != 0) ||
360 (band_geom->qual[ ipb + npix_tie ] != 0) ||
361 (band_geom->qual[ ipb + 1 + npix_tie ] != 0))
367 char *cptr, *vis_nir_file, *swir_ir_file, ds_nam[FILENAME_MAX];
368 char str_store[FILENAME_MAX];
370 H5T_class_t h5_class;
373 int ndim, sto_len, dim_siz[4];
375 int st_year, st_mon, st_day;
376 char *vnir_hg_bnam[] ={
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"09",
"10"};
377 char *vnir_lg_bnam[] = {
"08",
"11"};
378 char *swir_bnam[] = {
"01",
"02",
"03",
"04"};
379 char *vnir_geom_nam[] = {
"Latitude",
"Longitude"};
380 char *view_nam[] = {
"Sensor",
"Solar"};
381 char *ang_nam[] = {
"azimuth",
"zenith"};
382 char *sen_ang[2] = {
"Sensor_azimuth",
"Sensor_zenith"};
392 vis_nir_file =
l1file->name;
393 swir_ir_file =
data->swir_ir_file;
395 if (swir_ir_file[0] == 0) {
396 strcpy(swir_ir_file, vis_nir_file);
397 if ((cptr = strstr(swir_ir_file,
"VNRD")) ==
NULL) {
398 printf(
"%s, %d, I - VIS / NIR file has non-standard name - cannot create SWIR/IR file name\n", __FILE__, __LINE__);
399 printf(
"VIS/NIR file name: %s\n", vis_nir_file);
400 printf(
"Processing will only be done with the VIS/NIR data\n");
402 memcpy(cptr,
"IRS", 3);
408 if (
h5io_openr(vis_nir_file, 0, &vnir_fid) != 0) {
409 fprintf(
stderr,
"%s, %d, E - Failure to open %s\n", __FILE__,
410 __LINE__, vis_nir_file);
414 for (ibnd = 0; ibnd < n_vnir; ibnd++) {
415 sprintf(ds_nam,
"Image_data/Lt_VN%s", vnir_hg_bnam[ibnd]);
417 dim_siz, (vnir_mx_dn + ibnd), (vnir_scale + ibnd),
418 (vnir_off + ibnd), (vnir_sat + ibnd)) != 0)
426 if (
h5io_rd_attr((vnir_hg_dsid + ibnd),
"Spatial_resolution",
428 fprintf(
stderr,
"%s, %d, E - Cannot read band resolution\n", __FILE__,
435 if ((flt_buf = (
float *) malloc(
npix *
sizeof (
float))) ==
NULL) {
436 fprintf(
stderr,
"%s, %d, E - Cannot allocate the SGLI read buffer\n",
441 if ((dim_siz[1] !=
npix) || (dim_siz[0] !=
nscan)) {
442 fprintf(
stderr,
"%s, %d, E - Vis/NIR band array size mismatch\n",
444 fprintf(
stderr,
"for band %s\n", ds_nam);
450 for (ibnd = 0; ibnd < n_vnir_lg; ibnd++) {
451 sprintf(ds_nam,
"Image_data/Lt_VN%s", vnir_lg_bnam[ibnd]);
454 dim_siz, (vnir_lg_mx_dn + ibnd), (vnir_lg_scale + ibnd),
455 (vnir_lg_off + ibnd), (vnir_lg_sat + ibnd)) != 0)
458 if ((dim_siz[1] !=
npix) || (dim_siz[0] !=
nscan)) {
459 fprintf(
stderr,
"%s, %d, E - Vis/NIR band array size mismatch\n",
461 fprintf(
stderr,
"for band %s\n", ds_nam);
466 if (
h5io_set_grp(&vnir_fid,
"Global_attributes", &g_id) != 0) {
467 fprintf(
stderr,
"%s, %d, E - Cannot set Global_attributes group\n",
471 if (
h5io_rd_attr(&g_id,
"Scene_start_time", (
void *) str_store) != 0) {
472 fprintf(
stderr,
"%s, %d, E - Cannot read Scene_start_time\n",
477 sscanf(str_store,
"%4d%2d%2d", &st_year, &st_mon, &st_day);
478 st_day_unix =
ymds2unix((int16_t) st_year, (int16_t) st_mon, (int16_t) st_day,
482 strcpy(ds_nam,
"Image_data/Line_msec");
483 if (
h5io_set_ds(&vnir_fid, ds_nam, &gen_dsid) != 0) {
484 fprintf(
stderr,
"%s, %d, E - Cannot open dataset: %s\n", __FILE__,
488 scan_times = (int32_t *) malloc(
nscan *
sizeof (int32_t));
489 if (
h5io_rd_ds(&gen_dsid, (
void *) scan_times) != 0) {
490 fprintf(
stderr,
"%s, %d, E - Error reading dataset: %s\n", __FILE__,
495 fprintf(
stderr,
"%s, %d, E - Error closing dataset: %s\n", __FILE__,
502 for (ibnd = 0; ibnd < n_vnir_geom; ibnd++) {
503 sprintf(ds_nam,
"Geometry_data/%s", vnir_geom_nam[ibnd]);
504 if (
h5io_set_ds(&vnir_fid, ds_nam, (vnir_geom_dsid + ibnd)) != 0) {
505 fprintf(
stderr,
"%s, %d, E - Cannot open dataset: %s\n", __FILE__,
511 if (
h5io_info((vnir_geom_dsid + ibnd),
NULL, &h5_class, &h5_native_typ,
512 &ndim, dim_siz, &sto_len) != 0) {
513 fprintf(
stderr,
"%s, %d, E - h5io_info failure\n", __FILE__,
517 npix_tie = dim_siz[1];
518 nscan_tie = dim_siz[0];
519 if (
h5io_rd_attr((vnir_geom_dsid + ibnd),
"Resampling_interval",
520 (
void *) &resample) != 0) {
522 "%s, %d, E - Failed to read resampling for geo data\n",
528 (
void *) (vnir_geom_scale + ibnd)) != 0) ||
530 (
void *) (vnir_geom_off + ibnd)) != 0)) {
532 "%s, %d, E - Failed to read scale or offset for geo data\n",
541 for (ibnd = 0; ibnd < 2; ibnd++) {
542 for (ip = 0; ip < 2; ip++) {
543 sprintf(ds_nam,
"Geometry_data/%s_%s",
544 view_nam[ibnd], ang_nam[ip]);
546 &band_geom_nominal[ibnd].dsid[ip]) != 0) {
547 fprintf(
stderr,
"%s, %d, E - Cannot open dataset: %s\n", __FILE__,
551 if ((
h5io_rd_attr(&band_geom_nominal[ibnd].dsid[ip],
"Slope",
552 (
void *) (&band_geom_nominal[ibnd].
scale[ip])) != 0) ||
553 (
h5io_rd_attr(&band_geom_nominal[ibnd].dsid[ip],
"Offset",
554 (
void *) (&band_geom_nominal[ibnd].
offset[ip])) != 0)) {
556 "%s, %d, E - Failed to read scale or offset for nominal geo data\n",
562 if (
h5io_info(&band_geom_nominal[ibnd].dsid[ip],
NULL, &h5_class,
563 &h5_native_typ, &ndim, dim_siz, &sto_len) != 0) {
564 fprintf(
stderr,
"%s, %d, E - h5io_info failure for band_geom_nominal\n",
568 if ((dim_siz[1] != npix_tie) || (dim_siz[0] != nscan_tie)) {
570 "%s, %d, E - Unexpected band_geom_nominal sizes found\n",
575 band_geom_nominal[ibnd].grd_desc = grid_res;
582 for (ibnd = 0; ibnd < n_vnir; ibnd++) {
583 for (ip = 0; ip < 2; ip++) {
584 sprintf(ds_nam,
"Geometry_data/%s_VN%s",
585 sen_ang[ip], vnir_hg_bnam[ibnd]);
587 &band_geom_vnir[ibnd].dsid[ip]) != 0) {
588 fprintf(
stderr,
"%s, %d, E - Cannot open dataset: %s\n", __FILE__,
592 if ((
h5io_rd_attr(&band_geom_vnir[ibnd].dsid[ip],
"Slope",
593 (
void *) (&band_geom_vnir[ibnd].
scale[ip])) != 0) ||
595 (
void *) (&band_geom_vnir[ibnd].
offset[ip])) != 0)) {
597 "%s, %d, E - Failed to read scale or offset for geo data\n",
603 if (
h5io_info(&band_geom_vnir[ibnd].dsid[ip],
NULL, &h5_class,
604 &h5_native_typ, &ndim, dim_siz, &sto_len) != 0) {
605 fprintf(
stderr,
"%s, %d, E - h5io_info failure for geom_per_band\n",
609 if ((dim_siz[1] != npix_tie) || (dim_siz[0] != nscan_tie)) {
610 fprintf(
stderr,
"%s, %d, E - Unexpected geom_per_band sizes found\n", __FILE__, __LINE__);
614 band_geom_vnir[ibnd].grd_desc = grid_res;
617 for (ibnd = 0; ibnd < n_vnir_lg; ibnd++) {
618 for (ip = 0; ip < 2; ip++) {
619 sprintf(ds_nam,
"Geometry_data/%s_VN%s",
620 sen_ang[ip], vnir_lg_bnam[ibnd]);
622 &band_geom_vnir_lg[ibnd].dsid[ip]) != 0) {
623 fprintf(
stderr,
"%s, %d, E - Cannot open dataset: %s\n", __FILE__,
627 if ((
h5io_rd_attr(&band_geom_vnir_lg[ibnd].dsid[ip],
"Slope",
628 (
void *) (&band_geom_vnir_lg[ibnd].
scale[ip])) != 0) ||
629 (
h5io_rd_attr(&band_geom_vnir_lg[ibnd].dsid[ip],
"Offset",
630 (
void *) (&band_geom_vnir_lg[ibnd].
offset[ip])) != 0)) {
632 "%s, %d, E - Failed to read scale or offset for geo data\n",
638 if (
h5io_info(&band_geom_vnir_lg[ibnd].dsid[ip],
NULL, &h5_class,
639 &h5_native_typ, &ndim, dim_siz, &sto_len) != 0) {
641 "%s, %d, E - h5io_info failure for vis lg geom_per_band\n",
645 if ((dim_siz[1] != npix_tie) || (dim_siz[0] != nscan_tie)) {
647 "%s, %d, E - Unexpected vis_lg geom_per_band sizes found\n",
652 band_geom_vnir_lg[ibnd].grd_desc = grid_res;
657 if (swir_ir_file[0] != 0) {
659 if (
h5io_openr(swir_ir_file, 0, &swir_ir_fid) != 0) {
660 fprintf(
stderr,
"%s, %d, I - The SWIR/IR file: %s\n",
661 __FILE__, __LINE__, swir_ir_file);
662 fprintf(
stderr,
"does not exist. Using VIS/NIR file only\n");
665 fprintf(
stderr,
"%s, %d, I - SGLI SWIR/IR file will be used\n",
667 fprintf(
stderr,
" NAME: %s\n", swir_ir_file );
670 "%s, %d, E - SWIR/IR file: %s is not a SGLI L1b file\n", __FILE__,
671 __LINE__, swir_ir_file);
675 for (ibnd = 0; ibnd < n_swir; ibnd++) {
676 sprintf(ds_nam,
"Image_data/Lt_SW%s", swir_bnam[ibnd]);
678 dim_siz, (swir_mx_dn + ibnd), (swir_scale + ibnd),
679 (swir_off + ibnd), (swir_sat + ibnd)) != 0)
681 if ((resqkm == 1) && (ibnd != 2)) {
685 npix_1km = dim_siz[1];
687 if (dim_siz[1] != npix_1km) {
688 fprintf(
stderr,
"%s, %d, E - image data size mismatch between\n",
690 fprintf(
stderr,
"VIS/NIR file: %s\nand SWIR/IR file: %s\n",
691 vis_nir_file, swir_ir_file);
696 if ((dim_siz[1] !=
npix) || (dim_siz[0] !=
nscan)) {
698 "%s, %d, E - image data size mismatch between\n",
700 fprintf(
stderr,
"VIS/NIR file: %s\nand SWIR/IR file: %s\n",
701 vis_nir_file, swir_ir_file);
707 strcpy(ds_nam,
"Geometry_data/Latitude");
708 if (
h5io_set_ds(&swir_ir_fid, ds_nam, &gen_dsid) != 0) {
709 fprintf(
stderr,
"%s, %d, E - Cannot open dataset: %s\n", __FILE__,
714 &ndim, dim_siz, &sto_len) != 0) {
715 fprintf(
stderr,
"%s, %d, E - h5io_info failure\n", __FILE__,
719 if ((npix_tie != dim_siz[1]) || (nscan_tie != dim_siz[0])) {
721 "%s, %d, E - tie point data size mismatch between VIS/NIR file:\n",
723 fprintf(
stderr,
"VIS/NIR file: %s\nand SWIR/IR file: %s\n",
724 vis_nir_file, swir_ir_file);
731 for (ibnd = 0; ibnd < n_swir; ibnd++) {
732 for (ip = 0; ip < 2; ip++) {
733 sprintf(ds_nam,
"Geometry_data/%s_SW%s",
734 sen_ang[ip], swir_bnam[ibnd]);
736 &band_geom_swir[ibnd].dsid[ip]) != 0) {
737 fprintf(
stderr,
"%s, %d, E - Cannot open dataset: %s\n", __FILE__,
741 if ((
h5io_rd_attr(&band_geom_swir[ibnd].dsid[ip],
"Slope",
742 (
void *) (&band_geom_swir[ibnd].
scale[ip])) != 0) ||
744 (
void *) (&band_geom_swir[ibnd].
offset[ip])) != 0)) {
746 "%s, %d, E - Failed to read scale or offset for geo data\n",
751 if (
h5io_info(&band_geom_swir[ibnd].dsid[ip],
NULL, &h5_class,
752 &h5_native_typ, &ndim, dim_siz, &sto_len) != 0) {
754 "%s, %d, E - h5io_info failure for swir geom_per_band\n",
758 if ((resqkm == 0) || (ibnd == 2) ) {
759 if ((dim_siz[1] != npix_tie) || (dim_siz[0] != nscan_tie)) {
761 "%s, %d, E - Unexpected swir geom_per_band sizes found\n",
765 band_geom_swir[ibnd].grd_desc = grid_res;
768 if( ( ibnd == 0 ) && ( ip == 0 ) ) {
769 npix_tie_1km = dim_siz[1];
770 nscan_tie_1km = dim_siz[0];
771 resamp_1km = resample * 4;
773 if ((dim_siz[1] != npix_tie_1km) || (dim_siz[0] != nscan_tie_1km)) {
775 "%s, %d, E - Unexpected swir geom_per_band sizes found\n",
780 band_geom_swir[ibnd].grd_desc = ( grid_res + 1 );
794 int32_t ip_tie, il_tie, sscan, escan, tie_st_line, tie_st_lin_1km;
795 int32_t il, ipl, ip_1km, ip_off, scan_1km, isub, scan_tie, nscn_sub_tie;
796 uint16_t *ui16_buf = (uint16_t *) flt_buf;
797 int32_t
start[2],
count[2], ib, ip, ipb, ib_lg, ilr, iv;
798 int32_t lt_lg_state, lt_hg_state;
799 double lat_cvt, lon_cvt;
800 double xvec, yvec, zvec, xy_mod, *geo[3];
804 static int32_t firstcall = 1, cur_scan_1km[3] = {-1, -1, -1};
805 static int32_t tie_en_line, tie_en_lin_1km;
808 LT_BAD, LT_SAT, LT_NORM
811 uint16_t lt_strip = pow(2, 14) - 1;
812 float w_m2_to_mw_cm2 = 0.1;
825 if (firstcall == 1) {
833 tie_st_line = sscan / resample - 1;
834 tie_en_line = escan / resample + 2;
836 tie_st_line = (tie_st_line < 0) ? 0 : tie_st_line;
837 tie_en_line = (tie_en_line >= nscan_tie) ? nscan_tie - 1 : tie_en_line;
842 if (tie_en_line - tie_st_line < 3) {
843 if (tie_en_line >= 3)
844 tie_st_line = tie_en_line - 3;
846 tie_en_line = tie_st_line + 3;
848 if ((tie_st_line < 0) || (tie_en_line > nscan_tie - 1)) {
849 fprintf(
stderr,
"%s, %d, E - L1 file has fewer than 4 tie point lines\n",
851 fprintf(
stderr,
" Spline interpolation is not possible\n");
858 nscn_sub_tie = tie_en_line - tie_st_line + 1;
859 xa = (
double *) malloc(npix_tie *
sizeof (
double));
860 ya = (
double *) malloc(nscn_sub_tie *
sizeof (
double));
861 grid_res[0].nscn_sub_tie = nscn_sub_tie;
864 grid_res[0].npix_tie = npix_tie;
865 grid_res[0].tie_st_lin = tie_st_line;
866 grid_res[0].resamp = resample;
869 start[0] = tie_st_line;
871 count[0] = nscn_sub_tie;
874 if (((tie_el = (
float *) malloc(npix_tie * nscn_sub_tie *
sizeof (
float)))
876 ((tie_az = (
float *) malloc(npix_tie * nscn_sub_tie *
sizeof (
float)))
878 ((geo_x = (
double *) malloc(npix_tie * nscn_sub_tie *
sizeof (
double)))
880 ((geo_y = (
double *) malloc(npix_tie * nscn_sub_tie *
sizeof (
double)))
882 ((geo_z = (
double *) malloc(npix_tie * nscn_sub_tie *
sizeof (
double)))
884 ((geo_q = (
char *) calloc(npix_tie * nscn_sub_tie,
sizeof (
char)))
886 fprintf(
stderr,
"%s, %d, E - Failed to allocate lat, lon storage\n",
893 fprintf(
stderr,
"%s, %d, E Error reading latitude tie points\n",
899 fprintf(
stderr,
"%s, %d, E Error reading longitude tie points\n",
905 for (il = 0; il < nscn_sub_tie; il++) {
906 ya[il] = (il +
start[0]) * resample;
907 for (ip = 0; ip < npix_tie; ip++) {
908 if (il == 0) xa[ip] = ip * resample;
909 ipl = ip + il * npix_tie;
914 lat_cvt = tie_el[ipl];
915 lon_cvt = tie_az[ipl];
916 if ((lat_cvt < -90.) || (lat_cvt > 90.) ||
917 (lon_cvt < -180.) || (lon_cvt > 180.)) {
922 xy_mod = cos(
deg2rad(lat_cvt));
923 geo_x[ipl] = xy_mod * cos(
deg2rad(lon_cvt));
924 geo_y[ipl] = xy_mod * sin(
deg2rad(lon_cvt));
925 geo_z[ipl] = sin(
deg2rad(lat_cvt));
932 for (ip = 0; ip < 3; ip++) {
934 gsl_spline2d_alloc(gsl_interp2d_bicubic, npix_tie, nscn_sub_tie);
935 if ((gsl_spline2d_init(geo_int_id[ip], xa, ya, geo[ip], npix_tie,
936 nscn_sub_tie)) != 0) {
937 fprintf(
stderr,
"%s, %d, E gsl_spline2d_init error\n", __FILE__,
942 accel_x = gsl_interp_accel_alloc();
943 accel_y = gsl_interp_accel_alloc();
968 if (swirir_exist == 1) {
975 if (((ui16_buf_1km = (uint16_t *) malloc(npix_1km * 3 *
976 sizeof ( uint16_t))) ==
NULL) ||
977 ((swir_q = (
char *) calloc(npix_1km * 3,
sizeof (
char)))
979 fprintf(
stderr,
"%s, %d, E - Failed to allocate lat, lon storage\n",
997 tie_st_lin_1km = sscan / resamp_1km - 1;
998 tie_en_lin_1km = escan / resamp_1km + 2;
1000 tie_st_lin_1km = (tie_st_lin_1km < 0) ? 0 : tie_st_lin_1km;
1001 tie_en_lin_1km = (tie_en_lin_1km >= nscan_tie_1km) ? nscan_tie_1km - 1 : tie_en_lin_1km;
1006 if (tie_en_lin_1km - tie_st_lin_1km < 3) {
1007 if (tie_en_lin_1km >= 3)
1008 tie_st_lin_1km = tie_en_lin_1km - 3;
1010 tie_en_lin_1km = tie_st_lin_1km + 3;
1012 if ((tie_st_lin_1km < 0) || (tie_en_lin_1km > nscan_tie_1km - 1)) {
1013 fprintf(
stderr,
"%s, %d, E - L1 file has fewer than 4 tie point lines\n",
1014 __FILE__, __LINE__);
1015 fprintf(
stderr,
" Spline interpolation is not possible\n");
1022 grid_res[1].nscn_sub_tie = tie_en_lin_1km - tie_st_lin_1km + 1;
1023 xa_1km = (
double *) malloc(npix_tie_1km *
sizeof(
double) );
1024 for (ip = 0; ip < npix_tie_1km; ip++)
1025 xa_1km[ip] = ip * resamp_1km;
1026 ya_1km = (
double *) malloc(grid_res[1].nscn_sub_tie
1028 for (il = 0; il < grid_res[1].nscn_sub_tie; il++)
1029 ya_1km[il] = (il + tie_st_lin_1km) * resamp_1km;
1030 grid_res[1].xa = xa_1km;
1031 grid_res[1].ya = ya_1km;
1032 grid_res[1].npix_tie = npix_tie_1km;
1033 grid_res[1].tie_st_lin = tie_st_lin_1km;
1034 grid_res[1].resamp = resamp_1km;
1047 scan_tie = (
scan >= (resample * tie_en_line)) ?
1048 resample * tie_en_line - 1 :
scan;
1051 for (ib = 0; ib < n_vnir; ib++) {
1058 (
void *) ui16_buf) != 0) {
1059 fprintf(
stderr,
"%s, %d, E Error reading VIS/NIR hg, index %d\n",
1060 __FILE__, __LINE__, ib);
1065 for (ip = 0; ip <
npix; ip++) {
1067 if (*(ui16_buf + ip) < *(vnir_mx_dn + ib)) {
1071 lt_tmp = *(vnir_off + ib) + *(vnir_scale + ib) *
1072 (*(ui16_buf + ip) & lt_strip);
1073 if ((lt_tmp >= *(vnir_sat + ib)) &&
1074 (ib != 6) && (ib != 8))
1075 l1rec->hilt[ip] = 1;
1076 l1rec->Lt[ipb] = lt_tmp * w_m2_to_mw_cm2;
1083 for (iv = 0; iv < 2; iv++) {
1084 for (ip = 0; ip <
npix; ip++) {
1086 &azi_v, &zen_v, &nav_bad) != 0)
return 1;
1088 l1rec->senz[ip] = zen_v;
1089 l1rec->sena[ip] = azi_v;
1091 l1rec->solz[ip] = zen_v;
1092 l1rec->sola[ip] = azi_v;
1094 if (nav_bad != 0)
l1rec->navfail[ip] = 1;
1099 if (
l1_input->geom_per_band == 1) {
1100 for (ib = 0; ib < n_vnir; ib++) {
1104 for (ip = 0; ip <
npix; ip++) {
1108 &azi_v, &zen_v, &nav_bad) != 0)
return 1;
1109 l1rec->geom_per_band->senz[ipl] = zen_v;
1110 l1rec->geom_per_band->sena[ipl] = azi_v;
1111 if (nav_bad != 0)
l1rec->navfail[ip] = 1;
1113 l1rec->geom_per_band->solz[ipl] =
l1rec->solz[ip];
1114 l1rec->geom_per_band->sola[ipl] =
l1rec->sola[ip];
1119 for (ib = 0; ib < n_vnir_lg; ib++) {
1120 ib_lg = (ib == 0) ? 6 : 8;
1122 (
void *) ui16_buf) != 0) {
1123 fprintf(
stderr,
"%s, %d, E Error reading VIS/NIR hg, index %d\n",
1124 __FILE__, __LINE__, ib);
1134 for (ip = 0; ip <
npix; ip++) {
1136 ipb = ip *
nbands + ib_lg;
1138 lt_hg_state = LT_BAD;
1140 lt_hg_state = LT_NORM;
1141 if (
l1rec->Lt[ipb] >= *(vnir_sat + ib_lg) * 0.75 *
1143 lt_hg_state = LT_SAT;
1146 if (lt_hg_state != LT_NORM) {
1148 if (*(ui16_buf + ip) < *(vnir_lg_mx_dn + ib)) {
1149 lt_lg_state = LT_NORM;
1150 lt_tmp = *(vnir_lg_off + ib) + *(vnir_lg_scale + ib) *
1151 (*(ui16_buf + ip) & lt_strip);
1152 if (lt_tmp >= *(vnir_lg_sat + ib))
1153 lt_lg_state = LT_SAT;
1154 lt_tmp *= w_m2_to_mw_cm2;
1156 if (
l1_input->geom_per_band == 1) {
1158 &azi_v, &zen_v, &nav_bad) != 0)
return 1;
1161 lt_lg_state = LT_BAD;
1165 if (lt_lg_state == LT_NORM) {
1166 l1rec->Lt[ipb] = lt_tmp;
1167 if (
l1_input->geom_per_band == 1) {
1168 l1rec->geom_per_band->sena[ipb] = azi_v;
1169 l1rec->geom_per_band->senz[ipb] = zen_v;
1170 l1rec->navfail[ip] = nav_bad;
1172 }
else if (lt_lg_state == LT_SAT) {
1173 l1rec->Lt[ipb] = lt_tmp;
1174 l1rec->hilt[ip] = 1;
1175 if (
l1_input->geom_per_band == 1) {
1176 l1rec->geom_per_band->sena[ipb] = azi_v;
1177 l1rec->geom_per_band->senz[ipb] = zen_v;
1178 l1rec->navfail[ip] = nav_bad;
1180 }
else if (lt_hg_state == LT_SAT) {
1182 l1rec->hilt[ip] = 1;
1195 for (ip = 0; ip <
npix; ip++) {
1196 if ((gsl_spline2d_eval_e(geo_int_id[0], ip,
scan, accel_x,
1197 accel_y, &xvec) != 0) ||
1198 (gsl_spline2d_eval_e(geo_int_id[1], ip,
scan, accel_x,
1199 accel_y, &yvec) != 0) ||
1200 (gsl_spline2d_eval_e(geo_int_id[2], ip,
scan, accel_x,
1201 accel_y, &zvec) != 0)) {
1202 fprintf(
stderr,
"%s, %d, E Error in gsl_spline2d_eval\n",
1203 __FILE__, __LINE__);
1209 ip_tie = gsl_interp_accel_find(accel_x, xa, npix_tie, ip);
1210 il_tie = gsl_interp_accel_find(accel_y, ya, nscan_tie, scan_tie);
1211 ipb = ip_tie + il_tie * npix_tie;
1212 if ((geo_q[ipb] != 0) || (geo_q[ ipb + 1 ] != 0) ||
1213 (geo_q[ ipb + npix_tie ] != 0) ||
1214 (geo_q[ ipb + 1 + npix_tie ] != 0))
1215 l1rec->navfail[ip] = 1;
1221 if (swirir_exist == 1) {
1222 for (ib = 0; ib < n_swir; ib++) {
1223 if ((resqkm == 1) && (ib != 2)) {
1226 if (ib == 3) ilr = 2;
1227 scan_1km =
scan / 4;
1228 if (cur_scan_1km[ilr] != scan_1km) {
1229 start[0] = scan_1km;
1232 count[1] = npix_1km;
1234 (
void *) (ui16_buf_1km + ilr * npix_1km))) != 0) {
1235 fprintf(
stderr,
"%s, %d, E Error reading SWIR, band index %d\n",
1236 __FILE__, __LINE__, ib);
1239 cur_scan_1km[ilr] = scan_1km;
1241 for (ip_1km = 0; ip_1km < npix_1km; ip_1km++) {
1243 ipb = ip *
nbands + (ib + n_vnir);
1244 ip_off = ip_1km + npix_1km * ilr;
1245 if (*(ui16_buf_1km + ip_off) < *(swir_mx_dn + ib)) {
1246 lt_tmp = *(swir_off + ib) + *(swir_scale + ib) *
1247 (*(ui16_buf_1km + ip_off) & lt_strip);
1248 if (lt_tmp >= *(swir_sat + ib))
1249 for (isub = 0; isub < 4; isub++)
1250 l1rec->hilt[ ip + isub ] = 1;
1251 lt_tmp *= w_m2_to_mw_cm2;
1252 for (isub = 0; isub < 4; isub++)
1263 (
void *) ui16_buf) != 0) {
1264 fprintf(
stderr,
"%s, %d, E Error reading VIS/NIR hg, index %d\n",
1265 __FILE__, __LINE__, ib);
1269 for (ip = 0; ip <
npix; ip++) {
1270 ipb = ip *
nbands + (ib + n_vnir);
1271 if (*(ui16_buf + ip) <= *(swir_mx_dn + ib)) {
1272 lt_tmp = *(swir_off + ib) + *(swir_scale + ib) *
1273 (*(ui16_buf + ip) & lt_strip);
1274 if (lt_tmp >= *(swir_sat + ib))
1275 l1rec->hilt[ip] = 1;
1276 l1rec->Lt[ipb] = lt_tmp * w_m2_to_mw_cm2;
1286 if (
l1_input->geom_per_band == 1) {
1287 for (ib = 0; ib < n_swir; ib++) {
1294 if ( ( resqkm == 1 ) && ( ib != 1 ) ) {
1295 gsl_interp_accel_reset( accel_x );
1296 gsl_interp_accel_reset( accel_y );
1298 for (ip = 0; ip <
npix; ip++) {
1299 ipb = ip *
nbands + (ib + n_vnir);
1301 &azi_v, &zen_v, &nav_bad) != 0)
return 1;
1302 l1rec->geom_per_band->senz[ipb] = zen_v;
1303 l1rec->geom_per_band->sena[ipb] = azi_v;
1304 if (nav_bad != 0)
l1rec->navfail[ip] = 1;
1306 l1rec->geom_per_band->solz[ipb] =
l1rec->solz[ip];
1307 l1rec->geom_per_band->sola[ipb] =
l1rec->sola[ip];
1310 if ( resqkm == 1 ) {
1311 gsl_interp_accel_reset( accel_x );
1312 gsl_interp_accel_reset( accel_y );
1318 if (
l1_input->geom_per_band == 1) {
1319 for (ib = 0; ib < n_swir; ib++) {
1320 for (ip = 0; ip <
npix; ip++) {
1321 ipb = ip *
nbands + (ib + n_vnir);
1322 l1rec->geom_per_band->senz[ipb] =
l1rec->senz[ip];
1323 l1rec->geom_per_band->sena[ipb] =
l1rec->sena[ip];
1324 l1rec->geom_per_band->solz[ipb] =
l1rec->solz[ip];
1325 l1rec->geom_per_band->sola[ipb] =
l1rec->sola[ip];
1354 for (ibnd = 0; ibnd < n_vnir; ibnd++)
1356 for (ibnd = 0; ibnd < n_vnir_lg; ibnd++)
1358 for (ibnd = 0; ibnd < n_swir; ibnd++)
1360 for (ibnd = 0; ibnd < n_vnir_geom; ibnd++)
1365 for (ibnd = 0; ibnd < 3; ibnd++)
1366 gsl_spline2d_free(geo_int_id[ibnd]);
1370 for (ibnd = 0; ibnd < 2; ibnd++)
1371 for (iv = 0; iv < 3; iv++)
1372 gsl_spline2d_free(band_geom_nominal[ibnd].int_id_sen[iv]);
1376 if (
l1_input->geom_per_band == 1) {
1377 for (ibnd = 0; ibnd < n_vnir; ibnd++)
1378 for (iv = 0; iv < 3; iv++)
1379 gsl_spline2d_free(band_geom_vnir[ibnd].int_id_sen[iv]);
1380 for (ibnd = 0; ibnd < n_vnir_lg; ibnd++)
1381 for (iv = 0; iv < 3; iv++)
1382 gsl_spline2d_free(band_geom_vnir_lg[ibnd].int_id_sen[iv]);
1395 for (ibnd = 0; ibnd < n_swir; ibnd++)
1396 for (iv = 0; iv < 3; iv++)
1397 gsl_spline2d_free(band_geom_swir[ibnd].int_id_sen[iv]);
1399 for (ibnd = 0; ibnd < n_swir; ibnd++)