-
Notifications
You must be signed in to change notification settings - Fork 0
/
radiance.py
160 lines (120 loc) · 3.66 KB
/
radiance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from osgeo import gdal
import os
def gainbias(input_scene, gain, bias, output_scene):
"""
Function to convert digtial numbers to TOA radiance.
Uses the gain and bias method. To be used with reflective
or emissive bands.
Params
------
input_scene : str
File path to Landsat scene (.TIF)
gain : float
Band specific rescaling gain factor (multiplicative).
bias : float
Band specific rescaling bias factor (additive).
output_scene : str
File path to output scene (.TIF). File must not already
exist.
Returns
-------
None
"""
# ensure output does not already exist
if os.path.exists(output_scene):
raise ValueError(f"{output_scene} already exists!")
# open source dataset
src_ds = gdal.Open(input_scene)
src_band = src_ds.GetRasterBand(1)
# get source nodata value
no_data = src_band.GetNoDataValue()
# read in source array
src_ar = src_band.ReadAsArray()
# create target dataset
drv = gdal.GetDriverByName("GTiff")
trg_ds = drv.Create(
output_scene,
src_ds.RasterXSize,
src_ds.RasterYSize,
1,
gdal.GDT_Float32
)
# set target metadata
trg_ds.SetGeoTransform(src_ds.GetGeoTransform())
trg_ds.SetProjection(src_ds.GetProjection())
# get target band
trg_band = trg_ds.GetRasterBand(1)
# set target nodata value
trg_band.SetNoDataValue(no_data)
# convert to radiance
trg_ar = gain * src_ar + bias
trg_ar[src_ar == no_data] = no_data
# write radiance to band
trg_band.WriteArray(trg_ar)
# close handles
trg_ds.FlushCache()
trg_ds = None
trg_band = None
src_ds = None
src_band = None
def scaling(input_scene, lmin, lmax, qcalmin, qcalmax, output_scene):
"""
Function to convert digtial numbers to TOA radiance.
Uses the spectral radiance scaling method. To be used
with reflective or emissive bands.
Params
------
input_scene : str
File path to Landsat scene (.TIF)
lmin : float
Spectral radiance scale to qcalmin.
lmax : float
Spectral radiance scale to qcalmax.
qcalmin : float
The minimum quantized calibrated pixel value. Typically 1.
qcalmax : float
The maximum quantized calibrated pixel value. Typically 255.
output_scene : str
File path to output scene (.TIF). File must not already
exist.
Returns
-------
None
"""
# ensure output does not already exist
if os.path.exists(output_scene):
raise ValueError(f"{output_scene} already exists!")
# open source dataset
src_ds = gdal.Open(input_scene)
src_band = src_ds.GetRasterBand(1)
# get source nodata value
no_data = src_band.GetNoDataValue()
# read in source array
src_ar = src_band.ReadAsArray()
# create target dataset
drv = gdal.GetDriverByName("GTiff")
trg_ds = drv.Create(
output_scene,
src_ds.RasterXSize,
src_ds.RasterYSize,
1,
gdal.GDT_Float32
)
# set target metadata
trg_ds.SetGeoTransform(src_ds.GetGeoTransform())
trg_ds.SetProjection(src_ds.GetProjection())
# get target band
trg_band = trg_ds.GetRasterBand(1)
# set target nodata value
trg_band.SetNoDataValue(no_data)
# convert to radiance
trg_ar = ((lmax - lmin)/(qcalmax - qcalmin)) * (src_ar - qcalmin) + lmin
trg_ar[src_ar == no_data] = no_data
# write radiance to band
trg_band.WriteArray(trg_ar)
# close handles
trg_ds.FlushCache()
trg_ds = None
trg_band = None
src_ds = None
src_band = None