Basic operations on the time dimension of netCDF data using Xarray

As an atmospheric data scientist, I would like to perform calculations on the time dimension of a netCDF data to answer specific questions such as:

  • How can I select a subset of data?

  • What is the state of the atmosphere at any given time?

  • Is it possible to calculate daily or hourly averages for the given time period in the dataset?

Open Data

For our analyses, we are going to use the GFS hourly forecast data initialized at 00UTC, 10 October 2021. The use of visjobs package (see documentation) makes the data-retrieving process easy for us, therefore we are going to make use of it.

Let’s begin with importing the necessary tools, which we will exploit in our calculations.

# Import required libraries
import xarray as xr # tool for analyzing multi-dimensional data
from visjobs.datas import get_MODEL # tool for retrieving the data
import matplotlib.pyplot as plt # tool for visualization

# Ignore warnings
import warnings
warnings.filterwarnings("ignore")

plt.rcParams['figure.figsize'] = (12, 5)  # Set default plot size
init_year = '2021' # model init year
init_month = '10' # model init month
init_day = '10' # model init day
init_hour = '00' # model init hour in UTC

ds = get_MODEL.pick_data(year=init_year, month=init_month, 
                         day=init_day, hour=init_hour, 
                         model='GFS', hourly=True)
Addressing Data:  http://nomads.ncep.noaa.gov:80/dods/gfs_0p25_1hr/gfs20211010/gfs_0p25_1hr_00z
Connected GFS Data via OpenDAP

The get_MODEL object in visjobs library facilitates accessing various atmopsheric model data remotely. Carefully examine the arguments we passed into the function pick_data. We easily emphasize the model initialization date, model name, and other additional arguments in the function to get the data we are looking for.

  • Here’s our dataset: GFS Hourly 0.25 Degree Data Init at 00 UTC, 10 October 2021

See the retrieved data below. It contains 200+ variables (on single or pressure levels), each of which defined on the dimensions lat, lev, lon, time. When you click on the data variables button below, it becomes clear what variable is confined to which dimensions.

You’re strongly encouraged to check out the xarray data structures documentation if you are unfamiliar with the data elements Dimensions, Coordinates, Data Variables, and Attributes. At it’s core, these elements define our multi-dimensional dataset, leveraging the concept of matrices.

ds # Retrieved data
<xarray.Dataset>
Dimensions:         (lat: 721, lev: 41, lon: 1440, time: 121)
Coordinates:
  * time            (time) datetime64[ns] 2021-10-10 ... 2021-10-15
  * lev             (lev) float64 1e+03 975.0 950.0 925.0 ... 0.04 0.02 0.01
  * lat             (lat) float64 -90.0 -89.75 -89.5 -89.25 ... 89.5 89.75 90.0
  * lon             (lon) float64 0.0 0.25 0.5 0.75 ... 359.0 359.2 359.5 359.8
