Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloLopez807 committed May 13, 2024
1 parent 5222322 commit 9f2aeb4
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 13 deletions.
68 changes: 63 additions & 5 deletions app/api/netcdfs.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,91 @@
from fastapi import Header, APIRouter
from fastapi.responses import FileResponse
import uuid
from app.processing.netcdfs_processing import get_netcdf_from_point as get_netcdf
from app.processing.netcdfs_processing import get_netcdf_from_point as get_netcdf_point
from app.processing.netcdfs_processing import get_netcdf_from_area as get_netcdf_area
from app.processing.netcdfs_processing import get_netcdf_from_mask as get_netcdf_mask
import json


router = APIRouter()


@router.get("/")
@router.get("/get_netcdf_from_point")
def get_netcdf_from_point(
longitude: float, latitude: float, filepath: str, start_date: str, end_date: str
filepath : str, longitude: float, latitude: float, start_date = None, end_date = None
):
"""_summary_
Args:
longitude (float): Longitude of the point
latitude (float): Latitude of the point
filepath (str): Filepath of the netcdf file
filepath (str): Filepath or URL of the netcdf file
start_date (str): Start date of the data
end_date (str): End date of the data
Returns:
_type_: Netcdf file
"""
ds = get_netcdf_point(longitude, latitude, start_date, end_date, filepath)
unique_filename = str(uuid.uuid4())
ds.to_netcdf("/tmp/{0}.nc".format(unique_filename))
return FileResponse(
"/tmp/{0}.nc".format(unique_filename),
media_type="application/x-netcdf",
filename="data.nc",
)



@router.get("/get_netcdf_from_area")
def get_netcdf_from_area(
longitude_min: float, longitude_max : float, latitude_min: float, latitude_max : float, filepath: str, start_date = None, end_date = None
):
"""_summary_
Args:
longitude_min (float): Minimum longitude of the area
longitude_max (float): Maximum longitude of the area
latitude_min (float): Minimum latitude of the area
latitude_max (float): Maximum latitude of the area
filepath (str): Filepath or URL of the netcdf file
start_date (str): Start date of the data
end_date (str): End date of the data
Returns:
_type_: Netcdf file
"""
ds = get_netcdf(longitude, latitude, filepath, start_date, end_date)
ds = get_netcdf_area(longitude_min, longitude_max, latitude_min, latitude_max, filepath, start_date, end_date)
unique_filename = str(uuid.uuid4())
ds.to_netcdf("/tmp/{0}.nc".format(unique_filename))
return FileResponse(
"/tmp/{0}.nc".format(unique_filename),
media_type="application/x-netcdf",
filename="data.nc",
)



@router.get("/get_netcdf_from_mask")
def get_netcdf_from_mask(
filepath_mask : str, filepath_netcdf : str, start_date = None, end_date = None, row = None
):
"""_summary_
Args:
filepath_mask (str): Filepath or URL of the mask file
filepath_netcdf (str): Filepath or URL to the netcdf file
start_date (str): Start date of the data
end_date (str): End date of the data
row (int): Number of first "n" mask rows
Returns:
_type_: xarray dataset
"""
ds = get_netcdf_mask(filepath_mask, filepath_netcdf, start_date, end_date, row)
unique_filename = str(uuid.uuid4())
ds.to_netcdf("/tmp/{0}.nc".format(unique_filename))
return FileResponse(
"/tmp/{0}.nc".format(unique_filename),
media_type="application/x-netcdf",
filename="data.nc",
)
101 changes: 94 additions & 7 deletions app/processing/netcdfs_processing.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,111 @@
import os
import xarray as xr
import geopandas
import rioxarray
from shapely.geometry import mapping

