【问题标题】:How do I use part of my file names as input for a function?如何使用部分文件名作为函数的输入?
【发布时间】:2019-06-13 15:50:16
【问题描述】:

我有 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 变量?

【问题讨论】:

    标签: python function filenames


    【解决方案1】:

    如果总是最后三位:

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

    【讨论】:

      猜你喜欢
      • 2014-04-15
      • 2021-10-23
      • 2020-12-14
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多