Data variables:
    absvprs         (time, lev, lat, lon) float32 ...
    no4lftxsfc      (time, lat, lon) float32 ...
    acpcpsfc        (time, lat, lon) float32 ...
    albdosfc        (time, lat, lon) float32 ...
    apcpsfc         (time, lat, lon) float32 ...
    capesfc         (time, lat, lon) float32 ...
    cape180_0mb     (time, lat, lon) float32 ...
    cape90_0mb      (time, lat, lon) float32 ...
    cape255_0mb     (time, lat, lon) float32 ...
    cfrzravesfc     (time, lat, lon) float32 ...
    cfrzrsfc        (time, lat, lon) float32 ...
    cicepavesfc     (time, lat, lon) float32 ...
    cicepsfc        (time, lat, lon) float32 ...
    cinsfc          (time, lat, lon) float32 ...
    cin180_0mb      (time, lat, lon) float32 ...
    cin90_0mb       (time, lat, lon) float32 ...
    cin255_0mb      (time, lat, lon) float32 ...
    clwmrprs        (time, lev, lat, lon) float32 ...
    clwmrhy1        (time, lat, lon) float32 ...
    cnwatsfc        (time, lat, lon) float32 ...
    cpofpsfc        (time, lat, lon) float32 ...
    cpratavesfc     (time, lat, lon) float32 ...
    cpratsfc        (time, lat, lon) float32 ...
    crainavesfc     (time, lat, lon) float32 ...
    crainsfc        (time, lat, lon) float32 ...
    csnowavesfc     (time, lat, lon) float32 ...
    csnowsfc        (time, lat, lon) float32 ...
    cwatclm         (time, lat, lon) float32 ...
    cworkclm        (time, lat, lon) float32 ...
    dlwrfsfc        (time, lat, lon) float32 ...
    dpt2m           (time, lat, lon) float32 ...
    dswrfsfc        (time, lat, lon) float32 ...
    dzdtprs         (time, lev, lat, lon) float32 ...
    fldcpsfc        (time, lat, lon) float32 ...
    fricvsfc        (time, lat, lon) float32 ...
    gfluxsfc        (time, lat, lon) float32 ...
    grleprs         (time, lev, lat, lon) float32 ...
    grlehy1         (time, lat, lon) float32 ...
    gustsfc         (time, lat, lon) float32 ...
    hcdcavehcll     (time, lat, lon) float32 ...
    hcdchcll        (time, lat, lon) float32 ...
    hgtsfc          (time, lat, lon) float32 ...
    hgtprs          (time, lev, lat, lon) float32 ...
    hgt2pv          (time, lat, lon) float32 ...
    hgtneg2pv       (time, lat, lon) float32 ...
    hgttop0c        (time, lat, lon) float32 ...
    hgtceil         (time, lat, lon) float32 ...
    hgt0c           (time, lat, lon) float32 ...
    hgtmwl          (time, lat, lon) float32 ...
    hgttrop         (time, lat, lon) float32 ...
    hindexsfc       (time, lat, lon) float32 ...
    hlcy3000_0m     (time, lat, lon) float32 ...
    hpblsfc         (time, lat, lon) float32 ...
    icahtmwl        (time, lat, lon) float32 ...
    icahttrop       (time, lat, lon) float32 ...
    icecsfc         (time, lat, lon) float32 ...
    iceg_10m        (time, lat, lon) float32 ...
    icetksfc        (time, lat, lon) float32 ...
    icetmpsfc       (time, lat, lon) float32 ...
    icmrprs         (time, lev, lat, lon) float32 ...
    icmrhy1         (time, lat, lon) float32 ...
    landsfc         (time, lat, lon) float32 ...
    lcdcavelcll     (time, lat, lon) float32 ...
    lcdclcll        (time, lat, lon) float32 ...
    lftxsfc         (time, lat, lon) float32 ...
    lhtflsfc        (time, lat, lon) float32 ...
    mcdcavemcll     (time, lat, lon) float32 ...
    mcdcmcll        (time, lat, lon) float32 ...
    msletmsl        (time, lat, lon) float32 ...
    o3mrprs         (time, lev, lat, lon) float32 ...
    pevprsfc        (time, lat, lon) float32 ...
    plpl255_0mb     (time, lat, lon) float32 ...
    potsig995       (time, lat, lon) float32 ...
    prateavesfc     (time, lat, lon) float32 ...
    pratesfc        (time, lat, lon) float32 ...
    preslclb        (time, lat, lon) float32 ...
    preslclt        (time, lat, lon) float32 ...
    presmclb        (time, lat, lon) float32 ...
    presmclt        (time, lat, lon) float32 ...
    preshclb        (time, lat, lon) float32 ...
    preshclt        (time, lat, lon) float32 ...
    pressfc         (time, lat, lon) float32 ...
    pres80m         (time, lat, lon) float32 ...
    pres2pv         (time, lat, lon) float32 ...
    presneg2pv      (time, lat, lon) float32 ...
    prescclb        (time, lat, lon) float32 ...
    prescclt        (time, lat, lon) float32 ...
    presmwl         (time, lat, lon) float32 ...
    prestrop        (time, lat, lon) float32 ...
    prmslmsl        (time, lat, lon) float32 ...
    pwatclm         (time, lat, lon) float32 ...
    refcclm         (time, lat, lon) float32 ...
    refd4000m       (time, lat, lon) float32 ...
    refd1000m       (time, lat, lon) float32 ...
    refdhy1         (time, lat, lon) float32 ...
    refdhy2         (time, lat, lon) float32 ...
    rhprs           (time, lev, lat, lon) float32 ...
    rh2m            (time, lat, lon) float32 ...
    rhsg330_1000    (time, lat, lon) float32 ...
    rhsg440_1000    (time, lat, lon) float32 ...
    rhsg720_940     (time, lat, lon) float32 ...
    rhsg440_720     (time, lat, lon) float32 ...
    rhsig995        (time, lat, lon) float32 ...
    rh30_0mb        (time, lat, lon) float32 ...
    rhclm           (time, lat, lon) float32 ...
    rhtop0c         (time, lat, lon) float32 ...
    rh0c            (time, lat, lon) float32 ...
    rwmrprs         (time, lev, lat, lon) float32 ...
    rwmrhy1         (time, lat, lon) float32 ...
    sfcrsfc         (time, lat, lon) float32 ...
    shtflsfc        (time, lat, lon) float32 ...
    snmrprs         (time, lev, lat, lon) float32 ...
    snmrhy1         (time, lat, lon) float32 ...
    snodsfc         (time, lat, lon) float32 ...
    soill0_10cm     (time, lat, lon) float32 ...
    soill10_40cm    (time, lat, lon) float32 ...
    soill40_100cm   (time, lat, lon) float32 ...
    soill100_200cm  (time, lat, lon) float32 ...
    soilw0_10cm     (time, lat, lon) float32 ...
    soilw10_40cm    (time, lat, lon) float32 ...
    soilw40_100cm   (time, lat, lon) float32 ...
    soilw100_200cm  (time, lat, lon) float32 ...
    sotypsfc        (time, lat, lon) float32 ...
    spfhprs         (time, lev, lat, lon) float32 ...
    spfh2m          (time, lat, lon) float32 ...
    spfh80m         (time, lat, lon) float32 ...
    spfh30_0mb      (time, lat, lon) float32 ...
    sunsdsfc        (time, lat, lon) float32 ...
    tcdcaveclm      (time, lat, lon) float32 ...
    tcdcblcll       (time, lat, lon) float32 ...
    tcdcclm         (time, lat, lon) float32 ...
    tcdcprs         (time, lev, lat, lon) float32 ...
    tcdcccll        (time, lat, lon) float32 ...
    tmax2m          (time, lat, lon) float32 ...
    tmin2m          (time, lat, lon) float32 ...
    tmplclt         (time, lat, lon) float32 ...
    tmpmclt         (time, lat, lon) float32 ...
    tmphclt         (time, lat, lon) float32 ...
    tmpsfc          (time, lat, lon) float32 ...
    tmpprs          (time, lev, lat, lon) float32 ...
    tmp_1829m       (time, lat, lon) float32 ...
    tmp_2743m       (time, lat, lon) float32 ...
    tmp_3658m       (time, lat, lon) float32 ...
    tmp2m           (time, lat, lon) float32 ...
    tmp80m          (time, lat, lon) float32 ...
    tmp100m         (time, lat, lon) float32 ...
    tmpsig995       (time, lat, lon) float32 ...
    tmp30_0mb       (time, lat, lon) float32 ...
    tmp2pv          (time, lat, lon) float32 ...
    tmpneg2pv       (time, lat, lon) float32 ...
    tmpmwl          (time, lat, lon) float32 ...
    tmptrop         (time, lat, lon) float32 ...
    tozneclm        (time, lat, lon) float32 ...
    tsoil0_10cm     (time, lat, lon) float32 ...
    tsoil10_40cm    (time, lat, lon) float32 ...
    tsoil40_100cm   (time, lat, lon) float32 ...
    tsoil100_200cm  (time, lat, lon) float32 ...
    ugwdsfc         (time, lat, lon) float32 ...
    uflxsfc         (time, lat, lon) float32 ...
    ugrdprs         (time, lev, lat, lon) float32 ...
    ugrd_1829m      (time, lat, lon) float32 ...
    ugrd_2743m      (time, lat, lon) float32 ...
    ugrd_3658m      (time, lat, lon) float32 ...
    ugrd10m         (time, lat, lon) float32 ...
    ugrd20m         (time, lat, lon) float32 ...
    ugrd30m         (time, lat, lon) float32 ...
    ugrd40m         (time, lat, lon) float32 ...
    ugrd50m         (time, lat, lon) float32 ...
    ugrd80m         (time, lat, lon) float32 ...
    ugrd100m        (time, lat, lon) float32 ...
    ugrdsig995      (time, lat, lon) float32 ...
    ugrd30_0mb      (time, lat, lon) float32 ...
    ugrd2pv         (time, lat, lon) float32 ...
    ugrdneg2pv      (time, lat, lon) float32 ...
    ugrdpbl         (time, lat, lon) float32 ...
    ugrdmwl         (time, lat, lon) float32 ...
    ugrdtrop        (time, lat, lon) float32 ...
    ulwrfsfc        (time, lat, lon) float32 ...
    ulwrftoa        (time, lat, lon) float32 ...
    ustm6000_0m     (time, lat, lon) float32 ...
    uswrfsfc        (time, lat, lon) float32 ...
    uswrftoa        (time, lat, lon) float32 ...
    vgwdsfc         (time, lat, lon) float32 ...
    vegsfc          (time, lat, lon) float32 ...
    vflxsfc         (time, lat, lon) float32 ...
    vgrdprs         (time, lev, lat, lon) float32 ...
    vgrd_1829m      (time, lat, lon) float32 ...
    vgrd_2743m      (time, lat, lon) float32 ...
    vgrd_3658m      (time, lat, lon) float32 ...
    vgrd10m         (time, lat, lon) float32 ...
    vgrd20m         (time, lat, lon) float32 ...
    vgrd30m         (time, lat, lon) float32 ...
    vgrd40m         (time, lat, lon) float32 ...
    vgrd50m         (time, lat, lon) float32 ...
    vgrd80m         (time, lat, lon) float32 ...
    vgrd100m        (time, lat, lon) float32 ...
    vgrdsig995      (time, lat, lon) float32 ...
    vgrd30_0mb      (time, lat, lon) float32 ...
    vgrd2pv         (time, lat, lon) float32 ...
    vgrdneg2pv      (time, lat, lon) float32 ...
    vgrdpbl         (time, lat, lon) float32 ...
    vgrdmwl         (time, lat, lon) float32 ...
    vgrdtrop        (time, lat, lon) float32 ...
    vissfc          (time, lat, lon) float32 ...
    vratepbl        (time, lat, lon) float32 ...
    vstm6000_0m     (time, lat, lon) float32 ...
    vvelprs         (time, lev, lat, lon) float32 ...
    vvelsig995      (time, lat, lon) float32 ...
    vwsh2pv         (time, lat, lon) float32 ...
    vwshneg2pv      (time, lat, lon) float32 ...
    vwshtrop        (time, lat, lon) float32 ...
    watrsfc         (time, lat, lon) float32 ...
    weasdsfc        (time, lat, lon) float32 ...
    wiltsfc         (time, lat, lon) float32 ...
    var00212m       (time, lat, lon) float32 ...
