| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <math.h> |
| 5 |
|
| 6 |
#include <grass/gis.h> |
| 7 |
#include <grass/glocale.h> |
| 8 |
|
| 9 |
#include "local_proto.h" |
| 10 |
#include "earth_sun.h" |
| 11 |
|
| 12 |
#define PI M_PI |
| 13 |
#define D2R M_PI / 180. |
| 14 |
|
| 15 |
#define MAX_STR 256 |
| 16 |
#define METADATA_SIZE 65535 /* MTL.txt file size 65535 bytes */ |
| 17 |
#define TM5_MET_SIZE 28700 /* .met file size 28686 bytes */ |
| 18 |
|
| 19 |
static void chrncpy(char *dest, char src[], int n) |
| 20 |
{ |
| 21 |
int i; |
| 22 |
|
| 23 |
for (i = 0; i < n && src[i] != '\0' && src[i] != '\"'; i++) |
| 24 |
dest[i] = src[i]; |
| 25 |
dest[i] = '\0'; |
| 26 |
} |
| 27 |
|
| 28 |
static void date_replace_slash(char *str) |
| 29 |
{ |
| 30 |
while (*str) { |
| 31 |
if (*str == '/') |
| 32 |
*str = '-'; |
| 33 |
str++; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/* OLD Metadata Files */ |
| 38 |
void get_metformat(const char metadata[], char *key, char value[]) |
| 39 |
{ |
| 40 |
int i = 0; |
| 41 |
char *ptrmet = strstr(metadata, key); |
| 42 |
char *ptr; |
| 43 |
|
| 44 |
if (ptrmet != NULL) { |
| 45 |
ptr = strstr(ptrmet, " VALUE "); |
| 46 |
if (ptr != NULL) { |
| 47 |
while (*ptr++ != '=') |
| 48 |
; |
| 49 |
while (*ptr <= ' ') |
| 50 |
ptr++; |
| 51 |
if (*ptr == '\"') |
| 52 |
ptr++; |
| 53 |
while (i < MAX_STR && *ptr != '\"' && *ptr >= ' ') |
| 54 |
value[i++] = *ptr++; |
| 55 |
} |
| 56 |
} |
| 57 |
value[i] = '\0'; |
| 58 |
} |
| 59 |
|
| 60 |
/* future implementation |
| 61 |
double get_metdouble(const char metadata[], char *format, int code, char |
| 62 |
value[]) |
| 63 |
{ |
| 64 |
char key[MAX_STR]; |
| 65 |
|
| 66 |
sprintf(key, format, code); |
| 67 |
get_metformat(metadata, key, value); |
| 68 |
return atof(value); |
| 69 |
} |
| 70 |
*/ |
| 71 |
|
| 72 |
/* NEW Metadata Files */ |
| 73 |
void get_mtlformat(const char metadata[], char *key, char value[]) |
| 74 |
{ |
| 75 |
int i = 0; |
| 76 |
char *ptr = strstr(metadata, key); |
| 77 |
|
| 78 |
if (ptr != NULL) { |
| 79 |
while (*ptr++ != '=') |
| 80 |
; |
| 81 |
while (*ptr <= ' ') |
| 82 |
ptr++; |
| 83 |
if (*ptr == '\"') |
| 84 |
ptr++; |
| 85 |
while (i < MAX_STR && *ptr != '\"' && *ptr > ' ') |
| 86 |
value[i++] = *ptr++; |
| 87 |
} |
| 88 |
value[i] = '\0'; |
| 89 |
} |
| 90 |
|
| 91 |
/* future implementation |
| 92 |
double get_mtldouble(const char metadata[], char *format, int code, char |
| 93 |
value[]) |
| 94 |
{ |
| 95 |
char key[MAX_STR]; |
| 96 |
|
| 97 |
sprintf(key, format, code); |
| 98 |
get_mtlformat(metadata, key, value); |
| 99 |
return atof(value); |
| 100 |
} |
| 101 |
*/ |
| 102 |
|
| 103 |
/**************************************************************************** |
| 104 |
* PURPOSE: Read parameters from Landsat metadata files |
| 105 |
*****************************************************************************/ |
| 106 |
|
| 107 |
void lsat_metadata(char *metafile, lsat_data *lsat) |
| 108 |
{ |
| 109 |
FILE *f; |
| 110 |
char mtldata[METADATA_SIZE] = {'\0'}; |
| 111 |
char key[MAX_STR], value[MAX_STR]; |
| 112 |
void (*get_mtldata)(const char[], char *, char[]); |
| 113 |
|
| 114 |
/* void (*get_mtlreal) (const char[], char *, int, char[]); */ |
| 115 |
int i, j, ver_mtl; |
| 116 |
|
| 117 |
/* store metadata in ram */ |
| 118 |
if ((f = fopen(metafile, "r")) == NULL) |
| 119 |
G_fatal_error(_("Metadata file <%s> not found"), metafile); |
| 120 |
i = fread(mtldata, METADATA_SIZE - 1, 1, f); |
| 121 |
(void)fclose(f); |
| 122 |
|
| 123 |
/* set version of the metadata file */ |
| 124 |
get_mtldata = |
| 125 |
(strstr(mtldata, " VALUE ") != NULL) ? get_metformat : get_mtlformat; |
| 126 |
/* future implementation |
| 127 |
if (strstr(mtldata, " VALUE ") != NULL) |
| 128 |
{ |
| 129 |
get_mtldata = get_metformat; |
| 130 |
get_mtlreal = get_metdouble; |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
get_mtldata = get_mtlformat; |
| 135 |
get_mtlreal = get_mtldouble; |
| 136 |
} |
| 137 |
*/ |
| 138 |
ver_mtl = (strstr(mtldata, "QCALMAX_BAND") != NULL) ? 0 : 1; |
| 139 |
|
| 140 |
/* Fill with product metadata */ |
| 141 |
get_mtldata(mtldata, "SPACECRAFT_ID", value); |
| 142 |
if (value[0] == '\0') { |
| 143 |
get_mtldata(mtldata, "PLATFORMSHORTNAME", value); |
| 144 |
} |
| 145 |
i = 0; |
| 146 |
while (value[i] && (value[i] < '0' || value[i] > '9')) |
| 147 |
i++; |
| 148 |
lsat->number = atoi(value + i); |
| 149 |
|
| 150 |
get_mtldata(mtldata, "SENSOR_ID", value); |
| 151 |
if (value[0] == '\0') { |
| 152 |
get_mtldata(mtldata, "SENSORSHORTNAME", value); |
| 153 |
} |
| 154 |
chrncpy(lsat->sensor, value, 8); |
| 155 |
|
| 156 |
get_mtldata(mtldata, "DATE_ACQUIRED", value); |
| 157 |
if (value[0] == '\0') { |
| 158 |
get_mtldata(mtldata, "ACQUISITION_DATE", value); |
| 159 |
if (value[0] == '\0') { |
| 160 |
get_mtldata(mtldata, "CALENDARDATE", value); |
| 161 |
} |
| 162 |
} |
| 163 |
if (value[0] != '\0') { |
| 164 |
date_replace_slash(value); |
| 165 |
chrncpy(lsat->date, value, 10); |
| 166 |
} |
| 167 |
else |
| 168 |
G_warning("Using acquisition date from the command line 'date'"); |
| 169 |
|
| 170 |
get_mtldata(mtldata, "FILE_DATE", value); |
| 171 |
if (value[0] == '\0') { |
| 172 |
get_mtldata(mtldata, "CREATION_TIME", value); |
| 173 |
if (value[0] == '\0') { |
| 174 |
get_mtldata(mtldata, "PRODUCTIONDATETIME", value); |
| 175 |
} |
| 176 |
} |
| 177 |
if (value[0] != '\0') { |
| 178 |
date_replace_slash(value); |
| 179 |
chrncpy(lsat->creation, value, 10); |
| 180 |
} |
| 181 |
else |
| 182 |
G_warning("Using production date from the command line 'product_date'"); |
| 183 |
|
| 184 |
get_mtldata(mtldata, "SUN_AZIMUTH", value); |
| 185 |
lsat->sun_az = atof(value); |
| 186 |
if (lsat->sun_az == 0.) |
| 187 |
G_warning("Sun azimuth is %f", lsat->sun_az); |
| 188 |
|
| 189 |
get_mtldata(mtldata, "SUN_ELEVATION", value); |
| 190 |
if (value[0] == '\0') { |
| 191 |
get_mtldata(mtldata, "SolarElevation", value); |
| 192 |
} |
| 193 |
lsat->sun_elev = atof(value); |
| 194 |
if (lsat->sun_elev == 0.) |
| 195 |
G_warning("Sun elevation is %f", lsat->sun_elev); |
| 196 |
|
| 197 |
get_mtldata(mtldata, "SCENE_CENTER_TIME", value); |
| 198 |
if (value[0] == '\0') { |
| 199 |
get_mtldata(mtldata, "SCENE_CENTER_SCAN_TIME", value); |
| 200 |
} |
| 201 |
if (value[0] != '\0') { |
| 202 |
value[strlen(value) - 1] = '\0'; /* Remove trailing 'z' */ |
| 203 |
G_llres_scan(value, &lsat->time); /* Cast from hh:mm:ss into hh.hhh */ |
| 204 |
} |
| 205 |
if (lsat->time == 0.) |
| 206 |
G_warning("Scene time is %f", lsat->time); |
| 207 |
|
| 208 |
/* Fill the data with the basic/default sensor parameters */ |
| 209 |
switch (lsat->number) { |
| 210 |
case 1: |
| 211 |
set_MSS1(lsat); |
| 212 |
break; |
| 213 |
case 2: |
| 214 |
set_MSS2(lsat); |
| 215 |
break; |
| 216 |
case 3: |
| 217 |
set_MSS3(lsat); |
| 218 |
break; |
| 219 |
case 4: |
| 220 |
if (lsat->sensor[0] == 'M') |
| 221 |
set_MSS4(lsat); |
| 222 |
else |
| 223 |
set_TM4(lsat); |
| 224 |
break; |
| 225 |
case 5: |
| 226 |
if (lsat->sensor[0] == 'M') |
| 227 |
set_MSS5(lsat); |
| 228 |
else |
| 229 |
set_TM5(lsat); |
| 230 |
break; |
| 231 |
case 7: { |
| 232 |
char *fmt_gain[] = {"BAND%d_GAIN%d", " GAIN_BAND_%d_VCID_%d"}; |
| 233 |
for (i = 1, j = 0; i < 9; i++) { |
| 234 |
snprintf(key, sizeof(key), fmt_gain[ver_mtl], i, 1); |
| 235 |
if (i != 6) |
| 236 |
key[ver_mtl == 0 ? 10 : 12] = '\0'; |
| 237 |
get_mtldata(mtldata, key, value + j++); |
| 238 |
if (i == 6) { |
| 239 |
snprintf(key, sizeof(key), fmt_gain[ver_mtl], i, 2); |
| 240 |
get_mtldata(mtldata, key, value + j++); |
| 241 |
} |
| 242 |
} |
| 243 |
value[j] = '\0'; |
| 244 |
G_debug(1, "ETM+ gain = [%s]", value); |
| 245 |
set_ETM(lsat, value); |
| 246 |
break; |
| 247 |
} |
| 248 |
case 8: |
| 249 |
set_OLI(lsat); |
| 250 |
break; |
| 251 |
default: |
| 252 |
G_warning("Unable to recognize satellite platform [%d]", lsat->number); |
| 253 |
break; |
| 254 |
} |
| 255 |
|
| 256 |
/* Update the information from metadata file, if infile */ |
| 257 |
if (get_mtldata == get_mtlformat) { |
| 258 |
if (ver_mtl == 0) { |
| 259 |
G_verbose_message("Metada file is MTL file: old format"); |
| 260 |
for (i = 0; i < lsat->bands; i++) { |
| 261 |
snprintf(key, sizeof(key), "LMAX_BAND%d", lsat->band[i].code); |
| 262 |
get_mtldata(mtldata, key, value); |
| 263 |
lsat->band[i].lmax = atof(value); |
| 264 |
snprintf(key, sizeof(key), "LMIN_BAND%d", lsat->band[i].code); |
| 265 |
get_mtldata(mtldata, key, value); |
| 266 |
lsat->band[i].lmin = atof(value); |
| 267 |
snprintf(key, sizeof(key), "QCALMAX_BAND%d", |
| 268 |
lsat->band[i].code); |
| 269 |
get_mtldata(mtldata, key, value); |
| 270 |
lsat->band[i].qcalmax = atof(value); |
| 271 |
snprintf(key, sizeof(key), "QCALMIN_BAND%d", |
| 272 |
lsat->band[i].code); |
| 273 |
get_mtldata(mtldata, key, value); |
| 274 |
lsat->band[i].qcalmin = atof(value); |
| 275 |
} |
| 276 |
} |
| 277 |
else { |
| 278 |
G_verbose_message("Metada file is MTL file: new format"); |
| 279 |
/* Other possible values in the metadata file */ |
| 280 |
get_mtldata(mtldata, "EARTH_SUN_DISTANCE", |
| 281 |
value); /* Necessary after */ |
| 282 |
if (value[0] != '\0') |
| 283 |
lsat->dist_es = atof(value); |
| 284 |
if (strstr(mtldata, "RADIANCE_MAXIMUM_BAND") != NULL) { |
| 285 |
G_verbose_message( |
| 286 |
"RADIANCE & QUANTIZE from MIN_MAX_(RADIANCE|PIXEL_VALUE)"); |
| 287 |
for (i = 0; i < lsat->bands; i++) { |
| 288 |
if (lsat->number == 7 && lsat->band[i].thermal) { |
| 289 |
snprintf(key, sizeof(key), |
| 290 |
"RADIANCE_MAXIMUM_BAND_6_VCID_%d", |
| 291 |
lsat->band[i].code - 60); |
| 292 |
get_mtldata(mtldata, key, value); |
| 293 |
lsat->band[i].lmax = atof(value); |
| 294 |
snprintf(key, sizeof(key), |
| 295 |
"RADIANCE_MINIMUM_BAND_6_VCID_%d", |
| 296 |
lsat->band[i].code - 60); |
| 297 |
get_mtldata(mtldata, key, value); |
| 298 |
lsat->band[i].lmin = atof(value); |
| 299 |
snprintf(key, sizeof(key), |
| 300 |
"QUANTIZE_CAL_MAX_BAND_6_VCID_%d", |
| 301 |
lsat->band[i].code - 60); |
| 302 |
get_mtldata(mtldata, key, value); |
| 303 |
lsat->band[i].qcalmax = atof(value); |
| 304 |
snprintf(key, sizeof(key), |
| 305 |
"QUANTIZE_CAL_MIN_BAND_6_VCID_%d", |
| 306 |
lsat->band[i].code - 60); |
| 307 |
get_mtldata(mtldata, key, value); |
| 308 |
lsat->band[i].qcalmin = atof(value); |
| 309 |
} |
| 310 |
else { |
| 311 |
snprintf(key, sizeof(key), "RADIANCE_MAXIMUM_BAND_%d", |
| 312 |
lsat->band[i].code); |
| 313 |
get_mtldata(mtldata, key, value); |
| 314 |
lsat->band[i].lmax = atof(value); |
| 315 |
snprintf(key, sizeof(key), "RADIANCE_MINIMUM_BAND_%d", |
| 316 |
lsat->band[i].code); |
| 317 |
get_mtldata(mtldata, key, value); |
| 318 |
lsat->band[i].lmin = atof(value); |
| 319 |
snprintf(key, sizeof(key), "QUANTIZE_CAL_MAX_BAND_%d", |
| 320 |
lsat->band[i].code); |
| 321 |
get_mtldata(mtldata, key, value); |
| 322 |
lsat->band[i].qcalmax = atof(value); |
| 323 |
snprintf(key, sizeof(key), "QUANTIZE_CAL_MIN_BAND_%d", |
| 324 |
lsat->band[i].code); |
| 325 |
get_mtldata(mtldata, key, value); |
| 326 |
lsat->band[i].qcalmin = atof(value); |
| 327 |
} |
| 328 |
/* other possible values of each band */ |
| 329 |
if (lsat->band[i].thermal) { |
| 330 |
snprintf(key, sizeof(key), "K1_CONSTANT_BAND_%d", |
| 331 |
lsat->band[i].code); |
| 332 |
get_mtldata(mtldata, key, value); |
| 333 |
if (value[0] != '\0') |
| 334 |
lsat->band[i].K1 = atof(value); |
| 335 |
snprintf(key, sizeof(key), "K2_CONSTANT_BAND_%d", |
| 336 |
lsat->band[i].code); |
| 337 |
get_mtldata(mtldata, key, value); |
| 338 |
if (value[0] != '\0') |
| 339 |
lsat->band[i].K2 = atof(value); |
| 340 |
} |
| 341 |
else if (lsat->number == 8) { |
| 342 |
/* ESUN from REFLECTANCE and RADIANCE ADD_BAND */ |
| 343 |
snprintf(key, sizeof(key), |
| 344 |
"REFLECTANCE_MAXIMUM_BAND_%d", |
| 345 |
lsat->band[i].code); |
| 346 |
get_mtldata(mtldata, key, value); |
| 347 |
if (value[0] != '\0') |
| 348 |
lsat->band[i].esun = |
| 349 |
(double)(PI * lsat->dist_es * lsat->dist_es * |
| 350 |
lsat->band[i].lmax) / |
| 351 |
atof(value); |
| 352 |
} |
| 353 |
} |
| 354 |
if (lsat->number == 8) |
| 355 |
G_warning("ESUN evaluated from REFLECTANCE_MAXIMUM_BAND"); |
| 356 |
} |
| 357 |
else { |
| 358 |
G_verbose_message( |
| 359 |
"RADIANCE & QUANTIZE from RADIOMETRIC_RESCALING"); |
| 360 |
for (i = 0; i < lsat->bands; i++) { |
| 361 |
snprintf(key, sizeof(key), "RADIANCE_MULT_BAND_%d", |
| 362 |
lsat->band[i].code); |
| 363 |
get_mtldata(mtldata, key, value); |
| 364 |
lsat->band[i].gain = atof(value); |
| 365 |
snprintf(key, sizeof(key), "RADIANCE_ADD_BAND_%d", |
| 366 |
lsat->band[i].code); |
| 367 |
get_mtldata(mtldata, key, value); |
| 368 |
lsat->band[i].bias = atof(value); |
| 369 |
/* reversing to calculate the values of Lmin and Lmax ... */ |
| 370 |
lsat->band[i].lmin = |
| 371 |
lsat->band[i].gain * lsat->band[i].qcalmin + |
| 372 |
lsat->band[i].bias; |
| 373 |
lsat->band[i].lmax = |
| 374 |
lsat->band[i].gain * lsat->band[i].qcalmax + |
| 375 |
lsat->band[i].bias; |
| 376 |
/* ... qcalmax and qcalmin loaded in previous sensor_ |
| 377 |
* function */ |
| 378 |
if (lsat->number == 8) { |
| 379 |
if (lsat->band[i].thermal) { |
| 380 |
snprintf(key, sizeof(key), "K1_CONSTANT_BAND_%d", |
| 381 |
lsat->band[i].code); |
| 382 |
get_mtldata(mtldata, key, value); |
| 383 |
lsat->band[i].K1 = atof(value); |
| 384 |
snprintf(key, sizeof(key), "K2_CONSTANT_BAND_%d", |
| 385 |
lsat->band[i].code); |
| 386 |
get_mtldata(mtldata, key, value); |
| 387 |
lsat->band[i].K2 = atof(value); |
| 388 |
} |
| 389 |
else { |
| 390 |
snprintf(key, sizeof(key), |
| 391 |
"REFLECTANCE_MULT_BAND_%d", |
| 392 |
lsat->band[i].code); |
| 393 |
get_mtldata(mtldata, key, value); |
| 394 |
lsat->band[i].K1 = atof(value); |
| 395 |
snprintf(key, sizeof(key), |
| 396 |
"REFLECTANCE_ADD_BAND_%d", |
| 397 |
lsat->band[i].code); |
| 398 |
get_mtldata(mtldata, key, value); |
| 399 |
lsat->band[i].K2 = atof(value); |
| 400 |
/* ESUN from REFLECTANCE_ADD_BAND */ |
| 401 |
lsat->band[i].esun = |
| 402 |
(double)(PI * lsat->dist_es * lsat->dist_es * |
| 403 |
lsat->band[i].bias) / |
| 404 |
lsat->band[i].K2; |
| 405 |
/* |
| 406 |
double esun1 = (double) (PI * lsat->dist_es * |
| 407 |
lsat->dist_es * lsat->band[i].bias) / |
| 408 |
lsat->band[i].K2; double esun2 = (double) (PI * |
| 409 |
lsat->dist_es * lsat->dist_es * |
| 410 |
lsat->band[i].gain) / lsat->band[i].K1; |
| 411 |
lsat->band[i].esun = (esun1 + esun2) / 2.; |
| 412 |
*/ |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
G_warning( |
| 417 |
"ESUN evaluated from REFLECTANCE_ADDITIVE_FACTOR_BAND"); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
else { |
| 422 |
G_verbose_message("Metadata file is MET file"); |
| 423 |
G_verbose_message( |
| 424 |
"RADIANCE & QUANTIZE from band setting of the metadata file"); |
| 425 |
for (i = 0; i < lsat->bands; i++) { |
| 426 |
snprintf(key, sizeof(key), "Band%dGainSetting", lsat->band[i].code); |
| 427 |
get_mtldata(mtldata, key, value); |
| 428 |
if (value[0] == '\0') { |
| 429 |
G_warning("%s", key); |
| 430 |
continue; |
| 431 |
} |
| 432 |
lsat->band[i].gain = atof(value); |
| 433 |
snprintf(key, sizeof(key), "Band%dBiasSetting", lsat->band[i].code); |
| 434 |
get_mtldata(mtldata, key, value); |
| 435 |
if (value[0] == '\0') { |
| 436 |
G_warning("%s", key); |
| 437 |
continue; |
| 438 |
} |
| 439 |
lsat->band[i].bias = atof(value); |
| 440 |
|
| 441 |
lsat->band[i].qcalmax = 255.; |
| 442 |
lsat->band[i].qcalmin = 1.; |
| 443 |
lsat->band[i].lmin = |
| 444 |
lsat->band[i].gain * lsat->band[i].qcalmin + lsat->band[i].bias; |
| 445 |
lsat->band[i].lmax = |
| 446 |
lsat->band[i].gain * lsat->band[i].qcalmax + lsat->band[i].bias; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
return; |
| 451 |
} |