コード例 #1
0
ファイル: fwi.py プロジェクト: dataJSA/pyro-risks
def get_fwi_from_api(date: str):
    """Call the CDS API and return all fwi variables as a dataframe with geo coordinates and departments.

    When calling the API we get a zip file that must be extracted (in a tmp directory), then handle
    each queried variable which is in a separate netcdf file. A dataframe is created with all the variables
    and then finally we join codes and departments with geopandas.

    Args:
        date (str)

    Returns:
        pd.DataFrame
    """

    year, month, day = date.split("-")
    date_concat = date.replace("-", "")
    with tempfile.TemporaryDirectory() as tmp:
        call_fwi(tmp, year, month, day)

        file = zipfile.ZipFile(
            os.path.join(tmp, f"fwi_{year}_{month}_{day}.zip"))
        file.extractall(path=os.path.join(tmp, f"fwi_{year}_{month}_{day}"))

        df0 = pd.DataFrame({})
        for var_name in ["BUI", "DC", "DMC", "DSR", "FFMC", "FWI", "ISI"]:
            var_path = os.path.join(
                tmp,
                f"fwi_{year}_{month}_{day}/ECMWF_FWI_{var_name}_{date_concat}_1200_hr_v3.1_int.nc",
            )
            nc = Dataset(var_path, "r")
            lats = nc.variables["latitude"][:]
            var = nc.variables[var_name.lower()][:]
            nc.close()

            lons = np.arange(-180, 180.25, 0.25)
            var_cyclic = np.ma.hstack([var[0][:, 720:], var[0][:, :721]])
            lon2d, lat2d = np.meshgrid(lons, lats)
            df = pd.DataFrame({
                "latitude": lat2d.flatten(),
                "longitude": lon2d.flatten(),
                var_name.lower(): var_cyclic.flatten(),
            })
            df = df.dropna(subset=[var_name.lower()])
            df = df.reset_index(drop=True)
            if var_name == "BUI":
                df0 = pd.concat([df0, df], axis=1)
            else:
                df0 = pd.merge(df0,
                               df,
                               on=["latitude", "longitude"],
                               how="inner")
    geo_data = gpd.GeoDataFrame(
        df0,
        geometry=gpd.points_from_xy(df0["longitude"], df0["latitude"]),
        crs="EPSG:4326",
    )
    geo_masks = get_french_geom()
    geo_df = gpd.sjoin(geo_masks, geo_data, how="inner")
    return geo_df.drop(["index_right", "geometry"], axis=1)
コード例 #2
0
ファイル: test_datasets.py プロジェクト: pyronear/pyro-risks
 def test_get_french_geom(self):
     fr_geom = masks.get_french_geom()
     self.assertIsInstance(fr_geom, GeoDataFrame)
     self.assertTrue(
         all(
             v1 == v2 for v1, v2 in zip(fr_geom.columns, ["code", "nom", "geometry"])
         )
     )