'Get error when try to convert netcdf time to datetime in python
I want to read netcdf file and convert time of unit days since to python datetime object, when i try to use num2date() function this error is occur I don't know how to handle it
import netCDF4 as nc
import numpy as np
import datetime as dt
# from cftime import num2pydate
# from netCDF4 import Dataset, num2date
src = nc.Dataset("sconcaerh2o_monthly_IITB_WRF_Chem_NCAP.nc")
# print(src.ncattrs())
# print(src.CDI)
print(src.variables.keys())
dim = src.variables["sconcaerh2o"]
print(dim.dimensions[0])
times = src.variables["time"]
print(times.units)
dates = nc.num2pydate(times[0], units = times.units, calendar=times.calendar)
cdt = dt.datetime.now()
# print(nc.date2num(cdt, times.units))
And Output is
Traceback (most recent call last):
File "demo.py", line 15, in <module>
dates = num2pydate(times[0], units = times.units, calendar=times.calendar)
File "src/cftime/_cftime.pyx", line 299, in cftime._cftime.num2pydate
File "src/cftime/_cftime.pyx", line 499, in cftime._cftime.num2date
File "src/cftime/_cftime.pyx", line 107, in cftime._cftime._dateparse
File "src/cftime/_cftime.pyx", line 696, in cftime._cftime._parse_date
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Solution 1:[1]
Without some more information regarding your data set it's a little hard to understand the error that you have been getting. It looks like one of the variable you are putting in to num2date isn't properly set to a value and is set to None instead.
There is also an error in your code, the netCDF4 module does not have the num2pydate() function and the line creating the error in your output does not match the line in the code you have posted.
If you're trying to convert netCDF times to python datetime.datetime objects however, this can be achieved using the num2date() function in the netCDF module with some of it's keyword options: only_use_python_datetimes and only_use_cftime_datetimes.
only_use_cftime_datetimes is set to True by default, so num2date will always return cft.datetime objects. If you set this to false:
dates = nc.num2pydate(times, units = times.units, calendar=times.calendar,
only_use_cftime_datetimes=False)
num2date will return python datetime.datetimes if possible and cft.datetimes if not possible. If you want to be more strict, you could also set only_use_python_datetimes to True:
dates = nc.num2pydate(times, units = times.units, calendar=times.calendar,
only_use_cftime_datetimes=False,
only_use_python_datetimes=True)
num2py will then return only python datetime.datetime objects and will raise an error if that is not possible.
Solution 2:[2]
You can try several methods:
1.Configure the service point security protocol and select the SecurityProtocolType that suits you (Tls12, Ssl3, Tls, Tls11).
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
C# System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send
2.Set the ConnectionClose property of HttpClient.
_client.DefaultRequestHeaders.ConnectionClose = true;
HttpClient throwing "An error occurred while sending the request."
3.Try to hold the request long enough for it to complete normally and receive a valid response.
_client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
_client.DefaultRequestHeaders.Add("Keep-Alive", "3600");
.NET HttpClient - An existing connection was forcibly closed by the remote host
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rymo |
| Solution 2 | Lan Huang |
