Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

num2date does not support 'months since...'

Tags:

python

netcdf4

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 ?

like image 495
luna Avatar asked Jun 13 '26 02:06

luna


1 Answers

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.

like image 86
msi_gerva Avatar answered Jun 14 '26 16:06

msi_gerva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!