Attributes:
    title:        GFS 0.25 deg starting from 00Z10oct2021, downloaded Oct 10 ...
    Conventions:  COARDS\nGrADS
    dataType:     Grid
    history:      Mon Oct 11 00:38:59 GMT 2021 : imported by GrADS Data Serve...

The GFS model provides both hourly and 3-hourly forecast of the atmospheric parameters. The former encompasses the first 120 hours forecast range. Given that we retrieved the hourly GFS data using the visjobs tool, it makes sense that our data consists of 121 timesteps (see the time dimension).

Subset the data

Considering the fact that we are remotely accessing a large atmospheric data, it is reasonable to get a subset of the data to speed up the calculation processes. Let’s only use the 2 meter temperature variable, and for the sake of simplicity, we will be ignoring the data for the last 2 days of our forecast period and focusing only on the first 3 days.

We can effectively subset our data using the functions .isel and .sel functions of xarray. These functions resemble the .loc and .iloc that we use to handle Pandas DataFrames.

# Subset the 2 meter temperature forecast for the first 3 days of the forecast period for Turkey's coordinates 
ds_tmp2m = ds['tmp2m'].isel(time=slice(0,72)).sel(lat=slice(35,43), lon=slice(23,48))
ds_tmp2m
<xarray.DataArray 'tmp2m' (time: 72, lat: 33, lon: 101)>
[239976 values with dtype=float32]
Coordinates:
  * time     (time) datetime64[ns] 2021-10-10 ... 2021-10-12T23:00:00
  * lat      (lat) float64 35.0 35.25 35.5 35.75 36.0 ... 42.25 42.5 42.75 43.0
  * lon      (lon) float64 23.0 23.25 23.5 23.75 24.0 ... 47.25 47.5 47.75 48.0
