1

I am working with an nc file.The file has an array of dimensions time, number, latitude, longitude. I was trying to convert time from number to date via the num2date method.

time_unit = nc.variables["time"].getncattr('units') # first read the 'units' attributes from the variable time
print(time_unit)
time_cal = nc.variables["time"].getncattr('calendar') # read calendar type
local_time = nc.num2date(t, units=time_unit, calendar=time_cal) # convert time
print("Original time %s is now converted as %s" % (time[0], local_time[0]))

But i am facing the following error.

AttributeError                            Traceback (most recent call last)
<ipython-input-124-d9912be48299> in <module>()
      2 print(time_unit)
      3 time_cal = nc.variables["time"].getncattr('calendar') # read 
calendar type
----> 4 local_time = nc.num2date(t, units=time_unit, calendar=time_cal) # 
convert time
      5 print("Original time %s is now converted as %s" % (time[0], 
local_time[0]))

netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Dataset.__getattr__ 
(netCDF4\_netCDF4.c:20474)()

netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Dataset.getncattr 
(netCDF4\_netCDF4.c:19433)()

netCDF4\_netCDF4.pyx in netCDF4._netCDF4._get_att (netCDF4\_netCDF4.c:4451()

AttributeError: NetCDF: Attribute not found

Any solutions?

1 Answer 1

2

nc is your open netCDF file but num2date is located in netCDF4:

>>> import netCDF4
>>> netCDF4.num2date
<function netCDF4._netCDF4.num2date>

Therefore, call it like this:

local_time = netCDF4.num2date(t, units=time_unit, calendar=time_cal) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.