简体   繁体   中英

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. 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?

If it is always the last three digits:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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