-
Notifications
You must be signed in to change notification settings - Fork 2
/
sat_check.py
264 lines (207 loc) · 10.3 KB
/
sat_check.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
import argparse
def argument():
parser = argparse.ArgumentParser(description = '''
Apply check based on climatology to sat ORIG files.
Produces CHECKED and REJECTED files for each date at satellite resolution.
Statistics provided if argument dirstats is setted.
''',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument( '--origdir', '-i',
type = str,
required = True,
help = ''' ORIG sat directory, e.g. /gss/gss_work/DRES_OGS_BiGe/Observations/TIME_RAW_DATA/ONLINE/SAT/MODIS/DAILY/ORIG/'''
)
parser.add_argument( '--checkdir', '-o',
type = str,
required = True,
help = ''' Base for CHECKED and REJECTED sat directory, e.g. /gss/gss_work/DRES_OGS_BiGe/Observations/TIME_RAW_DATA/ONLINE/SAT/MODIS/DAILY/'''
)
parser.add_argument( '--climfile', '-c',
type = str,
required = True,
help = ''' Climatology .nc file used to apply check on sat data, e.g. /gss/gss_work/DRES_OGS_BiGe/Observations/CLIMATOLOGY/SAT/CCI02/SatClimatology.nc'''
)
parser.add_argument( '--mesh', '-m',
type = str,
required = True,
choices = ['SatOrigMesh','V4mesh','V1mesh','KD490mesh','SAT1km_mesh', 'Mesh24'],
help = ''' Name of the mesh of sat ORIG and used to dump checked data.'''
)
parser.add_argument( '--submaskdir', '-s',
type = str,
required = True,
help = ''' Directory with subbasins on satellite mask'''
)
parser.add_argument( '--statsdir', '-w',
type = str,
required = False,
help = ''' STATS directory'''
)
return parser.parse_args()
args = argument()
from commons.time_interval import TimeInterval
from commons.Timelist import TimeList
from basins import V2
from postproc import masks
import numpy as np
from commons.utils import addsep
import os
import scipy.io.netcdf as NC
import SatManager as Sat
ORIGDIR = addsep(args.origdir)
SUBMASKDIR = addsep(args.submaskdir)
CHECKDIR = addsep(args.checkdir)
CLIM_FILE = args.climfile
if args.statsdir is not None:
STATSDIR = addsep(args.statsdir)
maskSat = getattr(masks,args.mesh)
reset = False
Timestart="19501231"
Time__end="20500101"
TI = TimeInterval(Timestart,Time__end,"%Y%m%d")
TL_orig = TimeList.fromfilenames(TI, ORIGDIR ,"*.nc",prefix='',dateformat='%Y%m%d')
somecheck = False
for iTime,filename in enumerate(TL_orig.filelist):
outfile = CHECKDIR + '/CHECKED/' + os.path.basename(filename)
rejfile = CHECKDIR + '/REJECTED/' + os.path.basename(filename)
iDate = TL_orig.Timelist[iTime]
date8 = iDate.strftime('%Y%m%d')
if (not os.path.exists(outfile)) or (not os.path.exists(rejfile)):
somecheck = True
break
if args.statsdir is not None:
for masktype in ['ORIG','CHECK']:
filestats = STATSDIR + os.path.basename(filename)[:-3] + '_stats' + masktype + '.nc'
if (not os.path.exists(filestats)):
somecheck = True
break
if somecheck:
break
if somecheck or reset:
print('Read climatology')
MEAN,STD = Sat.readClimatology(CLIM_FILE)
filemaskmed = SUBMASKDIR + 'maskmed_1kmAll.npy'
maskmed_1km = np.load(filemaskmed)
masksub_M = {}
nsub = 0
subnames = ''
for sub in V2.P:
if sub.name=='atl': continue
nsub += 1
subnames += sub.name + ', '
filemasksub = SUBMASKDIR + 'masksub.' + sub.name + 'All.npy'
masksub = np.load(filemasksub)
masksub_M[sub.name] = np.zeros((maskSat.jpj,maskSat.jpi),dtype=bool)
masksub_M[sub.name][maskmed_1km] = masksub
else:
print('All checks done')
for iTime, filename in enumerate(TL_orig.filelist):
outfile = CHECKDIR + '/CHECKED/' + os.path.basename(filename)
rejfile = CHECKDIR + '/REJECTED/' + os.path.basename(filename)
exit_condition = os.path.exists(outfile) and os.path.exists(rejfile) and (not reset)
exit_condmask = np.zeros(2,dtype=bool)
iDate = TL_orig.Timelist[iTime]
date8 = iDate.strftime('%Y%m%d')
if exit_condition:
if args.statsdir is None:
continue
else:
for imsk,masktype in enumerate(['ORIG','CHECK']):
filestats = STATSDIR + os.path.basename(filename)[:-3] + '_stats' + masktype + '.nc'
exit_condmask[imsk] = (os.path.exists(filestats)) and (not reset)
if all(exit_condmask):
continue
julian = int( iDate.strftime("%j") )
print(' ... day ' + np.str(julian) + ' of ' + np.str(iDate.year))
if julian == 366:
julian = 365
maskreject = np.zeros_like(maskmed_1km,dtype=int)
DAILY_REF_MEAN = MEAN[julian-1,:,:]
DAILY_REF_STD = STD[julian-1,:,:]
CHL_IN = Sat.readfromfile(filename)
if args.mesh == 'SatOrigMesh': CHL_IN[581:,164:] = Sat.fillValue # BLACK SEA
cloudsLandTIME = CHL_IN == Sat.fillValue
cloudlandsCLIM = DAILY_REF_MEAN == Sat.fillValue
CHL_OUT = CHL_IN.copy()
CHL_OUT[cloudsLandTIME] = Sat.fillValue
CHL_OUT[cloudlandsCLIM] = Sat.fillValue
counter_refNAN = (~cloudsLandTIME & cloudlandsCLIM).sum(axis=None)
maskreject[cloudsLandTIME] = -5
maskreject[~cloudsLandTIME & cloudlandsCLIM] = 2
outOfRange = np.abs(CHL_IN - DAILY_REF_MEAN) > DAILY_REF_STD *2.0
outOfRange[cloudsLandTIME | cloudlandsCLIM ] = False
counter_elim = outOfRange.sum(axis = None)
CHL_OUT[outOfRange] = Sat.fillValue
maskreject[outOfRange] = 1
if exit_condition==False:
print('Done check with ', filename, ' (',iTime+1,' of ', len(TL_orig.filelist), ')')
print('Rejection: after check', counter_elim, ' values')
print('rejected for NAN in Climatology', counter_refNAN, ' values')
Sat.dumpGenericNativefile(outfile, CHL_OUT, "CHL",mesh=maskSat)
Sat.dumpGenericNativefile(rejfile, maskreject, "RejInd",mesh=maskSat)
if (args.statsdir is not None) and (not all(exit_condmask)):
masksubday = {}
for sub in V2.P:
if sub.name=='atl': continue
masksubday[sub.name] = {}
masksubday[sub.name]['ORIG'] = masksub_M[sub.name] & (cloudsLandTIME == False)
masksubday[sub.name]['CHECK'] = masksub_M[sub.name] & (cloudsLandTIME == False) & (maskreject == 0)
for masktype in ['ORIG','CHECK']:
filestats = STATSDIR + os.path.basename(filename)[:-3] + '_stats' + masktype + '.nc'
stats_clima = np.zeros((nsub,12))
stats_clima[:,:] = np.nan
stats_day = np.zeros((nsub,8))
stats_day[:,:] = np.nan
print(masktype + ' --- Cycle on sub ---')
for isub,sub in enumerate(V2.P):
if sub.name=='atl': continue
climadmean = DAILY_REF_MEAN[masksubday[sub.name][masktype]]
climadmean[climadmean<0] = np.nan
climadstd = DAILY_REF_STD[masksubday[sub.name][masktype]]
climadstd[np.isnan(climadmean)] = np.nan
climadmadd_2std = climadmean + 2*climadstd
climadmsub_2std = climadmean - 2*climadstd
chlsub = CHL_IN[masksubday[sub.name][masktype]]
chlsub[chlsub<0] = np.nan
if np.nansum(masksubday[sub.name][masktype])>0:
#print(sub.name + ' ... Np ' + np.str(np.nansum(masksubday)))
stats_clima[isub,0] = np.nanmean(climadmean)
stats_clima[isub,1] = np.nanmin(climadmean)
stats_clima[isub,2] = np.nanmax(climadmean)
stats_clima[isub,3] = np.nanmean(climadmadd_2std)
stats_clima[isub,4] = np.nanmin(climadmadd_2std)
stats_clima[isub,5] = np.nanmax(climadmadd_2std)
stats_clima[isub,6] = np.nanmean(climadmsub_2std)
stats_clima[isub,7] = np.nanmin(climadmsub_2std)
stats_clima[isub,8] = np.nanmax(climadmsub_2std)
stats_clima[isub,9] = np.nanmean(climadstd)
stats_clima[isub,10] = np.nanmin(climadstd)
stats_clima[isub,11] = np.nanmax(climadstd)
stats_day[isub,0] = np.nanmean(chlsub)
stats_day[isub,1] = np.nanmin(chlsub)
stats_day[isub,2] = np.nanmax(chlsub)
stats_day[isub,3] = np.nanmean(np.abs(chlsub-climadmean)/climadstd)
stats_day[isub,4] = np.nanmean(chlsub/climadmean)
stats_day[isub,5] = np.nansum(masksubday[sub.name]['ORIG'])
stats_day[isub,6] = np.nansum(outOfRange[masksubday[sub.name]['ORIG']]) #maskreject=1
stats_day[isub,7] = np.nansum(~cloudsLandTIME & cloudlandsCLIM & \
masksubday[sub.name]['ORIG']) #maskreject=2
ncOUT = NC.netcdf_file(filestats,'w')
ncOUT.createDimension('subbasin',nsub)
ncOUT.createDimension('stattype_clim',12)
ncOUT.createDimension('stattype_day',8)
setattr(ncOUT,'sublist',subnames)
ncvar = ncOUT.createVariable('SUBstatistics_clim','f',('subbasin','stattype_clim'))
ncvar[:] = stats_clima
ncvar = ncOUT.createVariable('SUBstatistics_day','f',('subbasin','stattype_day'))
ncvar[:] = stats_day
ncOUT.close()
# fid = open(fileclim,'wb')
# pickle.dump(stats_clima,fid)
# fid.close()
# fid = open(filechlsub,'wb')
# pickle.dump(stats_day,fid)
# fid.close()
print('Done statistics with ', filename, ' (',iTime+1,' of ', len(TL_orig.filelist), ')')
print(' --------------------------------------------------- ')