Пример #1
0
    def S2_cor(self, save=True):
        self.bands = 'B02', 'B03', 'B04', 'B08', 'B11', 'B12', 'B8A'
        self.Hfile = os.getcwd()+'/s_data/%s/%s/%s/%d/%d/%d/0/'%(self.S2_fname[:2], self.S2_fname[2],\
                                                                 self.S2_fname[3:5], self.year, self.S2_month, self.S2_day)
        #self.patch_pixs=300
        mete = readxml('%smetadata.xml' % self.Hfile)
        self.sza = np.zeros(7)
        self.sza[:] = mete['mSz']
        self.saa = self.sza.copy()
        self.saa[:] = mete['mSa']
        shape = (10000, 10000)
        # sometimes not all of the angles are available
        #try:
        #    self.vza = (mete['mVz'])[[1,2,3,7,11,12,8],]
        #    self.vaa = (mete['mVa'])[[1,2,3,7,11,12,8],]
        #except:
        #    self.vza = np.repeat(np.nanmean(mete['mVz']), 7)
        #    self.vaa = np.repeat(np.nanmean(mete['mVa']), 7)

        ll, ul, lr, ur = mg.toWgs(u'%s0000000000'%self.S2_fname), mg.toWgs(u'%s0000099999'%self.S2_fname),\
        mg.toWgs(u'%s9999900000'%self.S2_fname), mg.toWgs(u'%s9999999999'%self.S2_fname)

        self.dic = {
            'LL_LAT': ll[0],
            'LL_LON': ll[1],
            'LR_LAT': lr[0],
            'LR_LON': lr[1],
            'UL_LAT': ul[0],
            'UL_LON': ul[1],
            'UR_LAT': ur[0],
            'UR_LON': ur[1]
        }
        self.corners = 10000, 10000
        #if glob.glob(self.Hfile+'aot.tiff')==[]:
        #    self.aot_map, self.twv_map = self.S2_aot()
        #else:
        self.aot_map = tifffile.imread(self.Hfile + 'aot.tiff')
        self.twv_map = tifffile.imread(self.Hfile + 'twv.tiff')
        self.tco_map = tifffile.imread(self.Hfile + 'tco.tiff')
        gridx, gridy = np.mgrid[0:np.ceil(shape[0] / self.patch_pixs):1,
                                0:np.ceil(shape[1] / self.patch_pixs):1]

        self.emus = parallel_rw_pkl(None, '6S_emulation_S2_', 'r')
        self.inds = np.array(zip(gridx.ravel(), gridy.ravel()))
        self.S2_RSRs = pkl.load(open('pkls/S2_RSRs.pkl', 'r'))
        retval = parmap(self.S2_patch_correction, self.inds, nprocs=4)

        self.S2_cored = np.zeros((7, 10000, 10000))
        for _, ind in enumerate(self.inds):
            self.S2_cored[:,int(ind[0]*self.patch_pixs):int((ind[0]+1)*self.patch_pixs),\
                          int(ind[1]*self.patch_pixs):int((ind[1]+1)*self.patch_pixs)] = retval[_][1]
        if save:
            print 'Saving surface reflectance....'
            for i, band in enumerate(self.bands):
                tifffile.imsave(self.Hfile + 'sur_ref_band%s.tiff' % band,
                                self.S2_cored[i])
        return self.S2_cored
Пример #2
0
    def testNorthPoleCoordinates(self):
        self.assertEqual(mgrs.toMgrs(-88.52, -66.49), '  AYN4931665550')

        lat, lon = mgrs.toWgs('  AYN4931665550')
        self.assertAlmostEqual(lat, -88.51999757416547)
        self.assertAlmostEqual(lon, -66.49017323008184)

        lat, lon = mgrs.toWgs('    AYN4931665550')
        self.assertAlmostEqual(lat, -88.51999757416547)
        self.assertAlmostEqual(lon, -66.49017323008184)

        lat, lon = mgrs.toWgs('AYN4931665550')
        self.assertAlmostEqual(lat, -88.51999757416547)
        self.assertAlmostEqual(lon, -66.49017323008184)
