File: fitscoo.cpp

package info (click to toggle)
munipack 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,104 kB
  • sloc: cpp: 29,677; sh: 4,909; f90: 2,872; makefile: 278; python: 140; xml: 72; awk: 12
file content (297 lines) | stat: -rw-r--r-- 6,646 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
   xmunipack - coordinates

   Copyright © 1997-2013, 2017, 2019-2022 F.Hroch (hroch@physics.muni.cz)

   This file is part of Munipack.

   Munipack is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Munipack is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "fits.h"
#include <wx/wx.h>
#include <math.h>

#ifdef __WXDEBUG__
#include <wx/debug.h>
#endif

#define RAD 57.295779513082322865


// -----------------------------------------------------------------

FitsProjection::FitsProjection():
  acen(0.0),dcen(0.0),xcen(0.0),ycen(0.0),scale(1.0),angle(0.0),reflex(1.0) {}


FitsProjection::FitsProjection(const wxString& t, double a,double d,double x,
			       double y,double s,double r,double z):
  type(t.Upper()),acen(a/RAD),dcen(d/RAD),xcen(x),ycen(y),scale(s*RAD),
  angle(r/RAD), reflex(z) {}

FitsProjection::FitsProjection(const wxString& t, double a,double d, double x,
			       double y, double cd11, double cd12, double cd21,
			       double cd22):
  type(t.Upper()),acen(a/RAD),dcen(d/RAD),xcen(x),ycen(y),scale(1.0/RAD),
  angle(0.0), reflex(1.0)
{
  reflex = -cd11*cd22 > 0 ? 1.0 : -1.0;

  if( fabs(cd12) > 0 || fabs(cd22) > 0 )
    angle = atan2(reflex*cd12,cd22);
  else
    type = "";

  double q = fabs(cd11*cd22) + fabs(cd12*cd21);
  if( q > 0 )
    scale = RAD/sqrt(q);
  else
    type = "";

}

bool FitsProjection::IsOk() const { return type != "";  }

double FitsProjection::GetScale() const { return scale; }

void FitsProjection::ad2xy(double a,double d,double& x,double& y) const
{
  if( ! IsOk() ) {
    x = 0;
    y = 0;
    return;
  }

  double aa = cos(angle)*scale;
  double bb = sin(angle)*scale;

  double u,v;

  gnomon(a/RAD,d/RAD,u,v);
  x = xcen +  (aa*u + bb*v)*reflex;
  y = ycen + (-bb*u + aa*v);
}

void FitsProjection::xy2ad(double x,double y,double& a,double& d) const
{
  if( ! IsOk() ) {
    a = 0;
    d = 0;
    return;
  }

  double aa = cos(angle)/scale;
  double bb = sin(angle)/scale;

  double u,v,xx,yy;

  u = x - xcen;
  v = y - ycen;
  xx = (aa*u - bb*v)*reflex;
  yy =  bb*u + aa*v;

  ignomon(xx,yy,a,d);
  a = RAD*a;
  d = RAD*d;
}


void FitsProjection::gnomon(double a,double d,double& x,double& y) const
{
  double c,p,q,r,v,w,s;

  c = cos(d);
  p = sin(d);
  q = c*sin(a - acen);
  r = c*cos(a - acen);
  v = sin(dcen);
  w = cos(dcen);
  s = p*v + r*w;

  x = -q/s;
  y = (w*p - v*r)/s;
}

void FitsProjection::ignomon(double x,double y,double& a,double& d) const
{
  double p,q,r,v,w,t;

  v = sin(dcen);
  w = cos(dcen);
  t = sqrt(1.0 + x*x + y*y);
  p = (v + w*y)/t;
  q = -x/t;
  r = (w - v*y)/t;

  d = asin(p);
  a = atan2(q,r) + acen;
}

double FitsProjection::xy2dist(double x1,double y1,double x2,double y2) const
{
  // https://en.wikipedia.org/wiki/Haversine_formula
  double r1,d1,r2,d2;
  xy2ad(x1,y1,r1,d1);
  xy2ad(x2,y2,r2,d2);

  double sind = sin((d2 - d1)/2/RAD);
  double sinr = sin((r2 - r1)/2/RAD);
  double h = sind*sind + cos(d1/RAD)*cos(d2/RAD)*sinr*sinr;
  return h > 0 ? 2*RAD*asin(sqrt(h)) : 0;
}

