Esempio n. 1
0
 def setUp(self):
     D = Data(None, None)
     D.data = np.random.random((10, 20))
     lon = np.arange(-10.,10.)  # -10 ... 9
     lat = np.arange(-60., 50., 2.)  # -60 ... 48
     D.lon, D.lat = np.meshgrid(lon, lat)
     self.x = D
 def setUp(self):
     D = Data(None, None)
     tmp = np.random.random((55, 20))
     D.data = np.ma.array(tmp, mask=tmp != tmp)
     lon = np.arange(-10., 10.)  # -10 ... 9
     lat = np.arange(-60., 50., 2.)  # -60 ... 48
     LON, LAT = np.meshgrid(lon, lat)
     D.lon = np.ma.array(LON, mask=LON != LON)
     D.lat = np.ma.array(LAT, mask=LAT != LAT)
     self.x = D
Esempio n. 3
0
 def setUp(self):
     D = Data(None, None)
     tmp = np.random.random((55, 20))
     D.data = np.ma.array(tmp, mask=tmp!=tmp)
     lon = np.arange(-10.,10.)  # -10 ... 9
     lat = np.arange(-60., 50., 2.)  # -60 ... 48
     LON, LAT = np.meshgrid(lon, lat)
     D.lon = np.ma.array(LON, mask=LON!=LON)
     D.lat = np.ma.array(LAT, mask=LAT!=LAT)
     self.x = D
    def test_rasterize_init(self):
        x = Data(None, None)
        x._init_sample_object(ny=1, nx=272)
        x.lon = np.random.random(272)*10. + 5.  # 5 ... 15
        x.lat = np.random.random(272)*20. + 0.  # 0 ... 20

        lon = np.random.random((10,20))
        lat = np.random.random((30,20))

        with self.assertRaises(ValueError):
            x._rasterize(lon, lat, radius=0.1)
        lon = np.random.random((10,20))
        lat = np.random.random((10,20))

        with self.assertRaises(ValueError):
            x._rasterize(lon, lat, radius=None)
Esempio n. 5
0
    def test_rasterize_init(self):
        x = Data(None, None)
        x._init_sample_object(ny=1, nx=272)
        x.lon = np.random.random(272) * 10. + 5.  # 5 ... 15
        x.lat = np.random.random(272) * 20. + 0.  # 0 ... 20

        lon = np.random.random((10, 20))
        lat = np.random.random((30, 20))

        with self.assertRaises(ValueError):
            x._rasterize(lon, lat, radius=0.1)
        lon = np.random.random((10, 20))
        lat = np.random.random((10, 20))

        with self.assertRaises(ValueError):
            x._rasterize(lon, lat, radius=None)
    def test_rasterize_data(self):
        """
        testdataset

        +---+---+---+
        |1.2|2.3|   |
        +---+---+---+
        |   |   |0.7|
        +---+---+---+
        |   |5.2|   |
        +---+---+---+
        """
        x = Data(None, None)
        x._init_sample_object(ny=1, nx=272)

        x.lon = np.asarray([2.25, 2.45, 1.8, 3.6])
        x.lat = np.asarray([11.9, 10.1, 10.2, 11.3])
        x.data = np.asarray([5.2, 2.3, 1.2, 0.7])

        # target grid
        lon = np.asarray([1.5, 2.5, 3.5])
        lat = np.asarray([10., 11., 12.])
        LON, LAT = np.meshgrid(lon, lat)

        # rasterize data

        # no valid data
        res = x._rasterize(LON, LAT, radius=0.000001, return_object=True)
        self.assertEqual(res.data.mask.sum(), np.prod(LON.shape))

        with self.assertRaises(ValueError):
            res = x._rasterize(LON, LAT, radius=0.000001, return_object=False)

        # check valid results
        res = x._rasterize(LON, LAT, radius=0.5, return_object=True)
        self.assertEqual(res.data[0,0], 1.2)
        self.assertEqual(res.data[0,1], 2.3)
        self.assertEqual(res.data[1,2], 0.7)
        self.assertEqual(res.ny*res.nx - res.data.mask.sum(), 4)
Esempio n. 7
0
    def test_rasterize_data(self):
        """
        testdataset

        +---+---+---+
        |1.2|2.3|   |
        +---+---+---+
        |   |   |0.7|
        +---+---+---+
        |   |5.2|   |
        +---+---+---+
        """
        x = Data(None, None)
        x._init_sample_object(ny=1, nx=272)

        x.lon = np.asarray([2.25, 2.45, 1.8, 3.6])
        x.lat = np.asarray([11.9, 10.1, 10.2, 11.3])
        x.data = np.asarray([5.2, 2.3, 1.2, 0.7])

        # target grid
        lon = np.asarray([1.5, 2.5, 3.5])
        lat = np.asarray([10., 11., 12.])
        LON, LAT = np.meshgrid(lon, lat)

        # rasterize data

        # no valid data
        res = x._rasterize(LON, LAT, radius=0.000001, return_object=True)
        self.assertEqual(res.data.mask.sum(), np.prod(LON.shape))

        with self.assertRaises(ValueError):
            res = x._rasterize(LON, LAT, radius=0.000001, return_object=False)

        # check valid results
        res = x._rasterize(LON, LAT, radius=0.5, return_object=True)
        self.assertEqual(res.data[0, 0], 1.2)
        self.assertEqual(res.data[0, 1], 2.3)
        self.assertEqual(res.data[1, 2], 0.7)
        self.assertEqual(res.ny * res.nx - res.data.mask.sum(), 4)
Esempio n. 8
0


#~ AMZ 7 (20.000S, 66.377W) (1.239S, 79.729W) (11.439N, 68.800W) (11.439N, 50.000W) (20.000S, 50.000W)
#~ CAM 6 (11.439N, 68.800W) (1.239S, 79.729W) (28.566N, 118.323W) (28.566N, 90.315W)


tmp = np.ones((180, 360))
d = Data(None, None)
d.data = np.ma.array(tmp, mask=tmp!=tmp)
d.cell_area = np.ones_like(tmp)


lon = np.arange(-180., 180.) + 0.5
lat = np.arange(-90., 90.) + 0.5
d.lon, d.lat = np.meshgrid(lon, lat)

# Basemap plots
m = SingleMap(d)  # this is supposed to make a baemap plot with stripes
m.backend = 'basemap'  # overwrite default
m._draw = m._draw_basemap
m.plot(polygons=[P1, P2], proj_prop={'projection':'robin', 'lon_0':0.}, vmin_polygons=0., vmax_polygons=250.)
plt.title('Basemap')

# cartopy plots
m1 = SingleMap(d, backend='cartopy')
m1.plot(polygons=[P1, P2], proj_prop={'projection':'robin', 'lon_0':0.}, vmin_polygons=0., vmax_polygons=250.)
plt.title('Cartopy')

plt.show()
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')
Esempio n. 10
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')