0
votes

I have a netcdf file giving monthly precipitation values from 1948 to 2008. The time variable has a format as below:

float time(time) ;
        time:units = "months since 1948-01-01 00:00:00" ;
        time:time_origin = "01-JAN-1948:00:00:00" ;

When i try to use Xarray to open the dataset using the following command

ds=xr.open_dataset("C:/Users/vsri/Downloads/prcp_monthly_1948-2008.nc")

I get the following error

ValueError: unable to decode time units 'months since 1948-01-01 00:00:00' with the default calendar. Try opening your dataset with decode_times=False.

If i use the decode_Times=False argument, the time variable has a floating point value assigned to it (as below)

 Coordinates:
      * longitude  (longitude) float32 0.25 0.75 1.25 1.75 ... 358.75 359.25 359.75
      * latitude   (latitude) float32 -89.75 -89.25 -88.75 ... 88.75 89.25 89.75
      * z       (z) float32 0.0
      * time (time) float32 0.0 1.0 2.0 3.0 4.0 ... 728.0 729.0 730.0 731.0

I do not want to use decode_Times=False becasue I cannot use the resample function of xarray on the dataset any more.

Can someone guide me on how to make sure that xarray reads the data set with the proper time stamp and not as a floating point ?

1
Note there is a difference between a "calendar month" and a "month" defined by CF conventions. A calendar month varies in length depending on the specific month, while a month in CF conventions always has the same length. Because of this discrepancy xarray elects not to try to automatically decode times with these units. It is straightforward to work around this, however, so I should be able to provide an answer. My guess is the units in this file correspond to calendar months?spencerkclark
@spencerkclark: Thanks for your clarification. In this case I am not sure if it matters as the data is one value per month. So 732 months in the time period 1948-2008. Apologies if I am not getting the concept correctly. What is the workaround for this issue Xarray ?Vignesh Sridharan
I got something similar while opening a opendap dataset. I fixed it by installing cftime which xarray was trying to import in the traceback.jadelord

1 Answers

4
votes

Thanks for the update in your comment. In your case, since your data has a regular frequency, I recommend working around this by creating your own time coordinate with pandas.date_range:

import pandas as pd
import xarray as xr

ds = xr.open_dataset("C:/Users/vsri/Downloads/prcp_monthly_1948-2008.nc",
        decode_times=False)
units, reference_date = ds.time.attrs['units'].split('since')
ds['time'] = pd.date_range(start=reference_date, periods=ds.sizes['time'], freq='MS')

This will create an array of dates for the first of each month starting in 1948-01-01 and ending the appropriate number of months later.