00001 /*============================================================================ 00002 00003 WCSLIB 4.14 - 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 Direct correspondence concerning WCSLIB to mark@calabretta.id.au 00022 00023 Author: Mark Calabretta, Australia Telescope National Facility, CSIRO. 00024 http://www.atnf.csiro.au/people/Mark.Calabretta 00025 $Id: wcsunits.h,v 4.14 2012/07/13 10:02:27 cal103 Exp $ 00026 *============================================================================= 00027 * 00028 * WCSLIB 4.14 - C routines that implement the FITS World Coordinate System 00029 * (WCS) standard. Refer to 00030 * 00031 * "Representations of world coordinates in FITS", 00032 * Greisen, E.W., & Calabretta, M.R. 2002, A&A, 395, 1061 (Paper I) 00033 * 00034 * The Flexible Image Transport System (FITS), a data format widely used in 00035 * astronomy for data interchange and archive, is described in 00036 * 00037 * "Definition of The Flexible Image Transport System (FITS)", 00038 * Hanisch, R.J., Farris, A., Greisen, E.W., et al. 2001, A&A, 376, 359 00039 * 00040 * which formalizes NOST 100-2.0, a document produced by the NASA/Science 00041 * Office of Standards and Technology, see http://fits.gsfc.nasa.gov. 00042 * 00043 * Refer to the README file provided with WCSLIB for an overview of the 00044 * library. 00045 * 00046 * 00047 * Summary of the wcsunits routines 00048 * -------------------------------- 00049 * Routines in this suite deal with units specifications and conversions: 00050 * 00051 * - wcsunitse(): given two unit specifications, derive the conversion from 00052 * one to the other. 00053 * 00054 * - wcsutrne(): translates certain commonly used but non-standard unit 00055 * strings. It is intended to be called before wcsulexe() which only 00056 * handles standard FITS units specifications. 00057 * 00058 * - wcsulexe(): parses a standard FITS units specification of arbitrary 00059 * complexity, deriving the conversion to canonical units. 00060 * 00061 * 00062 * wcsunitse() - FITS units specification conversion 00063 * ------------------------------------------------- 00064 * wcsunitse() derives the conversion from one system of units to another. 00065 * 00066 * A deprecated form of this function, wcsunits(), lacks the wcserr** 00067 * parameter. 00068 * 00069 * Given: 00070 * have const char [] 00071 * FITS units specification to convert from (null- 00072 * terminated), with or without surrounding square 00073 * brackets (for inline specifications); text following 00074 * the closing bracket is ignored. 00075 * 00076 * want const char [] 00077 * FITS units specification to convert to (null- 00078 * terminated), with or without surrounding square 00079 * brackets (for inline specifications); text following 00080 * the closing bracket is ignored. 00081 * 00082 * Returned: 00083 * scale, 00084 * offset, 00085 * power double* Convert units using 00086 * 00087 = pow(scale*value + offset, power); 00088 * 00089 * Normally offset is zero except for log() or ln() 00090 * conversions, e.g. "log(MHz)" to "ln(Hz)". Likewise, 00091 * power is normally unity except for exp() conversions, 00092 * e.g. "exp(ms)" to "exp(/Hz)". Thus conversions 00093 * ordinarily consist of 00094 * 00095 = value *= scale; 00096 * 00097 * err struct wcserr ** 00098 * If enabled, for function return values > 1, this 00099 * struct will contain a detailed error message, see 00100 * wcserr_enable(). May be NULL if an error message is 00101 * not desired. 00102 * 00103 * Function return value: 00104 * int Status return value: 00105 * 0: Success. 00106 * 1-9: Status return from wcsulexe(). 00107 * 10: Non-conformant unit specifications. 00108 * 11: Non-conformant functions. 00109 * 00110 * scale is zeroed on return if an error occurs. 00111 * 00112 * 00113 * wcsutrne() - Translation of non-standard unit specifications 00114 * ------------------------------------------------------------ 00115 * wcsutrne() translates certain commonly used but non-standard unit strings, 00116 * e.g. "DEG", "MHZ", "KELVIN", that are not recognized by wcsulexe(), refer to 00117 * the notes below for a full list. Compounds are also recognized, e.g. 00118 * "JY/BEAM" and "KM/SEC/SEC". Extraneous embedded blanks are removed. 00119 * 00120 * A deprecated form of this function, wcsutrn(), lacks the wcserr** parameter. 00121 * 00122 * Given: 00123 * ctrl int Although "S" is commonly used to represent seconds, 00124 * its translation to "s" is potentially unsafe since the 00125 * standard recognizes "S" formally as Siemens, however 00126 * rarely that may be used. The same applies to "H" for 00127 * hours (Henry), and "D" for days (Debye). This 00128 * bit-flag controls what to do in such cases: 00129 * 1: Translate "S" to "s". 00130 * 2: Translate "H" to "h". 00131 * 4: Translate "D" to "d". 00132 * Thus ctrl == 0 doesn't do any unsafe translations, 00133 * whereas ctrl == 7 does all of them. 00134 * 00135 * Given and returned: 00136 * unitstr char [] Null-terminated character array containing the units 00137 * specification to be translated. 00138 * 00139 * Inline units specifications in the a FITS header 00140 * keycomment are also handled. If the first non-blank 00141 * character in unitstr is '[' then the unit string is 00142 * delimited by its matching ']'. Blanks preceding '[' 00143 * will be stripped off, but text following the closing 00144 * bracket will be preserved without modification. 00145 * 00146 * err struct wcserr ** 00147 * If enabled, for function return values > 1, this 00148 * struct will contain a detailed error message, see 00149 * wcserr_enable(). May be NULL if an error message is 00150 * not desired. 00151 * 00152 * Function return value: 00153 * int Status return value: 00154 * -1: No change was made, other than stripping blanks 00155 * (not an error). 00156 * 0: Success. 00157 * 9: Internal parser error. 00158 * 12: Potentially unsafe translation, whether applied 00159 * or not (see notes). 00160 * 00161 * Notes: 00162 * Translation of non-standard unit specifications: apart from leading and 00163 * trailing blanks, a case-sensitive match is required for the aliases listed 00164 * below, in particular the only recognized aliases with metric prefixes are 00165 * "KM", "KHZ", "MHZ", and "GHZ". Potentially unsafe translations of "D", 00166 * "H", and "S", shown in parentheses, are optional. 00167 * 00168 = Unit Recognized aliases 00169 = ---- ------------------------------------------------------------- 00170 = Angstrom angstrom 00171 = arcmin arcmins, ARCMIN, ARCMINS 00172 = arcsec arcsecs, ARCSEC, ARCSECS 00173 = beam BEAM 00174 = byte Byte 00175 = d day, days, (D), DAY, DAYS 00176 = deg degree, degrees, DEG, DEGREE, DEGREES 00177 = GHz GHZ 00178 = h hr, (H), HR 00179 = Hz hz, HZ 00180 = kHz KHZ 00181 = Jy JY 00182 = K kelvin, kelvins, Kelvin, Kelvins, KELVIN, KELVINS 00183 = km KM 00184 = m metre, meter, metres, meters, M, METRE, METER, METRES, METERS 00185 = min MIN 00186 = MHz MHZ 00187 = Ohm ohm 00188 = Pa pascal, pascals, Pascal, Pascals, PASCAL, PASCALS 00189 = pixel pixels, PIXEL, PIXELS 00190 = rad radian, radians, RAD, RADIAN, RADIANS 00191 = s sec, second, seconds, (S), SEC, SECOND, SECONDS 00192 = V volt, volts, Volt, Volts, VOLT, VOLTS 00193 = yr year, years, YR, YEAR, YEARS 00194 * 00195 * The aliases "angstrom", "ohm", and "Byte" for (Angstrom, Ohm, and byte) 00196 * are recognized by wcsulexe() itself as an unofficial extension of the 00197 * standard, but they are converted to the standard form here. 00198 * 00199 * 00200 * wcsulexe() - FITS units specification parser 00201 * -------------------------------------------- 00202 * wcsulexe() parses a standard FITS units specification of arbitrary 00203 * complexity, deriving the scale factor required to convert to canonical 00204 * units - basically SI with degrees and "dimensionless" additions such as 00205 * byte, pixel and count. 00206 * 00207 * A deprecated form of this function, wcsulex(), lacks the wcserr** parameter. 00208 * 00209 * Given: 00210 * unitstr const char [] 00211 * Null-terminated character array containing the units 00212 * specification, with or without surrounding square 00213 * brackets (for inline specifications); text following 00214 * the closing bracket is ignored. 00215 * 00216 * Returned: 00217 * func int* Special function type, see note 4: 00218 * 0: None 00219 * 1: log() ...base 10 00220 * 2: ln() ...base e 00221 * 3: exp() 00222 * 00223 * scale double* Scale factor for the unit specification; multiply a 00224 * value expressed in the given units by this factor to 00225 * convert it to canonical units. 00226 * 00227 * units double[WCSUNITS_NTYPE] 00228 * A units specification is decomposed into powers of 16 00229 * fundamental unit types: angle, mass, length, time, 00230 * count, pixel, etc. Preprocessor macro WCSUNITS_NTYPE 00231 * is defined to dimension this vector, and others such 00232 * WCSUNITS_PLANE_ANGLE, WCSUNITS_LENGTH, etc. to access 00233 * its elements. 00234 * 00235 * Corresponding character strings, wcsunits_types[] and 00236 * wcsunits_units[], are predefined to describe each 00237 * quantity and its canonical units. 00238 * 00239 * err struct wcserr ** 00240 * If enabled, for function return values > 1, this 00241 * struct will contain a detailed error message, see 00242 * wcserr_enable(). May be NULL if an error message is 00243 * not desired. 00244 * 00245 * Function return value: 00246 * int Status return value: 00247 * 0: Success. 00248 * 1: Invalid numeric multiplier. 00249 * 2: Dangling binary operator. 00250 * 3: Invalid symbol in INITIAL context. 00251 * 4: Function in invalid context. 00252 * 5: Invalid symbol in EXPON context. 00253 * 6: Unbalanced bracket. 00254 * 7: Unbalanced parenthesis. 00255 * 8: Consecutive binary operators. 00256 * 9: Internal parser error. 00257 * 00258 * scale and units[] are zeroed on return if an error 00259 * occurs. 00260 * 00261 * Notes: 00262 * 1: wcsulexe() is permissive in accepting whitespace in all contexts in a 00263 * units specification where it does not create ambiguity (e.g. not 00264 * between a metric prefix and a basic unit string), including in strings 00265 * like "log (m ** 2)" which is formally disallowed. 00266 * 00267 * 2: Supported extensions: 00268 * - "angstrom" (OGIP usage) is allowed in addition to "Angstrom". 00269 * - "ohm" (OGIP usage) is allowed in addition to "Ohm". 00270 * - "Byte" (common usage) is allowed in addition to "byte". 00271 * 00272 * 3: Table 6 of WCS Paper I lists eleven units for which metric prefixes are 00273 * allowed. However, in this implementation only prefixes greater than 00274 * unity are allowed for "a" (annum), "yr" (year), "pc" (parsec), "bit", 00275 * and "byte", and only prefixes less than unity are allowed for "mag" 00276 * (stellar magnitude). 00277 * 00278 * Metric prefix "P" (peta) is specifically forbidden for "a" (annum) to 00279 * avoid confusion with "Pa" (Pascal, not peta-annum). Note that metric 00280 * prefixes are specifically disallowed for "h" (hour) and "d" (day) so 00281 * that "ph" (photons) cannot be interpreted as pico-hours, nor "cd" 00282 * (candela) as centi-days. 00283 * 00284 * 4: Function types log(), ln() and exp() may only occur at the start of the 00285 * units specification. The scale and units[] returned for these refers 00286 * to the string inside the function "argument", e.g. to "MHz" in log(MHz) 00287 * for which a scale of 1e6 will be returned. 00288 * 00289 * 00290 * Global variable: const char *wcsunits_errmsg[] - Status return messages 00291 * ----------------------------------------------------------------------- 00292 * Error messages to match the status value returned from each function. 00293 * 00294 * 00295 * Global variable: const char *wcsunits_types[] - Names of physical quantities 00296 * ---------------------------------------------------------------------------- 00297 * Names for physical quantities to match the units vector returned by 00298 * wcsulexe(): 00299 * - 0: plane angle 00300 * - 1: solid angle 00301 * - 2: charge 00302 * - 3: mole 00303 * - 4: temperature 00304 * - 5: luminous intensity 00305 * - 6: mass 00306 * - 7: length 00307 * - 8: time 00308 * - 9: beam 00309 * - 10: bin 00310 * - 11: bit 00311 * - 12: count 00312 * - 13: stellar magnitude 00313 * - 14: pixel 00314 * - 15: solar ratio 00315 * - 16: voxel 00316 * 00317 * 00318 * Global variable: const char *wcsunits_units[] - Names of units 00319 * -------------------------------------------------------------- 00320 * Names for the units (SI) to match the units vector returned by wcsulexe(): 00321 * - 0: degree 00322 * - 1: steradian 00323 * - 2: Coulomb 00324 * - 3: mole 00325 * - 4: Kelvin 00326 * - 5: candela 00327 * - 6: kilogram 00328 * - 7: metre 00329 * - 8: second 00330 * 00331 * The remainder are dimensionless. 00332 *===========================================================================*/ 00333 00334 #ifndef WCSLIB_WCSUNITS 00335 #define WCSLIB_WCSUNITS 00336 00337 #include "wcserr.h" 00338 00339 #ifdef __cplusplus 00340 extern "C" { 00341 #endif 00342 00343 00344 extern const char *wcsunits_errmsg[]; 00345 00346 enum wcsunits_errmsg_enum { 00347 UNITSERR_SUCCESS = 0, /* Success. */ 00348 UNITSERR_BAD_NUM_MULTIPLIER = 1, /* Invalid numeric multiplier. */ 00349 UNITSERR_DANGLING_BINOP = 2, /* Dangling binary operator. */ 00350 UNITSERR_BAD_INITIAL_SYMBOL = 3, /* Invalid symbol in INITIAL 00351 context. */ 00352 UNITSERR_FUNCTION_CONTEXT = 4, /* Function in invalid context. */ 00353 UNITSERR_BAD_EXPON_SYMBOL = 5, /* Invalid symbol in EXPON context. */ 00354 UNITSERR_UNBAL_BRACKET = 6, /* Unbalanced bracket. */ 00355 UNITSERR_UNBAL_PAREN = 7, /* Unbalanced parenthesis. */ 00356 UNITSERR_CONSEC_BINOPS = 8, /* Consecutive binary operators. */ 00357 UNITSERR_PARSER_ERROR = 9, /* Internal parser error. */ 00358 UNITSERR_BAD_UNIT_SPEC = 10, /* Non-conformant unit 00359 specifications. */ 00360 UNITSERR_BAD_FUNCS = 11, /* Non-conformant functions. */ 00361 UNITSERR_UNSAFE_TRANS = 12 /* Potentially unsafe translation. */ 00362 }; 00363 00364 extern const char *wcsunits_types[]; 00365 extern const char *wcsunits_units[]; 00366 00367 #define WCSUNITS_PLANE_ANGLE 0 00368 #define WCSUNITS_SOLID_ANGLE 1 00369 #define WCSUNITS_CHARGE 2 00370 #define WCSUNITS_MOLE 3 00371 #define WCSUNITS_TEMPERATURE 4 00372 #define WCSUNITS_LUMINTEN 5 00373 #define WCSUNITS_MASS 6 00374 #define WCSUNITS_LENGTH 7 00375 #define WCSUNITS_TIME 8 00376 #define WCSUNITS_BEAM 9 00377 #define WCSUNITS_BIN 10 00378 #define WCSUNITS_BIT 11 00379 #define WCSUNITS_COUNT 12 00380 #define WCSUNITS_MAGNITUDE 13 00381 #define WCSUNITS_PIXEL 14 00382 #define WCSUNITS_SOLRATIO 15 00383 #define WCSUNITS_VOXEL 16 00384 00385 #define WCSUNITS_NTYPE 17 00386 00387 00388 int wcsunitse(const char have[], const char want[], double *scale, 00389 double *offset, double *power, struct wcserr **err); 00390 00391 int wcsutrne(int ctrl, char unitstr[], struct wcserr **err); 00392 00393 int wcsulexe(const char unitstr[], int *func, double *scale, double units[], 00394 struct wcserr **err); 00395 00396 /* Deprecated. */ 00397 int wcsunits(const char have[], const char want[], double *scale, 00398 double *offset, double *power); 00399 int wcsutrn(int ctrl, char unitstr[]); 00400 int wcsulex(const char unitstr[], int *func, double *scale, double units[]); 00401 00402 #ifdef __cplusplus 00403 } 00404 #endif 00405 00406 #endif /* WCSLIB_WCSUNITS */