Esempio n. 1
0
 def set_ymd_hms(self):
     """
     根据根据L1文件名 set self.level_1_ymd 和 self.level_1_ymd
     :return:
     """
     file_name = os.path.basename(self.in_file)
     self.ymd = get_ymd(file_name)
     self.hms = get_hm(file_name) + '00'
Esempio n. 2
0
 def _get_ymd(self):
     if self.error:
         return
     try:
         self.ymd = pb_time.get_ymd(self.l1_1000m)
         self.hm = pb_time.get_hm(self.l1_1000m)
     except Exception as why:
         print why
         self.error = True
Esempio n. 3
0
def _get_out_file(in_file, out_path, suffix):
    """
    通过输入的文件名和输出路径获取输出文件的完整路径
    :return:
    """
    try:
        if isinstance(in_file, str):
            ymd = pb_time.get_ymd(in_file)
            out_path = pb_io.path_replace_ymd(out_path, ymd)
            _name = os.path.basename(in_file)
            name = _name.replace("_c", "") + suffix
            out_file = os.path.join(out_path, name)
            if not os.path.isdir(os.path.dirname(out_file)):
                try:
                    os.makedirs(os.path.dirname(out_file))
                except OSError as why:
                    print why
            return out_file
    except Exception as why:
        print why
        return
