85 #define BAD_LAT_LON -900.0
89 static float initialLonVal = -180.0;
94 static float normalizeLon(
float lon) {
108 static void setMaxLon(
float *
max,
float val) {
110 if (normalizeLon(
val) > normalizeLon(*
max))
120 static void setMinLon(
float *
min,
float val) {
122 if (normalizeLon(
val) < normalizeLon(*
min))
132 static void setMaxLat(
float *
max,
float val) {
144 static void setMinLat(
float *
min,
float val) {
156 static void setGoodLL(
float *good,
float val) {
162 int main(int32 argc,
char *argv[]) {
164 char l2_path[FILENAME_MAX];
165 char ofile[FILENAME_MAX];
170 int32_t brs_nrec, brs_nsamp;
172 int32_t max_rec_used, max_samp_used;
176 static meta_l2Type meta_l2;
178 int32 *l2brs_flags, *
flags;
179 float32 *pix, *qual_pix;
180 int rec, samp, brs_rec, brs_samp;
181 int start_samp, start_rec, xsub = 2, ysub = 2,
qual = 999;
182 int end_samp, end_rec;
184 int spixl, epixl, sline, eline;
185 float32 *l2brs_data, *
data;
186 float *px_ll_first, *px_ll_last, *sc_ll_first, *sc_ll_last;
189 char infiles[
MAXVAL], *sp;
191 char replaces[
MAXVAL], ptime[17];
194 char palette_dir[FILENAME_MAX];
195 char product_table[FILENAME_MAX];
200 char *flaguse, *chl_flags, *sst_flags;
202 const float MISSING_DATAFLAG = -9.1E6;
203 char browse_prod[128] =
"chlor_a";
204 char qual_prod1[128] =
"qual_sst";
205 char qual_prod2[128] =
"qual_sst4";
206 unsigned char palette[256 * 3];
207 short r[256],
g[256],
b[256];
209 static l2_prod l2_str;
215 product_table_t *ptable =
NULL;
216 int32_t ptable_size = 0;
217 int32_t ptable_index = -1;
218 product_table_t *ptable_rec;
219 product_table_t tmp_ptable;
235 if (strcmp(argv[1],
"-version") == 0 || strcmp(argv[1],
"--version") == 0) {
246 for (
i = 0;
i < argc;
i++) {
247 strcat(proc_con, argv[
i]);
248 strcat(proc_con,
" ");
272 if (ptable ==
NULL) {
273 fprintf(
stderr,
"l2brsgen - error reading product table \"%s\"\n", product_table);
278 if (ptable_index == -1) {
282 tmp_ptable.description =
strdup(browse_prod);
285 tmp_ptable.name =
strdup(browse_prod);
286 tmp_ptable.palette =
strdup(palfile);
287 tmp_ptable.precision =
strdup(
"F");
289 tmp_ptable.scaling =
strdup(
"logarithmic");
291 tmp_ptable.scaling =
strdup(
"linear");
293 tmp_ptable.units =
strdup(
"unknown");
294 ptable_rec = &tmp_ptable;
296 fprintf(
stderr,
"l2brsgen - error finding \"%s\" in product table \"%s\" and datamin, datamax, stype not set.\n",
297 browse_prod, product_table);
301 ptable_rec = ptable + ptable_index;
305 if (strcasecmp(palfile,
"default") != 0) {
306 free(ptable_rec->palette);
307 ptable_rec->palette =
strdup(palfile);
316 free(ptable_rec->scaling);
318 ptable_rec->scaling =
strdup(
"logarithmic");
320 ptable_rec->scaling =
strdup(
"linear");
336 fprintf(
stderr,
"ifile=%s\n", l2_path);
338 fprintf(
stderr,
"prod=%s\n", browse_prod);
339 fprintf(
stderr,
"oformat=%s\n", oformat);
342 strcpy(palfile, palette_dir);
343 strcat(palfile,
"/");
344 strcat(palfile, ptable_rec->palette);
345 strcat(palfile,
".pal");
348 fprintf(
stderr,
"l2brsgen: Error reading palette file %s\n", palfile);
351 for (
i = 0;
i < 256;
i++) {
352 palette[
i * 3] =
r[
i];
353 palette[
i * 3 + 1] =
g[
i];
354 palette[
i * 3 + 2] =
b[
i];
360 syear = l2_str.syear;
362 smsec = l2_str.smsec;
363 eyear = l2_str.eyear;
365 emsec = l2_str.emsec;
367 nsamp = l2_str.nsamp;
369 flag_names = (
char *) calloc(strlen(l2_str.flagnames) + 1,
sizeof (char));
372 ntilts = l2_str.ntilts;
373 tilt_ranges = (
short *) calloc(20 * 2,
sizeof (
short));
374 for (
i = 0;
i < ntilts;
i++) {
375 tilt_ranges[2 *
i] = l2_str.tilt_ranges[0][
i];
376 tilt_ranges[2 *
i + 1] = l2_str.tilt_ranges[1][
i];
388 start_samp = spixl - 1;
391 end_samp = epixl - 1;
393 end_samp = nsamp - 1;
394 if (start_samp >= end_samp) {
395 fprintf(
stderr,
"l2brsgen: spixel (%d) needs to be less than epixl (%d)",
396 start_samp + 1, end_samp + 1);
397 fprintf(
stderr,
" cntl_pt_cols ");
401 start_rec = sline - 1;
407 if (start_rec >= end_rec) {
408 fprintf(
stderr,
"l2brsgen: sline (%d) needs to be less than eline (%d)",
409 start_rec + 1, end_rec + 1);
410 fprintf(
stderr,
" cntl_pt_cols ");
415 brs_nrec = (end_rec - start_rec) / ysub + 1;
416 brs_nsamp = (end_samp - start_samp) / xsub + 1;
420 max_rec_used = (brs_nrec - 1) * ysub + start_rec;
421 max_samp_used = (brs_nsamp - 1) * xsub + start_samp;
424 brs_crec = brs_nrec / 2;
425 center_samp = ((max_samp_used - start_samp) / 2) + start_samp;
427 if ((brs_lat = (
int16 *) calloc(brs_nrec * brs_nsamp,
sizeof (
int16))) ==
NULL) {
428 fprintf(
stderr,
"\nl2brsgen: Calloc error while allocating memory for");
429 fprintf(
stderr,
" brs_lat ");
434 if ((brs_lon = (
int16 *) calloc(brs_nrec * brs_nsamp,
sizeof (
int16))) ==
NULL) {
435 fprintf(
stderr,
"\nl2brsgen: Calloc error while allocating memory for");
436 fprintf(
stderr,
" brs_lon ");
442 l2brs_data = (float32 *) malloc(brs_nrec * brs_nsamp *
sizeof (float32));
443 if (l2brs_data ==
NULL) {
444 fprintf(
stderr,
"\nError: In allocating memory\n");
450 l2brs_flags = (int32 *) malloc(brs_nrec * brs_nsamp *
sizeof (int32));
451 if (l2brs_flags ==
NULL) {
452 fprintf(
stderr,
"\nError: In allocating memory\n");
458 px_ll_first = (
float *) malloc(2 * brs_nsamp *
sizeof (
float));
459 px_ll_last = (
float *) malloc(2 * brs_nsamp *
sizeof (
float));
460 sc_ll_first = (
float *) malloc(2 * brs_nrec *
sizeof (
float));
461 sc_ll_last = (
float *) malloc(2 * brs_nrec *
sizeof (
float));
462 if (!(px_ll_first && px_ll_last && sc_ll_first && sc_ll_last)) {
463 fprintf(
stderr,
"\nError: In allocating memory\n");
473 prod_index =
findprod(&l2_str, browse_prod);
474 qual_index =
findprod(&l2_str, qual_prod1);
475 if (qual_index == -1) {
476 qual_index =
findprod(&l2_str, qual_prod2);
481 if (prod_index == -1) {
482 fprintf(
stderr,
"\n Error: Browse product (%s) not found in input file \n", browse_prod);
506 setGoodLL(&initialLonVal, meta_l2.westlon);
513 for (brs_rec = 0; brs_rec < brs_nrec; brs_rec++) {
516 pix = l2_str.l2_data[prod_index];
517 if (strncmp(browse_prod,
"sst", 3) == 0 && qual_index != -1)
518 qual_pix = l2_str.l2_data[qual_index];
523 for (brs_samp = 0; brs_samp < brs_nsamp; brs_samp++) {
525 if (qual_pix && qual_pix[samp] >
qual)
526 *
data = MISSING_DATAFLAG;
530 *
flags = l2_str.l2_flags[samp];
532 *
lat = (
int16) roundf(l2_str.latitude[samp] * 360.0);
536 *
lon = (
int16) roundf(l2_str.longitude[samp] * 180.0);
540 if (rec == start_rec) {
541 *
top++ = l2_str.latitude[samp];
542 *
top++ = l2_str.longitude[samp];
544 if (rec == max_rec_used) {
545 *
bottom++ = l2_str.latitude[samp];
546 *
bottom++ = l2_str.longitude[samp];
552 meta_l2.northlat = l2_str.latitude[samp];
553 meta_l2.southlat = l2_str.latitude[samp];
554 meta_l2.westlon = l2_str.longitude[samp];
555 meta_l2.eastlon = l2_str.longitude[samp];
559 setMaxLat(&meta_l2.northlat, l2_str.latitude[samp]);
560 setMinLat(&meta_l2.southlat, l2_str.latitude[samp]);
561 setMaxLon(&meta_l2.eastlon, l2_str.longitude[samp]);
562 setMinLon(&meta_l2.westlon, l2_str.longitude[samp]);
572 *
left++ = l2_str.latitude[start_samp];
573 *
left++ = l2_str.longitude[start_samp];
574 *
right++ = l2_str.latitude[max_samp_used];
575 *
right++ = l2_str.longitude[max_samp_used];
584 setGoodLL(&meta_l2.startclat, l2_str.latitude[center_samp]);
585 setGoodLL(&meta_l2.startclon, l2_str.longitude[center_samp]);
589 if (brs_rec == brs_crec) {
591 if (l2_str.year != -999 && l2_str.day != -999 && l2_str.msec != -999) {
595 meta_l2.ctime =
strdup(timePtr);
606 setGoodLL(&meta_l2.endclat, l2_str.latitude[center_samp]);
607 setGoodLL(&meta_l2.endclon, l2_str.longitude[center_samp]);
608 if (l2_str.year != -999 && l2_str.day != -999 && l2_str.msec != -999) {
621 sp = strrchr(l2_path,
'/');
627 if (flaguse ==
NULL) {
628 if (strncmp(browse_prod,
"chl", 3) == 0) {
630 }
else if (strncmp(browse_prod,
"sst", 3) == 0) {
636 brs_nsamp, start_rec + 1, end_rec + 1, ysub, brs_nrec, browse_prod, l2brs_data,
637 l2brs_flags,
flag_names, flaguse, palette, px_ll_first, px_ll_last,
639 emsec,
dtype, nrec, nsamp, ntilts, &l2_str.tilt_flags[0],
640 tilt_ranges, brs_lat, brs_lon, &meta_l2, ptable_rec, oformat, apply_pal);
643 fprintf(
stderr,
"\n Error: put_l2brs unsuccessful\n");