-
Notifications
You must be signed in to change notification settings - Fork 59
/
GaiaSkySearch.C
1798 lines (1623 loc) · 55.4 KB
/
GaiaSkySearch.C
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Name:
* GaiaSkySearch
* Language:
* C++
* Purpose:
* Defines the members of the GaiaSkySearch class.
* Description:
* This class extends the SkySearch class (and thereby TclAstroCat) to use
* an external filter scheme to convert catalogues into "tab table"
* format. Keeping the names consistent and disposing of the intermediary
* files etc. These foreign catalogues are controlled by the
* GaiaLocalCatalog class.
*
* Methods related to plotting overlay are also overridden to
* resolve problems with catalogues that have both WCS and X-Y
* coordinates and to align catalogues to images using the facilities
* of AST.
*
* A "namesvr" command is provided so that coordinates can be
* retrieved for objects, without doing a full catalogue or image query.
* Authors:
* PWD: P.W. Draper (JAC, Durham University)
* Copyright:
* Copyright (C) 1998-2000 Central Laboratory of the Research Councils
* Copyright (C) 2006 Particle Physics & Astronomy Research Council.
* Copyright (C) 2008-2009 Science and Technology Facilities Council.
* All Rights Reserved.
* Licence:
* This program 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 2 of the
* License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street,Fifth Floor, Boston, MA
* 02110-1301, USA
* History:
* 25-SEP-1998 (PWD):
* Original version.
* 21-AUG-2000 (PWD):
* Added changes to support NDF origins.
* 22-AUG-2000 (PWD):
* Override "info" command to stop foreign files from being
* ignored (all files in /tmp not saved).
* {enter_new_authors_here}
*
*-
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstdlib>
#include <iostream>
#include <sstream>
extern "C" {
#include "ast.h"
}
#include "error.h"
#include "util.h"
#include "AstroCatalog.h"
#include "LocalCatalog.h"
#include "GaiaLocalCatalog.h"
#include "Skycat.h"
#include "SkySearch.h"
#include "GaiaSkySearch.h"
#include "StarWCS.h"
#include "TclQueryUtil.h"
#include "GaiaWorldCoords.h"
#include "GaiaQueryResult.h"
#include "StarRtdImage.h"
// Trig conversion factors.
static const double pi_ = 3.14159265358979323846;
static const double r2d_ = 57.295779513082323; // (180.0/pi_)
static const double d2r_ = 0.017453292519943295; // (pi_/180.0);
static int gaiaGenAstroQuery( Tcl_Interp* interp, int argc, char* argv[],
AstroQuery& q, WorldOrImageCoords& pos1,
WorldOrImageCoords& pos2,
char* equinoxStr, FILE* feedback,
CatalogInfoEntry* entry );
//
// Declare a table of tcl subcommands.
//
// NOTE: keep this table sorted, so we can use a binary search on it !
// (select lines in emacs and use M-x sort-lines)
//
static class GaiaSkySearchSubCmds {
public:
const char* name; // method name
int (GaiaSkySearch::*fptr)(int argc, char* argv[]);
int min_args; // minimum number of args
int max_args; // maximum number of args
} subcmds_[] = {
{"check", &GaiaSkySearch::checkCmd, 1, 1},
{"content", &GaiaSkySearch::contentCmd, 0, 0},
{"csize", &GaiaSkySearch::csizeCmd, 1, 1},
{"entry", &GaiaSkySearch::entryCmd, 1, 4},
{"hms", &GaiaSkySearch::hmsCmd, 1, 1},
{"imgplot", &GaiaSkySearch::imgplotCmd, 1, 4},
{"info", &GaiaSkySearch::infoCmd, 1, 2},
{"namesvr", &GaiaSkySearch::namesvrCmd, 2, 2},
{"open", &GaiaSkySearch::openCmd, 0, 2},
{"origin", &GaiaSkySearch::originCmd, 0, 2},
{"save", &GaiaSkySearch::saveCmd, 1, 5},
{"setequinox", &GaiaSkySearch::setequinoxCmd, 1, 1}
};
/**
* Call the given method in this class with the given arguments
*/
int GaiaSkySearch::call(const char* name, int len, int argc, char* argv[])
{
// Since this tcl command may have a lot of subcommands, we do a
// binary search on the method table.
int low = 0,
high = sizeof(subcmds_)/sizeof(*subcmds_) - 1,
mid,
cond;
while ( low <= high ) {
mid = (low + high) / 2;
if ( ( cond = strcmp( name, subcmds_[mid].name ) ) < 0 ) {
high = mid - 1;
}
else if ( cond > 0 ) {
low = mid + 1;
}
else {
GaiaSkySearchSubCmds& t = subcmds_[mid];
if ( check_args( name, argc, t.min_args, t.max_args ) != TCL_OK ) {
return TCL_ERROR;
}
return (this->*t.fptr)( argc, argv );
}
}
// Not found, so extend search to parent class.
return SkySearch::call(name, len, argc, argv);
}
//
// A call to this function can be made from the tkAppInit file at
// startup to install the Tcl extended "astrocat" command.
//
extern "C" {
int GaiaCat_Init(Tcl_Interp* interp)
{
// Install the astrocat command.
Tcl_CreateCommand(interp, "astrocat", GaiaSkySearch::astroCatCmd, NULL, NULL);
return TCL_OK;
}
}
/**
* Implementation of the tcl extended command "astrocat". This creates
* a new instance of the astrocat command.
*
*/
int GaiaSkySearch::astroCatCmd( ClientData, Tcl_Interp *interp,
int argc, char* argv[] )
{
if (argc != 2) {
Tcl_AppendResult( interp, "wrong # args: should be \"",
argv[0], " instanceName\"", NULL );
return TCL_ERROR;
}
GaiaSkySearch *cmd = new GaiaSkySearch( interp, argv[0], argv[1] );
return cmd->status();
}
/**
* Constructor -
*
* Create an astrocat object in tcl for accessing the contents of
* local and remote catalogues, including CAT catalogues.
*
* Note that the tcl command for this object is created in the
* parent class constructor.
*/
GaiaSkySearch::GaiaSkySearch( Tcl_Interp *interp,
const char *cmdname,
const char *instname )
: TclAstroCat( interp, cmdname, instname ), // Needed as SkySearch
// inherits TclAstroCat
// as "public virtual"!
SkySearch( interp, cmdname, instname ),
xOrigin_(0.0),
yOrigin_(0.0),
hms_(1)
{
// Do nothing.
}
/**
* Destructor -
*/
GaiaSkySearch::~GaiaSkySearch()
{
// Do nothing.
}
/**
* Open the given astronomical catalogue.
*/
int GaiaSkySearch::openCmd(int argc, char* argv[])
{
// Return if a catalogue is open if no args are given.
if ( argc == 0 ) {
if ( cat_ ) {
set_result( "1" );
}
else {
set_result( "0" );
}
return TCL_OK;
}
if ( cat_ ) {
delete cat_;
cat_ = NULL;
}
CatalogInfoEntry *e = NULL;
if ( argc == 2 && ( strlen(argv[1]) != 0 ) ) {
// Open given catalog in given directory path.
e = lookupCatalogDirectoryEntry( argv[1] );
if ( !e ) {
return TCL_ERROR;
}
e = CatalogInfo::lookup( e, argv[0] );
if ( !e ) {
return fmt_error( "catalog entry for '%s' not found under '%s' ",
argv[0], argv[1]);
}
}
else {
// Get the entry for this catalogue type.
e = CatalogInfo::lookup( argv[0] );
if ( !e ) {
return TCL_ERROR;
}
}
// Now open the catalog, using the appropriate type.
if ( strcmp( e->servType(), "local" ) == 0 ) {
// Local catalogues are either tab tables or a format
// recognised by GaiaLocalCatalog.
if ( GaiaLocalCatalog::is_foreign( argv[0] ) ) {
// Invalidate the URL of the entry to avoid premature
// loading (when a known local catalogue).
e->url( " " );
cat_ = new GaiaLocalCatalog( e, interp_ );
}
else {
cat_ = new LocalCatalog( e );
}
}
else {
cat_ = new AstroCatalog( e ); // Class for remote catalogues.
}
if ( !cat_ || cat_->status() != 0 ) {
if ( cat_->status() != 0 ) delete cat_;
cat_ = NULL;
return error( "Failed to open catalog:", argv[0] );
}
// Set up feedback, if requested.
if ( feedback_ ) {
cat_->feedback( feedback_ );
}
// Reset origins.
xOrigin_ = 0.0;
yOrigin_ = 0.0;
// Reset display format.
hms_ = 1;
return TCL_OK;
}
/**
* Check that the given filename is a valid local catalog.
*/
int GaiaSkySearch::checkCmd(int argc, char* argv[])
{
if ( GaiaLocalCatalog::is_foreign( argv[0] ) ) {
return GaiaLocalCatalog::check_table( argv[0] );
}
else {
return LocalCatalog::check_table( argv[0] );
}
}
/**
* Override the "entry" command to make sure url and longname are
* the same for foreign catalogues when written into the skycat.cfg
* file.
*/
int GaiaSkySearch::entryCmd( int argc, char* argv[] )
{
CatalogInfoEntry* e;
CatalogInfoEntry* dir = CatalogInfo::root();
char *url = NULL;
if ( !dir ) {
return TCL_ERROR;
}
// Trap "get" command and modify url so that it appears the same as
// longname for foreign catalogues.
if ( strcmp( argv[0], "get" ) == 0 ) {
if ( argc == 1 ) {
// Get entry from current catalog.
if ( !cat_ ) {
return error( "no catalog is open" );
}
e = cat_->entry();
}
else {
if ( argc > 2 ) {
// Use given catalog directory.
dir = lookupCatalogDirectoryEntry( argv[2] );
if ( !dir ) {
return TCL_ERROR;
}
}
// Get entry from named catalog.
e = CatalogInfo::lookup( dir, argv[1] );
if ( !e ) {
return error( "can't find catalog entry for: ", argv[1] );
}
}
// Reset the url to be longname and pass on to TclAstroCat command.
url = strdup( e->url() );
if ( strcmp( e->servType(), "local" ) == 0 ) {
e->url( e->longName() );
}
}
int ret = TclAstroCat::entryCmd( argc, argv );
// Restore dynamic url, if needed.
if ( url != NULL ) {
e->url( url );
free( url );
// If "get" then override report of various columns so that
// default configuration is reported (GAIA is more fussy
// about having X and Y columns -v- RA/Dec, so needs to know this).
if ( e->id_col() == 0 ) {
appendKeyVal( "id_col", "0" );
}
if ( e->ra_col() == 1 ) {
appendKeyVal( "ra_col", "1" );
}
if ( e->dec_col() == 2 ) {
appendKeyVal( "dec_col", "2" );
}
if ( e->x_col() == -1 ) {
appendKeyVal( "x_col", "-1" );
}
if ( e->y_col() == -1 ) {
appendKeyVal( "y_col", "-1" );
}
}
return ret;
}
/**
* Save command. Override so we can get foreign catalogues to
* do their conversion correctly.
*/
int GaiaSkySearch::saveCmd( int argc, char* argv[] )
{
// See if this is an attempt to add, or just create.
int iflag = 0;
if (argc >= 2) {
if (Tcl_GetBoolean(interp_, argv[1], &iflag) != TCL_OK)
return TCL_ERROR;
}
// Check if destination catalogue is a known foreign type.
char *filename = NULL;
int ret = TCL_OK;
AstroCatalog *tmp = NULL;
if ( GaiaLocalCatalog::is_foreign( argv[0] ) ) {
// If appending then the temporary file should already exist, so
// save the contents first. To do this we need to convert the
// file, if it hasn't been converted already.
filename = argv[0];
if ( iflag ) {
CatalogInfoEntry *e = CatalogInfo::lookup( argv[0] );
if ( ! e ) {
return TCL_ERROR;
}
// May be an existing file, check for existence of URL, plus it
// cannot be itself.
struct stat buf;
if ( stat( e->url(), &buf ) != 0 ||
strcmp( e->url(), e->longName() ) == 0) {
// Temporary file for this file doesn't exist, so we need to
// create it.
tmp = new GaiaLocalCatalog( e, interp_ );
}
argv[0] = (char *) e->url();
}
else {
// Create a temporary name to store the results.
argv[0] = tempnam( (char *) NULL, "gaia" );
}
argv[0] = strdup( argv[0] );
}
// Now do the real save.
ret = TclAstroCat::saveCmd( argc, argv );
if ( filename != NULL && ret == TCL_OK ) {
// Result stored in temporary file. Need to convert to
// appropriate foreign format. Create a pseudo CatalogInfoEntry
// so we can use a proper object to control the conversion.
CatalogInfoEntry f;
f.servType( "local" );
f.longName( filename );
f.shortName( filename );
f.url( filename );
ret = GaiaLocalCatalog::save( &f, interp_, argv[0], filename );
free( argv[0] );
argv[0] = filename;
}
return ret;
}
/**
* Determine the maximum size of character strings needed to store
* the columns of a tab-table. The tab-table is passed as a Tcl list
* of lists and the return is a list of the number of characters
* needed. This is implemented in C++ for speed reasons only.
*/
int GaiaSkySearch::csizeCmd( int argc, char *argv[] )
{
char **mainArgv;
int mainArgc = 0;
char **rowArgv;
int rowArgc = 0;
int *sizes = NULL;
int ncolumn = 0;
int len = 0;
int i;
int j;
// Split the list into a list of rows.
if ( Tcl_SplitList( interp_, argv[0], &mainArgc, &mainArgv ) != TCL_OK ) {
return error( "not a list" );
}
for ( i = 0; i < mainArgc; i++ ) {
if ( Tcl_SplitList( interp_, mainArgv[i], &rowArgc, &rowArgv ) != TCL_OK ) {
if ( mainArgc > 0 ) {
Tcl_Free( (char *) mainArgv );
}
return error( "not a sub list" );
}
else {
// Allocate memory for columns sizes.
if ( ncolumn == 0 ) {
sizes = new int[rowArgc];
ncolumn = rowArgc;
for ( j = 0; j < ncolumn; j++ ) {
sizes[j] = 0;
}
}
// Check list if same length as before.
if ( rowArgc != ncolumn ) {
if ( mainArgc > 0 ) {
Tcl_Free( (char *) mainArgv );
}
if ( rowArgc > 0 ) {
Tcl_Free( (char *) rowArgv );
}
return error( "lists are different lengths" );
}
// Compare lengths of these strings with previous ones.
for ( j = 0; j < ncolumn; j++ ) {
len = strlen( rowArgv[j] );
if ( len > sizes[j] ) {
sizes[j] = len;
}
}
// Free the memory.
if ( rowArgc > 0 ) {
Tcl_Free( (char *) rowArgv );
}
}
}
// Now construct the list to return.
if ( ncolumn > 0 ) {
ostringstream os;
for ( i = 0; i < ncolumn; i++ ) {
os << sizes[i] << " ";
}
set_result( os.str().c_str() );
delete [] sizes;
}
if ( mainArgc > 0 ) {
Tcl_Free( (char *) mainArgv );
}
return TCL_OK;
}
/*
* In GAIA %% is a pseudonym for "::". Needed as ":" separates multiple
* symbols and "::" allows global variable access.
*/
static void replace_percents( char *str )
{
char *p = str;
while ( ( p = strstr( p, "%%" ) ) ) {
*p++ = ':';
*p++ = ':';
}
}
/**
* Override plot_objects to sort out problems with plotting when
* have both pixel coordinates and sky coordinates. Also now we
* are using AST transformations we need to make sure that further
* transformations are not applied (the equinox is now the same as the
* image) and support STC regions.
*/
int GaiaSkySearch::plot_objects( Skycat* image, const QueryResult& r,
const char* cols, const char* symbol,
const char* expr )
{
char *glexpr;
char *glsymbol;
char** colNames = NULL;
char** exprList = NULL;
char** symb = NULL;
int nexpr = 0;
int nsymb = 0;
int numCols = 0;
int status = 0;
int* colIndexes = NULL;
// This loop executes only once and is used for error
// handling/cleanup via "break".
int once = 1;
while (once-- > 0) {
// Check that plot columns are valid and also save the column
// indexes for accessing row values as variables later.
if ((status = Tcl_SplitList(interp_, (char*)cols,
&numCols, &colNames)) != TCL_OK) {
break;
}
colIndexes = new int[numCols];
for (int i = 0; i < numCols; i++) {
if ( (colIndexes[i] = r.colIndex(colNames[i])) < 0 ) {
status = error("unrecognised column in plot expression: ", colNames[i]);
break;
}
}
if ( status != TCL_OK ) {
break;
}
// In GAIA %% is a pseudonym for "::".
glsymbol = strdup( symbol );
replace_percents( glsymbol );
// Parse symbol info, a variable length list of
// {shape color ratio angle label cond}
if ((status = Tcl_SplitList(interp_, glsymbol,
&nsymb, &symb)) != TCL_OK) {
free( glsymbol );
break;
}
free( glsymbol );
// Default values
char* shape = (char *) "";
char* fg = (char *) "white"; // if no color is specified, use 2: b&w
char* bg = (char *) "black";
char* ratio = (char *) "1"; // these may be Tcl expressions
char* angle = (char *) "0";
char* label = (char *) "";
char* cond = (char *) "1";
if ((status = parse_symbol(r, nsymb, symb, shape, fg, bg, ratio,
angle, label, cond)) != TCL_OK)
break;
// parse the size expr list: {size units}
glexpr = strdup( expr );
replace_percents( glexpr );
if ((status = Tcl_SplitList(interp_, (char*)glexpr,
&nexpr, &exprList)) != TCL_OK) {
free( glexpr );
break;
}
free( glexpr );
if (nexpr == 0 || strlen(exprList[0]) == 0) {
status = error("invalid symbol expression: ", expr);
break;
}
char* size = exprList[0];
char* units = (char *) "image";
if (nexpr > 1 && strlen(exprList[1]))
units = exprList[1];
// Is this an STC shape?
int stc_shape = 0;
if ( strcmp( shape, "stcshape" ) == 0 && r.stc_col() != -1 ) {
stc_shape = 1;
}
// xy_units are re-used to carry the shape, need to handle
// storage for that.
char xy_storage[32];
char *xy_units = xy_storage;
// for each row in the catalog, eval the expressions and plot the symbols
int nrows = r.numRows();
int id_col = r.id_col();
int stc_col = r.stc_col();
for (int rownum = 0; rownum < nrows; rownum++) {
char* id;
if ((status = r.get(rownum, id_col, id)) != 0)
break;
WorldOrImageCoords pos;
if ((status = r.getPos(rownum, pos)) != 0)
break;
double x, y;
if (r.isPix() && ! r.isWcs() ) { // PWD: modify here
x = pos.x();
y = pos.y();
strcpy(xy_units, "image");
// Subtract the origins.
x -= xOrigin_;
y -= yOrigin_;
}
else if (r.isWcs()) {
x = pos.ra_deg();
y = pos.dec_deg();
if ( stc_shape ) {
// xy_units are the shape.
if ((status = r.get(rownum, stc_col, xy_units)) != 0)
break;
}
else {
// Degrees in the same units as image, so same equinox
// in skycat speak.
const char *equinox = image->image()->wcs().equinoxStr();
strcpy(xy_units, "deg ");
strcat(xy_units, equinox);
}
}
else {
status = error("no wcs or image coordinates to plot");
break;
}
if ((status = plot_row(image, r, rownum, id, x, y, xy_units,
numCols, colNames, colIndexes, shape,
bg, fg, ratio, angle, label, cond, size,
units)) != TCL_OK)
break;
}
}
// free memory allocated for split Tcl lists and return the status
if (colNames)
ckfree((char *)colNames);
if (colIndexes)
delete[] colIndexes;
if (symb)
ckfree((char *)symb);
if (exprList)
ckfree((char *)exprList);
return status;
}
/*
* Parse the given symbol info and set the values of the last 7 args from
* it. Overridden to support GAIA symbols. Note it's a copy because the types
* are hardcoded.
*/
int GaiaSkySearch::parse_symbol( const QueryResult& r, int argc, char** argv,
char*& shape, char*& fg, char*& bg,
char*& ratio, char*& angle, char*& label,
char*& cond )
{
static const char* symbols[] = {
"arrow",
"arc",
"circle",
"compass",
"cross",
"diamond",
"ellipse",
"line",
"plus",
"square",
"stcshape",
"triangle",
"rectangle",
"rotbox",
"xrange",
"yrange"
};
static int nsymbols = sizeof(symbols)/sizeof(char*);
int found = 0;
if (argc < 1)
return error("empty plot symbol");
// symbol shape
shape = argv[0];
for (int i = 0; i < nsymbols; i++) {
if (strcmp(shape, symbols[i]) == 0) {
found++;
break;
}
}
if (!found)
return error("invalid plot symbol");
// color
if (argc >= 2) {
if (strlen(argv[1])) {
fg = bg = argv[1];
}
}
// ratio
if (argc >= 3) {
if (strlen(argv[2])) {
ratio = argv[2];
}
}
// angle
if (argc >= 4) {
if (strlen(argv[3])) {
angle = argv[3];
}
}
// label
if (argc >= 5) {
if (strlen(argv[4])) {
label = argv[4];
}
}
// cond
if (argc >= 6) {
if (strlen(argv[5])) {
cond = argv[5];
}
}
return TCL_OK;
}
/**
* Set or get the values used to offset image coordinates when
* plotted. These are the NDF origin values and are used to
* transform between image coordinate and pixel coordinates.
*
* Arguments are none, in which case the current origins are returned
* as the result, or two doubles, which are added to any image
* coordinates before plotting (note if any 0.5 corrections are
* required then you must add these here, the origin supplied are
* subtracted from the X and Y coordinates).
*/
int GaiaSkySearch::originCmd( int argc, char *argv[] )
{
if ( argc < 2 ) {
// Return the current origin.
Tcl_ResetResult( interp_ );
char buf[80];
sprintf( buf, "%f %f", xOrigin_, yOrigin_ );
set_result( buf );
}
else {
double xo;
double yo;
if ( Tcl_GetDouble( interp_, argv[0], &xo ) != TCL_OK ) {
return error( argv[0], " is not a floating point value" );
}
else {
if ( Tcl_GetDouble( interp_, argv[1], &yo ) != TCL_OK ) {
return error( argv[1], " is not a floating point value" );
}
}
xOrigin_ = xo;
yOrigin_ = yo;
}
return TCL_OK;
}
/**
* catalog "info" subcommand:
*
* usage: $cat info $serv_type ?$directory?
*
* This command returns a list of catalogs from the catalog config file.
* The "serv_type" argument determines which catalogs are listed (one of:
* catalog, namesvr, imagesvr, directory, local...).
*
* If $serv_type is an empty string, all entries are returned.
*
* If $directory is specified, the return list will be from the given
* catalog directory entry, retrieved via HTTP if needed. The default, if
* no $directory is given, is the top level list read from the default
* config file at startup.
*
* This method overrides the TclAstroCat version to make sure that our
* temporary files are not excluded from the configuration file.
*/
int GaiaSkySearch::infoCmd(int argc, char* argv[])
{
Tcl_ResetResult(interp_);
CatalogInfoEntry* e;
if (argc == 2) {
e = lookupCatalogDirectoryEntry(argv[1]);
if (!e)
return TCL_ERROR;
if (! e->link()) {
if (CatalogInfo::load(e) != 0) // follow catalog dir link
return TCL_ERROR;
}
}
else {
e = CatalogInfo::root();
}
if (!e || !e->link())
return error("can't find catalog info");
e = e->link(); // get pointer to first catalog in directory
// get the serv_type
Tcl_ResetResult(interp_);
int n = strlen(argv[0]);
for (; e != NULL; e = e->next()) {
if (strncmp(argv[0], e->servType(), n) == 0) {
// ignore local catalogs in /tmp, since they will be
// deleted later, unless they are foreign catalogues, that
// are not stored in a FITS extension!
int save = 1;
if ( strcmp(e->servType(), "local") == 0 ) {
if ( strncmp(e->url(), "/tmp/", 5) == 0 ) {
if ( GaiaLocalCatalog::is_foreign( e->longName() ) ) {
if ( strstr( e->longName(), "}" ) ) {
save = 0;
}
}
else {
save = 0;
}
}
}
if ( save ) {
Tcl_AppendElement(interp_, (char*)e->longName());
}
}
}
return TCL_OK;
}
/**
* Get the content of the current query (whole catalogue if local, or
* last query if remote) as a Tcl list.
*/
int GaiaSkySearch::contentCmd( int argc, char *argv[] )
{
if ( !cat_ ) {
return error( "no catalog is currently open" );
}
// If a local catalogue use whole.
LocalCatalog *lc = dynamic_cast<LocalCatalog *>( cat_ );
QueryResult *qr;
if ( ! lc ) {
if ( ! result_ ) {
return error( "no query is available" );
}
qr = result_;
}
else {
qr = &lc->getQuery();
}
int ncols = qr->numCols();
int nrows = qr->numRows();
char* s;
Tcl_ResetResult( interp_ );
Tcl_Obj *result = Tcl_GetObjResult( interp_ );
if ( cat_->isWcs() ) {
GaiaWorldCoords pos;
char dec_buf[32];
char ra_buf[32];
int dec_col = qr->dec_col();
int ra_col = qr->ra_col();
for ( int i = 0; i < nrows; i++ ) {
// Row includes formatted RA & Dec, do that first.
if ( qr->getPos( i, pos ) != 0 ) {
return error( "Failed getting world coordinates" );
}
pos.format( ra_buf, dec_buf, equinoxStr_ );
// Put the column values in a list.
Tcl_Obj *list = Tcl_NewListObj( 0, NULL );
for ( int j = 0; j < ncols; j++ ) {
if ( j == ra_col ) {
Tcl_ListObjAppendElement( interp_, list,
Tcl_NewStringObj(ra_buf, -1) );
}
else if ( j == dec_col ) {
Tcl_ListObjAppendElement( interp_, list,
Tcl_NewStringObj(dec_buf, -1) );
}
else {
if ( qr->get( i, j, s ) == 0 ) {
Tcl_ListObjAppendElement( interp_, list,
Tcl_NewStringObj( s, -1 ) );
}
else {
Tcl_ListObjAppendElement( interp_, list,
Tcl_NewStringObj( "", -1 ) );
}
}
}
Tcl_ListObjAppendElement( interp_, result, list );
}
}
else {
// Image coords or no coords - no special formatting needed.
for ( int i = 0; i < nrows; i++ ) {
Tcl_Obj *list = Tcl_NewListObj( 0, NULL );
for ( int j = 0; j < ncols; j++) {
if ( qr->get( i, j, s ) == 0 ) {
Tcl_ListObjAppendElement( interp_, list,
Tcl_NewStringObj( s, -1 ) );
}
else {
Tcl_ListObjAppendElement( interp_, list,
Tcl_NewStringObj( "", -1 ) );
}
}
Tcl_ListObjAppendElement( interp_, result, list );
}
}
return TCL_OK;
}