def get_netcdf_from_point(longitude: float, latitude: float, filepath: str, start_date: str, end_date: str):
def get_netcdf_from_point(longitude: float, latitude: float, start_date: str, end_date: str, filepath : str):
"""_summary_
Args:
longitude (float): Longitude of the point
latitude (float): Latitude of the point
filepath (str): Filepath of the netcdf file
filepath (str): Filepath of the local netcdf file
URL (str) : URL of the netcdf file
start_date (str): Start date of the data
end_date (str): End date of the data
Returns:
_type_: xarray dataset
"""
files = os.listdir(filepath)
if len(files) >1:
ds = xr.open_mfdataset("{0}/*.nc".format(filepath), concat_dim="time", combine='nested', engine="netcdf4")
else:
ds = xr.open_dataset("{0}/{1}".format(filepath, files[0]), engine="netcdf4")
try :
files = os.listdir(filepath)
if len(files) >1:
ds = xr.open_mfdataset("{0}*.nc".format(filepath), concat_dim="time", combine='nested', engine="netcdf4")
else:
ds = xr.open_dataset("{0}{1}".format(filepath, files[0]), engine="netcdf4")
except :
ds = xr.open_dataset(filepath, engine="netcdf4")
try :
ds = ds.rename({"lat" : "latitude", "lon" : "longitude"})
except:
pass
ds = ds.sel(time=slice(start_date, end_date))
ds = ds.sel(longitude=longitude, latitude=latitude, method="nearest")
return ds



def get_netcdf_from_area(longitude_min: float, longitude_max : float, latitude_min: float, latitude_max : float, filepath: str, start_date: str, end_date: str):
"""_summary_
Args:
longitude_min (float): Minimum longitude of the area
longitude_max (float): Maximum longitude of the area
latitude_min (float): Minimum latitude of the area
latitude_max (float): Maximum latitude of the area
filepath (str): Filepath of the netcdf file
start_date (str): Start date of the data
end_date (str): End date of the data
Returns:
_type_: xarray dataset
"""
try :
files = os.listdir(filepath)
if len(files) >1:
ds = xr.open_mfdataset("{0}*.nc".format(filepath), concat_dim="time", combine='nested', engine="netcdf4")
else:
ds = xr.open_dataset("{0}{1}".format(filepath, files[0]), engine="netcdf4")
except :
ds = xr.open_dataset(filepath, engine="netcdf4")
try :
ds = ds.rename({"lat" : "latitude", "lon" : "longitude"})
except:
pass
ds = ds.sel(time=slice(start_date, end_date))
mask_lon = (ds.longitude >= longitude_min) & (ds.longitude <= longitude_max)
mask_lat = (ds.latitude >= latitude_min) & (ds.latitude <= latitude_max)
ds = ds.where(mask_lon & mask_lat, drop=True)
return ds




def get_netcdf_from_mask(filepath_mask : str, filepath_netcdf : str, start_date = None, end_date = None, row = None) :
"""_summary_
Args:
filepath_mask (str): Filepath of the mask file
filepath_netcdf (str): Filepath to the netcdf file
start_date (str): Start date of the data
end_date (str): End date of the data
row (int): Number of first "n" rows
Returns:
_type_: xarray dataset
"""
try :
files = os.listdir(filepath_netcdf)
if len(files) >1:
ds = xr.open_mfdataset("{0}*.nc".format(filepath_netcdf), concat_dim="time", combine='nested', engine="netcdf4")
else:
ds = xr.open_dataset("{0}{1}".format(filepath_netcdf, files[0]), engine="netcdf4")
except :
ds = xr.open_dataset(filepath_netcdf, engine="netcdf4")
if row != None :
mask = geopandas.read_file(filepath_mask, rows=int(row), crs="epsg:4326")
else :
mask = geopandas.read_file(filepath_mask, crs="epsg:4326")
try :
ds = ds.rename({"lat" : "latitude", "lon" : "longitude"})
except :
pass
print(mask)
ds.rio.set_spatial_dims(x_dim="longitude", y_dim="latitude", inplace=True)
ds.rio.write_crs("epsg:4326", inplace=True)
ds = ds.rio.clip(mask.geometry.apply(mapping), mask.crs, drop=False)
try:
ds = ds.sel(time=slice(start_date, end_date))
except:
pass
return ds
2 changes: 1 addition & 1 deletion tests/test_get_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

longitude = -8.33
latitude = 43.46
filepath = "tests/data/get_data_from_point_single_file"
filepath = "tests/data/get_data_from_point_single_file/"
start_date = "2023-01-08"
end_date = "2023-01-11"

Expand Down
2 changes: 2 additions & 0 deletions tests/test_get_netcdf_from_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ def test_get_netcdf_from_point_multiple_files():
assert ds["sea_water_salinity"].values[8] == pytest.approx(10653 * 0.001 + 20)
assert ds["sea_surface_height_above_sea_level"].values[94] == pytest.approx(-1355 * 0.001)
assert ds["sea_water_salinity"].values[94] == pytest.approx(10521 * 0.001 + 20)


0 comments on commit 9f2aeb4

Please sign in to comment.