Example #1
0
    def test_reprojectDataset(self):
        """Test a dataset is correctly reprojected"""

        pM.reprojectDataset(self.testRasterFile, self.projectedDataset,
                            self.reprojectRaster)
        assert exists(self.reprojectRaster)
        prjDataset = gdal.Open(self.reprojectRaster)
        prjBand = prjDataset.GetRasterBand(1)
        prj_data = prjBand.ReadAsArray()
        # Check shape of arrays:
        self.assertEqual((2000, 5000), prj_data.shape)

        # Check geographic transform:
        self.assertEqual(prjDataset.GetGeoTransform(),
                         self.projectedDataset.GetGeoTransform())
        # Check projection: FIXME GetProjection() from projected dataset
        # drops AXIS["X",EAST],AXIS["Y",NORTH], from the projection
        # information compared to the match dataset.
        # self.assertEqual(prjDataset.GetProjection(),
        #                 self.projectedDataset.GetProjection())
        # Check values are correctly mapped:

        del prjDataset
Example #2
0
    def test_reprojectDataset_same_nc_img(self):
        """Test a dataset is correctly reprojected"""
        # Write a .nc file to test
        # This is the gust file
        f_nc = tempfile.NamedTemporaryFile(suffix='.nc',
                                        prefix='test_processMultipliers',
                                        delete=False)
        f_nc.close()


        # Write an .img file to test
        #This is the multiplier file
        f_img = tempfile.NamedTemporaryFile(suffix='.img',
                                        prefix='test_processMultipliers',
                                        delete=False)
        f_img.close()
        
        # Write a temporary track file
        f_track = tempfile.NamedTemporaryFile(suffix='.nc',
                                        prefix='test_track',
                                        delete=False)
        f_track.close()

        lat = np.asarray([ -23, -20, -17, -14, -11, -8, -5])
        lon = np.asarray([137, 140, 143, 146, 149, 152, 155, 158])

        delta = lon[1] - lon[0]
        lon_mid = lon + delta / 2.
        lat_mid = lat + delta / 2.
        speed = np.zeros(([lon.shape[0], lat.shape[0]]))
        speed.fill(42.5)

        # doing this just to get values in
        Vx = Vy = P = speed

        result = lat_mid, lon_mid, speed, Vx, Vy, P
        cfg = ConfigParser()
        wg = WindfieldGenerator(cfg)
        wg.saveGustToFile(f_track.name, result, f_nc.name)
        # nctools.ncSaveGrid(multiplier_name, multiplier_values, lat,
        #               lon, f_nc.name)

        # For the multiplier file
        lat = np.asarray([ -17, -14, -11])
        lon = np.asarray([140, 143, 146, 149])
        speed = np.zeros(([lon.shape[0], lat.shape[0]]))
        dx = 3.0
        dy = -3.0
        speed.fill(27.1701)
        pM.createRaster(speed, lon, lat,
                        dx, dy,
                        filename=f_img.name)
        speed.fill(42.5)
        m4_max_file = f_img.name
        # pulling out a section of the processMultipliers.main
        # load the wind data
        ncobj = Dataset(f_nc.name, 'r')

        lat = ncobj.variables['lat'][:]
        lon = ncobj.variables['lon'][:]

        delta = lon[1] - lon[0]
        lon = lon - delta / 2.
        lat = lat - delta / 2.

        # Wind speed:
        wspd = ncobj.variables['vmax'][:]
        wind_raster_file = 'region_wind.tif'
        wind_prj_file = 'gust_prj.tif'

        wind_raster = pM.createRaster(wspd, lon, lat, delta, -delta,
                                   filename=wind_raster_file)
        # This shows that the right data is going in
        src_data = wind_raster.GetRasterBand(1).ReadAsArray()
        m4_max = gdal.Open(m4_max_file, GA_ReadOnly)
        m4_max_data = m4_max.GetRasterBand(1).ReadAsArray()

        pM.reprojectDataset(wind_raster, m4_max_file,
                            wind_prj_file) #,
                            # match_projection=32756)

        wind_prj_ds = gdal.Open(wind_prj_file, GA_ReadOnly)
        wind_prj = wind_prj_ds.GetRasterBand(1)

        wind_data = wind_prj.ReadAsArray()

        # The info here is correct.
        #print "img data", pM.loadRasterFile(f_img.name)

        assert_almost_equal(wind_data, speed)

        keep = False
        ncobj.close()
        del m4_max
        if keep:
            print "f_nc.name", f_nc.name
            print "f_img.name", f_img.name
        else:
            os.remove(f_nc.name)
            os.remove(f_img.name)