Attributes:
    long_name:  ** 2 m above ground temperature [k] 

Check out the time dimension of the subsetted data. Do you see a change?

If we’d like to learn more about a particular variable in the dataset, we can find (most of the time) the detailed information in the attributes of that variable.

print(ds_tmp2m.attrs) # See the attributes of t2mp variable
{'long_name': '** 2 m above ground temperature [k] '}

The temperature data is given in Kelvin. But we won’t be bothered with unit conversions for now, since our data is still large.

Time dimension operations

Our essential aim is to perform certain time-related calculations and see how convenient it is to use xarray for this. Suppose you are eager to find answers to the problems below:

  • Analyzing the state of the atmosphere at any given time

  • Calculating the daily or hourly averages over the 3-days forecast range

State of the atmosphere at any given time

What if we wanted to analyze the atmosphere at a single forecast time? Again, we can exploit the .isel or .sel functions of xarray.

tmp2m_subset = ds_tmp2m.isel(time=0) # Get the first forecast time
tmp2m_subset
<xarray.DataArray 'tmp2m' (lat: 33, lon: 101)>
array([[296.1547 , 296.10468, 296.17468, ..., 280.39468, 279.66467, 280.41467],
       [296.25467, 296.10468, 295.91467, ..., 280.63467, 280.8447 , 280.0847 ],
       [296.25467, 296.1247 , 295.69467, ..., 280.56467, 281.5247 , 280.81467],
       ...,
       [279.8647 , 279.0247 , 279.03467, ..., 277.2147 , 282.5947 , 288.07468],
       [278.97467, 279.79468, 279.60468, ..., 281.73468, 287.4647 , 288.79468],
       [278.7147 , 277.7147 , 277.75467, ..., 286.39468, 288.48468, 288.38467]],
      dtype=float32)
