00001 /*============================================================================ 00002 00003 WCSLIB 4.13 - an implementation of the FITS WCS standard. 00004 Copyright (C) 1995-2012, Mark Calabretta 00005 00006 This file is part of WCSLIB. 00007 00008 WCSLIB is free software: you can redistribute it and/or modify it under the 00009 terms of the GNU Lesser General Public License as published by the Free 00010 Software Foundation, either version 3 of the License, or (at your option) 00011 any later version. 00012 00013 WCSLIB is distributed in the hope that it will be useful, but WITHOUT ANY 00014 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 00016 more details. 00017 00018 You should have received a copy of the GNU Lesser General Public License 00019 along with WCSLIB. If not, see <http://www.gnu.org/licenses/>. 00020 00021 Correspondence concerning WCSLIB may be directed to: 00022 Internet email: mcalabre@atnf.csiro.au 00023 Postal address: Dr. Mark Calabretta 00024 Australia Telescope National Facility, CSIRO 00025 PO Box 76 00026 Epping NSW 1710 00027 AUSTRALIA 00028 00029 Author: Mark Calabretta, Australia Telescope National Facility 00030 http://www.atnf.csiro.au/~mcalabre/index.html 00031 $Id: tab_8h-source.html,v 1.5 2012/05/11 04:10:50 pteuben Exp $ 00032 *============================================================================= 00033 * 00034 * WCSLIB 4.13 - C routines that implement tabular coordinate systems as 00035 * defined by the FITS World Coordinate System (WCS) standard. Refer to 00036 * 00037 * "Representations of world coordinates in FITS", 00038 * Greisen, E.W., & Calabretta, M.R. 2002, A&A, 395, 1061 (paper I) 00039 * 00040 * "Representations of spectral coordinates in FITS", 00041 * Greisen, E.W., Calabretta, M.R., Valdes, F.G., & Allen, S.L. 00042 * 2006, A&A, 446, 747 (Paper III) 00043 * 00044 * Refer to the README file provided with WCSLIB for an overview of the 00045 * library. 00046 * 00047 * 00048 * Summary of the tab routines 00049 * --------------------------- 00050 * These routines implement the part of the FITS WCS standard that deals with 00051 * tabular coordinates, i.e. coordinates that are defined via a lookup table. 00052 * They define methods to be used for computing tabular world coordinates from 00053 * intermediate world coordinates (a linear transformation of image pixel 00054 * coordinates), and vice versa. They are based on the tabprm struct which 00055 * contains all information needed for the computations. The struct contains 00056 * some members that must be set by the user, and others that are maintained 00057 * by these routines, somewhat like a C++ class but with no encapsulation. 00058 * 00059 * tabini(), tabmem(), tabcpy(), and tabfree() are provided to manage the 00060 * tabprm struct, and another, tabprt(), to print its contents. 00061 * 00062 * A setup routine, tabset(), computes intermediate values in the tabprm struct 00063 * from parameters in it that were supplied by the user. The struct always 00064 * needs to be set up by tabset() but it need not be called explicitly - refer 00065 * to the explanation of tabprm::flag. 00066 * 00067 * tabx2s() and tabs2x() implement the WCS tabular coordinate transformations. 00068 * 00069 * Accuracy: 00070 * --------- 00071 * No warranty is given for the accuracy of these routines (refer to the 00072 * copyright notice); intending users must satisfy for themselves their 00073 * adequacy for the intended purpose. However, closure effectively to within 00074 * double precision rounding error was demonstrated by test routine ttab.c 00075 * which accompanies this software. 00076 * 00077 * 00078 * tabini() - Default constructor for the tabprm struct 00079 * ---------------------------------------------------- 00080 * tabini() allocates memory for arrays in a tabprm struct and sets all members 00081 * of the struct to default values. 00082 * 00083 * PLEASE NOTE: every tabprm struct should be initialized by tabini(), possibly 00084 * repeatedly. On the first invokation, and only the first invokation, the 00085 * flag member of the tabprm struct must be set to -1 to initialize memory 00086 * management, regardless of whether tabini() will actually be used to allocate 00087 * memory. 00088 * 00089 * Given: 00090 * alloc int If true, allocate memory unconditionally for arrays in 00091 * the tabprm struct. 00092 * 00093 * If false, it is assumed that pointers to these arrays 00094 * have been set by the user except if they are null 00095 * pointers in which case memory will be allocated for 00096 * them regardless. (In other words, setting alloc true 00097 * saves having to initalize these pointers to zero.) 00098 * 00099 * M int The number of tabular coordinate axes. 00100 * 00101 * K const int[] 00102 * Vector of length M whose elements (K_1, K_2,... K_M) 00103 * record the lengths of the axes of the coordinate array 00104 * and of each indexing vector. M and K[] are used to 00105 * determine the length of the various tabprm arrays and 00106 * therefore the amount of memory to allocate for them. 00107 * Their values are copied into the tabprm struct. 00108 * 00109 * It is permissible to set K (i.e. the address of the 00110 * array) to zero which has the same effect as setting 00111 * each element of K[] to zero. In this case no memory 00112 * will be allocated for the index vectors or coordinate 00113 * array in the tabprm struct. These together with the 00114 * K vector must be set separately before calling 00115 * tabset(). 00116 * 00117 * Given and returned: 00118 * tab struct tabprm* 00119 * Tabular transformation parameters. Note that, in 00120 * order to initialize memory management tabprm::flag 00121 * should be set to -1 when tab is initialized for the 00122 * first time (memory leaks may result if it had already 00123 * been initialized). 00124 * 00125 * Function return value: 00126 * int Status return value: 00127 * 0: Success. 00128 * 1: Null tabprm pointer passed. 00129 * 2: Memory allocation failed. 00130 * 3: Invalid tabular parameters. 00131 * 00132 * For returns > 1, a detailed error message is set in 00133 * tabprm::err if enabled, see wcserr_enable(). 00134 * 00135 * 00136 * tabmem() - Acquire tabular memory 00137 * --------------------------------- 00138 * tabmem() takes control of memory allocated by the user for arrays in the 00139 * tabprm struct. 00140 * 00141 * Given and returned: 00142 * tab struct tabprm* 00143 * Tabular transformation parameters. 00144 * 00145 * Function return value: 00146 * int Status return value: 00147 * 0: Success. 00148 * 1: Null tabprm pointer passed. 00149 * 2: Memory allocation failed. 00150 * 00151 * For returns > 1, a detailed error message is set in 00152 * tabprm::err if enabled, see wcserr_enable(). 00153 * 00154 * 00155 * tabcpy() - Copy routine for the tabprm struct 00156 * --------------------------------------------- 00157 * tabcpy() does a deep copy of one tabprm struct to another, using tabini() to 00158 * allocate memory for its arrays if required. Only the "information to be 00159 * provided" part of the struct is copied; a call to tabset() is required to 00160 * set up the remainder. 00161 * 00162 * Given: 00163 * alloc int If true, allocate memory unconditionally for arrays in 00164 * the tabprm struct. 00165 * 00166 * If false, it is assumed that pointers to these arrays 00167 * have been set by the user except if they are null 00168 * pointers in which case memory will be allocated for 00169 * them regardless. (In other words, setting alloc true 00170 * saves having to initalize these pointers to zero.) 00171 * 00172 * tabsrc const struct tabprm* 00173 * Struct to copy from. 00174 * 00175 * Given and returned: 00176 * tabdst struct tabprm* 00177 * Struct to copy to. tabprm::flag should be set to -1 00178 * if tabdst was not previously initialized (memory leaks 00179 * may result if it was previously initialized). 00180 * 00181 * Function return value: 00182 * int Status return value: 00183 * 0: Success. 00184 * 1: Null tabprm pointer passed. 00185 * 2: Memory allocation failed. 00186 * 00187 * For returns > 1, a detailed error message is set in 00188 * tabprm::err (associated with tabdst) if enabled, see 00189 * wcserr_enable(). 00190 * 00191 * 00192 * tabfree() - Destructor for the tabprm struct 00193 * -------------------------------------------- 00194 * tabfree() frees memory allocated for the tabprm arrays by tabini(). 00195 * tabini() records the memory it allocates and tabfree() will only attempt to 00196 * free this. 00197 * 00198 * PLEASE NOTE: tabfree() must not be invoked on a tabprm struct that was not 00199 * initialized by tabini(). 00200 * 00201 * Returned: 00202 * tab struct tabprm* 00203 * Coordinate transformation parameters. 00204 * 00205 * Function return value: 00206 * int Status return value: 00207 * 0: Success. 00208 * 1: Null tabprm pointer passed. 00209 * 00210 * 00211 * tabprt() - Print routine for the tabprm struct 00212 * ---------------------------------------------- 00213 * tabprt() prints the contents of a tabprm struct using wcsprintf(). Mainly 00214 * intended for diagnostic purposes. 00215 * 00216 * Given: 00217 * tab const struct tabprm* 00218 * Tabular transformation parameters. 00219 * 00220 * Function return value: 00221 * int Status return value: 00222 * 0: Success. 00223 * 1: Null tabprm pointer passed. 00224 * 00225 * 00226 * tabset() - Setup routine for the tabprm struct 00227 * ----------------------------------------------- 00228 * tabset() allocates memory for work arrays in the tabprm struct and sets up 00229 * the struct according to information supplied within it. 00230 * 00231 * Note that this routine need not be called directly; it will be invoked by 00232 * tabx2s() and tabs2x() if tabprm::flag is anything other than a predefined 00233 * magic value. 00234 * 00235 * Given and returned: 00236 * tab struct tabprm* 00237 * Tabular transformation parameters. 00238 * 00239 * Function return value: 00240 * int Status return value: 00241 * 0: Success. 00242 * 1: Null tabprm pointer passed. 00243 * 3: Invalid tabular parameters. 00244 * 00245 * For returns > 1, a detailed error message is set in 00246 * tabprm::err if enabled, see wcserr_enable(). 00247 * 00248 * 00249 * tabx2s() - Pixel-to-world transformation 00250 * ---------------------------------------- 00251 * tabx2s() transforms intermediate world coordinates to world coordinates 00252 * using coordinate lookup. 00253 * 00254 * Given and returned: 00255 * tab struct tabprm* 00256 * Tabular transformation parameters. 00257 * 00258 * Given: 00259 * ncoord, 00260 * nelem int The number of coordinates, each of vector length 00261 * nelem. 00262 * 00263 * x const double[ncoord][nelem] 00264 * Array of intermediate world coordinates, SI units. 00265 * 00266 * Returned: 00267 * world double[ncoord][nelem] 00268 * Array of world coordinates, in SI units. 00269 * 00270 * stat int[ncoord] 00271 * Status return value status for each coordinate: 00272 * 0: Success. 00273 * 1: Invalid intermediate world coordinate. 00274 * 00275 * Function return value: 00276 * int Status return value: 00277 * 0: Success. 00278 * 1: Null tabprm pointer passed. 00279 * 3: Invalid tabular parameters. 00280 * 4: One or more of the x coordinates were invalid, 00281 * as indicated by the stat vector. 00282 * 00283 * For returns > 1, a detailed error message is set in 00284 * tabprm::err if enabled, see wcserr_enable(). 00285 * 00286 * 00287 * tabs2x() - World-to-pixel transformation 00288 * ---------------------------------------- 00289 * tabs2x() transforms world coordinates to intermediate world coordinates. 00290 * 00291 * Given and returned: 00292 * tab struct tabprm* 00293 * Tabular transformation parameters. 00294 * 00295 * Given: 00296 * ncoord, 00297 * nelem int The number of coordinates, each of vector length 00298 * nelem. 00299 * world const double[ncoord][nelem] 00300 * Array of world coordinates, in SI units. 00301 * 00302 * Returned: 00303 * x double[ncoord][nelem] 00304 * Array of intermediate world coordinates, SI units. 00305 * stat int[ncoord] 00306 * Status return value status for each vector element: 00307 * 0: Success. 00308 * 1: Invalid world coordinate. 00309 * 00310 * Function return value: 00311 * int Status return value: 00312 * 0: Success. 00313 * 1: Null tabprm pointer passed. 00314 * 3: Invalid tabular parameters. 00315 * 5: One or more of the world coordinates were 00316 * invalid, as indicated by the stat vector. 00317 * 00318 * For returns > 1, a detailed error message is set in 00319 * tabprm::err if enabled, see wcserr_enable(). 00320 * 00321 * 00322 * tabprm struct - Tabular transformation parameters 00323 * ------------------------------------------------- 00324 * The tabprm struct contains information required to transform tabular 00325 * coordinates. It consists of certain members that must be set by the user 00326 * ("given") and others that are set by the WCSLIB routines ("returned"). Some 00327 * of the latter are supplied for informational purposes while others are for 00328 * internal use only. 00329 * 00330 * int flag 00331 * (Given and returned) This flag must be set to zero whenever any of the 00332 * following tabprm structure members are set or changed: 00333 * 00334 * - tabprm::M (q.v., not normally set by the user), 00335 * - tabprm::K (q.v., not normally set by the user), 00336 * - tabprm::map, 00337 * - tabprm::crval, 00338 * - tabprm::index, 00339 * - tabprm::coord. 00340 * 00341 * This signals the initialization routine, tabset(), to recompute the 00342 * returned members of the tabprm struct. tabset() will reset flag to 00343 * indicate that this has been done. 00344 * 00345 * PLEASE NOTE: flag should be set to -1 when tabini() is called for the 00346 * first time for a particular tabprm struct in order to initialize memory 00347 * management. It must ONLY be used on the first initialization otherwise 00348 * memory leaks may result. 00349 * 00350 * int M 00351 * (Given or returned) Number of tabular coordinate axes. 00352 * 00353 * If tabini() is used to initialize the linprm struct (as would normally 00354 * be the case) then it will set M from the value passed to it as a 00355 * function argument. The user should not subsequently modify it. 00356 * 00357 * int *K 00358 * (Given or returned) Pointer to the first element of a vector of length 00359 * tabprm::M whose elements (K_1, K_2,... K_M) record the lengths of the 00360 * axes of the coordinate array and of each indexing vector. 00361 * 00362 * If tabini() is used to initialize the linprm struct (as would normally 00363 * be the case) then it will set K from the array passed to it as a 00364 * function argument. The user should not subsequently modify it. 00365 * 00366 * int *map 00367 * (Given) Pointer to the first element of a vector of length tabprm::M 00368 * that defines the association between axis m in the M-dimensional 00369 * coordinate array (1 <= m <= M) and the indices of the intermediate world 00370 * coordinate and world coordinate arrays, x[] and world[], in the argument 00371 * lists for tabx2s() and tabs2x(). 00372 * 00373 * When x[] and world[] contain the full complement of coordinate elements 00374 * in image-order, as will usually be the case, then map[m-1] == i-1 for 00375 * axis i in the N-dimensional image (1 <= i <= N). In terms of the FITS 00376 * keywords 00377 * 00378 * map[PVi_3a - 1] == i - 1. 00379 * 00380 * However, a different association may result if x[], for example, only 00381 * contains a (relevant) subset of intermediate world coordinate elements. 00382 * For example, if M == 1 for an image with N > 1, it is possible to fill 00383 * x[] with the relevant coordinate element with nelem set to 1. In this 00384 * case map[0] = 0 regardless of the value of i. 00385 * 00386 * double *crval 00387 * (Given) Pointer to the first element of a vector of length tabprm::M 00388 * whose elements contain the index value for the reference pixel for each 00389 * of the tabular coordinate axes. 00390 * 00391 * double **index 00392 * (Given) Pointer to the first element of a vector of length tabprm::M of 00393 * pointers to vectors of lengths (K_1, K_2,... K_M) of 0-relative indexes 00394 * (see tabprm::K). 00395 * 00396 * The address of any or all of these index vectors may be set to zero, 00397 * i.e. 00398 * 00399 = index[m] == 0; 00400 * 00401 * this is interpreted as default indexing, i.e. 00402 * 00403 = index[m][k] = k; 00404 * 00405 * double *coord 00406 * (Given) Pointer to the first element of the tabular coordinate array, 00407 * treated as though it were defined as 00408 * 00409 = double coord[K_M]...[K_2][K_1][M]; 00410 * 00411 * (see tabprm::K) i.e. with the M dimension varying fastest so that the 00412 * M elements of a coordinate vector are stored contiguously in memory. 00413 * 00414 * int nc 00415 * (Returned) Total number of coordinate vectors in the coordinate array 00416 * being the product K_1 * K_2 * ... * K_M (see tabprm::K). 00417 * 00418 * int padding 00419 * (An unused variable inserted for alignment purposes only.) 00420 * 00421 * int *sense 00422 * (Returned) Pointer to the first element of a vector of length tabprm::M 00423 * whose elements indicate whether the corresponding indexing vector is 00424 * monotonic increasing (+1), or decreasing (-1). 00425 * 00426 * int *p0 00427 * (Returned) Pointer to the first element of a vector of length tabprm::M 00428 * of interpolated indices into the coordinate array such that Upsilon_m, 00429 * as defined in Paper III, is equal to (p0[m] + 1) + tabprm::delta[m]. 00430 * 00431 * double *delta 00432 * (Returned) Pointer to the first element of a vector of length tabprm::M 00433 * of interpolated indices into the coordinate array such that Upsilon_m, 00434 * as defined in Paper III, is equal to (tabprm::p0[m] + 1) + delta[m]. 00435 * 00436 * double *extrema 00437 * (Returned) Pointer to the first element of an array that records the 00438 * minimum and maximum value of each element of the coordinate vector in 00439 * each row of the coordinate array, treated as though it were defined as 00440 * 00441 = double extrema[K_M]...[K_2][2][M] 00442 * 00443 * (see tabprm::K). The minimum is recorded in the first element of the 00444 * compressed K_1 dimension, then the maximum. This array is used by the 00445 * inverse table lookup function, tabs2x(), to speed up table searches. 00446 * 00447 * struct wcserr *err 00448 * (Returned) If enabled, when an error status is returned this struct 00449 * contains detailed information about the error, see wcserr_enable(). 00450 * 00451 * int m_flag 00452 * (For internal use only.) 00453 * int m_M 00454 * (For internal use only.) 00455 * int m_N 00456 * (For internal use only.) 00457 * int set_M 00458 * (For internal use only.) 00459 * int m_K 00460 * (For internal use only.) 00461 * int m_map 00462 * (For internal use only.) 00463 * int m_crval 00464 * (For internal use only.) 00465 * int m_index 00466 * (For internal use only.) 00467 * int m_indxs 00468 * (For internal use only.) 00469 * int m_coord 00470 * (For internal use only.) 00471 * 00472 * 00473 * Global variable: const char *tab_errmsg[] - Status return messages 00474 * ------------------------------------------------------------------ 00475 * Error messages to match the status value returned from each function. 00476 * 00477 *===========================================================================*/ 00478 00479 #ifndef WCSLIB_TAB 00480 #define WCSLIB_TAB 00481 00482 #include "wcserr.h" 00483 00484 #ifdef __cplusplus 00485 extern "C" { 00486 #endif 00487 00488 00489 extern const char *tab_errmsg[]; 00490 00491 enum tab_errmsg_enum { 00492 TABERR_SUCCESS = 0, /* Success. */ 00493 TABERR_NULL_POINTER = 1, /* Null tabprm pointer passed. */ 00494 TABERR_MEMORY = 2, /* Memory allocation failed. */ 00495 TABERR_BAD_PARAMS = 3, /* Invalid tabular parameters. */ 00496 TABERR_BAD_X = 4, /* One or more of the x coordinates were 00497 invalid. */ 00498 TABERR_BAD_WORLD = 5 /* One or more of the world coordinates were 00499 invalid. */ 00500 }; 00501 00502 struct tabprm { 00503 /* Initialization flag (see the prologue above). */ 00504 /*------------------------------------------------------------------------*/ 00505 int flag; /* Set to zero to force initialization. */ 00506 00507 /* Parameters to be provided (see the prologue above). */ 00508 /*------------------------------------------------------------------------*/ 00509 int M; /* Number of tabular coordinate axes. */ 00510 int *K; /* Vector of length M whose elements */ 00511 /* (K_1, K_2,... K_M) record the lengths of */ 00512 /* the axes of the coordinate array and of */ 00513 /* each indexing vector. */ 00514 int *map; /* Vector of length M usually such that */ 00515 /* map[m-1] == i-1 for coordinate array */ 00516 /* axis m and image axis i (see above). */ 00517 double *crval; /* Vector of length M containing the index */ 00518 /* value for the reference pixel for each */ 00519 /* of the tabular coordinate axes. */ 00520 double **index; /* Vector of pointers to M indexing vectors */ 00521 /* of lengths (K_1, K_2,... K_M). */ 00522 double *coord; /* (1+M)-dimensional tabular coordinate */ 00523 /* array (see above). */ 00524 00525 /* Information derived from the parameters supplied. */ 00526 /*------------------------------------------------------------------------*/ 00527 int nc; /* Number of coordinate vectors (of length */ 00528 /* M) in the coordinate array. */ 00529 int padding; /* (Dummy inserted for alignment purposes.) */ 00530 int *sense; /* Vector of M flags that indicate whether */ 00531 /* the Mth indexing vector is monotonic */ 00532 /* increasing, or else decreasing. */ 00533 int *p0; /* Vector of M indices. */ 00534 double *delta; /* Vector of M increments. */ 00535 double *extrema; /* (1+M)-dimensional array of coordinate */ 00536 /* extrema. */ 00537 00538 /* Error handling */ 00539 /*------------------------------------------------------------------------*/ 00540 struct wcserr *err; 00541 00542 /* Private - the remainder are for memory management. */ 00543 /*------------------------------------------------------------------------*/ 00544 int m_flag, m_M, m_N; 00545 int set_M; 00546 int *m_K, *m_map; 00547 double *m_crval, **m_index, **m_indxs, *m_coord; 00548 }; 00549 00550 /* Size of the tabprm struct in int units, used by the Fortran wrappers. */ 00551 #define TABLEN (sizeof(struct tabprm)/sizeof(int)) 00552 00553 00554 int tabini(int alloc, int M, const int K[], struct tabprm *tab); 00555 00556 int tabmem(struct tabprm *tab); 00557 00558 int tabcpy(int alloc, const struct tabprm *tabsrc, struct tabprm *tabdst); 00559 00560 int tabfree(struct tabprm *tab); 00561 00562 int tabprt(const struct tabprm *tab); 00563 00564 int tabset(struct tabprm *tab); 00565 00566 int tabx2s(struct tabprm *tab, int ncoord, int nelem, const double x[], 00567 double world[], int stat[]); 00568 00569 int tabs2x(struct tabprm *tab, int ncoord, int nelem, const double world[], 00570 double x[], int stat[]); 00571 00572 00573 /* Deprecated. */ 00574 #define tabini_errmsg tab_errmsg 00575 #define tabcpy_errmsg tab_errmsg 00576 #define tabfree_errmsg tab_errmsg 00577 #define tabprt_errmsg tab_errmsg 00578 #define tabset_errmsg tab_errmsg 00579 #define tabx2s_errmsg tab_errmsg 00580 #define tabs2x_errmsg tab_errmsg 00581 00582 #ifdef __cplusplus 00583 } 00584 #endif 00585 00586 #endif /* WCSLIB_TAB */