-
Notifications
You must be signed in to change notification settings - Fork 0
/
viirs_cloud_products.py
321 lines (271 loc) · 12.7 KB
/
viirs_cloud_products.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
#!/usr/bin/env python
# encoding: utf-8
"""
viirs_cloud_products.py
Purpose: Provide ingest functionality for VIIRS cloud IP and EDR.
Created by Geoff Cureton on 2012-11-13.
Copyright (c) 2012 University of Wisconsin SSEC. All rights reserved.
"""
file_Date = '$Date$'
file_Revision = '$Revision$'
file_Author = '$Author$'
file_HeadURL = '$HeadURL$'
file_Id = '$Id$'
__author__ = 'G.P. Cureton <[email protected]>'
__version__ = '$Id$'
__docformat__ = 'Epytext'
### System libraries
import time, string, sys
from glob import glob
from os import path,uname
import copy
import numpy as np
from numpy import ma as ma
import tables as pytables
from tables import exceptions as pyEx
### Local libraries
import viirs_edr_data as VD
class viirsCld:
def __init__(self):
'''
This __init__ method sets static data for the class.
'''
pass
def ingestLite(self,geoFile,cldFile,SDS,shrink,scale='log'):
'''
This method ingests the lat, long and the specified SDS from the geolocation
and VIIRS Cloud file.
'''
# Get the input file names
ViirsGeoFileName = str(geoFile)
ViirsCProdFileName = str(cldFile)
ViirsCProdSDStype = str(SDS )
shrinkFactor = int(shrink )
scale = str(scale )
self.ViirsGeoFileName = ViirsGeoFileName
self.ViirsCProdFileName = ViirsCProdFileName
self.ViirsCProdSDStype = SDS
self.scale = scale
# Get the various parameters related to the current product
CloudProduct = VD.CloudProdData.CloudProduct[ViirsCProdSDStype]
if isinstance(CloudProduct,VD.CloudProdData.CloudProd) :
pass
else :
CloudProduct = VD.CloudProdData.CloudProduct[ViirsCProdSDStype][scale] # cot and eps
# Get the lat and long from the geolocation file
try :
if(pytables.isHDF5File(ViirsGeoFileName)) :
try :
ViirsGeoFileObj = pytables.openFile(ViirsGeoFileName,mode='r')
print "Successfully opened geolocation file",ViirsGeoFileName
except IOError :
print "error: Could not open geolocation file: ",ViirsGeoFileName
sys.exit(1)
# Detemine the sdr group name and related information
group = ViirsGeoFileObj.getNode('/All_Data')
geoGroupName = '/All_Data/'+group.__members__[0]
group._g_close()
# Make a copy of the geolocation SDR, so we can close the file
try :
print "Reading Latitude dataset..."
dataName = 'Latitude'
geoNode = ViirsGeoFileObj.getNode('%s/%s' % (geoGroupName,dataName))
self.Lat = geoNode[::shrinkFactor,::shrinkFactor]
geoNode.close()
print "done"
print "Reading Longitude dataset..."
dataName = 'Longitude'
geoNode = ViirsGeoFileObj.getNode('%s/%s' % (geoGroupName,dataName))
self.Lon = geoNode[::shrinkFactor,::shrinkFactor]
geoNode.close()
print "done"
print "Reading SolarZenithAngle dataset..."
dataName = 'SolarZenithAngle'
geoNode = ViirsGeoFileObj.getNode('%s/%s' % (geoGroupName,dataName))
self.Sza = geoNode[::shrinkFactor,::shrinkFactor]
geoNode.close()
print "done"
print "Closing geolocation file"
ViirsGeoFileObj.close()
except pyEx.NoSuchNodeError :
print "error: There is no corresponding Node in /All_Data/VIIRS-MOD-GEO_All/ in ",ViirsGeoFileName
ViirsGeoFileObj.close()
sys.exit(1)
else :
print "error: %s is not a HDF5 file, aborting..." % (ViirsGeoFileName)
sys.exit(1)
except pyEx.HDF5ExtError :
print "error: Could not determine whether geolocation isHDF4 or HDF5, aborting..."
sys.exit(1)
# Remove any unit-length dimensions from geolocation arrays
self.Lon = np.squeeze(self.Lon)
self.Lat = np.squeeze(self.Lat)
self.Sza = np.squeeze(self.Sza)
print "Shape of Latitude:",np.shape(self.Lat)
print "Shape of Longitude:",np.shape(self.Lon)
print "Shape of Solar Zenith Angle:",np.shape(self.Sza)
# Open the VIIRS Cloud IP file
try :
ViirsCProdFileObj = pytables.openFile(ViirsCProdFileName,mode='r')
print "Successfully opened IP file",ViirsCProdFileName
except IOError :
print "error: Could not open IP file: ",ViirsCProdFileName
sys.exit(1)
# Get the VIIRS IP dataset
try :
sdsName = CloudProduct.SDSname
self.ViirsCProdSDS = ViirsCProdFileObj.getNode(sdsName)[::shrinkFactor,::shrinkFactor]
#CloudProduct.SDSname)[::shrinkFactor,::shrinkFactor]
print "Type of self.ViirsCProdSDS is: ",self.ViirsCProdSDS.dtype.name
print "Shape of ViirsCProdSDS:",np.shape(self.ViirsCProdSDS)
sdsName = CloudProduct.SDSphaseName
self.ViirsCProdSDSphase = ViirsCProdFileObj.getNode(sdsName)[::shrinkFactor,::shrinkFactor]
#CloudProduct.SDSphaseName)[::shrinkFactor,::shrinkFactor]
print "Type of self.ViirsCProdSDSphase is: ",self.ViirsCProdSDSphase.dtype.name
if self.ViirsCProdSDStype == 'cot' or self.ViirsCProdSDStype == 'eps':
self.ViirsCProdSDSphase = np.bitwise_and(self.ViirsCProdSDSphase,224) >> 5
if self.ViirsCProdSDStype == 'ctp' or self.ViirsCProdSDStype == 'ctt' or self.ViirsCProdSDStype == 'cth':
self.ViirsCProdSDSphase = np.bitwise_and(self.ViirsCProdSDSphase,7)
print "Type of self.ViirsCProdSDSphase is: ",self.ViirsCProdSDSphase.dtype.name
print "Shape of ViirsCProdSDSphase:",np.shape(self.ViirsCProdSDSphase)
print "Closing Cloud Product file"
ViirsCProdFileObj.close()
except pyEx.NoSuchNodeError :
print "error: There is no ",sdsName," dataset in",ViirsCProdFileName
ViirsCProdFileObj.close()
sys.exit(1)
return 0
def ingest(self,geoFile,cmFile,cldFile,SDS,shrink,scale):
'''
This method ingests the lat, long and the specified SDS from the geolocation
and VIIRS Cloud file.
'''
# Get the input file names
ViirsGeoFileName = str(geoFile)
ViirsCMaskFileName = str(cmFile )
ViirsCProdFileName = str(cldFile)
ViirsCProdSDStype = str(SDS )
shrinkFactor = int(shrink )
scale = str(scale )
self.ViirsGeoFileName = ViirsGeoFileName
self.ViirsCProdFileName = ViirsCProdFileName
self.ViirsCProdSDStype = SDS
self.scale = scale
# Get the various parameters related to the current product
CloudProduct = VD.CloudProdData.CloudProduct[ViirsCProdSDStype]
if isinstance(CloudProduct,VD.CloudProdData.CloudProd) :
pass
else :
CloudProduct = VD.CloudProdData.CloudProduct[ViirsCProdSDStype][scale] # cot and eps
# Get the lat and long from the geolocation file
try :
if(pytables.isHDF5File(ViirsGeoFileName)) :
try :
ViirsGeoFileObj = pytables.openFile(ViirsGeoFileName,mode='r')
print "Successfully opened geolocation file",ViirsGeoFileName
except IOError :
print "Could not open geolocation file: ",ViirsGeoFileName
if __name__=='__main__':
sys.exit(1)
else :
return 1
# Make a copy of the geolocation SDR, so we can close the file
try :
self.Lat = np.copy(np.array(ViirsGeoFileObj.getNode('/All_Data/VIIRS-MOD-GEO_All/Latitude')))[::shrinkFactor,::shrinkFactor]
self.Lon = np.copy(np.array(ViirsGeoFileObj.getNode('/All_Data/VIIRS-MOD-GEO_All/Longitude')))[::shrinkFactor,::shrinkFactor]
print "Closing geolocation file"
ViirsGeoFileObj.close()
except pyEx.NoSuchNodeError :
print "There is no corresponding Node in /All_Data/VIIRS-MOD-GEO_All/ in ",ViirsGeoFileName
ViirsGeoFileObj.close()
if __name__=='__main__':
sys.exit(1)
else :
return 1
else :
try :
ViirsGeoFileObj = SD( ViirsGeoFileName, SDC.READ )
print "Successfully opened geolocation file",ViirsGeoFileName
except pyHDF.HDF4Error:
print "Could not open geolocation file: ",ViirsGeoFileName
if __name__=='__main__':
sys.exit(1)
else :
return 1
# Make a copy of the geolocation SDR, so we can close the file
try :
self.Lat = np.copy(ViirsGeoFileObj.select('Latitude')[::shrinkFactor,::shrinkFactor])
self.Lon = np.copy(ViirsGeoFileObj.select('Longitude')[::shrinkFactor,::shrinkFactor])
ViirsGeoFileObj.end()
except pyHDF.HDF4Error:
print "Could not get geolocation dataset"
ViirsGeoFileObj.end()
if __name__=='__main__':
sys.exit(1)
else :
return 1
except pyEx.HDF5ExtError :
print "Could not determine whether geolocation isHDF4 or HDF5, aborting..."
if __name__=='__main__':
sys.exit(1)
else :
return 1
# Remove any unit-length dimensions from geolocation arrays
self.Lon = np.squeeze(self.Lon)
self.Lat = np.squeeze(self.Lat)
print "Shape of Latitude:",shape(self.Lat)
print "Shape of Longitude:",shape(self.Lon)
# Open Viirs Cloud Mask file
try :
ViirsCMaskFileObj = pytables.openFile(ViirsCMaskFileName,'r')
print "Successfully opened HDF5 Cloud Mask file ",ViirsCMaskFileName
except :
print "Could not open Cloud Mask file: ",ViirsCMaskFileName
if __name__=='__main__':
sys.exit(1)
else :
return 1
# Get the VIIRS VCM quantities (Phase and Quality)
try :
ViirsDataSetPath = '/All_Data/VIIRS-CM-IP_All/QF1_VIIRSCMIP'
self.ViirsCMquality = np.copy(ViirsCMaskFileObj.getNode(ViirsDataSetPath))[::shrinkFactor,::shrinkFactor]
self.ViirsCMquality = np.bitwise_and(self.ViirsCMquality,3) >> 0
self.ViirsCMqualityMask = ma.masked_equal(self.ViirsCMquality,0)
ViirsDataSetPath = '/All_Data/VIIRS-CM-IP_All/QF6_VIIRSCMIP'
self.ViirsCMphase = np.copy(ViirsCMaskFileObj.getNode(ViirsDataSetPath))[::shrinkFactor,::shrinkFactor]
self.ViirsCMphase = np.bitwise_and(self.ViirsCMphase,7) >> 0
print "Closing Cloud Mask file"
ViirsCMaskFileObj.close()
except pyEx.NoSuchNodeError :
print "There is no ",ViirsDataSetPath," SDR in",ViirsCMaskFileName
ViirsCMaskFileObj.close()
if __name__=='__main__':
sys.exit(1)
else :
return 1
# Open the VIIRS Cloud IP file
try :
ViirsCProdFileObj = pytables.openFile(ViirsCProdFileName,mode='r')
print "Successfully opened IP file"
except IOError :
print "Could not open IP file: ",ViirsCProdFileName
if __name__=='__main__':
sys.exit(1)
else :
return 1
# Get the VIIRS IP dataset
try :
self.ViirsCProdSDS = np.copy(ViirsCProdFileObj.getNode(CloudProduct.SDSname))[::shrinkFactor,::shrinkFactor]
print "Closing Cloud Product file"
ViirsCProdFileObj.close()
except pyEx.NoSuchNodeError :
print "There is no ",CloudProduct.SDSname," SDS in",ViirsCProdFileName
ViirsCProdFileObj.close()
if __name__=='__main__':
sys.exit(1)
else :
return 1
# Make logarithmic if appropriate
if(CloudProduct.logScale):
print "Log scaling the dataset..."
self.ViirsCProdSDS = np.log10(self.ViirsCProdSDS)