aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/libvoclient/vocSkybot.c
blob: b24d05d810ed7a694864a0a30615cccfa9515bbe (plain) (blame)
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
/**
 *  SkyBoT Minor Planet Service Interface:
 *  --------------------------------------
 *
 *  A utility class for accessing the IMCCE Skybot ephemerides service.  
 *  The service is called as part of the constructor for the object 
 *  allowing easy access to the result data.  Class methods also exist so
 *  a VO Client interface the same access to the data.
 *
 *  Reference:      http://www.imcce.fr/webservices/skybot/
 *
 *  Class Methods:      
 *  --------------
 *
 *              sb = voc_skybot  (ra, dec, rsz, dsz, epoch)
 *
 *      count = voc_skybotNObjs  (sb)
 *      str = voc_skybotStrAttr  (sb, attrname, index)
 *     dval = voc_skybotDblAttr  (sb, attrname, index)
 *
 *  Available Attributes:
 *  ---------------------
 *
 *      number  string      Asteroid number
 *      name    string      Asteroid name
 *      ra      double      J2000 Equatorial RA
 *      dec     double      J2000 Equatorial Dec
 *      class   string      Object classification
 *      vmag    double      Visual magnitude
 *      poserr  double      Error on position (arcsec)
 *      cdist   double      Body-to-center angular distance
 *      dra     double      RA motion (arcsec/hr)
 *      ddec    double      Dec motion (arcsec/hr)
 *      dgeo    double      Geocentric distance (AU)
 *      dhelio  double      Heliocentric distance (AU)
 *      px      double      Mean J2000 heliocentric position vector (AU)
 *      py      double          "       "       "       "       "
 *      pz      double          "       "       "       "       "
 *      vx      double      Mean J2000 heliocentric position vector (AU/day)
 *      vy      double          "       "       "       "       "
 *      vz      double          "       "       "       "       "       
 *      JD0     double      T0, epoch of position vector (JD)
 * 
 *
 *  Example Usage:      
 *  ---------------
 *
 *  1) Find bodies in a 900" radius around (0.0,0.0) on JD 2453939.123
 *
 *      VOCSkybot skybot = new VOCSkybot (0.0,0.0,900,2453939.123);
 *      for (int i=0; i < skybot.getNObjs(); i++)
 *          System.out.print   ("Name: '" + sb.getStrAttr("name",i)+"' " +
 *                              "RA: " + sb.getDblAttr("ra",i) + " " +
 *                              "Dec: " + sb.getDblAttr("dec",i) + " " +
 *                              "Mv: " + sb.getDblAttr("vmag",i) + " "); 
 * 
 * 
 *  @file       vocSkybot.c
 *  @author     Michael Fitzpatrick
 *  @version    June 2006
 *
 **************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

#define _VOCLIENT_LIB_
#include "VOClient.h"


extern VOClient *vo; 			/* Interface runtime struct	*/


/*  SkyBoT interface procedures.
 */
Skybot      voc_skybot (double ra, double dec, double rsz, double dsz,
		double epoch);
int         voc_skybotNObjs (Skybot sb);
char       *voc_skybotStrAttr (Skybot sb, char *attr, int index);
double      voc_skybotDblAttr (Skybot sb, char *attr, int index);


/**
 *  VOC_SKYBOT --  Call the SkyBoT (Sky Bodies Tracker) service from IMCCE.
 *  This service returns a list of minor planets withing the specified
 *  search radius/box about the poition at the epoch given.  Epoch is 
 *  assumed to be JD, ra/dec are J2000 decimal degrees, search size is 
 *  given in arcsec.
 *
 *  @brief      Call the SkyBoT (Sky Bodies Tracker) service from IMCCE.
 *  @fn         sb = voc_skybot (double ra, double dec, double rsz,
 *  			double dsz, double epoch)
 *
 *  @param  ra       	RA position of query (decimal degrees)
 *  @param  dec       	Dec position of query (decimal degrees)
 *  @param  rsz       	RA size of query (arcsec)
 *  @param  dsz       	Dec size of query (arcsec)
 *  @param  epoch      	epoch of query (JD)
 *  @returns            the string value
 */