Пример #3
0
    def testSouthPoleCoordinates(self):
        self.assertEqual(mgrs.toMgrs(86.598, -156.507), '  YYL4939146492')

        lat, lon = mgrs.toWgs('  YYL4939146492')
        self.assertAlmostEqual(lat, 86.59800323153932)
        self.assertAlmostEqual(lon, -156.50695504226658)

        lat, lon = mgrs.toWgs('    YYL4939146492')
        self.assertAlmostEqual(lat, 86.59800323153932)
        self.assertAlmostEqual(lon, -156.50695504226658)

        lat, lon = mgrs.toWgs('YYL4939146492')
        self.assertAlmostEqual(lat, 86.59800323153932)
        self.assertAlmostEqual(lon, -156.50695504226658)
Пример #4
0
    def processAlgorithm(self, progress):
        layer = dataobjects.getObjectFromUri(
                self.getParameterValue(self.INPUT))
        field = self.getParameterValue(self.FIELD)
        output = self.getOutputFromName(self.OUTPUT)

        idx = layer.fieldNameIndex(field)
        fields = layer.fields()

        epsg4326 = QgsCoordinateReferenceSystem('EPSG:4326')

        if QGis.QGIS_VERSION_INT < 29900:
            writer = output.getVectorWriter(fields, QGis.WKBPoint, epsg4326)
        else:
            from qgis.core import QgsWkbTypes
            writer = output.getVectorWriter(fields, QgsWkbTypes.Point, epsg4326)

        features = vector.features(layer)
        total = 100.0 / len(features)

        outFeat = QgsFeature()
        for current, feature in enumerate(features):
            try:
                mgrsCoord = feature[idx]
                y, x = mgrs.toWgs(mgrsCoord)
            except:
                pass

            pt = QgsPoint(x, y)
            outFeat.setGeometry(QgsGeometry.fromPoint(pt))
            outFeat.setAttributes(feature.attributes())
            writer.addFeature(outFeat)
            progress.setPercentage(int(current * total))

        del writer
Пример #5
0
    def testPopulatedPlaces(self):
        if os.environ.get('MGRSPY_TEST_PLACES', None) is None:
            self.skipTest('MGRSPY_TEST_PLACES env var not set')

        # Populated places from Natural Earth project (~2017, 1200+ places)
        with open('populated-places.csv') as populated_places:
            places = [p for p in csv.reader(populated_places, delimiter='\t')]

        lineno = 1
        for place in places:
            name = place[0]
            if name == 'NAMEASCII' or name.startswith('#'):
                log.warning('#{0} skipping'.format(lineno))
                lineno += 1
                continue
            lat = float(place[1])
            lon = float(place[2])
            mgr = place[3]
            zone = int(place[4])
            hemi = place[5].upper()
            easting = float(place[6])
            northing = float(place[7])
            epsg = int(place[8])

            log.warning('#{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}'.format(
                lineno, name, lat, lon, mgr, zone,
                hemi, easting, northing, epsg))

            # verify UTM zone/hemisphere, EPSG code
            hemi2, zone2, epsg2 = mgrs._epsgForWgs(lat, lon)
            self.assertEqual(hemi2, hemi)
            self.assertEqual(zone2, zone)
            self.assertEqual(epsg2, epsg)

            epsg3 = mgrs._epsgForUtm(0 if zone == 61 else zone, hemi)
            self.assertEqual(epsg3, epsg)

            # to MGRS
            self.assertEqual(mgrs.toMgrs(lat, lon).strip(), mgr)

            # # to WGS
            lat2, lon2 = mgrs.toWgs(mgr)
            # Test to only 4 places, as list generated by GeographicLib's
            #   GeoConvert, which may represent MGRS square coord by its center
            # TODO: Generate populated places MGRS using geotrans
            placement = 4
            if name.startswith('Amundsen'):
                # South Pole station that fails MGRS->WGS for placement > 1
                placement = 1
            self.assertAlmostEqual(lat2, lat, places=placement)
            self.assertAlmostEqual(lon2, lon, places=placement)

            lineno += 1
