Skip to content

Commit 8c3dc0c

Browse files
committed
converted to xcdat
1 parent 023da4c commit 8c3dc0c

21 files changed

+311
-380
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@ ENV/
9090
# Rope project settings
9191
.ropeproject
9292

93+
# debug_data
94+
pcmdi_metrics/monsoon_wang/*.nc
95+
pcmdi_metrics/monsoon_wang/debug_regions_specs.py
96+
pcmdi_metrics/monsoon_wang/test_param.py
97+
9398
test/

pcmdi_metrics/io/default_regions_define.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import xcdat as xc
21
from typing import Union
32

43
import xarray as xr
4+
import xcdat as xc
55

6-
#from pcmdi_metrics.io import da_to_ds, get_longitude, select_subset
6+
# from pcmdi_metrics.io import da_to_ds, get_longitude, select_subset
77
from .xcdat_dataset_io import da_to_ds, get_longitude, select_subset
88

99

@@ -51,7 +51,7 @@ def load_regions_specs():
5151
# South American Monsoon
5252
"SAMM": {"domain": {"latitude": (-45.0, 0.0), "longitude": (240.0, 330.0)}},
5353
# North African Monsoon
54-
#"NAFM": {"domain": {"latitude": (0.0, 45.0), "longitude": (310.0, 60.0)}},
54+
# "NAFM": {"domain": {"latitude": (0.0, 45.0), "longitude": (310.0, 60.0)}},
5555
"NAFM": {"domain": {"latitude": (0.0, 45.0), "longitude": (-50, 60.0)}},
5656
# South African Monsoon
5757
"SAFM": {"domain": {"latitude": (-45.0, 0.0), "longitude": (0.0, 90.0)}},
@@ -77,7 +77,7 @@ def load_regions_specs():
7777
return regions_specs
7878

7979

80-
#def region_subset(ds, regions_specs, region=None):
80+
# def region_subset(ds, regions_specs, region=None):
8181
# """
8282
# d: xarray.Dataset
8383
# regions_specs: dict
@@ -132,6 +132,7 @@ def load_regions_specs():
132132
#
133133
# return ds
134134

135+
135136
def region_subset(
136137
ds: Union[xr.Dataset, xr.DataArray],
137138
region: str,

pcmdi_metrics/io/xcdat_dataset_io.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import xarray as xr
44
import xcdat as xc
55

6-
76
# Internal function
87

98

pcmdi_metrics/monsoon_sperber/driver_monsoon_sperber.py

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,18 @@
4646
from shutil import copyfile
4747

4848
import matplotlib
49-
matplotlib.use('Agg')
50-
#import matplotlib.pyplot as plt
51-
from matplotlib import pyplot as plt
49+
50+
matplotlib.use("Agg")
5251
import numpy as np
5352
import pandas as pd
5453
import xarray as xr
5554
import xcdat as xc
55+
# import matplotlib.pyplot as plt
56+
from matplotlib import pyplot as plt
5657

5758
import pcmdi_metrics
5859
from pcmdi_metrics import resources
59-
from pcmdi_metrics.io import load_regions_specs, region_subset
60+
from pcmdi_metrics.io import load_regions_specs, region_subset, xcdat_open
6061
from pcmdi_metrics.mean_climate.lib import pmp_parser
6162
from pcmdi_metrics.monsoon_sperber.lib import (
6263
AddParserArgument,
@@ -67,7 +68,6 @@
6768
sperber_metrics,
6869
)
6970
from pcmdi_metrics.utils import create_land_sea_mask, fill_template
70-
from pcmdi_metrics.io import xcdat_open
7171

7272

7373
def tree():
@@ -92,7 +92,7 @@ def pick_year_last_day(ds):
9292
# =================================================
9393
# Hard coded options... will be moved out later
9494
# -------------------------------------------------
95-
#list_monsoon_regions = ["AIR", "AUS", "Sahel", "GoG", "NAmo", "SAmo"]
95+
# list_monsoon_regions = ["AIR", "AUS", "Sahel", "GoG", "NAmo", "SAmo"]
9696
list_monsoon_regions = ["AIR", "Sahel"]
9797
# list_monsoon_regions = ["all"]
9898

@@ -293,7 +293,16 @@ def pick_year_last_day(ds):
293293
modpath(model=model, exp=exp, realization=realization, variable=var)
294294
)
295295
if debug:
296-
print("model: ", model, " exp: ", exp, " realization: ", realization, " variable: ", var)
296+
print(
297+
"model: ",
298+
model,
299+
" exp: ",
300+
exp,
301+
" realization: ",
302+
realization,
303+
" variable: ",
304+
var,
305+
)
297306
print("debug: model_path_list: ", model_path_list)
298307
# land fraction
299308
model_lf_path = modpath_lf(model=model)
@@ -314,18 +323,18 @@ def pick_year_last_day(ds):
314323
ds_lf = xcdat_open(model_lf_path)
315324
except Exception:
316325
ds_lf = None
317-
318-
if not ds_lf:
326+
327+
if not ds_lf:
319328
lf_array = create_land_sea_mask(ds_lf, method="pcmdi")
320329
ds_lf = lf_array.to_dataset().compute()
321330
ds_lf = ds_lf.rename_vars({"lsmask": "sftlf"})
322331

323-
# use pcmdi mask
324-
# lf_array = create_land_sea_mask(ds_lf, method="pcmdi")
325-
# ds_lf = lf_array.to_dataset().compute()
326-
# ds_lf = ds_lf.rename_vars({"lsmask": "sftlf"})
332+
# use pcmdi mask
333+
# lf_array = create_land_sea_mask(ds_lf, method="pcmdi")
334+
# ds_lf = lf_array.to_dataset().compute()
335+
# ds_lf = ds_lf.rename_vars({"lsmask": "sftlf"})
327336

328-
if model in [ "EC-EARTH" ]: #, "BNU-ESM" ]:
337+
if model in ["EC-EARTH"]: # , "BNU-ESM" ]:
329338
ds_lf = ds_lf.isel(lat=slice(None, None, -1))
330339
lf = ds_lf.sftlf.sel(lat=slice(-90, 90)) # land frac file must be global
331340

@@ -350,10 +359,9 @@ def pick_year_last_day(ds):
350359
# Get time coordinate information
351360
print("model_path = ", model_path)
352361

353-
354362
dc = xcdat_open(model_path, decode_times=True)
355-
dc['time'].attrs['axis'] = 'T'
356-
dc['time'].attrs['standard_name'] = 'time'
363+
dc["time"].attrs["axis"] = "T"
364+
dc["time"].attrs["standard_name"] = "time"
357365
dc = xr.decode_cf(dc, decode_times=True)
358366
dc = dc.bounds.add_missing_bounds("X")
359367
dc = dc.bounds.add_missing_bounds("Y")
@@ -363,7 +371,6 @@ def pick_year_last_day(ds):
363371
c = xc.center_times(dc)
364372
eday = pick_year_last_day(dc)
365373

366-
367374
# Get starting and ending year and month
368375
startYear = c.time.values[0].year
369376
startMonth = c.time.values[0].month
@@ -493,7 +500,6 @@ def pick_year_last_day(ds):
493500
d.values = d.values * 86400.0
494501
d["units"] = units
495502

496-
497503
# variable for over land only
498504
d_land = model_land_only(model, d, lf, debug=debug)
499505

@@ -523,7 +529,6 @@ def pick_year_last_day(ds):
523529
d_sub_pr.values = d_sub_pr.values * 86400.0
524530
d_sub_pr["units"] = units
525531

526-
527532
else:
528533
# land-only rainfall
529534

@@ -544,11 +549,9 @@ def pick_year_last_day(ds):
544549
model, d_sub_pr, lf_sub, debug=debug
545550
)
546551

547-
548552
d_sub_pr.values = d_sub_pr.values * 86400.0
549553
d_sub_pr["units"] = units
550554

551-
552555
# Area average
553556

554557
ds_sub_pr = d_sub_pr.to_dataset().compute()
@@ -557,7 +560,6 @@ def pick_year_last_day(ds):
557560
ds_sub_pr = ds_sub_pr.bounds.add_missing_bounds("Y")
558561
ds_sub_pr = ds_sub_pr.bounds.add_missing_bounds("T")
559562

560-
561563
if "lat_bnds" not in ds_sub_pr.variables:
562564
lat_bnds = dc["lat_bnds"].sel(lat=ds_sub_pr["lat"])
563565
ds_sub_pr["lat_bnds"] = lat_bnds
@@ -567,8 +569,6 @@ def pick_year_last_day(ds):
567569
).compute()
568570
d_sub_aave = ds_sub_aave.pr
569571

570-
571-
572572
if debug:
573573
print("debug: region:", region)
574574

@@ -603,7 +603,6 @@ def pick_year_last_day(ds):
603603

604604
d_sub_aave = xr.concat([part1, part2], dim="time")
605605

606-
607606
if debug:
608607
print(
609608
"debug: ",
@@ -638,7 +637,6 @@ def pick_year_last_day(ds):
638637
coords={"time": time_coords},
639638
)
640639

641-
642640
if debug:
643641
print(
644642
"debug: pentad_time_series length: ",
@@ -648,14 +646,14 @@ def pick_year_last_day(ds):
648646
# Keep pentad time series length in consistent
649647
ref_length = int(365 / n)
650648
if len(pentad_time_series) < ref_length:
651-
652649
pentad_time_series = pentad_time_series.interp(
653-
time=pd.date_range(time_coords[0], time_coords[-1], periods=ref_length)
650+
time=pd.date_range(
651+
time_coords[0], time_coords[-1], periods=ref_length
652+
)
654653
)
655654

656655
time_coords = pentad_time_series.coords["time"]
657656

658-
659657
pentad_time_series_cumsum = np.cumsum(pentad_time_series)
660658
pentad_time_series = xr.DataArray(
661659
pentad_time_series,
@@ -672,7 +670,6 @@ def pick_year_last_day(ds):
672670
pentad_time_series_cumsum.attrs["units"] = str(d.units.values)
673671
pentad_time_series_cumsum.coords["time"] = time_coords
674672

675-
676673
if nc_out:
677674
# Archive individual year time series in netCDF file
678675
pentad_time_series.to_netcdf(file_path, mode="a")
@@ -819,9 +816,7 @@ def pick_year_last_day(ds):
819816

820817
# obs
821818
ax[region].plot(
822-
np.array(
823-
dict_obs_composite[reference_data_name][region]
824-
),
819+
np.array(dict_obs_composite[reference_data_name][region]),
825820
c="blue",
826821
label=reference_data_name,
827822
)
@@ -836,9 +831,9 @@ def pick_year_last_day(ds):
836831
ax[region].axvline(
837832
x=idx,
838833
ymin=0,
839-
ymax=dict_obs_composite[reference_data_name][
840-
region
841-
][idx].item(),
834+
ymax=dict_obs_composite[reference_data_name][region][
835+
idx
836+
].item(),
842837
c="blue",
843838
ls="--",
844839
)

pcmdi_metrics/monsoon_sperber/lib/argparse_functions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def AddParserArgument(P):
5454
"--meyear", dest="meyear", type=int, help="End year for model data set"
5555
)
5656
P.add_argument("--modnames", type=str, default=None, help="List of models")
57-
P.add_argument("--list_monsoon_regions", type=str, default=None, help="List of regions")
57+
P.add_argument(
58+
"--list_monsoon_regions", type=str, default=None, help="List of regions"
59+
)
5860
P.add_argument(
5961
"-r",
6062
"--realization",

pcmdi_metrics/monsoon_sperber/lib/divide_chunks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def divide_chunks_advanced(data, n, debug=False):
3030
day = day.values
3131
calendar = "gregorian"
3232
if debug:
33-
#print("month = ", month, "day = ", day)
33+
# print("month = ", month, "day = ", day)
3434
print("debug: first day of year is " + str(month) + "/" + str(day))
3535
if month not in [1, 7] or day != 1:
3636
sys.exit(

pcmdi_metrics/monsoon_sperber/lib/model_land_only.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def model_land_only(model, model_timeseries, lf, debug=False):
99
# - - - - - - - - - - - - - - - - - - - - - - - - -
1010

1111
if debug:
12-
#plot_map(model_timeseries[0], "_".join(["test", model, "beforeMask.png"]))
12+
# plot_map(model_timeseries[0], "_".join(["test", model, "beforeMask.png"]))
1313
print("debug: plot for beforeMask done")
1414

1515
# Check land fraction variable to see if it meet criteria
@@ -33,7 +33,7 @@ def model_land_only(model, model_timeseries, lf, debug=False):
3333
model_timeseries_masked = model_timeseries.where(lf > 90)
3434

3535
if debug:
36-
#plot_map(model_timeseries_masked[0], "_".join(["test", model, "afterMask.png"]))
36+
# plot_map(model_timeseries_masked[0], "_".join(["test", model, "afterMask.png"]))
3737
print("debug: plot for afterMask done")
3838

3939
return model_timeseries_masked

pcmdi_metrics/monsoon_sperber/param/Bo_param.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
# -------------------------------------------------
1515
update_json = False
1616
debug = False
17-
#debug = True
17+
# debug = True
1818

19-
#list_monsoon_regions = ["AIR", "AUS", "Sahel", "GoG", "NAmo", "SAmo"]
19+
# list_monsoon_regions = ["AIR", "AUS", "Sahel", "GoG", "NAmo", "SAmo"]
2020
list_monsoon_regions = ["AUS"]
2121
# =================================================
2222
# Observation
@@ -41,7 +41,7 @@
4141
modpath = "/work/lee1043/ESGF/xmls/cmip5/historical/day/pr/cmip5.%(model).%(exp).%(realization).day.pr.xml"
4242
modpath_lf = "/work/lee1043/ESGF/xmls/cmip5/historical/fx/sftlf/cmip5.%(model).historical.r0i0p0.fx.sftlf.xml"
4343

44-
#/p/css03/scratch/published-older/cmip5/output1/CSIRO-BOM/ACCESS1-0/historical/day/atmos/day/r1i1p1/v4/pr/pr_day_ACCESS1-0_historical_r1i1p1_19750101-19991231.nc
44+
# /p/css03/scratch/published-older/cmip5/output1/CSIRO-BOM/ACCESS1-0/historical/day/atmos/day/r1i1p1/v4/pr/pr_day_ACCESS1-0_historical_r1i1p1_19750101-19991231.nc
4545

4646
# modnames = ['ACCESS1-0', 'ACCESS1-3', 'BCC-CSM1-1', 'BCC-CSM1-1-M', 'BNU-ESM', 'CanCM4', 'CanESM2', 'CCSM4', 'CESM1-BGC', 'CESM1-CAM5', 'CESM1-FASTCHEM', 'CMCC-CESM', 'CMCC-CM', 'CMCC-CMS', 'CNRM-CM5', 'CSIRO-Mk3-6-0', 'EC-EARTH', 'FGOALS-g2', 'GFDL-CM3', 'GFDL-ESM2G', 'GFDL-ESM2M', 'GISS-E2-H', 'GISS-E2-R', 'HadGEM2-AO', 'HadGEM2-CC', 'HadGEM2-ES', 'INMCM4', 'IPSL-CM5A-LR', 'IPSL-CM5A-MR', 'IPSL-CM5B-LR', 'MIROC-ESM', 'MIROC-ESM-CHEM', 'MIROC4h', 'MIROC5', 'MPI-ESM-MR', 'MPI-ESM-P', 'MRI-CGCM3', 'MRI-ESM1', 'NorESM1-M'] # noqa
4747

@@ -60,7 +60,7 @@
6060
# =================================================
6161
# Output
6262
# -------------------------------------------------
63-
#pmprdir = "/p/user_pub/pmp/pmp_results/pmp_v1.1.2"
63+
# pmprdir = "/p/user_pub/pmp/pmp_results/pmp_v1.1.2"
6464
pmprdir = "/p/user_pub/climate_work/dong12/PMP_result/"
6565
case_id = "{:v%Y%m%d}".format(datetime.datetime.now())
6666

0 commit comments

Comments
 (0)