// -----------------------------------------------------------------


FitsCoo::FitsCoo(): FitsArray(),type(COO_PIXEL),haswcs(false),digits(4) {}

FitsCoo::FitsCoo(const FitsArray& a):
  FitsArray(a),type(COO_PIXEL),haswcs(false),digits(4)
{
  // decode WCS

  if( a.GetKey("CTYPE1").Find("TAN") != wxNOT_FOUND &&
      a.GetKey("CTYPE2").Find("TAN") != wxNOT_FOUND ) {

    double crpix1 = a.GetKeyDouble("CRPIX1");
    double crpix2 = a.GetKeyDouble("CRPIX2");
    double crval1 = a.GetKeyDouble("CRVAL1");
    double crval2 = a.GetKeyDouble("CRVAL2");
    double cd1_1 = a.GetKeyDouble("CD1_1");
    double cd1_2 = a.GetKeyDouble("CD1_2");
    double cd2_1 = a.GetKeyDouble("CD2_1");
    double cd2_2 = a.GetKeyDouble("CD2_2");
    proj = FitsProjection("GNONOMIC",crval1,crval2,crpix1,crpix2,
			  cd1_1,cd1_2,cd2_1,cd2_2);

    if( proj.IsOk() )
      // significant digits for the print format are determined by scale
      digits = wxMax(int(log10(proj.GetScale()/RAD) + 1),0);

    haswcs = proj.IsOk();
  }

}

bool FitsCoo::HasWCS() const
{
  return haswcs;
}

void FitsCoo::SetType(int t)
{
  type = static_cast<coords_type>(t);
}

void FitsCoo::SetType(const wxString& a)
{
  for(int i = COO_FIRST+1; i < COO_LAST; i++)
    if( a == Label_str(i) ) {
      SetType(i);
      return;
    }
}

coords_type FitsCoo::GetType() const
{
  return type;
}

void FitsCoo::GetEq(int xcen, int ycen, double& alpha, double& delta) const
{
  proj.xy2ad(xcen,ycen,alpha,delta);
}


void FitsCoo::RaSix(double alpha, int& h, int& min, double& sec) const
{
  double x = alpha/15.0;
  h = int(x);
  min = int(60.0*(x - h));
  sec = 3600.0*(x - (h + min/60.0));
}

void FitsCoo::DecSix(double delta, char& sign, int& d, int& m, double& s) const
{
  sign = delta > 0.0 ? '+' : '-';

  double y = fabs(delta);
  d = int(y);
  m = int(60.0*(y - d));
  s = 3600.0*(y - (d + m/60.0));
}


void FitsCoo::GetPix(int i, int j, wxString& a, wxString& b) const
{
  if( 0 <= i && i < Width() && 0 <= j && j < Height() ) {
    a.Printf("%d",i+1); // C-style indexing should be considered very funny!
    b.Printf("%d",j+1);
  }
  else {
    a.Clear();
    b.Clear();
  }
}

void FitsCoo::GetCoo(int i, int j, wxString& a, wxString& b) const
{
  if( haswcs && 0 <= i && i < Width() && 0 <= j && j < Height() ) {

    int h,min,d,m;
    double x,y, sec,s;
    char sg;

    GetEq(i,j,x,y);

    if( type == COO_EQDEG ) {
      a.Printf(L"%.*f°",digits,x);
      b.Printf(L"%.*f°",digits,y);
    }

    else if( type == COO_EQSIX ) {
      int n = wxMax(digits-4,0);
      RaSix(x,h,min,sec);
      DecSix(y,sg,d,m,s);
      a.Printf(L"%02dh%02dm%04.*fs",h,min,n+1,sec);
      b.Printf(L"%c%02d°%02d'%02.*f\"",sg,d,m,n,s);
    }
  }
  else {
    a.Clear();
    b.Clear();
  }
}

wxString FitsCoo::Label_str(int n)
{
  switch(n){
  case COO_PIXEL: return  "pixels";
  case COO_EQDEG: return L"Equatorial (°)";
  case COO_EQSIX: return  "Equatorial (h:m:s)";
  default:        return  "";
  }
}

wxArrayString FitsCoo::Label_str()
{
  wxArrayString a;

  for(int i = COO_FIRST+1; i < COO_LAST; i++)
    a.Add(Label_str(i));

  return a;
}