def test_without_region_pass(self): """Append centroids without region id.""" centr1 = Centroids() centr1.tag = Tag('file_1.mat', 'description 1') centr1.coord = np.array([[1, 2], [3, 4], [5, 6]]) centr1.id = np.array([5, 7, 9]) centr2 = Centroids() centr2.tag = Tag('file_2.mat', 'description 2') centr2.coord = np.array([[1, 2], [3, 4], [5, 6]]) centr2.id = np.array([5, 7, 9]) centr2.region_id = np.array([1, 1, 1]) centr1.append(centr2) self.assertTrue(np.array_equal(centr1.region_id, np.array([], int))) centr1 = Centroids() centr1.tag = Tag('file_1.mat', 'description 1') centr1.coord = np.array([[1, 2], [3, 4], [5, 6]]) centr1.id = np.array([5, 7, 9]) centr1.region_id = np.array([1, 1, 1]) centr2 = Centroids() centr2.tag = Tag('file_2.mat', 'description 2') centr2.coord = np.array([[1, 2], [3, 4], [5, 6]]) centr2.id = np.array([5, 7, 9]) centr1.append(centr2) self.assertTrue(np.array_equal(centr1.region_id, np.array([1, 1, 1])))
def test_all_new_elem_pass(self): """Append a centroids with a new element.""" centr1 = Centroids() centr1.tag = Tag('file_1.mat', 'description 1') centr1.coord = np.array([[1, 2], [3, 4], [5, 6]]) centr1.id = np.array([5, 7, 9]) centr2 = Centroids() centr2.tag = Tag('file_2.mat', 'description 2') centr2.coord = np.array([[7, 8], [1, 5]]) centr2.id = np.array([5, 7]) centr1.append(centr2) self.assertEqual(len(centr1.tag.file_name), 2) self.assertEqual(len(centr1.tag.description), 2) self.assertEqual(centr1.coord.shape, (5, 2)) self.assertTrue(np.array_equal(centr1.coord[0, :], [1, 2])) self.assertTrue(np.array_equal(centr1.coord[1, :], [3, 4])) self.assertTrue(np.array_equal(centr1.coord[2, :], [5, 6])) self.assertTrue(np.array_equal(centr1.coord[3, :], [7, 8])) self.assertTrue(np.array_equal(centr1.coord[4, :], [1, 5])) self.assertEqual(centr1.id.shape, (5, )) self.assertTrue(np.array_equal(centr1.id, \ np.array([5, 7, 9, 10, 11]))) self.assertTrue(np.array_equal(centr1.region_id, np.array([], int)))
def test_new_elem_pass(self): """Append a centroids with a new element.""" centr1 = Centroids() centr1.tag = Tag('file_1.mat', 'description 1') centr1.coord = np.array([[1, 2], [3, 4], [5, 6]]) centr1.id = np.array([5, 7, 9]) centr1.region_id = np.array([1, 2, 3]) centr1.dist_coast = np.array([1.5, 2.6, 3.5]) centr2 = Centroids() centr2.tag = Tag('file_2.mat', 'description 2') centr2.coord = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) centr2.id = np.array([6, 7, 9, 1]) centr2.region_id = np.array([3, 4, 5, 6]) centr2.dist_coast = np.array([4.5, 5.6, 6.5, 7.8]) centr1.append(centr2) self.assertEqual(len(centr1.tag.file_name), 2) self.assertEqual(len(centr1.tag.description), 2) self.assertEqual(centr1.coord.shape, (4, 2)) self.assertTrue(np.array_equal(centr1.coord[0, :], [1, 2])) self.assertTrue(np.array_equal(centr1.coord[1, :], [3, 4])) self.assertTrue(np.array_equal(centr1.coord[2, :], [5, 6])) self.assertTrue(np.array_equal(centr1.coord[3, :], [7, 8])) self.assertEqual(centr1.id.shape, (4, )) self.assertTrue(np.array_equal(centr1.id, np.array([5, 7, 9, 1]))) self.assertTrue( np.array_equal(centr1.region_id, np.array([1, 2, 3, 6]))) self.assertTrue( np.array_equal(centr1.dist_coast, np.array([1.5, 2.6, 3.5, 7.8])))
def good_centroids(): """Define well a Centroids""" cen = Centroids() cen.coord = np.array([[1, 2], [3, 4], [5, 6]]) cen.id = np.array([1, 2, 3]) cen.region_id = np.array([1, 2, 3]) return cen
def _centroids_creation(firms, centr_res_factor): """ Compute centroids for the firms dataset. The number of centroids is defined according to the data resolution. Parameters: firms (dataframe): dataset obtained from FIRMS data centr_res_factor (float): the factor applied to voluntarly decrease/increase the centroids resolution Returns: centroids (Centroids) res_data (float): data resolution (km) """ LOGGER.info('Defining the resolution of the centroids.') # Resolution of the centroids depends on the data origin. # Resolution in km. if 'instrument' in firms.columns: if firms['instrument'].any() == 'MODIS': res_data = 1.0 else: res_data = 0.375 # For VIIRS data else: res_data = RES_DATA # For undefined data origin, defined by user LOGGER.info('Computing centroids.') centroids = Centroids() dlat_km = abs(firms['latitude'].min() - firms['latitude'].max()) * ONE_LAT_KM dlon_km = abs(firms['longitude'].min() - firms['longitude'].max()) * ONE_LAT_KM* \ np.cos(np.radians((abs(firms['latitude'].min() - firms['latitude'].max()))/2)) nb_centr_lat = int(dlat_km/res_data * centr_res_factor) nb_centr_lon = int(dlon_km/res_data * centr_res_factor) centroids.coord = (np.mgrid[firms['latitude'].min() : firms['latitude'].max() : \ complex(0, nb_centr_lat), firms['longitude'].min() : firms['longitude'].max() : \ complex(0, nb_centr_lon)]).\ reshape(2, nb_centr_lat*nb_centr_lon).transpose() centroids._resolution = (\ (abs(firms['latitude'].min() - firms['latitude'].max())/nb_centr_lat), (abs(firms['longitude'].min() - firms['longitude'].max())/nb_centr_lon)) centroids.id = np.arange(len(centroids.coord)) # Calculate the area attributed to each centroid centroids.set_area_per_centroid() # Calculate if the centroids is on land or not centroids.set_on_land() # Calculate to distance to coast centroids.set_dist_coast() # Create on land grid centroids.land = np.zeros((nb_centr_lat, nb_centr_lon)) for _, centr in enumerate(centroids.id): if centroids.on_land[centr]: centr_ix = int(centr/nb_centr_lon) centr_iy = centr%nb_centr_lon centroids.land[centr_ix, centr_iy] = 1 centroids.nb_centr_lat = nb_centr_lat centroids.nb_centr_lon = nb_centr_lon return centroids, res_data
def test_appended_type(self): """Append the same centroids.""" centr1 = Centroids() centr1.tag = Tag('file_1.mat', 'description 1') centr1.coord = np.array([[1, 2], [3, 4], [5, 6]]) centr1.id = np.array([5, 7, 9]) centr2 = centr1 centr1.append(centr2) self.assertEqual(type(centr1.tag.file_name), str) self.assertEqual(type(centr1.tag.description), str) self.assertEqual(type(centr1.coord), np.ndarray) self.assertEqual(type(centr1.id), np.ndarray) self.assertTrue(type(centr1.region_id), np.ndarray)
def test_append_to_empty_fill(self): """Append to empty centroids.""" centr1 = Centroids() centr2 = Centroids() centr2.tag = Tag('file_1.mat', 'description 1') centr2.coord = np.array([[1, 2], [3, 4], [5, 6]]) centr2.id = np.array([5, 7, 9]) centr1.append(centr2) self.assertEqual(centr1.tag.file_name, 'file_1.mat') self.assertEqual(centr1.tag.description, 'description 1') self.assertEqual(centr1.coord.shape, (3, 2)) self.assertTrue(np.array_equal(centr1.coord[0, :], [1, 2])) self.assertTrue(np.array_equal(centr1.coord[1, :], [3, 4])) self.assertTrue(np.array_equal(centr1.coord[2, :], [5, 6])) self.assertEqual(centr1.id.shape, (3, )) self.assertTrue(np.array_equal(centr1.id, np.array([5, 7, 9]))) self.assertTrue(np.array_equal(centr1.region_id, np.array([], int)))
def _centroids_from_nc(file_name): """ Construct Centroids from the grid described by 'latitude' and 'longitude' variables in a netCDF file. """ LOGGER.info('Constructing centroids from %s', file_name) ncdf = xr.open_dataset(file_name) if hasattr(ncdf, 'latitude'): lats = ncdf.latitude.data lons = ncdf.longitude.data new_coord = np.array([ np.repeat(lats, len(lons)), np.tile(lons, len(lats)), ]).T elif hasattr(ncdf, 'lat'): lats = ncdf.lat.data lons = ncdf.lon.data new_coord = np.array([ np.repeat(lats, len(lons)), np.tile(lons, len(lats)), ]).T elif hasattr(ncdf, 'lat_1'): lats = ncdf.lat_1.data lons = ncdf.lon_1.data new_coord = np.array([ lats.reshape(-1), lons.reshape(-1), ]).T else: raise AttributeError('netcdf file has no field named latitude or ' 'other know abrivation for coordinates.') cent = Centroids() cent.coord = new_coord cent.id = np.arange(0, len(cent.coord)) cent.tag.description = 'Centroids constructed from: ' + file_name ncdf.close() cent.set_area_per_centroid() cent.set_on_land() return cent