def test_get_geolocation_grids(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") lon, lat = d.get_geolocation_grids() self.assertEqual(type(lon), np.ndarray) self.assertEqual(type(lat), np.ndarray) self.assertEqual(lat.shape, (500, 500))
def test_reproject_domain_if_tps_is_given(self): n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper) d = Domain(4326, "-te 27 70 30 72 -ts 500 500") n.reproject(d, tps=False) tmpfilename = os.path.join(self.tmp_data_path, 'nansat_reproject_domain.png') n.write_figure(tmpfilename, 2, clim='hist') self.assertEqual(n.shape(), (500, 500)) self.assertEqual(type(n[1]), np.ndarray) self.assertTrue(n.has_band('swathmask')) n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper) d = Domain(4326, "-te 27 70 30 72 -ts 500 500") n.reproject(d, tps=True) tmpfilename = os.path.join(self.tmp_data_path, 'nansat_reproject_domain.png') n.write_figure(tmpfilename, 2, clim='hist') self.assertEqual(n.shape(), (500, 500)) self.assertEqual(type(n[1]), np.ndarray) self.assertTrue(n.has_band('swathmask'))
def get_drift_vectors(n1, x1, y1, n2, x2, y2, nsr=NSR(), **kwargs): ''' Find ice drift speed m/s Parameters ---------- n1 : First Nansat object x1 : 1D vector - X coordinates of keypoints on image 1 y1 : 1D vector - Y coordinates of keypoints on image 1 n2 : Second Nansat object x1 : 1D vector - X coordinates of keypoints on image 2 y1 : 1D vector - Y coordinates of keypoints on image 2 nsr: Nansat.NSR(), projection that defines the grid Returns ------- u : 1D vector - eastward ice drift speed v : 1D vector - northward ice drift speed lon1 : 1D vector - longitudes of source points lat1 : 1D vector - latitudes of source points lon2 : 1D vector - longitudes of destination points lat2 : 1D vector - latitudes of destination points ''' # convert x,y to lon, lat lon1, lat1 = n1.transform_points(x1, y1) lon2, lat2 = n2.transform_points(x2, y2) # create domain that converts lon/lat to units of the projection d = Domain(nsr, '-te -10 -10 10 10 -tr 1 1') # find displacement in needed units x1, y1 = d.transform_points(lon1, lat1, 1) x2, y2 = d.transform_points(lon2, lat2, 1) return x2 - x1, y1 - y2, lon1, lat1, lon2, lat2
def test_reproject_GCPs(self): ds = gdal.Open(self.test_file) d = Domain(ds=ds) d.reproject_GCPs('+proj=stere +datum=WGS84 +ellps=WGS84 +lat_0=75 +lon_0=10 +no_defs') gcp = d.vrt.dataset.GetGCPs()[0] self.assertTrue(gcp.GCPX > 636161) self.assertTrue(gcp.GCPY < -288344)
def test_write_map(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") tmpfilename = os.path.join(ntd.tmp_data_path, 'domain_write_map.png') d.write_map(tmpfilename) self.assertTrue(os.path.exists(tmpfilename)) i = Image.open(tmpfilename) i.verify() self.assertEqual(i.info['dpi'], (50, 50))
def test_get_border(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") lon, lat = d.get_border() self.assertEqual(type(lon), np.ndarray) self.assertEqual(type(lat), np.ndarray) self.assertEqual(len(lat), 44) self.assertEqual(lat[0], lat[-1]) self.assertEqual(lon[0], lon[-1])
def test_reproject_GCPs(self): ds = gdal.Open(self.test_file) d = Domain(ds=ds) d.reproject_GCPs( '+proj=stere +datum=WGS84 +ellps=WGS84 +lat_0=75 +lon_0=10 +no_defs' ) gcp = d.vrt.dataset.GetGCPs()[0] self.assertTrue(gcp.GCPX > 636161) self.assertTrue(gcp.GCPY < -288344)
def test_integrated(self): ''' Shall use all developed functions for feature tracking''' lon1pm, lat1pm = np.meshgrid(np.linspace(-3, 2, 50), np.linspace(86.4, 86.8, 50)) sid = SeaIceDrift(self.testFiles[0], self.testFiles[1]) uft, vft, lon1ft, lat1ft, lon2ft, lat2ft = sid.get_drift_FT() upm, vpm, apm, rpm, hpm, lon2pm, lat2pm = sid.get_drift_PM( lon1pm, lat1pm, lon1ft, lat1ft, lon2ft, lat2ft) lon1, lat1 = sid.n1.get_border() lon2, lat2 = sid.n2.get_border() sid.n1.reproject(Domain(NSR().wkt, '-te -3 86.4 2 86.8 -ts 500 500')) s01 = sid.n1['sigma0_HV'] sid.n2.reproject(Domain(NSR().wkt, '-te -3 86.4 2 86.8 -ts 500 500')) s02 = sid.n2['sigma0_HV'] plt.imshow(s01, extent=[-3, 2, 86.4, 86.8], cmap='gray', aspect=12) plt.quiver(lon1ft, lat1ft, uft, vft, color='r', angles='xy', scale_units='xy', scale=0.5) plt.plot(lon2, lat2, '.-r') plt.xlim([-3, 2]) plt.ylim([86.4, 86.8]) plt.savefig('sea_ice_drift_tests_%s_img1_ft.png' % inspect.currentframe().f_code.co_name, dpi=150, bbox_inches='tight', pad_inches=0) plt.close('all') plt.imshow(s02, extent=[-3, 2, 86.4, 86.8], cmap='gray', aspect=12) gpi = rpm > 0.4 plt.quiver(lon1pm[gpi], lat1pm[gpi], upm[gpi], vpm[gpi], rpm[gpi] * hpm[gpi], angles='xy', scale_units='xy', scale=0.5) plt.plot(lon1, lat1, '.-r') plt.xlim([-3, 2]) plt.ylim([86.4, 86.8]) plt.savefig('sea_ice_drift_tests_%s_img2_pm.png' % inspect.currentframe().f_code.co_name, dpi=150, bbox_inches='tight', pad_inches=0) plt.close('all')
def test_write_map_labels(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") tmpfilename = os.path.join(ntd.tmp_data_path, 'domain_write_map_labels.png') d.write_map(tmpfilename, merLabels=[False, False, False, True], parLabels=[True, False, False, False]) self.assertTrue(os.path.exists(tmpfilename)) i = Image.open(tmpfilename) i.verify()
def test_overlaps_contains(self): Bergen = Domain(4326, "-te 5 60 6 61 -ts 500 500") WestCoast = Domain(4326, "-te 1 58 6 64 -ts 500 500") Norway = Domain(4326, "-te 3 55 30 72 -ts 500 500") Paris = Domain(4326, "-te 2 48 3 49 -ts 500 500") self.assertTrue(Bergen.overlaps(Norway)) self.assertTrue(Norway.contains(Bergen)) self.assertFalse(Bergen.contains(Norway)) self.assertTrue(Norway.overlaps(WestCoast)) self.assertFalse(Norway.contains(WestCoast)) self.assertFalse(Paris.overlaps(Norway)) self.assertFalse(Paris.contains(Norway))
def test_export2thredds_longlat_list(self): d = Domain("+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs", "-te 27 70 31 72 -ts 200 200") n = Nansat(domain=d) n.add_band(np.ones(d.shape(), np.float32), parameters={'name': 'L_469'}) n.set_metadata('time_coverage_start', '2016-01-19') tmpfilename = os.path.join(ntd.tmp_data_path, 'nansat_export2thredds_longlat.nc') n.export2thredds(tmpfilename, ['L_469']) ncI = netcdf_file(tmpfilename, 'r') ncIVar = ncI.variables['L_469'] self.assertTrue(ncIVar.grid_mapping in ncI.variables.keys())
def get_displacement_km(n1, x1, y1, n2, x2, y2, ll2km='domain'): ''' Find displacement in kilometers using Domain''' lon1, lat1 = n1.transform_points(x1, y1) lon2, lat2 = n2.transform_points(x2, y2) d = Domain( '+proj=stere +lon_0=%f +lat_0=%f +no_defs' % (lon1.mean(), lat1.mean()), '-te -100000 -100000 100000 100000 -tr 1000 1000') x1d, y1d = d.transform_points(lon1, lat1, 1) x2d, y2d = d.transform_points(lon2, lat2, 1) return x2d - x1d, y1d - y2d
def test_export2thredds_longlat_dict(self): d = Domain("+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs", "-te 27 70 31 72 -ts 200 200") n = Nansat.from_domain(d) n.add_band(np.ones(d.shape(), np.float32), parameters={'name': 'L_469'}) n.set_metadata('time_coverage_start', '2016-01-19') tmpfilename = os.path.join(self.tmp_data_path, 'nansat_export2thredds_longlat.nc') n.export2thredds(tmpfilename, {'L_469': {'type': '>i1'}}) ncI = Dataset(tmpfilename, 'r') ncIVar = ncI.variables['L_469'] self.assertTrue(ncIVar.grid_mapping in ncI.variables.keys()) self.assertEqual(ncIVar[:].dtype, np.int8)
def test_get_item_inf_expressions(self): ''' inf should be replaced with nan ''' d = Domain(4326, "-te 25 70 35 72 -ts 500 500") n = Nansat(domain=d, logLevel=40) arr = np.empty((500, 500)) n.add_band(arr, {'expression': 'np.array([0,1,2,3,np.inf,5,6,7])'}) self.assertIsInstance(n[1], np.ndarray) self.assertTrue(np.isnan(n[1][4]))
def test_integrated(self, mock_get_invalid_mask): ''' Shall use all developed functions for feature tracking''' invalid_mask = np.zeros((500,500)).astype(bool) invalid_mask[:100,:100] = True mock_get_invalid_mask.return_value = invalid_mask sid = SeaIceDrift(self.testFiles[0], self.testFiles[1]) lon1b, lat1b = sid.n1.get_border() lon1pm, lat1pm = np.meshgrid(np.linspace(lon1b.min(), lon1b.max(), 50), np.linspace(lat1b.min(), lat1b.max(), 50)) uft, vft, lon1ft, lat1ft, lon2ft, lat2ft = sid.get_drift_FT() upm, vpm, apm, rpm, hpm, lon2pm, lat2pm = sid.get_drift_PM( lon1pm, lat1pm, lon1ft, lat1ft, lon2ft, lat2ft) lon1, lat1 = sid.n1.get_border() lon2, lat2 = sid.n2.get_border() ext_str = '-te %s %s %s %s -ts 500 500' % (lon1b.min(), lat1b.min(), lon1b.max(), lat1b.max()) sid.n1.reproject(Domain(NSR().wkt, ext_str)) s01 = sid.n1['sigma0_HV'] sid.n2.reproject(Domain(NSR().wkt, ext_str)) s02 = sid.n2['sigma0_HV'] extent=[lon1b.min(), lon1b.max(), lat1b.min(), lat1b.max()] plt.imshow(s01, extent=extent, cmap='gray', aspect=10) plt.quiver(lon1ft, lat1ft, uft, vft, color='r', angles='xy', scale_units='xy', scale=0.2) plt.plot(lon2, lat2, '.-r') plt.xlim([lon1b.min(), lon1b.max()]) plt.ylim([lat1b.min(), lat1b.max()]) plt.savefig('sea_ice_drift_tests_%s_img1_ft.png' % inspect.currentframe().f_code.co_name, dpi=150, bbox_inches='tight', pad_inches=0) plt.close('all') plt.imshow(s02, extent=extent, cmap='gray', aspect=10) gpi = hpm*rpm > 4 plt.quiver(lon1pm[gpi], lat1pm[gpi], upm[gpi], vpm[gpi], rpm[gpi]*hpm[gpi], angles='xy', scale_units='xy', scale=0.2) plt.plot(lon1, lat1, '.-r') plt.xlim([lon1b.min(), lon1b.max()]) plt.ylim([lat1b.min(), lat1b.max()]) plt.savefig('sea_ice_drift_tests_%s_img2_pm.png' % inspect.currentframe().f_code.co_name, dpi=150, bbox_inches='tight', pad_inches=0) plt.close('all')
def test_reproject_pure_geolocation(self): n0 = Nansat(self.test_file_gcps) b0 = n0[1] lon0, lat0 = n0.get_geolocation_grids() d1 = Domain.from_lonlat(lon=lon0, lat=lat0) d2 = Domain.from_lonlat(lon=lon0, lat=lat0, add_gcps=False) d3 = Domain(NSR().wkt, '-te 27 70 31 72 -ts 500 500') n1 = Nansat.from_domain(d1, b0) n2 = Nansat.from_domain(d2, b0) n1.reproject(d3) n2.reproject(d3) b1 = n1[1] b2 = n2[1] self.assertTrue(np.allclose(b1, b2))
def setUp(self): self.test_file_gcps = os.path.join(ntd.test_data_path, 'gcps.tif') self.test_file_stere = os.path.join(ntd.test_data_path, 'stere.tif') self.test_file_complex = os.path.join(ntd.test_data_path, 'complex.nc') self.domain = Domain(4326, '-lle 27 70 31 72 -ts 700 650') plt.switch_backend('Agg') if not os.path.exists(self.test_file_gcps): raise ValueError('No test data available')
def test_get_item_inf_expressions(self): """ inf should be replaced with nan """ self.mock_pti['get_wkv_variable'].return_value=dict(short_name='newband') d = Domain(4326, "-te 25 70 35 72 -ts 500 500") n = Nansat.from_domain(d, log_level=40) arr = np.empty((500, 500)) n.add_band(arr, {'expression': 'np.array([0,1,2,3,np.inf,5,6,7])'}) self.assertIsInstance(n[1], np.ndarray) self.assertTrue(np.isnan(n[1][4]))
def test_get_item_basic_expressions(self): """ Testing get_item with some basic expressions """ self.mock_pti['get_wkv_variable'].return_value=dict(short_name='newband') d = Domain(4326, "-te 25 70 35 72 -ts 500 500") n = Nansat.from_domain(d, np.zeros((500, 500)), {'expression': 'np.ones((500, 500))'}) self.assertIsInstance(n[1], np.ndarray) self.assertEqual(n[1].shape, (500, 500)) band1 = n[1] self.assertTrue(np.allclose(band1, np.ones((500, 500))))
def test_reproject_domain(self): n = Nansat(self.test_file_gcps, logLevel=40) d = Domain(4326, "-te 27 70 30 72 -ts 500 500") n.reproject(d) tmpfilename = os.path.join(ntd.tmp_data_path, 'nansat_reproject_domain.png') n.write_figure(tmpfilename, 2, clim='hist') self.assertEqual(n.shape(), (500, 500)) self.assertEqual(type(n[1]), np.ndarray)
def test_reproject_no_addmask(self): ''' Should not add swath mask and return 0 in areas out of swath ''' n = Nansat(self.test_file_complex, logLevel=40) d = Domain(4326, '-te -92.08 26.85 -92.00 26.91 -ts 200 200') n.reproject(d, addmask=False) b = n[1] self.assertTrue(not n.has_band('swathmask')) self.assertTrue(np.isfinite(b[0, 0])) self.assertTrue(np.isfinite(b[100, 100]))
def test_reproject_domain_if_source_and_destination_domain_span_entire_lons(self, mock_Nansat): n = Nansat(self.test_file_arctic, log_level=40, mapper=self.default_mapper) d = Domain(4326, "-te -180 180 60 90 -ts 500 500") n.reproject(d) tmpfilename = os.path.join(self.tmp_data_path, 'nansat_reproject_domain.png') n.write_figure(tmpfilename, 2, clim='hist') self.assertEqual(n.shape(), (500, 500)) self.assertEqual(type(n[1]), np.ndarray) self.assertTrue(n.has_band('swathmask'))
def test_add_band(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") arr = np.random.randn(500, 500) n = Nansat.from_domain(d, log_level=40) n.add_band(arr, {'name': 'band1'}) self.assertEqual(type(n), Nansat) self.assertEqual(type(n[1]), np.ndarray) self.assertEqual(n.get_metadata('name', 1), 'band1') self.assertEqual(n[1].shape, (500, 500))
def test_add_bands_no_parameter(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") arr = np.random.randn(500, 500) n = Nansat.from_domain(d, log_level=40) n.add_bands([arr, arr]) self.assertEqual(type(n), Nansat) self.assertEqual(type(n[1]), np.ndarray) self.assertEqual(type(n[2]), np.ndarray)
def test_reproject_of_complex(self): """ Should return np.nan in areas out of swath """ n = Nansat(self.test_file_complex, log_level=40, mapper=self.default_mapper) d = Domain(4326, '-te -92.08 26.85 -92.00 26.91 -ts 200 200') n.reproject(d) b = n[1] self.assertTrue(n.has_band('swathmask')) self.assertTrue(np.isnan(b[0, 0])) self.assertTrue(np.isfinite(b[100, 100]))
def test_reproject_no_addmask(self): """ Should not add swath mask and return 0 in areas out of swath """ n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper) d = Domain(4326, '-te -92.08 26.85 -92.00 26.91 -ts 200 200') n.reproject(d, addmask=False) b = n[1] self.assertTrue(not n.has_band('swathmask')) self.assertTrue(np.isfinite(b[0, 0])) self.assertTrue(np.isfinite(b[100, 100]))
def test_add_subvrts_only_to_one_nansat(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") arr = np.random.randn(500, 500) n1 = Nansat.from_domain(d, log_level=40) n2 = Nansat.from_domain(d, log_level=40) n1.add_band(arr, {'name': 'band1'}) self.assertEqual(type(n1.vrt.band_vrts), dict) self.assertTrue(len(n1.vrt.band_vrts) > 0) self.assertEqual(n2.vrt.band_vrts, {})
def test_init_domain_array(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") n = Nansat(domain=d, array=np.random.randn(500, 500), parameters={'name': 'band1'}, logLevel=40) self.assertEqual(type(n), Nansat) self.assertEqual(type(n[1]), np.ndarray) self.assertEqual(n.get_metadata('name', 1), 'band1') self.assertEqual(n[1].shape, (500, 500))
def test_add_bands(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") arr = np.random.randn(500, 500) n = Nansat(domain=d, logLevel=40) n.add_bands([arr, arr], [{'name': 'band1'}, {'name': 'band2'}]) self.assertEqual(type(n), Nansat) self.assertEqual(type(n[1]), np.ndarray) self.assertEqual(type(n[2]), np.ndarray) self.assertEqual(n.get_metadata('name', 1), 'band1') self.assertEqual(n.get_metadata('name', 2), 'band2')
def test_from_domain_array(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") n = Nansat.from_domain(d, np.random.randn(500, 500), {'name': 'band1'}) self.assertEqual(type(n), Nansat) self.assertEqual(type(n[1]), np.ndarray) self.assertEqual(n.get_metadata('name', 1), 'band1') self.assertEqual(n[1].shape, (500, 500)) self.assertEqual(n.filename, '') self.assertIsInstance(n.logger, logging.Logger) self.assertEqual(n.name, '') self.assertEqual(n.path, '')
def test_get_item_basic_expressions(self): ''' Testing get_item with some basic expressions ''' d = Domain(4326, "-te 25 70 35 72 -ts 500 500") n = Nansat(domain=d, logLevel=40) arr = np.empty((500, 500)) n.add_band(arr, {'expression': '1+1'}) n.add_band(arr, {'expression': 'np.random.randn(500, 500)'}) self.assertIsInstance(n[1], int) self.assertIsInstance(n[2], np.ndarray) self.assertEqual(n[1], 2) self.assertEqual(len(n[2]), 500) self.assertEqual(len(n[2][0]), 500)
def test_add_bands(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") arr = np.random.randn(500, 500) n = Nansat.from_domain(d, log_level=40) n.add_bands([arr, arr], [{'name': 'band1'}, {'name': 'band2'}]) self.assertIsInstance(n, Nansat) self.assertEqual(n.vrt.vrt.vrt, None) self.assertIsInstance(n[1], np.ndarray) self.assertIsInstance(n[2], np.ndarray) self.assertEqual(n.get_metadata('name', 1), 'band1') self.assertEqual(n.get_metadata('name', 2), 'band2')
def test_get_pixelsize_meters(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") x, y = d.get_pixelsize_meters() self.assertTrue(x - 444 < 1) self.assertTrue(y - 723 < 1)
def test_transform_points(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") lon, lat = d.transform_points([1, 2, 3], [1, 2, 3]) self.assertEqual(type(lon), np.ndarray) self.assertEqual(type(lat), np.ndarray)
def test_write_kml(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") tmpfilename = os.path.join(ntd.tmp_data_path, 'domain_write_kml.kml') d.write_kml(kmlFileName=tmpfilename) self.assertTrue(os.path.exists(tmpfilename))
def test_init_from_lonlat(self): lat, lon = np.mgrid[-90:90:0.5, -180:180:0.5] d = Domain(lon=lon, lat=lat) self.assertEqual(type(d), Domain) self.assertEqual(d.shape(), lat.shape)
def test_shape(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") shape = d.shape() self.assertEqual(shape, (500, 500))
print 'Output file:', oFileName, '\n' ''' Domain class is a container for geographical reference of a raster A Domain object describes all attributes of geographical reference of a raster: width and height, pixel size, relation between pixel/line coordinates and geographical coordinates, type of data projection (e.g. geographical or stereographic) ''' # Create Domain object. It describes the desired grid of reprojected image: # projection, resolution, size, etc. In this case it is geographic projection; # -10 - 30 E, 50 - 70 W; 2000 x 2000 pixels d = Domain("+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs", "-te 25 70 35 72 -ts 2000 2000") d = Domain(4326, "-te 25 70 35 72 -ts 2000 2000") d.write_map(oFileName + '01_latlong_map.png') print 'Latlong Domain:', d, '\n' # get shape print 'shape:', d.shape(), '\n' # Generate two vectors with values of lat/lon for the border of domain lonVec, latVec = d.get_border() print 'lonVec :', lonVec, '\n' print 'latVec :', latVec, '\n' # Get upwards azimuth direction of domain. bearing_center = d.upwards_azimuth_direction() print 'bearing_center :', bearing_center, '\n'
def test_azimuth_y(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") au = d.azimuth_y() self.assertEqual(np.round(au[0, 0]), 0) self.assertEqual(np.round(au[10, 10]), 0)
def test_transform_points_inverse(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") x, y = d.transform_points([25, 26, 27], [70, 71, 72], 1) self.assertTrue(all(np.round(x) == [0, 50, 100])) self.assertTrue(all(np.round(y) == [500, 250, 0]))
def test_get_border_wkt(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") bwkt = d.get_border_wkt() self.assertEqual(type(bwkt), str) self.assertTrue('POLYGON' in bwkt)
def test_get_border_geometry(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") geom = d.get_border_geometry() self.assertEqual(type(geom), ogr.Geometry)
def test_get_corners(self): d = Domain(4326, "-te 25 70 35 72 -ts 500 500") lon, lat = d.get_corners() self.assertTrue(all(lon - [25., 25., 35., 35.] < 0.01)) self.assertTrue(all(lat - [72., 70., 72., 70.] < 0.01))