Skip to content

Commit bf0be5e

Browse files
committed
remove redundant get_extent code
1 parent 01622e8 commit bf0be5e

2 files changed

Lines changed: 5 additions & 121 deletions

File tree

oceanval/data/chunk_seasonal.py

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
concise = True
8888
time_name = None
8989
model_unit = ""
90-
raw_extent = "foo"
9190
xlim = "bar"
9291
ylim = "ba"
9392
if time_name is not None:
@@ -144,13 +143,9 @@
144143
except:
145144
model_unit = ds_obs.contents.unit[0]
146145
model_unit = fix_unit(model_unit)
147-
from oceanval.utils import get_extent
148-
raw_extent = get_extent(ds_annual[0])
149-
if np.abs(raw_extent[0] - df_model.lon.min()) > 3:
150-
# convert longitude to -180-180
151-
df_model["lon" ] = [x if x < 180 else x -360 for x in df_model.lon]
152-
df_obs["lon" ] = [x if x < 180 else x -360 for x in df_obs.lon]
153-
df_diff["lon" ] = [x if x < 180 else x -360 for x in df_diff.lon]
146+
df_model["lon" ] = [x if x < 180 else x -360 for x in df_model.lon]
147+
df_obs["lon" ] = [x if x < 180 else x -360 for x in df_obs.lon]
148+
df_diff["lon" ] = [x if x < 180 else x -360 for x in df_diff.lon]
154149

155150
# generate a temporary csv file name in /tmp
156151
# create adhoc dir if not
@@ -173,7 +168,7 @@
173168

174169
# %% tags=["remove-input"]
175170
# %%capture --no-display
176-
# %%R -i model_unit -w 800 -h 600 -i variable -i raw_extent -i concise -r 100
171+
# %%R -i model_unit -w 800 -h 600 -i variable -i concise -r 100
177172

178173

179174
if(concise == FALSE){
@@ -428,7 +423,7 @@
428423

429424
# %% tags=["remove-input"]
430425
# %%capture --no-display
431-
# %%R -i model_unit -w 800 -h 600 -i variable -i raw_extent -i concise -r 100
426+
# %%R -i model_unit -w 800 -h 600 -i variable -i concise -r 100
432427

433428
if (concise == FALSE){
434429

@@ -674,39 +669,7 @@
674669
b = 0, # Bottom margin
675670
l = 0)) # Left ggmargin
676671

677-
# figure out if it's a global file
678-
if(abs(raw_extent[1] - raw_extent[2]) > 350){
679-
gg1 <- gg1 +
680-
scale_x_continuous(breaks = c(-180, -90, 0, 90, 180), labels = c("180°W", "90°W", "0°", "90°E", "180°E"))+
681-
scale_y_continuous(breaks = c(-90, -45, 0, 45, 90), labels = c("90°S", "45°S", "0°", "45°N", "90°N"))
682672

683-
gg2 <- gg2 +
684-
scale_x_continuous(breaks = c(-180, -90, 0, 90, 180), labels = c("180°W", "90°W", "0°", "90°E", "180°E"))+
685-
scale_y_continuous(breaks = c(-90, -45, 0, 45, 90), labels = c("90°S", "45°S", "0°", "45°N", "90°N"))
686-
687-
gg3 <- gg3 +
688-
scale_x_continuous(breaks = c(-180, -90, 0, 90, 180), labels = c("180°W", "90°W", "0°", "90°E", "180°E"))+
689-
scale_y_continuous(breaks = c(-90, -45, 0, 45, 90), labels = c("90°S", "45°S", "0°", "45°N", "90°N"))
690-
691-
}
692-
693-
# appropriate plotting for northwest European Shelf
694-
if((raw_extent[1] > -30) & (raw_extent[2] < 20)){
695-
gg1 <- gg1 +
696-
scale_x_continuous(breaks = c(-20, -10, 0, 10), labels = c("20°W", "10°W", "0°", "10°E"))+
697-
scale_y_continuous(breaks = c(45, 50, 55, 60, 65), labels = c("45°N", "50°N", "55°N", "60°N", "65°N"))
698-
gg2 <- gg2 +
699-
scale_x_continuous(breaks = c(-20, -10, 0, 10), labels = c("20°W", "10°W", "0°", "10°E"))+
700-
scale_y_continuous(breaks = c(45, 50, 55, 60, 65), labels = c("45°N", "50°N", "55°N", "60°N", "65°N"))
701-
gg3 <- gg3 +
702-
scale_x_continuous(breaks = c(-20, -10, 0, 10), labels = c("20°W", "10°W", "0°", "10°E"))+
703-
scale_y_continuous(breaks = c(45, 50, 55, 60, 65), labels = c("45°N", "50°N", "55°N", "60°N", "65°N"))
704-
}
705-
706-
# gg1
707-
# reduce the size of the plot
708-
# options(repr.plot.width = 10, repr.plot.height = 3)
709-
# gg1
710673
cowplot::plot_grid(gg1, gg2, gg3, ncol = 1)
711674

712675
}

oceanval/utils.py

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,82 +19,3 @@ def extension_of_directory(starting_directory, exclude=[]):
1919

2020

2121

22-
def get_extent(ff):
23-
# add docstring
24-
""" "
25-
Get the extent of a netcdf file
26-
27-
Parameters
28-
----------
29-
ff : str
30-
The path to the netcdf file
31-
32-
Returns
33-
-------
34-
extent : list
35-
A list of the form [lon_min, lon_max, lat_min, lat_max]
36-
37-
"""
38-
39-
ds = nc.open_data(ff)
40-
ds.subset(variables=ds.variables[0])
41-
ds.top()
42-
ds.tmean()
43-
ds.as_missing(0)
44-
ds_xr = ds.to_xarray()
45-
lon_name = [x for x in ds_xr.coords if "lon" in x][0]
46-
lat_name = [x for x in ds_xr.coords if "lat" in x][0]
47-
48-
df = ds_xr.to_dataframe().reset_index()
49-
df = ds.to_dataframe().dropna().reset_index()
50-
df = df.rename(columns={lon_name: "lon", lat_name: "lat"})
51-
df = df.dropna()
52-
max_lon = df["lon"].max()
53-
if max_lon > 180:
54-
new_lon = df["lon"].values % 360
55-
new_lon = new_lon - 180
56-
lon_min = float(new_lon.min())
57-
lon_max = float(new_lon.max())
58-
else:
59-
lon_min = float(df.lon.min())
60-
lon_max = float(df.lon.max())
61-
lat_min = float(df.lat.min())
62-
lat_max = float(df.lat.max())
63-
return [lon_min, lon_max, lat_min, lat_max]
64-
65-
lons = ds_xr[lon_name].values
66-
lons = lons.flatten()
67-
# as a unique, sorted list
68-
lons = list(set(lons))
69-
lats = ds_xr[lat_name].values
70-
lats = lats.flatten()
71-
# as a unique, sorted list
72-
lats = list(set(lats))
73-
lats.sort()
74-
lons.sort()
75-
lon_res = np.abs(lons[2] - lons[1])
76-
lat_res = np.abs(lats[2] - lats[1])
77-
print(lon_res)
78-
print(lat_res)
79-
ds = nc.open_data(ff)
80-
ds.subset(variables=ds.variables[0])
81-
ds.top()
82-
ds.tmax()
83-
ds.to_latlon(lon=[-180, 180], lat=[-90, 90], res=[lon_res, lat_res])
84-
# rename
85-
lon_min = df.lon.min()
86-
lon_max = df.lon.max()
87-
lat_min = df.lat.min()
88-
lat_max = df.lat.max()
89-
lons = [lon_min, lon_max]
90-
lats = [lat_min, lat_max]
91-
extent = [
92-
lons[0] - lon_res,
93-
lons[1] + lon_res,
94-
lats[0] - lat_res,
95-
lats[1] + lat_res,
96-
]
97-
#
98-
return extent
99-
100-

0 commit comments

Comments
 (0)