File: irplib_wcs.c

package info (click to toggle)
cpl-plugin-sinfo 2.5.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,512 kB
  • ctags: 3,661
  • sloc: ansic: 82,282; sh: 11,405; makefile: 711; python: 278
file content (391 lines) | stat: -rw-r--r-- 14,268 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* $Id: irplib_wcs.c,v 1.8 2010-10-07 14:10:55 llundin Exp $
 *
 * This file is part of the irplib package
 * Copyright (C) 2002,2003 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
 */

/*
 * $Author: llundin $
 * $Date: 2010-10-07 14:10:55 $
 * $Revision: 1.8 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*-----------------------------------------------------------------------------
                                   Includes
 -----------------------------------------------------------------------------*/

#include <math.h>
#include <cpl.h>

#include "irplib_wcs.h"

/*----------------------------------------------------------------------------*/
/**
 * @defgroup irplib_wcs   Functions related to WCS
 */
/*----------------------------------------------------------------------------*/

static cpl_error_code irplib_wcs_is_iso8601(int, int, int, int, int, double);

/**@{*/

/*----------------------------------------------------------------------------*/
/**
  @brief Convert x, y coordinates (physical) to RA, DEC (World)
  @param wcs    The World Coordinate System solution
  @param x      The X coordinate (Physical)
  @param y      The Y coordinate (Physical)
  @param ra     The RA coordinate (World) (returned)
  @param dec    The DEC coordinate (World) (returned)
  @see cpl_wcs_convert()

 */
/*----------------------------------------------------------------------------*/
cpl_error_code irplib_wcs_xytoradec(const cpl_wcs *wcs,
                                    double         x,
                                    double         y,
                                    double        *ra,
                                    double        *dec)
{
    cpl_matrix   * xy;
    cpl_matrix   * radec  = NULL;
    cpl_array    * status = NULL;
    cpl_error_code error;

    cpl_ensure_code(ra  != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(dec != NULL, CPL_ERROR_NULL_INPUT);

    /* Load up the information */
    xy = cpl_matrix_new(1, 2);
    cpl_matrix_set(xy, 0, 0, x);
    cpl_matrix_set(xy, 0, 1, y);

    /* Call the conversion routine */
    error = cpl_wcs_convert(wcs, xy, &radec, &status, CPL_WCS_PHYS2WORLD);

    cpl_matrix_delete(xy);

    if (!error) {

        /* Pass it back now */
        *ra  = cpl_matrix_get(radec, 0, 0);
        *dec = cpl_matrix_get(radec, 0, 1);

    }

    /* Tidy and propagate error, if any */
    cpl_matrix_delete(radec);
    cpl_array_delete(status);

    return cpl_error_set_where(cpl_func);
}

/*----------------------------------------------------------------------------*/
/**
  @brief Convert RA, DEC (World) to x, y coordinates (physical)
  @param wcs    The World Coordinate System solution
  @param ra     The RA coordinate (World) (returned)
  @param dec    The DEC coordinate (World)
  @param x      The X coordinate (Physical) (returned)
  @param y      The Y coordinate (Physical) (returned)
  @see cpl_wcs_convert()

 */
/*----------------------------------------------------------------------------*/
cpl_error_code irplib_wcs_radectoxy(const cpl_wcs * wcs,
                                    double          ra,
                                    double          dec,
                                    double        * x,
                                    double        * y)
{
    cpl_matrix   * radec;
    cpl_matrix   * xy = NULL;
    cpl_array    * status   = NULL;
    cpl_error_code error;

    cpl_ensure_code(x != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(y != NULL, CPL_ERROR_NULL_INPUT);

    /* Feed the matrix with RA, DEC */
    radec = cpl_matrix_new(1, 2);
    cpl_matrix_set(radec, 0, 0, ra);
    cpl_matrix_set(radec, 0, 1, dec);

    error = cpl_wcs_convert(wcs, radec, &xy, &status, CPL_WCS_WORLD2PHYS);

    cpl_matrix_delete(radec);

    if (!error) {

        *x  = cpl_matrix_get(xy, 0, 0);
        *y  = cpl_matrix_get(xy, 0, 1);

    }

    /* Tidy and propagate error, if any */
    cpl_array_delete(status);
    cpl_matrix_delete(xy);

    return cpl_error_set_where(cpl_func);

}

/*----------------------------------------------------------------------------*/
/**
  @brief   Compute the great-circle distance between two points on a sphere
  @param   ra1    Right ascension of first point [degrees]
  @param   dec1   Declination of first point [degrees]
  @param   ra2    Right ascension of second point [degrees]
  @param   dec2   Declination of second point [degrees]
  @return  Non-negative distance [degrees].
  @see     http://en.wikipedia.org/wiki/Great-circle_distance (on 2005-10-23)
 */
/*----------------------------------------------------------------------------*/
double irplib_wcs_great_circle_dist(double ra1,
                                    double dec1,
                                    double ra2,
                                    double dec2)
{

    /* Convert all input from degrees to radian - and back for the result */
    const double dra  = sin( CPL_MATH_RAD_DEG * (ra2  - ra1 )/2.0 );
    const double ddec = sin( CPL_MATH_RAD_DEG * (dec2 - dec1)/2.0 );

    dec1 *= CPL_MATH_RAD_DEG;
    dec2 *= CPL_MATH_RAD_DEG;

    return 2.0 * asin(sqrt( ddec*ddec + cos(dec1)*cos(dec2)*dra*dra))
        * CPL_MATH_DEG_RAD;
}


/*----------------------------------------------------------------------------*/
/**
  @brief   Convert a date from ISO-8601 to Modified Julian Date (MJD)
  @param   pmjd   On success, the MJD
  @param   year   The ISO-8601 Year
  @param   month  The ISO-8601 Month  (1 for first)
  @param   day    The ISO-8601 Day    (1 for first)
  @param   hour   The ISO-8601 Hour   (0 for first)
  @param   minute The ISO-8601 Minute (0 for first)
  @param   second The ISO-8601 Second (0 for first)
  @return  CPL_ERROR_NONE on success, otherwise the error
  @see The conversion code in wcslib version 4.4.4

 */
/*----------------------------------------------------------------------------*/
cpl_error_code irplib_wcs_mjd_from_iso8601(double * pmjd, int year, int month,
                                           int day, int hour, int minute,
                                           double second)
{

    cpl_ensure_code(pmjd  != NULL,        CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(!irplib_wcs_is_iso8601(year, month, day, hour, minute,
                                           second), cpl_error_get_code());

    /* Compute MJD. */
    *pmjd = (double)((1461*(year - (12-month)/10 + 4712))/4
                     + (306*((month+9)%12) + 5)/10
                     - (3*((year - (12-month)/10 + 4900)/100))/4
                     + day - 2399904)
        + (hour + (minute + second/60.0)/60.0)/24.0;

    return CPL_ERROR_NONE;

}


/*----------------------------------------------------------------------------*/
/**
  @brief   Extract an ISO-8601 date from a string
  @param   pyear   The ISO-8601 Year
  @param   pmonth  The ISO-8601 Month  (1 for first)
  @param   pday    The ISO-8601 Day    (1 for first)
  @param   phour   The ISO-8601 Hour   (0 for first)
  @param   pminute The ISO-8601 Minute (0 for first)
  @param   psecond The ISO-8601 Second (0 for first)
  @param   iso8601 The ISO-8601 formatted string
  @return  CPL_ERROR_NONE on success, otherwise the error
  @see irplib_wcs_mjd_from_iso8601()
  @note The format must be the
        standard year-2000 form: CCYY-MM-DD[Thh:mm:ss[.sss...]]

 */
/*----------------------------------------------------------------------------*/
cpl_error_code irplib_wcs_iso8601_from_string(int * pyear, int * pmonth,
                                              int * pday, int * phour,
                                              int * pminute, double * psecond,
                                              const char * iso8601)
{

    /* Standard year-2000 form: CCYY-MM-DD[Thh:mm:ss[.sss...]] */
    const char * iso8601format = "%4d-%2d-%2dT%2d:%2d:%lf";

    cpl_ensure_code(pyear   != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(pmonth  != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(pday    != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(phour   != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(pminute != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(psecond != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(iso8601 != NULL, CPL_ERROR_NULL_INPUT);

    cpl_error_ensure(sscanf(iso8601, iso8601format, pyear, pmonth,
                            pday, phour, pminute, psecond) == 6,
                     CPL_ERROR_ILLEGAL_INPUT, return cpl_error_get_code(),
                     "%s is not formatted as %s", iso8601, iso8601format);

    cpl_ensure_code(!irplib_wcs_is_iso8601(*pyear, *pmonth, *pday, *phour,
                                           *pminute, *psecond),
                    cpl_error_get_code());

    return CPL_ERROR_NONE;
}


/*----------------------------------------------------------------------------*/
/**
  @brief   Convert a date from a ISO-8601 string to Modified Julian Date (MJD)
  @param   pmjd    On success, the MJD
  @param   iso8601 The ISO-8601 formatted string
  @return  CPL_ERROR_NONE on success, otherwise the error
  @see irplib_wcs_iso8601_from_string(), irplib_wcs_mjd_from_iso8601()

 */
/*----------------------------------------------------------------------------*/
cpl_error_code irplib_wcs_mjd_from_string(double * pmjd, const char * iso8601)
{


    int year, day, month, hour, minute;
    double second;

    return irplib_wcs_iso8601_from_string(&year, &month, &day, &hour,
                                          &minute, &second, iso8601)
        || irplib_wcs_mjd_from_iso8601(pmjd, year, month, day, hour, minute,
                                       second)
        ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
}



/*----------------------------------------------------------------------------*/
/**
  @brief   Convert a date from Modified Julian Date (MJD) to ISO-8601
  @param   pyear   The ISO-8601 Year
  @param   pmonth  The ISO-8601 Month  (1 for first)
  @param   pday    The ISO-8601 Day    (1 for first)
  @param   phour   The ISO-8601 Hour   (0 for first)
  @param   pminute The ISO-8601 Minute (0 for first)
  @param   psecond The ISO-8601 Second (0 for first)
  @param   mjd     The MJD
  @return  CPL_ERROR_NONE on success, otherwise the error
  @see irplib_wcs_mjd_from_iso8601()

 */
/*----------------------------------------------------------------------------*/
cpl_error_code irplib_wcs_iso8601_from_mjd(int * pyear, int * pmonth,
                                           int * pday, int * phour,
                                           int * pminute, double * psecond,
                                           double mjd)
{

    int jd, n4, dd;
    double t;

    cpl_ensure_code(pyear   != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(pmonth  != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(pday    != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(phour   != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(pminute != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(psecond != NULL, CPL_ERROR_NULL_INPUT);

    /* Copied from datfix() in wcslib (v. 4.4.4) */

    jd = 2400001 + (int)mjd;

    n4 =  4*(jd + ((2*((4*jd - 17918)/146097)*3)/4 + 1)/2 - 37);
    dd = 10*(((n4-237)%1461)/4) + 5;

    *pyear  = n4/1461 - 4712;
    *pmonth = (2 + dd/306)%12 + 1;
    *pday   = (dd%306)/10 + 1;

    t = mjd - (int)mjd; /* t is now days */

    t *= 24.0; /* t is now hours */
    *phour = (int)t;
    t = 60.0 * (t - *phour); /* t is now minutes */
    *pminute = (int)t;
    *psecond = 60.0 * (t - *pminute);

    /* A failure here implies that this code has a bug */
    cpl_ensure_code(!irplib_wcs_is_iso8601(*pyear, *pmonth, *pday, *phour,
                                           *pminute, *psecond),
                    CPL_ERROR_UNSPECIFIED);

    return CPL_ERROR_NONE;
}


/**@}*/

/*----------------------------------------------------------------------------*/
/**
  @internal
  @brief   Verify that the six numbers comprise a valid ISO-8601 date
  @param   year   The Year
  @param   month  The Month  (1 for first)
  @param   day    The Day    (1 for first)
  @param   hour   The Hour   (0 for first)
  @param   minute The Minute (0 for first)
  @param   second The Second (0 for first)
  @return  CPL_ERROR_NONE on valid input, otherwise CPL_ERROR_ILLEGAL_INPUT
 */
/*----------------------------------------------------------------------------*/
static cpl_error_code irplib_wcs_is_iso8601(int year, int month,
                                            int day, int hour,
                                            int minute, double second)
{

    const cpl_boolean is_leap = year % 4 ? CPL_FALSE : CPL_TRUE;
    const int mlen[] = {0, 31, is_leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30,
                        31, 30, 31};

    cpl_ensure_code(month > 0,            CPL_ERROR_ILLEGAL_INPUT);
    cpl_ensure_code(month <= 12,          CPL_ERROR_ILLEGAL_INPUT);

    cpl_ensure_code(day   > 0,            CPL_ERROR_ILLEGAL_INPUT);
    cpl_ensure_code(day   <= mlen[month], CPL_ERROR_ILLEGAL_INPUT);

    cpl_ensure_code(minute  < 60,         CPL_ERROR_ILLEGAL_INPUT);
    cpl_ensure_code(minute  >= 0,         CPL_ERROR_ILLEGAL_INPUT);

    cpl_ensure_code(second  < 60.0,       CPL_ERROR_ILLEGAL_INPUT);
    cpl_ensure_code(second  >= 0.0,       CPL_ERROR_ILLEGAL_INPUT);

    cpl_ensure_code(hour  >= 0,           CPL_ERROR_ILLEGAL_INPUT);
    /* 24:00:00 is valid ISO-8601 */
    cpl_ensure_code(hour  <= (minute > 0 || second > 0.0 ? 23 : 24),
                    CPL_ERROR_ILLEGAL_INPUT);

    return CPL_ERROR_NONE;
}