Skip to content

Commit

Permalink
Add parameter to drop/keep the coordinate levels of the index
Browse files Browse the repository at this point in the history
  • Loading branch information
uvchik committed Jun 18, 2021
1 parent ea93026 commit 792883a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/feedinlib/era5.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,12 @@ def select_geometry(ds, area):


def weather_df_from_era5(
era5_netcdf_filename, lib, start=None, end=None, area=None
era5_netcdf_filename,
lib,
start=None,
end=None,
area=None,
drop_coord_levels=False,
):
"""
Gets ERA5 weather data from netcdf file and converts it to a pandas
Expand All @@ -376,6 +381,9 @@ def weather_df_from_era5(
If you want data for an area you can provide a shape of this area or
specify a rectangular area giving a list of the
form [(lon west, lon east), (lat south, lat north)].
drop_coord_levels : bool
Decide whether the index levels of the coordinates will be dropped. A
ValueError is raised if there are more than one coordinates.
Returns
-------
Expand Down Expand Up @@ -411,8 +419,13 @@ def weather_df_from_era5(

# drop latitude and longitude from index in case a single location
# is given in parameter `area`
if area is not None and isinstance(area, list):
if np.size(area[0]) == 1 and np.size(area[1]) == 1:
if drop_coord_levels is True:
if len(df.groupby(level=[1, 2]).count()) > 1:
msg = ("You cannot drop the coordinate levels if there are more "
"than one point. You will get duplicate entries in the "
"index.")
raise ValueError(msg)
else:
df.index = df.index.droplevel(level=[1, 2])

if start is None:
Expand Down

0 comments on commit 792883a

Please sign in to comment.