[3] | 1 | #include <iostream> |
---|
[98] | 2 | #include <math.h> |
---|
[3] | 3 | #include <wcs.h> |
---|
[90] | 4 | #include <wcsunits.h> |
---|
[3] | 5 | #include <Utils/utils.hh> |
---|
| 6 | |
---|
[91] | 7 | int pixToWCSSingle(wcsprm *wcs, const double *pix, double *world) |
---|
[3] | 8 | { |
---|
[22] | 9 | /** |
---|
[91] | 10 | * pixToWCSSingle(wcsprm *wcs, const double *pix, double *world) |
---|
[22] | 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 | |
---|
[3] | 17 | int nelem=3,npts=1,flag; |
---|
[22] | 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 | |
---|
[3] | 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]; |
---|
[91] | 27 | if(flag=wcsp2s(wcs, npts, nelem, newpix, imgcrd, phi, theta, world, stat)>0){ |
---|
[71] | 28 | std::cerr << "ERROR <pixToWCSSingle> : WCS Error Code = " <<flag<<": " |
---|
| 29 | <<wcs_errmsg[flag]<<std::endl; |
---|
[3] | 30 | std::cerr << " stat value is " << stat[0] << std::endl; |
---|
| 31 | } |
---|
| 32 | delete [] stat; |
---|
| 33 | delete [] imgcrd; |
---|
| 34 | delete [] phi; |
---|
| 35 | delete [] theta; |
---|
[91] | 36 | delete [] newpix; |
---|
[3] | 37 | return flag; |
---|
| 38 | } |
---|
| 39 | |
---|
[91] | 40 | int wcsToPixSingle(wcsprm *wcs, const double *world, double *pix) |
---|
[3] | 41 | { |
---|
[22] | 42 | /** |
---|
[91] | 43 | * wcsToPixSingle(wcsprm *wcs, const double *world, double *pix) |
---|
[22] | 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 | |
---|
[3] | 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]; |
---|
[91] | 55 | if(flag=wcss2p(wcs, npts, nelem, world, phi, theta, imgcrd, pix, stat)>0){ |
---|
[71] | 56 | std::cerr << "ERROR <wcsToPixSingle> : WCS Error Code = " <<flag<<": " |
---|
| 57 | <<wcs_errmsg[flag]<<std::endl; |
---|
[3] | 58 | std::cerr << " stat value is " << stat[0] << std::endl; |
---|
| 59 | } |
---|
[22] | 60 | |
---|
| 61 | for(int i=0;i<nelem;i++) pix[i] -= 1.; // correct from 1-indexed to 0-indexed pixel array |
---|
| 62 | |
---|
[3] | 63 | delete [] stat; |
---|
| 64 | delete [] imgcrd; |
---|
| 65 | delete [] phi; |
---|
| 66 | delete [] theta; |
---|
| 67 | return flag; |
---|
| 68 | } |
---|
| 69 | |
---|
[91] | 70 | int pixToWCSMulti(wcsprm *wcs, const double *pix, double *world, const int npts) |
---|
[22] | 71 | { |
---|
| 72 | /** |
---|
[91] | 73 | * pixToWCSSingle(wcsprm *wcs, const double *pix, double *world) |
---|
[22] | 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]; |
---|
[91] | 90 | if(flag=wcsp2s(wcs, npts, nelem, newpix, imgcrd, phi, theta, world, stat)>0){ |
---|
| 91 | std::cerr << "ERROR <pixToWCSMulti> : WCS Error Code = " <<flag<<": " |
---|
[71] | 92 | <<wcs_errmsg[flag]<<std::endl; |
---|
[22] | 93 | std::cerr << " stat value is " << stat[0] << std::endl; |
---|
| 94 | } |
---|
| 95 | delete [] stat; |
---|
| 96 | delete [] imgcrd; |
---|
| 97 | delete [] phi; |
---|
| 98 | delete [] theta; |
---|
[38] | 99 | delete [] newpix; |
---|
[22] | 100 | return flag; |
---|
| 101 | } |
---|
| 102 | |
---|
[91] | 103 | int wcsToPixMulti(wcsprm *wcs, const double *world, double *pix, const int npts) |
---|
[22] | 104 | { |
---|
| 105 | /** |
---|
[91] | 106 | * wcsToPixSingle(wcsprm *wcs, const double *world, double *pix) |
---|
[22] | 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 | */ |
---|
[95] | 112 | |
---|
| 113 | int nelem=3,flag=0; |
---|
[22] | 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]; |
---|
[91] | 118 | if(flag=wcss2p(wcs, npts, nelem, world, phi, theta, imgcrd, pix, stat)>0){ |
---|
| 119 | std::cerr << "ERROR <wcsToPixMulti> : WCS Error Code = " <<flag<<": " |
---|
[71] | 120 | <<wcs_errmsg[flag]<<std::endl; |
---|
[22] | 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 | |
---|
[91] | 133 | double pixelToVelocity(wcsprm *wcs, double &x, double &y, double &z, string velUnits) |
---|
[3] | 134 | { |
---|
[22] | 135 | /** |
---|
[91] | 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 | |
---|
| 170 | double coordToVel(wcsprm *wcs, const double coord, string outputUnits) |
---|
| 171 | { |
---|
| 172 | /** |
---|
| 173 | * coordToVel(wcsprm *wcs, const double coord, string outputUnits) |
---|
[22] | 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 | |
---|
[90] | 179 | static int errmsg = 0; |
---|
| 180 | double scale, offset, power; |
---|
[91] | 181 | int flag = wcsunits( wcs->cunit[2], outputUnits.c_str(), &scale, &offset, &power); |
---|
[90] | 182 | |
---|
| 183 | if(flag>0){ |
---|
| 184 | if(errmsg==0){ |
---|
| 185 | std::cerr << "ERROR <coordToVel> : WCSUNITS Error Code = " << flag << ":" |
---|
| 186 | << wcsunits_errmsg[flag] << std::endl; |
---|
[91] | 187 | if(flag==10) std::cerr << "Tried to get conversion from " << wcs->cunit[2] |
---|
| 188 | << " to " << outputUnits.c_str() << std::endl; |
---|
[90] | 189 | std::cerr << "Using coordinate value instead\n"; |
---|
| 190 | errmsg = 1; |
---|
| 191 | } |
---|
| 192 | return coord; |
---|
[3] | 193 | } |
---|
[90] | 194 | else return pow( (coord*scale + offset), power); |
---|
| 195 | |
---|
[3] | 196 | } |
---|
| 197 | |
---|
[91] | 198 | double velToCoord(wcsprm *wcs, const float velocity, string outputUnits) |
---|
[3] | 199 | { |
---|
[22] | 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 | |
---|
[90] | 207 | double scale, offset, power; |
---|
[91] | 208 | int flag = wcsunits( wcs->cunit[2], outputUnits.c_str(), &scale, &offset, &power); |
---|
[90] | 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; |
---|
[3] | 217 | |
---|
| 218 | } |
---|