Skip to content
Snippets Groups Projects
Commit dc41c786 authored by Bouvry Arnaud's avatar Bouvry Arnaud
Browse files

[feature] Create conversion_functions.py for functions that can be used in several contexts

For now it contains conversion from cartesian to spherical coordinates. Takes numpy arrays as input, of dimensions (3,) or (N, 3) and automatically adapts.
parent 950a114f
No related branches found
No related tags found
2 merge requests!21v1.1.0,!19Resolve "[fix] Diffuse irradiance is not fully anisotropic"
......@@ -12,6 +12,7 @@ import pvlib.solarposition as pvlibSP
import matplotlib.pyplot as plt
import os
import MODULES.conversion_functions as cf
def fibonacci_half_sphere(samples=18):
......
import numpy as np
def cart_to_sph(x, y, z):
"""
Args:
cart_coords:
Returns:
zenith_angle: zenith angle [rad]
"""
r = np.sqrt(x**2 + y**2 + z**2)
if z > 0:
zenith = np.arctan((x**2+y**2)/z)
elif z < 0:
raise NotImplementedError('Not done yet')
elif (z == 0) and (np.sqrt(x**2+y**2) != 0):
zenith = np.pi/2
elev = np.pi/2 - zenith
if x > 0:
az = np.arctan(y/x)
elif (x < 0) and (y >= 0):
az = np.arctan(y/x) + np.pi
elif (x < 0) and (y < 0):
az = np.arctan(y/x) - np.pi
elif (x==0) and (y > 0):
az = np.pi/2
elif (x==0) and (y < 0):
az = -np.pi/2
elif (x==0) and (y==0):
az = np.nan
return az, zenith
def get_zenith_angle_from_cart(cart_coords):
"""
Args:
cart_coords: cartesian coordinates (x, y and z) to convert to azimuth, zenith
cart_coords type: Numpy array of shape (N, 3)
Returns:
azimuth_angle: zenith angle [rad]
zenith_angle: zenith angle [rad]
"""
cart_to_sph_vect = np.vectorize(cart_to_sph)
x_vect = np.atleast_2d(cart_coords)[:, 0]
y_vect = np.atleast_2d(cart_coords)[:, 1]
z_vect = np.atleast_2d(cart_coords)[:, 2]
azimuth, zenith_angle = cart_to_sph_vect(x_vect, y_vect, z_vect)
return azimuth, zenith_angle
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment