WCSLIB  5.7
dis.h
Go to the documentation of this file.
1 /*============================================================================
2 
3  WCSLIB 5.7 - an implementation of the FITS WCS standard.
4  Copyright (C) 1995-2015, Mark Calabretta
5 
6  This file is part of WCSLIB.
7 
8  WCSLIB is free software: you can redistribute it and/or modify it under the
9  terms of the GNU Lesser General Public License as published by the Free
10  Software Foundation, either version 3 of the License, or (at your option)
11  any later version.
12 
13  WCSLIB is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16  more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with WCSLIB. If not, see http://www.gnu.org/licenses.
20 
21  Direct correspondence concerning WCSLIB to mark@calabretta.id.au
22 
23  Author: Mark Calabretta, Australia Telescope National Facility, CSIRO.
24  http://www.atnf.csiro.au/people/Mark.Calabretta
25  $Id: dis.h,v 5.7 2015/06/29 02:44:16 mcalabre Exp $
26 *=============================================================================
27 *
28 * WCSLIB 5.7 - C routines that implement the FITS World Coordinate System
29 * (WCS) standard. Refer to the README file provided with WCSLIB for an
30 * overview of the library.
31 *
32 *
33 * Summary of the dis routines
34 * ---------------------------
35 * Routines in this suite implement extensions to the FITS World Coordinate
36 * System (WCS) standard proposed by
37 *
38 = "Representations of distortions in FITS world coordinate systems",
39 = Calabretta, M.R. et al. (WCS Paper IV, draft dated 2004/04/22),
40 = available from http://www.atnf.csiro.au/people/Mark.Calabretta
41 *
42 * In brief, a distortion function may occupy one of two positions in the WCS
43 * algorithm chain. Prior distortions precede the linear transformation
44 * matrix, whether it be PCi_ja or CDi_ja, and sequent distortions follow it.
45 * Paper IV defines FITS keywords used to specify parameters for predefined
46 * distortion functions. The following are used for prior distortions:
47 *
48 = CPDISja ...(string-valued, identifies the distortion function)
49 = DPja ...(record-valued, parameters)
50 = CPERRja ...(floating-valued, maximum value)
51 *
52 * Their counterparts for sequent distortions are CQDISia, DQia, and CQERRia.
53 * An additional floating-valued keyword, DVERRa, records the maximum value of
54 * the combined distortions.
55 *
56 * DPja and DQia are "record-valued". Syntactically, the keyvalues are
57 * standard FITS strings, but they are to be interpreted in a special way.
58 * The general form is
59 *
60 = DPja = '<field-specifier>: <float>'
61 *
62 * where the field-specifier consists of a sequence of fields separated by
63 * periods, and the ': ' between the field-specifier and the floating-point
64 * value is part of the record syntax. For example:
65 *
66 = DP1 = 'AXIS.1: 1'
67 *
68 * Certain field-specifiers are defined for all distortion functions, while
69 * others are defined only for particular distortions. Refer to Paper IV for
70 * further details. wcspih() parses all distortion keywords and loads them
71 * into a disprm struct for analysis by disset() which knows (or possibly does
72 * not know) how to interpret them. Of the Paper IV distortion functions, only
73 * the general Polynomial distortion is currently implemented here.
74 *
75 * The TPV "projection":
76 * ---------------------
77 * The distortion function component of the TPV celestial "projection" is also
78 * supported. The TPV projection, originally proposed in a draft of WCS Paper
79 * II, consists of a TAN projection with sequent polynomial distortion, the
80 * coefficients of which are encoded in PVi_ma keyrecords. Full details may be
81 * found at the registry of FITS conventions:
82 *
83 = http://fits.gsfc.nasa.gov/registry/tpvwcs/tpv.html
84 *
85 * Internally, wcsset() changes TPV to a TAN projection, translates the PVi_ma
86 * keywords to DQia and loads them into a disprm struct. These DQia keyrecords
87 * have the form
88 *
89 = DQia = 'TPV.m: <value>'
90 *
91 * where i, a, m, and the value for each DQia match each PVi_ma. Consequently,
92 * WCSLIB would handle a FITS header containing these keywords, along with
93 * CQDISja = 'TPV' and a few other required keywords.
94 *
95 * Simple Imaging Polynomial (SIP):
96 * --------------------------------
97 * These routines also support the Simple Imaging Polynomial (SIP), which arose
98 * as an application of an early draft of Paper IV. It is described in detail
99 * in
100 *
101 = http://fits.gsfc.nasa.gov/registry/sip.html
102 *
103 * SIP, which is defined only as a prior distortion for 2D celestial images,
104 * has the interesting feature that it records an approximation to the inverse
105 * polynomial distortion function. This is used by disx2p() to provide an
106 * initial estimate for its more precise iterative inversion. The
107 * special-purpose keywords used by SIP are parsed and translated by wcspih()
108 * as follows:
109 *
110 = A_p_q = <value> -> DP1 = 'SIP.FWD.p_q: <value>'
111 = AP_p_q = <value> -> DP1 = 'SIP.REV.p_q: <value>'
112 = B_p_q = <value> -> DP2 = 'SIP.FWD.p_q: <value>'
113 = BP_p_q = <value> -> DP2 = 'SIP.REV.p_q: <value>'
114 = A_DMAX = <value> -> DPERR1 = <value>
115 = B_DMAX = <value> -> DPERR2 = <value>
116 *
117 * SIP's A_ORDER and B_ORDER keywords are not used. WCSLIB would recognise a
118 * FITS header containing the above keywords, along with CPDISja = 'SIP' and a
119 * few other required keywords.
120 *
121 * Template Polynomial Distortion (TPD):
122 * -------------------------------------
123 * The "Template Polynomial Distortion" (TPD) is a superset of the TPV and SIP
124 * polynomial distortions that also supports 1D usage and inversions. Like TPV
125 * and SIP, the form of the polynomial is fixed (the "template") and only the
126 * coefficients for the required terms are set non-zero. TPD generalizes TPV
127 * in going to 9th degree, and SIP by accomodating TPV's linear and radial
128 * terms. Within WCSLIB, both TPV and SIP are implemented as special cases of
129 * TPD. Indeed, TPD was developed precisely for that purpose. Moreover, the
130 * general Polynomial distortion is translated and implemented internally as
131 * TPD whenever possible.
132 *
133 * However, WCSLIB also recognizes 'TPD' as a distortion function in its own
134 * right (i.e. a recognized value of CPDISja or CQDISia), for use as both prior
135 * and sequent distortions. Its DPja and DQia keyrecords have the form
136 *
137 = DPja = 'TPD.FWD.m: <value>'
138 = DPja = 'TPD.REV.m: <value>'
139 *
140 * for the forward and reverse distortion functions.
141 *
142 * In typical applications, TPD is considerably faster than the general
143 * Polynomial distortion, though nowhere near as powerful. As TPD has a finite
144 * and not too large number of possible terms (60), the coefficients for each
145 * can be stored (by disset()) in a fixed location in the disprm::dparm[]
146 * array. A large part of the speedup then arises from evaluating the
147 * polynomial using Horner's scheme.
148 *
149 * Separate implementations for polynomials of each degree, and conditionals
150 * for 1D polynomials and 2D polynomials with and without the radial variable,
151 * ensure that unused terms mostly do not impose a significant computational
152 * overhead.
153 *
154 * The TPD terms are as follows
155 *
156 = 0: 1 4: xx 12: xxxx 24: xxxxxx 40: xxxxxxxx
157 = 5: xy 13: xxxy 25: xxxxxy 41: xxxxxxxy
158 = 1: x 6: yy 14: xxyy 26: xxxxyy 42: xxxxxxyy
159 = 2: y 15: xyyy 27: xxxyyy 43: xxxxxyyy
160 = 3: r 7: xxx 16: yyyy 28: xxyyyy 44: xxxxyyyy
161 = 8: xxy 29: xyyyyy 45: xxxyyyyy
162 = 9: xyy 17: xxxxx 30: yyyyyy 46: xxyyyyyy
163 = 10: yyy 18: xxxxy 47: xyyyyyyy
164 = 11: rrr 19: xxxyy 31: xxxxxxx 48: yyyyyyyy
165 = 20: xxyyy 32: xxxxxxy
166 = 21: xyyyy 33: xxxxxyy 49: xxxxxxxxx
167 = 22: yyyyy 34: xxxxyyy 50: xxxxxxxxy
168 = 23: rrrrr 35: xxxyyyy 51: xxxxxxxyy
169 = 36: xxyyyyy 52: xxxxxxyyy
170 = 37: xyyyyyy 53: xxxxxyyyy
171 = 38: yyyyyyy 54: xxxxyyyyy
172 = 39: rrrrrrr 55: xxxyyyyyy
173 = 56: xxyyyyyyy
174 = 57: xyyyyyyyy
175 = 58: yyyyyyyyy
176 = 59: rrrrrrrrr
177 *
178 * where r = sqrt(xx + yy). Note that even powers of r are excluded since they
179 * can be accomodated by powers of (xx + yy).
180 *
181 * TPV uses all terms up to 39. The m in its PVi_ma keywords translates
182 * directly to the TPD coefficient number.
183 *
184 * SIP uses all terms except for 0, 3, 11, 23, 39, and 59, with terms 1 and 2
185 * only used for the inverse. Its A_p_q, etc. keywords must be translated
186 * using a map.
187 *
188 * Summary of the dis routines:
189 * ----------------------------
190 * These routines apply the distortion functions defined by the extension to
191 * the FITS WCS standard proposed in Paper IV. They are based on the disprm
192 * struct which contains all information needed for the computations. The
193 * struct contains some members that must be set by the user, and others that
194 * are maintained by these routines, somewhat like a C++ class but with no
195 * encapsulation.
196 *
197 * disndp(), dpfill(), disini(), discpy(), and disfree() are provided to manage
198 * the disprm struct, and another, disprt(), prints its contents.
199 *
200 * disperr() prints the error message(s) (if any) stored in a disprm struct.
201 *
202 * A setup routine, disset(), computes intermediate values in the disprm struct
203 * from parameters in it that were supplied by the user. The struct always
204 * needs to be set up by disset(), though disset() need not be called
205 * explicitly - refer to the explanation of disprm::flag.
206 *
207 * disp2x() and disx2p() implement the WCS distortion functions, disp2x() using
208 * separate functions, such as dispoly() and tpd7(), to do the computation.
209 *
210 * An auxiliary routine, diswarp(), computes various measures of the distortion
211 * over a specified range of coordinates.
212 *
213 * PLEASE NOTE: Distortions are not yet handled by wcsbth(), or wcscompare().
214 *
215 *
216 * disndp() - Memory allocation for DPja and DQia
217 * ----------------------------------------------
218 * disndp() changes the value of NDPMAX (default 256). This global variable
219 * controls the number of dpkey structs, for holding DPja or DQia keyvalues,
220 * that disini() should allocate space for.
221 *
222 * PLEASE NOTE: This function is not thread-safe.
223 *
224 * Given:
225 * n int Value of NDPMAX; ignored if < 0.
226 *
227 * Function return value:
228 * int Current value of NDPMAX.
229 *
230 *
231 * dpfill() - Fill the contents of a dpkey struct
232 * ----------------------------------------------
233 * dpfill() is a utility routine to aid in filling the contents of the dpkey
234 * struct. No checks are done on the validity of the inputs.
235 *
236 * WCS Paper IV specifies the syntax of a record-valued keyword as
237 *
238 = keyword = '<field-specifier>: <float>'
239 *
240 * However, some DPja and DQia record values, such as those of DPja.NAXES and
241 * DPja.AXIS.j, are intrinsically integer-valued. While FITS header parsers
242 * are not expected to know in advance which of DPja and DQia are integral and
243 * which are floating point, if the record's value parses as an integer (i.e.
244 * without decimal point or exponent), then preferably enter it into the dpkey
245 * struct as an integer. Either way, it doesn't matter as disset() accepts
246 * either data type for all record values.
247 *
248 * Given and returned:
249 * dp struct dpkey*
250 * Store for DPja and DQia keyvalues.
251 *
252 * Given:
253 * keyword const char *
254 * field const char *
255 * These arguments are concatenated with an intervening
256 * "." to construct the full record field name, i.e.
257 * including the keyword name, DPja or DQia (but
258 * excluding the colon delimiter which is NOT part of the
259 * name). Either may be given as a NULL pointer. Set
260 * both NULL to omit setting this component of the
261 * struct.
262 *
263 * j int Axis number (1-relative), i.e. the j in DPja or
264 * i in DQia. Can be given as 0, in which case the axis
265 * number will be obtained from the keyword component of
266 * the field name which must either have been given or
267 * preset.
268 *
269 * type int Data type of the record's value
270 * 0: Integer,
271 * 1: Floating point.
272 *
273 * i int For type == 0, the integer value of the record.
274 *
275 * f double For type == 1, the floating point value of the record.
276 *
277 * Function return value:
278 * int Status return value:
279 * 0: Success.
280 *
281 *
282 * disini() - Default constructor for the disprm struct
283 * ----------------------------------------------------
284 * disini() allocates memory for arrays in a disprm struct and sets all members
285 * of the struct to default values. Memory is allocated for up to NDPMAX DPja
286 * or DQia keywords per WCS representation. This may be changed via disndp()
287 * before disini() is called.
288 *
289 * PLEASE NOTE: every disprm struct must be initialized by disini(), possibly
290 * repeatedly. On the first invokation, and only the first invokation,
291 * disprm::flag must be set to -1 to initialize memory management, regardless
292 * of whether disini() will actually be used to allocate memory.
293 *
294 * Given:
295 * alloc int If true, allocate memory unconditionally for arrays in
296 * the disprm struct.
297 *
298 * If false, it is assumed that pointers to these arrays
299 * have been set by the user except if they are null
300 * pointers in which case memory will be allocated for
301 * them regardless. (In other words, setting alloc true
302 * saves having to initalize these pointers to zero.)
303 *
304 * naxis int The number of world coordinate axes, used to determine
305 * array sizes.
306 *
307 * Given and returned:
308 * dis struct disprm*
309 * Distortion function parameters. Note that, in order
310 * to initialize memory management disprm::flag must be
311 * set to -1 when dis is initialized for the first time
312 * (memory leaks may result if it had already been
313 * initialized).
314 *
315 * Function return value:
316 * int Status return value:
317 * 0: Success.
318 * 1: Null disprm pointer passed.
319 * 2: Memory allocation failed.
320 *
321 * For returns > 1, a detailed error message is set in
322 * disprm::err if enabled, see wcserr_enable().
323 *
324 *
325 * discpy() - Copy routine for the disprm struct
326 * ---------------------------------------------
327 * discpy() does a deep copy of one disprm struct to another, using disini() to
328 * allocate memory unconditionally for its arrays if required. Only the
329 * "information to be provided" part of the struct is copied; a call to
330 * disset() is required to initialize the remainder.
331 *
332 * Given:
333 * alloc int If true, allocate memory unconditionally for arrays in
334 * the destination. Otherwise, it is assumed that
335 * pointers to these arrays have been set by the user
336 * except if they are null pointers in which case memory
337 * will be allocated for them regardless.
338 *
339 * dissrc const struct disprm*
340 * Struct to copy from.
341 *
342 * Given and returned:
343 * disdst struct disprm*
344 * Struct to copy to. disprm::flag should be set to -1
345 * if disdst was not previously initialized (memory leaks
346 * may result if it was previously initialized).
347 *
348 * Function return value:
349 * int Status return value:
350 * 0: Success.
351 * 1: Null disprm pointer passed.
352 * 2: Memory allocation failed.
353 *
354 * For returns > 1, a detailed error message is set in
355 * disprm::err if enabled, see wcserr_enable().
356 *
357 *
358 * disfree() - Destructor for the disprm struct
359 * --------------------------------------------
360 * disfree() frees memory allocated for the disprm arrays by disini().
361 * disini() keeps a record of the memory it allocates and disfree() will only
362 * attempt to free this.
363 *
364 * PLEASE NOTE: disfree() must not be invoked on a disprm struct that was not
365 * initialized by disini().
366 *
367 * Given:
368 * dis struct disprm*
369 * Distortion function parameters.
370 *
371 * Function return value:
372 * int Status return value:
373 * 0: Success.
374 * 1: Null disprm pointer passed.
375 *
376 *
377 * disprt() - Print routine for the disprm struct
378 * ----------------------------------------------
379 * disprt() prints the contents of a disprm struct using wcsprintf(). Mainly
380 * intended for diagnostic purposes.
381 *
382 * Given:
383 * dis const struct disprm*
384 * Distortion function parameters.
385 *
386 * Function return value:
387 * int Status return value:
388 * 0: Success.
389 * 1: Null disprm pointer passed.
390 *
391 *
392 * disperr() - Print error messages from a disprm struct
393 * -----------------------------------------------------
394 * disperr() prints the error message(s) (if any) stored in a disprm struct.
395 * If there are no errors then nothing is printed. It uses wcserr_prt(), q.v.
396 *
397 * Given:
398 * dis const struct disprm*
399 * Distortion function parameters.
400 *
401 * prefix const char *
402 * If non-NULL, each output line will be prefixed with
403 * this string.
404 *
405 * Function return value:
406 * int Status return value:
407 * 0: Success.
408 * 1: Null disprm pointer passed.
409 *
410 *
411 * disset() - Setup routine for the disprm struct
412 * ----------------------------------------------
413 * disset(), sets up the disprm struct according to information supplied within
414 * it - refer to the explanation of disprm::flag.
415 *
416 * Note that this routine need not be called directly; it will be invoked by
417 * disp2x() and disx2p() if the disprm::flag is anything other than a
418 * predefined magic value.
419 *
420 * Given and returned:
421 * dis struct disprm*
422 * Distortion function parameters.
423 *
424 * Function return value:
425 * int Status return value:
426 * 0: Success.
427 * 1: Null disprm pointer passed.
428 * 2: Memory allocation failed.
429 * 3: Invalid parameter.
430 *
431 * For returns > 1, a detailed error message is set in
432 * disprm::err if enabled, see wcserr_enable().
433 *
434 *
435 * disp2x() - Apply distortion function
436 * ------------------------------------
437 * disp2x() applies the distortion functions. By definition, the distortion
438 * is in the pixel-to-world direction.
439 *
440 * Depending on the point in the algorithm chain at which it is invoked,
441 * disp2x() may transform pixel coordinates to corrected pixel coordinates, or
442 * intermediate pixel coordinates to corrected intermediate pixel coordinates,
443 * or image coordinates to corrected image coordinates.
444 *
445 *
446 * Given and returned:
447 * dis struct disprm*
448 * Distortion function parameters.
449 *
450 * Given:
451 * rawcrd const double[naxis]
452 * Array of coordinates.
453 *
454 * Returned:
455 * discrd double[naxis]
456 * Array of coordinates to which the distortion functions
457 * have been applied.
458 *
459 * Function return value:
460 * int Status return value:
461 * 0: Success.
462 * 1: Null disprm pointer passed.
463 * 2: Memory allocation failed.
464 * 3: Invalid parameter.
465 * 4: Distort error.
466 *
467 * For returns > 1, a detailed error message is set in
468 * disprm::err if enabled, see wcserr_enable().
469 *
470 *
471 * disx2p() - Apply de-distortion function
472 * ---------------------------------------
473 * disx2p() applies the inverse of the distortion functions. By definition,
474 * the de-distortion is in the world-to-pixel direction.
475 *
476 * Depending on the point in the algorithm chain at which it is invoked,
477 * disx2p() may transform corrected pixel coordinates to pixel coordinates, or
478 * corrected intermediate pixel coordinates to intermediate pixel coordinates,
479 * or corrected image coordinates to image coordinates.
480 *
481 * disx2p() iteratively solves for the inverse using disp2x(). It assumes
482 * that the distortion is small and the functions are well-behaved, being
483 * continuous and with continuous derivatives. Also that, to first order
484 * in the neighbourhood of the solution, discrd[j] ~= a + b*rawcrd[j], i.e.
485 * independent of rawcrd[i], where i != j. This is effectively equivalent to
486 * assuming that the distortion functions are separable to first order.
487 * Furthermore, a is assumed to be small, and b close to unity.
488 *
489 * If disprm::disx2p() is defined, then disx2p() uses it to provide an initial
490 * estimate for its more precise iterative inversion.
491 *
492 * Given and returned:
493 * dis struct disprm*
494 * Distortion function parameters.
495 *
496 * Given:
497 * discrd const double[naxis]
498 * Array of coordinates.
499 *
500 * Returned:
501 * rawcrd double[naxis]
502 * Array of coordinates to which the inverse distortion
503 * functions have been applied.
504 *
505 * Function return value:
506 * int Status return value:
507 * 0: Success.
508 * 1: Null disprm pointer passed.
509 * 2: Memory allocation failed.
510 * 3: Invalid parameter.
511 * 5: De-distort error.
512 *
513 * For returns > 1, a detailed error message is set in
514 * disprm::err if enabled, see wcserr_enable().
515 *
516 *
517 * diswarp() - Compute measures of distortion
518 * ------------------------------------------
519 * diswarp() computes various measures of the distortion over a specified range
520 * of coordinates.
521 *
522 * For prior distortions, the measures may be interpreted simply as an offset
523 * in pixel coordinates. For sequent distortions, the interpretation depends
524 * on the nature of the linear transformation matrix (PCi_ja or CDi_ja). If
525 * the latter introduces a scaling, then the measures will also be scaled.
526 * Note also that the image domain, which is rectangular in pixel coordinates,
527 * may be rotated, skewed, and/or stretched in intermediate pixel coordinates,
528 * and in general cannot be defined using pixblc[] and pixtrc[].
529 *
530 * PLEASE NOTE: the measures of total distortion may be essentially meaningless
531 * if there are multiple sequent distortions with different scaling.
532 *
533 * See also linwarp().
534 *
535 * Given and returned:
536 * dis struct disprm*
537 * Distortion function parameters.
538 *
539 * Given:
540 * pixblc const double[naxis]
541 * Start of the range of pixel coordinates (for prior
542 * distortions), or intermediate pixel coordinates (for
543 * sequent distortions). May be specified as a NULL
544 * pointer which is interpreted as (1,1,...).
545 *
546 * pixtrc const double[naxis]
547 * End of the range of pixel coordinates (prior) or
548 * intermediate pixel coordinates (sequent).
549 *
550 * pixsamp const double[naxis]
551 * If positive or zero, the increment on the particular
552 * axis, starting at pixblc[]. Zero is interpreted as a
553 * unit increment. pixsamp may also be specified as a
554 * NULL pointer which is interpreted as all zeroes, i.e.
555 * unit increments on all axes.
556 *
557 * If negative, the grid size on the particular axis (the
558 * absolute value being rounded to the nearest integer).
559 * For example, if pixsamp is (-128.0,-128.0,...) then
560 * each axis will be sampled at 128 points between
561 * pixblc[] and pixtrc[] inclusive. Use caution when
562 * using this option on non-square images.
563 *
564 * Returned:
565 * nsamp int* The number of pixel coordinates sampled.
566 *
567 * Can be specified as a NULL pointer if not required.
568 *
569 * maxdis double[naxis]
570 * For each individual distortion function, the
571 * maximum absolute value of the distortion.
572 *
573 * Can be specified as a NULL pointer if not required.
574 *
575 * maxtot double* For the combination of all distortion functions, the
576 * maximum absolute value of the distortion.
577 *
578 * Can be specified as a NULL pointer if not required.
579 *
580 * avgdis double[naxis]
581 * For each individual distortion function, the
582 * mean value of the distortion.
583 *
584 * Can be specified as a NULL pointer if not required.
585 *
586 * avgtot double* For the combination of all distortion functions, the
587 * mean value of the distortion.
588 *
589 * Can be specified as a NULL pointer if not required.
590 *
591 * rmsdis double[naxis]
592 * For each individual distortion function, the
593 * root mean square deviation of the distortion.
594 *
595 * Can be specified as a NULL pointer if not required.
596 *
597 * rmstot double* For the combination of all distortion functions, the
598 * root mean square deviation of the distortion.
599 *
600 * Can be specified as a NULL pointer if not required.
601 *
602 * Function return value:
603 * int Status return value:
604 * 0: Success.
605 * 1: Null disprm pointer passed.
606 * 2: Memory allocation failed.
607 * 3: Invalid parameter.
608 * 4: Distort error.
609 *
610 *
611 * disprm struct - Distortion parameters
612 * -------------------------------------
613 * The disprm struct contains all of the information required to apply a set of
614 * distortion functions. It consists of certain members that must be set by
615 * the user ("given") and others that are set by the WCSLIB routines
616 * ("returned"). While the addresses of the arrays themselves may be set by
617 * disini() if it (optionally) allocates memory, their contents must be set by
618 * the user.
619 *
620 * int flag
621 * (Given and returned) This flag must be set to zero whenever any of the
622 * following members of the disprm struct are set or modified:
623 *
624 * - disprm::naxis,
625 * - disprm::dtype,
626 * - disprm::ndp,
627 * - disprm::dp.
628 *
629 * This signals the initialization routine, disset(), to recompute the
630 * returned members of the disprm struct. disset() will reset flag to
631 * indicate that this has been done.
632 *
633 * PLEASE NOTE: flag must be set to -1 when disini() is called for the
634 * first time for a particular disprm struct in order to initialize memory
635 * management. It must ONLY be used on the first initialization otherwise
636 * memory leaks may result.
637 *
638 * int naxis
639 * (Given or returned) Number of pixel and world coordinate elements.
640 *
641 * If disini() is used to initialize the disprm struct (as would normally
642 * be the case) then it will set naxis from the value passed to it as a
643 * function argument. The user should not subsequently modify it.
644 *
645 * char (*dtype)[72]
646 * (Given) Pointer to the first element of an array of char[72] containing
647 * the name of the distortion function for each axis.
648 *
649 * int ndp
650 * (Given) The number of entries in the disprm::dp[] array.
651 *
652 * int ndpmax
653 * (Given) The length of the disprm::dp[] array.
654 *
655 * ndpmax will be set by disini() if it allocates memory for disprm::dp[],
656 * otherwise it must be set by the user. See also disndp().
657 *
658 * struct dpkey dp
659 * (Given) Address of the first element of an array of length ndpmax of
660 * dpkey structs.
661 *
662 * As a FITS header parser encounters each DPja or DQia keyword it should
663 * load it into a dpkey struct in the array and increment ndp. However,
664 * note that a single disprm struct must hold only DPja or DQia keyvalues,
665 * not both. disset() interprets them as required by the particular
666 * distortion function.
667 *
668 * double *maxdis
669 * (Given) Pointer to the first element of an array of double specifying
670 * the maximum absolute value of the distortion for each axis computed over
671 * the whole image.
672 *
673 * It is not necessary to reset the disprm struct (via disset()) when
674 * disprm::maxdis is changed.
675 *
676 * double totdis
677 * (Given) The maximum absolute value of the combination of all distortion
678 * functions specified as an offset in pixel coordinates computed over the
679 * whole image.
680 *
681 * It is not necessary to reset the disprm struct (via disset()) when
682 * disprm::totdis is changed.
683 *
684 * int **axmap
685 * (Returned) Pointer to the first element of an array of int* containing
686 * pointers to the first elements of the axis mapping arrays for each axis.
687 *
688 * An axis mapping associates the independent variables of a distortion
689 * function with the 0-relative image axis number. For example, consider
690 * an image with a spectrum on the first axis (axis 0), followed by RA
691 * (axis 1), Dec (axis2), and time (axis 3) axes. For a distortion in
692 * (RA,Dec) and no distortion on the spectral or time axes, the axis
693 * mapping arrays, axmap[j][], would be
694 *
695 = j=0: [-1, -1, -1, -1] ...no distortion on spectral axis,
696 = 1: [ 1, 2, -1, -1] ...RA distortion depends on RA and Dec,
697 = 2: [ 2, 1, -1, -1] ...Dec distortion depends on Dec and RA,
698 = 3: [-1, -1, -1, -1] ...no distortion on time axis,
699 *
700 * where -1 indicates that there is no corresponding independent
701 * variable.
702 *
703 * int *Nhat
704 * (Returned) Pointer to the first element of an array of int* containing
705 * the number of coordinate axes that form the independent variables of the
706 * distortion function.
707 *
708 * double **offset
709 * (Returned) Pointer to the first element of an array of double*
710 * containing an offset used to renormalize the independent variables of
711 * the distortion function for each axis.
712 *
713 * The offsets are subtracted from the independent variables before
714 * scaling.
715 *
716 * double **scale
717 * (Returned) Pointer to the first element of an array of double*
718 * containing a scale used to renormalize the independent variables of the
719 * distortion function for each axis.
720 *
721 * The scale is applied to the independent variables after the offsets are
722 * subtracted.
723 *
724 * int **iparm
725 * (Returned) Pointer to the first element of an array of int*
726 * containing pointers to the first elements of the arrays of integer
727 * distortion parameters for each axis.
728 *
729 * double **dparm
730 * (Returned) Pointer to the first element of an array of double*
731 * containing pointers to the first elements of the arrays of floating
732 * point distortion parameters for each axis.
733 *
734 * int i_naxis
735 * (Returned) Dimension of the internal arrays (normally equal to naxis).
736 *
737 * int ndis
738 * (Returned) The number of distortion functions.
739 *
740 * struct wcserr *err
741 * (Returned) If enabled, when an error status is returned, this struct
742 * contains detailed information about the error, see wcserr_enable().
743 *
744 * int (**disp2x)(DISP2X_ARGS)
745 * (For internal use only.)
746 * int (**disx2p)(DISX2P_ARGS)
747 * (For internal use only.)
748 * double *tmpmem
749 * (For internal use only.)
750 * int m_flag
751 * (For internal use only.)
752 * int m_naxis
753 * (For internal use only.)
754 * char (*m_dtype)[72]
755 * (For internal use only.)
756 * double **m_dp
757 * (For internal use only.)
758 * double *m_maxdis
759 * (For internal use only.)
760 *
761 *
762 * dpkey struct - Store for DPja and DQia keyvalues
763 * ------------------------------------------------
764 * The dpkey struct is used to pass the parsed contents of DPja or DQia
765 * keyrecords to disset() via the disprm struct. A disprm struct must hold
766 * only DPja or DQia keyvalues, not both.
767 *
768 * All members of this struct are to be set by the user.
769 *
770 * char field[72]
771 * (Given) The full field name of the record, including the keyword name.
772 * Note that the colon delimiter separating the field name and the value in
773 * record-valued keyvalues is not part of the field name. For example, in
774 * the following:
775 *
776 = DP3A = 'AXIS.1: 2'
777 *
778 * the full record field name is "DP3A.AXIS.1", and the record's value
779 * is 2.
780 *
781 * int j
782 * (Given) Axis number (1-relative), i.e. the j in DPja or i in DQia.
783 *
784 * int type
785 * (Given) The data type of the record's value
786 * - 0: Integer (stored as an int),
787 * - 1: Floating point (stored as a double).
788 *
789 * union value
790 * (Given) A union comprised of
791 * - dpkey::i,
792 * - dpkey::f,
793 *
794 * the record's value.
795 *
796 *
797 * Global variable: const char *dis_errmsg[] - Status return messages
798 * ------------------------------------------------------------------
799 * Error messages to match the status value returned from each function.
800 *
801 *===========================================================================*/
802 
803 #ifndef WCSLIB_DIS
804 #define WCSLIB_DIS
805 
806 #ifdef __cplusplus
807 extern "C" {
808 #endif
809 
810 
811 extern const char *dis_errmsg[];
812 
814  DISERR_SUCCESS = 0, /* Success. */
815  DISERR_NULL_POINTER = 1, /* Null disprm pointer passed. */
816  DISERR_MEMORY = 2, /* Memory allocation failed. */
817  DISERR_BAD_PARAM = 3, /* Invalid parameter value. */
818  DISERR_DISTORT = 4, /* Distortion error. */
819  DISERR_DEDISTORT = 5 /* De-distortion error. */
820 };
821 
822 /* For use in declaring distortion function prototypes (= DISX2P_ARGS). */
823 #define DISP2X_ARGS int inverse, const int iparm[], const double dparm[], \
824 int ncrd, const double rawcrd[], double *discrd
825 
826 /* For use in declaring de-distortion function prototypes (= DISP2X_ARGS). */
827 #define DISX2P_ARGS int inverse, const int iparm[], const double dparm[], \
828 int ncrd, const double discrd[], double *rawcrd
829 
830 
831 /* Struct used for storing DPja and DQia keyvalues. */
832 struct dpkey {
833  char field[72]; /* Full record field name (no colon). */
834  int j; /* Axis number, as in DPja (1-relative). */
835  int type; /* Data type of value. */
836  union {
837  int i; /* Integer record value. */
838  double f; /* Floating point record value. */
839  } value; /* Record value. */
840 };
841 
842 /* Size of the dpkey struct in int units, used by the Fortran wrappers. */
843 #define DPLEN (sizeof(struct dpkey)/sizeof(int))
844 
845 
846 struct disprm {
847  /* Initialization flag (see the prologue above). */
848  /*------------------------------------------------------------------------*/
849  int flag; /* Set to zero to force initialization. */
850 
851  /* Parameters to be provided (see the prologue above). */
852  /*------------------------------------------------------------------------*/
853  int naxis; /* The number of pixel coordinate elements, */
854  /* given by NAXIS. */
855  char (*dtype)[72]; /* For each axis, the distortion type. */
856  int ndp; /* Number of DPja or DQia keywords, and the */
857  int ndpmax; /* number for which space was allocated. */
858  struct dpkey *dp; /* DPja or DQia keyvalues (not both). */
859  double *maxdis; /* For each axis, the maximum distortion. */
860  double totdis; /* The maximum combined distortion. */
861 
862  /* Information derived from the parameters supplied. */
863  /*------------------------------------------------------------------------*/
864  int **axmap; /* For each axis, the axis mapping array. */
865  int *Nhat; /* For each axis, the number of coordinate */
866  /* axes that form the independent variables */
867  /* of the distortion function. */
868  double **offset; /* For each axis, renormalization offsets. */
869  double **scale; /* For each axis, renormalization scales. */
870  int **iparm; /* For each axis, the array of integer */
871  /* distortion parameters. */
872  double **dparm; /* For each axis, the array of floating */
873  /* point distortion parameters. */
874  int i_naxis; /* Dimension of the internal arrays. */
875  int ndis; /* The number of distortion functions. */
876 
877  /* Error handling, if enabled. */
878  /*------------------------------------------------------------------------*/
879  struct wcserr *err;
880 
881  /* Private - the remainder are for internal use. */
882  /*------------------------------------------------------------------------*/
883  int (**disp2x)(DISP2X_ARGS); /* For each axis, pointers to the */
884  int (**disx2p)(DISX2P_ARGS); /* distortion function and its inverse. */
885 
886  double *tmpmem;
887 
888  int m_flag, m_naxis; /* The remainder are for memory management. */
889  char (*m_dtype)[72];
890  struct dpkey *m_dp;
891  double *m_maxdis;
892 };
893 
894 /* Size of the disprm struct in int units, used by the Fortran wrappers. */
895 #define DISLEN (sizeof(struct disprm)/sizeof(int))
896 
897 
898 int disndp(int n);
899 
900 int dpfill(struct dpkey *dp, const char *keyword, const char *field, int j,
901  int type, int i, double f);
902 
903 int disini(int alloc, int naxis, struct disprm *dis);
904 
905 int discpy(int alloc, const struct disprm *dissrc, struct disprm *disdst);
906 
907 int disfree(struct disprm *dis);
908 
909 int disprt(const struct disprm *dis);
910 
911 int disperr(const struct disprm *dis, const char *prefix);
912 
913 int disset(struct disprm *dis);
914 
915 int disp2x(struct disprm *dis, const double rawcrd[], double discrd[]);
916 
917 int disx2p(struct disprm *dis, const double discrd[], double rawcrd[]);
918 
919 int diswarp(struct disprm *dis, const double pixblc[], const double pixtrc[],
920  const double pixsamp[], int *nsamp,
921  double maxdis[], double *maxtot,
922  double avgdis[], double *avgtot,
923  double rmsdis[], double *rmstot);
924 
925 
926 /* Specialist distortion functions (internal use only). */
927 int polyset(int j, struct disprm *dis);
928 int pol2tpd(int j, struct disprm *dis);
929 int dispoly(DISP2X_ARGS);
930 
931 int tpvset(int j, struct disprm *dis);
932 int sipset(int j, struct disprm *dis);
933 
934 int tpdset(int j, struct disprm *dis);
935 int tpd1(DISP2X_ARGS);
936 int tpd2(DISP2X_ARGS);
937 int tpd3(DISP2X_ARGS);
938 int tpd4(DISP2X_ARGS);
939 int tpd5(DISP2X_ARGS);
940 int tpd6(DISP2X_ARGS);
941 int tpd7(DISP2X_ARGS);
942 int tpd8(DISP2X_ARGS);
943 int tpd9(DISP2X_ARGS);
944 
945 #ifdef __cplusplus
946 }
947 #endif
948 
949 #endif /* WCSLIB_DIS */
int type
Definition: dis.h:835
int flag
Definition: dis.h:849
Store for DPja and DQia keyvalues.
Definition: dis.h:832
int discpy(int alloc, const struct disprm *dissrc, struct disprm *disdst)
Copy routine for the disprm struct.
int ** iparm
Definition: dis.h:870
Definition: dis.h:818
const char * dis_errmsg[]
Status return messages.
int disperr(const struct disprm *dis, const char *prefix)
Print error messages from a disprm struct.
int ** axmap
Definition: dis.h:864
int disp2x(struct disprm *dis, const double rawcrd[], double discrd[])
Apply distortion function.
int tpdset(int j, struct disprm *dis)
(Internal use only.)
double ** scale
Definition: dis.h:869
int tpd8(DISP2X_ARGS)
(Internal use only.)
int tpd5(DISP2X_ARGS)
(Internal use only.)
int pol2tpd(int j, struct disprm *dis)
(Internal use only.)
Error message handling.
Definition: wcserr.h:225
int disx2p(struct disprm *dis, const double discrd[], double rawcrd[])
Apply de-distortion function.
Definition: dis.h:817
int disset(struct disprm *dis)
Setup routine for the disprm struct.
int naxis
Definition: dis.h:853
struct dpkey * dp
Definition: dis.h:858
int m_flag
Definition: dis.h:888
Definition: dis.h:819
int ndis
Definition: dis.h:875
Distortion parameters.
Definition: dis.h:846
char field[72]
Definition: dis.h:833
int dispoly(DISP2X_ARGS)
(Internal use only.)
int ndp
Definition: dis.h:856
int tpd1(DISP2X_ARGS)
(Internal use only.)
int tpd9(DISP2X_ARGS)
(Internal use only.)
char(* m_dtype)[72]
Definition: dis.h:889
int j
Definition: dis.h:834
Definition: dis.h:815
int sipset(int j, struct disprm *dis)
(Internal use only.)
int tpd3(DISP2X_ARGS)
(Internal use only.)
int * Nhat
Definition: dis.h:865
int disfree(struct disprm *dis)
Destructor for the disprm struct.
dis_errmsg_enum
Definition: dis.h:813
int m_naxis
Definition: dis.h:888
int dpfill(struct dpkey *dp, const char *keyword, const char *field, int j, int type, int i, double f)
Fill the contents of a dpkey struct.
int disprt(const struct disprm *dis)
Print routine for the disprm struct.
double * tmpmem
Definition: dis.h:886
int tpd7(DISP2X_ARGS)
(Internal use only.)
int i_naxis
Definition: dis.h:874
double * maxdis
Definition: dis.h:859
double totdis
Definition: dis.h:860
int tpd2(DISP2X_ARGS)
(Internal use only.)
int(** disx2p)(DISX2P_ARGS)
Definition: dis.h:884
Definition: dis.h:814
int tpd4(DISP2X_ARGS)
(Internal use only.)
int tpvset(int j, struct disprm *dis)
(Internal use only.)
struct wcserr * err
Definition: dis.h:879
char(* dtype)[72]
Definition: dis.h:855
int disini(int alloc, int naxis, struct disprm *dis)
Default constructor for the disprm struct.
union dpkey::@0 value
double f
Definition: dis.h:838
Definition: dis.h:816
struct dpkey * m_dp
Definition: dis.h:890
double * m_maxdis
Definition: dis.h:891
int diswarp(struct disprm *dis, const double pixblc[], const double pixtrc[], const double pixsamp[], int *nsamp, double maxdis[], double *maxtot, double avgdis[], double *avgtot, double rmsdis[], double *rmstot)
Compute measures of distortion.
double ** offset
Definition: dis.h:868
#define DISX2P_ARGS
Definition: dis.h:827
int(** disp2x)(DISP2X_ARGS)
Definition: dis.h:883
int polyset(int j, struct disprm *dis)
(Internal use only.)
int ndpmax
Definition: dis.h:857
int tpd6(DISP2X_ARGS)
(Internal use only.)
int disndp(int n)
Memory allocation for DPja and DQia.
int i
Definition: dis.h:837
#define DISP2X_ARGS
Definition: dis.h:823
double ** dparm
Definition: dis.h:872