File: selectsrc.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 (380 lines) | stat: -rw-r--r-- 10,309 bytes parent folder | download | duplicates (5)
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
/*

  xmunipack - select astrometry source

  Copyright © 2012-3 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 "xmunipack.h"
#include <wx/wx.h>
#include <wx/statline.h>
#include <wx/regex.h>
#include <wx/utils.h>
#include <wx/filefn.h> 
#include <wx/listbook.h>
#include <cfloat>

using namespace std;



MuniSelectSource::MuniSelectSource(wxWindow *w, MuniConfig *c, bool t):
  wxDialog(w,wxID_ANY,"Coordinate Source",wxDefaultPosition,wxDefaultSize,
	   wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER),
  config(c), page(0), xframe(t), astrorel(false)
{
  SetIcon(config->munipack_icon);
  CreateControls();  
}

MuniSelectSource::~MuniSelectSource()
{
  EraseTemp();
}

void MuniSelectSource::CreateControls()
{
  wxSizerFlags lf;
  lf.Align(wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT);

  wxFont sf(*wxSMALL_FONT);

  const wxSize imageSize(24, 24);
  wxImageList *ilist = new wxImageList(imageSize.GetWidth(), imageSize.GetHeight());
  ilist->Add(wxArtProvider::GetIcon(wxART_GO_DOWN,wxART_TOOLBAR,imageSize));
  ilist->Add(wxArtProvider::GetIcon(wxART_FILE_OPEN,wxART_TOOLBAR,imageSize));

  // title
  wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);

  wxListbook *book = new wxListbook(this,wxID_ANY);
  book->AssignImageList(ilist);

  
  wxPanel *cpanel = new wxPanel(book);
  wxBoxSizer *csizer = new wxBoxSizer(wxVERTICAL);

  wxFlexGridSizer *grid = new wxFlexGridSizer(2);
  
  wxStaticText *vlabel = new wxStaticText(cpanel,wxID_ANY,"Virtual Observatory:");
  grid->Add(vlabel,lf);
  
  wxButton *vobutt = new wxButton(cpanel,wxID_ANY,"Search Catalogues");
  grid->Add(vobutt,wxSizerFlags().Border().Left());
  
  wxStaticText *flabel = new wxStaticText(cpanel,wxID_ANY,"File:");
  grid->Add(flabel,lf);
  wxFilePickerCtrl *cpick = new wxFilePickerCtrl(cpanel,wxID_ANY,"","Select A File",
			       "FITS files "+config->dirmask+")|"+
			       config->dirmask+"| All files (*)|*");
  grid->Add(cpick,wxSizerFlags().Expand());
  csizer->Add(grid,wxSizerFlags().Border().Center());

  csizer->Add(new wxStaticLine(cpanel,wxID_ANY),wxSizerFlags().Border().Expand());

  wxFlexGridSizer *s = new wxFlexGridSizer(4);
  s->AddGrowableCol(1);
  s->AddGrowableCol(3);

  s->Add(new wxStaticText(cpanel,wxID_ANY,L"Label α:"),lf);
  choice_ra = new wxChoice(cpanel,ID_ASTRO_CH_RA);
  s->Add(choice_ra,wxSizerFlags().DoubleBorder(wxRIGHT));
  s->Add(new wxStaticText(cpanel,wxID_ANY,L"Label δ:"),lf);
  choice_dec = new wxChoice(cpanel,ID_ASTRO_CH_DEC);
  s->Add(choice_dec);
  s->Add(new wxStaticText(cpanel,wxID_ANY,L"Label μ(α):"),lf);
  choice_pmra = new wxChoice(cpanel,ID_ASTRO_CH_PMRA);
  s->Add(choice_pmra,wxSizerFlags().DoubleBorder(wxRIGHT));
  s->Add(new wxStaticText(cpanel,wxID_ANY,L"Label μ(δ):"),lf);
  choice_pmdec = new wxChoice(cpanel,ID_ASTRO_CH_PMDEC);
  s->Add(choice_pmdec);

  s->Add(new wxStaticText(cpanel,wxID_ANY,"Label mag:"),lf);
  choice_mag = new wxChoice(cpanel,ID_ASTRO_CH_MAG);
  s->Add(choice_mag,wxSizerFlags().Border(wxTOP));

  csizer->Add(s,wxSizerFlags().Border().Center());
  cpanel->SetSizer(csizer);
  book->AddPage(cpanel,"Catalogue",false,0);

  Bind(wxEVT_COMMAND_FILEPICKER_CHANGED,&MuniSelectSource::OnCatFile,this,cpick->GetId());
  Bind(wxEVT_COMMAND_BUTTON_CLICKED,&MuniSelectSource::OnCatVO,this,vobutt->GetId());
  Bind(wxEVT_COMMAND_CHOICE_SELECTED,&MuniSelectSource::OnChoice,this,ID_ASTRO_CH_RA);
  Bind(wxEVT_COMMAND_CHOICE_SELECTED,&MuniSelectSource::OnChoice,this,ID_ASTRO_CH_DEC);
  Bind(wxEVT_COMMAND_CHOICE_SELECTED,&MuniSelectSource::OnChoice,this,ID_ASTRO_CH_PMRA);
  Bind(wxEVT_COMMAND_CHOICE_SELECTED,&MuniSelectSource::OnChoice,this,ID_ASTRO_CH_PMDEC);
  Bind(wxEVT_COMMAND_CHOICE_SELECTED,&MuniSelectSource::OnChoice,this,ID_ASTRO_CH_MAG);


  if( xframe ) {

    wxPanel *rpanel = new wxPanel(book);
    wxBoxSizer *rsizer = new wxBoxSizer(wxVERTICAL);
    
    wxBoxSizer *ffsizer = new wxBoxSizer(wxHORIZONTAL);
    flabel = new wxStaticText(rpanel,wxID_ANY,"File:");
    ffsizer->Add(flabel,lf);

    wxFilePickerCtrl *rpick = new wxFilePickerCtrl(rpanel,wxID_ANY,"","Select A File",
						   "FITS files "+config->dirmask+")|"+
						   config->dirmask+"| All files (*)|*");
    ffsizer->Add(rpick,wxSizerFlags(1));
    rsizer->Add(ffsizer,wxSizerFlags().Expand().DoubleBorder());
    
    wxCheckBox *check_rel = new wxCheckBox(rpanel,wxID_ANY,"Relative");
    rsizer->Add(check_rel,wxSizerFlags().Border().Center());

    rpanel->SetSizer(rsizer);

    book->AddPage(rpanel,"Frame",false,1);

    Bind(wxEVT_COMMAND_FILEPICKER_CHANGED,&MuniSelectSource::OnRefFile,this,rpick->GetId());
    Bind(wxEVT_COMMAND_CHECKBOX_CLICKED,&MuniSelectSource::OnCheckRel,this,check_rel->GetId());

  }

  topsizer->Add(book,wxSizerFlags(1).Expand().Border());

  wxStdDialogButtonSizer *buttsize = 
    static_cast<wxStdDialogButtonSizer *>(CreateButtonSizer(wxOK|wxCANCEL));
  if( buttsize )
    topsizer->Add(buttsize,wxSizerFlags().Expand().Border());

  SetSizerAndFit(topsizer);

  Bind(wxEVT_UPDATE_UI,&MuniSelectSource::OnUpdateUI,this);
  Bind(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED,&MuniSelectSource::OnBookChange,this,book->GetId());

}

void MuniSelectSource::EraseTemp()
{
  if( !tmpcatfile.IsEmpty() && wxFileExists(tmpcatfile) ) 
    wxRemoveFile(tmpcatfile);
  tmpcatfile.Clear();
}

void MuniSelectSource::OnUpdateUI(wxUpdateUIEvent& event)
{
  bool def = ! catfile.IsEmpty() || ! reffile.IsEmpty() || ! tmpcatfile.IsEmpty();
      
  wxASSERT(FindWindow(wxID_OK));
  FindWindow(wxID_OK)->Enable(def);
}

wxString MuniSelectSource::GetPath() const
{
  if( page == 0 ) {
    if( !catfile.IsEmpty() )
      return catfile;
    else if( !tmpcatfile.IsEmpty() )
      return tmpcatfile;
  }
  else if( page == 1 )
    return reffile;

  return wxEmptyString;
}

wxString MuniSelectSource::GetId() const
{
  return idlabel;
}

int MuniSelectSource::GetType() const
{
  switch (page) {
  case 0: return ID_ASTRO_CAT;
  case 1: return ID_ASTRO_REF;
  }
  return 0;
}

bool MuniSelectSource::IsTemporary() const
{
  return !tmpcatfile.IsEmpty();
}

bool MuniSelectSource::GetRelative() const
{
  return astrorel;
}

wxString MuniSelectSource::GetLabelRA() const
{
  return label_ra;
}

wxString MuniSelectSource::GetLabelDec() const
{
  return label_dec;
}

wxString MuniSelectSource::GetLabelPMRA() const
{
  return label_pmra;
}

wxString MuniSelectSource::GetLabelPMDec() const
{
  return label_pmdec;
}

wxString MuniSelectSource::GetLabelMag() const
{
  return label_mag;
}

void MuniSelectSource::OnCheckRel(wxCommandEvent& event)
{
  astrorel = event.IsChecked();
}

void MuniSelectSource::OnRefFile(wxFileDirPickerEvent& event)
{
  bool ext = false;

  FitsFile fits(event.GetPath());
  for(size_t i = 0; i < fits.HduCount(); i++) {
    if( fits.Hdu(i).GetExtname() == APEREXTNAME )
      ext = true;
  }
   
  if( ext ) {
    EraseTemp();
    idlabel = fits.GetName();
    Layout();
    Fit();
    catfile.Clear();
    reffile = event.GetPath();
  }
  else {
    wxLogMessage("MUNIPACK extension with detected sources not found.");
    wxASSERT(FindWindow(event.GetId()));
    static_cast<wxFilePickerCtrl *>(FindWindow(event.GetId()))->SetPath("");
    idlabel.Clear();
  }
}

void MuniSelectSource::OnCatFile(wxFileDirPickerEvent& event)
{
  wxArrayString cols;

  if( CheckCatalogue(event.GetPath(),cols) ) {
    
    catfile = event.GetPath();
    reffile.Clear();
    EraseTemp();

    SetLabels(cols);
  }
  Layout();
  Fit();
}

void MuniSelectSource::OnCatVO(wxCommandEvent& event)
{

  MuniCone cone(this,config);
  if( cone.ShowModal() == wxID_OK ) {

    wxArrayString cols;
    if( CheckCatalogue(cone.GetPath(),cols) ) {

      wxString xfile = wxFileName::CreateTempFileName("xmunipack-astrometer-search_");

      if( wxCopyFile(cone.GetPath(),xfile) ) {

	catfile.Clear();
        reffile.Clear();
	EraseTemp();	    

	tmpcatfile = xfile;

	SetLabels(cols);
	return;
      }
    }
  }
  idlabel.Clear();
}

void MuniSelectSource::SetLabels(const wxArrayString& cols)
{
  choice_ra->Set(cols);
  choice_dec->Set(cols);
  choice_pmra->Set(cols);
  choice_pmdec->Set(cols);
  choice_mag->Set(cols);

  VOCatConf catalogs;
  VOCatResources cat(catalogs.GetCat());

  if( cat.IsOk() ) {
  
    label_ra = cat.GetLabel("POS_EQ_RA");
    label_dec = cat.GetLabel("POS_EQ_DEC");
    label_pmra = cat.GetLabel("POS_EQ_PMRA");
    label_pmdec = cat.GetLabel("POS_EQ_PMDEC");
    label_mag = cat.GetSort();

    choice_ra->SetSelection(choice_ra->FindString(label_ra));
    choice_dec->SetSelection(choice_dec->FindString(label_dec));
    choice_pmra->SetSelection(choice_ra->FindString(label_pmra));
    choice_pmdec->SetSelection(choice_dec->FindString(label_pmdec));
    choice_mag->SetSelection(choice_mag->FindString(label_mag));
  }
}


bool MuniSelectSource::CheckCatalogue(const wxString& filename, wxArrayString& cols)
{
  FitsFile fits(filename);

  if( fits.IsOk() ) {
    for(size_t i = 0; i < fits.HduCount(); i++) {
      if( fits.Hdu(i).Type() == HDU_TABLE ) {
	cols = static_cast<FitsTable>(fits.Hdu(i)).GetColLabels();
	idlabel = fits.Hdu(i).GetExtname();
	return true;
      }
    }
  }
  return false;
}

void MuniSelectSource::OnChoice(wxCommandEvent& event)
{
  if( event.GetId() == ID_ASTRO_CH_RA ) 
    label_ra = event.GetString();
  if( event.GetId() == ID_ASTRO_CH_DEC ) 
    label_dec = event.GetString();
  if( event.GetId() == ID_ASTRO_CH_PMRA ) 
    label_pmra = event.GetString();
  if( event.GetId() == ID_ASTRO_CH_PMDEC ) 
    label_pmdec = event.GetString();
  if( event.GetId() == ID_ASTRO_CH_MAG )
    label_mag = event.GetString();
}

void MuniSelectSource::OnBookChange(wxBookCtrlEvent& event)
{
  page = event.GetSelection();
}