File: fitsmeta.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 (407 lines) | stat: -rw-r--r-- 8,136 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
404
405
406
407
/*

  xmunipack - fits meta-info


  Copyright © 2009-2011, 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 <fitsio.h>
#include <math.h>
#include <vector>
#include <wx/wx.h>
#include <wx/uri.h>
#include <wx/filesys.h>
#include <wx/tokenzr.h>
#include <cstdio>
#include <cstring>

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


// --------  FitsMetaHdu

// by single HDU
FitsMetaHdu::FitsMetaHdu(const FitsHeader& head, int t):
  FitsHeader(head),htype(t),type(head.GetType(htype)),
  subtype(head.GetFlavour(htype,type)),nrows(-1),ncols(-1) {}

FitsMetaHdu::FitsMetaHdu(const FitsHeader& head, int t, int maxis,const long *maxes):
  FitsHeader(head),htype(t),type(head.GetType(htype)),
  subtype(head.GetFlavour(htype,type)), nrows(-1),ncols(-1)
{
  for(int i = 0; i < maxis; i++)
    naxes.push_back(maxes[i]);
}

FitsMetaHdu::FitsMetaHdu(const FitsHeader& head, int ht, long nr, int nc):
  FitsHeader(head),htype(ht), type(head.GetType(htype)),
  subtype(head.GetFlavour(htype,type)), nrows(nr),ncols(nc) {}

// by full FITS file
FitsMetaHdu::FitsMetaHdu(const FitsHdu& hdu, const wxImage& ico):
  FitsHeader(hdu.GetHeader()), htype(hdu.GetHduType()),
  type(hdu.Type()),subtype(hdu.Flavour()), nrows(0),ncols(0),icon(ico)
{
  for(size_t i = 0; i < hdu.GetCount(); i++)
    Add(hdu.Item(i));

  if( type == HDU_IMAGE ) {
    const FitsArray array(hdu);
    for(int i = 0; i < array.Naxis(); i++)
      naxes.push_back(array.Naxes(i));
  }
  else if( type == HDU_TABLE ) {
    const FitsTable table(hdu);
    ncols = table.Width();
    nrows = table.Height();
  }
}

size_t FitsMetaHdu::Naxis() const
{
  return naxes.size();
}

long FitsMetaHdu::Naxes(size_t n) const
{
  if( 0 <= n && n < naxes.size() )
    return naxes[n];
  else
    return 0;
}

std::vector<long> FitsMetaHdu::GetNaxes() const
{
  return naxes;
}

long FitsMetaHdu::Width() const
{
  return Naxes(0);
}

long FitsMetaHdu::Height() const
{
  return Naxes(1);
}

long FitsMetaHdu::Nrows() const
{
  return nrows;
}

int FitsMetaHdu::Ncols() const
{
  return ncols;
}

hdu_type FitsMetaHdu::Type() const
{
  return type;
}

hdu_flavour FitsMetaHdu::SubType() const
{
  return subtype;
}


wxString FitsMetaHdu::Type_str() const
{
  return GetType_str(type);
}

wxString FitsMetaHdu::SubType_str() const
{
  return GetFlavour_str(subtype);
}

wxImage FitsMetaHdu::GetIcon() const
{
  wxASSERT(icon.IsOk());
  return icon;
}

void FitsMetaHdu::SetIcon(const wxImage& i)
{
  wxASSERT(i.IsOk());
  icon = i;
}

wxString FitsMetaHdu::GetControlLabel() const
{
  wxString label = GetKey("EXTNAME");
  if( label.IsEmpty() )
    label = Type_str();
  return label;
}


// ------- FitsMeta


FitsMeta::FitsMeta():status(1),type(FITS_UNKNOWN),size(wxInvalidSize) {}

FitsMeta::FitsMeta(const wxString& name):
  url(name),status(1),type(FITS_UNKNOWN),size(wxInvalidSize)
{
  wxFileName fn(wxFileSystem::URLToFileName(url));
  size = fn.GetSize();
}

FitsMeta::FitsMeta(const FitsFile& fits, const wxImage& ico,
		   const std::vector<wxImage>& list):
  url(fits.GetURL()),status(0),type_str(fits.Type_str()),type(fits.Type()),
  icon(ico), size(wxInvalidSize)
{
  wxASSERT(fits.Status() && list.size() == fits.HduCount());

  for(size_t k = 0; k < fits.HduCount(); k++)
    hdu.push_back(FitsMetaHdu(fits.Hdu(k),list[k]));

  wxFileName fn(wxFileSystem::URLToFileName(url));
  size = fn.GetSize();
}


FitsMeta::FitsMeta(const wxString& u, const std::vector<FitsMetaHdu>& h, wxULongLong s):
  url(u),status(0),type(FITS_UNKNOWN),hdu(h), size(s)
{
  if( hdu.size() == 1 && hdu[0].Type() == HDU_IMAGE ) {

    if( hdu[0].SubType() == HDU_IMAGE_FRAME ) {
      // simple, grayscale image
      type = FITS_GREY;
      type_str = "Grey image";
    }
    else  if( hdu[0].SubType() == HDU_IMAGE_COLOUR ) {
      // colour image
      type = FITS_COLOUR;
      type_str = "Colour image";
    }
  }
  else {
    // more hdus
    type = FITS_MULTI;
    type_str = "Multi-extension";
  }
}


wxImage FitsMeta::GetIcon() const
{
  return icon;
}

void FitsMeta::SetIcon(const wxImage& i)
{
  wxASSERT(i.IsOk());
  icon = i;
}

void FitsMeta::SetIconList(const std::vector<wxImage>& ilist)
{
  wxASSERT(ilist.size() == hdu.size());
  for(size_t k = 0; k < hdu.size(); k++)
    hdu[k].SetIcon(ilist[k]);
}

size_t FitsMeta::HduCount() const
{
  return hdu.size();
}

FitsMetaHdu FitsMeta::Hdu(size_t n) const
{
  wxASSERT(0 <= n && n < hdu.size() );
  return hdu[n];
}

FitsMetaHdu *FitsMeta::GetHdu(size_t n)
{
  wxASSERT(0 <= n && n < hdu.size() );
  return &hdu[n];
}

int FitsMeta::Type() const
{
  return type;
}

wxString FitsMeta::Type_str() const
{
  return type_str;
}


wxString FitsMeta::Mtime() const
{
  wxURI uri(url);
  wxFileName name(wxURI::Unescape(uri.GetPath()));

  wxDateTime t = name.GetModificationTime();
  if( t.IsValid() )
    return t.FormatDate()+" "+t.FormatTime();
  else
    return wxEmptyString;
}


wxString FitsMeta::GetKeys(const wxString& key) const
{
  wxString a;
  for(size_t k = 0; k < hdu.size(); k++) {
    wxString l = hdu[k].GetKey(key);
    if( a.IsEmpty() )
      a = l;
    else
      a += ";" + l;
  }
  return a;
}

wxString FitsMeta::GetDateobs(const wxString& key) const
{
  return GetKeys(key);
}

wxString FitsMeta::GetDate(const wxString& key) const
{
  FitsTime dateobs(GetKeys(key));
  return dateobs.GetDate();
}

wxString FitsMeta::GetTime(const wxString& key) const
{
  FitsTime dateobs(GetKeys(key));
  return dateobs.GetTime();
}

wxString FitsMeta::GetFilter(const wxString& key) const
{
  return type == FITS_COLOUR ? "XYZ" : GetKeys(key);
}

wxString FitsMeta::GetExposure(const wxString& key) const
{
  const wchar_t *prefin[] = { L"",L"m",L"μ",L"n",L"p",L"f",L"a",L"z",L"y" };
  const wchar_t *prefix[] = { L"",L"k",L"M",L"G",L"T",L"P",L"E",L"Z",L"Y" };
  const wxString fmt = "%g%ss";

  wxString line;
  wxStringTokenizer tokenizer(GetKeys(key),";");
  while ( tokenizer.HasMoreTokens() ) {

    if( line != "" ) line += ";";
    wxString l = tokenizer.GetNextToken();
    double e;
    if( ! l.IsEmpty() && l.ToDouble(&e) ) {
      if( e > 0 ) {
	wxString a;
	double d = log10(e);
	int thorder = d >= 0 ?  d / 3 : (d - 1) / 3;
	double q = e / pow(10.0,3*thorder);
	wxString text;
	if( d >= 0 ) {
	  wxASSERT(0 <= thorder && thorder < 9);
	  text.Printf(fmt,q,prefix[thorder]);
	}
	else {
	  wxASSERT(0 <= -thorder && -thorder < 9);
	  text.Printf(fmt,q,prefin[-thorder]);
	}
	line += text;
      }
      else
	line += "0s";
    }
    else
      line += l;
  }
  return line;
}

wxString FitsMeta::GetURL() const
{
  return url;
}

void FitsMeta::SetURL(const wxString& a)
{
  url = a;
}

bool FitsMeta::IsOk() const
{
  return status == 0;
}


wxString FitsMeta::GetName() const
{
  wxURI uri(url);
  wxFileName name(uri.GetPath());
  return name.GetName();
}

wxString FitsMeta::GetPath() const
{
  wxURI uri(url);
  wxFileName name(uri.GetPath());
  return name.GetPath();
}

wxString FitsMeta::GetFullPath() const
{
  wxURI uri(url);
  wxFileName name(uri.GetPath());
  return name.GetFullPath();
}

wxString FitsMeta::GetFullName() const
{
  wxURI uri(url);
  wxFileName name(uri.GetPath());
  return name.GetFullName();
}

wxString FitsMeta::GetHumanReadableSize() const
{
  return wxFileName::GetHumanReadableSize(size);
}


wxULongLong FitsMeta::GetSize() const
{
  return size;
}

void FitsMeta::Clear()
{
  url.Clear();
  type_str.Clear();
  type = FITS_UNKNOWN;
  hdu.clear();
  icon = wxImage();
  size = wxInvalidSize;
}