Пример #1
0
 def _fill_nan(self,):
     self._vza = np.array(parmap(fill_nan, list(self._vza)))
     self._vaa = np.array(parmap(fill_nan, list(self._vaa)))
     self._saa, self._sza, self._ele, self._aot, self._tcwv, self._tco3, self._aot_unc, self._tcwv_unc, self._tco3_unc = \
     parmap(fill_nan, [self._saa, self._sza, self._ele, self._aot, self._tcwv, self._tco3, self._aot_unc, self._tcwv_unc, self._tco3_unc])
     self._aot_unc = array_to_raster(self._aot_unc, self.example_file)
     self._tcwv_unc = array_to_raster(self._tcwv_unc, self.example_file)
     self._tco3_unc = array_to_raster(self._tco3_unc, self.example_file)
Пример #2
0
 def _fill_nan(self, ):
     self._vza = np.array(parmap(fill_nan, list(self._vza)))
     self._vaa = np.array(parmap(fill_nan, list(self._vaa)))
     self._saa, self._sza, self._ele, self._aot, self._tcwv, self._tco3 = parmap(
         fill_nan,
         [
             self._saa, self._sza, self._ele, self._aot, self._tcwv,
             self._tco3
         ],
     )
     self._aot = self._aot
     self._aot = np.maximum(self._aot, 0.01)
Пример #3
0
    def fire_gaus_optimize(self, ):
        xs, ys = self.fire_shift_optimize()
        if self.costs.min() < 0.1:
            min_val = [4, 4, -15, xs - 2, ys - 2]
            max_val = [40, 40, 15, xs + 2, ys + 2]
            self.bounds = [4, 40], [4, 40], [-15,
                                             15], [xs - 2,
                                                   xs + 2], [ys - 2, ys + 2]

            ps, distributions = create_training_set(self.parameters,
                                                    min_val,
                                                    max_val,
                                                    n_train=50)
            print('Start solving...')
            self.gaus_solved = parmap(self.gaus_optimize, ps, nprocs=5)
            result = np.array(
                [np.hstack((i[0], i[1])) for i in self.gaus_solved])
            print(
                'solved psf',
                dict(
                    zip(self.parameters + [
                        'cost',
                    ], result[np.argmin(result[:, -1])])))
            return result[np.argmin(result[:, -1]), :]
        else:
            print('Cost is too large, plese check!')
            return []
Пример #4
0
    def fire_shift_optimize(self, ):
        #self.S2_PSF_optimization()
        self._preprocess()
        if self.lh_mask.sum() == 0:
            self.costs = np.array([
                100000000000.,
            ])
            return 0, 0
        min_val = [-50, -50]
        max_val = [50, 50]
        ps, distributions = create_training_set(['xs', 'ys'],
                                                min_val,
                                                max_val,
                                                n_train=50)
        self.shift_solved = parmap(self.shift_optimize, ps)
        self.paras, self.costs = np.array([i[0] for i in self.shift_solved]), \
                                           np.array([i[1] for i in self.shift_solved])

        if (1 - self.costs.min()) >= 0.6:
            xs, ys = self.paras[self.costs == np.nanmin(self.costs)][0].astype(
                int)
        else:
            xs, ys = 0, 0
        #print 'Best shift is ', xs, ys, 'with the correlation of', 1-self.costs.min()
        return xs, ys
Пример #5
0
    def _get_convolved_toa(self, ):

        imgs = [band_g.ReadAsArray() for band_g in self._toa_bands]
        self.bad_pixs = self.bad_pix[self.hx, self.hy]
        if self.full_res[0] % 2 != 0:
            xgaus = np.exp(-2.0 * (np.pi**2) * (self.psf_xstd**2) *
                           ((0.5 * np.arange(self.full_res[0] + 1) /
                             (self.full_res[0] + 1))**2))
        else:
            xgaus = np.exp(
                -2.0 * (np.pi**2) * (self.psf_xstd**2) *
                ((0.5 * np.arange(self.full_res[0]) / self.full_res[0])**2))
        if self.full_res[1] % 2 != 0:
            ygaus = np.exp(-2.0 * (np.pi**2) * (self.psf_ystd**2) *
                           ((0.5 * np.arange(self.full_res[1] + 1) /
                             (self.full_res[1] + 1))**2))
        else:
            ygaus = np.exp(
                -2. * (np.pi**2) * (self.psf_ystd**2) *
                ((0.5 * np.arange(self.full_res[1]) / self.full_res[1])**2))
        gaus_2d = np.outer(xgaus, ygaus)
        par = partial(convolve, gaus_2d=gaus_2d, hx=self.hx, hy=self.hy)
        if np.array(self.ref_scale).ndim == 2:
            self.ref_scale = self.ref_scale[self.hx, self.hy]
        if np.array(self.ref_off).ndim == 2:
            self.ref_off = self.ref_off[self.hx, self.hy]
        self.toa = np.array(parmap(par, imgs)) * self.ref_scale + self.ref_off
