Skip to content

Commit

Permalink
Changes to be committed:
Browse files Browse the repository at this point in the history
   (use "git reset HEAD <file>..." to unstage)

	modified:   driver_monsoon_sperber.py

 Untracked files:
   (use "git add <file>..." to include in what will be committed)

	../../pcmdi_utils/
  • Loading branch information
bosup committed Jan 14, 2024
1 parent e2cc991 commit f23c7c8
Showing 1 changed file with 47 additions and 24 deletions.
71 changes: 47 additions & 24 deletions pcmdi_metrics/monsoon_sperber/driver_monsoon_sperber.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import numpy as np
import xarray as xr
import xcdat as xc
import pandas as pd

import pcmdi_metrics
from pcmdi_metrics import resources
Expand Down Expand Up @@ -346,13 +347,14 @@ def tree():
mip, model, exp, run, "monsoon_sperber", startYear, endYear
)

fout = cdms2.open(
os.path.join(
file_path = os.path.join(
outdir(output_type="diagnostic_results"),
output_filename + ".nc",
),
"w",
)
)
try:
fout = xr.open_dataset(file_path, mode='a') # 'a' stands for append mode
except FileNotFoundError:
fout = xr.Dataset()

# Plotting setup
if plot:
Expand Down Expand Up @@ -519,6 +521,7 @@ def tree():
)

pentad_time_series = []
time_coords = np.array([], dtype='datetime64')

for d_sub_aave_chunk in list_d_sub_aave_chunks:
# ignore when chunk length is shorter than defined
Expand All @@ -529,6 +532,10 @@ def tree():
axis=0, skipna=True
).compute()
pentad_time_series.append(float(ave_chunk))
datetime_str = str(d_sub_aave_chunk['time'][0].values)
datetime = pd.to_datetime([datetime_str[:10]])
time_coords = np.concatenate([time_coords, datetime])
time_coords = pd.to_datetime(time_coords)

if debug:
print(
Expand All @@ -543,17 +550,20 @@ def tree():
pentad_time_series, ref_length, debug=debug
)

pentad_time_series_cumsum = np.cumsum(pentad_time_series)
pentad_time_series = xr.DataArray(pentad_time_series)
pentad_time_series.attrs["units"] = d.units
pentad_time_series_cumsum = np.cumsum(pentad_time_series)
pentad_time_series = xr.DataArray(pentad_time_series,dims='time', name=region + "_" + str(year))
pentad_time_series.attrs['units'] = str(d.units.values)
pentad_time_series.coords['time'] = time_coords

pentad_time_series_cumsum = xr.DataArray(pentad_time_series_cumsum,dims='time',\
name=region + "_" + str(year) + "_cumsum")
pentad_time_series_cumsum.attrs['units'] = str(d.units.values)
pentad_time_series_cumsum.coords['time'] = time_coords

if nc_out:
# Archive individual year time series in netCDF file
fout.write(pentad_time_series, id=region + "_" + str(year))
fout.write(
pentad_time_series_cumsum,
id=region + "_" + str(year) + "_cumsum",
)
pentad_time_series.to_netcdf(file_path, mode='a')
pentad_time_series_cumsum.to_netcdf(file_path, mode='a')

"""
if plot:
Expand Down Expand Up @@ -610,6 +620,23 @@ def tree():
composite_pentad_time_series_cumsum_normalized = metrics_result[
"frac_accum"
]

composite_pentad_time_series = xr.DataArray(composite_pentad_time_series, \
dims='time', name=region + "_comp")
composite_pentad_time_series.attrs['units'] = str(d.units)
composite_pentad_time_series.coords['time'] = time_coords

composite_pentad_time_series_cumsum = xr.DataArray(composite_pentad_time_series_cumsum, \
dims='time', name=region + "_comp_cumsum")
composite_pentad_time_series_cumsum.attrs['units'] = str(d.units)
composite_pentad_time_series_cumsum.coords['time'] = time_coords

composite_pentad_time_series_cumsum_normalized = xr.DataArray(\
composite_pentad_time_series_cumsum_normalized,\
dims='time', name=region + "_comp_cumsum_fraction")
composite_pentad_time_series_cumsum_normalized.attrs['units'] = str(d.units)
composite_pentad_time_series_cumsum_normalized.coords['time'] = time_coords

if model == "obs":
dict_obs_composite[reference_data_name][region] = {}
dict_obs_composite[reference_data_name][
Expand All @@ -632,15 +659,11 @@ def tree():

# Archice in netCDF file
if nc_out:
fout.write(composite_pentad_time_series, id=region + "_comp")
fout.write(
composite_pentad_time_series_cumsum,
id=region + "_comp_cumsum",
)
fout.write(
composite_pentad_time_series_cumsum_normalized,
id=region + "_comp_cumsum_fraction",
)

composite_pentad_time_series.to_netcdf(file_path, mode='a')
composite_pentad_time_series_cumsum.to_netcdf(file_path, mode='a')
composite_pentad_time_series_cumsum_normalized.to_netcdf(file_path, mode='a')

if region == list_monsoon_regions[-1]:
fout.close()

Expand All @@ -664,7 +687,7 @@ def tree():
ymin=0,
ymax=composite_pentad_time_series_cumsum_normalized[
idx
],
].item(),
c="red",
ls="--",
)
Expand All @@ -687,7 +710,7 @@ def tree():
ymin=0,
ymax=dict_obs_composite[reference_data_name][region][
idx
],
].item(),
c="blue",
ls="--",
)
Expand Down

0 comments on commit f23c7c8

Please sign in to comment.