[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // fitsHeader.cc: Information about the FITS file's header. |
---|
| 3 | // ----------------------------------------------------------------------- |
---|
| 4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 5 | // |
---|
| 6 | // This program is free software; you can redistribute it and/or modify it |
---|
| 7 | // under the terms of the GNU General Public License as published by the |
---|
| 8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 9 | // option) any later version. |
---|
| 10 | // |
---|
| 11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 14 | // for more details. |
---|
| 15 | // |
---|
| 16 | // You should have received a copy of the GNU General Public License |
---|
| 17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | // |
---|
| 20 | // Correspondence concerning Duchamp may be directed to: |
---|
| 21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 22 | // Postal address: Dr. Matthew Whiting |
---|
| 23 | // Australia Telescope National Facility, CSIRO |
---|
| 24 | // PO Box 76 |
---|
| 25 | // Epping NSW 1710 |
---|
| 26 | // AUSTRALIA |
---|
| 27 | // ----------------------------------------------------------------------- |
---|
[272] | 28 | #include <iostream> |
---|
| 29 | #include <sstream> |
---|
| 30 | #include <string> |
---|
[394] | 31 | #include <wcslib/wcs.h> |
---|
| 32 | #include <wcslib/wcsunits.h> |
---|
[393] | 33 | #include <duchamp/duchamp.hh> |
---|
| 34 | #include <duchamp/param.hh> |
---|
| 35 | #include <duchamp/fitsHeader.hh> |
---|
| 36 | #include <duchamp/Utils/utils.hh> |
---|
[272] | 37 | |
---|
[378] | 38 | namespace duchamp |
---|
[272] | 39 | { |
---|
| 40 | |
---|
[378] | 41 | FitsHeader::FitsHeader() |
---|
| 42 | { |
---|
| 43 | this->wcs = (struct wcsprm *)calloc(1,sizeof(struct wcsprm)); |
---|
| 44 | this->wcs->flag=-1; |
---|
| 45 | wcsini(true, 3, this->wcs); |
---|
| 46 | this->wcsIsGood = false; |
---|
| 47 | this->nwcs = 0; |
---|
| 48 | this->scale=1.; |
---|
| 49 | this->offset=0.; |
---|
| 50 | this->power=1.; |
---|
| 51 | this->fluxUnits="counts"; |
---|
| 52 | this->intFluxUnits="counts"; |
---|
| 53 | } |
---|
[272] | 54 | |
---|
[378] | 55 | FitsHeader::~FitsHeader() |
---|
| 56 | { |
---|
| 57 | /** |
---|
| 58 | * Uses the WCSLIB function wcsvfree to clear the wcsprm struct. |
---|
| 59 | */ |
---|
| 60 | wcsvfree(&nwcs,&wcs); |
---|
| 61 | } |
---|
[272] | 62 | |
---|
[378] | 63 | FitsHeader::FitsHeader(const FitsHeader& h) |
---|
| 64 | { |
---|
| 65 | operator=(h); |
---|
| 66 | } |
---|
[272] | 67 | |
---|
[378] | 68 | FitsHeader& FitsHeader::operator= (const FitsHeader& h) |
---|
| 69 | { |
---|
| 70 | if(this == &h) return *this; |
---|
| 71 | this->wcs = (struct wcsprm *)calloc(1,sizeof(struct wcsprm)); |
---|
| 72 | this->wcs->flag = -1; |
---|
| 73 | wcsini(true, h.wcs->naxis, this->wcs); |
---|
| 74 | wcscopy(true, h.wcs, this->wcs); |
---|
| 75 | wcsset(this->wcs); |
---|
| 76 | this->nwcs = h.nwcs; |
---|
| 77 | this->wcsIsGood = h.wcsIsGood; |
---|
| 78 | this->spectralUnits = h.spectralUnits; |
---|
| 79 | this->fluxUnits = h.fluxUnits; |
---|
| 80 | this->intFluxUnits = h.intFluxUnits; |
---|
| 81 | this->beamSize = h.beamSize; |
---|
| 82 | this->bmajKeyword = h.bmajKeyword; |
---|
| 83 | this->bminKeyword = h.bminKeyword; |
---|
| 84 | this->bpaKeyword = h.bpaKeyword; |
---|
| 85 | this->blankKeyword = h.blankKeyword; |
---|
| 86 | this->bzeroKeyword = h.bzeroKeyword; |
---|
| 87 | this->bscaleKeyword = h.bscaleKeyword; |
---|
| 88 | this->scale = h.scale; |
---|
| 89 | this->offset = h.offset; |
---|
| 90 | this->power = h.power; |
---|
| 91 | return *this; |
---|
| 92 | } |
---|
[272] | 93 | |
---|
[378] | 94 | void FitsHeader::setWCS(struct wcsprm *w) |
---|
| 95 | { |
---|
| 96 | /** |
---|
| 97 | * A function that assigns the wcs parameters, and runs |
---|
| 98 | * wcsset to set it up correctly. |
---|
| 99 | * Performs a check to see if the WCS is good (by looking at |
---|
| 100 | * the lng and lat wcsprm parameters), and sets the wcsIsGood |
---|
| 101 | * flag accordingly. |
---|
| 102 | * \param w A WCSLIB wcsprm struct with the correct parameters. |
---|
| 103 | */ |
---|
| 104 | wcscopy(true, w, this->wcs); |
---|
| 105 | wcsset(this->wcs); |
---|
| 106 | if( (w->lng!=-1) && (w->lat!=-1) ) this->wcsIsGood = true; |
---|
| 107 | } |
---|
[272] | 108 | |
---|
[378] | 109 | struct wcsprm *FitsHeader::getWCS() |
---|
| 110 | { |
---|
| 111 | /** |
---|
| 112 | * A function that returns a properly initilized wcsprm object |
---|
| 113 | * corresponding to the WCS. |
---|
| 114 | */ |
---|
| 115 | struct wcsprm *wNew = (struct wcsprm *)calloc(1,sizeof(struct wcsprm)); |
---|
| 116 | wNew->flag=-1; |
---|
| 117 | wcsini(true, this->wcs->naxis, wNew); |
---|
| 118 | wcscopy(true, this->wcs, wNew); |
---|
| 119 | wcsset(wNew); |
---|
| 120 | return wNew; |
---|
| 121 | } |
---|
[272] | 122 | |
---|
[378] | 123 | int FitsHeader::wcsToPix(const double *world, double *pix) |
---|
| 124 | { |
---|
| 125 | return wcsToPixSingle(this->wcs, world, pix); |
---|
[419] | 126 | } |
---|
[378] | 127 | int FitsHeader::wcsToPix(const double *world, double *pix, const int npts) |
---|
| 128 | { |
---|
| 129 | return wcsToPixMulti(this->wcs, world, pix, npts); |
---|
[419] | 130 | } |
---|
[378] | 131 | int FitsHeader::pixToWCS(const double *pix, double *world) |
---|
| 132 | { |
---|
| 133 | return pixToWCSSingle(this->wcs, pix, world); |
---|
[419] | 134 | } |
---|
[378] | 135 | int FitsHeader::pixToWCS(const double *pix, double *world, const int npts) |
---|
| 136 | { |
---|
| 137 | return pixToWCSMulti(this->wcs, pix,world, npts); |
---|
[419] | 138 | } |
---|
[272] | 139 | |
---|
[378] | 140 | |
---|
| 141 | double FitsHeader::pixToVel(double &x, double &y, double &z) |
---|
| 142 | { |
---|
| 143 | double vel; |
---|
| 144 | if(this->wcsIsGood){ |
---|
| 145 | double *pix = new double[3]; |
---|
| 146 | double *world = new double[3]; |
---|
| 147 | pix[0] = x; pix[1] = y; pix[2] = z; |
---|
| 148 | pixToWCSSingle(this->wcs,pix,world); |
---|
| 149 | vel = this->specToVel(world[2]); |
---|
| 150 | delete [] pix; |
---|
| 151 | delete [] world; |
---|
| 152 | } |
---|
| 153 | else vel = z; |
---|
| 154 | return vel; |
---|
[272] | 155 | } |
---|
| 156 | |
---|
[378] | 157 | double* FitsHeader::pixToVel(double &x, double &y, double *zarray, int size) |
---|
| 158 | { |
---|
| 159 | double *newzarray = new double[size]; |
---|
| 160 | if(this->wcsIsGood){ |
---|
| 161 | double *pix = new double[size*3]; |
---|
| 162 | for(int i=0;i<size;i++){ |
---|
| 163 | pix[3*i] = x; |
---|
| 164 | pix[3*i+1] = y; |
---|
| 165 | pix[3*i+2] = zarray[i]; |
---|
| 166 | } |
---|
| 167 | double *world = new double[size*3]; |
---|
| 168 | pixToWCSMulti(this->wcs,pix,world,size); |
---|
| 169 | delete [] pix; |
---|
| 170 | for(int i=0;i<size;i++) newzarray[i] = this->specToVel(world[3*i+2]); |
---|
| 171 | delete [] world; |
---|
[272] | 172 | } |
---|
[378] | 173 | else{ |
---|
| 174 | for(int i=0;i<size;i++) newzarray[i] = zarray[i]; |
---|
| 175 | } |
---|
| 176 | return newzarray; |
---|
[272] | 177 | } |
---|
[378] | 178 | |
---|
| 179 | double FitsHeader::specToVel(const double &coord) |
---|
| 180 | { |
---|
| 181 | double vel; |
---|
| 182 | if(power==1.0) vel = coord*this->scale + this->offset; |
---|
| 183 | else vel = pow( (coord*this->scale + this->offset), this->power); |
---|
| 184 | return vel; |
---|
[272] | 185 | } |
---|
| 186 | |
---|
[378] | 187 | double FitsHeader::velToSpec(const float &velocity) |
---|
| 188 | { |
---|
| 189 | // return velToCoord(this->wcs,velocity,this->spectralUnits);}; |
---|
| 190 | return (pow(velocity, 1./this->power) - this->offset) / this->scale; |
---|
| 191 | } |
---|
[272] | 192 | |
---|
[378] | 193 | std::string FitsHeader::getIAUName(double ra, double dec) |
---|
| 194 | { |
---|
| 195 | if(strcmp(this->wcs->lngtyp,"RA")==0) |
---|
| 196 | return getIAUNameEQ(ra, dec, this->wcs->equinox); |
---|
| 197 | else |
---|
| 198 | return getIAUNameGAL(ra, dec); |
---|
| 199 | } |
---|
[272] | 200 | |
---|
[378] | 201 | bool FitsHeader::needBeamSize() |
---|
| 202 | { |
---|
| 203 | /** |
---|
| 204 | * A function that tells you whether the beam correction is |
---|
| 205 | * needed. It checks to see whether the flux units string ends in |
---|
| 206 | * "/beam" (in which case the beam size etc is needed and |
---|
| 207 | * integrated fluxes need to be corrected). |
---|
| 208 | * /return True if FitsHeader::fluxUnits ends in "/beam". False |
---|
| 209 | * otherwise. |
---|
| 210 | */ |
---|
| 211 | int size = this->fluxUnits.size(); |
---|
[413] | 212 | if(size<6) return false; |
---|
| 213 | else { |
---|
| 214 | std::string tailOfFluxUnits = this->fluxUnits.substr(size-5,size); |
---|
| 215 | return (tailOfFluxUnits == "/beam"); |
---|
| 216 | } |
---|
[378] | 217 | } |
---|
[272] | 218 | |
---|
[378] | 219 | void FitsHeader::fixUnits(Param &par) |
---|
| 220 | { |
---|
| 221 | /** |
---|
| 222 | * Put the units for the FITS header into some sort of standard form. |
---|
| 223 | * |
---|
| 224 | * We first get the desired spectral units from the Parameter set, |
---|
| 225 | * and then transform the spectral units of the wcsprm struct to |
---|
| 226 | * those units. If this doesn't work, we leave them as they are. If |
---|
| 227 | * they are blank, we make them SPC and give an error message -- |
---|
| 228 | * this should hopefully NOT happen. |
---|
| 229 | * |
---|
| 230 | * We also work out the units for the integrated flux. If there are |
---|
| 231 | * three axes, we just append the spectral units to the flux units |
---|
| 232 | * (removing "/beam" if it is present). If there are just two, we |
---|
| 233 | * simply keep it the same, removing the "/beam". |
---|
| 234 | * |
---|
| 235 | * \param par The parameter set telling us what the desired |
---|
| 236 | * spectral units are. |
---|
| 237 | */ |
---|
[375] | 238 | |
---|
[378] | 239 | // define spectral units from the param set |
---|
| 240 | this->spectralUnits = par.getSpectralUnits(); |
---|
| 241 | double sc=1.; |
---|
| 242 | double of=0.; |
---|
| 243 | double po=1.; |
---|
| 244 | // if((this->wcsIsGood) && (this->naxis>2)){ |
---|
| 245 | if(this->wcsIsGood){ |
---|
| 246 | int status = wcsunits( this->wcs->cunit[this->wcs->spec], |
---|
| 247 | (char *)this->spectralUnits.c_str(), |
---|
| 248 | &sc, &of, &po); |
---|
| 249 | if(status > 0){ |
---|
| 250 | std::stringstream errmsg; |
---|
| 251 | errmsg << "WCSUNITS Error, Code = " << status |
---|
| 252 | << ": " << wcsunits_errmsg[status] << "\n"; |
---|
| 253 | if(status == 10) |
---|
| 254 | errmsg << "Tried to get conversion from \"" |
---|
| 255 | << this->wcs->cunit[this->wcs->spec] << "\" to \"" |
---|
| 256 | << this->spectralUnits.c_str() << "\".\n"; |
---|
| 257 | this->spectralUnits = this->wcs->cunit[this->wcs->spec]; |
---|
| 258 | if(this->spectralUnits==""){ |
---|
| 259 | errmsg << "Spectral units not specified. " |
---|
| 260 | << "For data presentation, we will use dummy units of \"SPC\"." |
---|
| 261 | << "\n" |
---|
| 262 | << "Please report this occurence -- it should not happen now! " |
---|
| 263 | << "In the meantime, you may want to set the CUNIT" |
---|
| 264 | << this->wcs->spec + 1 <<" keyword to make this work.\n"; |
---|
| 265 | this->spectralUnits = "SPC"; |
---|
| 266 | } |
---|
| 267 | duchampError("fixUnits", errmsg.str()); |
---|
| 268 | } |
---|
[300] | 269 | |
---|
[272] | 270 | } |
---|
[378] | 271 | this->scale = sc; |
---|
| 272 | this->offset= of; |
---|
| 273 | this->power = po; |
---|
[356] | 274 | |
---|
[378] | 275 | // Work out the integrated flux units, based on the spectral units. |
---|
| 276 | // If flux is per beam, trim the /beam from the flux units and multiply |
---|
| 277 | // by the spectral units. |
---|
| 278 | // Otherwise, just muliply by the spectral units. |
---|
[413] | 279 | if(this->fluxUnits.size()>5){ |
---|
[378] | 280 | |
---|
| 281 | if(makelower(this->fluxUnits.substr(this->fluxUnits.size()-5, |
---|
| 282 | this->fluxUnits.size() )) == "/beam"){ |
---|
| 283 | this->intFluxUnits = this->fluxUnits.substr(0,this->fluxUnits.size()-5); |
---|
| 284 | } |
---|
| 285 | else this->intFluxUnits = this->fluxUnits; |
---|
[272] | 286 | |
---|
[378] | 287 | if(this->naxis>2) this->intFluxUnits += " " + this->spectralUnits; |
---|
[272] | 288 | } |
---|
[300] | 289 | |
---|
[272] | 290 | } |
---|
| 291 | |
---|
| 292 | } |
---|