def test_lonlat_precomp(self): area_def = geometry.AreaDefinition('areaD', 'Europe (3km, HRV, VTC)', 'areaD', {'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere'}, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) lons, lats = area_def.get_lonlats() area_def2 = geometry.AreaDefinition('areaD', 'Europe (3km, HRV, VTC)', 'areaD', {'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere'}, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001], lons=lons, lats=lats) lon, lat = area_def.get_lonlat(400, 400) self.assertAlmostEqual(lon, 5.5028467120975835, msg='lon retrieval from precomputated grid failed') self.assertAlmostEqual(lat, 52.566998432390619, msg='lat retrieval from precomputated grid failed')
def goes_2_roi(loaded_goes, target_extent, target_rows, target_cols, cartopy_target_proj, data_key='Rad', radius_of_influence=50000): """Function that goes from loaded GOES data to data resampled in a projection for an extent""" dat = loaded_goes.metpy.parse_cf(data_key) geos_crs = dat.metpy.cartopy_crs cartopy_source_extent = geos_crs.x_limits + geos_crs.y_limits pyresample_source_extent = (cartopy_source_extent[0], cartopy_source_extent[2], cartopy_source_extent[1], cartopy_source_extent[3]) rad = dat.data source_area = geometry.AreaDefinition('GOES-1X', 'Full Disk', 'GOES-1X', geos_crs.proj4_params, rad.shape[1], rad.shape[0], pyresample_source_extent) area_target_def = geometry.AreaDefinition('areaTest', 'Target Region', 'areaTest', cartopy_target_proj.proj4_params, target_rows, target_cols, target_extent) geos_con_nn = image.ImageContainerNearest( rad, source_area, radius_of_influence=radius_of_influence) # Here we are using pyresample for the remapping area_proj_con_nn = geos_con_nn.resample(area_target_def) return area_proj_con_nn.image_data
def test_not_area_equal(self): area_def = geometry.AreaDefinition( 'areaD', 'Europe (3km, HRV, VTC)', 'areaD', { 'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere' }, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) msg_area = geometry.AreaDefinition( 'msg_full', 'Full globe MSG image 0 degrees', 'msg_full', { 'a': '6378169.0', 'b': '6356584.0', 'h': '35785831.0', 'lon_0': '0', 'proj': 'geos' }, 3712, 3712, [ -5568742.4000000004, -5568742.4000000004, 5568742.4000000004, 5568742.4000000004 ]) self.assertFalse(area_def == msg_area, 'area_defs are not expected to be equal')
def test_swath_equal_area(self): area_def = geometry.AreaDefinition( 'areaD', 'Europe (3km, HRV, VTC)', 'areaD', { 'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere' }, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) swath_def = geometry.SwathDefinition(*area_def.get_lonlats()) self.assertFalse(swath_def != area_def, "swath_def and area_def should be equal") area_def = geometry.AreaDefinition( 'areaD', 'Europe (3km, HRV, VTC)', 'areaD', { 'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere' }, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) self.assertFalse(area_def != swath_def, "swath_def and area_def should be equal")
def test_swath_not_equal_area(self): area_def = geometry.AreaDefinition( 'areaD', 'Europe (3km, HRV, VTC)', 'areaD', { 'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere' }, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) lons = np.array([1.2, 1.3, 1.4, 1.5]) lats = np.array([65.9, 65.86, 65.82, 65.78]) swath_def = geometry.SwathDefinition(lons, lats) self.assertFalse(swath_def == area_def, "swath_def and area_def should be different") area_def = geometry.AreaDefinition( 'areaD', 'Europe (3km, HRV, VTC)', 'areaD', { 'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere' }, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) self.assertFalse(area_def == swath_def, "swath_def and area_def should be different")
def test_get_lonlats(self): """Test get_lonlats on StackedAreaDefinition.""" area3 = geometry.AreaDefinition( "area3", 'area3', "geosmsg", { 'a': '6378169.0', 'b': '6356583.8', 'h': '35785831.0', 'lon_0': '0.0', 'proj': 'geos', 'units': 'm' }, 5568, 464, (3738502.0095458371, 3251436.5796920112, -1830246.0673044831, 2787374.2399544837)) # transition area4 = geometry.AreaDefinition( "area4", 'area4', "geosmsg", { 'a': '6378169.0', 'b': '6356583.8', 'h': '35785831.0', 'lon_0': '0.0', 'proj': 'geos', 'units': 'm' }, 5568, 464, (5567747.7409681147, 2787374.2399544837, -1000.3358822065015, 2323311.9002169576)) final_area = geometry.StackedAreaDefinition(area3, area4) self.assertEqual(len(final_area.defs), 2) lons, lats = final_area.get_lonlats() lons0, lats0 = final_area.defs[0].get_lonlats() lons1, lats1 = final_area.defs[1].get_lonlats() np.testing.assert_allclose(lons[:464, :], lons0) np.testing.assert_allclose(lons[464:, :], lons1) np.testing.assert_allclose(lats[:464, :], lats0) np.testing.assert_allclose(lats[464:, :], lats1)
def test_area_equal(self): area_def = geometry.AreaDefinition('areaD', 'Europe (3km, HRV, VTC)', 'areaD', {'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere'}, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) area_def2 = geometry.AreaDefinition('areaD', 'Europe (3km, HRV, VTC)', 'areaD', {'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere'}, 800, 800, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) self.assertFalse( area_def != area_def2, 'area_defs are not equal as expected')
def map_slice(lon_slice, lat_slice, data_slice): """ given a slice, return data mapped to a laea projection and the crs dicitonary for the projection Parameters ---------- lon_slice,lat_slice,data_slice: 2d arrays (float) 3 2-d arrays of the same shape giving longitudes, latitudes and data values for each pixel Returns ------- mapped_data: 2d array (float) 2-d array with same number of rows and columns as data_slice, projected to new coordinate system dst_crs: dict dictionary with keys needed by pyprojto create the destination projection """ llcrnrlat = lat_slice[-1, 0] llcrnrlon = lon_slice[-1, 0] urcrnrlat = lat_slice[0, -1] urcrnrlon = lon_slice[0, -1] src_crs = dict(units='m', proj='eqc', datum='WGS84') src_proj = pyproj.Proj(src_crs) llcrnrx, llcrnry = src_proj(llcrnrlon, llcrnrlat) urcrnrx, urcrnry = src_proj(urcrnrlon, urcrnrlat) src_extent = [llcrnrx, llcrnry, urcrnrx, urcrnry] src_height, src_width = data_slice.shape from_def = geometry.AreaDefinition('src', 'src image', 'area_src', src_crs, src_width, src_height, src_extent) lat_0 = (lat_slice[0, 0] + lat_slice[-1, 0]) / 2. lon_0 = (lon_slice[0, 0] + lon_slice[0, -1]) / 2. dst_crs = { 'datum': 'WGS84', 'lat_0': lat_0, 'lon_0': lon_0, 'proj': 'laea' } dst_proj = pyproj.Proj(dst_crs) llcrnrx, llcrnry = dst_proj(llcrnrlon, llcrnrlat) urcrnrx, urcrnry = dst_proj(urcrnrlon, urcrnrlat) dst_extent = [llcrnrx, llcrnry, urcrnrx, urcrnry] to_def = geometry.AreaDefinition('big', 'big image', 'area_big', dst_crs, src_width, src_height, dst_extent) from_nn = image.ImageContainerNearest(par_slice, from_def, radius_of_influence=50000) to_nn = from_nn.resample(to_def) mapped_data = to_nn.image_data return mapped_data, dst_crs
def test_nearest_neighbor_area_area(self): from pyresample import utils, geometry proj_str = "+proj=lcc +datum=WGS84 +ellps=WGS84 +lat_0=25 +lat_1=25 +lon_0=-95 +units=m +no_defs" proj_dict = pyresample.utils._proj4.proj4_str_to_dict(proj_str) extents = [0, 0, 1000. * 5000, 1000. * 5000] area_def = geometry.AreaDefinition('CONUS', 'CONUS', 'CONUS', proj_dict, 400, 500, extents) extents2 = [-1000, -1000, 1000. * 4000, 1000. * 4000] area_def2 = geometry.AreaDefinition('CONUS', 'CONUS', 'CONUS', proj_dict, 600, 700, extents2) rows, cols = utils.generate_nearest_neighbour_linesample_arrays(area_def, area_def2, 12000.)
def test_grid_filter(self): lons = np.array([-170, -30, 30, 170]) lats = np.array([20, -40, 50, -80]) swath_def = geometry.SwathDefinition(lons, lats) data = np.array([1, 2, 3, 4]) filter_area = geometry.AreaDefinition('test', 'test', 'test', { 'proj': 'eqc', 'lon_0': 0.0, 'lat_0': 0.0 }, 8, 8, (-20037508.34, -10018754.17, 20037508.34, 10018754.17)) filter = np.array([ [1, 1, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1], ]) grid_filter = geo_filter.GridFilter(filter_area, filter) swath_def_f, data_f = grid_filter.filter(swath_def, data) expected = np.array([1, 4]) self.assertTrue(np.array_equal(data_f, expected), 'Failed grid filtering data') expected_lons = np.array([-170, 170]) expected_lats = np.array([20, -80]) self.assertTrue( np.array_equal(swath_def_f.lons[:], expected_lons) and np.array_equal(swath_def_f.lats[:], expected_lats), 'Failed finding grid filtering lon lats')
def _geos_16_grid(dset): from pyresample import geometry from numpy import asarray projection = dset.goes_imager_projection h = projection.perspective_point_height a = projection.semi_major_axis b = projection.semi_minor_axis lon_0 = projection.longitude_of_projection_origin sweep = projection.sweep_angle_axis x = dset.x * h y = dset.y * h x_ll = x[0] # lower left corner x_ur = x[-1] # upper right corner y_ll = y[0] # lower left corner y_ur = y[-1] # upper right corner x_h = (x_ur - x_ll) / (len(x) - 1.) / 2. # 1/2 grid size y_h = (y_ur - y_ll) / (len(y) - 1.) / 2. # 1/2 grid size area_extent = (x_ll - x_h, y_ll - y_h, x_ur + x_h, y_ur + y_h) proj_dict = { 'a': float(a), 'b': float(b), 'lon_0': float(lon_0), 'h': float(h), 'proj': 'geos', 'units': 'm', 'sweep': sweep } area = geometry.AreaDefinition('GEOS_ABI', 'ABI', 'GOES_ABI', proj_dict, len(x), len(y), asarray(area_extent)) return area
def get_area_def(self, dsid): """Get area definition.""" if not self.is_geo: raise NotImplementedError("Don't know how to get the Area Definition for this file") platform = self.get_platform(self['/attr/Platform_Name']) res = self._calc_area_resolution(dsid.resolution) proj = self._get_proj(platform, float(self['/attr/Subsatellite_Longitude'])) area_name = '{} {} Area at {}m'.format( platform, self.metadata.get('sector_id', ''), int(res)) lon = self._load_nav('pixel_longitude') lat = self._load_nav('pixel_latitude') extents = self._get_extents(proj, res, lon, lat) area_def = geometry.AreaDefinition( area_name, area_name, area_name, proj4_str_to_dict(proj), lon.shape[1], lon.shape[0], area_extent=extents, ) return area_def
def _load_from_file(filename, satscene, calibrate): hdr, ftr, umarf, data = linear_load(filename) for channel in satscene.channels_to_load: mda = _get_metadata(hdr, ftr, channel) if channel == "HRV": dat = dec10to16(data["hrv"]["line_data"]).reshape((int(umarf["NumberLinesHRV"]))) * 1.0 else: dat = dec10to16(data["visir"]["line_data"][:, CHANNELS[channel], :]) * 1.0 print dat.min(), dat.max() uil = UImageLoader(mda, dat, mask=False, calibrate=calibrate) md, res = uil() satscene[channel] = np.ma.masked_equal(res, 0) proj_params = 'proj=geos lon_0=9.50 lat_0=0.00 a=6378169.00 b=6356583.80 h=35785831.00'.split() proj_dict = {} for param in proj_params: key, val = param.split("=") proj_dict[key] = val from pyresample import geometry satscene[channel].area = geometry.AreaDefinition( satscene.satname + satscene.instrument_name + str(md.area_extent) + str(res.shape), "On-the-fly area", proj_dict["proj"], proj_dict, res.shape[1], res.shape[0], md.area_extent)
def get_area_definition(pdict, a_ext): """Get the area definition for a geo-sat. Args: pdict: A dictionary containing common parameters: nlines: Number of lines in image ncols: Number of columns in image ssp_lon: Subsatellite point longitude (deg) a: Earth equatorial radius (m) b: Earth polar radius (m) h: Platform height (m) a_name: Area name a_desc: Area description p_id: Projection id a_ext: A four element tuple containing the area extent (scan angle) for the scene in radians Returns: a_def: An area definition for the scene """ proj_dict = { 'a': float(pdict['a']), 'b': float(pdict['b']), 'lon_0': float(pdict['ssp_lon']), 'h': float(pdict['h']), 'proj': 'geos', 'units': 'm' } a_def = geometry.AreaDefinition(pdict['a_name'], pdict['a_desc'], pdict['p_id'], proj_dict, int(pdict['ncols']), int(pdict['nlines']), a_ext) return a_def
def get_area_def(self, key, info=None): """ Projection information are hard coded for 0 degree geos projection Test dataset doen't provide the values in the file container. Only fill values are inserted """ # TODO Get projection information from input file a = 6378169. h = 35785831. b = 6356583.8 lon_0 = 0. #area_extent = (-5432229.9317116784, -5429229.5285458621, # 5429229.5285458621, 5432229.9317116784) area_extent = (-5570248.4773392612, -5567248.074173444, 5567248.074173444, 5570248.4773392612) proj_dict = { 'a': float(a), 'b': float(b), 'lon_0': float(lon_0), 'h': float(h), 'proj': 'geos', 'units': 'm' } area = geometry.AreaDefinition('LI_area_name', "LI area", 'geosli', proj_dict, self.ncols, self.nlines, area_extent) self.area = area logger.debug("Dataset area definition: \n {}".format(area)) return area
def get_area_def(self, dataset_id): a = self.mda['projection_parameters']['a'] b = self.mda['projection_parameters']['b'] h = self.mda['projection_parameters']['h'] lon_0 = self.mda['projection_parameters']['ssp_longitude'] proj_dict = { 'a': float(a), 'b': float(b), 'lon_0': float(lon_0), 'h': float(h), 'proj': 'geos', 'units': 'm' } if dataset_id.name == 'HRV': nlines = self.mda['hrv_number_of_lines'] ncols = self.mda['hrv_number_of_columns'] else: nlines = self.mda['number_of_lines'] ncols = self.mda['number_of_columns'] area = geometry.AreaDefinition('some_area_name', "On-the-fly area", 'geosmsg', proj_dict, ncols, nlines, self.get_area_extent(dataset_id)) return area
def area_def_from_dict(area_def_dict): """ given an dictionary produced by dump_area_def return a pyresample area_def introduced in level2_cartopy_plot Parameters ---------- area_def_dict: dict dictionary containing area_def parameters Returns ------- pyresample area_def object """ keys = [ 'area_id', 'proj_id', 'name', 'proj_dict', 'x_size', 'y_size', 'area_extent' ] arglist = [area_def_dict[key] for key in keys] area_def = geometry.AreaDefinition(*arglist) return area_def
def resample_it(lat, lon, data, file_str, channel, output_path="/home/off/HSAF_SRC/offline/H35/input"): date_ = datetime.datetime.strptime(file_str.split("_")[4], "%Y%m%d%H%M%S") date_str = date_.strftime("%Y%m%d") time_str = date_.strftime("%H%M") from pyresample import kd_tree, image, geometry _p_type = 'M01' area_def = geometry.AreaDefinition('a', 'b', 'c', {'proj': 'longlat'}, 35999, 8999, [-180, 0, 180, 90]) swath_def = geometry.SwathDefinition(lons=lon, lats=lat) swath_con = image.ImageContainerNearest(data, swath_def, radius_of_influence=5000) area_con = swath_con.resample(area_def) # area_con = kd_tree.resample_nearest(swath_def, data.ravel(), area_def, radius_of_influence = 5000, nprocs = 10) im_data = area_con.image_data with h5py.File( os.path.join( output_path, "eps_" + _p_type + "_" + date_str + "_" + time_str + "_" + time_str + '__' + str(channel) + '.hdf'), 'w') as h5file: h5file.create_dataset('/' + str(channel), shape=im_data.shape, dtype=im_data.dtype, data=im_data)
def _get_area_def(self): """Get the area definition of the band.""" cfac = np.int32(self.mda['cfac']) lfac = np.int32(self.mda['lfac']) coff = np.float32(self.mda['coff']) loff = self._get_line_offset() a = self.mda['projection_parameters']['a'] b = self.mda['projection_parameters']['b'] h = self.mda['projection_parameters']['h'] lon_0 = self.mda['projection_parameters']['SSP_longitude'] nlines = int(self.mda['number_of_lines']) ncols = int(self.mda['number_of_columns']) area_extent = self.get_area_extent((nlines, ncols), (loff, coff), (lfac, cfac), h) proj_dict = { 'a': float(a), 'b': float(b), 'lon_0': float(lon_0), 'h': float(h), 'proj': 'geos', 'units': 'm' } area = geometry.AreaDefinition(AREA_NAMES[self.area_id]['short'], AREA_NAMES[self.area_id]['long'], 'geosmsg', proj_dict, ncols, nlines, area_extent) return area
def project_pyresample(self): """ Reproject the satellite image on an equirectangular map using the pyresample library """ from pyresample import image, geometry from .satellites import pc self.load_image() x_size = self.data.shape[1] y_size = self.data.shape[0] proj_dict = {'a': '6378137.0', 'b': '6356752.3', 'lon_0': self.longitude, 'h': '35785831.0', 'proj': 'geos'} self.extent = 5568742.4 * 0.964 area_extent = (-self.extent, -self.extent, self.extent, self.extent) area = geometry.AreaDefinition('geo', 'geostat', 'geo', proj_dict, x_size, y_size, area_extent) dataIC = image.ImageContainerQuick(self.data, area) dataResampled = dataIC.resample(pc(self.outwidth, self.outheight)) dataResampledImage = self.rescale(dataResampled.image_data) dataResampledImage = self.polar_clouds(dataResampledImage) weight = self.get_weight() if self.debug: print("image max:", np.max(dataResampledImage)) result = np.array([dataResampledImage, weight]) return result
class NasaGSFCGridThickness(SourceGridBaseClass): # Area definition for pyresample source_areadef = geometry.AreaDefinition( 'nsidc_npstere', 'NSIDC North Polar Stereographic (25km)', 'nsidc_npstere', { 'lat_0': '90.00', 'lat_ts': '70.00', 'lon_0': '-45.00', 'proj': 'stere' }, 304, 448, [-3800000.0, -5350000.0, 3800000.0, 5850000.0]) def __init__(self, *args, **kwargs): super(NasaGSFCGridThickness, self).__init__(*args, **kwargs) self.read_nc() self.resample_sourcegrid_to_targetgrid() self.thickness = np.flipud(self.thickness) self.apply_target_grid_masks() def read_nc(self): data = ReadNC(self.filename) sit = data.sea_ice_thickness sit[np.where(sit < -999.)] = np.nan self.source_thickness = sit # np.flipud(sit)
class CS2SMOSGridThickness(SourceGridBaseClass): # Area definition for pyresample source_areadef = geometry.AreaDefinition( 'ease2', 'NSIDC North Polar Stereographic (25km)', 'ease2', { 'lat_0': '90.00', 'lat_ts': '70.00', 'lon_0': '0.00', 'proj': 'laea' }, 720, 720, [-9000000., -9000000., 9000000., 9000000.]) def __init__(self, *args, **kwargs): super(CS2SMOSGridThickness, self).__init__(*args, **kwargs) self.read_nc() self.resample_sourcegrid_to_targetgrid() self.apply_target_grid_masks() def read_nc(self): thickness_grids = [] n_files = len(self.filename) for filename in self.filename: data = ReadNC(filename) thickness_grids.append(data.analysis_thickness) average_thickness = thickness_grids[0] for i in np.arange(1, n_files): average_thickness += thickness_grids[i] average_thickness /= np.float(n_files) self.source_thickness = np.flipud(average_thickness)
def get_area_def(self, dsid): """Get the area definition of the dataset.""" try: from pyproj import CRS except ImportError: CRS = None geocoding = self.root.find('.//Tile_Geocoding') epsg = geocoding.find('HORIZONTAL_CS_CODE').text rows = int( geocoding.find('Size[@resolution="' + str(dsid['resolution']) + '"]/NROWS').text) cols = int( geocoding.find('Size[@resolution="' + str(dsid['resolution']) + '"]/NCOLS').text) geoposition = geocoding.find('Geoposition[@resolution="' + str(dsid['resolution']) + '"]') ulx = float(geoposition.find('ULX').text) uly = float(geoposition.find('ULY').text) xdim = float(geoposition.find('XDIM').text) ydim = float(geoposition.find('YDIM').text) area_extent = (ulx, uly + rows * ydim, ulx + cols * xdim, uly) if CRS is not None: proj = CRS(epsg) else: proj = {'init': epsg} area = geometry.AreaDefinition(self.tile, "On-the-fly area", self.tile, proj, cols, rows, area_extent) return area
def test_grid_filter2D(self): lons = np.array([[-170, -30, 30, 170], [-170, -30, 30, 170]]) lats = np.array([[20, -40, 50, -80], [25, -35, 55, -75]]) swath_def = geometry.SwathDefinition(lons, lats) data1 = np.ones((2, 4)) data2 = np.ones((2, 4)) * 2 data3 = np.ones((2, 4)) * 3 data = np.dstack((data1, data2, data3)) filter_area = geometry.AreaDefinition('test', 'test', 'test', { 'proj': 'eqc', 'lon_0': 0.0, 'lat_0': 0.0 }, 8, 8, (-20037508.34, -10018754.17, 20037508.34, 10018754.17)) filter = np.array([ [1, 1, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1], ]) grid_filter = geo_filter.GridFilter(filter_area, filter, nprocs=2) swath_def_f, data_f = grid_filter.filter(swath_def, data) expected = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) self.assertTrue(np.array_equal(data_f, expected), 'Failed 2D grid filtering data') expected_lons = np.array([-170, 170, -170, 170]) expected_lats = np.array([20, -80, 25, -75]) self.assertTrue( np.array_equal(swath_def_f.lons[:], expected_lons) and np.array_equal(swath_def_f.lats[:], expected_lats), 'Failed finding 2D grid filtering lon lats')
def test_boundary(self): area_def = geometry.AreaDefinition('areaD', 'Europe (3km, HRV, VTC)', 'areaD', {'a': '6378144.0', 'b': '6356759.0', 'lat_0': '50.00', 'lat_ts': '50.00', 'lon_0': '8.00', 'proj': 'stere'}, 10, 10, [-1370912.72, -909968.64000000001, 1029087.28, 1490031.3600000001]) proj_x_boundary, proj_y_boundary = area_def.proj_x_coords, area_def.proj_y_coords expected_x = np.array([-1250912.72, -1010912.72, -770912.72, -530912.72, -290912.72, -50912.72, 189087.28, 429087.28, 669087.28, 909087.28]) expected_y = np.array([1370031.36, 1130031.36, 890031.36, 650031.36, 410031.36, 170031.36, -69968.64, -309968.64, -549968.64, -789968.64]) self.assertTrue(np.allclose(proj_x_boundary, expected_x), 'Failed to find projection x coords') self.assertTrue(np.allclose(proj_y_boundary, expected_y), 'Failed to find projection y coords')
def _get_area_def(self, msg): """Get the area definition of the datasets in the file.""" proj_param = msg.projparams.copy() Rx = 2 * np.arcsin(1. / msg['NrInRadiusOfEarth']) / msg['dx'] Ry = 2 * np.arcsin(1. / msg['NrInRadiusOfEarth']) / msg['dy'] x_0 = -msg['XpInGridLengths'] x_1 = msg['Nx'] - msg['XpInGridLengths'] y_0 = (msg['Ny'] - msg['YpInGridLengths']) * -1 y_1 = msg['YpInGridLengths'] min_x = (x_0 * Rx) * proj_param['h'] max_x = (x_1 * Rx) * proj_param['h'] min_y = (y_0 * Ry) * proj_param['h'] max_y = (y_1 * Ry) * proj_param['h'] area_extent = (min_x, min_y, max_x, max_y) area = geometry.AreaDefinition('hsaf_region', 'A region from H-SAF', 'geos', proj_param, msg['Nx'], msg['Ny'], area_extent) return area
def get_area_def(self, dsid): a = self.mda['projection_parameters']['a'] b = self.mda['projection_parameters']['b'] h = self.mda['projection_parameters']['h'] lon_0 = self.mda['projection_parameters']['SSP_longitude'] nlines = int(self.mda['number_of_lines']) ncols = int(self.mda['number_of_columns']) proj_dict = { 'a': float(a), 'b': float(b), 'lon_0': float(lon_0), 'h': float(h), 'proj': 'geos', 'units': 'm' } area = geometry.AreaDefinition('some_area_name', "On-the-fly area", 'geosmsg', proj_dict, ncols, nlines, self.area_extent) self.area = area return area
def average(self, downscaling_factor=2, average_window=None): """ Makes a mean convolution of an image. :Parameters: `downscaling_factor` : int image downscaling factor, default is a factor 2. `average_window` : int window size for calculating mean values, default is the same as downscaling_factor. :Returns: `image` : GeoImage mean convoluted image. """ from mpop.imageo.geo_image import GeoImage from pyresample import geometry import scipy.ndimage as ndi self.check_channels(9.65) if average_window == None: average_window = downscaling_factor LOG.info("Downsampling a factor %d and averaging " % downscaling_factor + "in a window of %dx%d" % (average_window, average_window)) ch = self[9.65] # If average window and downscale factor is the same # the following could be used: # # data = data.reshape([shight, hight/shight, # swidth, width/swidth]).mean(3).mean(1) # avg kernel kernel = (np.ones((average_window, average_window), dtype=np.float) / (average_window * average_window)) # do convolution data = ndi.filters.correlate(ch.data.astype(np.float), kernel, mode='nearest') # downscale data = data[1::downscaling_factor, 1::downscaling_factor] # New area, and correct for integer truncation. p_size_x, p_size_y = (ch.area.pixel_size_x * downscaling_factor, ch.area.pixel_size_y * downscaling_factor) area_extent = (ch.area.area_extent[0], ch.area.area_extent[1], ch.area.area_extent[0] + data.shape[1] * p_size_x, ch.area.area_extent[1] + data.shape[0] * p_size_y) area = geometry.AreaDefinition( self._data_holder.satname + self._data_holder.instrument_name + str(area_extent) + str(data.shape), "On-the-fly area", ch.area.proj_id, ch.area.proj_dict, data.shape[1], data.shape[0], area_extent) return GeoImage(data, area, self.time_slot, fill_value=(0, ), mode='L')
def get_area_def(self, dsid): """Get the area definition of the band.""" cfac = np.int32(self.mda['cfac']) lfac = np.int32(self.mda['lfac']) coff = np.float32(self.mda['coff']) loff = np.float32(self.mda['loff']) a = self.mda['projection_parameters']['a'] b = self.mda['projection_parameters']['b'] h = self.mda['projection_parameters']['h'] lon_0 = self.mda['projection_parameters']['SSP_longitude'] nlines = int(self.mda['number_of_lines']) ncols = int(self.mda['number_of_columns']) area_extent = self.get_area_extent((nlines, ncols), (loff, coff), (lfac, cfac), h) proj_dict = { 'a': float(a), 'b': float(b), 'lon_0': float(lon_0), 'h': float(h), 'proj': 'geos', 'units': 'm' } area = geometry.AreaDefinition('some_area_name', "On-the-fly area", 'geosmsg', proj_dict, ncols, nlines, area_extent) self.area = area return area
def get_area_def(self, key, info): """ Projection information are hard coded for 0 degree geos projection Test dataset doen't provide the values in the file container. Only fill values are inserted """ # TODO Get projection information from input file a = 6378169. h = 35785831. b = 6356583.8 lon_0 = 0. area_extent = (-5432229.9317116784, -5429229.5285458621, 5429229.5285458621, 5432229.9317116784) proj_dict = { 'a': float(a), 'b': float(b), 'lon_0': float(lon_0), 'h': float(h), 'proj': 'geos', 'units': 'm' } area = geometry.AreaDefinition('Test_area_name', "Test area", 'geosli', proj_dict, self.ncols, self.nlines, area_extent) self.area = area return area