我有200个文件,范围从A2016071.4d.L3m_OC.nc到A2016271.4d.L3m_OC.nc,其中最后3位数字(即071和271)代表一年中的儒略日。如何调整此功能:
import numpy as np
def daylength(dayOfYear, lat):
latInRad = np.deg2rad(lat)
declinationOfEarth = 23.45*np.sin(np.deg2rad(360.0*(283.0+dayOfYear)/365.0))
if -np.tan(latInRad) * np.tan(np.deg2rad(declinationOfEarth)) <= -1.0:
return 24.0
elif -np.tan(latInRad) * np.tan(np.deg2rad(declinationOfEarth)) >= 1.0:
return 0.0
else:
hourAngle = np.rad2deg(np.arccos(-np.tan(latInRad) * np.tan(np.deg2rad(declinationOfEarth))))
return 2.0*hourAngle/15.0
vec_daylength = np.vectorize(daylength)
vec_daylength(dayOfYear, lat)
以便dayOfYear
使用文件名中的数字,而不必手动定义200个dayOfYear
变量?
答案 0 :(得分:0)
如果始终是最后三位数字:
fname = 'A2016071.4d.L3m_OC.nc'
dayOfYear = int( fname.split('.')[0][-3:] )
print( dayOfYear )