Search code examples
pythonpython-3.xnetcdfgeonetcdf4

NetCDF combining time variable units for different days


Im currently working with aerosol data for 20 years worth of flights. I want to be able to define a period of time to display the data over. However, all of the data has units of seconds since the start of the day. This can be seen here:

 <class 'netCDF4._netCDF4.Variable'>
float64 UTC_time(UTC_time)
    standard_name: time
    long_name: UTC time
    units: seconds since 1996-07-30 00:00:00
    calendar: standard
unlimited dimensions:
current shape = (1006,)
filling off

<class 'netCDF4._netCDF4.Variable'>
float64 UTC_time(UTC_time)
    standard_name: time
    long_name: UTC time
    units: seconds since 1996-07-30 00:00:00
    calendar: standard
unlimited dimensions:
current shape = (9864,)
filling off

Is it possible to set a start time for all this data so I can refer to the whole data set and for example only take all data from July 2015.

Thanks so much for any possible assistance.


Solution

  • You can convert the units of time from seconds since 1996-07-30 00:00:00 to calendar dates using netCDF num2date.

    import netCDF4
    
    ncfile = netCDF4.Dataset('file.nc', 'r')
    time = ncfile.variables['time'] #don't cast to numpy array yet
    time_convert = netCDF4.num2date(time[:], time.units, time.calendar)