File: value.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 (403 lines) | stat: -rw-r--r-- 9,212 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
   xmunipack - value units (intensity, flux, etc..) conversions

   Copyright © 1997-2013, 2019-22 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/>.

*/

/*

   This routine represents physical value intensity and provides
   its represenattion in various physical units.

   Currently only HST magnitude - photometry convention is used.

*/


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

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


using namespace std;

FitsValue::FitsValue(): type(UNIT_COUNT),hascal(false) {}

FitsValue::FitsValue(const FitsArray& a, const wxString& phsystemfile,
		     const wxString& f1, const wxString& f2,
		     const wxString& f3):
  type(UNIT_COUNT),hascal(false),array(a),fits_key_area(f1),
  fits_key_exptime(f2), fits_key_filter(f3)
{
  hascal = Init();
  if( hascal )
    Init_phsystem(phsystemfile);

  hascal = ! phconv.empty(); // photosystem is missing or an unknown
}

void FitsValue::Init_phsystem(const wxString& phsystemfile)
{
  phsystems = FitsPhotosystems(phsystemfile);
  if( phsystems.IsOk() ) {
    for(size_t i = 0; i < filter.size(); i++) {

      PhotoFilter phfilter = phsystems.GetFilter(photsys,filter);
      if( phfilter.IsOk() )
	phconv.push_back(PhotoConv(phfilter,area,exptime,scale));
    }
  }
}


bool FitsValue::Init()
{
  double sc11, sc12;

  area = GetKeyDouble(array,fits_key_area);
  exptime = GetKeyDouble(array,fits_key_exptime);
  sc11 = GetKeyDouble(array,"CD1_1");
  sc12 = GetKeyDouble(array,"CD1_2");
  scale = 3600.0*sqrt(sc11*sc11 + sc12*sc12);

  filter = array.GetKey(fits_key_filter);
  photsys = array.GetKey("PHOTSYS");

  return ! photsys.IsEmpty();
}

double FitsValue::GetKeyDouble(const FitsHdu& a, const wxString& key) const
{
  if( ! key.IsEmpty() ) {
    wxString s = a.GetKey(key);

    double x;
    if( !s.IsEmpty() &&  s.ToDouble(&x) )
      return x;
  }
  return 1.0;
}


void FitsValue::SetType(int t)
{
  if( hascal ) {
    if( UNIT_FIRST + 1 < t && t < UNIT_LAST )
      type = static_cast<units_type>(t);
    else
      type = UNIT_PHOTON;
  }
  else {
    if( UNIT_FIRST < t && t < UNIT_PHOTON )
      type = static_cast<units_type>(t);
    else
      type = UNIT_COUNT;
  }
}

void FitsValue::SetType(const wxString& a)
{
  for(int i = UNIT_FIRST+1; i < UNIT_LAST; i++)
    if( a == Label_str(i) ) {
      SetType(i);
      return;
    }
}


units_type FitsValue::GetType() const
{
  return type;
}

wxString FitsValue::GetName() const
{
  return Label_str(type);
}


wxString FitsValue::GetUnit() const
{
  return Units_str(type);
}

wxString FitsValue::ToString(double d, int k) const
{
  wxString a;

  if( type == UNIT_PHOTON || type == UNIT_COUNT ) {
    if( array.Bitpix() > 0 )
      a.Printf("%d",int(d+0.5));
    else
      a = HumanFormat(d);
  }

  if( hascal ) {

    if( type == UNIT_MAG )

      a.Printf("%.5g",phconv[k].GetMag(d));

    else if( type == UNIT_INTENSITY )

      a = HumanFormat(phconv[k].GetIntensity(d));

  }

  return a;
}

wxString FitsValue::Get_str(int i, int j) const
{
  wxASSERT(array.IsOk() && array.Naxis() == 2);
  if( 0 <= i && i < array.Width() && 0 <= j && j < array.Height() ) {
    double c = array.Pixel(i,j);
    return ToString(c,0);
  }
  return "";
}

vector<wxString> FitsValue::Get_str(int i, int j, const vector<int>& z) const
{
  wxASSERT(array.IsOk() && array.Naxis() == 3);

  vector<wxString> labels;
  if( 0 <= i && i < array.Width() && 0 <= j && j < array.Height() &&
      z.size() == 3 && array.IsColour() ) {

    float X = array.Pixel(i,j,2);
    float Y = array.Pixel(i,j,1);
    float Z = array.Pixel(i,j,0);

    float *d[3] = { &X, &Y, &Z };

    for(size_t i = 0; i < 3; i++) {
      double q = *d[i];
      wxString a;
      a.Printf(fabs(q) < 1e5 ? "% 7.1f" : "%.2e",q);
      labels.push_back(a);
    }
    return labels;
  }
  else {
    vector<wxString> labels = { "", "", ""};
    return labels;
  }
}

wxString FitsValue::Label_str(int n)
{
  switch(n){
  case PHQUANTITY_COUNT:     return "Count";
  case PHQUANTITY_PHOTON:    return "Photon";
  case PHQUANTITY_MAG:       return "Magnitude";
  case PHQUANTITY_INTENSITY: return "Intensity";
  default:                   return  "???";
  }
}

wxArrayString FitsValue::Label_str()
{
  wxArrayString a;

  for(int i = PHQUANTITY_FIRST+1; i < PHQUANTITY_LAST; i++)
    a.Add(Label_str(i));

  return a;
}

