https://github.com/xCDAT/xcdat/issues/582

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>
No description has been provided for this image
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
xarray.Dataset
    • time: 1872
    • bnds: 2
    • lat: 145
    • lon: 192
    • lat
      (lat)
      float64
      -90.0 -88.75 -87.5 ... 88.75 90.0
      bounds :
      lat_bnds
      units :
      degrees_north
      axis :
      Y
      long_name :
      latitude
      standard_name :
      latitude
      array([-90.  , -88.75, -87.5 , -86.25, -85.  , -83.75, -82.5 , -81.25, -80.  ,
             -78.75, -77.5 , -76.25, -75.  , -73.75, -72.5 , -71.25, -70.  , -68.75,
             -67.5 , -66.25, -65.  , -63.75, -62.5 , -61.25, -60.  , -58.75, -57.5 ,
             -56.25, -55.  , -53.75, -52.5 , -51.25, -50.  , -48.75, -47.5 , -46.25,
             -45.  , -43.75, -42.5 , -41.25, -40.  , -38.75, -37.5 , -36.25, -35.  ,
             -33.75, -32.5 , -31.25, -30.  , -28.75, -27.5 , -26.25, -25.  , -23.75,
             -22.5 , -21.25, -20.  , -18.75, -17.5 , -16.25, -15.  , -13.75, -12.5 ,
             -11.25, -10.  ,  -8.75,  -7.5 ,  -6.25,  -5.  ,  -3.75,  -2.5 ,  -1.25,
               0.  ,   1.25,   2.5 ,   3.75,   5.  ,   6.25,   7.5 ,   8.75,  10.  ,
              11.25,  12.5 ,  13.75,  15.  ,  16.25,  17.5 ,  18.75,  20.  ,  21.25,
              22.5 ,  23.75,  25.  ,  26.25,  27.5 ,  28.75,  30.  ,  31.25,  32.5 ,
              33.75,  35.  ,  36.25,  37.5 ,  38.75,  40.  ,  41.25,  42.5 ,  43.75,
              45.  ,  46.25,  47.5 ,  48.75,  50.  ,  51.25,  52.5 ,  53.75,  55.  ,
              56.25,  57.5 ,  58.75,  60.  ,  61.25,  62.5 ,  63.75,  65.  ,  66.25,
              67.5 ,  68.75,  70.  ,  71.25,  72.5 ,  73.75,  75.  ,  76.25,  77.5 ,
              78.75,  80.  ,  81.25,  82.5 ,  83.75,  85.  ,  86.25,  87.5 ,  88.75,
              90.  ])
    • lon
      (lon)
      float64
      0.0 1.875 3.75 ... 356.2 358.1
      bounds :
      lon_bnds
      units :
      degrees_east
      axis :
      X
      long_name :
      longitude
      standard_name :
      longitude
      array([  0.   ,   1.875,   3.75 ,   5.625,   7.5  ,   9.375,  11.25 ,  13.125,
              15.   ,  16.875,  18.75 ,  20.625,  22.5  ,  24.375,  26.25 ,  28.125,
              30.   ,  31.875,  33.75 ,  35.625,  37.5  ,  39.375,  41.25 ,  43.125,
              45.   ,  46.875,  48.75 ,  50.625,  52.5  ,  54.375,  56.25 ,  58.125,
              60.   ,  61.875,  63.75 ,  65.625,  67.5  ,  69.375,  71.25 ,  73.125,
              75.   ,  76.875,  78.75 ,  80.625,  82.5  ,  84.375,  86.25 ,  88.125,
              90.   ,  91.875,  93.75 ,  95.625,  97.5  ,  99.375, 101.25 , 103.125,
             105.   , 106.875, 108.75 , 110.625, 112.5  , 114.375, 116.25 , 118.125,
             120.   , 121.875, 123.75 , 125.625, 127.5  , 129.375, 131.25 , 133.125,
             135.   , 136.875, 138.75 , 140.625, 142.5  , 144.375, 146.25 , 148.125,
             150.   , 151.875, 153.75 , 155.625, 157.5  , 159.375, 161.25 , 163.125,
             165.   , 166.875, 168.75 , 170.625, 172.5  , 174.375, 176.25 , 178.125,
             180.   , 181.875, 183.75 , 185.625, 187.5  , 189.375, 191.25 , 193.125,
             195.   , 196.875, 198.75 , 200.625, 202.5  , 204.375, 206.25 , 208.125,
             210.   , 211.875, 213.75 , 215.625, 217.5  , 219.375, 221.25 , 223.125,
             225.   , 226.875, 228.75 , 230.625, 232.5  , 234.375, 236.25 , 238.125,
             240.   , 241.875, 243.75 , 245.625, 247.5  , 249.375, 251.25 , 253.125,
             255.   , 256.875, 258.75 , 260.625, 262.5  , 264.375, 266.25 , 268.125,
             270.   , 271.875, 273.75 , 275.625, 277.5  , 279.375, 281.25 , 283.125,
             285.   , 286.875, 288.75 , 290.625, 292.5  , 294.375, 296.25 , 298.125,
             300.   , 301.875, 303.75 , 305.625, 307.5  , 309.375, 311.25 , 313.125,
             315.   , 316.875, 318.75 , 320.625, 322.5  , 324.375, 326.25 , 328.125,
             330.   , 331.875, 333.75 , 335.625, 337.5  , 339.375, 341.25 , 343.125,
             345.   , 346.875, 348.75 , 350.625, 352.5  , 354.375, 356.25 , 358.125])
    • time
      (time)
      object
      1850-01-16 12:00:00 ... 2005-12-...
      bounds :
      time_bnds
      axis :
      T
      long_name :
      time
      standard_name :
      time
      array([cftime.DatetimeProlepticGregorian(1850, 1, 16, 12, 0, 0, 0, has_year_zero=True),
             cftime.DatetimeProlepticGregorian(1850, 2, 15, 0, 0, 0, 0, has_year_zero=True),
             cftime.DatetimeProlepticGregorian(1850, 3, 16, 12, 0, 0, 0, has_year_zero=True),
             ...,
             cftime.DatetimeProlepticGregorian(2005, 10, 16, 12, 0, 0, 0, has_year_zero=True),
             cftime.DatetimeProlepticGregorian(2005, 11, 16, 0, 0, 0, 0, has_year_zero=True),
             cftime.DatetimeProlepticGregorian(2005, 12, 16, 12, 0, 0, 0, has_year_zero=True)],
            dtype=object)
    • time_bnds
      (time, bnds)
      object
      ...
      [3744 values with dtype=object]
    • lat_bnds
      (lat, bnds)
      float64
      ...
      [290 values with dtype=float64]
    • lon_bnds
      (lon, bnds)
      float64
      ...
      [384 values with dtype=float64]
    • ts
      (time, lat, lon)
      float32
      ...
      standard_name :
      surface_temperature
      long_name :
      Surface Temperature
      comment :
      ""skin"" temperature (i.e., SST for open ocean)
      units :
      K
      cell_methods :
      time: mean
      cell_measures :
      area: areacella
      history :
      2012-01-17T03:16:09Z altered by CMOR: replaced missing value flag (-1.07374e+09) with standard missing value (1e+20).
      associated_files :
      baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_atmos_fx_ACCESS1-0_historical_r0i0p0.nc areacella: areacella_fx_ACCESS1-0_historical_r0i0p0.nc
      [52116480 values with dtype=float32]
    • lat
      PandasIndex
      PandasIndex(Index([ -90.0, -88.75,  -87.5, -86.25,  -85.0, -83.75,  -82.5, -81.25,  -80.0,
             -78.75,
             ...
              78.75,   80.0,  81.25,   82.5,  83.75,   85.0,  86.25,   87.5,  88.75,
               90.0],
            dtype='float64', name='lat', length=145))
    • lon
      PandasIndex
      PandasIndex(Index([    0.0,   1.875,    3.75,   5.625,     7.5,   9.375,   11.25,  13.125,
                15.0,  16.875,
             ...
              341.25, 343.125,   345.0, 346.875,  348.75, 350.625,   352.5, 354.375,
              356.25, 358.125],
            dtype='float64', name='lon', length=192))
    • time
      PandasIndex
      PandasIndex(CFTimeIndex([1850-01-16 12:00:00, 1850-02-15 00:00:00, 1850-03-16 12:00:00,
                   1850-04-16 00:00:00, 1850-05-16 12:00:00, 1850-06-16 00:00:00,
                   1850-07-16 12:00:00, 1850-08-16 12:00:00, 1850-09-16 00:00:00,
                   1850-10-16 12:00:00,
                   ...
                   2005-03-16 12:00:00, 2005-04-16 00:00:00, 2005-05-16 12:00:00,
                   2005-06-16 00:00:00, 2005-07-16 12:00:00, 2005-08-16 12:00:00,
                   2005-09-16 00:00:00, 2005-10-16 12:00:00, 2005-11-16 00:00:00,
                   2005-12-16 12:00:00],
                  dtype='object',
                  length=1872,
                  calendar='proleptic_gregorian',
                  freq=None))
  • institution :
    CSIRO (Commonwealth Scientific and Industrial Research Organisation, Australia), and BOM (Bureau of Meteorology, Australia)
    institute_id :
    CSIRO-BOM
    experiment_id :
    historical
    source :
    ACCESS1-0 2011. Atmosphere: AGCM v1.0 (N96 grid-point, 1.875 degrees EW x approx 1.25 degree NS, 38 levels); ocean: NOAA/GFDL MOM4p1 (nominal 1.0 degree EW x 1.0 degrees NS, tripolar north of 65N, equatorial refinement to 1/3 degree from 10S to 10 N, cosine dependent NS south of 25S, 50 levels); sea ice: CICE4.1 (nominal 1.0 degree EW x 1.0 degrees NS, tripolar north of 65N, equatorial refinement to 1/3 degree from 10S to 10 N, cosine dependent NS south of 25S); land: MOSES2 (1.875 degree EW x 1.25 degree NS, 4 levels
    model_id :
    ACCESS1-0
    forcing :
    GHG, Oz, SA, Sl, Vl, BC, OC, (GHG = CO2, N2O, CH4, CFC11, CFC12, CFC113, HCFC22, HFC125, HFC134a)
    parent_experiment_id :
    piControl
    parent_experiment_rip :
    r1i1p1
    branch_time :
    109207.0
    contact :
    The ACCESS wiki: http://wiki.csiro.au/confluence/display/ACCESS/Home. Contact Tony.Hirst@csiro.au regarding the ACCESS coupled climate model. Contact Peter.Uhe@csiro.au regarding ACCESS coupled climate model CMIP5 datasets.
    history :
    CMIP5 compliant file produced from raw ACCESS model output using the ACCESS Post-Processor and CMOR2. 2012-01-17T03:16:09Z CMOR rewrote data to comply with CF standards and CMIP5 requirements.
    references :
    See http://wiki.csiro.au/confluence/display/ACCESS/ACCESS+Publications
    initialization_method :
    1
    physics_version :
    1
    tracking_id :
    3ba59dfe-51df-471d-ae2f-74959d08bd9f
    version_number :
    v20120115
    product :
    output
    experiment :
    historical
    frequency :
    mon
    creation_date :
    2012-01-17T03:16:09Z
    Conventions :
    CF-1.4
    project_id :
    CMIP5
    table_id :
    Table Amon (27 April 2011) 9c851218e3842df9a62ef38b1e2575bb
    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>
No description has been provided for this image
In [ ]:
ds[data_var].values[0] = da
In [ ]:
ds[data_var].isel(time=0).plot()
Out[ ]:
<matplotlib.collections.QuadMesh at 0x14a9b3f50>
No description has been provided for this image
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

https://docs.xarray.dev/en/stable/generated/xarray.DataArray.mean.html

In [ ]:
ds.temporal.average(data_var)[data_var].plot()
Out[ ]:
<matplotlib.collections.QuadMesh at 0x149eda710>
No description has been provided for this image
In [ ]:
ds.temporal.average(data_var, skipna=True)[data_var].plot()
Out[ ]:
<matplotlib.collections.QuadMesh at 0x149f29710>
No description has been provided for this image
In [ ]:
ds.temporal.average(data_var, skipna=False)[data_var].plot()
Out[ ]:
<matplotlib.collections.QuadMesh at 0x14aaa8810>
No description has been provided for this image