1 /*
2  * This file is part of Siril, an astronomy image processor.
3  * Copyright (C) 2005-2011 Francois Meyer (dulle at free.fr)
4  * Copyright (C) 2012-2021 team free-astro (see more in AUTHORS file)
5  * Reference site is https://free-astro.org/index.php/Siril
6  *
7  * Siril is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Siril is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Siril. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <glib.h>
22 #include <math.h>
23 #include <stdio.h>
24 
25 #include "siril_world_cs.h"
26 
27 struct _SirilWorldCS {
28 	gdouble alpha;
29 
30 	gdouble delta;
31 
32 	gint ref_count; /* (atomic) */
33 };
34 
siril_world_cs_alloc()35 static SirilWorldCS* siril_world_cs_alloc() {
36 	SirilWorldCS *world_cs;
37 
38 	world_cs = g_slice_new0(SirilWorldCS);
39 	world_cs->ref_count = 1;
40 
41 	return world_cs;
42 }
43 
44 
siril_world_cs_ref(SirilWorldCS * world_cs)45 SirilWorldCS *siril_world_cs_ref(SirilWorldCS *world_cs) {
46 	g_return_val_if_fail(world_cs != NULL, NULL);
47 	g_return_val_if_fail(world_cs->ref_count > 0, NULL);
48 
49 	g_atomic_int_inc(&world_cs->ref_count);
50 
51 	return world_cs;
52 }
53 
siril_world_cs_unref(SirilWorldCS * world_cs)54 void siril_world_cs_unref(SirilWorldCS *world_cs) {
55 	g_return_if_fail(world_cs != NULL);
56 	g_return_if_fail(world_cs->ref_count > 0);
57 
58 	if (g_atomic_int_dec_and_test(&world_cs->ref_count)) {
59 		g_slice_free(SirilWorldCS, world_cs);
60 	}
61 }
62 
siril_world_cs_new_from_a_d(gdouble alpha,gdouble delta)63 SirilWorldCS* siril_world_cs_new_from_a_d(gdouble alpha, gdouble delta) {
64 	SirilWorldCS *world_cs;
65 	/*
66 	 * make sure new RA lies in range  0 < RA < 360
67 	 */
68 	if (alpha < 0) {
69 		alpha += 360.0;
70 	}
71 	if (alpha >= 360.0) {
72 		alpha -= 360.0;
73 	}
74 
75 	/*
76 	 * make sure Dec lies in range  -90 < Dec < +90
77 	 */
78 	if (delta < -90) {
79 		delta += 180;
80 	}
81 	if (delta > 90) {
82 		delta -= 180;
83 	}
84 
85 	world_cs = siril_world_cs_alloc();
86 	world_cs->alpha = alpha;
87 	world_cs->delta = delta;
88 
89 	return world_cs;
90 }
91 
siril_world_cs_new_from_ra_dec(gdouble ra_h,gdouble ra_m,gdouble ra_s,gdouble dec_deg,gdouble dec_m,gdouble dec_s)92 SirilWorldCS* siril_world_cs_new_from_ra_dec(gdouble ra_h, gdouble ra_m, gdouble ra_s, gdouble dec_deg, gdouble dec_m, gdouble dec_s) {
93 	SirilWorldCS *world_cs;
94 
95 	world_cs = siril_world_cs_alloc();
96 
97 	world_cs->alpha = ra_h * 15.0 + ra_m * 15.0 / 60.0 + ra_s * 15.0 / 3600.0;
98 	if (dec_deg > 0) {
99 		world_cs->delta = ((dec_s / 3600.0) + (dec_m / 60.0) + dec_deg);
100 	} else {
101 		world_cs->delta = (-(dec_s / 3600.0) - (dec_m / 60.0) + dec_deg);
102 	}
103 	return world_cs;
104 }
105 
siril_world_cs_new_from_objct_ra_dec(gchar * objctra,gchar * objctdec)106 SirilWorldCS* siril_world_cs_new_from_objct_ra_dec(gchar *objctra, gchar *objctdec) {
107 	SirilWorldCS *world_cs;
108 	int ra_h, ra_m, dec_deg, dec_m;
109 	gdouble ra_s, dec_s;
110 	gboolean south;
111 
112 	sscanf(objctra, "%d %d %lf", &ra_h, &ra_m, &ra_s);
113 	sscanf(objctdec, "%d %d %lf", &dec_deg, &dec_m, &dec_s);
114 	south = objctdec[0] == '-';
115 
116 	world_cs = siril_world_cs_alloc();
117 
118 	world_cs->alpha = ra_h * 15.0 + ra_m * 15.0 / 60.0 + ra_s * 15.0 / 3600.0;
119 	if ((dec_deg == 0 && !south) || dec_deg > 0) {
120 		world_cs->delta = ((dec_s / 3600.0) + (dec_m / 60.0) + dec_deg);
121 	} else {
122 		world_cs->delta = (-(dec_s / 3600.0) - (dec_m / 60.0) + dec_deg);
123 	}
124 	return world_cs;
125 }
126 
siril_world_cs_get_alpha(SirilWorldCS * world_cs)127 gdouble siril_world_cs_get_alpha(SirilWorldCS *world_cs) {
128 	return world_cs->alpha;
129 }
130 
siril_world_cs_get_delta(SirilWorldCS * world_cs)131 gdouble siril_world_cs_get_delta(SirilWorldCS *world_cs) {
132 	return world_cs->delta;
133 }
134 
siril_world_cs_delta_format(SirilWorldCS * world_cs,const gchar * format)135 gchar* siril_world_cs_delta_format(SirilWorldCS *world_cs, const gchar *format) {
136 	g_return_val_if_fail(world_cs != NULL, NULL);
137 	g_return_val_if_fail(format != NULL, NULL);
138 	g_return_val_if_fail(g_utf8_validate (format, -1, NULL), NULL);
139 
140 	gchar sig = '+';
141 	gdouble dec = world_cs->delta;
142 
143 	if (dec < 0) sig = '-';
144 
145 	dec = fabs(dec);
146 
147 	int degree = (int) dec;
148 	int min = abs((int) ((dec - degree) * 60.0));
149 	double sec = (fabs((dec - degree) * 60.0) - min) * 60.0;
150 
151 	gchar *ptr = g_strrstr(format, "lf");
152 	if (ptr) { // floating point for second
153 		return g_strdup_printf(format, sig, degree, min, sec);
154 	}
155 	return g_strdup_printf(format, sig, degree, min, (int) round(sec));
156 }
157 
siril_world_cs_alpha_format(SirilWorldCS * world_cs,const gchar * format)158 gchar* siril_world_cs_alpha_format(SirilWorldCS *world_cs, const gchar *format) {
159 	g_return_val_if_fail(world_cs != NULL, NULL);
160 	g_return_val_if_fail(format != NULL, NULL);
161 	g_return_val_if_fail(g_utf8_validate (format, -1, NULL), NULL);
162 
163 	gdouble ra = world_cs->alpha;
164 
165 	ra = fabs(ra);
166 
167 	int hour = (int)(ra / 15.0);
168 	int min = (int)(((ra / 15.0) - hour) * 60.0);
169 	double sec = ((((ra / 15.0) - hour) * 60.0) - min) * 60.0;
170 
171 	gchar *ptr = g_strrstr(format, "lf");
172 	if (ptr) { // floating point for second
173 		return g_strdup_printf(format, hour, min, sec);
174 	}
175 	return g_strdup_printf(format, hour, min, (int) round(sec));
176 }
177 
siril_world_cs_get_ra_hour_min_sec(SirilWorldCS * world_cs,int * hour,int * min,double * sec)178 void siril_world_cs_get_ra_hour_min_sec(SirilWorldCS *world_cs, int *hour, int *min, double *sec) {
179 	int h, m;
180 	gdouble s;
181 
182 	h = (int)(world_cs->alpha / 15.0);
183 	m = (int)(((world_cs->alpha / 15.0) - h) * 60.0);
184 	s = ((((world_cs->alpha / 15.0) - h) * 60.0) - m) * 60.0;
185 
186 	if (hour)
187 		*hour = h;
188 	if (min)
189 		*min = m;
190 	if (sec)
191 		*sec = s;
192 }
193 
siril_world_cs_get_dec_deg_min_sec(SirilWorldCS * world_cs,int * deg,int * min,double * sec)194 void siril_world_cs_get_dec_deg_min_sec(SirilWorldCS *world_cs, int *deg, int *min, double *sec) {
195 	int d, m;
196 	gdouble s;
197 
198 	d = (int) world_cs->delta;
199 	m = abs((int) ((world_cs->delta - d) * 60.0));
200 	s = (fabs((world_cs->delta - d) * 60.0) - m) * 60.0;
201 
202 	if (deg)
203 		*deg = d;
204 	if (min)
205 		*min = m;
206 	if (sec)
207 		*sec = s;
208 }
209