Пример #6
0
    def testWgsCoordinates(self):
        # to MGRS
        self.assertEqual(mgrs.toMgrs(42.0, -93.0), '15TVG0000049776')
        self.assertEqual(mgrs.toMgrs(42.0, -93.0, 5), '15TVG0000049776')
        self.assertEqual(mgrs.toMgrs(42.0, -93.0, 3), '15TVG000497')
        self.assertEqual(mgrs.toMgrs(42.0, -93.0, 0), '15TVG')

        self.assertEqual(mgrs.toMgrs(38.9072, -77.0369), '18SUJ2338308450')
        self.assertEqual(mgrs.toMgrs(39.9526, -75.1652), '18SVK8588822509')
        self.assertEqual(mgrs.toMgrs(37.6539, 44.0062), '38SMG1233767880')

        # to WGS
        lat, lon = mgrs.toWgs('15TVG0000049776')
        self.assertAlmostEqual(lat, 41.99364855788585)
        self.assertAlmostEqual(lon, -94.20734290469866)

        lat, lon = mgrs.toWgs('15TVG000497')
        self.assertAlmostEqual(lat, 41.54988934568494)
        self.assertAlmostEqual(lon, -94.19904899028688)

        lat, lon = mgrs.toWgs('15TVG')
        self.assertAlmostEqual(lat, 41.545413660388625)
        self.assertAlmostEqual(lon, -94.19896628704795)

        lat, lon = mgrs.toWgs('18SUJ2338308450')
        self.assertAlmostEqual(lat, 38.90719314018781)
        self.assertAlmostEqual(lon, -77.03690158268294)

        lat, lon = mgrs.toWgs('18SVK8588822509')
        self.assertAlmostEqual(lat, 39.95259667537377)
        self.assertAlmostEqual(lon, -75.16520969399382)

        lat, lon = mgrs.toWgs('38SMG1233767880')
        self.assertAlmostEqual(lat, 37.65389907949628)
        self.assertAlmostEqual(lon, 44.00619523636414)
Пример #7
0
 def zoomToPressed(self):
     mgrsCoord = str(self.leMgrsCoordinate.text()).strip()
     lat, lon = mgrs.toWgs(mgrsCoord)
     canvasCrs = self.canvas.mapSettings().destinationCrs()
     transform4326 = QgsCoordinateTransform(self.epsg4326, canvasCrs)
     center = transform4326.transform(lon, lat)
     self.canvas.zoomByFactor(1, center)
     self.canvas.refresh()
     if self.marker is None:
         self.marker = QgsVertexMarker(self.canvas)
     self.marker.setCenter(center)
     self.marker.setIconSize(8)
     self.marker.setPenWidth(4)
     self.btnRemoveMarker.setDisabled(False)
