1

I have this python function that calculates day length depending on latitude and day of the year (source : https://gist.github.com/anttilipp/ed3ab35258c7636d87de6499475301ce).

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 

where dayOfYear : int and lat : float

How do I adapt this so lat uses the values from my array latarray to create a new array of the same .shape(883,1368) ?

3 Answers 3

1

How about a list comprehension? Then you don't need to change your function:

[daylength(dayOfYear, lat) for lat in latarray]
Sign up to request clarification or add additional context in comments.

1 Comment

or map: list(map(lambda x: daylength(dayOfYear, x), latarray))
0

You can use vectorize function in your array and it will apply your function in each element in the array latarray. See demo below:

import numpy as np
vfunc = np.vectorize(daylength)
dayOfYear = 100
latarray = np.array([[10, 20, 30], [3, 6, 9]])
vfunc(dayOfYear, latarray)

Result:
array([[12.16899985, 12.34893859, 12.5537987 ],
       [12.05022628, 12.10073141, 12.1518005 ]])

Reference: https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html

Comments

0
value=np.zeros((883,1368))

for i in range(np.size(latarray,0)):
    for j in range(np.size(latarray,1)):
         value[i,j] = daylength(DayOfYear, latarray[i,j])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.