Annualization#

Example Datasets: Zhu, F. & Zhu, J. Long simulations of the Miocene Climatic Optimum, DOI: 10.5065/3QFN-GN70 (2025). https://rda.ucar.edu/datasets/d010026/.

[1]:
%load_ext autoreload
%autoreload 2

import os
import numpy as np
os.chdir('/glade/u/home/fengzhu/Github/x4c/docsrc/notebooks')
import x4c
print(x4c.__version__)
2025.6.22

Loading a netCDF file as an xarray.Dataset#

The x4c.load_dataset() function is an enhanced version of xarray.load_dataset() for CESM output. The users may specify the component (comp) and grid (grid) information while loading the file.

[ ]:
dirpath = '/glade/campaign/cesm/community/palwg/MCO_LongSims/b.e13.B1850C5.ne16_g16.icesm131_d18O_fixer.Miocene.3xCO2.005'
fname = 'b.e13.B1850C5.ne16_g16.icesm131_d18O_fixer.Miocene.3xCO2.005.cam.h0.TS.895101-900012.nc'
ds = x4c.load_dataset(
    os.path.join(dirpath, 'atm/proc/tseries/month_1', fname),
    comp='atm',
    grid='ne16np4',
)
ds
<xarray.Dataset> Size: 34MB
Dimensions:       (lev: 30, ilev: 31, ncol: 13826, time: 600, nbnd: 2)
Coordinates:
  * lev           (lev) float64 240B 3.643 7.595 14.36 ... 957.5 976.3 992.6
  * ilev          (ilev) float64 248B 2.255 5.032 10.16 ... 967.5 985.1 1e+03
  * time          (time) object 5kB 8951-02-01 00:00:00 ... 9001-01-01 00:00:00
Dimensions without coordinates: ncol, nbnd
Data variables: (12/31)
    hyam          (lev) float64 240B 0.003643 0.007595 0.01436 ... 0.001989 0.0
    hybm          (lev) float64 240B 0.0 0.0 0.0 0.0 ... 0.9512 0.9743 0.9926
    P0            float64 8B 1e+05
    hyai          (ilev) float64 248B 0.002255 0.005032 0.01016 ... 0.0 0.0
    hybi          (ilev) float64 248B 0.0 0.0 0.0 0.0 ... 0.9636 0.9851 1.0
    lat           (ncol) float64 111kB -35.26 -35.98 -37.07 ... 37.91 36.74
    ...            ...
    n2ovmr        (time) float64 5kB 2.757e-07 2.757e-07 ... 2.757e-07 2.757e-07
    f11vmr        (time) float64 5kB 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
    f12vmr        (time) float64 5kB 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
    sol_tsi       (time) float64 5kB -1.0 -1.0 -1.0 -1.0 ... -1.0 -1.0 -1.0 -1.0
    nsteph        (time) int32 2kB 156805488 156806832 ... 157678512 157680000
    TS            (time, ncol) float32 33MB 303.9 303.3 302.5 ... 292.6 293.3
Attributes: (12/18)
    np:               4
    ne:               16
    Conventions:      CF-1.0
    source:           CAM
    case:             b.e13.B1850C5.ne16_g16.icesm131_d18O_fixer.Miocene.3xCO...
    title:            UNSET
    ...               ...
    path:             /glade/campaign/cesm/community/palwg/MCO_LongSims/b.e13...
    comp:             atm
    grid:             ne16np4
    gw:               <xarray.DataArray 'area' (ncol: 13826)> Size: 111kB\nar...
    lat:              <xarray.DataArray 'lat' (ncol: 13826)> Size: 111kB\narr...
    lon:              <xarray.DataArray 'lon' (ncol: 13826)> Size: 111kB\narr...

Annualization#

x4c provides a handy function annualize() for annualization of a xarray.Dataset or xarray.DataArray.

Note: All the Xarray extension features provided by x4c should be called via a .x syntax. See examples below.

[11]:
ds_ann = ds.x.annualize()
ds_ann
[11]:
<xarray.Dataset> Size: 19MB
Dimensions:  (time: 50, lev: 30, ilev: 31, ncol: 13826)
Coordinates:
  * lev      (lev) float64 240B 3.643 7.595 14.36 24.61 ... 957.5 976.3 992.6
  * ilev     (ilev) float64 248B 2.255 5.032 10.16 18.56 ... 967.5 985.1 1e+03
  * time     (time) object 400B 8951-12-31 00:00:00 ... 9000-12-31 00:00:00
