In [ ]:
import xcdat as xc
import numpy as np
In [ ]:
inputfile = "/Users/lee1043/Documents/Research/git/pcmdi_metrics_20230620_pcmdi/pcmdi_metrics/doc/jupyter/Demo/demo_data/CMIP5_demo_data/ts_Amon_ACCESS1-0_historical_r1i1p1_185001-200512.nc"
data_var = "ts"
In [ ]:
ds = xc.open_dataset(inputfile)
In [ ]:
ds[data_var].isel(time=0).plot()
Out[Â ]:
<matplotlib.collections.QuadMesh at 0x14a814910>
In [ ]:
ds
Out[Â ]:
<xarray.Dataset> Size: 209MB Dimensions: (time: 1872, bnds: 2, lat: 145, lon: 192) Coordinates: * lat (lat) float64 1kB -90.0 -88.75 -87.5 -86.25 ... 87.5 88.75 90.0 * lon (lon) float64 2kB 0.0 1.875 3.75 5.625 ... 354.4 356.2 358.1 * time (time) object 15kB 1850-01-16 12:00:00 ... 2005-12-16 12:00:00 Dimensions without coordinates: bnds Data variables: time_bnds (time, bnds) object 30kB ... lat_bnds (lat, bnds) float64 2kB ... lon_bnds (lon, bnds) float64 3kB ... ts (time, lat, lon) float32 208MB ... Attributes: (12/28) institution: CSIRO (Commonwealth Scientific and Industrial Res... institute_id: CSIRO-BOM experiment_id: historical source: ACCESS1-0 2011. Atmosphere: AGCM v1.0 (N96 grid-p... model_id: ACCESS1-0 forcing: GHG, Oz, SA, Sl, Vl, BC, OC, (GHG = CO2, N2O, CH4... ... ... table_id: Table Amon (27 April 2011) 9c851218e3842df9a62ef3... title: ACCESS1-0 model output prepared for CMIP5 historical parent_experiment: pre-industrial control modeling_realm: atmos realization: 1 cmor_version: 2.8.0
In [ ]:
ds.isel(time=0).spatial.average(data_var)[data_var].item()
Out[Â ]:
285.88807821517435
In [ ]:
da = ds[data_var].isel(time=0).copy()
# Define the latitude and longitude ranges
lat_range = (20, 70)
lon_range = (210, 320)
# Create masks for the latitude and longitude ranges
lat_mask = (da.lat >= lat_range[0]) & (da.lat <= lat_range[1])
lon_mask = (da.lon >= lon_range[0]) & (da.lon <= lon_range[1])
# Use broadcasting to apply the masks across the array dimensions
da.values[np.ix_(lat_mask, lon_mask)] = np.nan
In [ ]:
da.plot()
Out[Â ]:
<matplotlib.collections.QuadMesh at 0x14a944c10>
In [ ]:
ds[data_var].values[0] = da
In [ ]:
ds[data_var].isel(time=0).plot()
Out[Â ]:
<matplotlib.collections.QuadMesh at 0x14a9b3f50>
In [ ]:
ds.isel(time=0).spatial.average(data_var)[data_var].item()
Out[Â ]:
286.89966412418875
In [ ]:
ds.isel(time=0).spatial.average(data_var, skipna=True)[data_var].item()
Out[Â ]:
286.89966412418875
In [ ]:
ds.isel(time=0).spatial.average(data_var, skipna=False)[data_var].item()
Out[Â ]:
nan
In [ ]:
ds.temporal.average(data_var)[data_var].plot()
Out[Â ]:
<matplotlib.collections.QuadMesh at 0x149eda710>
In [ ]:
ds.temporal.average(data_var, skipna=True)[data_var].plot()
Out[Â ]:
<matplotlib.collections.QuadMesh at 0x149f29710>
In [ ]:
ds.temporal.average(data_var, skipna=False)[data_var].plot()
Out[Â ]:
<matplotlib.collections.QuadMesh at 0x14aaa8810>