00001 /*============================================================================ 00002 00003 WCSLIB 4.4 - an implementation of the FITS WCS standard. 00004 Copyright (C) 1995-2009, 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.1 2009/09/14 20:25:24 irby Exp $ 00032 *============================================================================= 00033 * 00034 * WCSLIB 4.4 - 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 * K const int[] 00101 * Vector of length M whose elements (K_1, K_2,... K_M) 00102 * record the lengths of the axes of the coordinate array 00103 * and of each indexing vector. M and K[] are used to 00104 * determine the length of the various tabprm arrays and 00105 * therefore the amount of memory to allocate for them. 00106 * Their values are copied into the tabprm struct. 00107 * 00108 * It is permissible to set K (i.e. the address of the 00109 * array) to zero which has the same effect as setting 00110 * each element of K[] to zero. In this case no memory 00111 * will be allocated for the index vectors or coordinate 00112 * array in the tabprm struct. These together with the 00113 * K vector must be set separately before calling 00114 * tabset(). 00115 * 00116 * Given and returned: 00117 * tab struct tabprm* 00118 * Tabular transformation parameters. Note that, in 00119 * order to initialize memory management tabprm::flag 00120 * should be set to -1 when tab is initialized for the 00121 * first time (memory leaks may result if it had already 00122 * been initialized). 00123 * 00124 * Function return value: 00125 * int Status return value: 00126 * 0: Success. 00127 * 1: Null tabprm pointer passed. 00128 * 2: Memory allocation failed. 00129 * 3: Invalid tabular parameters. 00130 * 00131 * 00132 * tabmem() - Acquire tabular memory 00133 * --------------------------------- 00134 * tabmem() takes control of memory allocated by the user for arrays in the 00135 * tabprm struct. 00136 * 00137 * Given and returned: 00138 * tab struct tabprm* 00139 * Tabular transformation parameters. 00140 * 00141 * Function return value: 00142 * int Status return value: 00143 * 0: Success. 00144 * 1: Null tabprm pointer passed. 00145 * 00146 * 00147 * tabcpy() - Copy routine for the tabprm struct 00148 * --------------------------------------------- 00149 * tabcpy() does a deep copy of one tabprm struct to another, using tabini() to 00150 * allocate memory for its arrays if required. Only the "information to be 00151 * provided" part of the struct is copied; a call to tabset() is required to 00152 * set up the remainder. 00153 * 00154 * Given: 00155 * alloc int If true, allocate memory unconditionally for arrays in 00156 * the tabprm struct. 00157 * 00158 * If false, it is assumed that pointers to these arrays 00159 * have been set by the user except if they are null 00160 * pointers in which case memory will be allocated for 00161 * them regardless. (In other words, setting alloc true 00162 * saves having to initalize these pointers to zero.) 00163 * 00164 * tabsrc const struct tabprm* 00165 * Struct to copy from. 00166 * 00167 * Given and returned: 00168 * tabdst struct tabprm* 00169 * Struct to copy to. tabprm::flag should be set to -1 00170 * if tabdst was not previously initialized (memory leaks 00171 * may result if it was previously initialized). 00172 * 00173 * Function return value: 00174 * int Status return value: 00175 * 0: Success. 00176 * 1: Null tabprm pointer passed. 00177 * 2: Memory allocation failed. 00178 * 00179 * 00180 * tabfree() - Destructor for the tabprm struct 00181 * -------------------------------------------- 00182 * tabfree() frees memory allocated for the tabprm arrays by tabini(). 00183 * tabini() records the memory it allocates and tabfree() will only attempt to 00184 * free this. 00185 * 00186 * PLEASE NOTE: tabfree() must not be invoked on a tabprm struct that was not 00187 * initialized by tabini(). 00188 * 00189 * Returned: 00190 * tab struct tabprm* 00191 * Coordinate transformation parameters. 00192 * 00193 * Function return value: 00194 * int Status return value: 00195 * 0: Success. 00196 * 1: Null tabprm pointer passed. 00197 * 00198 * 00199 * tabprt() - Print routine for the tabprm struct 00200 * ---------------------------------------------- 00201 * tabprt() prints the contents of a tabprm struct. 00202 * 00203 * Given: 00204 * tab const struct tabprm* 00205 * Tabular transformation parameters. 00206 * 00207 * Function return value: 00208 * int Status return value: 00209 * 0: Success. 00210 * 1: Null tabprm pointer passed. 00211 * 00212 * 00213 * tabset() - Setup routine for the tabprm struct 00214 * ----------------------------------------------- 00215 * tabset() allocates memory for work arrays in the tabprm struct and sets up 00216 * the struct according to information supplied within it. 00217 * 00218 * Note that this routine need not be called directly; it will be invoked by 00219 * tabx2s() and tabs2x() if tabprm::flag is anything other than a predefined 00220 * magic value. 00221 * 00222 * Given and returned: 00223 * tab struct tabprm* 00224 * Tabular transformation parameters. 00225 * 00226 * Function return value: 00227 * int Status return value: 00228 * 0: Success. 00229 * 1: Null tabprm pointer passed. 00230 * 3: Invalid tabular parameters. 00231 * 00232 * 00233 * tabx2s() - Pixel-to-world transformation 00234 * ---------------------------------------- 00235 * tabx2s() transforms intermediate world coordinates to world coordinates 00236 * using coordinate lookup. 00237 * 00238 * Given and returned: 00239 * tab struct tabprm* 00240 * Tabular transformation parameters. 00241 * 00242 * Given: 00243 * ncoord, 00244 * nelem int The number of coordinates, each of vector length 00245 * nelem. 00246 * x const double[ncoord][nelem] 00247 * Array of intermediate world coordinates, SI units. 00248 * 00249 * Returned: 00250 * world double[ncoord][nelem] 00251 * Array of world coordinates, in SI units. 00252 * stat int[ncoord] 00253 * Status return value status for each coordinate: 00254 * 0: Success. 00255 * 1: Invalid intermediate world coordinate. 00256 * 00257 * Function return value: 00258 * int Status return value: 00259 * 0: Success. 00260 * 1: Null tabprm pointer passed. 00261 * 3: Invalid tabular parameters. 00262 * 4: One or more of the x coordinates were invalid, 00263 * as indicated by the stat vector. 00264 * 00265 * 00266 * tabs2x() - World-to-pixel transformation 00267 * ---------------------------------------- 00268 * tabs2x() transforms world coordinates to intermediate world coordinates. 00269 * 00270 * Given and returned: 00271 * tab struct tabprm* 00272 * Tabular transformation parameters. 00273 * 00274 * Given: 00275 * ncoord, 00276 * nelem int The number of coordinates, each of vector length 00277 * nelem. 00278 * world const double[ncoord][nelem] 00279 * Array of world coordinates, in SI units. 00280 * 00281 * Returned: 00282 * x double[ncoord][nelem] 00283 * Array of intermediate world coordinates, SI units. 00284 * stat int[ncoord] 00285 * Status return value status for each vector element: 00286 * 0: Success. 00287 * 1: Invalid world coordinate. 00288 * 00289 * Function return value: 00290 * int Status return value: 00291 * 0: Success. 00292 * 1: Null tabprm pointer passed. 00293 * 3: Invalid tabular parameters. 00294 * 5: One or more of the world coordinates were 00295 * invalid, as indicated by the stat vector. 00296 * 00297 * 00298 * tabprm struct - Tabular transformation parameters 00299 * ------------------------------------------------- 00300 * The tabprm struct contains information required to transform tabular 00301 * coordinates. It consists of certain members that must be set by the user 00302 * ("given") and others that are set by the WCSLIB routines ("returned"). Some 00303 * of the latter are supplied for informational purposes while others are for 00304 * internal use only. 00305 * 00306 * int flag 00307 * (Given and returned) This flag must be set to zero whenever any of the 00308 * following tabprm structure members are set or changed: 00309 * 00310 * - tabprm::M (q.v., not normally set by the user), 00311 * - tabprm::K (q.v., not normally set by the user), 00312 * - tabprm::map, 00313 * - tabprm::crval, 00314 * - tabprm::index, 00315 * - tabprm::coord. 00316 * 00317 * This signals the initialization routine, tabset(), to recompute the 00318 * returned members of the tabprm struct. tabset() will reset flag to 00319 * indicate that this has been done. 00320 * 00321 * PLEASE NOTE: flag should be set to -1 when tabini() is called for the 00322 * first time for a particular tabprm struct in order to initialize memory 00323 * management. It must ONLY be used on the first initialization otherwise 00324 * memory leaks may result. 00325 * 00326 * int M 00327 * (Given or returned) Number of tabular coordinate axes. 00328 * 00329 * If tabini() is used to initialize the linprm struct (as would normally 00330 * be the case) then it will set M from the value passed to it as a 00331 * function argument. The user should not subsequently modify it. 00332 * 00333 * int *K 00334 * (Given or returned) Pointer to the first element of a vector of length 00335 * tabprm::M whose elements (K_1, K_2,... K_M) record the lengths of the 00336 * axes of the coordinate array and of each indexing vector. 00337 * 00338 * If tabini() is used to initialize the linprm struct (as would normally 00339 * be the case) then it will set K from the array passed to it as a 00340 * function argument. The user should not subsequently modify it. 00341 * 00342 * int *map 00343 * (Given) Pointer to the first element of a vector of length tabprm::M 00344 * that defines the association between axis m in the M-dimensional 00345 * coordinate array (1 <= m <= M) and the indices of the intermediate world 00346 * coordinate and world coordinate arrays, x[] and world[], in the argument 00347 * lists for tabx2s() and tabs2x(). 00348 * 00349 * When x[] and world[] contain the full complement of coordinate elements 00350 * in image-order, as will usually be the case, then map[m-1] == i-1 for 00351 * axis i in the N-dimensional image (1 <= i <= N). In terms of the FITS 00352 * keywords 00353 * 00354 * map[PVi_3a - 1] == i - 1. 00355 * 00356 * However, a different association may result if x[], for example, only 00357 * contains a (relevant) subset of intermediate world coordinate elements. 00358 * For example, if M == 1 for an image with N > 1, it is possible to fill 00359 * x[] with the relevant coordinate element with nelem set to 1. In this 00360 * case map[0] = 0 regardless of the value of i. 00361 * 00362 * double *crval 00363 * (Given) Pointer to the first element of a vector of length tabprm::M 00364 * whose elements contain the index value for the reference pixel for each 00365 * of the tabular coordinate axes. 00366 * 00367 * double **index 00368 * (Given) Pointer to the first element of a vector of length tabprm::M of 00369 * pointers to vectors of lengths (K_1, K_2,... K_M) of 0-relative indexes 00370 * (see tabprm::K). 00371 * 00372 * The address of any or all of these index vectors may be set to zero, 00373 * i.e. 00374 * 00375 = index[m] == 0; 00376 * 00377 * this is interpreted as default indexing, i.e. 00378 * 00379 = index[m][k] = k; 00380 * 00381 * double *coord 00382 * (Given) Pointer to the first element of the tabular coordinate array, 00383 * treated as though it were defined as 00384 * 00385 = double coord[K_M]...[K_2][K_1][M]; 00386 * 00387 * (see tabprm::K) i.e. with the M dimension varying fastest so that the 00388 * M elements of a coordinate vector are stored contiguously in memory. 00389 * 00390 * int nc 00391 * (Returned) Total number of coordinate vectors in the coordinate array 00392 * being the product K_1 * K_2 * ... * K_M (see tabprm::K). 00393 * 00394 * int padding 00395 * (An unused variable inserted for alignment purposes only.) 00396 * 00397 * int *sense 00398 * (Returned) Pointer to the first element of a vector of length tabprm::M 00399 * whose elements indicate whether the corresponding indexing vector is 00400 * monotonic increasing (+1), or decreasing (-1). 00401 * 00402 * double *p0 00403 * (Returned) Pointer to the first element of a vector of length tabprm::M 00404 * of interpolated indices into the coordinate array such that Upsilon_m, 00405 * as defined in Paper III, is equal to p0[m] + tabprm::delta[m]. 00406 * 00407 * double *delta 00408 * (Returned) Pointer to the first element of a vector of length tabprm::M 00409 * of interpolated indices into the coordinate array such that Upsilon_m, 00410 * as defined in Paper III, is equal to tabprm::p0[m] + delta[m]. 00411 * 00412 * double *extrema 00413 * (Returned) Pointer to the first element of an array that records the 00414 * minimum and maximum value of each element of the coordinate vector in 00415 * each row of the coordinate array, treated as though it were defined as 00416 * 00417 = double extrema[K_M]...[K_2][2][M] 00418 * 00419 * (see tabprm::K). The minimum is recorded in the first element of the 00420 * compressed K_1 dimension, then the maximum. This array is used by the 00421 * inverse table lookup function, tabs2x(), to speed up table searches. 00422 * 00423 * int m_flag 00424 * (For internal use only.) 00425 * int m_M 00426 * (For internal use only.) 00427 * int m_N 00428 * (For internal use only.) 00429 * int set_M 00430 * (For internal use only.) 00431 * int m_K 00432 * (For internal use only.) 00433 * int m_map 00434 * (For internal use only.) 00435 * int m_crval 00436 * (For internal use only.) 00437 * int m_index 00438 * (For internal use only.) 00439 * int m_indxs 00440 * (For internal use only.) 00441 * int m_coord 00442 * (For internal use only.) 00443 * 00444 * 00445 * Global variable: const char *tab_errmsg[] - Status return messages 00446 * ------------------------------------------------------------------ 00447 * Error messages to match the status value returned from each function. 00448 * 00449 *===========================================================================*/ 00450 00451 #ifndef WCSLIB_TAB 00452 #define WCSLIB_TAB 00453 00454 #ifdef __cplusplus 00455 extern "C" { 00456 #endif 00457 00458 00459 extern const char *tab_errmsg[]; 00460 00461 00462 struct tabprm { 00463 /* Initialization flag (see the prologue above). */ 00464 /*------------------------------------------------------------------------*/ 00465 int flag; /* Set to zero to force initialization. */ 00466 00467 /* Parameters to be provided (see the prologue above). */ 00468 /*------------------------------------------------------------------------*/ 00469 int M; /* Number of tabular coordinate axes. */ 00470 int *K; /* Vector of length M whose elements */ 00471 /* (K_1, K_2,... K_M) record the lengths of */ 00472 /* the axes of the coordinate array and of */ 00473 /* each indexing vector. */ 00474 int *map; /* Vector of length M usually such that */ 00475 /* map[m-1] == i-1 for coordinate array */ 00476 /* axis m and image axis i (see above). */ 00477 double *crval; /* Vector of length M containing the index */ 00478 /* value for the reference pixel for each */ 00479 /* of the tabular coordinate axes. */ 00480 double **index; /* Vector of pointers to M indexing vectors */ 00481 /* of lengths (K_1, K_2,... K_M). */ 00482 double *coord; /* (1+M)-dimensional tabular coordinate */ 00483 /* array (see above). */ 00484 00485 /* Information derived from the parameters supplied. */ 00486 /*------------------------------------------------------------------------*/ 00487 int nc; /* Number of coordinate vectors (of length */ 00488 /* M) in the coordinate array. */ 00489 int padding; /* (Dummy inserted for alignment purposes.) */ 00490 int *sense; /* Vector of M flags that indicate whether */ 00491 /* the Mth indexing vector is monotonic */ 00492 /* increasing, or else decreasing. */ 00493 int *p0; /* Vector of M indices. */ 00494 double *delta; /* Vector of M increments. */ 00495 double *extrema; /* (1+M)-dimensional array of coordinate */ 00496 /* extrema. */ 00497 00498 int m_flag, m_M, m_N; /* The remainder are for memory management. */ 00499 int set_M; 00500 int *m_K, *m_map; 00501 double *m_crval, **m_index, **m_indxs, *m_coord; 00502 }; 00503 00504 /* Size of the tabprm struct in int units, used by the Fortran wrappers. */ 00505 #define TABLEN (sizeof(struct tabprm)/sizeof(int)) 00506 00507 00508 int tabini(int alloc, int M, const int K[], struct tabprm *tab); 00509 00510 int tabmem(struct tabprm *tab); 00511 00512 int tabcpy(int alloc, const struct tabprm *tabsrc, struct tabprm *tabdst); 00513 00514 int tabfree(struct tabprm *tab); 00515 00516 int tabprt(const struct tabprm *tab); 00517 00518 int tabset(struct tabprm *tab); 00519 00520 int tabx2s(struct tabprm *tab, int ncoord, int nelem, const double x[], 00521 double world[], int stat[]); 00522 00523 int tabs2x(struct tabprm *tab, int ncoord, int nelem, const double world[], 00524 double x[], int stat[]); 00525 00526 00527 /* Deprecated. */ 00528 #define tabini_errmsg tab_errmsg 00529 #define tabcpy_errmsg tab_errmsg 00530 #define tabfree_errmsg tab_errmsg 00531 #define tabprt_errmsg tab_errmsg 00532 #define tabset_errmsg tab_errmsg 00533 #define tabx2s_errmsg tab_errmsg 00534 #define tabs2x_errmsg tab_errmsg 00535 00536 #ifdef __cplusplus 00537 } 00538 #endif 00539 00540 #endif /* WCSLIB_TAB */