Пример #8
0
    def S2_PSF_optimization(self):
        self.h, self.v = mtile_cal(self.lat, self.lon)
        m = mgrs.MGRS()
        #mg_coor = m.toMGRS(self.lat, self.lon, MGRSPrecision=4)
        #self.place = self.S2_fname
        #self.Hfiles = glob.glob(directory +'l_data/LC8%03d%03d%d*LGN00_sr_band1.tif'%(self.path, self.row, self.year))
        if len(self.S2_fname) == 5:
            self.Hfile = os.getcwd()+'/s_data/%s/%s/%s/%d/%d/%d/0/'%(self.S2_fname[:2], self.S2_fname[2],\
                                                                     self.S2_fname[3:5], self.year, self.S2_month, self.S2_day)
        else:
            self.Hfile = os.getcwd()+'/s_data/%s/%s/%s/%d/%d/%d/0/'%(self.S2_fname[:1], self.S2_fname[1],\
                                                                     self.S2_fname[2:4], self.year, self.S2_month, self.S2_day)
        #Lfile = glob.glob('m_data/MCD43A1.A%d%03d.h%02dv%02d.006.*.hdf'%(year,doy,h,v))[0]
        #self.doy = datetime .datetime(self.year, self.month, self.day).timetuple().tm_yday
        self.Lfile = glob.glob('m_data/MCD43A1.A%d%03d.h%02dv%02d.006.*.hdf' %
                               (self.year, self.S2_doy, self.h, self.v))[0]

        if glob.glob(self.Hfile + 'cloud.tif') == []:
            cl = classification(fhead=self.Hfile,
                                bands=(2, 3, 4, 8, 11, 12, 13),
                                bounds=None)
            cl.Get_cm_p()
            self.cloud = cl.cm.copy()
            tifffile.imsave(self.Hfile + 'cloud.tif', self.cloud.astype(int))
            self.H_data = np.repeat(np.repeat(cl.b12, 2, axis=1), 2, axis=0)
            del cl
        else:
            b12 = gdal.Open(self.Hfile + 'B12.jp2').ReadAsArray() * 0.0001
            self.H_data = np.repeat(np.repeat(b12, 2, axis=1), 2, axis=0)
            self.cloud = tifffile.imread(self.Hfile + 'cloud.tif').astype(bool)
        cloud_cover = 1. * self.cloud.sum() / self.cloud.size
        cloud_cover = 1. * self.cloud.sum() / self.cloud.size
        if cloud_cover > 0.2:
            print 'Too much cloud, cloud proportion: %.03f !!' % cloud_cover
            return []
        else:
            mete = readxml('%smetadata.xml' % self.Hfile)
            self.sza = np.zeros(7)
            self.sza[:] = mete['mSz']
            self.saa = self.sza.copy()
            self.saa[:] = mete['mSa']
            try:
                self.vza = (mete['mVz'])[[1, 2, 3, 7, 11, 12, 8], ]
                self.vaa = (mete['mVa'])[[1, 2, 3, 7, 11, 12, 8], ]
            except:
                self.vza = np.repeat(np.nanmean(mete['mVz']), 7)
                self.vaa = np.repeat(np.nanmean(mete['mVa']), 7)

            ll, ul, lr, ur = mg.toWgs(u'%s0000000000'%self.S2_fname), mg.toWgs(u'%s0000099999'%self.S2_fname),\
            mg.toWgs(u'%s9999900000'%self.S2_fname), mg.toWgs(u'%s9999999999'%self.S2_fname)

            dic = {
                'LL_LAT': ll[0],
                'LL_LON': ll[1],
                'LR_LAT': lr[0],
                'LR_LON': lr[1],
                'UL_LAT': ul[0],
                'UL_LON': ul[1],
                'UR_LAT': ur[0],
                'UR_LON': ur[1]
            }
            corners = 10000, 10000

            #self.L_inds, self.H_inds = get_coords(self.lat,self.lon)
            self.L_inds, self.H_inds = MSL_geo_trans(self.lat, self.lon, dic,
                                                     corners)

            self.Lx, self.Ly = self.L_inds
            self.Hx, self.Hy = self.H_inds

            angles = (self.sza[-2], self.vza[-2], (self.vaa - self.saa)[-2])

            self.brdf, self.qa = get_brdf_six(self.Lfile,
                                              angles=angles,
                                              bands=(7, ),
                                              Linds=self.L_inds)
            self.brdf, self.qa = self.brdf.flatten(), self.qa.flatten()

            struct = ndimage.generate_binary_structure(2, 2)
            dia_cloud = ndimage.binary_dilation(self.cloud,
                                                structure=struct,
                                                iterations=60).astype(
                                                    self.cloud.dtype)

            mask = ~(self.H_data <= 0).astype('bool')
            small_mask = ndimage.binary_erosion(mask,
                                                structure=struct,
                                                iterations=60).astype(
                                                    mask.dtype)
            self.val_mask = (~dia_cloud) & small_mask

            self.L_data = np.zeros(self.brdf.shape[0])
            self.L_data[:] = np.nan
            self.L_data[self.qa == 0] = self.brdf[self.qa == 0]
            #args = s, self.L_data,

            avker = np.ones((120, 120))
            navker = avker / avker.sum()
            self.s = signal.fftconvolve(self.H_data, navker, mode='same')
            self.s[~self.val_mask] = np.nan

            min_val = [-100, -100]
            max_val = [100, 100]

            ps, distributions = create_training_set(['xs', 'ys'],
                                                    min_val,
                                                    max_val,
                                                    n_train=50)
            solved = parmap(self.op1, ps, nprocs=10)
            paras, costs = np.array([i[0] for i in solved
                                     ]), np.array([i[1] for i in solved])
            xs, ys = paras[costs == costs.min()][0]

            if costs.min() < 0.1:
                min_val = [5, 5, -15, xs - 5, ys - 5]
                max_val = [100, 100, 15, xs + 5, ys + 5]

                self.bounds = [5,
                               100], [5,
                                      100], [-15,
                                             15], [xs - 5,
                                                   xs + 5], [ys - 5, ys + 5]

                ps, distributions = create_training_set(
                    ['xstd', 'ystd', 'ang', 'xs', 'ys'],
                    min_val,
                    max_val,
                    n_train=50)
                print 'Start solving...'

                self.solved = parmap(self.op, ps, nprocs=10)

                print self.solved
                return self.solved, self.brdf, self.qa
            else:
                print 'Cost is too large, plese check!', xs, ys, costs.min()
                return []
