12 static char sc_eqn[128], sc_type[64], param_val[128], units_val[32];
137 int32
put_l2brs(
char *l2brs_path,
char *replaces,
char *ptime,
char *infiles,
138 int32 px_start, int32 px_end, int32 px_subsamp, int32 brs_nsamp,
139 int32 sc_start, int32 sc_end, int32 sc_subsamp, int32 brs_nrec,
140 char *l2brs_name, float32 *l2brs_data, int32 *l2brs_flags,
141 char *
flag_names,
char *flaguse,
unsigned char *palette,
142 float32 *px_ll_first, float32 *px_ll_last, float32 *sc_ll_first,
145 int32 nrec, int32 nsamp, int32 ntilts,
short *tilt_flags,
147 meta_l2Type *meta_l2, product_table_t *ptable_rec,
const char* oformat, int32 apply_pal) {
149 int32
i, fid, sdfid, sdsid,
rank = 2, dimsizes[3],
start[3];
151 uint32 mask_253, mask_254;
153 unsigned char *brs_data;
154 uint32 required_mask = 0;
159 if (strcasecmp(ptable_rec->scaling,
"logarithmic") == 0) {
162 strcpy(sc_type,
"logarithmic");
163 strcpy(sc_eqn,
"Base**((Slope*brs_data) + Intercept) = ");
164 strcat(sc_eqn, ptable_rec->description);
167 slope = (ptable_rec->max - ptable_rec->min) / 250.0;
168 strcpy(sc_type,
"linear");
169 strcpy(sc_eqn,
"Slope*brs_data + Intercept = ");
170 strcat(sc_eqn, ptable_rec->description);
172 strcpy(param_val, ptable_rec->description);
173 strcpy(units_val, ptable_rec->units);
175 brs_data = (
unsigned char *) malloc(
sizeof (
unsigned char) * brs_nsamp * brs_nrec);
176 if (brs_data ==
NULL) {
177 fprintf(
stderr,
"\nput_l2brs: Unable to allocate space for scaling brs data\n");
188 fprintf(
stderr,
"\nError: put_l2brs: flag_names = null string");
189 fprintf(
stderr,
"\n\t masking will not be applied ");
191 if (flaguse !=
NULL && flaguse[0] !=
'\0') {
197 for (
i = 0;
i < brs_nsamp * brs_nrec;
i++) {
198 if (l2brs_flags[
i] & mask_253)
200 else if (l2brs_flags[
i] & mask_254)
202 else if ((l2brs_flags[
i] & required_mask) != required_mask)
204 else if (l2brs_data[
i] < ptable_rec->min)
207 if (strcasecmp(ptable_rec->scaling,
"logarithmic") == 0)
208 scaled_value = floor((log10(l2brs_data[
i]) -
intercept) /
slope + 0.5);
212 if (scaled_value > 250.0)
215 brs_data[
i] = (
unsigned char) scaled_value;
219 if (strcmp(oformat,
"HDF4") == 0) {
220 sdfid = SDstart(l2brs_path, DFACC_CREATE);
221 fid = Hopen(l2brs_path, DFACC_RDWR, 0);
224 write_attrs(sdfid, l2brs_path, replaces, ptime, infiles, px_start, px_end,
225 px_subsamp, brs_nsamp, sc_start, sc_end, sc_subsamp, brs_nrec,
227 nrec, nsamp, meta_l2);
229 write_image(l2brs_path, brs_data, brs_nsamp, brs_nrec, palette);
235 dimsizes[0] = brs_nsamp;
238 start, px_ll_first)) < 0)
243 SDsetdimname(SDgetdimid(sdsid, 0),
"Pixels per Scan");
244 SDsetdimname(SDgetdimid(sdsid, 1),
"Lat/Lon");
248 dimsizes[0] = brs_nsamp;
251 start, px_ll_last)) < 0)
256 SDsetdimname(SDgetdimid(sdsid, 0),
"Pixels per Scan");
257 SDsetdimname(SDgetdimid(sdsid, 1),
"Lat/Lon");
261 dimsizes[0] = brs_nrec;
264 start, sc_ll_first)) < 0)
269 SDsetdimname(SDgetdimid(sdsid, 0),
"Number of Scans");
270 SDsetdimname(SDgetdimid(sdsid, 1),
"Lat/Lon");
274 dimsizes[0] = brs_nrec;
277 start, sc_ll_last)) < 0)
282 SDsetdimname(SDgetdimid(sdsid, 0),
"Number of Scans");
283 SDsetdimname(SDgetdimid(sdsid, 1),
"Lat/Lon");
287 if ((
write_tilt_sets(fid, sdfid, ntilts, tilt_flags, tilt_ranges)) < 0)
292 if ((
write_nav_sets(fid, sdfid, brs_nrec, brs_nsamp, cntl_pt_lat, cntl_pt_lon)) < 0)
301 }
else if (strcmp(oformat,
"PPM") == 0) {
302 if ((outfp = fopen(l2brs_path,
"w")) ==
NULL) {
303 fprintf(
stderr,
"put_l2brs: Error: Unable to open %s for writing.\n", l2brs_path);
310 fprintf(outfp,
"P6\n");
311 fprintf(outfp,
"%d %d\n", brs_nsamp, brs_nrec);
312 fprintf(outfp,
"255\n");
313 for (
i = 0;
i < brs_nsamp * brs_nrec;
i++) {
314 fwrite(palette + brs_data[
i]*3, 1, 3, outfp);
320 fprintf(outfp,
"P5\n");
321 fprintf(outfp,
"%d %d\n", brs_nsamp, brs_nrec);
322 fprintf(outfp,
"255\n");
324 for (
i = 0;
i < brs_nsamp * brs_nrec;
i++) {
325 fwrite(brs_data +
i, 1, 1, outfp);
333 }
else if (strcmp(oformat,
"PNG") == 0) {
335 outfp = fopen(l2brs_path,
"w");
337 fprintf(
stderr,
"put_l2brs: Error: Unable to open %s for writing.\n", l2brs_path);
341 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
344 fprintf(
stderr,
"put_l2brs: Error: Unable to create PNG write structure.\n");
348 png_infop info_ptr = png_create_info_struct(png_ptr);
350 png_destroy_write_struct(&png_ptr, (png_infopp)
NULL);
351 fprintf(
stderr,
"put_l2brs: Error: Unable to create PNG info structure.\n");
354 if (setjmp(png_jmpbuf(png_ptr))) {
355 png_destroy_write_struct(&png_ptr, (png_infopp)
NULL);
356 fprintf(
stderr,
"put_l2brs: Error: Unable to call PNG setjmp().\n");
359 png_init_io(png_ptr, outfp);
363 png_set_IHDR(png_ptr, info_ptr, brs_nsamp, brs_nrec,
364 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
365 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
366 png_set_PLTE(png_ptr, info_ptr, (png_const_colorp) palette, 256);
370 png_set_IHDR(png_ptr, info_ptr, brs_nsamp, brs_nrec,
371 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
372 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
375 uint8 * row_pointers[brs_nrec];
376 for (
i = 0;
i < brs_nrec;
i++)
377 row_pointers[
i] = brs_data + (
i * brs_nsamp);
378 png_set_rows(png_ptr, info_ptr, row_pointers);
380 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY,
NULL);
383 png_destroy_write_struct(&png_ptr, (png_infopp)
NULL);
390 fprintf(
stderr,
"put_l2brs: Error: oformat=%s is not supported.\n", oformat);
466 void write_attrs(int32 sdfid,
char *l2brs_path,
char *replaces,
char *ptime,
467 char *infiles, int32 px_start, int32 px_end, int32 px_subsamp,
468 int32 brs_nsamp, int32 sc_start, int32 sc_end, int32 sc_subsamp,
469 int32 brs_nrec,
char *l2brs_name,
char *proc_con,
int16 syear,
471 char *
dtype, int32 nrec, int32 nsamp, meta_l2Type *meta_l2) {
472 div_t quot1, quot2, quot3;
473 char string[20], pname[2048], *
p, *pinfile;
474 int32 lac_px_st, lac_px_subsamp;
477 char *soft_name =
"l2brsgen";
478 char *soft_ver =
"5.1";
481 p = strrchr(l2brs_path,
'/');
483 strcpy(pname, l2brs_path);
487 SDsetattr(sdfid,
L2B_PNAME, DFNT_CHAR, strlen(pname) + 1, (VOIDP) pname);
489 strcpy(pname, meta_l2->sensor_name);
491 SDsetattr(sdfid,
L2B_TITLE, DFNT_CHAR, strlen(pname) + 1, pname);
492 SDsetattr(sdfid,
SENSOR_NAME, DFNT_CHAR, strlen(meta_l2->sensor_name) + 1,
493 meta_l2->sensor_name);
496 "%s Level-2 %s %s browse data, day %3.3d, %4.4d",
501 SDsetattr(sdfid,
SOFT_NAME, DFNT_CHAR, strlen(soft_name) + 1,
503 SDsetattr(sdfid,
SOFT_VER, DFNT_CHAR, strlen(soft_ver) + 1,
505 SDsetattr(sdfid,
REPLACES, DFNT_CHAR, strlen(replaces) + 1, (VOIDP) replaces);
507 if (meta_l2->mission != 0x0) {
508 SDsetattr(sdfid,
L2BRS_MISSION, DFNT_CHAR, strlen(meta_l2->mission) + 1,
509 (VOIDP) meta_l2->mission);
512 if (meta_l2->mission_char != 0x0) {
513 SDsetattr(sdfid,
MSNCHAR, DFNT_CHAR, strlen(meta_l2->mission_char) + 1,
514 (VOIDP) meta_l2->mission_char);
517 if (meta_l2->sensor != 0x0) {
518 SDsetattr(sdfid,
SENSOR, DFNT_CHAR, strlen(meta_l2->sensor) + 1,
519 (VOIDP) meta_l2->sensor);
522 if (meta_l2->sensor_char != 0x0) {
523 SDsetattr(sdfid,
SNSCHAR, DFNT_CHAR, strlen(meta_l2->sensor_char) + 1,
524 (VOIDP) meta_l2->sensor_char);
527 SDsetattr(sdfid,
PTIME, DFNT_CHAR, strlen(ptime) + 1, (VOIDP) ptime);
529 strlen(infiles) + 1, (VOIDP) infiles);
530 SDsetattr(sdfid,
PROC_CON, DFNT_CHAR, strlen(proc_con) + 1, (VOIDP) proc_con);
532 if(meta_l2->infiles) {
533 pinfile = strrchr(meta_l2->infiles,
'/');
535 pinfile = meta_l2->infiles;
541 SDsetattr(sdfid,
PINFILES, DFNT_CHAR, strlen(pinfile) + 1,
552 quot2 = div(quot1.rem,
MSECMIN);
553 quot3 = div(quot2.rem,
MSECSEC);
554 sprintf(
string,
"%4.4d%3.3d%2.2d%2.2d%2.2d%3.3d",
syear,
sday,
555 quot1.quot, quot2.quot, quot3.quot, quot3.rem);
556 SDsetattr(sdfid,
STIME, DFNT_CHAR, strlen(
string) + 1, (VOIDP)
string);
558 quot2 = div(quot1.rem,
MSECMIN);
559 quot3 = div(quot2.rem,
MSECSEC);
560 sprintf(
string,
"%4.4d%3.3d%2.2d%2.2d%2.2d%3.3d",
eyear,
eday,
561 quot1.quot, quot2.quot, quot3.quot, quot3.rem);
562 SDsetattr(sdfid,
END_TIME, DFNT_CHAR,
563 strlen(
string) + 1, (VOIDP)
string);
565 if (meta_l2->ctime != 0x0)
566 SDsetattr(sdfid,
CTIME, DFNT_CHAR, strlen(meta_l2->ctime) + 1,
567 (VOIDP) meta_l2->ctime);
568 if (meta_l2->ntime != 0x0)
569 SDsetattr(sdfid,
NTIME, DFNT_CHAR, strlen(meta_l2->ntime) + 1,
570 (VOIDP) meta_l2->ntime);
579 if (meta_l2->snode != 0x0)
580 SDsetattr(sdfid,
SNODE, DFNT_CHAR, strlen(meta_l2->snode) + 1,
581 (VOIDP) meta_l2->snode);
582 if (meta_l2->enode != 0x0)
583 SDsetattr(sdfid,
ENODE, DFNT_CHAR, strlen(meta_l2->enode) + 1,
584 (VOIDP) meta_l2->enode);
602 SDsetattr(sdfid,
PARAM, DFNT_CHAR, strlen(param_val) + 1, (VOIDP) param_val);
603 SDsetattr(sdfid,
UNITS, DFNT_CHAR, strlen(units_val) + 1, (VOIDP) units_val);
606 lac_px_st = meta_l2->pix_start + ((px_start - 1) * meta_l2->pix_sub);
609 lac_px_subsamp = meta_l2->pix_sub * px_subsamp;
618 SDsetattr(sdfid,
SC_TYPE, DFNT_CHAR, strlen(sc_type) + 1, (VOIDP) sc_type);
619 SDsetattr(sdfid,
SC_EQN, DFNT_CHAR, strlen(sc_eqn) + 1, (VOIDP) sc_eqn);
655 write_image(
char *l2brs_path,
unsigned char *l2brs_data, int32 brs_nsamp, int32 brs_nrec,
656 unsigned char *palette) {
659 DFR8setpalette(palette);
661 if ((DFR8addimage(l2brs_path, (VOIDP) l2brs_data, brs_nsamp, brs_nrec, 0)) < 0) {
662 fprintf(
stderr,
"\n Error writing browse image\n");
666 if ((img_ref = DFR8lastref()) > 0) {
667 if ((DFANputlabel(l2brs_path, DFTAG_RIG, img_ref,
"brs_data")) < 0) {
668 fprintf(
stderr,
"\nwrite_image: Error writing - brs_data label\n");
669 fprintf(
stderr,
"\n No label is written to the raster image\n");
672 fprintf(
stderr,
"\nwrite_image: Error reading image ref. no. \n");
673 fprintf(
stderr,
"\n No label is written to the raster image\n");
711 int32 *
start,
void *buf) {
714 if ((sdsid = SDcreate(sdfid,
label, ntype,
rank, dimsizes)) < 0)
717 if ((ret = SDwritedata(sdsid,
start,
NULL, dimsizes, (VOIDP) buf)) < 0)
759 int32 vid, sdsid, sdsref;
760 int32
rank,
start[3] = {0, 0, 0}, dimsizes[3];
761 char FUNC[] =
"write_nav_sets:";
764 if ((vid = Vattach(fid, -1,
"w")) < 0) {
765 sprintf(
ERR_MSG,
"\n%s Vattach failed for navigation vgp", FUNC);
769 Vsetname(vid,
"Navigation");
772 dimsizes[0] = brs_nrec;
773 dimsizes[1] = brs_nsamp;
776 dimsizes,
start, cntl_pt_lat)) < 0)
return FAIL;
781 "\n%s Error writing attribute %s for cnlt_pt_lat", FUNC,
LONGNAME);
785 if ((sdsref = SDidtoref(sdsid)) < 0) {
786 sprintf(
ERR_MSG,
"\nError SDidtoref failed for cntl_pt_lat dataset");
790 if ((Vaddtagref(vid, DFTAG_NDG, sdsref)) < 0) {
791 sprintf(
ERR_MSG,
"\nError Vaddtagref failed for cntl_pt_lat");
795 SDsetdimname(SDgetdimid(sdsid, 0),
"Number of Scans");
796 SDsetdimname(SDgetdimid(sdsid, 1),
"Pixels per Scan");
805 dimsizes[0] = brs_nrec;
806 dimsizes[1] = brs_nsamp;
809 dimsizes,
start, cntl_pt_lon)) < 0)
return FAIL;
814 "\n%s Error writing attribute %s for cnlt_pt_lon", FUNC,
LONGNAME);
818 if ((sdsref = SDidtoref(sdsid)) < 0) {
819 sprintf(
ERR_MSG,
"\nError SDidtoref failed for cntl_pt_lon dataset");
823 if ((Vaddtagref(vid, DFTAG_NDG, sdsref)) < 0) {
824 sprintf(
ERR_MSG,
"\nError Vaddtagref failed for cntl_pt_lon");
828 SDsetdimname(SDgetdimid(sdsid, 0),
"Number of Scans");
829 SDsetdimname(SDgetdimid(sdsid, 1),
"Pixels per Scan");
876 int16 *tilt_ranges) {
877 int32 vid, sdsid, sdsref;
878 int32
rank,
start[3] = {0, 0, 0}, dimsizes[3] = {0, 0, 0};
880 char FUNC[] =
"write_tilt_sets";
883 if ((vid = Vattach(fid, -1,
"w")) < 0) {
884 sprintf(
ERR_MSG,
"\n%s Vattach failed for tilt vgp", FUNC);
888 Vsetname(vid,
"Sensor Tilt");
894 dimsizes,
start, &ntilts)) < 0)
899 if ((sdsref = SDidtoref(sdsid)) < 0) {
900 sprintf(
ERR_MSG,
"\nError SDidtoref failed for ntilts dataset");
904 if ((Vaddtagref(vid, DFTAG_NDG, sdsref)) < 0) {
905 sprintf(
ERR_MSG,
"\nError Vaddtagref failed for ntilts");
913 start, tilt_flags)) < 0)
922 if ((sdsref = SDidtoref(sdsid)) < 0) {
923 sprintf(
ERR_MSG,
"\nError SDidtoref failed for tilt_flags dataset");
927 if ((Vaddtagref(vid, DFTAG_NDG, sdsref)) < 0) {
928 sprintf(
ERR_MSG,
"\nError Vaddtagref failed for tilt_flags");
938 dimsizes,
start, tilt_ranges)) < 0)
943 if ((sdsref = SDidtoref(sdsid)) < 0) {
944 sprintf(
ERR_MSG,
"\nError SDidtoref failed for tilt_ranges dataset");
948 if ((Vaddtagref(vid, DFTAG_NDG, sdsref)) < 0) {
949 sprintf(
ERR_MSG,
"\nError Vaddtagref failed for tilt_ranges");