36#include "irplib_wcs.h"
46static cpl_error_code irplib_wcs_is_iso8601(
int,
int,
int,
int,
int,
double);
49#define IRPLIB_ISO8601_FORMAT "%4d-%2d-%2dT%2d:%2d:%lf"
65cpl_error_code irplib_wcs_xytoradec(
const cpl_wcs *wcs,
72 cpl_matrix * radec = NULL;
73 cpl_array * status = NULL;
76 cpl_ensure_code(ra != NULL, CPL_ERROR_NULL_INPUT);
77 cpl_ensure_code(dec != NULL, CPL_ERROR_NULL_INPUT);
80 xy = cpl_matrix_new(1, 2);
81 cpl_matrix_set(xy, 0, 0, x);
82 cpl_matrix_set(xy, 0, 1, y);
85 error = cpl_wcs_convert(wcs, xy, &radec, &status, CPL_WCS_PHYS2WORLD);
87 cpl_matrix_delete(xy);
92 *ra = cpl_matrix_get(radec, 0, 0);
93 *dec = cpl_matrix_get(radec, 0, 1);
98 cpl_matrix_delete(radec);
99 cpl_array_delete(status);
101 return cpl_error_set_where(cpl_func);
116cpl_error_code irplib_wcs_radectoxy(
const cpl_wcs * wcs,
123 cpl_matrix * xy = NULL;
124 cpl_array * status = NULL;
125 cpl_error_code error;
127 cpl_ensure_code(x != NULL, CPL_ERROR_NULL_INPUT);
128 cpl_ensure_code(y != NULL, CPL_ERROR_NULL_INPUT);
131 radec = cpl_matrix_new(1, 2);
132 cpl_matrix_set(radec, 0, 0, ra);
133 cpl_matrix_set(radec, 0, 1, dec);
135 error = cpl_wcs_convert(wcs, radec, &xy, &status, CPL_WCS_WORLD2PHYS);
137 cpl_matrix_delete(radec);
141 *x = cpl_matrix_get(xy, 0, 0);
142 *y = cpl_matrix_get(xy, 0, 1);
147 cpl_array_delete(status);
148 cpl_matrix_delete(xy);
150 return cpl_error_set_where(cpl_func);
165double irplib_wcs_great_circle_dist(
double ra1,
172 const double dra = sin( CPL_MATH_RAD_DEG * (ra2 - ra1 )/2.0 );
173 const double ddec = sin( CPL_MATH_RAD_DEG * (dec2 - dec1)/2.0 );
175 dec1 *= CPL_MATH_RAD_DEG;
176 dec2 *= CPL_MATH_RAD_DEG;
178 return 2.0 * asin(sqrt( ddec*ddec + cos(dec1)*cos(dec2)*dra*dra))
198cpl_error_code irplib_wcs_mjd_from_iso8601(
double * pmjd,
int year,
int month,
199 int day,
int hour,
int minute,
203 cpl_ensure_code(pmjd != NULL, CPL_ERROR_NULL_INPUT);
204 cpl_ensure_code(!irplib_wcs_is_iso8601(year, month, day, hour, minute,
205 second), cpl_error_get_code());
208 *pmjd = (double)((1461*(year - (12-month)/10 + 4712))/4
209 + (306*((month+9)%12) + 5)/10
210 - (3*((year - (12-month)/10 + 4900)/100))/4
212 + (hour + (minute + second/60.0)/60.0)/24.0;
214 return CPL_ERROR_NONE;
236cpl_error_code irplib_wcs_iso8601_from_string(
int * pyear,
int * pmonth,
237 int * pday,
int * phour,
238 int * pminute,
double * psecond,
239 const char * iso8601)
244 cpl_ensure_code(pyear != NULL, CPL_ERROR_NULL_INPUT);
245 cpl_ensure_code(pmonth != NULL, CPL_ERROR_NULL_INPUT);
246 cpl_ensure_code(pday != NULL, CPL_ERROR_NULL_INPUT);
247 cpl_ensure_code(phour != NULL, CPL_ERROR_NULL_INPUT);
248 cpl_ensure_code(pminute != NULL, CPL_ERROR_NULL_INPUT);
249 cpl_ensure_code(psecond != NULL, CPL_ERROR_NULL_INPUT);
250 cpl_ensure_code(iso8601 != NULL, CPL_ERROR_NULL_INPUT);
252 nret = sscanf(iso8601, IRPLIB_ISO8601_FORMAT, pyear, pmonth,
253 pday, phour, pminute, psecond);
256 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Parsed"
257 " %d != 6: input %s is not in format %s",
258 nret, iso8601, IRPLIB_ISO8601_FORMAT);
261 return irplib_wcs_is_iso8601(*pyear, *pmonth, *pday, *phour, *pminute,
263 ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
277cpl_error_code irplib_wcs_mjd_from_string(
double * pmjd,
const char * iso8601)
281 int year, day, month, hour, minute;
284 return irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
285 &minute, &second, iso8601)
286 || irplib_wcs_mjd_from_iso8601(pmjd, year, month, day, hour, minute,
288 ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
308cpl_error_code irplib_wcs_iso8601_from_mjd(
int * pyear,
int * pmonth,
309 int * pday,
int * phour,
310 int * pminute,
double * psecond,
317 cpl_ensure_code(pyear != NULL, CPL_ERROR_NULL_INPUT);
318 cpl_ensure_code(pmonth != NULL, CPL_ERROR_NULL_INPUT);
319 cpl_ensure_code(pday != NULL, CPL_ERROR_NULL_INPUT);
320 cpl_ensure_code(phour != NULL, CPL_ERROR_NULL_INPUT);
321 cpl_ensure_code(pminute != NULL, CPL_ERROR_NULL_INPUT);
322 cpl_ensure_code(psecond != NULL, CPL_ERROR_NULL_INPUT);
326 jd = 2400001 + (int)mjd;
328 n4 = 4*(jd + ((2*((4*jd - 17918)/146097)*3)/4 + 1)/2 - 37);
329 dd = 10*(((n4-237)%1461)/4) + 5;
331 *pyear = n4/1461 - 4712;
332 *pmonth = (2 + dd/306)%12 + 1;
333 *pday = (dd%306)/10 + 1;
339 t = 60.0 * (t - *phour);
341 *psecond = 60.0 * (t - *pminute);
344 cpl_ensure_code(!irplib_wcs_is_iso8601(*pyear, *pmonth, *pday, *phour,
346 CPL_ERROR_UNSPECIFIED);
348 return CPL_ERROR_NONE;
367static cpl_error_code irplib_wcs_is_iso8601(
int year,
int month,
369 int minute,
double second)
372 const cpl_boolean is_leap = (year % 4) ? CPL_FALSE : CPL_TRUE;
373 const int mlen[] = {0, 31, is_leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30,
376 cpl_ensure_code(month > 0, CPL_ERROR_ILLEGAL_INPUT);
377 cpl_ensure_code(month <= 12, CPL_ERROR_ILLEGAL_INPUT);
379 cpl_ensure_code(day > 0, CPL_ERROR_ILLEGAL_INPUT);
380 cpl_ensure_code(day <= mlen[month], CPL_ERROR_ILLEGAL_INPUT);
382 cpl_ensure_code(minute < 60, CPL_ERROR_ILLEGAL_INPUT);
383 cpl_ensure_code(minute >= 0, CPL_ERROR_ILLEGAL_INPUT);
385 cpl_ensure_code(second < 60.0, CPL_ERROR_ILLEGAL_INPUT);
386 cpl_ensure_code(second >= 0.0, CPL_ERROR_ILLEGAL_INPUT);
388 cpl_ensure_code(hour >= 0, CPL_ERROR_ILLEGAL_INPUT);
390 cpl_ensure_code(hour <= (minute > 0 || second > 0.0 ? 23 : 24),
391 CPL_ERROR_ILLEGAL_INPUT);
393 return CPL_ERROR_NONE;