Commit a3985649 authored by CEVAER's avatar CEVAER
Browse files

Use API to retrieve track data

parent 0aac4db3
......@@ -44,6 +44,16 @@ def get_track_points_from_database(session, date):
return req_tracks
def get_track_points_from_api(date):
min_date = date.strftime("%Y-%m-%d")
max_date = (date + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
df = pd.read_csv(f"https://cyclobs.ifremer.fr/app/api/track?min_date={min_date}&max_date={max_date}")
df["date"] = pd.to_datetime(df["date"])
df = df[df["date"] >= date]
return df
# Selecting best pass
def select_pass(selected_set, file_date, track_point, lon_index, lat_index, time_col_name, is_full_time):
df = selected_set.to_dataframe()
......@@ -85,7 +95,7 @@ def get_colocated_track_points(dataset, track_points, file_date, wind_col_name,
time_col_name, pass_col_name=None, lon_col_name="lon", lat_col_name="lat",
deg_delta=3, time_delta=60):
track_point_offsets = []
for track_point in track_points:
for index, track_point in track_points.iterrows():
logger.debug(f"Track point : {track_point}")
sel = dataset.sel(**{lon_col_name: track_point["lon"], lat_col_name: track_point["lat"], "method": "nearest"})
......@@ -95,8 +105,10 @@ def get_colocated_track_points(dataset, track_points, file_date, wind_col_name,
f" minute: {sel[time_col_name].values},"
f" lon_index: {lon_index}, lat_index {lat_index}")
trk_ptn = track_point.to_dict()
if pass_col_name:
select_pass(sel, file_date, track_point, lon_index, lat_index, time_col_name, is_full_time)
select_pass(sel, file_date, trk_ptn, lon_index, lat_index, time_col_name, is_full_time)
else:
df = sel.to_dataframe().reset_index()
if not pd.isna(df.loc[0, time_col_name]):
......@@ -105,12 +117,12 @@ def get_colocated_track_points(dataset, track_points, file_date, wind_col_name,
else:
point_date = df.loc[0, time_col_name]
time_offset = abs(track_point["date"] - point_date)
track_point["time_offset"] = time_offset
track_point["node"] = None
track_point["lon_index"] = lon_index
track_point["lat_index"] = lat_index
if "time_offset" in track_point:
track_point_offsets.append(track_point)
trk_ptn["time_offset"] = time_offset
trk_ptn["node"] = None
trk_ptn["lon_index"] = lon_index
trk_ptn["lat_index"] = lat_index
if "time_offset" in trk_ptn:
track_point_offsets.append(trk_ptn)
kept_track_points = {}
# Getting the best track point for each sid (storm id).
......@@ -386,12 +398,10 @@ def process_file(session, file, output_path, extract_date_func, attrs, var_to_de
file_date = extract_date_func(filename)
logger.debug(f"File date {file_date}")
track_points = get_track_points_from_database(session, file_date)
track_points = get_track_points_from_api(file_date)
logger.debug(f"Number of track point found : {len(track_points)}")
track_points = [{"sid": track_point[0].sid, "lon": shape360(track_point.lon, 0)[0],
"lat": track_point.lat,
"date": track_point[0].date} for track_point in track_points]
track_points["lon"] = track_points["lon"].apply(lambda x: shape360(x, 0)[0])
dataset = xarray.open_dataset(file)
df = dataset.to_dataframe()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment