def test_resample_bilinear(self): """Test whole bilinear resampling.""" from pyresample.bilinear import resample_bilinear # Single array res = resample_bilinear(self.data1, self.swath_def, self.target_def, 50e5, neighbours=32, nprocs=1) self.assertEqual(res.shape, self.target_def.shape) # There are 12 pixels with value 1, all others are zero self.assertEqual(res.sum(), 12) self.assertEqual((res == 0).sum(), 4) # Single array with masked output res = resample_bilinear(self.data1, self.swath_def, self.target_def, 50e5, neighbours=32, nprocs=1, fill_value=None) self.assertTrue(hasattr(res, 'mask')) # There should be 12 valid pixels self.assertEqual(self.target_def.size - res.mask.sum(), 12) # Two stacked arrays data = np.dstack((self.data1, self.data2)) res = resample_bilinear(data, self.swath_def, self.target_def) shp = res.shape self.assertEqual(shp[0:2], self.target_def.shape) self.assertEqual(shp[-1], 2)
def test_resample_bilinear(self): # Single array res = bil.resample_bilinear(self.data1, self.swath_def, self.target_def, 50e5, neighbours=32, nprocs=1) self.assertEqual(res.shape, self.target_def.shape) # There are 12 pixels with value 1, all others are zero self.assertEqual(res.sum(), 12) self.assertEqual((res == 0).sum(), 4) # Single array with masked output res = bil.resample_bilinear(self.data1, self.swath_def, self.target_def, 50e5, neighbours=32, nprocs=1, fill_value=None) self.assertTrue(hasattr(res, 'mask')) # There should be 12 valid pixels self.assertEqual(self.target_def.size - res.mask.sum(), 12) # Two stacked arrays data = np.dstack((self.data1, self.data2)) res = bil.resample_bilinear(data, self.swath_def, self.target_def) shp = res.shape self.assertEqual(shp[0:2], self.target_def.shape) self.assertEqual(shp[-1], 2)
def test_resample_bilinear(self): # Single array res = bil.resample_bilinear(self.data1, self.swath_def, self.target_def) self.assertEqual(res.size, self.target_def.size) # There should be only one pixel with value 1, all others are 0 self.assertEqual(res.sum(), 1) # Single array with masked output res = bil.resample_bilinear(self.data1, self.swath_def, self.target_def, fill_value=None) self.assertTrue(hasattr(res, 'mask')) # There should be only one valid pixel self.assertEqual(self.target_def.size - res.mask.sum(), 1) # Two stacked arrays data = np.dstack((self.data1, self.data2)) res = bil.resample_bilinear(data, self.swath_def, self.target_def) shp = res.shape self.assertEqual(shp[0], self.target_def.size) self.assertEqual(shp[1], 2)
def resample(self, target_geo_def): """Resamples image to area definition using bilinear approach Parameters ---------- target_geo_def : object Target geometry definition Returns ------- image_container : object ImageContainerBilinear object of resampled geometry """ if self.image_data.ndim > 2 and self.ndim > 1: image_data = self.image_data.reshape( self.image_data.shape[0] * self.image_data.shape[1], self.image_data.shape[2]) else: image_data = self.image_data.ravel() try: mask = image_data.mask.copy() image_data = image_data.data.copy() image_data[mask] = np.nan except AttributeError: pass resampled_image = \ bilinear.resample_bilinear(image_data, self.geo_def, target_geo_def, radius=self.radius_of_influence, neighbours=self.neighbours, epsilon=self.epsilon, fill_value=self.fill_value, nprocs=self.nprocs, reduce_data=self.reduce_data, segments=self.segments) try: resampled_image = resampled_image.reshape(target_geo_def.shape) except ValueError: # The input data was 3D shp = target_geo_def.shape new_shp = [shp[0], shp[1], image_data.shape[-1]] resampled_image = resampled_image.reshape(new_shp) return ImageContainerBilinear(resampled_image, target_geo_def, self.radius_of_influence, epsilon=self.epsilon, fill_value=self.fill_value, reduce_data=self.reduce_data, nprocs=self.nprocs, segments=self.segments)
def resample(self, target_geo_def): """Resamples image to area definition using bilinear approach Parameters ---------- target_geo_def : object Target geometry definition Returns ------- image_container : object ImageContainerBilinear object of resampled geometry """ if self.image_data.ndim > 2 and self.ndim > 1: image_data = self.image_data.reshape(self.image_data.shape[0] * self.image_data.shape[1], self.image_data.shape[2]) else: image_data = self.image_data.ravel() try: mask = image_data.mask.copy() image_data = image_data.data.copy() image_data[mask] = np.nan except AttributeError: pass resampled_image = \ bilinear.resample_bilinear(image_data, self.geo_def, target_geo_def, radius=self.radius_of_influence, neighbours=self.neighbours, epsilon=self.epsilon, fill_value=self.fill_value, nprocs=self.nprocs, reduce_data=self.reduce_data, segments=self.segments) try: resampled_image = resampled_image.reshape(target_geo_def.shape) except ValueError: # The input data was 3D shp = target_geo_def.shape new_shp = [shp[0], shp[1], image_data.shape[-1]] resampled_image = resampled_image.reshape(new_shp) return ImageContainerBilinear(resampled_image, target_geo_def, self.radius_of_influence, epsilon=self.epsilon, fill_value=self.fill_value, reduce_data=self.reduce_data, nprocs=self.nprocs, segments=self.segments)
def resample(self, target_geo_def): """Resamples image to area definition using bilinear approach. Parameters ---------- target_geo_def : object Target geometry definition Returns ------- image_container : object ImageContainerBilinear object of resampled geometry """ # import here, instead of the top of the script, to avoid xarray/zarr warning msg # while import the top level pyresample module from pyresample import bilinear image_data = self.image_data try: mask = image_data.mask.copy() image_data = image_data.data.copy() image_data[mask] = np.nan except AttributeError: pass resampled_image = \ bilinear.resample_bilinear(image_data, self.geo_def, target_geo_def, radius=self.radius_of_influence, neighbours=self.neighbours, epsilon=self.epsilon, fill_value=self.fill_value, nprocs=self.nprocs, reduce_data=self.reduce_data, segments=self.segments) return ImageContainerBilinear(resampled_image, target_geo_def, self.radius_of_influence, epsilon=self.epsilon, fill_value=self.fill_value, reduce_data=self.reduce_data, nprocs=self.nprocs, segments=self.segments)
def resample(self, target_geo_def): """Resamples image to area definition using bilinear approach Parameters ---------- target_geo_def : object Target geometry definition Returns ------- image_container : object ImageContainerBilinear object of resampled geometry """ image_data = self.image_data try: mask = image_data.mask.copy() image_data = image_data.data.copy() image_data[mask] = np.nan except AttributeError: pass resampled_image = \ bilinear.resample_bilinear(image_data, self.geo_def, target_geo_def, radius=self.radius_of_influence, neighbours=self.neighbours, epsilon=self.epsilon, fill_value=self.fill_value, nprocs=self.nprocs, reduce_data=self.reduce_data, segments=self.segments) return ImageContainerBilinear(resampled_image, target_geo_def, self.radius_of_influence, epsilon=self.epsilon, fill_value=self.fill_value, reduce_data=self.reduce_data, nprocs=self.nprocs, segments=self.segments)
def resample_WRF(self, st, et, delta): ''' Create Times variable and resample emission species DataArray. ''' # generate date every hour datetime_list = list(self.perdelta(st, et, timedelta(hours=1))) t_format = '%Y-%m-%d_%H:%M:%S' # convert datetime to date string Times = [] for timstep in datetime_list: times_str = strftime(t_format, timstep.timetuple()) Times.append(times_str) # the method of creating "Times" with unlimited dimension # ref: htttps://github.com/pydata/xarray/issues/3407 Times = xr.DataArray(np.array(Times, dtype=np.dtype(('S', 19)) ), dims=['Time']) self.chemi = xr.Dataset({'Times': Times}) # resample orig_def = SwathDefinition(lons=self.emi['longitude'], lats=self.emi['latitude']) for vname in self.emi.data_vars: if 'E_' in vname: logging.info(f'Resample {vname} ...') resampled_list = [] for t in range(self.emi[vname].shape[0]): # different resample methods # see: http://earthpy.org/interpolation_between_grids_with_pyresample.html if resample_method == 'nearest': resampled_list.append(resample_nearest( orig_def, self.emi[vname][t, :, :].values, self.area_def, radius_of_influence=self.radius_of_influence, fill_value=0.) ) elif resample_method == 'idw': resampled_list.append(resample_custom( orig_def, self.emi[vname][t, :, :].values, self.area_def, radius_of_influence=self.radius_of_influence, neighbours=10, weight_funcs=lambda r: 1/r**2, fill_value=0.) ) elif resample_method == 'bilinear': resampled_list.append(resample_bilinear( self.emi[vname][t, :, :].values, orig_def, self.area_def, radius=self.radius_of_influence, neighbours=10, nprocs=4, reduce_data=True, segments=None, fill_value=0., epsilon=0) ) # combine 2d array list to one 3d # ref: https://stackoverflow.com/questions/4341359/ # convert-a-list-of-2d-numpy-arrays-to-one-3d-numpy-array # we also need to flip the 3d array, # because of the "strange" order of 1d array in MEIC nc file # then add another dimension for zdim. resampled_data = np.flip(np.rollaxis(np.dstack(resampled_list), -1), 1)[:, np.newaxis, ...] # assign to self.chemi with dims self.chemi[vname] = xr.DataArray(resampled_data, dims=['Time', 'emissions_zdim', 'south_north', 'west_east' ] ) # add attrs needed by WRF-Chem v_attrs = {'FieldType': 104, 'MemoryOrder': 'XYZ', 'description': vname, 'stagger': '', 'coordinates': 'XLONG XLAT', 'units': self.emi[vname].attrs['units'] } self.chemi[vname] = self.chemi[vname].assign_attrs(v_attrs) logging.debug(' '*8 + ' min: ' + str(self.chemi[vname].min().values) + ' max: ' + str(self.chemi[vname].max().values) + ' mean ' + str(self.chemi[vname].mean().values) )
def atms( infile, gridding_radius=25000, ): ds = xr.open_dataset(infile) outEpsg = 3857 nd = -999 outProj = Proj(init='epsg:{0}'.format(outEpsg)) inProj = Proj(init='epsg:4326') lons, lats = ds.lon.values, ds.lat.values xx, yy = transform(inProj, outProj, lons, lats) minx, miny = transform(inProj, outProj, -180, -86) maxx, maxy = transform(inProj, outProj, 180, 86) res = 16000 eastings = np.arange(round(minx), round(maxx), res) northings = np.arange(round(miny), round(maxy), res) ee = eastings[np.where((eastings > xx.min()) & (eastings < xx.max()))] nn = northings[np.where((northings > yy.min()) & (northings < yy.max()))] swath_def = geometry.SwathDefinition(lons=lons, lats=lats) area_def = geometry.AreaDefinition( 'mercator', 'WGS 84 / Pseudo-Mercator - Projected', 'mercator', { 'x_0': '0.0', 'y_0': '0.0', 'lat_ts': '0.00', 'lon_0': '0.00', 'proj': 'merc', 'k': '1.0', 'datum': 'WGS84', 'ellps': 'WGS84', 'a': '6378137', 'b': '6378137' }, ee.size, nn.size, [ee.min(), nn.min(), ee.max(), nn.max()]) data = None # TODO: dynamically estimate sigama based on beam footprints eps = 0.1 result = bilinear.resample_bilinear( ds.land_frac.where(ds['sat_zen'] < 50).values, swath_def, area_def, radius=gridding_radius, neighbours=32, nprocs=1, fill_value=nd, reduce_data=True, segments=None, epsilon=eps) result[np.where( result >= 0)] = np.abs(result[np.where(result >= 0)] - 1) * 10000 name, _ = os.path.splitext(infile) outName = name + '_waterfrac.TIF' gt = (ee.min(), res, 0, nn.max(), 0, -res) writeGeotiff(outName, result, gt, outEpsg, noData=nd) return outName
def atms( infile, gridding_radius=25000, ): ds = xr.open_dataset(infile) outEpsg = 3857 nd = -999 outProj = Proj(init="epsg:{0}".format(outEpsg)) inProj = Proj(init="epsg:4326") lons, lats = ds.lon.values, ds.lat.values xx, yy = transform(inProj, outProj, lons, lats) minx, miny = transform(inProj, outProj, -180, -86) maxx, maxy = transform(inProj, outProj, 180, 86) res = 16000 eastings = np.arange(round(minx), round(maxx), res) northings = np.arange(round(miny), round(maxy), res) ee = eastings[np.where((eastings > xx.min()) & (eastings < xx.max()))] nn = northings[np.where((northings > yy.min()) & (northings < yy.max()))] swath_def = geometry.SwathDefinition(lons=lons, lats=lats) area_def = geometry.AreaDefinition( "mercator", "WGS 84 / Pseudo-Mercator - Projected", "mercator", { "x_0": "0.0", "y_0": "0.0", "lat_ts": "0.00", "lon_0": "0.00", "proj": "merc", "k": "1.0", "datum": "WGS84", "ellps": "WGS84", "a": "6378137", "b": "6378137", }, ee.size, nn.size, [ee.min(), nn.min(), ee.max(), nn.max()], ) data = None # TODO: dynamically estimate sigama based on beam footprints eps = 0.1 result = bilinear.resample_bilinear( ds.land_frac.where(ds["sat_zen"] < 50).values, swath_def, area_def, radius=gridding_radius, neighbours=32, nprocs=1, fill_value=nd, reduce_data=True, segments=None, epsilon=eps, ) result[np.where( result >= 0)] = np.abs(result[np.where(result >= 0)] - 1) * 10000 name, _ = os.path.splitext(infile) out_name = name + "_waterfrac.TIF" gt = (ee.min(), res, 0, nn.max(), 0, -res) write_geotiff(out_name, result, gt, outEpsg, no_data=nd) return out_name