Пример #9
0
    def S2_aot(self, ):
        self.wl = 0.490, 0.560, 0.665, 0.842, 1.610, 2.190, 0.865
        self.bands = 'B02', 'B03', 'B04', 'B08', 'B11', 'B12', 'B8A'
        m = mgrs.MGRS()
        mg_coor = m.toMGRS(self.lat, self.lon, MGRSPrecision=4)
        self.place = mg_coor[:5]

        self.Hfile = os.getcwd()+'/s_data/%s/%s/%s/%d/%d/%d/0/'%(self.S2_fname[:2], self.S2_fname[2],\
                                                                 self.S2_fname[3:5], self.year, self.S2_month, self.S2_day)

        self.Lfile = glob.glob(
            '%s/MCD43A1.A%d%03d.h%02dv%02d.006.*.hdf' %
            (self.mdata, self.year, self.S2_doy, self.h, self.v))[0]

        mete = readxml('%smetadata.xml' % self.Hfile)
        self.sza = np.zeros(7)
        self.sza[:] = mete['mSz']
        self.saa = self.sza.copy()
        self.saa[:] = mete['mSa']
        # sometimes not all of the angles are available
        try:
            self.vza = (mete['mVz'])[[1, 2, 3, 7, 11, 12, 8], ]
            self.vaa = (mete['mVa'])[[1, 2, 3, 7, 11, 12, 8], ]
        except:
            self.vza = np.repeat(np.nanmean(mete['mVz']), 7)
            self.vaa = np.repeat(np.nanmean(mete['mVa']), 7)

        ll, ul, lr, ur = mg.toWgs(u'%s0000000000'%self.S2_fname), mg.toWgs(u'%s0000099999'%self.S2_fname),\
        mg.toWgs(u'%s9999900000'%self.S2_fname), mg.toWgs(u'%s9999999999'%self.S2_fname)

        self.dic = {
            'LL_LAT': ll[0],
            'LL_LON': ll[1],
            'LR_LAT': lr[0],
            'LR_LON': lr[1],
            'UL_LAT': ul[0],
            'UL_LON': ul[1],
            'UR_LAT': ur[0],
            'UR_LON': ur[1]
        }
        self.corners = 10000, 10000

        #self.L_inds, self.H_inds = get_coords(self.lat,self.lon)
        self.L_inds, self.H_inds = MSL_geo_trans(self.lat, self.lon, self.dic,
                                                 self.corners)

        self.Lx, self.Ly = self.L_inds
        self.Hx, self.Hy = self.H_inds

        if glob.glob(self.Lfile + '_S2_aoi_brdf.pkl') == []:
            self.brdf, self.qa = get_brdf_six(
                self.Lfile, (self.sza, self.vza, self.vaa - self.saa),
                bands=[3, 4, 1, 2, 2, 6, 7],
                flag=None,
                Linds=self.L_inds)
            pkl.dump(np.array([self.brdf, self.qa]),
                     open(self.Lfile + '_S2_aoi_brdf.pkl', 'w'))

        else:
            self.brdf, self.qa = pkl.load(
                open(self.Lfile + '_S2_aoi_brdf.pkl', 'r'))

        if glob.glob(self.Hfile + 'cloud.tif') == []:
            cl = classification(fhead=self.Hfile,
                                bands=(2, 3, 4, 8, 11, 12, 13),
                                bounds=None)
            cl.Get_cm_p()
            self.cloud = cl.cm.copy()
            tifffile.imsave(self.Hfile + 'cloud.tif', self.cloud.astype(int))
            self.H_data = np.repeat(np.repeat(cl.b12, 2, axis=1), 2, axis=0)
            del cl
        else:
            self.cloud = tifffile.imread(self.Hfile + 'cloud.tif')

        struct = ndimage.generate_binary_structure(2, 2)
        self.dia_cloud = ndimage.binary_dilation(self.cloud.astype(bool),
                                                 structure=struct,
                                                 iterations=60).astype(bool)

        shape = (10000, 10000)
        xstd, ystd, angle, xs, ys = self.S2_psf[:5]
        self.shx, self.shy = (self.Hx + xs).astype(int), (self.Hy +
                                                          ys).astype(int)
        self.val = (self.Hx + xs < shape[0]) & (self.Hy + ys < shape[1]) & (
            self.Hx + xs > 0) & (self.Hy + ys > 0)
        self.ker = self.gaussian(xstd, ystd, angle, True)

        retval = parmap(self.S2_get_to_cor, self.bands, nprocs=len(self.bands))
        self.S2_mask = np.array(retval)[:, 1, :].astype(bool)
        self.S2_data = np.array(retval)[:, 0, :]

        Mcomb_mask = np.all(self.qa < 2, axis=0)
        Scomb_mask = np.all(self.S2_mask, axis=0)

        s2 = self.S2_data.copy()
        br = self.brdf.copy()
        s2[:, (~Scomb_mask) | (~Mcomb_mask[self.val])] = np.nan
        s2[np.isnan(s2)], br[np.isnan(br)] = -9999999, -9999999
        mas = np.all((br[:, self.val] > 0) & (br[:, self.val] < 1) & (s2 > 0) &
                     (s2 < 1),
                     axis=0)
        self.to_cor = self.shx[self.val][mas], self.shy[
            self.val][mas], s2[:, mas], br[:, self.val][:, mas]

        dif = self.to_cor[3] - self.to_cor[2]
        u, d = dif.mean(axis=1) + 3 * dif.std(axis=1), dif.mean(
            axis=1) - 3 * dif.std(axis=1)
        in_mask = np.all(np.array([(dif[i] > d[i]) & (dif[i] < u[i])
                                   for i in range(len(dif))]),
                         axis=0)

        self.to_cor = self.shx[self.val][mas][in_mask], self.shy[self.val][mas][in_mask],\
        s2[:,mas][:,in_mask], br[:,self.val][:,mas][:, in_mask]
        self.qa = magic**(self.qa[:, self.val][:, mas][:, in_mask])

        self.emus = parallel_rw_pkl(None, '6S_emulation_S2_', 'r')
        self.w = (np.array(self.wl))**(-self._alpha)
        self.w = self.w / (self.w.sum())

        self.patch_pixs = 300
        patches = []
        self.inds = []
        indx, indy = self.to_cor[:2]
        self.post_uncs = []

        for i in np.arange(0, np.ceil(shape[0] / self.patch_pixs)):
            for j in np.arange(0, np.ceil(shape[1] / self.patch_pixs)):
                patch_mask = (indx>i*self.patch_pixs) & (indx<(i+1)*self.patch_pixs)\
                & (indy>j*self.patch_pixs) & (indy<(j+1)*self.patch_pixs)
                if patch_mask.sum() == 0:
                    patches.append(([0, 0, 0], 0))
                    self.inds.append([i, j])
                else:
                    patches.append(self._S2_opt(i, j, patch_mask))
                    self.inds.append([i, j])

        self.inds = np.array(self.inds)
        paras = np.array([i[0] for i in patches])
        cost = np.array([i[1] for i in patches
                         ]).reshape(int(self.inds[:, 0].max() + 1),
                                    int(self.inds[:, 1].max() + 1))
        para_names = 'aot', 'twv', 'tco'
        masks = []
        para_maps = []
        smed_paras = []
        unc_maps = []

        for _ in range(3):
            mask = (np.array(paras[:, _]).reshape(cost.shape)
                    == 0) | np.isnan(cost)
            masks.append(mask)
            unc = np.array([
                np.r_[np.array([i[0], i[1]]), i[2][_]] for i in self.post_uncs
            ])
            unc_map = np.zeros_like(cost)
            unc_map[:] = np.nan
            unc_map[unc[:, 0].astype(int), unc[:, 1].astype(int)] = unc[:, 2]
            unc_maps.append(unc_map)
            w = np.zeros_like(cost)
            w[~mask] = 1. / unc_map[~mask]
            para_map = np.zeros_like(cost)
            para_map[mask] = paras[:, _].reshape(cost.shape)[~mask].mean()
            para_map[~mask] = paras[:, _].reshape(cost.shape)[~mask]
            para_maps.append(para_map)
            smed_para = smoothn(para_map, s=0.05, W=w**2, isrobust=True)[0]
            smed_paras.append(smed_para)
            tifffile.imsave(self.Hfile + '%s.tiff' % para_names[_], smed_para)

        self.masks, self.para_maps, self.unc_maps, self.smed_paras = masks, para_maps, unc_maps, smed_paras
        return unc_maps, smed_paras, para_maps
    def S2_aot(self):
        self.wl = 0.490, 0.560, 0.665, 0.842, 0.865, 1.610, 2.190
        self.bands = 'B02', 'B03', 'B04', 'B08', 'B8A', 'B11', 'B12'
        m = mgrs.MGRS()
        mg_coor = m.toMGRS(self.lat, self.lon, MGRSPrecision=4)
        self.place = mg_coor[:5]

        self.Hfile = os.getcwd() + '/s_data/%s/%s/%s/%d/%d/%d/0/' % (
            mg_coor[:2], mg_coor[2], mg_coor[3:5], self.year, self.month,
            self.day)
        self.Lfile = glob.glob('m_data/MCD43A1.A%d%03d.h%02dv%02d.006.*.hdf' %
                               (self.year, self.doy, self.h, self.v))[0]

        mete = readxml('%smetadata.xml' % self.Hfile)
        self.sza = np.zeros(7)
        self.sza[:] = mete['mSz']
        self.saa = self.sza.copy()
        self.saa[:] = mete['mSa']
        self.vza = (mete['mVz'])[[1, 2, 3, 7, 8, 11, 12], ]
        self.vaa = (mete['mVa'])[[1, 2, 3, 7, 8, 11, 12], ]

        self.L_inds, self.H_inds = get_coords(self.lat, self.lon)
        self.Lx, self.Ly = self.L_inds
        self.Hx, self.Hy = self.H_inds

        if glob.glob(self.Lfile + '_S2_aoi_brdf.pkl') == []:
            self.brdf, self.qa = get_brdf_six(
                self.Lfile, (self.sza, self.vza, self.vaa - self.saa),
                bands=[3, 4, 1, 2, 2, 6, 7],
                flag=None,
                Linds=self.L_inds)
            pkl.dump(np.array([self.brdf, self.qa]),
                     open(self.Lfile + '_S2_aoi_brdf.pkl', 'w'))

        else:
            self.brdf, self.qa = pkl.load(
                open(self.Lfile + '_S2_aoi_brdf.pkl', 'r'))

        if glob.glob(self.Hfile + 'cloud.tif') == []:
            cl = classification(fhead=self.Hfile,
                                bands=(2, 3, 4, 8, 11, 12, 13),
                                bounds=None)
            cl.Get_cm_p()
            self.cloud = cl.cm.copy()
            tifffile.imsave(self.Hfile + 'cloud.tif', self.cloud.astype(int))
            self.H_data = np.repeat(np.repeat(cl.b12, 2, axis=1), 2, axis=0)
            del cl
        else:
            self.cloud = tifffile.imread(self.Hfile + 'cloud.tif')

        struct = ndimage.generate_binary_structure(2, 2)
        self.dia_cloud = ndimage.binary_dilation(self.cloud.astype(bool),
                                                 structure=struct,
                                                 iterations=60).astype(bool)

        shape = (10000, 10000)
        xstd, ystd, angle, xs, ys = self.psf[:5]
        self.shx, self.shy = (self.Hx + xs).astype(int), (self.Hy +
                                                          ys).astype(int)
        self.val = (self.Hx + xs < shape[0]) & (self.Hy + ys < shape[1]) & (
            self.Hx + xs > 0) & (self.Hy + ys > 0)
        self.ker = self.gaussian(xstd, ystd, angle, True)

        retval = parmap(self.S2_get_to_cor, self.bands, nprocs=len(self.bands))
        self.S2_mask = np.array(retval)[:, 1, :].astype(bool)
        self.S2_data = np.array(retval)[:, 0, :]

        Mcomb_mask = np.all(self.qa == 0, axis=0)
        Scomb_mask = np.all(self.S2_mask, axis=0)

        s2 = self.S2_data.copy()
        br = self.brdf.copy()
        s2[:, (~Scomb_mask) | (~Mcomb_mask[self.val])] = np.nan
        s2[np.isnan(s2)], br[np.isnan(br)] = -9999999, -9999999
        mas = np.all((br[:, self.val] > 0) & (br[:, self.val] < 1) & (s2 > 0) &
                     (s2 < 1),
                     axis=0)
        self.to_cor = self.shx[self.val][mas], self.shy[
            self.val][mas], s2[:, mas], br[:, self.val][:, mas]

        self.S2_emus = pkl.load(open('6S_emulation_S2.pkl', 'r'))
        self.aot = read_net(self.year, self.month,self.day, np.arange(self.lat-2,self.lat+2, 0.125),\
                            np.arange(self.lon-3,self.lon+3, 0.125), dataset='aot')
        self.twv = read_net(self.year, self.month,self.day, np.arange(self.lat-2,self.lat+2, 0.125),\
                            np.arange(self.lon-3,self.lon+3, 0.125), dataset='wv')
        self.tco = read_net(self.year, self.month,self.day, np.arange(self.lat-2,self.lat+2, 0.125),\
                            np.arange(self.lon-3,self.lon+3, 0.125), dataset='tco')

        ll, ul, lr, ur = mg.toWgs(u'%s0000000000'%self.S2_fname), mg.toWgs(u'%s0000099999'%self.S2_fname),\
        mg.toWgs(u'%s9999900000'%self.S2_fname), mg.toWgs(u'%s9999999999'%self.S2_fname)

        dic = {
            'LL_LAT': ll[0],
            'LL_LON': ll[1],
            'LR_LAT': lr[0],
            'LR_LON': lr[1],
            'UL_LAT': ul[0],
            'UL_LON': ul[1],
            'UR_LAT': ur[0],
            'UR_LON': ur[1]
        }
        corners = 10000, 10000

        self.w = (np.array(self.wl) / self.wl[0])**(-self._alpha)
        self._lats, self._lons = np.arange(self.lat - 2, self.lat + 2,
                                           0.125), np.arange(
                                               self.lon - 3, self.lon + 3,
                                               0.125)
        this_lat, this_lon = cor_inter(
            np.array([self.to_cor[0], self.to_cor[1]]), dic, corners)
        self.eles = elevation(this_lat, this_lon) / 1000.

        ret = parmap(self._opt2, range(len(self.to_cor[0])), nprocs=10)
        parallel_rw_pkl(ret, 'S2_%s_%s_aot' % (self.place, self.doy), 'w')

        return ret
Пример #11
0
 def testSpecialCases(self):
     self.assertEqual(mgrs.toMgrs(-90, 180), '  BAN0000000000')
     lat, lon = mgrs.toWgs('BAN0000000000')
     self.assertAlmostEqual(lat, -90.0)
     self.assertAlmostEqual(lon, 0.0)