Esempio n. 1
0
    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)
        cent = Centroids()
        ncdf = xr.open_dataset(file_name)
        if hasattr(ncdf, 'latitude'):
            lats = ncdf.latitude.data
            lons = ncdf.longitude.data
        elif hasattr(ncdf, 'lat'):
            lats = ncdf.lat.data
            lons = ncdf.lon.data
        elif hasattr(ncdf, 'lat_1'):
            lats = ncdf.lat_1.data
            lons = ncdf.lon_1.data
        else:
            raise AttributeError('netcdf file has no field named latitude or '
                                 'other know abrivation for coordinates.')
        ncdf.close()

        lats, lons = np.array(
            [np.repeat(lats, len(lons)),
             np.tile(lons, len(lats))])
        cent = Centroids()
        cent.set_lat_lon(lats, lons)
        cent.set_area_pixel()
        cent.set_on_land()

        return cent
Esempio n. 2
0
 def test_on_land(self):
     """ Test set_on_land """
     centr_ras = Centroids()
     centr_ras.set_raster_file(HAZ_DEMO_FL, window= Window(0, 0, 50, 60))
     centr_ras.set_on_land()
     centr_ras.check()
     self.assertTrue(np.array_equal(centr_ras.on_land, np.ones(60 * 50, bool)))
Esempio n. 3
0
 def test_on_land(self):
     """Test set_on_land"""
     centr = Centroids()
     centr.lat, centr.lon, centr.geometry = self.data_vector()
     centr.geometry.crs = {'init': 'epsg:4326'}
     centr.set_on_land()
     centr.set_region_id()
     centr.region_id[centr.region_id > 0] = 1
     self.assertTrue(
         np.array_equal(centr.on_land.astype(int), centr.region_id))
Esempio n. 4
0
    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)
        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.set_lat_lon(coord[:, 0], coord[:, 1])

#        centroids.set_raster_from_pnt_bounds((firms['longitude'].min(),
#        firms['latitude'].min(), firms['longitude'].max(), firms['latitude'].max()),
#        res=res_data/centr_res_factor)    ---> propagation works?

        # Calculate the area attributed to each centroid
        centroids.set_area_approx()
        # 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 = centroids.on_land.reshape((nb_centr_lat, nb_centr_lon)).astype(int)
        centroids.nb_centr_lat = nb_centr_lat
        centroids.nb_centr_lon = nb_centr_lon
        centroids.empty_geometry_points()
        return centroids, res_data
Esempio n. 5
0
    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)
        cent = Centroids()
        ncdf = xr.open_dataset(file_name)
        create_meshgrid = True
        if hasattr(ncdf, 'latitude'):
            lats = ncdf.latitude.data
            lons = ncdf.longitude.data
        elif hasattr(ncdf, 'lat'):
            lats = ncdf.lat.data
            lons = ncdf.lon.data
        elif hasattr(ncdf, 'lat_1'):
            if len(ncdf.lon_1.shape)>1 & \
                (ncdf.lon_1.shape == ncdf.lat_1.shape) \
                :
                lats = ncdf.lat_1.data.flatten()
                lons = ncdf.lon_1.data.flatten()
                create_meshgrid = False
            else:
                lats = ncdf.lat_1.data
                lons = ncdf.lon_1.data
        elif hasattr(ncdf, 'clat'):
            lats = ncdf.clat.data
            lons = ncdf.clon.data
            if ncdf.clat.attrs['units'] == 'radian':
                lats = np.rad2deg(lats)
                lons = np.rad2deg(lons)
            create_meshgrid = False
        else:
            raise AttributeError('netcdf file has no field named latitude or '
                                 'other know abrivation for coordinates.')
        ncdf.close()

        if create_meshgrid:
            lats, lons = np.array(
                [np.repeat(lats, len(lons)),
                 np.tile(lons, len(lats))])
        cent = Centroids()
        cent.set_lat_lon(lats, lons)
        cent.set_area_pixel()
        cent.set_on_land()

        return cent
Esempio n. 6
0
 def test_elevation_3_pass(self):
     """ test set_elevation """
     centr = Centroids()
     centr.lat, centr.lon, centr.geometry = self.data_vector()
     centr.geometry.crs = DEF_CRS
     centr.set_elevation(product='SRTM3', resampling=Resampling.nearest)
     centr.set_on_land()
     self.assertTrue(np.all(centr.elevation[centr.on_land] > 0))
     self.assertTrue(
         np.all(
             centr.elevation[np.logical_not(centr.on_land)] == DEM_NODATA))
     self.assertEqual(centr.elevation[0], 23)
     self.assertEqual(centr.elevation[1], 92)
     self.assertEqual(centr.elevation[2], 69)
     self.assertEqual(centr.elevation[3], 77)
     self.assertEqual(centr.elevation[4], 133)
     self.assertEqual(centr.elevation[5], 41)
     self.assertEqual(centr.elevation.min(), DEM_NODATA)
Esempio n. 7
0
def def_ori_centroids(min_lon, min_lat, max_lon, max_lat, centr_res_factor):

    res_data = 0.375  # For VIIRS data

    centroids = Centroids()
    dlat_km = abs(min_lat - max_lat) * ONE_LAT_KM
    dlon_km = abs(min_lon - max_lon) * ONE_LAT_KM * np.cos(
        np.radians((abs(min_lat - max_lat)) / 2))
    nb_centr_lat = int(dlat_km / res_data * centr_res_factor)
    nb_centr_lon = int(dlon_km / res_data * centr_res_factor)
    coord = (np.mgrid[min_lat : max_lat : complex(0, nb_centr_lat), \
             min_lon : max_lon : complex(0, nb_centr_lon)]). \
             reshape(2, nb_centr_lat*nb_centr_lon).transpose()
    centroids.set_lat_lon(coord[:, 0], coord[:, 1])

    # Calculate the area attributed to each centroid
    centroids.set_area_approx()
    # Calculate if the centroids is on land or not
    centroids.set_on_land()
    # Create on land grid
    centroids.empty_geometry_points()
    return centroids
Esempio n. 8
0
    def _centroids_creation(firms, res_data, centr_res_factor):
        """ Get centroids from the firms dataset and refactor them.

        Parameters:
            firms (DataFrame): dataset obtained from FIRMS data
            res_data (float): FIRMS instrument resolution in degrees
            centr_res_factor (float): the factor applied to voluntarly decrease/increase
                the centroids resolution

        Returns:
            centroids (Centroids)
        """
        centroids = Centroids()
        centroids.set_raster_from_pnt_bounds((firms['longitude'].min(), \
            firms['latitude'].min(), firms['longitude'].max(), \
            firms['latitude'].max()), res=res_data/centr_res_factor)
        centroids.set_meta_to_lat_lon()
        centroids.set_area_approx()
        centroids.set_on_land()
        centroids.empty_geometry_points()

        return centroids
def def_ori_centroids(firms, centr_res_factor):
    # res_data in km
    if firms['instrument'].any() == 'MODIS':
        res_data = 1.0
    else:
        res_data = 0.375  # For VIIRS data

    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)
    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.set_lat_lon(coord[:, 0], coord[:, 1])

    centroids.set_area_approx()
    centroids.set_on_land()
    centroids.empty_geometry_points()
    return centroids, res_data