Reputation: 403
I am trying to calculate how many hours of sunlight each latitude of Earth receives every year. I have a formula which calculates this, that uses two arrays as input, the day of the year, and the latitude. What I want to do is to use the formula to create a 2D-array with each day on one axis and each latitude on another, but I am unsure how to do it since my formula will only return the dot product of the two arrays rather than the cross product, making the result only a single axis. My code looks something like this:
Latitude = np.linspace(1,90,90)
Days = np.linspace(1,365,365)
P = np.arcsin(0.39795 * np.cos(0.2163108 + 2 * np.arctan(0.9671396 * np.tan(0.00860 * (Days - 186)))))
daylight = 24 - (24 / np.pi) * np.arccos((np.sin(0.8333 * np.pi / 180) + np.sin(Latitude * np.pi / 180) * np.sin(P)) / (np.cos(Latitude * np.pi / 180) * np.cos(P)))
What I what is then to calculate the formula "daylight" but to create a resulting 2D-array such that each value of P is multiplied with each value of Latitude, creating a 90x365 matrix.
Upvotes: 0
Views: 1126
Reputation: 1744
If this is the core problem statement:
What I what is then to calculate the formula "daylight" but to create a resulting 2D-array such that each value of P is multiplied with each value of Latitude, creating a 90x365 matrix.
Then, just do the outer product of the two arrays, from your formula,
_P = np.sin(P)
_Lat = np.sin(Latitude * np.pi / 180)
PLat = np.outer(_P, _Lat)
np.outer
used in this form will multiply each element of the first array (size m) with each element of the second (size n), yielding a m x n array.
Read the docs.
After you've created PLat
, the outer product matrix, add/multiply it with the other scalars.
Upvotes: 1
Reputation: 86
In Python you can create arrays like this:
a = [i for i in range(5)]
And you can also put multiple of these inside each other:
a = [[x*y for x in P] for y in Latitude]
And of course here you could insert your formula:
daylight = [[24 - (24 / np.pi) * np.arccos((np.sin(0.8333 * np.pi / 180) + np.sin(y * np.pi / 180) * np.sin(x)) / (np.cos(y * np.pi / 180) * np.cos(x))) for x in P] for y in Latitude]
Also this result contains a lot of nan
values. From the context I would assume that these are polar nights/days. You could perhaps do some sort of np.arccos(min(1,max(-1,...))
to take care of these.
Upvotes: 1
Reputation: 273
Try numpy.meshgrid. This will create two 2D-arrays for latitude and days for you:
lat2d, days2d = np.meshgrid(Latitude, Days)
Those arrays will have shape (365, 90)
and one axis represents one variable (so in lat2d
, all rows will be the same and in days2d
all columns will be the same). You can then use those arrays as in your formula.
Upvotes: 1