如何从python中的NetCDF文件中将数据信息(YYYYMMDD)拆分为YYYY MM DD?
How to split data information (YYYMMDD) ino YYYY MM DD from a NetCDF file in python?
我是 python 的新手,我阅读了一些以前的相关问题,但这些问题并没有完全回答我的问题:
我有一个已经切片的 .nc 文件用于我的案例研究。在 python 中,我进行了处理,我需要 3 个不同的列 [2015 01 31] 中的时间,尽管在 YYYYMMDD 的一列中给了我 [20150101 20150131 .... 20171221]
我已经尝试使用模块 netCDF4 和 numpy 来处理和拆分我需要的数据。
#Script for reading .nc files using the netcdf4 module in Python
import numpy as np
import netCDF4 as nc
os. chdir('/mnt/lustre02/work/ch0636/g260150/sims/validation/selectedmodelRCPs/RCP2.6/Model1/')
#extract time in a single array to append it to the whole dly. weather file
data=nc.Dataset('precipitation.nc','r')
time=data.variables['time'][:]
b=np.zeros((time.size,1))
b[:,0]=time
#Extracting time sections: year, month, day
year=[]
month=[]
day=[]
for i in range(len(b)):
year.append(b[i][0:4])
month.append(b[i][4:6])
day.append(b[i][6:])
print(day)
print(month)
print(year)
运行这部分给出的那一天是包括年月日在内的完整日期。月和日是空数组。
我不太确定如何以正确的方式实际处理这个问题,这有助于我获得进一步处理所需的内容。
要使用您的解决方案获取年月日数组,我建议这样修改:
year=[]
month=[]
day=[]
for i in range(len(b)):
yval=np.floor(b[i]/10000.)
mval=np.floor((b[i]-yval*10000.)/100.)
dval=np.floor(b[i]-yval*10000.-mval*100.)
year.append(int(yval))
month.append(int(mval))
day.append(int(dval))
print(day)
print(month)
print(year)
尽管如此,我也可以建议这样的代码,我们首先将时间转换为日期值,然后提取年、月和日:
# other way:
import datetime
def get_date(datevalin):
fraction_of_day=datevalin-np.floor(datevalin)
d0=datetime.datetime.strptime(str(int(np.floor(datevalin))),'%Y%m%d')
dval=d0+datetime.timedelta(seconds=fraction_of_day*24.*3600)
return dval
dates=[get_date(val) for val in time];
year=[val.year for val in dates];
month=[val.month for val in dates];
day=[val.day for val in dates];
我是 python 的新手,我阅读了一些以前的相关问题,但这些问题并没有完全回答我的问题:
我有一个已经切片的 .nc 文件用于我的案例研究。在 python 中,我进行了处理,我需要 3 个不同的列 [2015 01 31] 中的时间,尽管在 YYYYMMDD 的一列中给了我 [20150101 20150131 .... 20171221]
我已经尝试使用模块 netCDF4 和 numpy 来处理和拆分我需要的数据。
#Script for reading .nc files using the netcdf4 module in Python
import numpy as np
import netCDF4 as nc
os. chdir('/mnt/lustre02/work/ch0636/g260150/sims/validation/selectedmodelRCPs/RCP2.6/Model1/')
#extract time in a single array to append it to the whole dly. weather file
data=nc.Dataset('precipitation.nc','r')
time=data.variables['time'][:]
b=np.zeros((time.size,1))
b[:,0]=time
#Extracting time sections: year, month, day
year=[]
month=[]
day=[]
for i in range(len(b)):
year.append(b[i][0:4])
month.append(b[i][4:6])
day.append(b[i][6:])
print(day)
print(month)
print(year)
运行这部分给出的那一天是包括年月日在内的完整日期。月和日是空数组。 我不太确定如何以正确的方式实际处理这个问题,这有助于我获得进一步处理所需的内容。
要使用您的解决方案获取年月日数组,我建议这样修改:
year=[]
month=[]
day=[]
for i in range(len(b)):
yval=np.floor(b[i]/10000.)
mval=np.floor((b[i]-yval*10000.)/100.)
dval=np.floor(b[i]-yval*10000.-mval*100.)
year.append(int(yval))
month.append(int(mval))
day.append(int(dval))
print(day)
print(month)
print(year)
尽管如此,我也可以建议这样的代码,我们首先将时间转换为日期值,然后提取年、月和日:
# other way:
import datetime
def get_date(datevalin):
fraction_of_day=datevalin-np.floor(datevalin)
d0=datetime.datetime.strptime(str(int(np.floor(datevalin))),'%Y%m%d')
dval=d0+datetime.timedelta(seconds=fraction_of_day*24.*3600)
return dval
dates=[get_date(val) for val in time];
year=[val.year for val in dates];
month=[val.month for val in dates];
day=[val.day for val in dates];