Coordinates:
    time     datetime64[ns] 2021-10-10
  * lat      (lat) float64 35.0 35.25 35.5 35.75 36.0 ... 42.25 42.5 42.75 43.0
  * lon      (lon) float64 23.0 23.25 23.5 23.75 24.0 ... 47.25 47.5 47.75 48.0
Attributes:
    long_name:  ** 2 m above ground temperature [k] 

Note that the time dimension has disappeared. How?

Now let’s simply visualize the subsetted dataset.

tmp2m_subset.plot(cmap='gnuplot', vmin=260, vmax=300) 
<matplotlib.collections.QuadMesh at 0x2813f3ffcd0>
../_images/basic-operations-on-the-time-dimension-of-netcdf-data-using-xarray_22_1.png

It is crucial to realize the importance of defining the coordinate reference systems (crs) of the multi-dimensional atmospheric data we are playing with. The ignorance or misuse of the crs might introduce problems later on. But in our analysis, we won’t be dealing with the crs information for the simplicity. If you’ve never heard of the coordinate reference systems, please refer to this documentation.

Hourly averages over the first 3 days

Let’s make a graph showing what the average values in each hour will be (Remember that we have a forecast range of three days). For example, we can elaborate on analyzing the 3-days average temperature for 01 UTC. We will utilize the groupby function, which allows us to group the dimensional properties of data according to certain criteria.

%%time
# Use groupby on the dataset and get the mean
ds_tmp2m.groupby('time.hour').mean()
Wall time: 4min 36s
<xarray.DataArray 'tmp2m' (hour: 24, lat: 33, lon: 101)>
array([[[296.0615 , 296.20145, 296.31482, ..., 281.43814, 280.5548 ,
         280.71146],
        [295.95145, 296.01813, 296.0048 , ..., 281.64148, 281.77145,
         280.7548 ],
        [295.83813, 295.87814, 295.5748 , ..., 281.47812, 282.18814,
         281.34482],
        ...,
        [280.73477, 279.52814, 279.5348 , ..., 278.70816, 283.61813,
         288.74814],
        [280.42148, 281.27148, 280.95145, ..., 283.0048 , 288.08145,
         289.35147],
        [280.1648 , 279.38147, 279.39813, ..., 287.12482, 289.1248 ,
         289.16812]],

       [[295.87402, 295.92734, 296.104  , ..., 280.93735, 280.08066,
         280.224  ],
        [295.75732, 295.78732, 295.67065, ..., 281.1107 , 281.2507 ,
         280.314  ],
        [295.77066, 295.7407 , 295.31067, ..., 281.01733, 281.644  ,
         280.92398],
...
        [280.4172 , 278.9839 , 279.2172 , ..., 280.7172 , 284.45053,
         289.31717],
        [280.25052, 281.18387, 280.95053, ..., 284.08386, 288.7172 ,
         290.0172 ],
        [279.9172 , 279.55054, 279.78384, ..., 287.85056, 289.8172 ,
         289.9172 ]],

       [[295.70114, 295.86783, 295.90115, ..., 283.66782, 282.20114,
         282.4678 ],
        [295.76782, 295.93448, 295.66785, ..., 284.2345 , 283.83447,
         282.5678 ],
        [295.4678 , 295.66782, 295.33447, ..., 283.43448, 283.93448,
         283.1345 ],
        ...,
        [280.36783, 278.9678 , 279.16782, ..., 280.56784, 284.6345 ,
         289.36783],
        [280.26782, 281.16782, 280.9678 , ..., 284.0678 , 288.80115,
         290.0012 ],
        [279.86783, 279.53452, 279.83447, ..., 287.90115, 289.80115,
         289.90115]]], dtype=float32)
