33 #include "bdb_query.hpp"
36 namespace swarm {
namespace query {
41 void get_Tsys(gpulog::logrecord &lr,
double &T,
int &sys)
56 std::ostream& record_output_default(std::ostream &out, gpulog::logrecord &lr)
60 out << lr.msgid() <<
"\t" << T <<
"\t" << sys;
64 bool keplerian_output =
false;
68 { keplerian_output =
true; planets_coordinate_system = coordinate_system; }
71 { keplerian_output =
false; planets_coordinate_system = coordinate_system; }
74 { planets_coordinate_system = coordinate_system; }
78 double a, e, i, O, w , M;
81 keplerian_t keplerian_for_cartesian (
const body& b,
const body& center )
84 double x = b.x - center.x;
85 double y = b.y - center.y;
86 double z = b.z - center.z;
87 double vx = b.vx - center.vx;
88 double vy = b.vy - center.vy;
89 double vz = b.vz - center.vz;
90 double mass = b.mass + center.mass;
91 calc_keplerian_for_cartesian(k.a, k.e, k.i, k.O, k.w, k.M, x, y,z, vx, vy, vz, mass );
95 body center_of_mass(
const body* bodies,
const int nbod ){
97 center.x = center.y = center.z = center.vx = center.vy = center.vz = 0.;
102 center.x += bodies[i].x*bodies[i].mass;
103 center.y += bodies[i].y*bodies[i].mass;
104 center.z += bodies[i].z*bodies[i].mass;
105 center.vx += bodies[i].vx*bodies[i].mass;
106 center.vy += bodies[i].vy*bodies[i].mass;
107 center.vz += bodies[i].vz*bodies[i].mass;
108 center.mass += bodies[i].mass;
111 center.x /= center.mass;
112 center.y /= center.mass;
113 center.z /= center.mass;
114 center.vx /= center.mass;
115 center.vy /= center.mass;
116 center.vz /= center.mass;
122 std::ostream& record_output_1(std::ostream &out, gpulog::logrecord &lr, body_range_t &body_range)
125 int nbod, sys, flags;
127 lr >> time >> sys >> flags >> nbod >> bodies;
130 const body &star = bodies[0];
132 switch(planets_coordinate_system)
138 center = center_of_mass( bodies, nbod );
144 center.x = center.y = center.z = center.vx = center.vy = center.vz = 0.; center.mass = star.mass;
148 if((time<=0.) &&
false)
150 if (keplerian_output)
151 { std::cerr <<
"# Output in Keplerian coordinates "; }
152 else { std::cerr <<
"# Output in Cartesian coordinates "; }
153 switch(planets_coordinate_system)
156 std::cerr <<
"(astrocentric) " << center.x <<
' ' << center.y <<
' '<< center.z <<
" " << center.vx <<
' ' << center.vy <<
' ' << center.vz;
159 std::cerr <<
"(barycentric) "<< center.x <<
' ' << center.y <<
' '<< center.z <<
" " << center.vx <<
' ' << center.vy <<
' ' << center.vz;
162 std::cerr <<
"(jacobi) " << center.x <<
' ' << center.y <<
' '<< center.z <<
" " << center.vx <<
' ' << center.vy <<
' ' << center.vz;
165 std::cerr <<
"(origin) " << center.x <<
' ' << center.y <<
' '<< center.z <<
" " << center.vx <<
' ' << center.vy <<
' ' << center.vz;
172 size_t bufsize = 1000;
174 for(
int bod = 0; bod < nbod; bod++)
176 if(!body_range.in(bod)) {
continue; }
177 const body &b = bodies[bod];
178 if( keplerian_output && (bod==0) ) {
continue; }
179 if( (planets_coordinate_system==jacobi) && (bod==0) ) {
continue; }
180 if( ( !keplerian_output && (planets_coordinate_system!=jacobi) && (bod> 0) ) ||
181 ( !keplerian_output && (planets_coordinate_system==jacobi) && (bod> 1) ) ||
182 ( keplerian_output && (bod> 1 ) ) ){ out <<
"\n"; }
184 if( planets_coordinate_system == jacobi )
185 { center = center_of_mass ( bodies, bod); }
187 if( keplerian_output && bod > 0)
189 if(planets_coordinate_system==barycentric) center.mass -= b.mass;
190 keplerian_t orbit = keplerian_for_cartesian( b, center );
191 if(planets_coordinate_system==barycentric) center.mass += b.mass;
192 const double rad2deg = 180./M_PI;
193 snprintf(buf, bufsize,
"%10d %lg %6d %6d %lg % 9.5lg % 9.5lg % 9.5lg % 9.5lg % 9.5lg % 9.5lg %d", lr.msgid(), time, sys, bod, b.mass, orbit.a, orbit.e , orbit.i*rad2deg, orbit.O*rad2deg, orbit.w *rad2deg, orbit.M*rad2deg, flags);
195 if(!keplerian_output)
197 double x = b.x - center.x;
198 double y = b.y - center.y;
199 double z = b.z - center.z;
200 double vx= b.vx- center.vx;
201 double vy= b.vy- center.vy;
202 double vz= b.vz- center.vz;
203 snprintf(buf, bufsize,
"%10d %lg %6d %6d %lg %9.5lg %9.5lg %9.5lg %9.5lg %9.5lg %9.5lg %d", lr.msgid(), time, sys, bod, b.mass,
x, y, z, vx, vy, vz, flags);
213 std::ostream& record_output_2(std::ostream &out, gpulog::logrecord &lr, body_range_t &body_range)
216 int sys, body_id = 0;
220 if(!body_range.in(b.body_id))
return out;
222 size_t bufsize = 1000;
225 if( keplerian_output)
227 double mass = 1. + b.mass;
229 calc_keplerian_for_cartesian(orbit.a, orbit.e, orbit.i, orbit.O, orbit.w, orbit.M, b.x, b.y,b.z,b.vx,b.vy,b.vz,mass );
230 const double rad2deg = 180./M_PI;
231 snprintf(buf, bufsize,
"%10d %lg %6d %6d %lg % 9.5lg % 9.5lg % 9.5lg % 9.5lg % 9.5lg % 9.5lg ***WARNING: NOT ACCURATE***", lr.msgid(), T, sys, body_id, b.mass, orbit.a, orbit.e , orbit.i*rad2deg, orbit.O*rad2deg, orbit.w *rad2deg, orbit.M*rad2deg);
235 snprintf(buf, bufsize,
"%10d %lg %6d %6d %lg % 9.5lf % 9.5lf % 9.5lf % 9.5lf % 9.5lf % 9.5lf", lr.msgid(), T, sys, body_id, b.mass, b.x, b.y, b.z, b.vx, b.vy, b.vz);
244 std::ostream& record_output_15(std::ostream &out, gpulog::logrecord &lr, body_range_t &body_range)
249 lr >> T >> sys >> body_id >> minb >> vproj;
250 if(!body_range.in(body_id))
return out;
252 size_t bufsize = 1000;
254 snprintf(buf, bufsize,
"%10d %lg %6d %6d %lg %lg Transit Experimental", lr.msgid(), T, sys, body_id, minb, vproj);
262 std::ostream& record_output_11(std::ostream &out, gpulog::logrecord &lr, body_range_t &body_range)
267 lr >> T >> sys >> body_id >> vz;
268 if(!body_range.in(body_id))
return out;
270 size_t bufsize = 1000;
272 snprintf(buf, bufsize,
"%10d %lg %6d %6d %lg Rv Experimental", lr.msgid(), T, sys, body_id, vz);
280 std::ostream& record_output_16(std::ostream &out, gpulog::logrecord &lr, body_range_t &body_range)
285 lr >> T >> sys >> body_id >> minb >> vproj;
286 if(!body_range.in(body_id))
return out;
288 size_t bufsize = 1000;
290 snprintf(buf, bufsize,
"%10d %lg %6d %6d %lg %lg Occultation Experimental", lr.msgid(), T, sys, body_id, minb, vproj);
300 int evtid = lr.msgid();
305 return record_output_1(out,lr,bod);
307 return record_output_2(out,lr,bod);
309 return record_output_default(out,lr);
311 return record_output_11(out,lr,bod);
313 return record_output_15(out,lr,bod);
315 return record_output_16(out,lr,bod);
317 return record_output_default(out,lr);
323 void execute_binary_query(
const std::string &datafile, time_range_t T, sys_range_t sys, body_range_t bod) {
324 swarmdb db(datafile);
325 swarmdb::result r = db.query(sys, T);
327 gpulog::logrecord lr;
335 void execute(
const std::string &datafile, time_range_t T, sys_range_t sys, body_range_t bod)
337 if(datafile.substr(datafile.length()-3,datafile.length()) ==
".db"){
338 execute_bdb_query(datafile,T,sys,bod);
340 execute_binary_query(datafile,T,sys,bod);