source: branches/fitshead-branch/Utils/wcsFunctions.cc @ 1441

Last change on this file since 1441 was 98, checked in by Matthew Whiting, 18 years ago

Added #include <math.h> to the start of wcsFunctions.cc and baselineSubtract.cc
to comply with gcc v4.0.

File size: 7.3 KB
Line 
1#include <iostream>
2#include <math.h>
3#include <wcs.h>
4#include <wcsunits.h>
5#include <Utils/utils.hh>
6
7int pixToWCSSingle(wcsprm *wcs, const double *pix, double *world)
8{
9  /**
10   *  pixToWCSSingle(wcsprm *wcs, const double *pix, double *world)
11   *   Uses wcs to convert the three-dimensional pixel position referenced by pix
12   *   to world coordinates, which are placed in the array world[].
13   *   Assumes that pix only has one point with an x,y,and z pixel positions.
14   *   Offsets these pixel values by 1 to account for the C arrays being indexed to 0.
15   */
16
17  int nelem=3,npts=1,flag;
18
19  double *newpix = new double[nelem*npts];
20  for(int i=0;i<nelem*npts;i++) newpix[i] = pix[i] + 1.; 
21  // correct from 0-indexed to 1-indexed pixel array
22
23  int    *stat   = new int[npts];
24  double *imgcrd = new double[nelem*npts];
25  double *phi    = new double[npts];
26  double *theta  = new double[npts];
27  if(flag=wcsp2s(wcs, npts, nelem, newpix, imgcrd, phi, theta, world, stat)>0){
28    std::cerr << "ERROR <pixToWCSSingle> : WCS Error Code = " <<flag<<": "
29              <<wcs_errmsg[flag]<<std::endl;
30    std::cerr << "  stat value is " << stat[0] << std::endl;
31  }
32  delete [] stat;
33  delete [] imgcrd;
34  delete [] phi;
35  delete [] theta;
36  delete [] newpix;
37  return flag;
38}
39
40int wcsToPixSingle(wcsprm *wcs, const double *world, double *pix)
41{
42  /**
43   *  wcsToPixSingle(wcsprm *wcs, const double *world, double *pix)
44   *   Uses wcs to convert the three-dimensional world coordinate position referenced by world
45   *   to pixel coordinates, which are placed in the array pix[].
46   *   Assumes that world only has one point (three values eg. RA, Dec, Velocity)
47   *   Offsets the pixel values by 1 to account for the C arrays being indexed to 0.
48   */
49
50  int nelem=3,npts=1,flag;
51  int    *stat   = new int[npts];
52  double *imgcrd = new double[nelem*npts];
53  double *phi    = new double[npts];
54  double *theta  = new double[npts];
55  if(flag=wcss2p(wcs, npts, nelem, world, phi, theta, imgcrd, pix, stat)>0){
56    std::cerr << "ERROR <wcsToPixSingle> : WCS Error Code = " <<flag<<": "
57              <<wcs_errmsg[flag]<<std::endl;
58    std::cerr << "  stat value is " << stat[0] << std::endl;
59  }
60
61  for(int i=0;i<nelem;i++) pix[i] -= 1.;  // correct from 1-indexed to 0-indexed pixel array
62
63  delete [] stat;
64  delete [] imgcrd;
65  delete [] phi;
66  delete [] theta;
67  return flag;
68}
69
70int pixToWCSMulti(wcsprm *wcs, const double *pix, double *world, const int npts)
71{
72  /**
73   *  pixToWCSSingle(wcsprm *wcs, const double *pix, double *world)
74   *   Uses wcs to convert the three-dimensional pixel positions referenced by pix
75   *   to world coordinates, which are placed in the array world[].
76   *   pix is assumed to hold the positions of npts points.
77   *   Offsets these pixel values by 1 to account for the C arrays being indexed to 0.
78   */
79
80  int nelem=3,flag;
81
82  double *newpix = new double[nelem*npts];
83  for(int i=0;i<nelem*npts;i++) newpix[i] = pix[i] + 1.; 
84  // correct from 0-indexed to 1-indexed pixel array
85
86  int    *stat   = new int[npts];
87  double *imgcrd = new double[nelem*npts];
88  double *phi    = new double[npts];
89  double *theta  = new double[npts];
90  if(flag=wcsp2s(wcs, npts, nelem, newpix, imgcrd, phi, theta, world, stat)>0){
91    std::cerr << "ERROR <pixToWCSMulti> : WCS Error Code = " <<flag<<": "
92              <<wcs_errmsg[flag]<<std::endl;
93    std::cerr << "  stat value is " << stat[0] << std::endl;
94  }
95  delete [] stat;
96  delete [] imgcrd;
97  delete [] phi;
98  delete [] theta;
99  delete [] newpix;
100  return flag;
101}
102
103int wcsToPixMulti(wcsprm *wcs, const double *world, double *pix, const int npts)
104{
105  /**
106   *  wcsToPixSingle(wcsprm *wcs, const double *world, double *pix)
107   *   Uses wcs to convert the three-dimensional world coordinate position referenced by world
108   *   to pixel coordinates, which are placed in the array pix[].
109   *   world is assumed to hold the positions of npts points.
110   *   Offsets the pixel values by 1 to account for the C arrays being indexed to 0.
111   */
112
113  int nelem=3,flag=0;
114  int    *stat   = new int[npts];
115  double *imgcrd = new double[nelem*npts];
116  double *phi    = new double[npts];
117  double *theta  = new double[npts];
118  if(flag=wcss2p(wcs, npts, nelem, world, phi, theta, imgcrd, pix, stat)>0){
119    std::cerr << "ERROR <wcsToPixMulti> : WCS Error Code = " <<flag<<": "
120              <<wcs_errmsg[flag]<<std::endl;
121    std::cerr << "  stat value is " << stat[0] << std::endl;
122  }
123
124  for(int i=0;i<nelem;i++) pix[i] -= 1.;  // correct from 1-indexed to 0-indexed pixel array
125
126  delete [] stat;
127  delete [] imgcrd;
128  delete [] phi;
129  delete [] theta;
130  return flag;
131}
132
133double pixelToVelocity(wcsprm *wcs, double &x, double &y, double &z, string velUnits)
134{
135  /**
136   *  pixelToVelocity(wcsprm *wcs, double &x, double &y, double &z, string velUnits)
137   *   Uses wcs to convert the three-dimensional pixel position (x,y,z)
138   *   to world coordinates.
139   *   Returns the velocity in the units given by velUnits.
140   */
141
142  int    *stat   = new int[1];
143  double *pixcrd = new double[3];
144  double *imgcrd = new double[3];
145  double *world  = new double[3];
146  double *phi    = new double[1];
147  double *theta  = new double[1];
148  // correct from 0-indexed to 1-indexed pixel array by adding 1 to pixel values.
149  pixcrd[0] = x + 1;
150  pixcrd[1] = y + 1;
151  pixcrd[2] = z + 1;
152  if(int flag=wcsp2s(wcs, 1, 3, pixcrd, imgcrd, phi, theta, world, stat)>0){
153    std::cerr<<"ERROR <pixelToVelocity> : WCS Error Code = "<<flag<<": "
154             <<wcs_errmsg[flag]<<std::endl;
155    std::cerr<<"  stat value is "<<stat[0]<<std::endl;
156  }
157
158  double vel = coordToVel(wcs, world[2], velUnits);
159
160  delete [] stat;
161  delete [] pixcrd;
162  delete [] imgcrd;
163  delete [] world;
164  delete [] phi;
165  delete [] theta;
166 
167  return vel;
168}
169 
170double coordToVel(wcsprm *wcs, const double coord, string outputUnits)
171{
172  /**
173   *  coordToVel(wcsprm *wcs, const double coord, string outputUnits)
174   *   Convert the wcs coordinate given by coord to a velocity in km/s.
175   *   Does this by checking the ztype parameter in wcs to see if it is FREQ or otherwise,
176   *   and converts accordingly.
177   */
178
179  static int errmsg = 0;
180  double scale, offset, power;
181  int flag = wcsunits( wcs->cunit[2], outputUnits.c_str(), &scale, &offset, &power);
182
183  if(flag>0){
184    if(errmsg==0){
185      std::cerr << "ERROR <coordToVel> : WCSUNITS Error Code = " << flag << ":"
186                << wcsunits_errmsg[flag] << std::endl;
187      if(flag==10) std::cerr << "Tried to get conversion from " << wcs->cunit[2]
188                             << " to " << outputUnits.c_str() << std::endl;
189      std::cerr << "Using coordinate value instead\n";
190      errmsg = 1;
191    }
192    return coord;
193  }
194  else return pow( (coord*scale + offset), power);
195
196}
197
198double velToCoord(wcsprm *wcs, const float velocity, string outputUnits)
199{
200  /**
201   *  velToCoord(wcsprm *wcs, const double coord)
202   *   Convert the velocity given to the appropriate world coordinate for wcs.
203   *   Does this by checking the ztype parameter in wcs to see if it is FREQ or otherwise,
204   *   and converts accordingly.
205   */
206
207  double scale, offset, power;
208  int flag = wcsunits( wcs->cunit[2], outputUnits.c_str(), &scale, &offset, &power);
209
210  if(flag>0){
211    std::cerr << "ERROR <velToCoord> : WCSUNITS Error Code = " << flag << ":"
212              << wcsunits_errmsg[flag] << std::endl;
213    std::cerr << "Using coordinate value instead\n";
214    return velocity;
215  }
216  else return (pow(velocity, 1./power) - offset) / scale;
217 
218}
Note: See TracBrowser for help on using the repository browser.