17#include "nomadbinfile2mysql.h"
19#include "angconversion.h"
20#include "MeshIterator.h"
32 m_Mesh =
new HTMesh(HTMLevel, HTMLevel);
33 strcpy(db_tbl, _db_tbl);
44 stardata->RA = bswap_32(stardata->RA);
45 stardata->Dec = bswap_32(stardata->Dec);
46 stardata->dRA = bswap_16(stardata->dRA);
47 stardata->dDec = bswap_16(stardata->dDec);
48 stardata->B = bswap_16(stardata->B);
49 stardata->V = bswap_16(stardata->V);
55bool NOMADStarDataWriter::createTable()
58 char create_query[2048];
60 "CREATE TABLE IF NOT EXISTS `%s` (`Trixel` int(32) NOT NULL COMMENT 'Trixel Number', `RA` double NOT NULL "
61 "COMMENT 'RA Hours', `Dec` double NOT NULL COMMENT 'Declination Degrees', `dRA` double NOT NULL COMMENT "
62 "'Proper Motion along RA', `dDec` double NOT NULL COMMENT 'Proper Motion along Dec', `PM` double NOT NULL "
63 "COMMENT 'Proper Motion (magnitude)', `V` float NOT NULL COMMENT 'Visual Magnitude', `B` float NOT NULL "
64 "COMMENT 'Blue Magnitude', `Mag` float NOT NULL COMMENT 'Magnitude for sorting', `UID` int(64) NOT NULL "
65 "auto_increment COMMENT 'Unique ID', `Copies` tinyint(8) NOT NULL COMMENT 'Number of Copies of the star', "
66 "PRIMARY KEY (`UID`), UNIQUE KEY `UID` (`UID`), KEY `Trixel` (`Trixel`,`PM`,`Mag`)) ENGINE=MyISAM DEFAULT "
67 "CHARSET=latin1 AUTO_INCREMENT=1",
70 if (mysql_query(m_MySQLLink, create_query))
72 cerr <<
"ERROR: Table creation failed!" << endl;
84 double *endDec,
float years)
87 double theta0 = hour2rad(startRA);
88 double lat0 = deg2rad(startDec);
90 double PMperyear = sqrt(dRA * dRA + dDec * dDec);
92 double dir0 = (years > 0.0) ? atan2(dRA, dDec) : atan2(-dRA, -dDec);
93 double PM = PMperyear * fabs(years);
95 double dst = deg2rad(arcsec2deg(PM / 1000.0));
97 double phi0 = M_PI / 2.0 - lat0;
99 double lat1 = asin(sin(lat0) * cos(dst) + cos(lat0) * sin(dst) * cos(dir0));
100 double dtheta = atan2(sin(dir0) * sin(dst) * cos(lat0), cos(dst) - sin(lat0) * sin(lat1));
102 *endRA = rad2hour(theta0 + dtheta);
103 *endDec = rad2deg(lat1);
109bool NOMADStarDataWriter::insertStarData(
unsigned int trixel,
const DeepStarData *
const data)
113 float B, V, RA, Dec, dRA, dDec;
116 B = ((double)data->B) / 1000.0;
117 V = ((double)data->V) / 1000.0;
118 RA = ((double)data->RA) / 1000000.0;
119 Dec = ((double)data->Dec) / 100000.0;
120 dRA = ((double)data->dRA) / 1000.0;
121 dDec = ((double)data->dDec) / 1000.0;
126 unsigned int originalTrixelID = m_Mesh->
index(RA, Dec);
127 if (trixel != originalTrixelID)
131 if (V == 30.0 && B != 30.0)
141 double RA1, Dec1, RA2, Dec2;
144 PM = sqrt(dRA * dRA + dDec * dDec);
149 unsigned int TrixelList[900];
152 double separation = sqrt(hour2deg(RA1 - RA2) * hour2deg(RA1 - RA2) +
153 (Dec1 - Dec2) * (Dec1 - Dec2));
154 if (separation > 50.0 / 60.0)
158 while (trixels.hasNext())
160 TrixelList[ntrixels] = trixels.next();
166 TrixelList[0] = originalTrixelID;
172 cerr <<
"Ntrixels is zero in trixel " << originalTrixelID;
176 for (
int i = 0; i < ntrixels; ++i)
179 "INSERT INTO `%s` (`Trixel`, `RA`, `Dec`, `dRA`, `dDec`, `B`, `V`, `mag`, `PM`, `Copies`) VALUES "
180 "(\'%d\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%u\')",
181 db_tbl, TrixelList[i], RA, Dec, dRA, dDec, B, V, mag, PM,
182 ((TrixelList[i] == originalTrixelID) ?
186 if (mysql_query(m_MySQLLink, query))
188 cerr <<
"MySQL INSERT INTO failed! Query was: " <<
endl <<
query <<
endl;
195bool NOMADStarDataWriter::truncateTable()
199 sprintf(query,
"TRUNCATE TABLE `%s`", db_tbl);
200 if (mysql_query(m_MySQLLink, query))
202 cerr <<
"Truncate table query \"" <<
query <<
"\" failed!" <<
endl;
211bool NOMADStarDataWriter::writeStarDataToDB()
235 for (trixel = 0; trixel < ntrixels; ++trixel)
237 fseek(DataFile, m_IndexOffset + trixel * INDEX_ENTRY_SIZE + 4, SEEK_SET);
238 fread(&offset, 4, 1, DataFile);
239 fread(&nstars, 4, 1, DataFile);
242 if (offset > (
unsigned)(pow2(31) - 1))
244 fseek(DataFile, pow2(31) - 1, SEEK_SET);
245 fseek(DataFile, offset - pow2(31) + 1, SEEK_CUR);
248 fseek(DataFile, offset, SEEK_SET);
250 for (
int i = 0; i < nstars; ++i)
260 float B, V, RA, Dec, dRA, dDec;
263 B = ((double)data.B) / 1000.0;
264 V = ((double)data.V) / 1000.0;
265 RA = ((double)data.RA) / 1000000.0;
266 Dec = ((double)data.Dec) / 100000.0;
267 dRA = ((double)data.dRA) / 1000.0;
268 dDec = ((double)data.dDec) / 1000.0;
273 unsigned int originalTrixelID = m_Mesh->
index(hour2deg(RA), Dec);
274 if (trixel != originalTrixelID)
276 cout <<
"Trixel = " << trixel <<
", but this is the original Trixel ID: " << originalTrixelID
277 <<
". Skipping" <<
endl;
278 cout <<
"Skipped star has (RA, Dec) = " << RA << Dec <<
"; (dRA, dDec) = " << dRA << dDec
279 <<
"; and (B, V) = " << B << V <<
"." <<
endl;
280 cout <<
"This suspected duplicate is star " << i <<
"in trixel " << trixel;
285 if (V == 30.0 && B != 30.0)
295 double RA1, Dec1, RA2, Dec2, RA1deg, RA2deg;
298 PM = sqrt(dRA * dRA + dDec * dDec);
302 RA1deg = hour2deg(RA1);
303 RA2deg = hour2deg(RA2);
305 unsigned int TrixelList[60];
308 double separationsqr = (RA1deg - RA2deg) * (RA1deg - RA2deg) +
309 (Dec1 - Dec2) * (Dec1 - Dec2);
313 m_Mesh->
intersect(RA1deg, Dec1, RA2deg, Dec2);
315 while (trixels.hasNext())
317 TrixelList[nt] = trixels.next();
323 TrixelList[0] = originalTrixelID;
329 cerr <<
"# of trixels is zero in trixel " << originalTrixelID;
333 for (
int i = 0; i < nt; ++i)
336 "INSERT INTO `%s` (`Trixel`, `RA`, `Dec`, `dRA`, `dDec`, `B`, `V`, `mag`, `PM`, `Copies`) "
337 "VALUES (\'%d\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', \'%lf\', "
339 db_tbl, TrixelList[i], RA, Dec, dRA, dDec, B, V, mag, PM,
340 ((TrixelList[i] == originalTrixelID) ?
344 if (mysql_query(m_MySQLLink, query))
346 cerr <<
"MySQL INSERT INTO failed! Query was: " <<
endl <<
query <<
endl;
352 if (trixel % 100 == 0)
353 cout <<
"Finished trixel " << trixel <<
endl;
359bool NOMADStarDataWriter::readFileHeader()
363 char ASCII_text[125];
370 fread(ASCII_text, 124, 1, DataFile);
371 ASCII_text[124] =
'\0';
372 printf(
"%s", ASCII_text);
374 fread(&endian_id, 2, 1, DataFile);
375 if (endian_id != 0x4B53)
377 fprintf(stdout,
"Byteswapping required\n");
382 fprintf(stdout,
"Byteswapping not required\n");
386 fread(&version_no, 1, 1, DataFile);
387 fprintf(stdout,
"Version number: %d\n", version_no);
389 fread(&nfields, 2, 1, DataFile);
394 for (i = 0; i < nfields; ++i)
395 fread(&de,
sizeof(
struct dataElement), 1, DataFile);
397 fread(&ntrixels, 4, 1, DataFile);
399 ntrixels = bswap_32(ntrixels);
400 fprintf(stdout,
"Number of trixels reported = %d\n", ntrixels);
402 m_IndexOffset = ftell(DataFile);
411 if (!readFileHeader())
416 if (!writeStarDataToDB())
420int main(
int argc,
char *argv[])
429 fprintf(stderr,
"USAGE: %s <NOMAD bin file> <MySQL DB User> <Password> <DB Name> <Table Name>\n", argv[0]);
433 strcpy(db_tbl, argv[5]);
434 strcpy(db_name, argv[4]);
436 f = fopen(argv[1],
"r");
440 fprintf(stderr,
"ERROR: Could not open file %s for binary read.\n", argv[1]);
445 if (mysql_init(&link) ==
nullptr)
447 fprintf(stderr,
"ERROR: Failed to initialize MySQL connection!\n");
451 ret = mysql_real_connect(&link,
"localhost", argv[2], argv[3], db_name, 0,
nullptr, 0);
455 fprintf(stderr,
"ERROR: MySQL connect failed for the following reason: %s\n", mysql_error(&link));
460 if (mysql_select_db(&link, db_name))
462 fprintf(stderr,
"ERROR: Could not select MySQL database %s. MySQL said: %s", db_name, mysql_error(&link));
HTMesh was originally intended to be a simple interface to the HTM library for the KStars project tha...
Trixel index(double ra, double dec) const
returns the index of the trixel that contains the specified point.
void intersect(double ra, double dec, double radius, BufNum bufNum=0)
NOTE: The intersect() routines below are all used to find the trixels needed to cover a geometric obj...
MeshIterator is a very lightweight class used to iterate over the result set of an HTMesh intersectio...
static void bswap_stardata(DeepStarData *stardata)
Byteswaps the DeepStarData structure.
NOMADStarDataWriter(FILE *f, int HTMLevel, MYSQL *link, char *_db_tbl)
Constructor.
bool write()
Writes the star data into the DB by calling multiple functions.
~NOMADStarDataWriter()
Destructor.
static void calculatePMCoords(double startRA, double startDec, double dRA, double dDec, double *endRA, double *endDec, float years)
Computes the (unprecessed) coordinates of a star after accounting for proper motion.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QTextStream & endl(QTextStream &stream)
A 16-byte structure that holds star data for really faint stars.
A structure describing a data field in the binary file.