Debian Codegraph @

imagery/i.landsat.toar/main.c

raw
1 /****************************************************************************
2 *
3 * MODULE: i.landsat.toar
4 *
5 * AUTHOR(S): E. Jorge Tizado - ej.tizado at unileon.es
6 * Hamish Bowman (small grassification cleanups)
7 * Yann Chemin (v7 + L5TM _MTL.txt support) [removed after update]
8 * Adopted for GRASS 7 by Martin Landa <landa.martin gmail.com>
9 *
10 * PURPOSE: Calculate TOA Radiance or Reflectance and Kinetic Temperature
11 * for Landsat 1/2/3/4/5 MS, 4/5 TM, 7 ETM+, and 8 OLI/TIRS
12 *
13 * COPYRIGHT: (C) 2006-2013 by the GRASS Development Team
14 *
15 * This program is free software under the GNU General
16 * Public License (>=v2). Read the file COPYING that
17 * comes with GRASS for details.
18 *
19 *****************************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <grass/gis.h>
24 #include <grass/raster.h>
25 #include <grass/glocale.h>
26
27 #include "local_proto.h"
28
29 #define QCALMAX 65536 /* L1-7=256 but L8=65536 */
30
31 int main(int argc, char *argv[])
32 {
33 struct History history;
34 struct GModule *module;
35 char *desc;
36
37 struct Cell_head cellhd, orig_cellhd;
38
39 void *inrast, *outrast;
40 int infd, outfd;
41 void *ptr;
42 int nrows, ncols, row, col;
43
44 RASTER_MAP_TYPE in_data_type;
45
46 struct Option *input_prefix, *output_prefix, *metfn, *sensor, *adate,
47 *pdate, *elev, *bgain, *metho, *perc, *dark, *atmo, *lsatmet, *oscale;
48 char *inputname, *met, *outputname, *sensorname;
49 struct Flag *frad, *print_meta, *named;
50
51 lsat_data lsat;
52 char band_in[GNAME_MAX], band_out[GNAME_MAX];
53 int i, j, q, method, pixel, dn_dark[MAX_BANDS], dn_mode[MAX_BANDS], dn_sat;
54 int overwrite;
55 double qcal, rad, ref, percent, ref_mode, rayleigh, scale;
56 unsigned long hist[QCALMAX], h_max;
57
58 struct Colors colors;
59 struct FPRange range;
60 double min, max;
61
62 /* initialize GIS environment */
63 G_gisinit(argv[0]);
64
65 /* initialize module */
66 module = G_define_module();
67 module->description =
68 _("Calculates top-of-atmosphere radiance or reflectance and "
69 "temperature for Landsat MSS/TM/ETM+/OLI");
70 G_add_keyword(_("imagery"));
71 G_add_keyword(_("radiometric conversion"));
72 G_add_keyword(_("radiance"));
73 G_add_keyword(_("reflectance"));
74 G_add_keyword(_("brightness temperature"));
75 G_add_keyword(_("atmospheric correction"));
76 G_add_keyword(_("satellite"));
77 G_add_keyword(_("Landsat"));
78 module->overwrite = TRUE;
79
80 /* It defines the different parameters */
81 input_prefix = G_define_standard_option(G_OPT_R_BASENAME_INPUT);
82 input_prefix->label = _("Base name of input raster bands");
83 input_prefix->description = _("Example: 'B.' for B.1, B.2, ...");
84
85 output_prefix = G_define_standard_option(G_OPT_R_BASENAME_OUTPUT);
86 output_prefix->label = _("Prefix for output raster maps");
87 output_prefix->description =
88 _("Example: 'B.toar.' generates B.toar.1, B.toar.2, ...");
89
90 metfn = G_define_standard_option(G_OPT_F_INPUT);
91 metfn->key = "metfile";
92 metfn->required = NO;
93 metfn->description = _("Name of Landsat metadata file (.met or MTL.txt)");
94 metfn->guisection = _("Metadata");
95
96 sensor = G_define_option();
97 sensor->key = "sensor";
98 sensor->type = TYPE_STRING;
99 sensor->label = _("Spacecraft sensor");
100 sensor->description =
101 _("Required only if 'metfile' not given (recommended for sanity)");
102 sensor->options = "mss1,mss2,mss3,mss4,mss5,tm4,tm5,tm7,oli8";
103 desc = NULL;
104 G_asprintf(
105 &desc,
106 "mss1;%s;mss2;%s;mss3;%s;mss4;%s;mss5;%s;tm4;%s;tm5;%s;tm7;%s;oli8;%s",
107 _("Landsat-1 MSS"), _("Landsat-2 MSS"), _("Landsat-3 MSS"),
108 _("Landsat-4 MSS"), _("Landsat-5 MSS"), _("Landsat-4 TM"),
109 _("Landsat-5 TM"), _("Landsat-7 ETM+"), _("Landsat_8 OLI/TIRS"));
110 sensor->descriptions = desc;
111 sensor->required = NO; /* perhaps YES for clarity */
112 sensor->guisection = _("Metadata");
113
114 metho = G_define_option();
115 metho->key = "method";
116 metho->type = TYPE_STRING;
117 metho->required = NO;
118 metho->options = "uncorrected,dos1,dos2,dos2b,dos3,dos4";
119 metho->label = _("Atmospheric correction method");
120 metho->answer = "uncorrected";
121 metho->guisection = _("Metadata");
122
123 adate = G_define_option();
124 adate->key = "date";
125 adate->type = TYPE_STRING;
126 adate->required = NO;
127 adate->key_desc = "yyyy-mm-dd";
128 adate->label = _("Image acquisition date (yyyy-mm-dd)");
129 adate->description = _("Required only if 'metfile' not given");
130 adate->guisection = _("Metadata");
131
132 elev = G_define_option();
133 elev->key = "sun_elevation";
134 elev->type = TYPE_DOUBLE;
135 elev->required = NO;
136 elev->label = _("Sun elevation in degrees");
137 elev->description = _("Required only if 'metfile' not given");
138 elev->guisection = _("Metadata");
139
140 pdate = G_define_option();
141 pdate->key = "product_date";
142 pdate->type = TYPE_STRING;
143 pdate->required = NO;
144 pdate->key_desc = "yyyy-mm-dd";
145 pdate->label = _("Image creation date (yyyy-mm-dd)");
146 pdate->description = _("Required only if 'metfile' not given");
147 pdate->guisection = _("Metadata");
148
149 bgain = G_define_option();
150 bgain->key = "gain";
151 bgain->type = TYPE_STRING;
152 bgain->required = NO;
153 bgain->label = _("Gain (H/L) of all Landsat ETM+ bands (1-5,61,62,7,8)");
154 bgain->description = _("Required only if 'metfile' not given");
155 bgain->guisection = _("Settings");
156
157 perc = G_define_option();
158 perc->key = "percent";
159 perc->type = TYPE_DOUBLE;
160 perc->required = NO;
161 perc->label = _("Percent of solar radiance in path radiance");
162 perc->description = _("Required only if 'method' is any DOS");
163 perc->answer = "0.01";
164 perc->guisection = _("Settings");
165
166 dark = G_define_option();
167 dark->key = "pixel";
168 dark->type = TYPE_INTEGER;
169 dark->required = NO;
170 dark->label = _("Minimum pixels to consider digital number as dark object");
171 dark->description = _("Required only if 'method' is any DOS");
172 dark->answer = "1000";
173 dark->guisection = _("Settings");
174
175 atmo = G_define_option();
176 atmo->key = "rayleigh";
177 atmo->type = TYPE_DOUBLE;
178 atmo->required = NO;
179 atmo->label =
180 _("Rayleigh atmosphere (diffuse sky irradiance)"); /* scattering
181 coefficient? */
182 atmo->description = _("Required only if 'method' is DOS3");
183 atmo->answer = "0.0";
184 atmo->guisection = _("Settings");
185
186 lsatmet = G_define_option();
187 lsatmet->key = "lsatmet";
188 lsatmet->type = TYPE_STRING;
189 lsatmet->required = NO;
190 lsatmet->multiple = YES;
191 lsatmet->label = _("return value stored for a given metadata");
192 lsatmet->description = _("Required only if 'metfile' and -p given");
193 lsatmet->options = "number,creation,date,sun_elev,sensor,bands,sunaz,time";
194 desc = NULL;
195 G_asprintf(&desc,
196 "number;%s;creation;%s;date;%s;sun_elev;%s;sensor;%s;bands;%s;"
197 "sunaz;%s;time;%s",
198 _("Landsat Number"), _("Creation timestamp"), _("Date"),
199 _("Sun Elevation"), _("Sensor"), _("Bands count"),
200 _("Sun Azimuth Angle"), _("Time"));
201 lsatmet->descriptions = desc;
202 lsatmet->guisection = _("Settings");
203
204 oscale = G_define_option();
205 oscale->key = "scale";
206 oscale->type = TYPE_DOUBLE;
207 oscale->answer = "1.0";
208 oscale->required = NO;
209 oscale->description = _("Scale factor for output");
210
211 /* define the different flags */
212 frad = G_define_flag();
213 frad->key = 'r';
214 frad->description =
215 _("Output at-sensor radiance instead of reflectance for all bands");
216
217 named = G_define_flag();
218 named->key = 'n';
219 named->description = _("Input raster maps use as extension the number of "
220 "the band instead the code");
221
222 print_meta = G_define_flag();
223 print_meta->key = 'p';
224 print_meta->description = _("Print output metadata info");
225
226 /* options and afters parser */
227 if (G_parser(argc, argv))
228 exit(EXIT_FAILURE);
229
230 /*****************************************
231 * ---------- START --------------------
232 * Stores options and flags to variables
233 *****************************************/
234 met = metfn->answer;
235 inputname = input_prefix->answer;
236 outputname = output_prefix->answer;
237 sensorname = sensor->answer ? sensor->answer : "";
238
239 overwrite = G_check_overwrite(argc, argv);
240
241 Rast_get_window(&orig_cellhd);
242
243 G_zero(&lsat, sizeof(lsat));
244
245 if (adate->answer != NULL) {
246 strncpy(lsat.date, adate->answer, 11);
247 lsat.date[10] = '\0';
248 if (strlen(lsat.date) != 10)
249 G_fatal_error(_("Illegal date format: [%s] (yyyy-mm-dd)"),
250 lsat.date);
251 }
252
253 if (pdate->answer != NULL) {
254 strncpy(lsat.creation, pdate->answer, 11);
255 lsat.creation[10] = '\0';
256 if (strlen(lsat.creation) != 10)
257 G_fatal_error(_("Illegal date format: [%s] (yyyy-mm-dd)"),
258 lsat.creation);
259 }
260
261 lsat.sun_elev = elev->answer == NULL ? 0. : atof(elev->answer);
262
263 percent = atof(perc->answer);
264 pixel = atoi(dark->answer);
265 rayleigh = atof(atmo->answer);
266 scale = atof(oscale->answer);
267
268 /*
269 * Data from metadata file
270 */
271 lsat.flag =
272 NOMETADATAFILE; /* Unnecessary because G_zero filled, but for sanity */
273 if (met != NULL) {
274 lsat.flag = METADATAFILE;
275 lsat_metadata(met, &lsat);
276 if (print_meta->answer) {
277 char *lsatmeta;
278
279 if (lsatmet->answer == NULL) {
280 G_fatal_error(_("Please use a metadata keyword with -p"));
281 }
282
283 for (i = 0; lsatmet->answers[i] != NULL; i++) {
284 lsatmeta = lsatmet->answers[i];
285
286 if (strcmp(lsatmeta, "number") == 0) {
287 fprintf(stdout, "number=%d\n", lsat.number);
288 }
289 if (strcmp(lsatmeta, "creation") == 0) {
290 fprintf(stdout, "creation=%s\n", lsat.creation);
291 }
292 if (strcmp(lsatmeta, "date") == 0) {
293 fprintf(stdout, "date=%s\n", lsat.date);
294 }
295 if (strcmp(lsatmeta, "sun_elev") == 0) {
296 fprintf(stdout, "sun_elev=%f\n", lsat.sun_elev);
297 }
298 if (strcmp(lsatmeta, "sunaz") == 0) {
299 fprintf(stdout, "sunaz=%f\n", lsat.sun_az);
300 }
301 if (strcmp(lsatmeta, "sensor") == 0) {
302 fprintf(stdout, "sensor=%s\n", lsat.sensor);
303 }
304 if (strcmp(lsatmeta, "bands") == 0) {
305 fprintf(stdout, "bands=%d\n", lsat.bands);
306 }
307 if (strcmp(lsatmet->answer, "time") == 0) {
308 fprintf(stdout, "%f\n", lsat.time);
309 }
310 }
311 exit(EXIT_SUCCESS);
312 }
313 G_debug(1, "lsat.number = %d, lsat.sensor = [%s]", lsat.number,
314 lsat.sensor);
315
316 if (!lsat.sensor[0] || lsat.number > 8 || lsat.number < 1)
317 G_fatal_error(_("Failed to identify satellite"));
318
319 G_debug(1, "Landsat-%d %s with data set in metadata file [%s]",
320 lsat.number, lsat.sensor, met);
321
322 if (elev->answer != NULL) {
323 lsat.sun_elev = atof(elev->answer);
324 G_warning("Overwriting solar elevation of metadata file");
325 }
326 }
327 /*
328 * Data from command line
329 */
330 else if (adate->answer == NULL || elev->answer == NULL) {
331 G_fatal_error(_("Lacking '%s' and/or '%s' for this satellite"),
332 adate->key, elev->key);
333 }
334 else {
335 if (strcmp(sensorname, "tm7") == 0) {
336 if (bgain->answer == NULL || strlen(bgain->answer) != 9)
337 G_fatal_error(
338 _("Landsat-7 requires band gain with 9 (H/L) characters"));
339 set_ETM(&lsat, bgain->answer);
340 }
341 else if (strcmp(sensorname, "oli8") == 0)
342 set_OLI(&lsat);
343 else if (strcmp(sensorname, "tm5") == 0)
344 set_TM5(&lsat);
345 else if (strcmp(sensorname, "tm4") == 0)
346 set_TM4(&lsat);
347 else if (strcmp(sensorname, "mss5") == 0)
348 set_MSS5(&lsat);
349 else if (strcmp(sensorname, "mss4") == 0)
350 set_MSS4(&lsat);
351 else if (strcmp(sensorname, "mss3") == 0)
352 set_MSS3(&lsat);
353 else if (strcmp(sensorname, "mss2") == 0)
354 set_MSS2(&lsat);
355 else if (strcmp(sensorname, "mss1") == 0)
356 set_MSS1(&lsat);
357 else
358 G_fatal_error(_("Unknown satellite type (defined by '%s')"),
359 sensorname);
360 }
361
362 /*****************************************
363 * ------------ PREPARATION --------------
364 *****************************************/
365 if (strcasecmp(metho->answer, "corrected") == 0) /* deleted 2013 */
366 method = CORRECTED;
367 else if (strcasecmp(metho->answer, "dos1") == 0)
368 method = DOS1;
369 else if (strcasecmp(metho->answer, "dos2") == 0)
370 method = DOS2;
371 else if (strcasecmp(metho->answer, "dos2b") == 0)
372 method = DOS2b;
373 else if (strcasecmp(metho->answer, "dos3") == 0)
374 method = DOS3;
375 else if (strcasecmp(metho->answer, "dos4") == 0)
376 method = DOS4;
377 else
378 method = UNCORRECTED;
379
380 /*
381 if (metho->answer[3] == '2') method = (metho->answer[4] == '\0') ? DOS2 :
382 DOS2b; else if (metho->answer[3] == '1') method = DOS1; else if
383 (metho->answer[3] == '3') method = DOS3; else if (metho->answer[3] ==
384 '4') method = DOS4; else method = UNCORRECTED;
385 */
386
387 for (i = 0; i < lsat.bands; i++) {
388 dn_mode[i] = 0;
389 dn_dark[i] = (int)lsat.band[i].qcalmin;
390 dn_sat = (int)(0.90 * lsat.band[i].qcalmax);
391 /* Begin: calculate dark pixel */
392 if (method > DOS && !lsat.band[i].thermal) {
393 for (q = 0; q <= lsat.band[i].qcalmax; q++)
394 hist[q] = 0L;
395
396 snprintf(band_in, sizeof(band_in), "%s%d", inputname,
397 lsat.band[i].code);
398 Rast_get_cellhd(band_in, "", &cellhd);
399 Rast_set_window(&cellhd);
400 if ((infd = Rast_open_old(band_in, "")) < 0)
401 G_fatal_error(_("Unable to open raster map <%s>"), band_in);
402
403 in_data_type = Rast_get_map_type(infd);
404 if (in_data_type < 0)
405 G_fatal_error(_("Unable to read data type of raster map <%s>"),
406 band_in);
407 inrast = Rast_allocate_buf(in_data_type);
408
409 nrows = Rast_window_rows();
410 ncols = Rast_window_cols();
411
412 G_message("Calculating dark pixel of <%s>... ", band_in);
413 for (row = 0; row < nrows; row++) {
414 Rast_get_row(infd, inrast, row, in_data_type);
415 for (col = 0; col < ncols; col++) {
416 switch (in_data_type) {
417 case CELL_TYPE:
418 ptr = (void *)((CELL *)inrast + col);
419 q = (int)*((CELL *)ptr);
420 break;
421 case FCELL_TYPE:
422 ptr = (void *)((FCELL *)inrast + col);
423 q = (int)*((FCELL *)ptr);
424 break;
425 case DCELL_TYPE:
426 ptr = (void *)((DCELL *)inrast + col);
427 q = (int)*((DCELL *)ptr);
428 break;
429 default:
430 ptr = NULL;
431 q = -1.;
432 }
433 if (!Rast_is_null_value(ptr, in_data_type) &&
434 q >= lsat.band[i].qcalmin && q <= lsat.band[i].qcalmax)
435 hist[q]++;
436 }
437 }
438 /* DN of dark object */
439 for (j = lsat.band[i].qcalmin; j <= lsat.band[i].qcalmax; j++) {
440 if (hist[j] >= (unsigned int)pixel) {
441 dn_dark[i] = j;
442 break;
443 }
444 }
445 /* Mode of DN (exclude potentially saturated) */
446 h_max = 0L;
447 for (j = lsat.band[i].qcalmin; j < dn_sat; j++) {
448 /* G_debug(5, "%d-%ld", j, hist[j]); */
449 if (hist[j] > h_max) {
450 h_max = hist[j];
451 dn_mode[i] = j;
452 }
453 }
454 G_verbose_message(
455 "... DN = %.2d [%lu] : mode %.2d [%lu], excluding DN > %d",
456 dn_dark[i], hist[dn_dark[i]], dn_mode[i], hist[dn_mode[i]],
457 dn_sat);
458
459 G_free(inrast);
460 Rast_close(infd);
461 }
462 /* End: calculate dark pixel */
463
464 /* Calculate transformation constants */
465 lsat_bandctes(&lsat, i, method, percent, dn_dark[i], rayleigh);
466 }
467
468 /*
469 * unnecessary or necessary with more checking as acquisition date,...
470 * if (strlen(lsat.creation) == 0)
471 * G_fatal_error(_("Unknown production date (defined by '%s')"),
472 * pdate->key);
473 */
474
475 if (G_verbose() > G_verbose_std()) {
476 fprintf(stderr, "\n LANDSAT: %d SENSOR: %s\n", lsat.number,
477 lsat.sensor);
478 fprintf(stderr, " ACQUISITION DATE %s [production date %s]\n",
479 lsat.date, lsat.creation);
480 fprintf(stderr, " Earth-sun distance = %.8lf\n", lsat.dist_es);
481 fprintf(stderr, " Solar elevation angle = %.8lf\n", lsat.sun_elev);
482 fprintf(stderr, " Atmospheric correction: %s\n",
483 (method == UNCORRECTED ? "UNCORRECTED" : metho->answer));
484 if (method > DOS) {
485 fprintf(stderr,
486 " Percent of solar irradiance in path radiance = %.4lf\n",
487 percent);
488 }
489 for (i = 0; i < lsat.bands; i++) {
490 fprintf(stderr, "-------------------\n");
491 fprintf(stderr, " BAND %d %s(code %d)\n", lsat.band[i].number,
492 (lsat.band[i].thermal ? "thermal " : ""),
493 lsat.band[i].code);
494 fprintf(stderr,
495 " calibrated digital number (DN): %.1lf to %.1lf\n",
496 lsat.band[i].qcalmin, lsat.band[i].qcalmax);
497 fprintf(stderr, " calibration constants (L): %.5lf to %.5lf\n",
498 lsat.band[i].lmin, lsat.band[i].lmax);
499 fprintf(stderr, " at-%s radiance = %.8lf * DN + %.5lf\n",
500 (method > DOS ? "surface" : "sensor"), lsat.band[i].gain,
501 lsat.band[i].bias);
502 if (lsat.band[i].thermal) {
503 fprintf(stderr,
504 " at-sensor temperature = %.5lf / log[(%.5lf / "
505 "radiance) + 1.0]\n",
506 lsat.band[i].K2, lsat.band[i].K1);
507 }
508 else {
509 fprintf(
510 stderr,
511 " mean solar exoatmospheric irradiance (ESUN): %.5lf\n",
512 lsat.band[i].esun);
513 fprintf(stderr, " at-%s reflectance = radiance / %.5lf\n",
514 (method > DOS ? "surface" : "sensor"), lsat.band[i].K1);
515 if (method > DOS) {
516 fprintf(stderr,
517 " the darkness DN with a least %d pixels is %d\n",
518 pixel, dn_dark[i]);
519 fprintf(stderr, " the DN mode is %d\n", dn_mode[i]);
520 }
521 }
522 }
523 fprintf(stderr, "-------------------\n");
524 fflush(stderr);
525 }
526
527 /*****************************************
528 * ------------ CALCULUS -----------------
529 *****************************************/
530
531 G_message(_("Calculating..."));
532 for (i = 0; i < lsat.bands; i++) {
533 snprintf(band_in, sizeof(band_in), "%s%d", inputname,
534 (named->answer ? lsat.band[i].number : lsat.band[i].code));
535 snprintf(band_out, sizeof(band_out), "%s%d", outputname,
536 lsat.band[i].code);
537
538 /* set same size as original band raster */
539 Rast_get_cellhd(band_in, "", &cellhd);
540 Rast_set_window(&cellhd);
541 if ((infd = Rast_open_old(band_in, "")) < 0)
542 G_fatal_error(_("Unable to open raster map <%s>"), band_in);
543
544 if (G_find_raster2(band_out, "")) {
545 if (overwrite) {
546 G_warning(
547 _("Raster map <%s> already exists and will be overwritten"),
548 band_out);
549 }
550 else {
551 G_warning(_("Raster map <%s> exists. Skipping."), band_out);
552 continue;
553 }
554 }
555
556 in_data_type = Rast_get_map_type(infd);
557 if (in_data_type < 0)
558 G_fatal_error(_("Unable to read data type of raster map <%s>"),
559 band_in);
560
561 /* controlling, if we can write the raster */
562 if (G_legal_filename(band_out) < 0)
563 G_fatal_error(_("<%s> is an illegal file name"), band_out);
564
565 if ((outfd = Rast_open_new(band_out, DCELL_TYPE)) < 0)
566 G_fatal_error(_("Unable to create raster map <%s>"), band_out);
567
568 /* allocate input and output buffer */
569 inrast = Rast_allocate_buf(in_data_type);
570 outrast = Rast_allocate_buf(DCELL_TYPE);
571
572 nrows = Rast_window_rows();
573 ncols = Rast_window_cols();
574
575 G_important_message((frad->answer
576 ? _("Writing radiance of <%s> to <%s>...")
577 : (lsat.band[i].thermal)
578 ? _("Writing temperature of <%s> to <%s>...")
579 : _("Writing reflectance of <%s> to <%s>...")),
580 band_in, band_out);
581 for (row = 0; row < nrows; row++) {
582 G_percent(row, nrows, 2);
583
584 Rast_get_row(infd, inrast, row, in_data_type);
585 for (col = 0; col < ncols; col++) {
586 switch (in_data_type) {
587 case CELL_TYPE:
588 ptr = (void *)((CELL *)inrast + col);
589 qcal = (double)((CELL *)inrast)[col];
590 break;
591 case FCELL_TYPE:
592 ptr = (void *)((FCELL *)inrast + col);
593 qcal = (double)((FCELL *)inrast)[col];
594 break;
595 case DCELL_TYPE:
596 ptr = (void *)((DCELL *)inrast + col);
597 qcal = (double)((DCELL *)inrast)[col];
598 break;
599 default:
600 ptr = NULL;
601 qcal = -1.;
602 }
603 if (Rast_is_null_value(ptr, in_data_type) ||
604 qcal < lsat.band[i].qcalmin) {
605 Rast_set_d_null_value((DCELL *)outrast + col, 1);
606 }
607 else {
608 rad = lsat_qcal2rad(qcal, &lsat.band[i]);
609 if (frad->answer) {
610 ref = rad;
611 }
612 else {
613 if (lsat.band[i].thermal) {
614 ref = lsat_rad2temp(rad, &lsat.band[i]);
615 }
616 else {
617 ref = lsat_rad2ref(rad, &lsat.band[i]) * scale;
618 if (ref < 0. && method > DOS)
619 ref = 0.;
620 }
621 }
622 ((DCELL *)outrast)[col] = ref;
623 }
624 }
625 Rast_put_row(outfd, outrast, DCELL_TYPE);
626 }
627 G_percent(1, 1, 1);
628
629 ref_mode = 0.;
630 if (method > DOS && !lsat.band[i].thermal) {
631 ref_mode = lsat_qcal2rad(dn_mode[i], &lsat.band[i]);
632 ref_mode = lsat_rad2ref(ref_mode, &lsat.band[i]);
633 }
634
635 G_free(inrast);
636 Rast_close(infd);
637 G_free(outrast);
638 Rast_close(outfd);
639
640 /*
641 * needed?
642 * if (out_type != CELL_TYPE)
643 * G_quantize_fp_map_range(band_out, G_mapset(), 0., 360., 0,
644 * 360);
645 */
646
647 /* set grey255 colortable */
648 Rast_init_colors(&colors);
649 Rast_read_fp_range(band_out, G_mapset(), &range);
650 Rast_get_fp_range_min_max(&range, &min, &max);
651 Rast_make_grey_scale_fp_colors(&colors, min, max);
652 Rast_write_colors(band_out, G_mapset(), &colors);
653
654 /* Initialize the 'history' structure with basic info */
655 Rast_short_history(band_out, "raster", &history);
656 Rast_append_format_history(
657 &history, " %s of Landsat-%d %s (method %s)",
658 (frad->answer
659 ? "Radiance"
660 : (lsat.band[i].thermal ? "Temperature" : "Reflectance")),
661 lsat.number, lsat.sensor, metho->answer);
662 Rast_append_history(&history, "----------------------------------------"
663 "-------------------------");
664 Rast_append_format_history(
665 &history, " Acquisition date (and time) ........... %s (%.4lf h)",
666 lsat.date, lsat.time);
667 Rast_append_format_history(
668 &history, " Production date ....................... %s\n",
669 lsat.creation);
670 Rast_append_format_history(
671 &history, " Earth-sun distance (d) ................ %.7lf",
672 lsat.dist_es);
673 Rast_append_format_history(
674 &history, " Sun elevation (and azimuth) ........... %.5lf (%.5lf)",
675 lsat.sun_elev, lsat.sun_az);
676 Rast_append_format_history(
677 &history, " Digital number (DN) range ............. %.0lf to %.0lf",
678 lsat.band[i].qcalmin, lsat.band[i].qcalmax);
679 Rast_append_format_history(
680 &history,
681 " Calibration constants (Lmin to Lmax) .. %+.5lf to %+.5lf",
682 lsat.band[i].lmin, lsat.band[i].lmax);
683 Rast_append_format_history(
684 &history,
685 " DN to Radiance (gain and bias) ........ %+.5lf and %+.5lf",
686 lsat.band[i].gain, lsat.band[i].bias);
687 if (lsat.band[i].thermal) {
688 Rast_append_format_history(
689 &history,
690 " Temperature (K1 and K2) ............... %.3lf and %.3lf",
691 lsat.band[i].K1, lsat.band[i].K2);
692 }
693 else {
694 Rast_append_format_history(
695 &history, " Mean solar irradiance (ESUN) .......... %.3lf",
696 lsat.band[i].esun);
697 Rast_append_format_history(
698 &history, " Radiance to Reflectance (divide by) ... %+.5lf",
699 lsat.band[i].K1);
700 if (method > DOS) {
701 Rast_append_format_history(&history, " ");
702 Rast_append_format_history(
703 &history, " Dark object (%4d pixels) DN = ........ %d",
704 pixel, dn_dark[i]);
705 Rast_append_format_history(
706 &history, " Mode in reflectance histogram ......... %.5lf",
707 ref_mode);
708 }
709 }
710 Rast_append_history(&history, "----------------------------------------"
711 "--------------------------");
712
713 Rast_command_history(&history);
714 Rast_write_history(band_out, &history);
715
716 if (lsat.band[i].thermal)
717 Rast_write_units(band_out, "Kelvin");
718 else if (frad->answer)
719 Rast_write_units(band_out, "W/(m^2 sr um)");
720 else
721 Rast_write_units(band_out, "unitless");
722
723 /* set raster timestamp from acq date? (see r.timestamp module) */
724 }
725 Rast_set_window(&orig_cellhd);
726
727 exit(EXIT_SUCCESS);
728 }