Пример #6
0
    def _get_boa(self, temporal_filling=16):

        qa_temp = "MCD43_%s_BRDF_Albedo_Band_Mandatory_Quality_Band%d.vrt"
        da_temp = "MCD43_%s_BRDF_Albedo_Parameters_Band%d.vrt"
        doy = self.obs_time.strftime("%Y%j")
        if temporal_filling == True:
            temporal_filling = 16
        if temporal_filling:
            days = [(self.obs_time - timedelta(days=int(i)))
                    for i in np.arange(temporal_filling, 0, -1)
                    ] + [(self.obs_time + timedelta(days=int(i)))
                         for i in np.arange(0, temporal_filling + 1, 1)]
            fnames = []
            for temp in [da_temp, qa_temp]:
                for day in days:
                    for band in self.boa_bands:
                        fname = self.mcd43_dir + "/".join([
                            day.strftime("%Y-%m-%d"),
                            temp % (day.strftime("%Y%j"), band),
                        ])
                        fnames.append(fname)
        else:
            fnames = []
            for temp in [da_temp, qa_temp]:
                for band in self.boa_bands:
                    fname = self.MCD43_dir + "/".join([
                        datetime.strftime(self.obs_time, "%Y-%m-%d"),
                        temp % (doy, band),
                    ])
                    fnames.append(fname)
        das, ws = self._read_MCD43(fnames)
        mg = gdal.Warp(
            "",
            fnames[0],
            format="MEM",
            dstNodata=None,
            xRes=self.aero_res * 0.5,
            yRes=self.aero_res * 0.5,
            cutlineDSName=self.aoi,
            cropToCutline=True,
            resampleAlg=0,
        )
        hg = self.example_file
        self.hx, self.hy, hmask, rmask = self._get_index(mg, hg)

        No_band = len(self.toa_bands)
        mask = ~(mg.ReadAsArray()[0] == 0)
        self._annoying_angles(mg)
        sza = np.repeat(self._sza[None, ...], len(self._vza),
                        axis=0)[:, mask][:, hmask][:, rmask]
        saa = np.repeat(self._saa[None, ...], len(self._vza),
                        axis=0)[:, mask][:, hmask][:, rmask]
        angles = (
            self._vza[:, mask][:, hmask][:, rmask],
            sza,
            self._vaa[:, mask][:, hmask][:, rmask] - saa,
        )
        kk = get_kk(angles)
        k_vol = kk.Ross
        k_geo = kk.Li
        kers = np.array([np.ones(k_vol.shape), k_vol, k_geo])
        surs = []
        for i in range(No_band):
            surs.append(
                (das[i::No_band][:, :, mask][:, :, hmask][:, :, rmask] *
                 kers[:, i] * 0.001).sum(axis=1))
        if temporal_filling:
            boa = []
            w = []
            for i in range(No_band):
                das = surs[i]
                Ws = ws[i::No_band][:, mask][:, hmask][:, rmask]
                chunks = zip(np.array_split(das, 18, axis=1),
                             np.array_split(Ws, 18, axis=1))
                ret = parmap(smooth, chunks)
                _b = np.hstack([i[0] for i in ret])
                _w = np.hstack([i[1] for i in ret])
                boa.append(_b)
                w.append(_w)
            boa = np.array(boa)
            w = np.array(w)
            unc = 0.015 / w
        else:
            boa = np.array(surs)
            unc = 0.015 / ws
        self.boa = boa
        self.boa_unc = np.minimum(unc, 0.5)