Skip to content

time coordinate

André Castro edited this page Aug 27, 2024 · 1 revision

time coordinate values from netCDFs is handled in python by the cftime librabry

Python library for decoding time units and variable values in a netCDF file conforming to the Climate and Forecasting (CF) netCDF conventions.

test netCDF time values:

Compare the netCDF values to timestamp with the epoque times of the time_list

from cftime import num2date

time_list = [0.0001168672, 0.01690636, 0.03340224, 0.05017757, 0.06667817,
             0.08345514, 0.1002276, 0.116727]

timestamps = ["2023-11-27T14:14:00", "2023-11-27T14:15:00", 
              "2023-11-27T14:16:00", "2023-11-27T14:17:00", "2023-11-27T14:18:00",
              "2023-11-27T14:19:00", "2023-11-27T14:20:00", "2023-11-27T14:21:00" 
              ]

for index, time_item in enumerate(time_list):
    nc_cftime = num2date(time_item, units="hours since 2023-11-27 14:14:00 +00:00")
    nc_cftime_str = nc_cftime.strftime("%Y-%m-%dT%H:%M:%S")
    print(f'nc_cftime_str: {nc_cftime_str}')
    print(f'timestamp:     {timestamps[index]}')
    assert nc_cftime_str == timestamps[index]

Clone this wiki locally