File: cone.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-- 11,072 bytes parent folder | download | duplicates (3)
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
/*

  Virtual Observatory capable cone search

  Copyright © 2010 - 2014, 2018-9 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 "votable.h"
#include "vocatconf.h"
#include "voclient.h"
#include <wx/wx.h>
#include <wx/app.h>
#include <wx/url.h>
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#include <wx/filename.h>
#include <wx/filefn.h>
#include <fitsio.h>
#include <list>
#include <cstdio>
#include <cmath>


using namespace std;


class Cone: public wxAppConsole
{
  bool PatchJohn(const wxString&);
  void PipePrint(const wxString&);
  void Stop(const wxString&);
  wxString tmpcone;

public:
  bool OnInit();
  int OnRun();
  int OnExit();

};
IMPLEMENT_APP_CONSOLE(Cone)


bool Cone::OnInit()
{
  wxLog::DisableTimestamp();
  tmpcone = wxFileName::CreateTempFileName("cone_");
  return true;
}

int Cone::OnExit()
{
  if( wxFileExists(tmpcone) )
    wxRemoveFile(tmpcone);

  return wxAppConsole::OnExit();
}

int Cone::OnRun()
{
  const wxString amp("&");
  wxString url, output, type, sort, ra, dec, sr, magmin, magmax,
    catname("UCAC4");
  list<wxString> pars;
  bool verbose = false, pipelog = false, patch = false;


  // limit logging
  wxLog::SetLogLevel(wxLOG_Message);
  wxLog::SetVerbose(); // important to activate previous setup

  wxFFileInputStream istream(stdin);
  wxTextInputStream input(istream);

  // replace by using of regex? fortran-fread ?

  while(istream.IsOk() && ! istream.Eof()) {

    wxString line = input.ReadLine();

    if( line.StartsWith("VERBOSE") ) {
      verbose = GetBool(line);
      if( verbose )
	wxLog::SetLogLevel(wxLOG_Debug);
      else
	wxLog::SetLogLevel(wxLOG_Message);
      wxLog::SetVerbose();
    }

    if( line.StartsWith("PIPELOG") )
      pipelog = GetBool(line);

    if( line.StartsWith("PATCH") )
      patch = GetBool(line);

    if( line.StartsWith("URL") )
      url = GetString(line);

    if( line.StartsWith("CATNAME") )
      catname = GetString(line);

    if( line.StartsWith("OUTPUT") )
      output = GetString(line);

    if( line.StartsWith("TYPE") )
      type = GetString(line);

    if( line.StartsWith("SORT") )
      sort = GetString(line);

    if( line.StartsWith("MAGMIN") )
      magmin = wxString::FromCDouble(GetDouble(line));

    if( line.StartsWith("MAGMAX") )
      magmax = wxString::FromCDouble(GetDouble(line));

    if( line.StartsWith("PAR") )
      pars.push_back(GetString(line));
  }

  if( output.IsEmpty() ) {
    Stop("Missing output filename.");
    return 1;
  }


  wxASSERT(!url.IsEmpty());

  if( magmin != "" && magmax == "" )
    url += amp + sort + "=>" + magmin;
  else if ( magmax != "" && magmin == "" )
    url += amp + sort + "=<" + magmax;
  else if( magmin != "" && magmax != "" )
    url += amp + sort + "=" + magmin + ".." + magmax;

  for(list<wxString>::const_iterator a = pars.begin(); a != pars.end(); ++a)
    url += (a != pars.end() ? amp : "") + *a;

  wxURL u(url);
  if( ! u.IsOk() ) {
    if( u.GetError() == wxURL_SNTXERR )
      wxLogError("Syntax error in the URL string.");
    else if( u.GetError() == wxURL_NOPROTO )
      wxLogError("Found no protocol which can get this URL.");
    else if( u.GetError() == wxURL_NOHOST )
      wxLogError("A host name is required for this protocol.");
    else if( u.GetError() == wxURL_NOPATH )
      wxLogError("A path is required for this protocol.");
    Stop("Badly formatted URL string.");
    return 1;
  }

  wxString server = u.GetServer();
  wxString path = u.GetPath() + wxString("?") + u.GetQuery();

  if( pipelog ) PipePrint("Connecting "+server+"...");
  VOclient voc;
  if( ! voc.Connect(server) ) {
    Stop("Failed to connect of VO server.");
    return 1;
  }

  if( pipelog ) PipePrint("Downloading data...");
  if( ! voc.Get(path,tmpcone) ) {
    Stop("Download failed.");
    return 1;
  }

  if( pipelog ) PipePrint("Parsing XML data...");

  VOTable vt(tmpcone);
  wxLogInfo("XML IsOK? %s", vt.IsOk() ? "T" : "F");

  if( ! vt.IsOk() ) {
    if( pipelog ) PipePrint("Parsing XML data failed.");
    Stop("Parsing XML data failed.");
    return 1;
  }

  if( vt.HasError() ) {
    Stop(vt.GetErrorMsg());
    return 1;
  }

  if( vt.IsEmpty() ) {
    wxLogWarning("No objects found by given constrains.");
    if( pipelog ) PipePrint("No objects found.");
    Stop("");
    return 0;
  }
  else
    wxLogInfo("Cone search: %d objects found.",vt.RecordCount());

  if( ! sort.IsEmpty() ) {
    if( pipelog ) PipePrint("Sorting XML data...");
    vt.Sort(sort);
  }

  if( type.IsEmpty() )
    type = GetFileType(output);

  if( pipelog ) PipePrint("Data are being saved ...");
  if( type == "FITS" ) {
    FITStable ft(vt);
    if( ft.Save(output,true) ) {
      if( patch ) {
	if( catname == "UCAC4" ) {
	  if( ! PatchJohn(output) ) {
	    Stop("Application of Johnson's patch failed.");
	    return 1;
	  }
	}
	else
	  wxLogWarning("Johnson's patch is applicable only on UCAC 4.");
      }
    }
    else {
      Stop("FITS save failed.");
      return 1;
    }
  }
  else {
    if( ! vt.Save(output) ) {
      Stop("Save to "+type+" failed.");
      return 1;
    }
  }

  if( pipelog ) wxPrintf("=CONE> %d objects found\n",vt.RecordCount());

  Stop("");
  return 0;
}

bool Cone::PatchJohn(const wxString& fitsname)
{
  /*
     Adds two new colums: Johnson RI filters to the table.
     Also removes the original Gunn ri columns.

     https://gaia.esac.esa.int/documentation/GDR1/Data_processing/chap_cu5phot/sec_phot_calibr.html

      http://www.sdss.org/dr4/algorithms/sdssUBVRITransform.html
      see Lupton (2005)

  */

  wxLogInfo("Transforming Gunn's ri to Johnson RI ..");

  fitsfile *fits = 0;
  int status = 0;

  if( fits_open_table(&fits,fitsname.char_str(),READWRITE,&status) ) {
    fits_report_error(stderr, status);
    return false;
  }

  long nrows;
  if( fits_get_num_rows(fits,&nrows,&status) ) {
    fits_report_error(stderr, status);
    return false;
  }

  float *rmag = new float[nrows];
  float *imag = new float[nrows];
  float *e_rmag = new float[nrows];
  float *e_imag = new float[nrows];
  char **f_rmag = new char*[nrows];
  char **f_imag = new char*[nrows];
  for(int i = 0; i < nrows; i++) {
    f_rmag[i] = new char[7];
    f_imag[i] = new char[7];
  }

  int ncol, anynul;
  fits_get_colnum(fits,CASEINSEN,(char *)"rmag",&ncol,&status);
  fits_read_col(fits,TFLOAT,ncol,1,1,nrows,NULL,rmag,&anynul,&status);
  fits_delete_col(fits,ncol,&status);
  fits_get_colnum(fits,CASEINSEN,(char *)"e_rmag",&ncol,&status);
  fits_read_col(fits,TFLOAT,ncol,1,1,nrows,NULL,e_rmag,&anynul,&status);
  fits_delete_col(fits,ncol,&status);
  fits_get_colnum(fits,CASEINSEN,(char *)"f_rmag",&ncol,&status);
  fits_read_col(fits,TSTRING,ncol,1,1,nrows,NULL,f_rmag,&anynul,&status);
  fits_delete_col(fits,ncol,&status);
  fits_get_colnum(fits,CASEINSEN,(char *)"imag",&ncol,&status);
  fits_read_col(fits,TFLOAT,ncol,1,1,nrows,NULL,imag,&anynul,&status);
  fits_delete_col(fits,ncol,&status);
  fits_get_colnum(fits,CASEINSEN,(char *)"e_imag",&ncol,&status);
  fits_read_col(fits,TFLOAT,ncol,1,1,nrows,NULL,e_imag,&anynul,&status);
  fits_delete_col(fits,ncol,&status);
  fits_get_colnum(fits,CASEINSEN,(char *)"f_imag",&ncol,&status);
  fits_read_col(fits,TSTRING,ncol,1,1,nrows,NULL,f_imag,&anynul,&status);
  fits_delete_col(fits,ncol,&status);

  if( status != 0 ) {
    fits_report_error(stderr, status);
    return false;
  }

  float *Rmag = new float[nrows];
  float *Imag = new float[nrows];
  float *e_Rmag = new float[nrows];
  float *e_Imag = new float[nrows];

  for(int i = 0; i < nrows; i++) {
    if( isnormal(rmag[i]) &&  isnormal(imag[i]) ) {
      float ri = rmag[i] - imag[i];
      Rmag[i] = rmag[i] - 0.2936*ri - 0.1439;
      Imag[i] = rmag[i] - 1.2444*ri - 0.3820;
      e_Rmag[i] = e_rmag[i];
      e_Imag[i] = e_imag[i];
    }
    else {
      Rmag[i] = 99.999;
      Imag[i] = 99.999;
      e_Rmag[i] = 9.999;
      e_Imag[i] = 9.999;
    }
    /*
    wxLogInfo("%d %f %f %f %f %d %d",i,Rmag[i],Imag[i],rmag[i],imag[i],
	      isnormal(rmag[i]),isnan(imag[i]));
    */
  }

  fits_insert_col(fits,ncol,(char *)"Rmag",(char *)"1E",&status);
  fits_write_col(fits,TFLOAT,ncol,1,1,nrows,Rmag,&status);
  ncol++;
  fits_insert_col(fits,ncol,(char *)"e_Rmag",(char *)"1E",&status);
  fits_write_col(fits,TFLOAT,ncol,1,1,nrows,e_Rmag,&status);
  ncol++;
  fits_insert_col(fits,ncol,(char *)"f_Rmag",(char *)"1A",&status);
  fits_write_col(fits,TSTRING,ncol,1,1,nrows,f_rmag,&status);
  ncol++;
  fits_insert_col(fits,ncol,(char *)"Imag",(char *)"1E",&status);
  fits_write_col(fits,TFLOAT,ncol,1,1,nrows,Imag,&status);
  ncol++;
  fits_insert_col(fits,ncol,(char *)"e_Imag",(char *)"1E",&status);
  fits_write_col(fits,TFLOAT,ncol,1,1,nrows,e_Imag,&status);
  ncol++;
  fits_insert_col(fits,ncol,(char *)"f_Imag",(char *)"1A",&status);
  fits_write_col(fits,TSTRING,ncol,1,1,nrows,f_imag,&status);

  fits_write_comment(fits,"",&status);
  fits_write_comment(fits," *** NOTICE ***",&status);
  fits_write_comment(fits,
      "Rmag, Imag in Johnson system, and related columns, has been derived",
      &status);
  fits_write_comment(fits,
      "from Gunn's ri magnitudes by the transformation, Lupton (2005): ",
      &status);
  fits_write_comment(fits,
      "http://www.sdss.org/dr4/algorithms/sdssUBVRITransform.html",&status);

  fits_close_file(fits, &status);
  fits_report_error(stderr,status);

  delete[] rmag; delete[] imag; delete[] e_rmag; delete[] e_imag;
  delete[] Rmag; delete[] Imag; delete[] e_Rmag; delete[] e_Imag;
  for(int i = 0; i < nrows; i++) {
    delete[] f_rmag[i];
    delete[] f_imag[i];
  }
  delete[] f_rmag; delete[] f_imag;

  return true;
}

void Cone::Stop(const wxString& msg)
{
  // It is just strangle workaround.
  // The code simulates STOP commands which terminate of Fortran programs.
  // We have no faith to correctness of exit codes returned by child processes
  // because a correct terminate would leads to generate 'Child process
  // (PID XXX) still alive but pipe closed so generating a close notification'
  // under some GNU/Linux distributions (Fedora). Processing of STOP mark
  // eliminates use of process execution terminate codes.
  // One looks as a feature (bug) of WX.
  if( msg == "" )
    fprintf(stderr,"STOP 0\n");
  else
    fprintf(stderr,"STOP '%s'\n",static_cast<const char *>(msg.c_str()));
}

void Cone::PipePrint(const wxString& line)
{
  wxPrintf("=CONE> "+line+"\n");
  fflush(stdout);
}