Esempio n. 4
0
    def Load(self, L1File):
        ipath = os.path.dirname(L1File)
        iname = os.path.basename(L1File)

        self.file_attr = self.read_file_attr(L1File)

        if 'FY3C' in iname[:4]:
            # 根据输入的L1文件自动拼接GEO文件

            geoFile = os.path.join(ipath, iname[0:-12] + 'GEOXX_MS.HDF')
            obcFile = os.path.join(ipath, iname[0:-12] + 'OBCXX_MS.HDF')
            print u'%s' % L1File
            print u'%s' % geoFile
            print u'%s' % obcFile

            #################### 读取L1文件 ######################
            try:
                with h5py.File(L1File, 'r') as h5File_R:
                    ary_ch3 = h5File_R.get('/Data/EV_Emissive')[:]
                    ary_ch7 = h5File_R.get('/Data/EV_RefSB')[:]
                    ary_offsets = h5File_R.get(
                        '/Data/Emissive_Radiance_Offsets')[:]
                    ary_scales = h5File_R.get(
                        '/Data/Emissive_Radiance_Scales')[:]
                    ary_ref_cal = h5File_R.attrs['RefSB_Cal_Coefficients']
                    ary_Nonlinear = h5File_R.attrs[
                        'Prelaunch_Nonlinear_Coefficients']
                    try:
                        ary_tb_coeff = h5File_R.attrs[
                            'Emmisive_BT_Coefficients']
                    except Exception:
                        ary_tb_coeff = h5File_R.attrs[
                            'Emissive_BT_Coefficients']
            except Exception as e:
                self.error = True
                print str(e)
                return
            #################### 读取GEO文件 ######################
            try:
                with h5py.File(geoFile, 'r') as h5File_R:
                    ary_satz = h5File_R.get('/Geolocation/SensorZenith')[:]
                    ary_sata = h5File_R.get('/Geolocation/SensorAzimuth')[:]
                    ary_sunz = h5File_R.get('/Geolocation/SolarZenith')[:]
                    ary_suna = h5File_R.get('/Geolocation/SolarAzimuth')[:]
                    ary_lon = h5File_R.get('/Geolocation/Longitude')[:]
                    ary_lat = h5File_R.get('/Geolocation/Latitude')[:]
                    ary_height = h5File_R.get('/Geolocation/DEM')[:]
                    ary_LandCover = h5File_R.get('/Geolocation/LandCover')[:]
                    ary_LandSeaMask = h5File_R.get(
                        '/Geolocation/LandSeaMask')[:]
            except Exception as e:
                self.error = True
                print str(e)
                return
            #################### 读取OBC文件 ####################
            try:
                with h5py.File(obcFile, 'r') as h5File_R:
                    ary_sv = h5File_R.get('/Calibration/Space_View')[:]
                    ary_bb = h5File_R.get('/Calibration/Blackbody_View')[:]
            except Exception as e:
                self.error = True
                print str(e)
                return

        else:
            # FY3A/FY3B VIRR
            # 根据输入的L1文件自动拼接OBC文件
            obcFile = os.path.join(ipath, iname[0:-12] + 'OBCXX_MS.HDF')
            print u'%s' % L1File
            print u'%s' % obcFile
            #################### 读取L1文件 ####################
            try:
                with h5py.File(L1File, 'r') as h5File_R:
                    ary_ch3 = h5File_R.get('/EV_Emissive')[:, 0:1800, 0:2048]
                    ary_ch7 = h5File_R.get('/EV_RefSB')[:, 0:1800, 0:2048]
                    ary_offsets = h5File_R.get('/Emissive_Radiance_Offsets')[
                        0:1800, :]
                    ary_scales = h5File_R.get('/Emissive_Radiance_Scales')[
                        0:1800, :]
                    ary_ref_cal = h5File_R.attrs['RefSB_Cal_Coefficients']
                    ary_Nonlinear = h5File_R.attrs[
                        'Prelaunch_Nonlinear_Coefficients']
                    try:
                        ary_tb_coeff = h5File_R.attrs[
                            'Emmisive_BT_Coefficients']
                    except Exception:
                        ary_tb_coeff = h5File_R.attrs[
                            'Emissive_BT_Coefficients']
                    ary_satz = h5File_R.get('/SensorZenith')[0:1800, 0:2048]
                    ary_sata = h5File_R.get('/SensorAzimuth')[0:1800, 0:2048]
                    ary_sunz = h5File_R.get('/SolarZenith')[0:1800, 0:2048]
                    ary_suna = h5File_R.get('/SolarAzimuth')[0:1800, 0:2048]
                    ary_lon = h5File_R.get('/Longitude')[0:1800, 0:2048]
                    ary_lat = h5File_R.get('/Latitude')[0:1800, 0:2048]
                    ary_height = h5File_R.get('/Height')[0:1800, 0:2048]
                    ary_LandCover = h5File_R.get('/LandCover')[0:1800, 0:2048]
                    ary_LandSeaMask = h5File_R.get('/LandSeaMask')[0:1800,
                                                                   0:2048]
            except Exception as e:
                self.error = True
                print str(e)
                return
            #################### 读取OBC文件 ####################
            try:
                with h5py.File(obcFile, 'r') as h5File_R:
                    ary_sv = h5File_R.get('Space_View')[:]
                    ary_bb = h5File_R.get('Blackbody_View')[:]
            except Exception as e:
                self.error = True
                print(str(e))
                return

        # 通道的中心波数和光谱响应
        for i in xrange(self.Band):
            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')
                                  })
            if BandName in ['CH_03', 'CH_04', 'CH_05']:
                waveNum = 10**4 / dictWave['num'][::-1]
            else:
                waveNum = 10**7 / dictWave['num'][::-1]
            waveRad = dictWave['rad'][::-1]
            if BandName not in self.waveNum:
                self.waveNum[BandName] = waveNum
                self.waveRad[BandName] = waveRad
            else:
                self.waveNum[BandName] = np.concatenate(
                    (self.waveNum[BandName], waveNum))
                self.waveRad[BandName] = np.concatenate(
                    (self.waveRad[BandName], waveRad))
        ############### 数据大小 使用经度维度 ###############
        dshape = ary_lon.shape

        ##################可见光数据进行定标 ########
        #         LutAry = np.loadtxt(self.LutFile, dtype={'names': ('TBB', 'CH_03', 'CH_04', 'CH_05'),
        #                             'formats': ('i4', 'f4', 'f4', 'f4')})
        # 定标系数b,a
        proj_Cal_Coeff = np.full((7, 2), np.nan)
        for i in range(7):
            for j in range(2):
                proj_Cal_Coeff[i, j] = ary_ref_cal[i * 2 + j - 1]

        # 1 - 6 通道的ref
        for i in xrange(self.Band):
            BandName = 'CH_%02d' % (i + 1)
            if i < 2 or i >= 5:
                # 下标k (1 2 6 7 8 9 10存放在一个三维数组中)
                if i < 2:
                    k = i
                elif i >= 5:
                    k = i - 3

                # DN值存放无效值用nan填充
                DN = np.full(dshape, np.nan)
                idx = np.logical_and(ary_ch7[k] < 32767, ary_ch7[k] > 0)
                DN[idx] = ary_ch7[k][idx]

                # 反射率值存放无效值用nan填充
                k0 = proj_Cal_Coeff[k][1]
                k1 = proj_Cal_Coeff[k][0]
                Ref = (DN * k0 + k1) / 100.
                K0 = np.full(dshape, k0, dtype=np.float32)
                K1 = np.full(dshape, k1, dtype=np.float32)
                if BandName not in self.Dn:
                    self.Dn[BandName] = DN
                    self.Ref[BandName] = Ref
                    self.K0[BandName] = K0
                    self.K1[BandName] = K1
                else:
                    self.Dn[BandName] = np.concatenate((self.Dn[BandName], DN))
                    self.Ref[BandName] = np.concatenate(
                        (self.Ref[BandName], Ref))
                    self.K0[BandName] = np.concatenate((self.K0[BandName], K0))
                    self.K1[BandName] = np.concatenate((self.K1[BandName], K1))

            if 2 <= i <= 4:
                # DN Rad Tbb 值存放,默认 nan填充
                k = i - 2

                DN = np.full(dshape, np.nan)
                Rad_pre = np.full(dshape, np.nan)

                condition = np.logical_and(ary_ch3[k] < 32767, ary_ch3[k] > 0)
                idx = np.where(condition)

                # 下标i-2 (3,4,5通道DN存放在一个三维数组中)
                DN[idx] = ary_ch3[k][idx]

                Rad_pre[idx] = DN[idx] * ary_scales[idx[0], k] + \
                    ary_offsets[idx[0], k]
                k0 = ary_Nonlinear[k]
                k1 = ary_Nonlinear[3 + k]
                k2 = ary_Nonlinear[6 + k]
                Rad_nonlinearity = Rad_pre**2 * k2 + Rad_pre * k1 + k0

                K0 = np.full(dshape, np.nan)
                K0[:] = ary_scales[:, k].reshape(-1, 1)
                K1 = np.full(dshape, np.nan)
                K1[:] = ary_offsets[:, k].reshape(-1, 1)

                self.Rad_pre[BandName] = Rad_pre
                Rad = Rad_pre + Rad_nonlinearity

                Tbb = pb_sat.planck_r2t(Rad, self.WN[BandName])
                Tbb = Tbb * self.TeA[BandName] + self.TeB[BandName]

                k0_coeff = ary_tb_coeff[k * 2 + 1]
                k1_coeff = ary_tb_coeff[k * 2]
                Tbb_coeff = Tbb * k0_coeff + k1_coeff

                if BandName not in self.Dn:
                    self.Dn[BandName] = DN
                    self.Rad[BandName] = Rad
                    self.K0[BandName] = K0
                    self.K1[BandName] = K1
                    self.Tbb[BandName] = Tbb
                    self.Tbb_coeff[BandName] = Tbb_coeff
                else:
                    self.Dn[BandName] = np.concatenate((self.Dn[BandName], DN))
                    self.Rad[BandName] = np.concatenate(
                        (self.Rad[BandName], Rad))
                    self.K0[BandName] = np.concatenate((self.K0[BandName], K0))
                    self.K1[BandName] = np.concatenate((self.K1[BandName], K1))
                    self.Tbb[BandName] = np.concatenate(
                        (self.Tbb[BandName], Tbb))
                    self.Tbb_coeff[BandName] = np.concatenate(
                        (self.Tbb_coeff[BandName], Tbb_coeff))
        # SV, BB
        for i in xrange(self.Band):
            BandName = 'CH_%02d' % (i + 1)
            SV = np.full(dshape, np.nan)
            BB = np.full(dshape, np.nan)
            SV[:] = ary_sv[i, :, 0].reshape(-1, 1)
            BB[:] = ary_bb[i, :, 0].reshape(-1, 1)

            if BandName not in self.SV:
                self.SV[BandName] = SV
                self.BB[BandName] = BB
            else:
                self.SV[BandName] = np.concatenate((self.SV[BandName], SV))
                self.BB[BandName] = np.concatenate((self.BB[BandName], BB))
        ##################### 全局信息赋值 ############################
        # 对时间进行赋值合并
        Time = np.full(dshape, np.nan)
        ymd = get_ymd(L1File)
        hm = get_hm(L1File)
        file_date = datetime.strptime(ymd + hm, '%Y%m%d%H%M')
        secs = (file_date - datetime(1970, 1, 1, 0, 0, 0)).total_seconds()
        Time[:] = secs
        if self.Time == []:
            self.Time = Time
        else:
            self.Time = np.concatenate((self.Time, Time))

        # 土地覆盖
        ary_LandCover_idx = np.full(dshape, np.nan)
        condition = np.logical_and(ary_LandCover >= 0, ary_LandCover <= 254)
        ary_LandCover_idx[condition] = ary_LandCover[condition]

        if self.LandCover == []:
            self.LandCover = ary_LandCover_idx
        else:
            self.LandCover = np.concatenate(
                (self.LandCover, ary_LandCover_idx))

        # 海陆掩码
        ary_LandSeaMask_idx = np.full(dshape, np.nan)
        condition = np.logical_and(ary_LandSeaMask >= 0, ary_LandSeaMask <= 7)
        ary_LandSeaMask_idx[condition] = ary_LandSeaMask[condition]

        if self.LandSeaMask == []:
            self.LandSeaMask = ary_LandSeaMask_idx
        else:
            self.LandSeaMask = np.concatenate(
                (self.LandSeaMask, ary_LandSeaMask_idx))

        # 经纬度
        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))

        # 高度
        ary_height_idx = np.full(dshape, np.nan)
        condition = np.logical_and(ary_height > -1000., ary_height < 10000.)
        ary_height_idx[condition] = ary_height[condition]
        if self.Height == []:
            self.Height = ary_height_idx
        else:
            self.Height = np.concatenate((self.Height, ary_height_idx))

        # 卫星方位角 天顶角
        ary_sata_idx = np.full(dshape, np.nan)
        condition = np.logical_and(ary_sata > -18000, ary_sata < 18000)
        ary_sata_idx[condition] = ary_sata[condition]

        if self.satAzimuth == []:
            self.satAzimuth = ary_sata_idx / 100.
        else:
            self.satAzimuth = np.concatenate(
                (self.satAzimuth, ary_sata_idx / 100.))

        ary_satz_idx = np.full(dshape, np.nan)
        condition = np.logical_and(ary_satz > 0, ary_satz < 18000)
        ary_satz_idx[condition] = ary_satz[condition]
        if self.satZenith == []:
            self.satZenith = ary_satz_idx / 100.
        else:
            self.satZenith = np.concatenate(
                (self.satZenith, ary_satz_idx / 100.))

        # 太阳方位角 天顶角
        ary_suna_idx = np.full(dshape, np.nan)
        condition = np.logical_and(ary_suna > -18000, ary_suna < 18000)
        ary_suna_idx[condition] = ary_suna[condition]

        if self.sunAzimuth == []:
            self.sunAzimuth = ary_suna_idx / 100.
        else:
            self.sunAzimuth = np.concatenate(
                (self.sunAzimuth, ary_suna_idx / 100.))

        ary_sunz_idx = np.full(dshape, np.nan)
        condition = np.logical_and(ary_sunz > 0, ary_sunz < 18000)
        ary_sunz_idx[condition] = ary_sunz[condition]

        if self.sunZenith == []:
            self.sunZenith = ary_sunz_idx / 100.
        else:
            self.sunZenith = np.concatenate(
                (self.sunZenith, ary_sunz_idx / 100.))

        self.get_extract_data()