Debian Codegraph @

imagery/i.landsat.toar/landsat.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <grass/gis.h>
5 #include <grass/glocale.h>
6
7 #include "landsat.h"
8
9 #define PI M_PI
10 #define R2D 180. / M_PI
11 #define D2R M_PI / 180.
12
13 /****************************************************************************
14 * PURPOSE: Calibrated Digital Number to at-satellite Radiance
15 *****************************************************************************/
16 double lsat_qcal2rad(double qcal, band_data *band)
17 {
18 return (double)(qcal * band->gain + band->bias);
19 }
20
21 /****************************************************************************
22 * PURPOSE: Radiance of non-thermal band to at-satellite Reflectance
23 *****************************************************************************/
24 double lsat_rad2ref(double rad, band_data *band)
25 {
26 return (double)(rad / band->K1);
27 }
28
29 /****************************************************************************
30 * PURPOSE: Radiance of thermal band to at-satellite Temperature
31 *****************************************************************************/
32 double lsat_rad2temp(double rad, band_data *band)
33 {
34 return (double)(band->K2 / log((band->K1 / rad) + 1.));
35 }
36
37 /****************************************************************************
38 * PURPOSE: Some band constants
39 *
40 * zenith = 90 - sun_elevation
41 * sin( sun_elevation ) = cos( sun_zenith )
42 *
43 * lsat : satellite data
44 * i : band number
45 * method : level of atmospheric correction
46 * percent : percent of solar irradiance in path radiance
47 * dark : digital number of object for DOS
48 *****************************************************************************/
49
50 #define abs(x) (((x) > 0) ? (x) : (-x))
51
52 void lsat_bandctes(lsat_data *lsat, int i, char method, double percent,
53 int dark, double rayleigh)
54 {
55 double pi_d2, sin_e, cos_v;
56
57 /* TAUv = at. transmittance surface-sensor */
58 /* TAUz = at. transmittance sun-surface */
59 /* Edown = diffuse sky spectral irradiance */
60 double TAUv, TAUz, Edown;
61
62 pi_d2 = (double)(PI * lsat->dist_es * lsat->dist_es);
63 sin_e = (double)(sin(D2R * lsat->sun_elev));
64 cos_v = (double)(cos(D2R * (lsat->number < 4 ? 9.2 : 8.2)));
65
66 /** Global irradiance on the sensor.
67 * Radiance to reflectance coefficient, only NO thermal bands.
68 * K1 and K2 variables are also utilized as thermal constants
69 */
70 if (lsat->band[i].thermal == 0) {
71 switch (method) {
72 case DOS2: {
73 TAUv = 1.;
74 TAUz = (lsat->band[i].wavemax < 1.) ? sin_e : 1.;
75 Edown = 0.;
76 break;
77 }
78 case DOS2b: {
79 TAUv = (lsat->band[i].wavemax < 1.) ? cos_v : 1.;
80 TAUz = (lsat->band[i].wavemax < 1.) ? sin_e : 1.;
81 Edown = 0.;
82 break;
83 }
84 case DOS3: {
85 double t;
86
87 t = 2. / (lsat->band[i].wavemax + lsat->band[i].wavemin);
88 t = 0.008569 * t * t * t * t *
89 (1 + 0.0113 * t * t + 0.000013 * t * t * t * t);
90 TAUv = exp(-t / cos_v);
91 TAUz = exp(-t / sin_e);
92 Edown = rayleigh;
93 break;
94 }
95 case DOS4: {
96 double Ro = (lsat->band[i].lmax - lsat->band[i].lmin) *
97 (dark - lsat->band[i].qcalmin) /
98 (lsat->band[i].qcalmax - lsat->band[i].qcalmin) +
99 lsat->band[i].lmin;
100 double Tv = 1.;
101 double Tz = 1.;
102 double Lp = 0.;
103
104 do {
105 TAUz = Tz;
106 TAUv = Tv;
107 Lp = Ro - percent * TAUv *
108 (lsat->band[i].esun * sin_e * TAUz + PI * Lp) /
109 pi_d2;
110 Tz = 1. - (4. * pi_d2 * Lp) / (lsat->band[i].esun * sin_e);
111 if (Tz <= 0)
112 G_fatal_error(
113 _("The DOS4 method is not applicable here: "
114 "approximation of atmospheric transmittance "
115 "coefficients is unstable. Use another DOS method or "
116 "use other sun_elevation parameter"));
117 Tv = exp(sin_e * log(Tz) / cos_v);
118 } while (TAUv != Tv && TAUz != Tz);
119 TAUz = (Tz < 1. ? Tz : 1.);
120 TAUv = (Tv < 1. ? Tv : 1.);
121 Edown = (Lp < 0. ? 0. : PI * Lp);
122 break;
123 }
124 default: /* DOS1 and Without atmospheric-correction */
125 TAUv = 1.;
126 TAUz = 1.;
127 Edown = 0.;
128 break;
129 }
130 lsat->band[i].K2 = 0.;
131 lsat->band[i].K1 =
132 TAUv * (lsat->band[i].esun * sin_e * TAUz + Edown) / pi_d2;
133 if (method > DOS)
134 G_verbose_message("... TAUv = %.5f, TAUz = %.5f, Edown = %.5f\n",
135 TAUv, TAUz, Edown);
136 }
137
138 /** Digital number to radiance coefficients.
139 * Without atmospheric calibration for thermal bands.
140 */
141 lsat->band[i].gain = (lsat->band[i].lmax - lsat->band[i].lmin) /
142 (lsat->band[i].qcalmax - lsat->band[i].qcalmin);
143
144 if (method == UNCORRECTED || lsat->band[i].thermal) {
145 /* L = G * (DN - Qmin) + Lmin
146 * -> bias = Lmin - G * Qmin
147 */
148 lsat->band[i].bias =
149 (lsat->band[i].lmin - lsat->band[i].gain * lsat->band[i].qcalmin);
150 }
151 else if (method > DOS) {
152 /* L = Lsat - Lpath = G * DNsat + B - (G * + B - p * rad_sun)
153 * = G * DNsat - G * + p * rad_sun
154 * -> bias = p * rad_sun - G
155 */
156 lsat->band[i].bias =
157 percent * lsat->band[i].K1 - lsat->band[i].gain * dark;
158 }
159 }