def get_angles(self, angle_id): """Get sun-satellite viewing angles""" tic = datetime.now() sunz40km = self._data["ang"][:, :, 0] * 1e-2 satz40km = self._data["ang"][:, :, 1] * 1e-2 azidiff40km = self._data["ang"][:, :, 2] * 1e-2 try: from geotiepoints.interpolator import Interpolator except ImportError: logger.warning("Could not interpolate sun-sat angles, " "python-geotiepoints missing.") self.sunz, self.satz, self.azidiff = sunz40km, satz40km, azidiff40km else: cols40km = np.arange(24, 2048, 40) cols1km = np.arange(2048) lines = sunz40km.shape[0] rows40km = np.arange(lines) rows1km = np.arange(lines) along_track_order = 1 cross_track_order = 3 satint = Interpolator( [sunz40km, satz40km, azidiff40km], (rows40km, cols40km), (rows1km, cols1km), along_track_order, cross_track_order) self.sunz, self.satz, self.azidiff = satint.interpolate() logger.debug("Interpolate sun-sat angles: time %s", str(datetime.now() - tic)) return create_xarray(getattr(self, ANGLES[angle_id]))
def test_fill_borders(self): lons = np.arange(20).reshape((4, 5), order="F") lats = np.arange(20).reshape((4, 5), order="C") lines = np.array([2, 7, 12, 17]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(20) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols), chunk_size=10) satint.fill_borders('x', 'y') self.assertTrue( np.allclose( satint.tie_data[0], np.array([[ -2.00000000e+00, -4.00000000e-01, 3.60000000e+00, 7.60000000e+00, 1.16000000e+01, 1.56000000e+01, 1.64000000e+01 ], [ -1.60000000e+00, 0.00000000e+00, 4.00000000e+00, 8.00000000e+00, 1.20000000e+01, 1.60000000e+01, 1.68000000e+01 ], [ -6.00000000e-01, 1.00000000e+00, 5.00000000e+00, 9.00000000e+00, 1.30000000e+01, 1.70000000e+01, 1.78000000e+01 ], [ -2.00000000e-01, 1.40000000e+00, 5.40000000e+00, 9.40000000e+00, 1.34000000e+01, 1.74000000e+01, 1.82000000e+01 ], [ 4.44089210e-16, 1.60000000e+00, 5.60000000e+00, 9.60000000e+00, 1.36000000e+01, 1.76000000e+01, 1.84000000e+01 ], [ 4.00000000e-01, 2.00000000e+00, 6.00000000e+00, 1.00000000e+01, 1.40000000e+01, 1.80000000e+01, 1.88000000e+01 ], [ 1.40000000e+00, 3.00000000e+00, 7.00000000e+00, 1.10000000e+01, 1.50000000e+01, 1.90000000e+01, 1.98000000e+01 ], [ 1.80000000e+00, 3.40000000e+00, 7.40000000e+00, 1.14000000e+01, 1.54000000e+01, 1.94000000e+01, 2.02000000e+01 ]]))) self.assertTrue( np.allclose(satint.row_indices, np.array([0, 2, 7, 9, 10, 12, 17, 19]))) self.assertTrue( np.allclose(satint.col_indices, np.array([0, 2, 7, 12, 17, 22, 23])))
def get_angles(self, angle_id): """Get sun-satellite viewing angles""" tic = datetime.now() sunz40km = self._data["ang"][:, :, 0] * 1e-2 satz40km = self._data["ang"][:, :, 1] * 1e-2 azidiff40km = self._data["ang"][:, :, 2] * 1e-2 try: from geotiepoints.interpolator import Interpolator except ImportError: logger.warning("Could not interpolate sun-sat angles, " "python-geotiepoints missing.") self.sunz, self.satz, self.azidiff = sunz40km, satz40km, azidiff40km else: cols40km = np.arange(24, 2048, 40) cols1km = np.arange(2048) lines = sunz40km.shape[0] rows40km = np.arange(lines) rows1km = np.arange(lines) along_track_order = 1 cross_track_order = 3 satint = Interpolator([sunz40km, satz40km, azidiff40km], (rows40km, cols40km), (rows1km, cols1km), along_track_order, cross_track_order) self.sunz, self.satz, self.azidiff = satint.interpolate() logger.debug("Interpolate sun-sat angles: time %s", str(datetime.now() - tic)) return create_xarray(getattr(self, ANGLES[angle_id]))
def test_results_in_c_order(self): lons = np.arange(10).reshape((2, 5), order="F") lats = np.arange(10).reshape((2, 5), order="C") lines = np.array([2, 7]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(10) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols)) interp_results = satint.interpolate() assert interp_results[0].flags['C_CONTIGUOUS'] is True
def test_extrapolate_cols(self): lons = np.arange(10).reshape((2, 5), order="F") lats = np.arange(10).reshape((2, 5), order="C") lines = np.array([2, 7]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(10) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols)) self.assertTrue(np.allclose(satint._extrapolate_cols(satint.tie_data[0]), np.array([[-0.8, 0., 2., 4., 6., 8., 8.4], [0.2, 1., 3., 5., 7., 9., 9.4]])))
def test_extrapolate_cols(self): lons = np.arange(10).reshape((2, 5), order="F") lats = np.arange(10).reshape((2, 5), order="C") lines = np.array([2, 7]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(10) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols)) self.assertTrue(np.allclose(satint._extrapolate_cols(satint.tie_data[0]), np.array([[-0.8, 0. , 2. , 4. , 6. , 8. , 8.4], [ 0.2, 1. , 3. , 5. , 7. , 9. , 9.4]])))
def test_extrapolate_rows(self): lons = np.arange(10).reshape((2, 5), order="F") lats = np.arange(10).reshape((2, 5), order="C") lines = np.array([2, 7]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(10) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols)) self.assertTrue( np.allclose( satint._extrapolate_rows(lons), np.array([[-0.4, 1.6, 3.6, 5.6, 7.6], [0., 2., 4., 6., 8.], [1., 3., 5., 7., 9.], [1.4, 3.4, 5.4, 7.4, 9.4]])))
def test_extrapolate_rows(self): lons = np.arange(10).reshape((2, 5), order="F") lats = np.arange(10).reshape((2, 5), order="C") lines = np.array([2, 7]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(10) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols)) self.assertTrue(np.allclose(satint._extrapolate_rows(lons), np.array([[-0.4, 1.6, 3.6, 5.6, 7.6], [ 0. , 2. , 4. , 6. , 8. ], [ 1. , 3. , 5. , 7. , 9. ], [ 1.4, 3.4, 5.4, 7.4, 9.4]])))
def test_fill_row_borders(self): lons = np.arange(20).reshape((4, 5), order="F") lats = np.arange(20).reshape((4, 5), order="C") lines = np.array([2, 7, 12, 17]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(20) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols)) satint._fill_row_borders() self.assertTrue( np.allclose( satint.tie_data[0], np.array([[-0.4, 3.6, 7.6, 11.6, 15.6], [0., 4., 8., 12., 16.], [1., 5., 9., 13., 17.], [2., 6., 10., 14., 18.], [3., 7., 11., 15., 19.], [3.4, 7.4, 11.4, 15.4, 19.4]]))) self.assertTrue( np.allclose(satint.row_indices, np.array([0, 2, 7, 12, 17, 19]))) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols), chunk_size=10) satint._fill_row_borders() self.assertTrue( np.allclose( satint.tie_data[0], np.array([[-0.4, 3.6, 7.6, 11.6, 15.6], [0., 4., 8., 12., 16.], [1., 5., 9., 13., 17.], [1.4, 5.4, 9.4, 13.4, 17.4], [1.6, 5.6, 9.6, 13.6, 17.6], [2., 6., 10., 14., 18.], [3., 7., 11., 15., 19.], [3.4, 7.4, 11.4, 15.4, 19.4]]))) self.assertTrue( np.allclose(satint.row_indices, np.array([0, 2, 7, 9, 10, 12, 17, 19])))
def get_angles(self, angle_id): """Get sun-satellite viewing angles.""" sunz40km = self._data["ang"][:, :, 0] * 1e-2 satz40km = self._data["ang"][:, :, 1] * 1e-2 azidiff40km = self._data["ang"][:, :, 2] * 1e-2 try: from geotiepoints.interpolator import Interpolator except ImportError: logger.warning("Could not interpolate sun-sat angles, " "python-geotiepoints missing.") self.sunz, self.satz, self.azidiff = sunz40km, satz40km, azidiff40km else: cols40km = np.arange(24, 2048, 40) cols1km = np.arange(2048) lines = sunz40km.shape[0] rows40km = np.arange(lines) rows1km = np.arange(lines) along_track_order = 1 cross_track_order = 3 satint = Interpolator( [sunz40km, satz40km, azidiff40km], (rows40km, cols40km), (rows1km, cols1km), along_track_order, cross_track_order) self.sunz, self.satz, self.azidiff = delayed(satint.interpolate, nout=3)() self.sunz = da.from_delayed(self.sunz, (lines, 2048), sunz40km.dtype) self.satz = da.from_delayed(self.satz, (lines, 2048), satz40km.dtype) self.azidiff = da.from_delayed(self.azidiff, (lines, 2048), azidiff40km.dtype) return create_xarray(getattr(self, ANGLES[angle_id]))
def get_dataset(self, key, info): """Load a dataset """ if key.name not in self.datasets: return if self.nc is None: self.nc = h5netcdf.File(self.filename, 'r') logger.debug('Reading %s.', key.name) variable = self.nc[self.datasets[key.name]] values = (np.ma.masked_equal(variable[:], variable.attrs['_FillValue'], copy=False) * variable.attrs.get('scale_factor', 1) + variable.attrs.get('add_offset', 0)) units = variable.attrs['units'] l_step = self.nc.attrs['al_subsampling_factor'] c_step = self.nc.attrs['ac_subsampling_factor'] if c_step != 1 or l_step != 1: from geotiepoints.interpolator import Interpolator tie_lines = np.arange( 0, (values.shape[0] - 1) * l_step + 1, l_step) tie_cols = np.arange(0, (values.shape[1] - 1) * c_step + 1, c_step) lines = np.arange((values.shape[0] - 1) * l_step + 1) cols = np.arange((values.shape[1] - 1) * c_step + 1) along_track_order = 1 cross_track_order = 3 satint = Interpolator([values], (tie_lines, tie_cols), (lines, cols), along_track_order, cross_track_order) (values, ) = satint.interpolate() proj = Dataset(values, copy=False, units=units, platform_name=self.platform_name, sensor=self.sensor) proj.info.update(key.to_dict()) return proj
def get_dataset(self, key, info): """Load a dataset """ if key.name not in self.datasets: return if self.nc is None: self.nc = h5netcdf.File(self.filename, 'r') logger.debug('Reading %s.', key.name) variable = self.nc[self.datasets[key.name]] values = (np.ma.masked_equal(variable[:], variable.attrs['_FillValue'], copy=False) * variable.attrs.get('scale_factor', 1) + variable.attrs.get('add_offset', 0)) units = variable.attrs['units'] l_step = self.nc.attrs['al_subsampling_factor'] c_step = self.nc.attrs['ac_subsampling_factor'] if c_step != 1 or l_step != 1: from geotiepoints.interpolator import Interpolator tie_lines = np.arange( 0, (values.shape[0] - 1) * l_step + 1, l_step) tie_cols = np.arange(0, (values.shape[1] - 1) * c_step + 1, c_step) lines = np.arange((values.shape[0] - 1) * l_step + 1) cols = np.arange((values.shape[1] - 1) * c_step + 1) along_track_order = 1 cross_track_order = 3 satint = Interpolator([values], (tie_lines, tie_cols), (lines, cols), along_track_order, cross_track_order) (values, ) = satint.interpolate() proj = Projectable(values, copy=False, units=units, platform_name=self.platform_name, sensor=self.sensor) return proj
def _create_40km_interpolator(lines, *arrays_40km): from geotiepoints.interpolator import Interpolator cols40km = np.arange(24, 2048, 40) cols1km = np.arange(2048) rows40km = np.arange(lines) rows1km = np.arange(lines) along_track_order = 1 cross_track_order = 3 satint = Interpolator(arrays_40km, (rows40km, cols40km), (rows1km, cols1km), along_track_order, cross_track_order) return satint
def test_fill_row_borders(self): lons = np.arange(20).reshape((4, 5), order="F") lats = np.arange(20).reshape((4, 5), order="C") lines = np.array([2, 7, 12, 17]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(20) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols)) satint._fill_row_borders() self.assertTrue(np.allclose(satint.tie_data[0], np.array([[ -0.4, 3.6, 7.6, 11.6, 15.6], [ 0. , 4. , 8. , 12. , 16. ], [ 1. , 5. , 9. , 13. , 17. ], [ 2. , 6. , 10. , 14. , 18. ], [ 3. , 7. , 11. , 15. , 19. ], [ 3.4, 7.4, 11.4, 15.4, 19.4]]))) self.assertTrue(np.allclose(satint.row_indices, np.array([ 0, 2, 7, 12, 17, 19]))) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols), chunk_size=10) satint._fill_row_borders() self.assertTrue(np.allclose(satint.tie_data[0], np.array([[ -0.4, 3.6, 7.6, 11.6, 15.6], [ 0. , 4. , 8. , 12. , 16. ], [ 1. , 5. , 9. , 13. , 17. ], [ 1.4, 5.4, 9.4, 13.4, 17.4], [ 1.6, 5.6, 9.6, 13.6, 17.6], [ 2. , 6. , 10. , 14. , 18. ], [ 3. , 7. , 11. , 15. , 19. ], [ 3.4, 7.4, 11.4, 15.4, 19.4]]))) self.assertTrue(np.allclose(satint.row_indices, np.array([ 0, 2, 7, 9, 10, 12, 17, 19])))
def test_fill_borders(self): lons = np.arange(20).reshape((4, 5), order="F") lats = np.arange(20).reshape((4, 5), order="C") lines = np.array([2, 7, 12, 17]) cols = np.array([2, 7, 12, 17, 22]) hlines = np.arange(20) hcols = np.arange(24) satint = Interpolator((lons, lats), (lines, cols), (hlines, hcols), chunk_size=10) satint.fill_borders('x', 'y') self.assertTrue(np.allclose(satint.tie_data[0], np.array([[ -2.00000000e+00, -4.00000000e-01, 3.60000000e+00, 7.60000000e+00, 1.16000000e+01, 1.56000000e+01, 1.64000000e+01], [ -1.60000000e+00, 0.00000000e+00, 4.00000000e+00, 8.00000000e+00, 1.20000000e+01, 1.60000000e+01, 1.68000000e+01], [ -6.00000000e-01, 1.00000000e+00, 5.00000000e+00, 9.00000000e+00, 1.30000000e+01, 1.70000000e+01, 1.78000000e+01], [ -2.00000000e-01, 1.40000000e+00, 5.40000000e+00, 9.40000000e+00, 1.34000000e+01, 1.74000000e+01, 1.82000000e+01], [ 4.44089210e-16, 1.60000000e+00, 5.60000000e+00, 9.60000000e+00, 1.36000000e+01, 1.76000000e+01, 1.84000000e+01], [ 4.00000000e-01, 2.00000000e+00, 6.00000000e+00, 1.00000000e+01, 1.40000000e+01, 1.80000000e+01, 1.88000000e+01], [ 1.40000000e+00, 3.00000000e+00, 7.00000000e+00, 1.10000000e+01, 1.50000000e+01, 1.90000000e+01, 1.98000000e+01], [ 1.80000000e+00, 3.40000000e+00, 7.40000000e+00, 1.14000000e+01, 1.54000000e+01, 1.94000000e+01, 2.02000000e+01]]))) self.assertTrue(np.allclose(satint.row_indices, np.array([ 0, 2, 7, 9, 10, 12, 17, 19]))) self.assertTrue(np.allclose(satint.col_indices, np.array([ 0, 2, 7, 12, 17, 22, 23])))
def _do_interpolate(self, data): if not isinstance(data, tuple): data = (data, ) shape = data[0].shape from geotiepoints.interpolator import Interpolator tie_lines = np.arange(0, (shape[0] - 1) * self.l_step + 1, self.l_step) tie_cols = np.arange(0, (shape[1] - 1) * self.c_step + 1, self.c_step) lines = np.arange((shape[0] - 1) * self.l_step + 1) cols = np.arange((shape[1] - 1) * self.c_step + 1) along_track_order = 1 cross_track_order = 3 satint = Interpolator([x.values for x in data], (tie_lines, tie_cols), (lines, cols), along_track_order, cross_track_order) int_data = satint.interpolate() return [ xr.DataArray(da.from_array(x, chunks=(CHUNK_SIZE, CHUNK_SIZE)), dims=['y', 'x']) for x in int_data ]
def __init__(self, lon_lat_data, *args, **kwargs): Interpolator.__init__(self, None, *args, **kwargs) self.lon_tiepoint = None self.lat_tiepoint = None try: # Maybe it's a pyresample object ? self.set_tiepoints(lon_lat_data.lons, lon_lat_data.lats) xyz = lon_lat_data.get_cartesian_coords() self.tie_data = [xyz[:, :, 0], xyz[:, :, 1], xyz[:, :, 2]] except AttributeError: self.set_tiepoints(lon_lat_data[0], lon_lat_data[1]) lons_rad = np.radians(self.lon_tiepoint) lats_rad = np.radians(self.lat_tiepoint) x__ = EARTH_RADIUS * np.cos(lats_rad) * np.cos(lons_rad) y__ = EARTH_RADIUS * np.cos(lats_rad) * np.sin(lons_rad) z__ = EARTH_RADIUS * np.sin(lats_rad) self.tie_data = [x__, y__, z__] self.new_data = [] for num in range(len(self.tie_data)): self.new_data.append([])
def interpolate(self): newx, newy, newz = Interpolator.interpolate(self) lon = get_lons_from_cartesian(newx, newy) lat = get_lats_from_cartesian(newx, newy, newz) return lon, lat
def get_dataset(self, key, info): """Load a dataset.""" if key.name not in self.datasets: return if self.nc is None: self.nc = h5netcdf.File(self.filename, 'r') logger.debug('Reading %s.', key.name) l_step = self.nc.attrs['al_subsampling_factor'] c_step = self.nc.attrs['ac_subsampling_factor'] if (c_step != 1 or l_step != 1) and key.name not in self.cache: if key.name.startswith('satellite'): zen, zattrs = self._get_scaled_variable( self.datasets['satellite_zenith_angle']) azi, aattrs = self._get_scaled_variable( self.datasets['satellite_azimuth_angle']) elif key.name.startswith('solar'): zen, zattrs = self._get_scaled_variable( self.datasets['solar_zenith_angle']) azi, aattrs = self._get_scaled_variable( self.datasets['solar_azimuth_angle']) else: raise NotImplementedError("Don't know how to read " + key.name) x, y, z = angle2xyz(azi, zen) shape = x.shape from geotiepoints.interpolator import Interpolator tie_lines = np.arange(0, (shape[0] - 1) * l_step + 1, l_step) tie_cols = np.arange(0, (shape[1] - 1) * c_step + 1, c_step) lines = np.arange((shape[0] - 1) * l_step + 1) cols = np.arange((shape[1] - 1) * c_step + 1) along_track_order = 1 cross_track_order = 3 satint = Interpolator([x, y, z], (tie_lines, tie_cols), (lines, cols), along_track_order, cross_track_order) ( x, y, z, ) = satint.interpolate() azi, zen = xyz2angle(x, y, z) if 'zenith' in key.name: values, attrs = zen, zattrs elif 'azimuth' in key.name: values, attrs = azi, aattrs else: raise NotImplementedError("Don't know how to read " + key.name) if key.name.startswith('satellite'): self.cache['satellite_zenith_angle'] = zen, zattrs self.cache['satellite_azimuth_angle'] = azi, aattrs elif key.name.startswith('solar'): self.cache['solar_zenith_angle'] = zen, zattrs self.cache['solar_azimuth_angle'] = azi, aattrs elif key.name in self.cache: values, attrs = self.cache[key.name] else: values, attrs = self._get_scaled_variable(self.datasets[key.name]) units = attrs['units'] proj = Dataset(values, copy=False, units=units, platform_name=self.platform_name, sensor=self.sensor) proj.info.update(key.to_dict()) return proj
def get_dataset(self, key, info): """Load a dataset.""" if key.name not in self.datasets: return if self.nc is None: self.nc = xr.open_dataset(self.filename, decode_cf=True, mask_and_scale=True, engine='h5netcdf', chunks={'tie_columns': CHUNK_SIZE, 'tie_rows': CHUNK_SIZE}) self.nc = self.nc.rename({'tie_columns': 'x', 'tie_rows': 'y'}) logger.debug('Reading %s.', key.name) l_step = self.nc.attrs['al_subsampling_factor'] c_step = self.nc.attrs['ac_subsampling_factor'] if (c_step != 1 or l_step != 1) and self.cache.get(key.name) is None: if key.name.startswith('satellite'): zen = self.nc[self.datasets['satellite_zenith_angle']] zattrs = zen.attrs azi = self.nc[self.datasets['satellite_azimuth_angle']] aattrs = azi.attrs elif key.name.startswith('solar'): zen = self.nc[self.datasets['solar_zenith_angle']] zattrs = zen.attrs azi = self.nc[self.datasets['solar_azimuth_angle']] aattrs = azi.attrs else: raise NotImplementedError("Don't know how to read " + key.name) x, y, z = angle2xyz(azi, zen) shape = x.shape from geotiepoints.interpolator import Interpolator tie_lines = np.arange( 0, (shape[0] - 1) * l_step + 1, l_step) tie_cols = np.arange(0, (shape[1] - 1) * c_step + 1, c_step) lines = np.arange((shape[0] - 1) * l_step + 1) cols = np.arange((shape[1] - 1) * c_step + 1) along_track_order = 1 cross_track_order = 3 satint = Interpolator([x.values, y.values, z.values], (tie_lines, tie_cols), (lines, cols), along_track_order, cross_track_order) (x, y, z, ) = satint.interpolate() del satint x = xr.DataArray(da.from_array(x, chunks=(CHUNK_SIZE, CHUNK_SIZE)), dims=['y', 'x']) y = xr.DataArray(da.from_array(y, chunks=(CHUNK_SIZE, CHUNK_SIZE)), dims=['y', 'x']) z = xr.DataArray(da.from_array(z, chunks=(CHUNK_SIZE, CHUNK_SIZE)), dims=['y', 'x']) azi, zen = xyz2angle(x, y, z) azi.attrs = aattrs zen.attrs = zattrs if 'zenith' in key.name: values = zen elif 'azimuth' in key.name: values = azi else: raise NotImplementedError("Don't know how to read " + key.name) if key.name.startswith('satellite'): self.cache['satellite_zenith_angle'] = zen self.cache['satellite_azimuth_angle'] = azi elif key.name.startswith('solar'): self.cache['solar_zenith_angle'] = zen self.cache['solar_azimuth_angle'] = azi elif key.name in self.cache: values = self.cache[key.name] else: values = self.nc[self.datasets[key.name]] values.attrs['platform_name'] = self.platform_name values.attrs['sensor'] = self.sensor values.attrs.update(key.to_dict()) return values