wxString FitsValue::Units_str(int n)
{
  switch(n){
  case UNIT_COUNT:     return  "";
  case UNIT_PHOTON:    return  "";
  case UNIT_MAG:       return L"mag/arcsec²";
  case UNIT_INTENSITY: return L"eV/s/m²/arcsec²";
  default:             return  "";
  }
}

wxArrayString FitsValue::Units_str()
{
  wxArrayString a;

  for(int i = UNIT_FIRST+1; i < UNIT_LAST; i++)
    a.Add(Units_str(i));

  return a;
}


// ----- Photometric Systems

const double PhotoConv::cspeed = 299792458.0;
const double PhotoConv::hplanck = 6.62606957e-34;
const double PhotoConv::evolt = 1.6021766208e-19;  // = 1eV
const double PhotoConv::sqrtpi2 = 1.2533141373155001; // = sqrt(pi/2)

PhotoConv::PhotoConv(const PhotoFilter& phfilter, double a, double e, double s):
  area(a), exptime(e), scale(s)
{
  flam = phfilter.flam;
  leff = phfilter.leff;
  lwidth = phfilter.lwidth;
}

double PhotoConv::intensity(double photon) const
{
  double phi = photon / (area * exptime * scale * scale);
  return phi * hplanck * cspeed / leff;
}

double PhotoConv::mag(double f, double f0) const
{
  if( f0 <= 0.0 || f <= 0.0) return 99.999;
  double x = f / f0;
  return -2.5*log10(x);
}

double PhotoConv::GetIntensity(double photon) const
{
  return intensity(photon) / evolt;
}

double PhotoConv::GetMag(double photon) const
{
  return mag(intensity(photon), sqrtpi2 * flam * (1e9*lwidth));
}


FitsPhotosystems::FitsPhotosystems(const wxString& name)
{
  fitsfile *f;
  int status = 0;
  float nullval = 0.0;
  int dummy,htype;
  char extname[FLEN_CARD];//,comment[FLEN_CARD];
  const char *cols[] = {"FILTER","LAM_EFF","LAM_FWHM","FLAM_REF"};
  int col[4];

  int nhdu = 0;

  // open file
  status = 0;
  fits_open_file(&f, name.fn_str(), READONLY, &status);
  fits_get_num_hdus(f,&nhdu,&status);
  if( status ) goto crash;

  for(int k = 1; k < nhdu; k++) { // the first HDU is skipped

    fits_movabs_hdu(f,k+1,&htype,&status);
    if( status ) goto crash;

    fits_read_key(f,TSTRING,"EXTNAME",extname,NULL,&status);

    //    fits_read_keyword(f,"EXTNAME",extname,comment,&status);
    // removing aphostrophes
    /*
    wxString e(extname);
    wxString e1 = e.SubString(1,e.Len()-2);
    e = e1.Trim();
    strcpy(extname,e.ToAscii());
    */

    if( htype == BINARY_TBL ) {

      long nrows;
      int ncols;
      fits_get_num_rows(f,&nrows,&status);
      fits_get_num_cols(f,&ncols,&status);
      if( status ) goto crash;

      for(int i = 0; i < 4; i++)
	fits_get_colnum(f,CASESEN,(char*)cols[i],&col[i],&status);

      double *leff = new double[nrows];
      double *lwidth = new double[nrows];
      double *flam = new double[nrows];

      long frow = 1, felem = 1;

      int width;
      fits_get_col_display_width(f,col[0],&width,&status);
      char **fs = new char*[nrows];
      for(int i = 0; i < nrows; i++)
	fs[i] = new char[width];
      fits_read_col(f,TSTRING,col[0], frow, felem, nrows, &nullval,fs,
		    &dummy, &status);
      fits_read_col(f,TDOUBLE,col[1], frow, felem, nrows, &nullval,leff,
		    &dummy, &status);
      fits_read_col(f,TDOUBLE,col[2], frow, felem, nrows, &nullval,lwidth,
		    &dummy,&status);
      fits_read_col(f,TDOUBLE,col[3], frow, felem, nrows, &nullval,flam,
		    &dummy,&status);
      if( status ) goto crash;

      vector<PhotoFilter> phfs;
      for(int i = 0; i < nrows; i++)
	phfs.push_back(PhotoFilter(fs[i],leff[i],lwidth[i],flam[i]));

      phsystems.push_back(Photosys(extname,phfs));

      delete[] leff;
      delete[] lwidth;
      delete[] flam;
      for(int i = 0; i < nrows; i++)
	delete[] fs[i];
      delete[] fs;

    }
  }

  fits_close_file(f, &status);

 crash:

  fits_report_error(stderr,status);
}

bool FitsPhotosystems::IsOk() const
{
  return ! phsystems.empty();
}

PhotoFilter FitsPhotosystems::GetFilter(const wxString& name,
					const wxString& filter) const
{
  for(size_t i = 0; i < phsystems.size(); i++) {
    if( phsystems[i].GetName() == name ) {
      return phsystems[i].GetFilter(filter);
    }
  }
  return PhotoFilter();
}


PhotoFilter Photosys::GetFilter(const wxString& filter) const
{
  for(size_t i = 0; i < filters.size(); i++) {
    if( filters[i].name == filter ) {
      return filters[i];
    }
  }
  return PhotoFilter();
}