-
Notifications
You must be signed in to change notification settings - Fork 1
/
carra2py.py
409 lines (267 loc) · 12.3 KB
/
carra2py.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 3 14:26:47 2023
@author: rabni
"""
import sys,os
from pyproj import CRS,Transformer
from scipy.spatial import KDTree
import xarray as xr
import numpy as np
from rasterio.transform import Affine
import rasterio
import urllib.request
import pandas as pd
import glob
import logging
import netCDF4 as nc
import time
import warnings
import datetime as dt
warnings.filterwarnings("ignore", category=FutureWarning)
if not os.path.exists("logs"):
os.makedirs("logs")
logging.basicConfig(
format='%(asctime)s [%(levelname)s] %(name)s - %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.FileHandler(f'logs/carra2py_{time.strftime("%Y_%m_%d",time.localtime())}.log'),
logging.StreamHandler()
])
def opentiff(filename):
"Input: Filename of GeoTIFF File "
"Output: xgrid,ygrid, data paramater of Tiff, the data projection"
da = xr.open_rasterio(filename)
proj = CRS.from_string(da.crs)
transform = Affine(*da.transform)
elevation = np.array(da.variable[0],dtype=np.float32)
nx,ny = da.sizes['x'],da.sizes['y']
x,y = np.meshgrid(np.arange(nx,dtype=np.float32), np.arange(ny,dtype=np.float32)) * transform
da.close()
return x,y,elevation,proj
def exporttiff(x,y,z,crs,path,filename):
"Input: xgrid,ygrid, data paramater, the data projection, export path, name of tif file"
resx = (x[0,1] - x[0,0])
resy = (y[1,0] - y[0,0])
transform = Affine.translation((x.ravel()[0]),(y.ravel()[0])) * Affine.scale(resx, resy)
if resx == 0:
resx = (x[0,0] - x[1,0])
resy = (y[0,0] - y[0,1])
transform = Affine.translation((y.ravel()[0]),(x.ravel()[0])) * Affine.scale(resx, resy)
with rasterio.open(
path + os.sep + filename,
'w',
driver='GTiff',
height=z.shape[0],
width=z.shape[1],
count=1,
dtype=z.dtype,
crs=crs,
transform=transform,
) as dst:
dst.write(z, 1)
dst.close()
return None
def reproject(raw_lon,raw_lat):
WGSProj = CRS.from_string("+init=EPSG:4326")
PolarProj = CRS.from_string("+init=EPSG:3413")
wgs_data = Transformer.from_proj(WGSProj, PolarProj)
xx, yy = wgs_data.transform(raw_lon,raw_lat)
return xx,yy
def blockPrint():
sys.stdout = open(os.devnull, 'w')
def enablePrint():
sys.stdout = sys.__stdout__
class AVHRR():
def __init__(self,date,block = None):
self.date = date
self.base_folder = os.getcwd()
self.block = block
#print(self.block)
#print(self.date)
#print(self.base_folder)
if self.block:
blockPrint()
else:
enablePrint()
def get_data(self,polar = None):
if not os.path.exists(self.base_folder + os.sep + 'rawdata'):
os.mkdir(self.base_folder + os.sep + 'rawdata')
data_folder = self.base_folder + os.sep + 'rawdata' + os.sep + self.date
if not os.path.exists(data_folder):
os.mkdir(data_folder)
os.chdir(data_folder)
base_url = 'https://www.ncei.noaa.gov/data/avhrr-polar-pathfinder-extended/access/nhem'
date_1 = dt.datetime.strptime(self.date, "%Y%m%d")
procdatesplus = [(date_1 + delta).strftime("%Y%m%d") for delta in \
[dt.timedelta(days=int(d)) for d in np.arange(0,10)]]
procdates = ['20190624','20190625','20190618'] + procdatesplus
check = 0
for d in procdates:
if check == 0:
try:
file = 'Polar-APP-X_v02r00_Nhem_0400_d' + self.date + '_c' + d + '.nc'
urllib.request.urlretrieve(base_url + "/" + self.date[:4] + "/" + file, file)
check = 1
except:
pass
if check == 0:
logging.info(f'Data service is unavailable for {self.date} , try another date or at another time')
return None
os.chdir(self.base_folder)
ncfile = nc.Dataset(data_folder + os.sep + file)
if polar is None:
raw_alb = np.array(ncfile["cdr_surface_albedo"])[0]
raw_cloud = np.array(ncfile["cdr_cloud_binary_mask"])[0]
raw_alb[raw_alb == 9999] = np.nan
#raw_cloud[raw_cloud == 9999] = np.nan
raw_lon = np.array(ncfile["longitude"])
raw_lat = np.array(ncfile["latitude"])
raw_alb[raw_cloud==1] = np.nan
ncfile.close()
if not any(~(np.isnan(raw_alb.ravel()))):
logging.info(f'No surface albedo data was available on {self.date}')
return None
return raw_lon,raw_lat,raw_alb
else:
raw_alb = np.array(ncfile["cdr_surface_albedo"])[0]
raw_cloud = np.array(ncfile["cdr_cloud_binary_mask"])[0]
raw_alb[raw_alb == 9999] = np.nan
#raw_cloud[raw_cloud == 9999] = np.nan
raw_alb[raw_cloud==1] = np.nan
raw_x,raw_y = reproject(np.array(ncfile["longitude"]),np.array(ncfile["latitude"]))
ncfile.close()
if not any(~(np.isnan(raw_alb.ravel()))):
logging.info(f'No surface albedo data was available on {self.date}')
return None
return raw_x,raw_y,raw_alb
def proc(self,raw_data = None, area = None,res = 2500):
if not raw_data:
raw_data = self.get_data(polar = 1)
if not raw_data:
return None
else:
xx = raw_data[0]
yy = raw_data[1]
albedo = raw_data[2]
else:
xx = raw_data[0]
yy = raw_data[1]
albedo = raw_data[2]
res_proc = [1000,2500,5000]
if res not in res_proc:
raise Exception('Specified resolution is not available. ' + \
'Please use one of these options ' + str(res_proc))
mask_list = glob.glob(self.base_folder + os.sep + 'masks' + os.sep + '*.csv')
grid_list = glob.glob(self.base_folder + os.sep + 'masks' + os.sep + '*' + str(res) + 'm.tif')
if area:
mask_list = [m for m in mask_list if m.split(os.sep)[-1].split('_')[0] in area]
grid_list = [g for g in grid_list if g.split(os.sep)[-1].split('_')[0] in area]
eps = (2 * 10**-5) # Shape Parameter
data = {}
for m,g in zip(mask_list,grid_list):
a = m.split(os.sep)[-1].split('_')[0]
logging.info(f'Processing for {a},{self.date}')
df = pd.read_csv(m)
min_x = int(df['MINX'])
max_x = int(df['MAXX'])
min_y = int(df['MINY'])
max_y = int(df['MAXY'])
bbmsk = (xx <= max_x) & (xx >= min_x)\
& (yy >= min_y) & (yy <= max_y)
xx_filt = xx[bbmsk]
yy_filt = yy[bbmsk]
alb_filt = albedo[bbmsk]
x_grid,y_grid,z_grid,gridproj = opentiff(g)
datagrid = np.ones_like(z_grid) * z_grid
tree = KDTree(np.transpose(np.array([xx_filt,yy_filt])))
for i,(xmid,ymid) in enumerate(zip(x_grid.ravel(),y_grid.ravel())):
dd, ii = tree.query([xmid,ymid],k = 20,p = 2)
dd = dd[~np.isnan(alb_filt.ravel()[ii])]
ii = ii[~np.isnan(alb_filt.ravel()[ii])]
if (len(ii) == 0) or (datagrid.ravel()[i] != 220):
datagrid.ravel()[i] = np.nan
else:
w = np.exp(-(eps * dd)**2)
datagrid.ravel()[i] = np.average(alb_filt.ravel()[ii], weights = w)
if not any(~(np.isnan(datagrid.ravel()))):
logging.info(f'No surface albedo data was available at {a},{self.date}')
else:
logging.info(f'Processing done for {a},{self.date}')
data[a] = {"x" : x_grid,\
"y" : y_grid,\
"albedo" : datagrid}
if len(data) == 0:
return None
else:
return data
def export_to_tif(self,output = None, path = 'default'):
if path == 'default':
pathoutput = self.base_folder + os.sep + "output"
if not os.path.exists(pathoutput):
os.mkdir(pathoutput)
path = self.base_folder + os.sep + "output" + os.sep + self.date
if not os.path.exists(path):
os.mkdir(path)
else:
logging.info(f'Output already existst, skipping {self.date}')
crs = CRS.from_string("+init=EPSG:3413")
if output is None:
output = self.proc()
if not output:
return
for a in output:
x = output[a]["x"]
y = output[a]["y"]
z = output[a]["albedo"]
res = int((x[0,1] - x[0,0]) + (x[0,0] - x[1,0]))
filename = self.date + "_" + a + "_" + str(res) + "m_AVHRR.tif"
exporttiff(x, y, z, crs, path, filename)
return
def export_to_csv(self,output = None, path = 'default'):
if output is None:
output = self.proc()
if not output:
return
if path == 'default':
path = self.base_folder + os.sep + "output" + os.sep + self.date
if not os.path.exists(path):
os.mkdir(path)
for a in output:
x = output[a]["x"]
y = output[a]["y"]
z = output[a]["albedo"]
res = int((x[0,1] - x[0,0]) + (x[0,0] - x[1,0]))
filename = self.date + "_" + a + "_" + str(res) + "m_AVHRR.csv"
df = pd.DataFrame({'x' : x.ravel(),\
'y' : y.ravel(),\
'albedo' : z.ravel()})
df.to_csv(path + os.sep + filename)
return
def export_to_nc(self,output = None, path = 'default'):
if output is None:
output = self.proc()
if not output:
return
if path == 'default':
path = self.base_folder + os.sep + "output" + os.sep + self.date
if not os.path.exists(path):
os.mkdir(path)
for a in output:
x = output[a]["x"]
y = output[a]["y"]
z = output[a]["albedo"]
res = int((x[0,1] - x[0,0]) + (x[0,0] - x[1,0]))
filename = self.date + "_" + a + "_" + str(res) + "m_AVHRR.nc"
ds = nc.Dataset(path + os.sep + filename, 'w', format='NETCDF4')
ds.createDimension('id1', np.shape(x)[0])
ds.createDimension('id2', np.shape(x)[1])
x_out = ds.createVariable('x', 'f4', ('id1', 'id2'), zlib=True)
y_out = ds.createVariable('y', 'f4', ('id1', 'id2'), zlib=True)
alb_out = ds.createVariable('albedo', 'f4', ('id1', 'id2'), zlib=True)
x_out[:,:] = x
y_out[:,:] = y
alb_out[:,:] = z
ds.close()
return