PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/kstars/tools/modcalcvlsr.cpp

https://gitlab.com/g10h4ck/kstars
C++ | 475 lines | 357 code | 86 blank | 32 comment | 64 complexity | 259fa07f5313ff4af2aaf1b2a2037fa8 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0
  1. /***************************************************************************
  2. modcalcvlsr.cpp - description
  3. -------------------
  4. begin : sun mar 13 2005
  5. copyright : (C) 2005 by Pablo de Vicente
  6. email : p.devicente@wanadoo.es
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "modcalcvlsr.h"
  17. #include <QPointer>
  18. #include <QFileDialog>
  19. #include <KLocalizedString>
  20. #include <KMessageBox>
  21. #include "ksnumbers.h"
  22. #include "dms.h"
  23. #include "skyobjects/skypoint.h"
  24. #include "geolocation.h"
  25. #include "kstarsdata.h"
  26. #include "kstarsdatetime.h"
  27. #include "widgets/dmsbox.h"
  28. #include "dialogs/locationdialog.h"
  29. #include "dialogs/finddialog.h"
  30. modCalcVlsr::modCalcVlsr(QWidget *parentSplit) :
  31. QFrame(parentSplit), velocityFlag(0)
  32. {
  33. setupUi(this);
  34. RA->setDegType(false);
  35. Date->setDateTime( KStarsDateTime::currentDateTime() );
  36. initGeo();
  37. VLSR->setValidator( new QDoubleValidator( VLSR ) );
  38. VHelio->setValidator( new QDoubleValidator( VHelio ) );
  39. VGeo->setValidator( new QDoubleValidator( VGeo ) );
  40. VTopo->setValidator( new QDoubleValidator( VTopo ) );
  41. // signals and slots connections
  42. connect(Date, SIGNAL( dateTimeChanged( const QDateTime & ) ),
  43. this, SLOT( slotCompute() ) );
  44. connect(NowButton, SIGNAL( clicked() ), this, SLOT( slotNow() ) );
  45. connect(LocationButton, SIGNAL( clicked() ), this, SLOT( slotLocation() ) );
  46. connect(ObjectButton, SIGNAL( clicked() ), this, SLOT( slotFindObject() ) );
  47. connect(RA, SIGNAL( editingFinished() ), this, SLOT( slotCompute() ) );
  48. connect(Dec, SIGNAL( editingFinished() ), this, SLOT( slotCompute() ) );
  49. connect(VLSR, SIGNAL( editingFinished() ), this, SLOT( slotCompute() ) );
  50. connect(VHelio, SIGNAL( editingFinished() ), this, SLOT( slotCompute() ) );
  51. connect(VGeo, SIGNAL( editingFinished() ), this, SLOT( slotCompute() ) );
  52. connect(VTopo, SIGNAL( editingFinished() ), this, SLOT( slotCompute() ) );
  53. connect(RunButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch()));
  54. show();
  55. }
  56. modCalcVlsr::~modCalcVlsr(){
  57. }
  58. void modCalcVlsr::initGeo(void)
  59. {
  60. geoPlace = KStarsData::Instance()->geo();
  61. LocationButton->setText( geoPlace->fullName() );
  62. }
  63. void modCalcVlsr::slotNow()
  64. {
  65. Date->setDateTime( KStarsDateTime::currentDateTime());
  66. slotCompute();
  67. }
  68. void modCalcVlsr::slotFindObject() {
  69. QPointer<FindDialog> fd = new FindDialog( KStars::Instance() );
  70. if ( fd->exec() == QDialog::Accepted ) {
  71. SkyObject *o = fd->selectedObject();
  72. RA->showInHours( o->ra0() );
  73. Dec->showInDegrees( o->dec0() );
  74. }
  75. delete fd;
  76. }
  77. void modCalcVlsr::slotLocation() {
  78. QPointer<LocationDialog> ld( new LocationDialog( this ) );
  79. if ( ld->exec() == QDialog::Accepted && ld ) {
  80. GeoLocation *newGeo = ld->selectedCity();
  81. if ( newGeo ) {
  82. geoPlace = newGeo;
  83. LocationButton->setText( geoPlace->fullName() );
  84. }
  85. }
  86. delete ld;
  87. slotCompute();
  88. }
  89. void modCalcVlsr::slotCompute()
  90. {
  91. bool ok1(false), ok2(false);
  92. SkyPoint sp( RA->createDms(false, &ok1), Dec->createDms(true, &ok2) );
  93. if ( !ok1 || !ok2 ) return;
  94. KStarsDateTime dt = Date->dateTime();
  95. double vst[3];
  96. geoPlace->TopocentricVelocity( vst, dt.gst() );
  97. if ( sender()->objectName() == "VLSR" ) velocityFlag = 0;
  98. if ( sender()->objectName() == "VHelio" ) velocityFlag = 1;
  99. if ( sender()->objectName() == "VGeo" ) velocityFlag = 2;
  100. if ( sender()->objectName() == "VTopo" ) velocityFlag = 3;
  101. switch ( velocityFlag ) {
  102. case 0: //Hold VLSR constant, compute the others
  103. {
  104. double vlsr = VLSR->text().toDouble();
  105. double vhelio = sp.vHeliocentric( vlsr, dt.djd() );
  106. double vgeo = sp.vGeocentric( vhelio, dt.djd() );
  107. VHelio->setText( QString::number( vhelio ) );
  108. VGeo->setText( QString::number( vgeo ) );
  109. VTopo->setText( QString::number( sp.vTopocentric(vgeo, vst) ) );
  110. break;
  111. }
  112. case 1: //Hold VHelio constant, compute the others
  113. {
  114. double vhelio = VHelio->text().toDouble();
  115. double vlsr = sp.vHelioToVlsr( vhelio, dt.djd() );
  116. double vgeo = sp.vGeocentric( vhelio, dt.djd() );
  117. VLSR->setText( QString::number( vlsr ) );
  118. VGeo->setText( QString::number( vgeo ) );
  119. VTopo->setText( QString::number( sp.vTopocentric(vgeo, vst) ) );
  120. break;
  121. }
  122. case 2: //Hold VGeo constant, compute the others
  123. {
  124. double vgeo = VGeo->text().toDouble();
  125. double vhelio = sp.vGeoToVHelio( vgeo, dt.djd() );
  126. double vlsr = sp.vHelioToVlsr( vhelio, dt.djd() );
  127. VLSR->setText( QString::number( vlsr ) );
  128. VHelio->setText( QString::number( vhelio ) );
  129. VTopo->setText( QString::number( sp.vTopocentric(vgeo, vst) ) );
  130. break;
  131. }
  132. case 3: //Hold VTopo constant, compute the others
  133. {
  134. double vtopo = VTopo->text().toDouble();
  135. double vgeo = sp.vTopoToVGeo( vtopo, vst );
  136. double vhelio = sp.vGeoToVHelio( vgeo, dt.djd() );
  137. double vlsr = sp.vHelioToVlsr( vhelio, dt.djd() );
  138. VLSR->setText( QString::number( vlsr ) );
  139. VHelio->setText( QString::number( vhelio ) );
  140. VGeo->setText( QString::number( vgeo ) );
  141. break;
  142. }
  143. default: //oops
  144. qDebug() << xi18n("Error: do not know which velocity to use for input.");
  145. break;
  146. }
  147. }
  148. void modCalcVlsr::slotUtChecked(){
  149. if ( UTCheckBatch->isChecked() )
  150. UTBoxBatch->setEnabled( false );
  151. else {
  152. UTBoxBatch->setEnabled( true );
  153. }
  154. }
  155. void modCalcVlsr::slotDateChecked(){
  156. if ( DateCheckBatch->isChecked() )
  157. DateBoxBatch->setEnabled( false );
  158. else {
  159. DateBoxBatch->setEnabled( true );
  160. }
  161. }
  162. void modCalcVlsr::slotRaChecked(){
  163. if ( RACheckBatch->isChecked() ) {
  164. RABoxBatch->setEnabled( false );
  165. }
  166. else {
  167. RABoxBatch->setEnabled( true );
  168. }
  169. }
  170. void modCalcVlsr::slotDecChecked(){
  171. if ( DecCheckBatch->isChecked() ) {
  172. DecBoxBatch->setEnabled( false );
  173. }
  174. else {
  175. DecBoxBatch->setEnabled( true );
  176. }
  177. }
  178. void modCalcVlsr::slotEpochChecked(){
  179. if ( EpochCheckBatch->isChecked() )
  180. EpochBoxBatch->setEnabled( false );
  181. else
  182. EpochBoxBatch->setEnabled( true );
  183. }
  184. void modCalcVlsr::slotLongChecked(){
  185. if ( LongCheckBatch->isChecked() )
  186. LongitudeBoxBatch->setEnabled( false );
  187. else
  188. LongitudeBoxBatch->setEnabled( true );
  189. }
  190. void modCalcVlsr::slotLatChecked(){
  191. if ( LatCheckBatch->isChecked() )
  192. LatitudeBoxBatch->setEnabled( false );
  193. else {
  194. LatitudeBoxBatch->setEnabled( true );
  195. }
  196. }
  197. void modCalcVlsr::slotHeightChecked(){
  198. if ( ElevationCheckBatch->isChecked() )
  199. ElevationBoxBatch->setEnabled( false );
  200. else {
  201. ElevationBoxBatch->setEnabled( true );
  202. }
  203. }
  204. void modCalcVlsr::slotVlsrChecked(){
  205. if ( InputVelocityCheckBatch->isChecked() )
  206. InputVelocityBoxBatch->setEnabled( false );
  207. else {
  208. InputVelocityBoxBatch->setEnabled( true );
  209. }
  210. }
  211. void modCalcVlsr::slotInputFile() {
  212. QString inputFileName;
  213. inputFileName = QFileDialog::getOpenFileName(0, QString(), QString());
  214. InputFileBoxBatch->setUrl( inputFileName );
  215. }
  216. void modCalcVlsr::slotOutputFile() {
  217. QString outputFileName;
  218. outputFileName = QFileDialog::getSaveFileName( );
  219. OutputFileBoxBatch->setUrl( outputFileName );
  220. }
  221. void modCalcVlsr::slotRunBatch() {
  222. QString inputFileName;
  223. inputFileName = InputFileBoxBatch->url().toLocalFile();
  224. // We open the input file and read its content
  225. if ( QFile::exists(inputFileName) ) {
  226. QFile f( inputFileName );
  227. if ( !f.open( QIODevice::ReadOnly) ) {
  228. QString message = xi18n( "Could not open file %1.", f.fileName() );
  229. KMessageBox::sorry( 0, message, xi18n( "Could Not Open File" ) );
  230. inputFileName.clear();
  231. return;
  232. }
  233. // processLines(&f);
  234. QTextStream istream(&f);
  235. processLines(istream);
  236. // readFile( istream );
  237. f.close();
  238. } else {
  239. QString message = xi18n( "Invalid file: %1", inputFileName );
  240. KMessageBox::sorry( 0, message, xi18n( "Invalid file" ) );
  241. inputFileName.clear();
  242. InputFileBoxBatch->setUrl( inputFileName );
  243. return;
  244. }
  245. }
  246. void modCalcVlsr::processLines( QTextStream &istream ) {
  247. // we open the output file
  248. // QTextStream istream(&fIn);
  249. QString outputFileName;
  250. outputFileName = OutputFileBoxBatch->url().toLocalFile();
  251. QFile fOut( outputFileName );
  252. fOut.open(QIODevice::WriteOnly);
  253. QTextStream ostream(&fOut);
  254. QString line;
  255. QChar space = ' ';
  256. int i = 0;
  257. long double jd0;
  258. SkyPoint spB;
  259. double sra, cra, sdc, cdc;
  260. dms raB, decB, latB, longB;
  261. QString epoch0B;
  262. double vhB, vgB, vtB, vlsrB, heightB;
  263. double vtopo[3];
  264. QTime utB;
  265. QDate dtB;
  266. KStarsDateTime dt0B;
  267. while ( ! istream.atEnd() ) {
  268. line = istream.readLine();
  269. line.trimmed();
  270. //Go through the line, looking for parameters
  271. QStringList fields = line.split( ' ' );
  272. i = 0;
  273. // Read Ut and write in ostream if corresponds
  274. if(UTCheckBatch->isChecked() ) {
  275. utB = QTime::fromString( fields[i] );
  276. i++;
  277. } else
  278. utB = UTBoxBatch->time();
  279. if ( AllRadioBatch->isChecked() )
  280. ostream << QLocale().toString( utB ) << space;
  281. else
  282. if(UTCheckBatch->isChecked() )
  283. ostream << QLocale().toString( utB ) << space;
  284. // Read date and write in ostream if corresponds
  285. if(DateCheckBatch->isChecked() ) {
  286. dtB = QDate::fromString( fields[i] );
  287. i++;
  288. } else
  289. dtB = DateBoxBatch->date();
  290. if ( AllRadioBatch->isChecked() )
  291. ostream << QLocale().toString( dtB, QLocale::LongFormat ).append(space);
  292. else
  293. if(DateCheckBatch->isChecked() )
  294. ostream << QLocale().toString( dtB, QLocale::LongFormat ).append(space);
  295. // Read RA and write in ostream if corresponds
  296. if(RACheckBatch->isChecked() ) {
  297. raB = dms::fromString( fields[i],false);
  298. i++;
  299. } else
  300. raB = RABoxBatch->createDms(false);
  301. if ( AllRadioBatch->isChecked() )
  302. ostream << raB.toHMSString() << space;
  303. else
  304. if(RACheckBatch->isChecked() )
  305. ostream << raB.toHMSString() << space;
  306. // Read DEC and write in ostream if corresponds
  307. if(DecCheckBatch->isChecked() ) {
  308. decB = dms::fromString( fields[i], true);
  309. i++;
  310. } else
  311. decB = DecBoxBatch->createDms();
  312. if ( AllRadioBatch->isChecked() )
  313. ostream << decB.toDMSString() << space;
  314. else
  315. if(DecCheckBatch->isChecked() )
  316. ostream << decB.toDMSString() << space;
  317. // Read Epoch and write in ostream if corresponds
  318. if(EpochCheckBatch->isChecked() ) {
  319. epoch0B = fields[i];
  320. i++;
  321. } else
  322. epoch0B = EpochBoxBatch->text();
  323. if ( AllRadioBatch->isChecked() )
  324. ostream << epoch0B << space;
  325. else
  326. if(EpochCheckBatch->isChecked() )
  327. ostream << epoch0B << space;
  328. // Read vlsr and write in ostream if corresponds
  329. if(InputVelocityCheckBatch->isChecked() ) {
  330. vlsrB = fields[i].toDouble();
  331. i++;
  332. } else
  333. vlsrB = InputVelocityComboBatch->currentText().toDouble();
  334. if ( AllRadioBatch->isChecked() )
  335. ostream << vlsrB << space;
  336. else
  337. if(InputVelocityCheckBatch->isChecked() )
  338. ostream << vlsrB << space;
  339. // Read Longitude and write in ostream if corresponds
  340. if (LongCheckBatch->isChecked() ) {
  341. longB = dms::fromString( fields[i],true);
  342. i++;
  343. } else
  344. longB = LongitudeBoxBatch->createDms(true);
  345. if ( AllRadioBatch->isChecked() )
  346. ostream << longB.toDMSString() << space;
  347. else
  348. if (LongCheckBatch->isChecked() )
  349. ostream << longB.toDMSString() << space;
  350. // Read Latitude
  351. if (LatCheckBatch->isChecked() ) {
  352. latB = dms::fromString( fields[i], true);
  353. i++;
  354. } else
  355. latB = LatitudeBoxBatch->createDms(true);
  356. if ( AllRadioBatch->isChecked() )
  357. ostream << latB.toDMSString() << space;
  358. else
  359. if (LatCheckBatch->isChecked() )
  360. ostream << latB.toDMSString() << space;
  361. // Read height and write in ostream if corresponds
  362. if(ElevationCheckBatch->isChecked() ) {
  363. heightB = fields[i].toDouble();
  364. i++;
  365. } else
  366. heightB = ElevationBoxBatch->text().toDouble();
  367. if ( AllRadioBatch->isChecked() )
  368. ostream << heightB << space;
  369. else
  370. if(ElevationCheckBatch->isChecked() )
  371. ostream << heightB << space;
  372. // We make the first calculations
  373. spB = SkyPoint (raB, decB);
  374. dt0B.setFromEpoch(epoch0B);
  375. vhB = spB.vHeliocentric(vlsrB, dt0B.djd());
  376. jd0 = KStarsDateTime(dtB,utB).djd();
  377. vgB = spB.vGeocentric(vlsrB, jd0);
  378. geoPlace->setLong( longB );
  379. geoPlace->setLat( latB );
  380. geoPlace->setHeight( heightB );
  381. dms gsidt = KStarsDateTime(dtB,utB).gst();
  382. geoPlace->TopocentricVelocity(vtopo, gsidt);
  383. spB.ra().SinCos(sra, cra);
  384. spB.dec().SinCos(sdc, cdc);
  385. vtB = vgB - (vtopo[0]*cdc*cra + vtopo[1]*cdc*sra + vtopo[2]*sdc);
  386. ostream << vhB << space << vgB << space << vtB << endl;
  387. }
  388. fOut.close();
  389. }