def get_ref(self): """ return Ref """ data = dict() if self.resolution == 750: # 分辨率为 1000 satellite_type1 = ['NPP', 'JPSS-1'] # NPP if self.satellite in satellite_type1: data_file = self.in_file with h5py.File(data_file, 'r') as h5r: # 前12个通道是ref # 由于不确定性 比较了14和15年的数据值一致,定值。偶尔出现-999.33导致计算错 ref_a = 2.4417415E-5 vmin = 0 vmax = 60000 sunz = self.get_solar_zenith() dsl = sun_earth_dis_correction(self.ymd) # 逐个通道处理 for i in xrange(1, 12, 1): band = 'CH_{:02d}'.format(i) sds_name = '/All_Data/VIIRS-M%d-SDR_All/Reflectance' % i ary_ref = h5r.get(sds_name)[:] data_pre = ary_ref data_pre = data_pre.astype(np.float32) invalid_index = np.logical_or(data_pre <= vmin, data_pre > vmax) data_pre[invalid_index] = np.nan # 修正,徐娜提供 data[band] = data_pre * ref_a * \ np.cos(np.deg2rad(sunz)) * dsl else: raise ValueError('Cant read this satellite`s data.: {}'.format( self.satellite)) else: raise ValueError( 'Cant read this data, please check its resolution: {}'.format( self.in_file)) return data
def get_ref(self): """ return ref """ data = dict() dsl = sun_earth_dis_correction(self.ymd) if self.resolution == 1000: # 分辨率为 1000 satellite_type = ['AQUA', 'TERRA'] data_file = self.in_file if self.satellite in satellite_type: dn = self.get_dn() h4r = SD(data_file, SDC.READ) # 12通道ab ary_ch1_2_a = h4r.select( 'EV_250_Aggr1km_RefSB').attributes()['reflectance_scales'] ary_ch1_2_b = h4r.select( 'EV_250_Aggr1km_RefSB').attributes()['reflectance_offsets'] ary_ch3_7_a = h4r.select( 'EV_500_Aggr1km_RefSB').attributes()['reflectance_scales'] ary_ch3_7_b = h4r.select( 'EV_500_Aggr1km_RefSB').attributes()['reflectance_offsets'] ary_ch8_19and26_a = h4r.select( 'EV_1KM_RefSB').attributes()['reflectance_scales'] ary_ch8_19and26_b = h4r.select( 'EV_1KM_RefSB').attributes()['reflectance_offsets'] # ary_ch20_36_a = h4r.select( # 'EV_1KM_Emissive').attributes()['radiance_scales'] # ary_ch20_36_b = h4r.select( # 'EV_1KM_Emissive').attributes()['radiance_offsets'] h4r.end() # 删除13i 14i通道的下标 ary_ch8_19and26_a.pop(6) ary_ch8_19and26_a.pop(8) # 逐个通道处理 for i in xrange(self.channels): band = 'CH_{:02d}'.format(i + 1) # 1,2通道 if i < 2: k = i data_pre = (dn[band] - ary_ch1_2_b[k]) * ary_ch1_2_a[k] # 3,4,5,6,7通道 elif i >= 2 and i < 7: k = i - 2 data_pre = (dn[band] - ary_ch3_7_b[k]) * ary_ch3_7_a[k] # 8,9,10,11,12,13lo,14lo,15,16,17,18,19,26通道 elif i >= 7 and i <= 19: k = i - 7 if i == 19: band = 'CH_{:02d}'.format(i + 7) data_pre = (dn[band] - ary_ch8_19and26_b[k] ) * ary_ch8_19and26_a[k] else: continue data[band] = data_pre * dsl return data
def Load(self, L1File): print u'读取 L1所有数据信息...... %s' % L1File try: h5File_R = h5py.File(L1File, 'r') ary_lon = h5File_R.get('/All_Data/VIIRS-MOD-GEO_All/Longitude')[:] ary_lat = h5File_R.get('/All_Data/VIIRS-MOD-GEO_All/Latitude')[:] ary_satz = h5File_R.get( '/All_Data/VIIRS-MOD-GEO_All/SatelliteZenithAngle')[:] ary_sata = h5File_R.get( '/All_Data/VIIRS-MOD-GEO_All/SatelliteAzimuthAngle')[:] ary_sunz = h5File_R.get( '/All_Data/VIIRS-MOD-GEO_All/SolarZenithAngle')[:] ary_suna = h5File_R.get( '/All_Data/VIIRS-MOD-GEO_All/SolarAzimuthAngle')[:] ary_time = h5File_R.get('/All_Data/VIIRS-MOD-GEO_All/StartTime')[:] Ref = {} # 由于不确定性 比较了14和15年的数据值一致,定值。偶尔出现-999.33导致计算错 Ref_a = 2.4417415E-5 Rad = {} Rad_a = {} Rad_b = {} Tbb = {} Tbb_a = {} Tbb_b = {} for i in xrange(1, 17, 1): if i < 12: Ref[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/Reflectance' % i)[:] elif i == 13: Rad[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/Radiance' % i)[:] Tbb[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/BrightnessTemperature' % i)[:] Rad_a[i] = 1. Rad_b[i] = 0. Tbb_a[i] = 1. Tbb_b[i] = 0. else: Rad[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/Radiance' % i)[:] Rad_a[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/RadianceFactors' % i)[:][0] Rad_b[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/RadianceFactors' % i)[:][1] Tbb[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/BrightnessTemperature' % i)[:] Tbb_a[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/BrightnessTemperatureFactors' % i)[:][0] Tbb_b[i] = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/BrightnessTemperatureFactors' % i)[:][1] except Exception as e: print str(e) finally: h5File_R.close() # 11111111通道的中心波数和光谱响应 # for i in xrange(self.Band): i = 14 BandName = 'CH_%02d' % (i + 1) srfFile = os.path.join( MainPath, 'SRF', '%s_%s_SRF_CH%02d_Pub.txt' % (self.sat, self.sensor, (i + 1))) dictWave = np.loadtxt( srfFile, dtype={'names': ('num', 'rad'), 'formats': ('f4', 'f4')}) waveNum = 10 ** 7 / dictWave['num'][::-1] waveRad = dictWave['rad'][::-1] self.waveNum[BandName] = waveNum self.waveRad[BandName] = waveRad # 2222222 数据大小 使用经度维度 ############### dshape = ary_lon.shape # 对时间进行赋值合并 v_ymd2seconds = np.vectorize(npp_ymd2seconds) T1 = v_ymd2seconds(ary_time) Time = np.full(dshape, -999) for i in xrange(len(Time)): Time[i, :] = T1[i / 16] if self.Time == []: self.Time = Time else: self.Time = np.concatenate((self.Time, Time)) ymd = time.gmtime(self.Time[0, 0]) ymd1 = time.strftime('%Y%m%d', ymd) dsol = sun_earth_dis_correction(ymd1) # 3333333 1-12通道的可见光数据进行定标 ######## for i in range(1, 12, 1): BandName = 'CH_%02d' % i condition = np.logical_and(Ref[i] > 0, Ref[i] < 65500) idx = np.where(condition) Ref_data = np.full(dshape, np.nan) Ref_data[idx] = Ref[i][idx] Ref_data = Ref_data * Ref_a * np.cos(np.deg2rad(ary_sunz)) * dsol if BandName not in self.Ref.keys(): self.Ref[BandName] = Ref_data else: self.Ref[BandName] = np.concatenate( (self.Ref[BandName], Ref_data)) # 红外 for i in xrange(12, 17, 1): BandName = 'CH_%02d' % i condition = np.logical_and(Rad[i] > 0, Rad[i] < 65500) idx = np.where(condition) Rad_data = np.full(dshape, np.nan) Tbb_data = np.full(dshape, np.nan) Rad_data[idx] = Rad[i][idx] * \ ((10000 / self.WN[BandName]) ** 2) / 10. Rad_data[idx] = Rad_data[idx] * Rad_a[i] + Rad_b[i] Tbb_data[idx] = Tbb[i][idx] * Tbb_a[i] + Tbb_b[i] if BandName not in self.Rad.keys(): self.Rad[BandName] = Rad_data else: self.Rad[BandName] = np.concatenate( (self.Rad[BandName], Rad_data)) if BandName not in self.Tbb.keys(): self.Tbb[BandName] = Tbb_data else: self.Tbb[BandName] = np.concatenate( (self.Tbb[BandName], Tbb_data)) # 经纬度 ary_lon_idx = np.full(dshape, np.nan) condition = np.logical_and(ary_lon > -180., ary_lon < 180.) ary_lon_idx[condition] = ary_lon[condition] if self.Lons == []: self.Lons = ary_lon_idx else: self.Lons = np.concatenate((self.Lons, ary_lon_idx)) ary_lat_idx = np.full(dshape, np.nan) condition = np.logical_and(ary_lat > -90., ary_lat < 90.) ary_lat_idx[condition] = ary_lat[condition] if self.Lats == []: self.Lats = ary_lat_idx else: self.Lats = np.concatenate((self.Lats, ary_lat_idx)) # 角度信息 if self.satAzimuth == []: self.satAzimuth = ary_sata else: self.satAzimuth = np.concatenate((self.satAzimuth, ary_sata)) if self.satZenith == []: self.satZenith = ary_satz else: self.satZenith = np.concatenate((self.satZenith, ary_satz)) if self.sunAzimuth == []: self.sunAzimuth = ary_suna else: self.sunAzimuth = np.concatenate((self.sunAzimuth, ary_suna)) if self.sunZenith == []: self.sunZenith = ary_sunz else: self.sunZenith = np.concatenate((self.sunZenith, ary_sunz))
def Load(self, L1File): print(u'读取 L1 %s' % L1File) iname = os.path.basename(L1File) try: h4File = SD(L1File, SDC.READ) # 读取1-2通道数据 in_data_r250 = h4File.select('EV_250_Aggr1km_RefSB').get() in_data_r250_s = h4File.select( 'EV_250_Aggr1km_RefSB').attributes()['reflectance_scales'] in_data_r250_o = h4File.select( 'EV_250_Aggr1km_RefSB').attributes()['reflectance_offsets'] # 读取 3-7通道数据 in_data_r500 = h4File.select('EV_500_Aggr1km_RefSB').get() in_data_r500_s = h4File.select( 'EV_500_Aggr1km_RefSB').attributes()['reflectance_scales'] in_data_r500_o = h4File.select( 'EV_500_Aggr1km_RefSB').attributes()['reflectance_offsets'] # 读取8-20通道, 包含26通道 in_data_r1km = h4File.select('EV_1KM_RefSB').get() in_data_r1km_s = h4File.select( 'EV_1KM_RefSB').attributes()['reflectance_scales'] in_data_r1km_o = h4File.select( 'EV_1KM_RefSB').attributes()['reflectance_offsets'] # 读取20-36通道 不包含26通道 in_data_t1km = h4File.select('EV_1KM_Emissive').get() in_data_t1km_s = h4File.select( 'EV_1KM_Emissive').attributes()['radiance_scales'] in_data_t1km_o = h4File.select( 'EV_1KM_Emissive').attributes()['radiance_offsets'] except Exception as e: print str(e) finally: h4File.end() # 处理定位文件,默认和L1同一目录 geoFile = find_modis_geo_file(L1File) print(u'读取geo %s' % geoFile) try: h4File = SD(geoFile, SDC.READ) in_data_lon = h4File.select('Longitude').get() in_data_lat = h4File.select('Latitude').get() # in_data_LandSeaMask = h4File.select('Land/SeaMask').get() # # 多个/不知道是bug还是就这样 in_data_satz = h4File.select('SensorZenith').get() in_data_satz_s = h4File.select( 'SensorZenith').attributes()['scale_factor'] in_data_sata = h4File.select('SensorAzimuth').get() in_data_sata_s = h4File.select( 'SensorAzimuth').attributes()['scale_factor'] in_data_sunz = h4File.select('SolarZenith').get() in_data_sunz_s = h4File.select( 'SolarZenith').attributes()['scale_factor'] in_data_suna = h4File.select('SolarAzimuth').get() in_data_suna_s = h4File.select( 'SolarAzimuth').attributes()['scale_factor'] except Exception as e: print(str(e)) finally: h4File.end() # 角度信息赋值 self.satZenith = in_data_satz * in_data_satz_s self.satAzimuth = in_data_sata * in_data_sata_s self.sunZenith = in_data_sunz * in_data_sunz_s self.sunAzimuth = in_data_suna * in_data_suna_s self.Lons = in_data_lon self.Lats = in_data_lat # 1-2通道 modis_shape2 = in_data_r250.shape[1:] modis_shape3 = (38, ) + modis_shape2 data_ch38 = np.full(modis_shape3, np.nan) for i in range(0, 2, 1): # 过滤 无效值 condition = np.logical_and(in_data_r250[i] < 32767, in_data_r250[i] > 0) idx = np.where(condition) data_ch38[i][idx] = (in_data_r250[i][idx] - in_data_r250_o[i]) * in_data_r250_s[i] # 3-7通道 for i in range(2, 7, 1): # 过滤 无效值 condition = np.logical_and(in_data_r500[i - 2] < 32767, in_data_r500[i - 2] > 0) idx = np.where(condition) data_ch38[i][idx] = (in_data_r500[i - 2][idx] - in_data_r500_o[i - 2]) * in_data_r500_s[i - 2] # 8-19 外加一个 26通道 for i in range(7, 22, 1): # 过滤 无效值 condition = np.logical_and(in_data_r1km[i - 7] < 32767, in_data_r1km[i - 7] > 0) idx = np.where(condition) data_ch38[i][idx] = (in_data_r1km[i - 7][idx] - in_data_r1km_o[i - 7]) * in_data_r1km_s[i - 7] # 20-36通道 不包括26通道 for i in range(22, 38, 1): # 过滤 无效值 condition = np.logical_and(in_data_t1km[i - 22] < 32767, in_data_t1km[i - 22] > 0) idx = np.where(condition) data_ch38[i][idx] = (in_data_t1km[i - 22][idx] - in_data_t1km_o[i - 22]) * in_data_t1km_s[i - 22] # 删除通道13hi 和 14 hi data_ch36 = np.delete(data_ch38, (13, 15), 0) newRad = np.full((16, ) + modis_shape2, np.nan) # 对时间进行赋值 self.Time = np.full(modis_shape2, -999.) nameClass = pb_name.nameClassManager() info = nameClass.getInstance(iname) ymd = info.dt_s.strftime('%Y%m%d') print ymd dsol = sun_earth_dis_correction(ymd) # print info.dt_s secs = int((info.dt_s - datetime(1970, 1, 1, 0, 0, 0)).total_seconds()) self.Time[:] = secs # 把modis的rad转换和FY的单位一致 # 20,21, 22,23,24,25,27,28,29,30,31,32,33,34,35,36 cwn = [ 2.647409E+03, 2.511760E+03, 2.517908E+03, 2.462442E+03, 2.248296E+03, 2.209547E+03, 1.474262E+03, 1.361626E+03, 1.169626E+03, 1.028740E+03, 9.076813E+02, 8.308411E+02, 7.482978E+02, 7.307766E+02, 7.182094E+02, 7.035007E+02 ] for i in xrange(16): newRad[i] = data_ch36[i + 20] * \ ((10000 / cwn[i]) ** 2) / 10. * dsol if i <= 5: self.Rad['CH_%02d' % (i + 20)] = newRad[i] else: self.Rad['CH_%02d' % (i + 21)] = newRad[i] # 把rad转亮温 radiance2tbb(data_ch36) # 整理通道顺序,把26通道放在对应的26通道 # del26_data_ch35 = np.delete(data_ch36, 19, 0) # order_data_ch36 = np.insert(del26_data_ch35, 25, data_ch36[19], 0) for i in range(0, 36, 1): # self.CA['CH_%02d' % (i + 1)] = order_data_ch36[i] if i < 19: self.Ref['CH_%02d' % (i + 1)] = data_ch36[i] elif i == 19: # 26 self.Ref['CH_%02d' % (i + 7)] = data_ch36[i] elif i > 19 and i <= 25: self.Tbb['CH_%02d' % (i)] = data_ch36[i] elif i > 25: self.Tbb['CH_%02d' % (i + 1)] = data_ch36[i]
def Load(self, L1File): print u'读取 L1所有数据信息...... %s' % L1File if not os.path.isfile(L1File): print 'Error: %s not found' % L1File return try: h5File_R = h5py.File(L1File, 'r') except Exception as e: print str(e) return try: ary_lon = h5File_R.get('/All_Data/VIIRS-MOD-GEO_All/Longitude')[:] ary_lat = h5File_R.get('/All_Data/VIIRS-MOD-GEO_All/Latitude')[:] self.satAzimuth = h5File_R.get( '/All_Data/VIIRS-MOD-GEO_All/SatelliteAzimuthAngle')[:] self.satZenith = h5File_R.get( '/All_Data/VIIRS-MOD-GEO_All/SatelliteZenithAngle')[:] self.sunAzimuth = h5File_R.get( '/All_Data/VIIRS-MOD-GEO_All/SolarAzimuthAngle')[:] self.sunZenith = h5File_R.get( '/All_Data/VIIRS-MOD-GEO_All/SolarZenithAngle')[:] # Times = h5File_R.get('/All_Data/VIIRS-MOD-GEO_All/MidTime')[:] Times = h5File_R.get('/All_Data/VIIRS-MOD-GEO_All/StartTime')[:] tmpTime = np.full_like(ary_lon, -999, dtype='i4') v_ymd2seconds = np.vectorize(npp_ymd2seconds) T1 = v_ymd2seconds(Times) for i in xrange(len(tmpTime)): tmpTime[i, :] = T1[i / 16] self.Time = tmpTime ymd = time.gmtime(self.Time[0, 0]) ymd = time.strftime('%Y%m%d', ymd) # print ymd dsol = sun_earth_dis_correction(ymd) for i in xrange(1, 17, 1): # sv,bb 赋值 None, 有值则赋真实值 bandName = 'CH_%02d' % i if i < 12: ref = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/Reflectance' % i)[:] # ref_fac = h5File_R.get('/All_Data/VIIRS-M%d-SDR_All/ReflectanceFactors' % i)[:][0] # 由于不确定性 比较了14和15年的数据值一致,定值。偶尔出现-999.33导致计算错 ref_fac = 2.4417415E-5 idx = np.where(ref < 65500) newRef = np.full_like(ref, np.nan, 'f4') newRef[idx] = ref[idx] self.Ref[bandName] = newRef * ref_fac * \ np.cos(np.deg2rad(self.sunZenith)) * dsol elif i == 13: rad = h5File_R.get('/All_Data/VIIRS-M%d-SDR_All/Radiance' % i)[:] tbb = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/BrightnessTemperature' % i)[:] idx = np.where(rad > 0) newRad = np.full_like(rad, np.nan, 'f4') newRad[idx] = rad[idx] * \ ((10000 / self.WN['CH_%02d' % i]) ** 2) / 10. idx = np.where(tbb > 0) newTbb = np.full_like(tbb, np.nan, 'f4') newTbb[idx] = tbb[idx] self.Rad[bandName] = newRad self.Tbb[bandName] = newTbb else: rad = h5File_R.get('/All_Data/VIIRS-M%d-SDR_All/Radiance' % i)[:] rad_fac = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/RadianceFactors' % i)[:][0] rad_int = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/RadianceFactors' % i)[:][1] tbb = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/BrightnessTemperature' % i)[:] tbb_fac = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/BrightnessTemperatureFactors' % i)[:][0] tbb_int = h5File_R.get( '/All_Data/VIIRS-M%d-SDR_All/BrightnessTemperatureFactors' % i)[:][1] idx = np.where(rad < 65500) newRad = np.full_like(rad, np.nan, 'f4') newRad[idx] = rad[idx] * \ ((10000 / self.WN['CH_%02d' % i]) ** 2) / 10. idx = np.where(tbb < 65500) newTbb = np.full_like(tbb, np.nan, 'f4') newTbb[idx] = tbb[idx] self.Rad[bandName] = newRad * rad_fac + rad_int self.Tbb[bandName] = newTbb * tbb_fac + tbb_int except Exception as e: print str(e) return finally: h5File_R.close() # 通道的中心波数和光谱响应 # for i in xrange(self.Band): i = 14 BandName = 'CH_%02d' % (i + 1) srfFile = os.path.join( MainPath, 'SRF', '%s_%s_SRF_CH%02d_Pub.txt' % (self.sat, self.sensor, (i + 1))) dictWave = np.loadtxt(srfFile, dtype={ 'names': ('num', 'rad'), 'formats': ('f4', 'f4') }) waveNum = 10**7 / dictWave['num'][::-1] waveRad = dictWave['rad'][::-1] self.waveNum[BandName] = waveNum self.waveRad[BandName] = waveRad # 经纬度 ary_lon_idx = np.full(ary_lon.shape, np.nan) condition = np.logical_and(ary_lon > -180., ary_lon < 180.) ary_lon_idx[condition] = ary_lon[condition] if self.Lons == []: self.Lons = ary_lon_idx else: self.Lons = np.concatenate((self.Lons, ary_lon_idx)) ary_lat_idx = np.full(ary_lon.shape, np.nan) condition = np.logical_and(ary_lat > -90., ary_lat < 90.) ary_lat_idx[condition] = ary_lat[condition] if self.Lats == []: self.Lats = ary_lat_idx else: self.Lats = np.concatenate((self.Lats, ary_lat_idx))