简体   繁体   English

如何使用部分文件名作为函数的输入?

[英]How do I use part of my file names as input for a function?

I have a range of 200 files named A2016071.4d.L3m_OC.nc to A2016271.4d.L3m_OC.nc where the last 3 digits (ie 071 and 271) represent the julian day of the year. 我有200个文件,名为A2016071.4d.L3m_OC.nc到A2016271.4d.L3m_OC.nc,其中最后3位数字(即071和271)代表一年中的朱利安日。 How do I adjust this function: 如何调整此功能:

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)

So that the dayOfYear uses the numbers in the files name rather than manually having to define 200 dayOfYear variables? 那么dayOfYear使用文件名中的数字而不是手动定义200 dayOfYear变量?

If it is always the last three digits: 如果它总是最后三位数:

fname = 'A2016071.4d.L3m_OC.nc'
dayOfYear = int( fname.split('.')[0][-3:] )
print( dayOfYear )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM