Esempio n. 1
0
 def test_raster_no_Polygon(self):
     lon = np.random.random((10, 20))
     lat = np.random.random((10, 20))
     R = Raster(lon, lat)
     with self.assertRaises(ValueError):
         P = np.arange(10)
         R._rasterize_single_polygon(P)
Esempio n. 2
0
 def test_raster_no_Polygon(self):
     lon = np.random.random((10,20))
     lat = np.random.random((10,20))
     R = Raster(lon, lat)
     with self.assertRaises(ValueError):
         P = np.arange(10)
         R._rasterize_single_polygon(P)
Esempio n. 3
0
 def test_raster_multiple_polygon(self):  # this is quite slow!
     lon = np.linspace(-180., 180., 361)
     lat = np.linspace(-90., 90., 181)
     LON, LAT = np.meshgrid(lon, lat)
     #~ #~
     # test a single polygon
     poly = []
     poly1 = [(-10., -10.), (-10., 20), (15., 0.), (0., -15.)]
     poly.append(Polygon(1, poly1))
     #~ #~
     poly2 = [(-50., -80.), (-50., -70.), (-40., -70.), (-40., -75.)]
     poly.append(Polygon(2, poly2))
     #~ #~
     R = Raster(LON, LAT)
     R.rasterize_polygons(poly)
     #~ #~
     u = np.unique(R.mask[~R.mask.mask])
     self.assertTrue(len(u) == 2)
     self.assertTrue(1 in u)
     self.assertTrue(2 in u)
Esempio n. 4
0
    def test_raster_multiple_polygon(self):  # this is quite slow!
        lon = np.linspace(-180., 180., 361)
        lat = np.linspace(-90., 90., 181)
        LON,LAT=np.meshgrid(lon, lat)
#~ #~
        # test a single polygon
        poly=[]
        poly1 = [(-10.,-10.), (-10.,20), (15.,0.), (0.,-15.)]
        poly.append(Polygon(1, poly1))
#~ #~
        poly2 = [(-50.,-80.), (-50.,-70.), (-40.,-70.), (-40.,-75.)]
        poly.append(Polygon(2, poly2))
#~ #~
        R=Raster(LON,LAT)
        R.rasterize_polygons(poly)
#~ #~
        u = np.unique(R.mask[~R.mask.mask])
        self.assertTrue(len(u)==2)
        self.assertTrue(1 in u)
        self.assertTrue(2 in u)
Esempio n. 5
0
    def test_raster_single_polygon(self):
        lon = np.linspace(-180., 180., 361)
        lat = np.linspace(-90., 90., 181)
        LON,LAT=np.meshgrid(lon, lat)

        # test a single polygon
        poly = [(-10.,-10.), (-10.,20), (15.,0.), (0.,-25.)]
        P = Polygon(5, poly)
        R=Raster(LON,LAT)
        R.mask = np.zeros(LON.shape)*np.nan
        R._rasterize_single_polygon(P)

        print np.unique(R.mask)

        R.mask = np.ma.array(R.mask, mask=np.isnan(R.mask))

        u = np.unique(R.mask[~R.mask.mask])

        print R.mask
        print u

        self.assertTrue(len(u)==1)
        self.assertTrue(5. in u)
Esempio n. 6
0
    def test_raster_single_polygon(self):
        lon = np.linspace(-180., 180., 361)
        lat = np.linspace(-90., 90., 181)
        LON, LAT = np.meshgrid(lon, lat)

        # test a single polygon
        poly = [(-10., -10.), (-10., 20), (15., 0.), (0., -25.)]
        P = Polygon(5, poly)
        R = Raster(LON, LAT)
        R.mask = np.zeros(LON.shape) * np.nan
        R._rasterize_single_polygon(P)

        print np.unique(R.mask)

        R.mask = np.ma.array(R.mask, mask=np.isnan(R.mask))

        u = np.unique(R.mask[~R.mask.mask])

        print R.mask
        print u

        self.assertTrue(len(u) == 1)
        self.assertTrue(5. in u)
Esempio n. 7
0
 def test_raster_wrong_latlon(self):
     lon = np.random.random(10)
     lat = np.random.random(10)
     print lon.ndim
     with self.assertRaises(ValueError):
         R = Raster(lon, lat)
Esempio n. 8
0
 def test_raster_wrong_geometry(self):
     lon = np.random.random((10, 20))
     lat = np.random.random((11, 20))
     with self.assertRaises(ValueError):
         R = Raster(lon, lat)
Esempio n. 9
0
    def _import_regional_file(self, region_file, varname, targetgrid=None, logfile=None):
        """
        check if the regional file can be either imported or if
        regions are provided as vector data. In the latter case
        the regions are rasterized and results are stored in a netCDF
        file

        Parameters
        ----------
        region_file : str
            name of file defining the region. This is either a netCDF
            file which contains the mask as different integer values
            or it is a *.reg file which contains the regions as
            vector data.
        varname : str
            name of variable in netCDF file
        targetgrid : str
            name of targetgrid; either 't63grid' or the name of a file
            with a valid geometry

        Returns
        -------
            region_filename, region_file_varname
        """

        if not os.path.exists(region_file):
            raise ValueError('ERROR: region file is not existing: ' + region_file)

        ext = os.path.splitext(region_file)[1]
        if ext == '.nc':
            # netCDF file was given. Try to read variable
            if varname is None:
                raise ValueError('ERROR: no variable name given!')
            try:
                tmp = Data(region_file, varname, read=True)
            except:
                raise ValueError('ERROR: the regional masking file can not be read!')
            del tmp

            # everything is fine
            return region_file, varname

        elif ext == '.reg':
            # regions were given as vector files. Read it and
            # rasterize the data and store results in a temporary
            # file
            import tempfile

            if targetgrid is None:
                raise ValueError('ERROR: targetgrid needs to be specified for vectorization of regions!')

            if targetgrid == 't63grid':
                ls_mask = get_T63_landseamask(True, area='global', mask_antarctica=False)
            else:
                ls_mask = get_generic_landseamask(True, area='global', target_grid=targetgrid,
                                                  mask_antarctica=False)

            # temporary netCDF filename
            region_file1 = tempfile.mktemp(prefix='region_mask_', suffix='.nc')
            R = RegionParser(region_file)  # read region vector data
            M = Raster(ls_mask.lon, ls_mask.lat)
            polylist = []
            if logfile is not None:
                logf = open(logfile, 'w')
            else:
                logf = None

            id = 1
            for k in R.regions.keys():
                reg = R.regions[k]
                polylist.append(pycmbsPolygon(id, zip(reg.lon, reg.lat)))
                if logf is not None:  # store mapping table
                    logf.write(k + '\t' + str(id) + '\n')
                id += 1

            M.rasterize_polygons(polylist)
            if logf is not None:
                logf.close()

            # generate dummy output file
            O = Data(None, None)
            O.data = M.mask
            O.lat = ls_mask.lat
            O.lon = ls_mask.lon
            varname = 'regions'
            O.save(region_file1, varname=varname, format='nc', delete=True)
            print('Regionfile was store in file: %s' % region_file1)

            # check again that file is readable
            try:
                tmp = Data(region_file1, varname, read=True)
            except:
                print region_file1, varname
                raise ValueError('ERROR: the generated region file is not readable!')
            del tmp

            return region_file1, varname

        else:
            raise ValueError('ERROR: unsupported file type')