我正在使用ECMWF的ERA5每小时陆地数据,其中包含气候变量。
文件的一般方面是:
'era5-hourly-2m_temperature_firstfourdays-january_2017.nc'
Dimensions: (latitude: 184, longitude: 129, time: 96)
Coordinates:
* longitude (longitude) float32 -81.4 -81.3 -81.2 -81.1 ... -68.8 -68.7 -68.6
* latitude (latitude) float32 -0.1 -0.2 -0.3 -0.4 ... -18.2 -18.3 -18.4
* time (time) datetime64[ns] 2017-01-01 ... 2017-01-04T23:00:00
Data variables:
t2m (time, latitude, longitude) float32 ...
Attributes:
Conventions: CF-1.6
history: 2020-01-09 19:38:29 GMT by grib_to_netcdf-2.15.0: /opt/ecmw...
这是一个信息矩阵,其中包含许多变量和观察结果。
在进行任何先前的分析之前,我想使用Python将UTC时间转换为本地时间(UTC-5)。我在许多网页和论坛上用Google搜索和浏览,但没有找到任何能回答我问题的答案。我意识到许多帖子中都存在命令:
日期时间, pytz , tzinfo , astimezone ,
和其他示例,但没有一个示例被视为netCDF文件。
谢谢。
答案 0 :(得分:1)
首先,我建议您省去很多麻烦,并尽可能地使用UTC。
如果您真的需要当地时间,请使用datetime
和pytz
。顺便说一下,转换与netcdf
没有任何关系,但是请记住netCDF4
模块提供了有用的功能num2date
和date2num
[docs]。
from datetime import datetime
import pytz
string = '2017-01-04T23:00:00'
dt_obj = datetime.strptime(string, '%Y-%m-%dT%H:%M:%S')
# note that dt_obj is naive, i.e. it has no timezone info, so let's add it:
dt_obj = dt_obj.replace(tzinfo=pytz.utc)
print(datetime.strftime(dt_obj, '%Y-%m-%dT%H:%M:%S %Z %z'))
# 2017-01-04T23:00:00 UTC +0000
# now let's shift time to another timezone:
new_timezone = pytz.timezone('US/Eastern')
dt_obj = dt_obj.astimezone(new_timezone)
print(datetime.strftime(dt_obj, '%Y-%m-%dT%H:%M:%S %Z %z'))
# 2017-01-04T18:00:00 EST -0500