Skip to content

Commit

Permalink
Merge branch '405_sic_ao' into 405_sic_ao_lee1043_finaltweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lee1043 committed Feb 2, 2024
2 parents da08a66 + 687e500 commit e5039a9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 54 deletions.
1 change: 1 addition & 0 deletions .binder/apt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
texlive-full
1 change: 1 addition & 0 deletions .binder/environment.yml
4 changes: 2 additions & 2 deletions .binder/postBuild
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
set -eux
python -m pip install -vv --no-deps --no-build-isolation -e .

pip install .
11 changes: 0 additions & 11 deletions apt.txt

This file was deleted.

1 change: 0 additions & 1 deletion environment.yml

This file was deleted.

99 changes: 64 additions & 35 deletions pcmdi_metrics/io/xcdat_openxml.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,89 @@
import glob
import os
import sys
from typing import Union

import xcdat
import xarray as xr
import xcdat as xc
import xmltodict


def xcdat_open(infile, data_var=None, decode_times=True):
"""
Parameter
---------
infile:
list of string, or string
File(s) to open using xcdat
data_var:
(Optional[str], optional) – The key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables, by default None.
Output
------
ds:
xcdat dataset
def xcdat_open(
infile: Union[str, list], data_var: str = None, decode_times: bool = True
) -> xr.Dataset:
"""Open input file (netCDF, or xml generated by cdscan)
Parameters
----------
infile : Union[str, list]
list of string, or string, for path of file(s) to open using xcdat
data_var : str, optional
key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables, by default None, which loads all data variables
decode_times : bool, optional
If True, attempt to decode times encoded in the standard NetCDF datetime format into cftime.datetime objects. Otherwise, leave them encoded as numbers. This keyword may not be supported by all the backends, by default True
Returns
-------
xr.Dataset
xarray dataset opened via xcdat
Usage
-----
>>> from pcmdi_metrics.io import xcdat_open
# Open a single netCDF file
>>> ds = xcdat_open('mydata.nc')
# Open multiple files
>>> ds2 = xcdat_open(['mydata1.nc', 'mydata2.nc'] # Open multipe netCDF files
# Open with specifing the variable 'ts'
>>> ds3 = xcdat_open(['mydata1.nc', 'mydata2.nc'], data_var='ts')
# Open an xml file
>>> ds = xcdat_open('mydata.xml')
"""
if isinstance(infile, list):
ds = xcdat.open_mfdataset(infile, data_var=data_var, decode_times=decode_times)
ds = xc.open_mfdataset(infile, data_var=data_var, decode_times=decode_times)
else:
if infile.split(".")[-1].lower() == "xml":
ds = xcdat_openxml(infile, data_var=data_var, decode_times=decode_times)
else:
ds = xcdat.open_mfdataset(
infile, data_var=data_var, decode_times=decode_times
)
ds = xc.open_dataset(infile, data_var=data_var, decode_times=decode_times)

return ds


def xcdat_openxml(xmlfile, data_var=None, decode_times=True):
"""
Parameter
---------
infile:
xml file to open using xcdat
data_var:
(Optional[str], optional) – The key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables, by default None.
Output
------
ds:
xcdat dataset
def xcdat_openxml(
xmlfile: str, data_var: str = None, decode_times: bool = True
) -> xr.Dataset:
"""Open input file (xml generated by cdscan)
Parameters
----------
infile: str
path of xml file to open using xcdat
data_var: str, optional
key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables, by default None, which loads all data variables
decode_times : bool, optional
If True, attempt to decode times encoded in the standard NetCDF datetime format into cftime.datetime objects. Otherwise, leave them encoded as numbers. This keyword may not be supported by all the backends, by default True
Returns
-------
xr.Dataset
xarray dataset opened via xcdat
"""
if not os.path.exists(xmlfile):
sys.exit("ERROR: File not exist: {}".format(xmlfile))
sys.exit(f"ERROR: File not exist: {xmlfile}")

with open(xmlfile) as fd:
with open(xmlfile, encoding="utf-8") as fd:
doc = xmltodict.parse(fd.read())

ncfile_list = glob.glob(os.path.join(doc["dataset"]["@directory"], "*.nc"))
ds = xcdat.open_mfdataset(ncfile_list, data_var=data_var, decode_times=decode_times)

if len(ncfile_list) > 1:
ds = xc.open_mfdataset(
ncfile_list, data_var=data_var, decode_times=decode_times
)
else:
ds = xc.open_dataset(
ncfile_list[0], data_var=data_var, decode_times=decode_times
)

return ds
6 changes: 1 addition & 5 deletions pcmdi_metrics/sea_ice/sea_ice_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,7 @@ def get_xy_coords(ds, xvar):
print("--------------------------------------------")
for rgn in real_clim:
print(rgn)
# Average all realizations, fix bounds, get climatologies and totals
# total_rgn = (totals_dict[rgn] / len(list_of_runs)).to_dataset(name=var)
# Get model mean
real_clim[rgn]["model_mean"][var] = real_clim[rgn]["model_mean"][
var
] / len(list_of_runs)
Expand Down Expand Up @@ -815,12 +814,9 @@ def get_xy_coords(ds, xvar):
]
sector_short = ["ca", "na", "np", "io", "sa", "sp"]
fig7, ax7 = plt.subplots(6, 1, figsize=(5, 9))
# mlabels = model_list + ["bootstrap"]
mlabels = model_list
ind = np.arange(len(mlabels)) # the x locations for the groups
# ind = np.arange(len(mods)+1) # the x locations for the groups
width = 0.3
# n = len(ind) - 1
n = len(ind)
for inds, sector in enumerate(sector_list):
# Assemble data
Expand Down

0 comments on commit e5039a9

Please sign in to comment.