Dimensions without coordinates: ncol
Data variables: (12/28)
    hyam     (time, lev) float64 12kB 0.003643 0.007595 0.01436 ... 0.001989 0.0
    hybm     (time, lev) float64 12kB 0.0 0.0 0.0 0.0 ... 0.9512 0.9743 0.9926
    P0       (time) float64 400B 1e+05 1e+05 1e+05 1e+05 ... 1e+05 1e+05 1e+05
    hyai     (time, ilev) float64 12kB 0.002255 0.005032 0.01016 ... 0.0 0.0
    hybi     (time, ilev) float64 12kB 0.0 0.0 0.0 0.0 ... 0.9636 0.9851 1.0
    lat      (time, ncol) float64 6MB -35.26 -35.98 -37.07 ... 37.91 37.91 36.74
    ...       ...
    n2ovmr   (time) float64 400B 2.757e-07 2.757e-07 ... 2.757e-07 2.757e-07
    f11vmr   (time) float64 400B 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
    f12vmr   (time) float64 400B 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
    sol_tsi  (time) float64 400B -1.0 -1.0 -1.0 -1.0 ... -1.0 -1.0 -1.0 -1.0
    nsteph   (time) float64 400B 1.568e+08 1.568e+08 ... 1.577e+08 1.577e+08
    TS       (time, ncol) float32 3MB 300.2 299.6 298.8 ... 298.8 297.5 298.3
Attributes: (12/18)
    np:               4
    ne:               16
    Conventions:      CF-1.0
    source:           CAM
    case:             b.e13.B1850C5.ne16_g16.icesm131_d18O_fixer.Miocene.3xCO...
    title:            UNSET
    ...               ...
    path:             /glade/campaign/cesm/community/palwg/MCO_LongSims/b.e13...
    comp:             atm
    grid:             ne16np4
    gw:               <xarray.DataArray 'area' (ncol: 13826)> Size: 111kB\nar...
    lat:              <xarray.DataArray 'lat' (ncol: 13826)> Size: 111kB\narr...
    lon:              <xarray.DataArray 'lon' (ncol: 13826)> Size: 111kB\narr...
[15]:
da_ann = da.x.annualize()
da_ann
[15]:
<xarray.DataArray 'TS' (time: 50, ncol: 13826)> Size: 3MB
array([[300.2453 , 299.6187 , 298.81784, ..., 297.99307, 296.7727 ,
        297.60065],
       [300.0269 , 299.341  , 298.51907, ..., 298.119  , 296.77554,
        297.7726 ],
       [300.55447, 299.93738, 299.14325, ..., 298.04315, 296.96646,
        297.72507],
       ...,
       [300.6285 , 299.9808 , 299.15076, ..., 297.54794, 296.3383 ,
        297.15594],
       [300.7547 , 300.02304, 299.08948, ..., 297.5419 , 296.20016,
        297.22223],
       [300.8983 , 300.17877, 299.24155, ..., 298.75955, 297.5373 ,
        298.30246]], shape=(50, 13826), dtype=float32)
Coordinates:
    lat      (ncol) float64 111kB -35.26 -35.98 -37.07 ... 37.91 37.91 36.74
    lon      (ncol) float64 111kB 315.0 316.6 319.1 320.6 ... 132.5 137.5 135.0
  * time     (time) object 400B 8951-12-31 00:00:00 ... 9000-12-31 00:00:00
Dimensions without coordinates: ncol
Attributes:
    units:         K
    long_name:     Surface temperature (radiative)
    cell_methods:  time: mean
    path:          /glade/campaign/cesm/community/palwg/MCO_LongSims/b.e13.B1...
    gw:            <xarray.DataArray 'area' (ncol: 13826)> Size: 111kB\narray...
    lat:           <xarray.DataArray 'lat' (ncol: 13826)> Size: 111kB\narray(...
    lon:           <xarray.DataArray 'lon' (ncol: 13826)> Size: 111kB\narray(...
    comp:          atm
    grid:          ne16np4