Skybot      
voc_skybot (double ra, double dec, double rsz, double dsz, double epoch)
{
    Skybot   sb = (Skybot) VOC_NULL;
    vocRes_t *result = (vocRes_t *) NULL;
    vocMsg_t *msg = (vocMsg_t *) msg_newCallMsg (0, "skybot", 0);

    /*  Make sure we've been initialized properly first.
     */
    if (vo == (VOClient *) NULL) {
        if (voc_initVOClient (NULL) == ERR) {
            fprintf (stderr, "ERROR: Can't initialize VO Client....\n");
            exit (1);
        } else if (VOC_DEBUG)
            printf ("Warning: Initializing VO Client....\n");
    }

    /* Add the parameters of the call.
     */
    msg_addFloatParam (msg, ra);
    msg_addFloatParam (msg, dec);
    msg_addFloatParam (msg, rsz);
    msg_addFloatParam (msg, dsz);
    msg_addFloatParam (msg, epoch);
    
    /* Read result and check for any faults.
     */
    if (msg_resultStatus ((result = msg_sendMsg (vo->io_chan, msg))) == ERR) {
        if (!vo->quiet)
	    fprintf (stderr, "ERROR: cannot invoke SkyBoT service\n");
    } else
        sb = msg_getIntResult (result, 0);

    if (msg) free ((void *)msg);         /* free the pointers            */
    if (result) free ((void *)result);

    return (sb);
}


/**
 *  VOC_SKYBOTSTRATTR -- Return a real-valued field as a string value. 
 *
 *  @brief      Return a real-valued field as a string value. 
 *  @fn         dval = voc_skybotStrAttr (Skybot sb, char *attr, int index)
 *
 *  @param  sb          handle to skybot query
 *  @param  attr        attribute name
 *  @param  index       object index
 *  @returns            the string value
 */
char *
voc_skybotStrAttr (Skybot sb, char *attrname, int index)
{
    char     *val = NULL, *str = NULL;
    int       len = 0;

    if (sb > 0) {
        vocRes_t *result = (vocRes_t *) NULL;
        vocMsg_t *msg = (vocMsg_t *) msg_newCallMsg (sb, "sbStrAttr", 0);

    	msg_addStringParam (msg, attrname);
    	msg_addIntParam (msg, index);

        /* Send message and read result.
         */
        if (msg_resultStatus ((result=msg_sendMsg(vo->io_chan,msg))) == ERR) {
            if (!vo->quiet)
		fprintf (stderr, "ERROR: skybotStrAttr() failed\n");
        } else {
            val = msg_getStringResult (result, 0);
            str = calloc (1, (len = strlen(val)+1));
            strncpy (str, val, len);
        }

        if (msg) free ((void *)msg);     /* free the pointers            */
        if (result) free ((void *)result);
    }

    return (str);
}


/**
 *  VOC_SKYBOTDBLATTR -- Return a real-valued field as a double-precision value.
 *
 *  @brief      Return a real-valued field as a double-precision value.
 *  @fn         dval = voc_skybotDblAttr (Skybot sb, char *attr, int index)
 *
 *  @param  sb          handle to skybot query
 *  @param  attr        attribute name
 *  @param  index       object index
 *  @returns            the double-precision value
 */
double      
voc_skybotDblAttr (Skybot sb, char *attrname, int index)
{
    double   dval = (double) 0.0;

    if (sb > 0) {
        vocRes_t *result = (vocRes_t *) NULL;
        vocMsg_t *msg = (vocMsg_t *) msg_newCallMsg (sb, "sbDblAttr", 0);

    	msg_addStringParam (msg, attrname);
    	msg_addIntParam (msg, index);

        /* Send message and read result.
         */
        if (msg_resultStatus ((result=msg_sendMsg(vo->io_chan,msg))) == ERR) {
            if (!vo->quiet)
		fprintf (stderr, "ERROR: skybotDblAttr() failed\n");
        } else
            dval = msg_getFloatResult (result, 0);

        if (msg) free ((void *)msg);     /* free the pointers            */
        if (result) free ((void *)result);

    } else if (!vo->quiet)
        fprintf (stderr, "ERROR: Null object to resolverRA\n");

    return (dval);
}


/**
 *  VOC_SKYBOTNOBJS -- Return the number of objects found from the query.
 *
 *  @brief      Return the number of objects found from the query.
 *  @fn         int = voc_skybotNObjs (Skybot sb)
 *
 *  @param  sb          handle to skybot query
 *  @returns            the number of objects found 
 */
int      
voc_skybotNObjs (Skybot sb)
{
    int   ival = (int) 0;

    if (sb > 0) {
        vocRes_t *result = (vocRes_t *) NULL;
        vocMsg_t *msg = (vocMsg_t *) msg_newCallMsg (sb, "sbNObjs", 0);

        /* Send message and read result.
         */
        if (msg_resultStatus ((result=msg_sendMsg(vo->io_chan,msg))) == ERR) {
            if (!vo->quiet)
		fprintf (stderr, "ERROR: skybotNObjs() failed\n");
        } else
            ival = msg_getFloatResult (result, 0);

        if (msg) free ((void *)msg);     /* free the pointers            */
        if (result) free ((void *)result);

    } else if (!vo->quiet)
        fprintf (stderr, "ERROR: Null object to skybotNObjs\n");

    return (ival);
}