-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathB3.daily_classify_from_DAAC_MOD04_3K_hdf_MP.py
446 lines (369 loc) · 24.8 KB
/
B3.daily_classify_from_DAAC_MOD04_3K_hdf_MP.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
#############################################################
'''
import numpy as np
import pandas as pd
import os
import _MODIS_AOD_utilities
import _Python_utilities
from datetime import datetime
#########################################
from multiprocessing import Process, Queue
class Multiprocessor():
def __init__(self):
self.processes = []
self.queue = Queue()
@staticmethod
def _wrapper(func, queue, args, kwargs):
ret = func(*args, **kwargs)
queue.put(ret)
def restart(self):
self.processes = []
self.queue = Queue()
def run(self, func, *args, **kwargs):
args2 = [func, self.queue, args, kwargs]
p = Process(target=self._wrapper, args=args2)
self.processes.append(p)
p.start()
def wait(self):
rets = []
for p in self.processes:
ret = self.queue.get()
rets.append(ret)
for p in self.processes:
p.join()
return rets
#########################################
log_dir = "logs/"
log_file = "{}{}.log".format(log_dir, os.path.basename(__file__)[:-3])
err_log_file = "{}{}_err.log".format(log_dir, os.path.basename(__file__)[:-3])
print("log_file: {}".format(log_file))
print("err_log_file: {}".format(err_log_file))
#########################################
# Set variables
#########################################
# base_dr = "../Aerosol/MODIS Terra C6.1 - Aerosol 5-Min L2 Swath 3km/"
# Dataset_DOI = "http://dx.doi.org/10.5067/MODIS/MOD04_L2.006"
# base_dr = "../Aerosol/MODIS Aqua C6.1 - Aerosol 5-Min L2 Swath 3km/"
# Dataset_DOI = "http://dx.doi.org/10.5067/MODIS/MYD04_L2.006"
base_drs = ["../Aerosol/MODIS Aqua C6.1 - Aerosol 5-Min L2 Swath 3km/",
"../Aerosol/MODIS Terra C6.1 - Aerosol 5-Min L2 Swath 3km/"]
base_drs = ["../Aerosol/MODIS Aqua C6.1 - Aerosol 5-Min L2 Swath 10km/",
"../Aerosol/MODIS Terra C6.1 - Aerosol 5-Min L2 Swath 10km/"]
# base_drs = ["../Aerosol/MODIS Aqua C6.1 - Aerosol 5-Min L2 Swath 3km/2016/"]
# Set Datafield name
DATAFIELD_NAME = "Optical_Depth_Land_And_Ocean"
resolution = 0.01
# Set lon, lat, resolution
Llon, Rlon, Slat, Nlat = 110, 150, 10, 60
save_dr = "../L3_{0}/{0}_{1}_{2}_{3}_{4}_{5}_{6}/".format(DATAFIELD_NAME, str(Llon), str(Rlon),
str(Slat), str(Nlat), str(resolution), "date_10K")
#########################################
if not os.path.exists(save_dr):
os.makedirs(save_dr)
print('*' * 80)
print("{} is created...".format(save_dr))
else:
print('*' * 80)
print("{} is already exist...".format(save_dr))
#########################################
# single thread class
#########################################
class Classifier():
def __init__(self, proc_date):
self.proc_date = proc_date
# def fetch(self):
print("Starting process data in {0} - {1} ...\n" \
.format(self.proc_date[0].strftime('%Y%m%d'), self.proc_date[1].strftime('%Y%m%d')))
self.df_proc = df[(df['fullname_dt'] >= self.proc_date[0]) & (df['fullname_dt'] < self.proc_date[1])]
# check file exist??
if (os.path.exists('{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_alldata.npy' \
.format(save_dr, DATAFIELD_NAME, self.proc_date[0].strftime('%Y%m%d'),
self.proc_date[1].strftime('%Y%m%d'),
str(Llon), str(Rlon), str(Slat), str(Nlat), str(resolution))) \
and os.path.exists('{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_info.csv' \
.format(save_dr, DATAFIELD_NAME, self.proc_date[0].strftime('%Y%m%d'),
self.proc_date[1].strftime('%Y%m%d'),
str(Llon), str(Rlon), str(Slat), str(Nlat), str(resolution)))):
print(('{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8} files are exist...'
.format(save_dr, DATAFIELD_NAME, self.proc_date[0].strftime('%Y%m%d'),
self.proc_date[1].strftime('%Y%m%d'),
str(Llon), str(Rlon), str(Slat), str(Nlat), str(resolution))))
else:
if len(self.df_proc) == 0:
print("There is no data in {0} - {1} ...\n" \
.format(self.proc_date[0].strftime('%Y%m%d'), self.proc_date[1].strftime('%Y%m%d')))
else:
print("self.df_proc: {}".format(self.df_proc))
self.processing_log = "#This file is created using Python : https://github.com/guitar79/MODIS_AOD_Python\n"
self.processing_log += "#start date = {}, end date = {}\n" \
.format(self.proc_date[0].strftime('%Y%m%d'), self.proc_date[1].strftime('%Y%m%d'))
self.processing_log += "#Llon = {}, Rlon = {}, Slat = {}, Nlat = {}, resolution = {}\n" \
.format(str(Llon), str(Rlon), str(Slat), str(Nlat), str(resolution))
# make array_data
print("{0}-{1} Start making grid arrays...\n". \
format(self.proc_date[0].strftime('%Y%m%d'), self.proc_date[1].strftime('%Y%m%d')))
array_data = _MODIS_AOD_utilities.make_grid_array(Llon, Rlon, Slat, Nlat, resolution)
print('Grid arrays are created...........\n')
self.total_data_cnt = 0
self.file_no = 0
self.processing_log += "#processing file Num : {}\n".format(len(self.df_proc["fullname"]))
self.processing_log += "#processing file list\n"
self.processing_log += "#file No, total_data_dount, data_count, filename, mean(sst), max(sst), min(sst), min(self.longitude), max(self.longitude), min(self.latitude), max(self.latitude)\n"
self.array_alldata = array_data.copy()
print('self.array_alldata is copied...........\n')
for self.fullname in self.df_proc["fullname"]:
# self.fullname = self.df_proc["fullname"][0]
self.file_no += 1
self.fullname_el = self.fullname.split("/")
print("Reading hdf file {0}/{1} : {2}\n".format(self.file_no, len(self.df_proc["fullname"]),
self.fullname))
try:
self.hdf_raw, self.latitude, self.longitude, self.cntl_pt_cols, self.cntl_pt_rows \
= _MODIS_AOD_utilities.read_MODIS_hdf_to_ndarray(self.fullname, DATAFIELD_NAME)
self.hdf_value = self.hdf_raw[:, :]
if 'bad_value_scaled' in self.hdf_raw.attributes():
# self.hdf_value[self.hdf_value == self.hdf_raw.attributes()['bad_value_scaled']] = np.nan
self.hdf_value = np.where(self.hdf_value == self.hdf_raw.attributes()['bad_value_scaled'],
np.nan,
self.hdf_value)
# print("'bad_value_scaled' data is changed to np.nan...\n")
elif 'fill_value' in self.hdf_raw.attributes():
# self.hdf_value[self.hdf_value == self.hdf_raw.attributes()['fill_value']] = np.nan
self.hdf_value = np.where(self.hdf_value == self.hdf_raw.attributes()['fill_value'], np.nan,
self.hdf_value)
# print("'fill_value' data is changed to np.nan...\n")
elif '_FillValue' in self.hdf_raw.attributes():
# self.hdf_value[self.hdf_value == self.hdf_raw.attributes()['_FillValue']] = np.nan
self.hdf_value = np.where(self.hdf_value == self.hdf_raw.attributes()['_FillValue'], np.nan,
self.hdf_value)
# print("'_FillValue' data is changed to np.nan...\n")\
else:
self.hdf_value = np.where(self.hdf_value == -32767, np.nan, self.hdf_value)
# print("-32767 value of hdf data is changed to np.nan ...\n")
if 'valid_range' in self.hdf_raw.attributes():
self.hdf_value = np.where(self.hdf_value < self.hdf_raw.attributes()['valid_range'][0],
np.nan, self.hdf_value)
self.hdf_value = np.where(self.hdf_value > self.hdf_raw.attributes()['valid_range'][1],
np.nan, self.hdf_value)
# print("invalid_range data changed to np.nan...\n")
if 'scale_factor' in self.hdf_raw.attributes() and 'add_offset' in self.hdf_raw.attributes():
self.scale_factor = self.hdf_raw.attributes()['scale_factor']
self.offset = self.hdf_raw.attributes()['add_offset']
elif 'slope' in self.hdf_raw.attributes() and 'intercept' in self.hdf_raw.attributes():
self.scale_factor = self.hdf_raw.attributes()['slope']
self.offset = self.hdf_raw.attributes()['intercept']
else:
self.scale_factor, self.offset = 1, 0
self.hdf_value = np.asarray(self.hdf_value)
self.hdf_value = self.hdf_value * self.scale_factor + self.offset
# print("self.latitude: {}".format(self.latitude))
# print("self.longitude: {}".format(self.longitude))
### print("self.hdf_value: {}".format(self.hdf_value))
### print("str(self.hdf_raw.attributes()): {}".format(str(self.hdf_raw.attributes())))
### print("np.shape(self.latitude): {}".format(np.shape(self.latitude)))
### print("np.shape(self.longitude): {}".format(np.shape(self.longitude)))
### print("np.shape(self.hdf_value): {}".format(np.shape(self.hdf_value)))
### print("len(self.cntl_pt_cols): {}".format(len(self.cntl_pt_cols)))
### print("len(self.cntl_pt_rows): {}".format(len(self.cntl_pt_rows)))
# check self.latitude and self.longitude
if np.shape(self.latitude) == np.shape(self.longitude):
if np.shape(self.longitude)[0] != np.shape(self.hdf_value)[0]:
print("np.shape(self.longitude)[0] != np.shape(self.hdf_value)[0] is true...")
row = 0
self.longitude_new = np.empty(shape=(np.shape(self.hdf_value)))
for row in range(len(self.longitude[0])):
for i in range(len(self.cntl_pt_rows) - 1):
self.longitude_value = np.linspace(self.longitude[row, i],
self.longitude[row, i + 1],
self.cntl_pt_rows[i])
for j in range(i):
self.longitude_new[row, row + j] = self.longitude_value[j]
# print("np.shape(self.longitude_new): {}".format(np.shape(self.longitude_new)))
self.longitude = self.longitude_new.copy()
elif np.shape(self.longitude)[1] != np.shape(self.hdf_value)[1]:
print("np.shape(self.longitude)[1] != np.shape(self.hdf_value)[1] is true...")
col = 0
self.longitude_new = np.empty(shape=(np.shape(self.hdf_value)))
for row in range(len(self.longitude[1])):
for i in range(len(self.cntl_pt_cols) - 1):
self.longitude_value = np.linspace(self.longitude[row, i], \
self.longitude[row, i + 1], \
self.cntl_pt_cols[i + 1] - self.cntl_pt_cols[
i] + 1)
for j in range(len(self.longitude_value) - 1):
self.longitude_new[row, self.cntl_pt_cols[i] - 1 + j] = \
self.longitude_value[j]
self.longitude_new[row, np.shape(self.longitude_new)[1] - 1] = self.longitude[
row, np.shape(self.longitude)[1] - 1]
# print("np.shape(self.longitude_new): {}".format(np.shape(self.longitude_new)))
self.longitude = self.longitude_new.copy()
self.longitude = np.asarray(self.longitude)
# print("type(self.longitude): {}".format(type(self.longitude)))
print("np.shape(self.longitude): {}".format(np.shape(self.longitude)))
if np.shape(self.latitude)[0] != np.shape(self.hdf_value)[0]:
print("np.shape(self.latitude)[0] != np.shape(self.hdf_value)[0] is not same...")
row = 0
self.latitude_new = np.empty(shape=(np.shape(self.hdf_value)))
for row in range(len(self.latitude[0])):
for i in range(len(self.cntl_pt_rows) - 1):
self.latitude_value = np.linspace(self.latitude[row, i],
self.latitude[row, i + 1],
self.cntl_pt_rows[i])
for j in range(i):
self.latitude_new[row, row + j] = self.latitude_value[j]
print("np.shape(self.latitude_new): {}".format(np.shape(self.latitude_new)))
self.latitude = self.latitude_new.copy()
elif np.shape(self.latitude)[1] != np.shape(self.hdf_value)[1]:
print("np.shape(self.latitude)[1] != np.shape(self.hdf_value)[1] is true...")
col = 0
self.latitude_new = np.empty(shape=(np.shape(self.hdf_value)))
for row in range(len(self.latitude[1])):
for i in range(len(self.cntl_pt_cols) - 1):
self.latitude_value = np.linspace(self.latitude[row, i], \
self.latitude[row, i + 1], \
self.cntl_pt_cols[i + 1] - self.cntl_pt_cols[
i] + 1)
for j in range(len(self.latitude_value) - 1):
self.latitude_new[row, self.cntl_pt_cols[i] - 1 + j] = self.latitude_value[
j]
self.latitude_new[row, np.shape(self.latitude_new)[1] - 1] = self.latitude[
row, np.shape(self.latitude)[1] - 1]
print("np.shape(self.latitude_new): {}".format(np.shape(self.latitude_new)))
self.latitude = self.latitude_new.copy()
self.latitude = np.asarray(self.latitude)
# print("type(self.latitude): {}".format(type(self.latitude)))
print("np.shape(self.latitude): {}".format(np.shape(self.latitude)))
# check dimension
if not (np.shape(self.latitude) == np.shape(self.hdf_value) \
and np.shape(self.longitude) == np.shape(self.hdf_value)):
print(
"(np.shape(self.latitude) == np.shape(self.hdf_value) and np.shape(self.longitude == np.shape(self.hdf_value)) is not true...")
else:
self.longitude = np.where(self.longitude < Llon, np.nan, self.longitude)
self.longitude = np.where(self.longitude > Rlon, np.nan, self.longitude)
self.latitude = np.where(self.latitude > Nlat, np.nan, self.latitude)
self.latitude = np.where(self.latitude < Slat, np.nan, self.latitude)
self.lon_cood = np.array((self.longitude - Llon) / resolution)
self.lat_cood = np.array((Nlat - self.latitude) / resolution)
# print("self.longitude: {}".format(self.longitude))
### print("np.shape(self.lon_cood): {}".format(np.shape(self.lon_cood)))
# print("self.lon_cood: {}".format(self.lon_cood))
# print("self.latitude: {}".format(self.latitude))
### print("np.shape(self.lat_cood): {}".format(np.shape(self.lat_cood)))
# print("self.lat_cood: {}".format(self.lat_cood))
### print("self.hdf_value: {}".format(self.hdf_value))
if np.isnan(self.hdf_value).all():
self.processing_log += "{0}, 0, 0, {1}, \n" \
.format(str(self.file_no), str(self.filenameel[-1]))
print("There is no hdf data...")
# print("(np.isnan(self.hdf_value).all()) is true...")
else:
self.data_cnt = 0
self.NaN_cnt = 0
for i in range(np.shape(self.lon_cood)[0]):
for j in range(np.shape(self.lon_cood)[1]):
if (not np.isnan(self.longitude[i, j])) and (not np.isnan(self.latitude[i, j])) \
and (not np.isnan(self.hdf_value[i][j])):
self.data_cnt += 1
# self.array_alldata[int(self.lon_cood[i][j])][int(self.lat_cood[i][j])].append(self.hdf_value[i][j])
self.array_alldata[int(self.lon_cood[i][j])][int(self.lat_cood[i][j])].append(
(self.fullname_el[-1], self.hdf_value[i][j]))
### print("self.array_alldata[{}][{}].append({}, {})" \
### .format(int(self.lon_cood[i][j]), int(self.lat_cood[i][j]), self.fullname_el[-1],
### self.hdf_value[i][j]))
# print("{} data added...".format(self.data_cnt))
self.total_data_cnt += self.data_cnt
self.Wlon1, self.Elon1, self.Slat1, self.Nlat1, self.Clon1, self.Clat1 = _MODIS_AOD_utilities.findRangeOfMap(
self.longitude,
self.latitude)
self.processing_log += "{0}, {1}, {2}, {3}, {4:.02f}, {5:.02f}, {6:.02f}, {7:.02f}, {8:.02f}, {9:.02f}, {10:.02f}, {11}\n" \
.format(str(self.file_no), str(self.total_data_cnt), str(self.data_cnt),
str(self.fullname_el[-1]),
np.nanmean(self.hdf_value), np.nanmax(self.hdf_value),
np.nanmin(self.hdf_value),
self.Wlon1, self.Elon1, self.Slat1, self.Nlat1, str(self.hdf_raw.attributes()))
except Exception as err:
# _Python_utilities.write_log(err_log_file, err)
print(err)
continue
self.processing_log += "#processing finished!!!\n"
# print("self.array_alldata: {}".format(self.array_alldata))
print("self.prodessing_log: {}".format(self.processing_log))
self.array_alldata = np.array(self.array_alldata)
print("self.array_alldata: \n{}".format(self.array_alldata))
print("self.array_alldata.shape: {}".format(self.array_alldata.shape))
if self.array_alldata.size == 0:
print("self.array_alldata.size == 0")
else:
np.save('{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_alldata.npy' \
.format(save_dr, DATAFIELD_NAME,
self.proc_date[0].strftime('%Y%m%d'), self.proc_date[1].strftime('%Y%m%d'),
str(Llon), str(Rlon), str(Slat), str(Nlat), str(resolution)), self.array_alldata)
_Python_utilities.write_log(log_file,
'{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_alldata.npy is created...' \
.format(save_dr, DATAFIELD_NAME,
self.proc_date[0].strftime('%Y%m%d'),
self.proc_date[1].strftime('%Y%m%d'),
str(Llon), str(Rlon), str(Slat), str(Nlat), str(resolution)))
with open('{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_info.csv' \
.format(save_dr, DATAFIELD_NAME,
self.proc_date[0].strftime('%Y%m%d'),
self.proc_date[1].strftime('%Y%m%d'),
str(Llon), str(Rlon), str(Slat), str(Nlat), str(resolution)), 'w') as f:
f.write(self.processing_log)
print('#' * 60)
_Python_utilities.write_log(log_file,
'{0}{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8} files are is created.\n' \
.format(save_dr, DATAFIELD_NAME,
self.proc_date[0].strftime('%Y%m%d'),
self.proc_date[1].strftime('%Y%m%d'),
str(Llon), str(Rlon), str(Slat), str(Nlat), str(resolution)))
fullnames = []
for dirName in base_drs:
# dirName = "../Aerosol/MODIS Aqua C6.1 - Aerosol 5-Min L2 Swath 3km/2002/185/"
try:
fullnames.extend(_Python_utilities.getFullnameListOfallFiles("{}".format(dirName)))
except Exception as err:
# _Python_utilities.write_log(err_log_file, err)
print(err)
continue
fullnames = sorted(fullnames)
df = pd.DataFrame({'fullname': fullnames})
df = df[df.fullname.str.contains(".hdf")]
for idx, row in df.iterrows():
print(row["fullname"])
df.at[idx, "fullname_dt"] = _MODIS_AOD_utilities.fullname_to_datetime_for_DAAC3K(df.loc[idx, "fullname"])
df.index = df['fullname_dt']
print("make datetime column in df:\n{}".format(df))
#########################################
# make processing period tuple
#########################################
proc_dates = []
# make processing period tuple
from dateutil.relativedelta import relativedelta
set_S_datetime = datetime(2001, 1, 1) # convert startdate to date type
set_E_datetime = datetime(2022, 1, 1)
date1 = set_S_datetime
date2 = set_S_datetime
while date2 < set_E_datetime:
date2 = date1 + relativedelta(days=1)
dates = (date1, date2)
proc_dates.append(dates)
date1 = date2
print("len(proc_dates): {}".format(len(proc_dates)))
#########################################
myMP = Multiprocessor()
num_cpu = 8
values = []
num_batches = len(proc_dates) // num_cpu + 1
for batch in range(num_batches):
myMP.restart()
for proc_date in proc_dates[batch * num_batches:(batch + 1) * num_batches]:
myMP.run(Classifier, proc_date)
print("Batch " + str(batch))
myMP.wait()
values.append(myMP.wait())
print("OK batch" + str(batch))