scem.c (2303B)
1 /* Copyright (C) 2025 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "scem.h" 17 #include "scem_c.h" 18 19 #include <math.h> 20 21 /******************************************************************************* 22 * Exported functions 23 ******************************************************************************/ 24 res_T 25 scem_sun_position_from_earth 26 (const struct tm* time, /* In UTC */ 27 const struct scem_location* pos, /* Local position */ 28 const enum scem_sun_algo algo, 29 struct scem_sun_pos* sun) 30 { 31 res_T res = RES_OK; 32 33 if(!time || !pos || !sun || (unsigned)algo >= SCEM_SUN_ALGO_NONE__) 34 return RES_BAD_ARG; 35 36 if(pos->latitude < -90 37 || pos->latitude > +90 38 || pos->longitude < -180 39 || pos->longitude > +180) 40 return RES_BAD_ARG; 41 42 switch(algo) { 43 case SCEM_SUN_MEEUS: 44 res = sun_position_from_earth_meeus(time, pos, sun); 45 break; 46 case SCEM_SUN_PSA: 47 res = sun_position_from_earth_psa(time, pos, sun); 48 break; 49 default: FATAL("Unreachable code\n"); break; 50 } 51 return res; 52 } 53 54 res_T 55 scem_sun_position_to_sun_vector 56 (const struct scem_sun_pos* spherical, 57 double sun_dir[3]) 58 { 59 double cos_azimuth; 60 double sin_azimuth; 61 double cos_elevation; 62 double sin_elevation; 63 double azimuth; 64 double elevation; 65 66 if(!spherical || !sun_dir) return RES_BAD_ARG; 67 68 azimuth = spherical->azimuth; 69 elevation = spherical->elevation; 70 71 cos_azimuth = cos(azimuth); 72 sin_azimuth = sin(azimuth); 73 cos_elevation = cos(elevation); 74 sin_elevation = sin(elevation); 75 76 sun_dir[0] = -(cos_elevation * sin_azimuth); 77 sun_dir[1] = -(cos_elevation * cos_azimuth); 78 sun_dir[2] = -(sin_elevation); 79 return RES_OK; 80 }