Skip to content

Commit 72ccad4

Browse files
committed
fixed bug in hwsd2 convert.py and created simple visualize_netcdf.py script
1 parent fea7286 commit 72ccad4

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

HWSD2/convert.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
#####################################################
2828

2929
# main parameters
30-
# VAR = "cSoil"
31-
VAR = "cSoilAbove1m"
32-
# LAYERS = ["D1", "D2", "D3", "D4", "D5", "D6", "D7"] # cSoil
33-
LAYERS = ["D1", "D2", "D3", "D4", "D5"] # cSoilAbove1m
30+
VAR = "cSoil"
31+
# VAR = "cSoilAbove1m"
32+
LAYERS = ["D1", "D2", "D3", "D4", "D5", "D6", "D7"] # cSoil
33+
# LAYERS = ["D1", "D2", "D3", "D4", "D5"] # cSoilAbove1m
3434
POOLS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # soil types
3535
SDATE = datetime.datetime(1960, 1, 1)
3636
EDATE = datetime.datetime(2022, 1, 1)
@@ -303,11 +303,13 @@ def create_netcdf(
303303
)
304304

305305
# export as netcdf
306-
time_range = f"{ds['time'].min().dt.year:d}{ds['time'].min().dt.month:02d}"
307-
time_range += f"-{ds['time'].max().dt.year:d}{ds['time'].max().dt.month:02d}"
308306
ds.to_netcdf(
309-
"{variable}_{frequency}_{source_id}_{time_mark}.nc".format(
310-
variable=var, frequency="fx", source_id="HWSD2", time_mark=time_range
307+
"{variable}_{frequency}_{source_id}_{st_date}-{en_date}.nc".format(
308+
variable=var,
309+
frequency="fx",
310+
source_id="HWSD2",
311+
st_date=sdate.strftime("%Y%m%d"),
312+
en_date=edate.strftime("%Y%m%d"),
311313
),
312314
encoding={VAR: {"zlib": True}},
313315
)

scripts/visualize_netcdf.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from pathlib import Path
2+
3+
import matplotlib.pyplot as plt
4+
import xarray as xr
5+
6+
# set parameters
7+
wdir = "../HWSD2"
8+
file_path = f"{wdir}/cSoil_fx_HWSD2_19600101-20220101.nc"
9+
tstep = 0
10+
vmin = 0 # Minimum value for colormap
11+
vmax = 100 # Maximum value for colormap
12+
var = "cSoil"
13+
14+
# open netcdf and select time step
15+
base_name = Path(file_path).stem
16+
data = xr.open_dataset(file_path)
17+
da = data[var].isel(time=tstep)
18+
19+
# create and save full map
20+
plt.figure(figsize=(10, 6))
21+
p = da.plot(vmin=vmin, vmax=vmax)
22+
plt.savefig(f"{wdir}/{base_name}_timestep_{tstep}.png", dpi=300, bbox_inches="tight")
23+
plt.close()
24+
25+
#### Create zoomed-in map ####
26+
# Define Southeastern US bounding box
27+
lon_min, lon_max = -95, -75
28+
lat_min, lat_max = 25, 37
29+
30+
# Determine proper slicing directions
31+
lat_vals = da["lat"].values
32+
lon_vals = da["lon"].values
33+
34+
lat_slice = (
35+
slice(lat_min, lat_max) if lat_vals[0] < lat_vals[-1] else slice(lat_max, lat_min)
36+
)
37+
lon_slice = (
38+
slice(lon_min, lon_max) if lon_vals[0] < lon_vals[-1] else slice(lon_max, lon_min)
39+
)
40+
41+
# Clip the data dynamically
42+
da_se = da.sel(lon=lon_slice, lat=lat_slice)
43+
44+
# Plot the clipped region
45+
plt.figure(figsize=(8, 6))
46+
p = da_se.plot(vmin=vmin, vmax=vmax)
47+
plt.savefig(f"{wdir}/{base_name}_SE-US_zoom.png", dpi=300, bbox_inches="tight")
48+
plt.close()

0 commit comments

Comments
 (0)