3

Hello i have a neetcdf4 file with monthly precipitation data for over 10 years. what i am trying to do is :

  1. read the file
  2. select a subset area based on longitude latitude
  3. i need to calculate the moving average per 3 years
  4. plot the results
dataset = Dataset('test.nc','r',format='NETCDF4')
lons = dataset.variables['lon'][:]
lats = dataset.variables['lat'][:]
times = dataset.variables['time'][:]
times_units = dataset.variables['time'].units
prep_solide = dataset.variables['PREC'][:,:,:]
prec_units =dataset.variables['PREC'].units

dates = num2date(times[:],' months since 1801-01-01 00:00:00')

The error i keep getting is

ValueError: unsupported time units

is there any other way to fix this error ?

mrk
  • 8,059
  • 3
  • 56
  • 78
luna
  • 31
  • 7

1 Answers1

2

As you already noticed "months" is not supported by num2date function. To be honest, what is the meaning of 2 months since 1800-01-01 00:00:00 anyway? Is it 1800-03-01 00:00:00 or something else (1 month = 365.25/12 days and hence 1800-03-02 02:21:00)?

In any case, I would make the dates with my own function. For example, in your case:

dates = [datetime.datetime(1800,1,1)+datetime.timedelta(seconds = 365.25/12*24.0*3600.0*float(val)) for val in times]

As I do not know what is the value of 1 month in seconds in your data, I used 365.25/12.

msi_gerva
  • 2,021
  • 3
  • 22
  • 28
  • Thank you so much for your answer. In fact in my case the meaning of two month is 1800-03-01 00:00:00 . But i believe that with the way you presented it i can extract the month and the year for my analysis easily regardless of the day and hour . Thank you so much – luna Sep 21 '18 at 14:02