-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_IBOAR.py
358 lines (266 loc) · 12.3 KB
/
get_IBOAR.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# -*- coding: utf-8 -*-
"""
@author: Adrien Wehrlé, GEUS (Geological Survey of Denmark and Greenland)
Computes the Intrinsic Bottom of Atmosphere Reflectance (IBOAR) for a given scene
and given bands. ArcticDEM derived slopes and aspects are used after resampling
and clipping.
Functions are first initialized and then run at the end of the code.
INPUTS:
run_command_line: run the code from the command line (using an argument parser) [boolean]
time_it: set to True to print processing time [boolean]
verbose: set to True to print details about processing [boolean]
inpath: path to the folder containing the desired S3 scene processed using the
2nd step of SICE processing pipeline (https://github.com/mankoff/SICE/) [string]
inpath_adem: path to the folder containing regional ArcticDEM derived
slopes and aspects [string]
region: region over which the toolchain is run [string]
slope_thres: slope threshold in degrees to create slope_flag. Default
is set to 15° based on the "small slope approximation"
(Picard et al, 2020) [int]
outpath: path where to save {var}_eff.tif [string]
WARNING: SZA.tif, OZA.tif, SAA.tif and rBRR_{band_num}.tif are needed
in each scene folder for the algorithm to run.
OUTPUTS:
for a given scene:
slope.tif: tiff file containing the slope [.tif]
slope_flag.tif: tiff file containing slope_flag based on the "small slope
approximation" (Picard et al, 2020) [.tif]
aspect.tif: tiff file containing the effective angles [.tif]
SZA_eff.tif: tiff file containing the effective solar zenith angle [.tif]
OZA_eff.tif: tiff file containing the viewing solar zenith angle [.tif]
IBOAR_{band_num}.tif: tiff file containing the effective angles for each
band_num [.tif]
"""
import numpy as np
import sys
import rasterio
import glob
import os
import argparse
from osgeo import gdal, gdalconst
import time
run_command_line=False
time_it=True
verbose=True
if time_it:
start_time = time.time()
if run_command_line:
parser = argparse.ArgumentParser()
parser.add_argument('inpath')
parser.add_argument('inpath_dem')
args = parser.parse_args()
if not run_command_line:
var='SZA'
inpath='/srv/home/8675309/AW/20190802/'
inpath_adem='/srv/home/8675309/AW/'
region='Greenland'
slope_thres=15
outpath='/srv/home/8675309/AW/'
def get_effective_angles(var=var,inpath=inpath,inpath_adem=inpath_adem,region=region,
slope_thres=slope_thres,outpath=outpath,verbose=verbose):
'''
Determines effective solar/viewing angles to compute the Intrinsic BOA Reflectance (IBOAR).
INPUTS:
var: name of the variable to compute [string]
inpath: path to the folder containing the variables ({var} and SAA needed) [string]
inpath_adem: path to the folder containing regional ArcticDEM derived
slopes and aspects [string]
region: region over which the toolchain is run [string]
slope_thres: slope threshold in degrees to create slope_flag. Default
is set to 15° based on the "small slope approximation"
(Picard et al, 2020). 1 for slope<=slope_thres,
255 (no data) for slope>slope_thres [int]
outpath: path where to save {var}_eff.tif [string]
OUTPUTS:
{var}_eff: effective angles [array]
slope_flag: slope mask based on the "small slope approximation"
(Picard et al, 2020). 1 for slope<=threshold,
255 (no data) for slope>threshold [array]
{var}_eff.tif: tiff file containing the effective angles [.tif]
if variable is set to SZA:
slope.tif: tiff file containing the slope [.tif]
slope_flag.tif: tiff file containing the slope_flag [.tif]
aspect.tif: tiff file containing the slope aspect [.tif]
'''
#loading variables
try:
angle_name=var+'.tif'
angle=rasterio.open(inpath+var+'.tif').read(1)
except:
if verbose:
print('ERROR: %s is missing' %angle_name)
return
try:
saa=rasterio.open(inpath+'SAA.tif').read(1)
except:
if verbose:
print('ERROR: SAA.tif is missing')
return
def resample_clip_adem(var,inpath=inpath,inpath_adem=inpath_adem,outpath=outpath,reg=region):
'''
Resamples and clips ArcticDEM derived slopes and aspects to match S3Snow
outputs.
INPUTS:
var: name of the variable to compute ("slope" or "aspect") [string]
inpath: path to the folder containing the variables (var, height and saa needed) [string]
inpath_adem: path to the folder containing regional ArcticDEM derived
slopes and aspects [string]
outpath: path where to save slope.tif and aspect.tif [string]
OUTPUTS:
if var is set to slope:
{outpath}/slope.tif: Resampled and clipped ArcticDEM derived slopes [.tif]
if var is set to slope:
{outpath}/aspect.tif: Resampled and clipped ArcticDEM derived aspects [.tif]
'''
#source
src_filename = inpath_adem+reg+'_arcticdem_'+var+'.tif'
src = gdal.Open(src_filename, gdalconst.GA_ReadOnly)
src_proj = src.GetProjection()
src_geotrans = src.GetGeoTransform()
#raster to match
match_filename = inpath+angle_name
match_ds = gdal.Open(match_filename, gdalconst.GA_ReadOnly)
match_proj = match_ds.GetProjection()
match_geotrans = match_ds.GetGeoTransform()
wide = match_ds.RasterXSize
high = match_ds.RasterYSize
#output/destination
dst_filename = inpath+var+'.tif'
dst = gdal.GetDriverByName('Gtiff').Create(dst_filename, wide, high, 1, gdalconst.GDT_Float32)
dst.SetGeoTransform( match_geotrans )
dst.SetProjection( match_proj)
#run
gdal.ReprojectImage(src, dst, src_proj, match_proj, gdalconst.GRA_NearestNeighbour)
#running resample_clip_adem()
resample_clip_adem(var='slope')
resample_clip_adem(var='aspect')
#loading slope and aspect
slope=rasterio.open(inpath+'slope.tif').read(1)
aspect=rasterio.open(inpath+'aspect.tif').read(1)
#creating a flag based on the "small slope approximation"
slope_flag=slope.copy()
slope_flag[np.where(slope<=slope_thres)]=1
slope_flag[np.where(slope>slope_thres)]=255
#converting slope, aspect, angle and saa to radians
angle_rad = np.deg2rad(angle)
saa_rad = np.deg2rad(saa)
slope_rad = np.deg2rad(slope)
aspect_rad = np.deg2rad(aspect)
#calculating effective angle
mu = np.cos(angle_rad) * np.cos(slope_rad) + np.sin(angle_rad) * np.sin(slope_rad) * \
np.cos(saa_rad - aspect_rad)
eff = np.arccos(mu)
angle_eff=np.rad2deg(eff)
#loading initial metadata to save the output
profile=rasterio.open(inpath+var+'.tif','r').profile
angle_eff=np.nan_to_num(angle_eff) #nan no data don't pass
profile.update(nodata=0)
#writing the output
output_filename=outpath+var+'_eff'+'.tif'
with rasterio.open(output_filename, 'w', **profile) as dst:
dst.write(angle_eff, 1)
#writing slope_flag
profile.update(nodata=255)
slope_flag_filename=outpath+'slope_flag_'+str(slope_thres)+'_degrees.tif'
with rasterio.open(slope_flag_filename, 'w', **profile) as dst:
dst.write(slope_flag, 1)
#returning slope, aspect and slope flag only for SZA (only once)
if var=='SZA':
return angle_eff, slope, aspect,slope_flag
if var=='OZA':
return angle_eff
def get_IBOAR(slope,aspect,slope_flag,inpath=inpath,outpath=outpath,verbose=verbose):
'''
Determines the Intrinsic Bottom of Atmosphere Reflectance (IBOAR) for given bands
as inputs of Alexander Kokhanovsky's algorithm.
INPUTS:
slope: slope raster [array]
aspect: aspect of the slope raster [array]
slope_flag: slope mask based on the "small slope approximation" (Picard et al, 2020).
1 for slope<=threshold, 255 (no data) for slope>threshold [array]
inpath: path to the folder containing the variables (rBRR and SAA needed) [string]
outpath: path where to save R_slope_{band}.tif [string]
verbose: set to True to print details about processing [boolean]
OUTPUTS:
IBOAR_{band_num}.tif: tiff file containing the effective angles for each
band_num [.tif]
'''
#loading solar and viewing zenith angles (flat)
sza=rasterio.open(inpath+'SZA.tif').read(1)
oza=rasterio.open(inpath+'OZA.tif').read(1)
#listing available BRR bands
BRRs_paths=list(np.sort(glob.glob(inpath+'rBRR*')))
if len(BRRs_paths)==0:
if verbose:
print('ERROR: no rBRR_XX.tif files')
return
#loading solar azimuth angle (flat)
saa_io=rasterio.open(inpath+'SAA.tif')
profile=saa_io.profile #saving profile as base for IBOAR file
saa=saa_io.read(1)
#computing IBOAR for each available band
for i,brr in enumerate(BRRs_paths):
#loading BOAR (flat)
boar=rasterio.open(brr).read(1)
#computing iboar
mu0=np.cos(np.deg2rad(sza))
mu=np.cos(np.deg2rad(oza))
mu0_ov=mu0*np.cos(np.deg2rad(slope))+np.sin(np.deg2rad(sza))\
*np.sin(np.deg2rad(slope))*np.cos(np.deg2rad(saa)-np.deg2rad(aspect))
iboar=boar*mu0/mu0_ov
#masking iboar with slope_flag
iboar[slope_flag==255]=255
#saving band number
band_num=BRRs_paths[i].split(os.sep)[-1].split('.')[0][-2:]
#writing IBOAR_{band_num} in a tif
profile.update(nodata=255)
with rasterio.open(outpath+'IBOAR_'+band_num+'.tif', 'w', **profile) as dst:
dst.write(iboar, 1)
#running the routine for the desired scene
if run_command_line==True:
#running get_effective_angles() for SZA and OZA
try:
SZA_eff, slope, aspect,slope_flag=get_effective_angles('SZA',inpath=args.inpath)
except:
if verbose:
print('ERROR: get_effective_angles() did not completed, see above')
sys.exit()
try:
OZA_eff=get_effective_angles('OZA',inpath=args.inpath)
except:
if verbose:
print('ERROR: get_effective_angles() did not completed, see above')
sys.exit()
#running get_IBOAR for IBOAR at a given band
try:
get_IBOAR(slope,aspect,slope_flag,inpath=args.inpath)
except:
if verbose:
print('ERROR: get_IBOAR() did not completed, see above')
sys.exit()
if not run_command_line:
err=False
#running get_effective_angles() for SZA and OZA
try:
SZA_eff, slope, aspect, slope_flag=get_effective_angles('SZA')
except:
if verbose:
print('ERROR: get_effective_angles() did not completed, see above')
err=True
if not err:
try:
OZA_eff=get_effective_angles('OZA')
except:
if verbose:
print('ERROR: get_effective_angles() did not completed, see above')
if not err:
#running get_IBOAR for IBOAR at a given band
try:
get_IBOAR(slope,aspect,slope_flag)
except:
print('ERROR: get_IBOAR() did not completed, see above')
if time_it:
end_time = time.time()
processing_time=(end_time - start_time)/60
if verbose:
print('--- Processing time: %.3f minutes ---' %processing_time)