Coordinates:
  * lat      (lat) float64 35.0 35.25 35.5 35.75 36.0 ... 42.25 42.5 42.75 43.0
  * lon      (lon) float64 23.0 23.25 23.5 23.75 24.0 ... 47.25 47.5 47.75 48.0
  * hour     (hour) int64 0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23

How long do you think it would take the computer to perform the calculation above?

It took almost 5 minutes to run the code cell above, therefore we better select a particular grid point, on which we will perform calculations (e.g. the grid over the Istanbul city).

Once we subset the data on a single latitude and longitude point, the lat and lon dimensions will disappear. Can you guess why?

# Subset a grid near Istanbul and convert Kelvin to Celcius
ds_tmp2m_ist = ds_tmp2m.sel(lat=41, lon=29) - 273.15
ds_tmp2m_ist
<xarray.DataArray 'tmp2m' (time: 72)>
array([16.9747  , 16.948883, 16.94281 , 16.868683, 16.761108, 16.72232 ,
       16.78598 , 16.87439 , 17.141907, 17.674194, 17.86615 , 17.84195 ,
       17.641937, 17.18393 , 16.838959, 16.650024, 16.511108, 16.411102,
       16.311096, 16.35202 , 16.392456, 16.359009, 16.401031, 16.467255,
       16.512634, 16.469727, 16.457275, 16.445465, 16.377533, 16.507965,
       16.812256, 17.273285, 17.963013, 18.88739 , 19.32483 , 19.75    ,
       20.0542  , 19.766998, 18.85608 , 18.49411 , 17.8526  , 17.608795,
       17.456818, 16.888916, 17.006958, 16.975128, 16.764404, 16.62976 ,
       16.457123, 16.19342 , 16.16211 , 16.1969  , 16.237915, 16.477448,
       16.92517 , 17.549744, 18.56668 , 19.533813, 20.025757, 20.157227,
       19.67218 , 19.336304, 18.262115, 17.581482, 16.698456, 17.18634 ,
       17.111084, 16.845367, 16.788483, 17.00241 , 16.836182, 16.75647 ],
      dtype=float32)
Coordinates:
  * time     (time) datetime64[ns] 2021-10-10 ... 2021-10-12T23:00:00
    lat      float64 41.0
    lon      float64 29.0

Now let’s get the hourly averages for the clipped dataset, which is a 1-dimensional array defined with a time dimension.

%%time
ds_tmp2m_ist.groupby('time.hour').mean().plot()
Wall time: 30.9 ms
[<matplotlib.lines.Line2D at 0x2813fb2ef70>]
../_images/basic-operations-on-the-time-dimension-of-netcdf-data-using-xarray_31_2.png

We can also find the maximum temperature values at each hour over the forecast period instead of the averages.

%%time
ds_tmp2m_ist.groupby('time.hour').max().plot()
Wall time: 21.9 ms
[<matplotlib.lines.Line2D at 0x2813fb8e790>]
../_images/basic-operations-on-the-time-dimension-of-netcdf-data-using-xarray_33_2.png

Daily averages for the first 3 days

Do you think that we can calculate the daily averages instead of hourly ones?

If your answer was yes, you’re perfect!

All you have to do is use time.day instead of time.hour in the groupby function.

ds_tmp2m_ist.groupby('time.day').mean()
<xarray.DataArray 'tmp2m' (day: 3)>
array([15.44429 , 15.773591, 16.52334 ], dtype=float32)
Coordinates:
    lat      float64 41.0
    lon      float64 30.0
  * day      (day) int64 10 11 12

Check out the new dimensions above! What do you think about it?

Further Discussion

We realize how easy it is to process the time dimension of a netCDF data using xarray. See other documentations on Climaturk to become more familiar with handling the atmospheric data.

Elaborate on the questions below:

  • Question 1: Is it possible to calculate the monthly averages using the dataset we have retrieved above

  • Question 2: Can we make use of the groupby feature for anomaly calculations?

See you until the next time!


Blog by:
Berkay DÖNMEZ : Linkedin | Github | Twitter | Instagram
Kutay DÖNMEZ : LinkedIn | Github | Twitter | Instagram

ABOUT US