forked from Bill-Gray/lunar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2ades.c
181 lines (160 loc) · 6.7 KB
/
csv2ades.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
/* Code to process Gaia-DR2 observations from CSV files at
http://cdn.gea.esac.esa.int/Gaia/gdr2/sso_observation/csv/
_or_ Gaia-DR3 observations from somewhat differently formatted files at
http://cdn.gea.esac.esa.int/Gaia/gdr3/Solar_system/sso_observation/
into the ADES format. Run as, for example,
./csv2ades input_file.csv > output.ades
./csv2ades -4179 input_file.csv > output.ades
The second example would output only observations for object (4179)
Toutatis. The first would output all of the observations.
Further notes : there are four GZIPped files in the above directory
for DR2, ranging from about 50 to 78 MBytes compressed and expanding
to 150 to 230 MBytes uncompressed. Each contains astrometry for a
specific range of numbered minor planets; for example, the first has
data for objects (8) Flora to (4435) Holt.
SsoObservation_-4284967216_-4284922946.csv.gz 8 - 4435
SsoObservation_-4284922936_-4284857966.csv.gz 4436 - 10933
SsoObservation_-4284857946_-4284702156.csv.gz 10935 - 26514
SsoObservation_-4284702096_-4283326086.csv.gz 26520 - 164121
The Gaia DR3 data are less obviously arranged and are larger.
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "watdefs.h"
#include "date.h"
static char *get_csv( char *obuff, const char *ibuff,
const char *header, const char *tag)
{
size_t i;
unsigned nval = 0;
const char *found = strstr( header, tag);
assert( found);
assert( found >= header);
for( i = 0; i < (size_t)(found - header); i++)
if( header[i] == ',')
nval++;
while( nval-- && ibuff)
{
ibuff = strchr( ibuff, ',');
if( ibuff)
ibuff++;
}
assert( ibuff);
for( i = 0; ibuff[i] && ibuff[i] != ','; i++)
obuff[i] = ibuff[i];
obuff[i] = '\0';
return( obuff);
}
static void csv_to_ades( const char *ibuff, const char *header,
const int low_num, const int high_num)
{
char tbuff[90];
int num;
get_csv( tbuff, ibuff, header, "number_mp,");
num = atoi( tbuff);
if( num >= low_num && num <= high_num)
{
const double jd_epoch = 2455197.5; /* 2010 Jan 1.0 */
double jd_obs;
printf( " <optical>\n");
printf( " <permID>%s</permID>\n", tbuff);
printf( " <mode>TDI</mode>\n");
printf( " <stn>258</stn>\n");
printf( " <sys>ICRF_AU</sys>\n");
printf( " <ctr>0</ctr>\n");
printf( " <pos1>%s</pos1>\n", get_csv( tbuff, ibuff, header, "x_gaia,"));
printf( " <pos2>%s</pos2>\n", get_csv( tbuff, ibuff, header, "y_gaia,"));
printf( " <pos3>%s</pos3>\n", get_csv( tbuff, ibuff, header, "z_gaia,"));
jd_obs = atof( get_csv( tbuff, ibuff, header, "epoch_utc,")) + jd_epoch;
full_ctime( tbuff, jd_obs, FULL_CTIME_MILLISECS | FULL_CTIME_YMD
| FULL_CTIME_LEADING_ZEROES | FULL_CTIME_MONTHS_AS_DIGITS);
tbuff[4] = tbuff[7] = '-';
tbuff[10] = 'T';
printf( " <obsTime>%sZ</obsTime>\n", tbuff);
printf( " <ra>%s</ra>\n", get_csv( tbuff, ibuff, header, "ra,"));
printf( " <dec>%s</dec>\n", get_csv( tbuff, ibuff, header, "dec,"));
printf( " <rmsRA>%f</rmsRA>\n", atof( get_csv( tbuff, ibuff, header, "ra_error_random,")) / 1000.);
printf( " <rmsDec>%f</rmsDec>\n", atof( get_csv( tbuff, ibuff, header, "dec_error_random,")) / 1000.);
printf( " <rmsCorr>%s</rmsCorr>\n", get_csv( tbuff, ibuff, header, "ra_dec_correlation_random,"));
printf( " <astCat>Gaia3</astCat>\n");
get_csv( tbuff, ibuff, header, "g_mag,");
if( *tbuff >= '0' && *tbuff <= '9')
{
double g_flux, g_flux_sigma;
printf( " <mag>%s</mag>\n", tbuff);
g_flux = atof( get_csv( tbuff, ibuff, header, "g_flux"));
g_flux_sigma = atof( get_csv( tbuff, ibuff, header, "g_flux_error"));
printf( " <rmsMag>%.3f</rmsMag>\n",
(g_flux_sigma / g_flux) / log( 2.512));
printf( " <band>G</band>\n");
printf( " <photCat>Gaia2</photCat>\n");
}
printf( " </optical>\n");
}
}
int main( const int argc, const char **argv)
{
FILE *hdr_file = fopen( "gaia.hdr", "rb");
char buff[800];
int i, low_num = 1, high_num = 1000000;
if( !hdr_file)
{
perror( "Can't open 'gaia.hdr'");
return( -1);
}
for( i = 1; i < argc; i++)
if( argv[i][0] == '-')
sscanf( argv[i] + 1, "%d,%d", &low_num, &high_num);
while( fgets( buff, sizeof( buff), hdr_file))
if( *buff != '*')
printf( "%s", buff);
else for( i = 1; i < argc; i++)
if( argv[i][0] != '-')
{
FILE *ifile = fopen( argv[i], "rb");
char header[800];
assert( ifile);
*header = '\0';
while( fgets( buff, sizeof( buff), ifile))
{
if( !memcmp( buff, "solution_id", 11))
strcpy( header, buff);
else if( *buff != '#')
csv_to_ades( buff, header, low_num, high_num);
}
fclose( ifile);
}
fclose( hdr_file);
return( 0);
}
/*
<optical>
<permID>3200</permID> number_mp
<mode>TDI</mode>
<stn>258</stn>
<sys>ICRF_AU</sys>
<ctr>0</ctr>
<pos1>-0.9241930322</pos1> x_gaia
<pos2>+0.3423918499</pos2> y_gaia
<pos3>+0.1490296836</pos3> z_gaia
<obsTime>2016-02-27T15:45:22.593Z</obsTime> ! epoch_utc (days since 2016 Feb 27 = JD 2457445.5)
<ra>29.28052137</ra> ra
<dec>+21.81708923</dec> dec
<rmsRA>0.30945110</rmsRA> ! ra_error_random (IN milliarcsec)
<rmsDec>0.52852813</rmsDec> ! dec_error_random
<rmsCorr>0.9999607648</rmsCorr> ra_dec_correlation_random
<astCat>Gaia2</astCat>
<exp>4.4</exp>
<remarks>TCB=2016-02-27T15:46:49.938Z, transitId=47101903417545381, observationId=471019034175453811, rmsRaSyst=0.00848502 as, rmsDecSyst=0.00394375 as, rmsCorrSyst=0.88192202, positionScanAngle=300.348 deg</remarks>
</optical>
solution_id,source_id,observation_id,number_mp, 0-3
epoch,epoch_err,epoch_utc,ra, 4-7
dec,ra_error_systematic,dec_error_systematic,ra_dec_correlation_systematic, 8-11
ra_error_random,dec_error_random,ra_dec_correlation_random,g_mag, 12-15
g_flux,g_flux_error,x_gaia,y_gaia, 16-19
z_gaia,vx_gaia,vy_gaia,vz_gaia, 20-23
position_angle_scan,level_of_confidence 24-26
*/