00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "modcalcplanets.h"
00019 #include "modcalcplanets.moc"
00020 #include "dms.h"
00021 #include "dmsbox.h"
00022 #include "kstars.h"
00023 #include "kstarsdata.h"
00024 #include "kssun.h"
00025 #include "ksplanet.h"
00026 #include "ksmoon.h"
00027 #include "kspluto.h"
00028 #include "libkdeedu/extdate/extdatetimeedit.h"
00029 #include "ksnumbers.h"
00030
00031 #include <qcombobox.h>
00032 #include <qdatetimeedit.h>
00033 #include <qstring.h>
00034 #include <qtextstream.h>
00035 #include <kfiledialog.h>
00036 #include <kmessagebox.h>
00037 #include <qcheckbox.h>
00038 #include <qradiobutton.h>
00039
00040
00041 modCalcPlanets::modCalcPlanets(QWidget *parentSplit, const char *name) : modCalcPlanetsDlg (parentSplit,name) {
00042 showCurrentDateTime();
00043 showLongLat();
00044 raBox->setDegType(FALSE);
00045 show();
00046 }
00047
00048 modCalcPlanets::~modCalcPlanets(){
00049 }
00050
00051 void modCalcPlanets::showCurrentDateTime (void)
00052 {
00053 KStars *ks = (KStars*) parent()->parent()->parent();
00054 KStarsDateTime dt = ks->data()->geo()->LTtoUT( KStarsDateTime::currentDateTime() );
00055
00056 dateBox->setDate( dt.date() );
00057 timeBox->setTime( dt.time() );
00058
00059 dateBoxBatch->setDate( dt.date() );
00060 utBoxBatch->setTime( dt.time() );
00061 }
00062
00063 KStarsDateTime modCalcPlanets::getDateTime (void)
00064 {
00065 return KStarsDateTime( dateBox->date() , timeBox->time() );
00066 }
00067
00068 void modCalcPlanets::showLongLat(void)
00069 {
00070
00071 KStars *ks = (KStars*) parent()->parent()->parent();
00072 longBox->show( ks->geo()->lng() );
00073 latBox->show( ks->geo()->lat() );
00074
00075 longBoxBatch->show( ks->geo()->lng() );
00076 latBoxBatch->show( ks->geo()->lat() );
00077 }
00078
00079
00080 GeoLocation modCalcPlanets::getObserverPosition (void)
00081 {
00082 GeoLocation geoPlace;
00083
00084 geoPlace.setLong( longBox->createDms() );
00085 geoPlace.setLat( latBox->createDms() );
00086 geoPlace.setHeight( 0.0 );
00087
00088 return geoPlace;
00089 }
00090
00091 void modCalcPlanets::slotComputePosition (void)
00092 {
00093 KStarsData *kd = (KStarsData*) parent()->parent()->parent();
00094 KStarsDateTime dt = getDateTime();
00095 long double julianDay = dt.djd();
00096 GeoLocation position( getObserverPosition() );
00097 KSNumbers num( julianDay );
00098 dms LST( position.GSTtoLST( dt.gst() ) );
00099
00100
00101 KSPlanet Earth( kd, I18N_NOOP( "Earth" ));
00102 Earth.findPosition( &num );
00103
00104
00105 if (planetComboBox->currentItem() == 0 ) {
00106 KSPlanet p( kd, I18N_NOOP( "Mercury" ));
00107 p.findPosition( &num, position.lat(), &LST, &Earth);
00108 p.EquatorialToHorizontal( &LST, position.lat());
00109 showCoordinates( p );
00110 }
00111 else if(planetComboBox->currentItem() == 1) {
00112 KSPlanet p( kd, I18N_NOOP( "Venus" ));
00113 p.findPosition( &num, position.lat(), &LST, &Earth);
00114 p.EquatorialToHorizontal( &LST, position.lat());
00115 showCoordinates( p );
00116 }
00117 else if(planetComboBox->currentItem() == 2) {
00118 showCoordinates( Earth );
00119 }
00120 else if(planetComboBox->currentItem() == 3) {
00121 KSPlanet p( kd, I18N_NOOP( "Mars" ));
00122 p.findPosition( &num, position.lat(), &LST, &Earth);
00123 p.EquatorialToHorizontal( &LST, position.lat());
00124 showCoordinates( p );
00125 }
00126 else if(planetComboBox->currentItem() == 4) {
00127 KSPlanet p( kd, I18N_NOOP( "Jupiter" ));
00128 p.findPosition( &num, position.lat(), &LST, &Earth);
00129 p.EquatorialToHorizontal( &LST, position.lat());
00130 showCoordinates( p );
00131 }
00132 else if(planetComboBox->currentItem() == 5) {
00133 KSPlanet p( kd, I18N_NOOP( "Saturn" ));
00134 p.findPosition( &num, position.lat(), &LST, &Earth);
00135 p.EquatorialToHorizontal( &LST, position.lat());
00136 showCoordinates( p );
00137 }
00138 else if(planetComboBox->currentItem() == 6) {
00139 KSPlanet p( kd, I18N_NOOP( "Uranus" ));
00140 p.findPosition( &num, position.lat(), &LST, &Earth);
00141 p.EquatorialToHorizontal( &LST, position.lat());
00142 showCoordinates( p );
00143 }
00144 else if(planetComboBox->currentItem() == 7) {
00145 KSPlanet p( kd, I18N_NOOP( "Neptune" ));
00146 p.findPosition( &num, position.lat(), &LST, &Earth);
00147 p.EquatorialToHorizontal( &LST, position.lat());
00148 showCoordinates( p );
00149 }
00150 else if(planetComboBox->currentItem() == 8) {
00151 KSPluto p( kd );
00152 p.findPosition( &num, position.lat(), &LST, &Earth);
00153 p.EquatorialToHorizontal( &LST, position.lat());
00154 showCoordinates( p );
00155 }
00156 else if(planetComboBox->currentItem() == 9) {
00157 KSMoon p( kd );
00158 p.findPosition( &num, position.lat(), &LST, &Earth);
00159 p.EquatorialToHorizontal( &LST, position.lat());
00160 showCoordinates( p );
00161 }
00162 else if(planetComboBox->currentItem() == 10) {
00163 KSSun p( kd );
00164 p.findPosition( &num, position.lat(), &LST, &Earth);
00165 p.EquatorialToHorizontal( &LST, position.lat());
00166 p.setRsun(0.0);
00167 showCoordinates( p );
00168 }
00169 }
00170
00171 void modCalcPlanets::showCoordinates( const KSPlanet &ksp) {
00172
00173 showHeliocentricEclipticCoords(ksp.helEcLong(), ksp.helEcLat(), ksp.rsun() );
00174 showGeocentricEclipticCoords(ksp.ecLong(), ksp.ecLat(), ksp.rearth() );
00175 showEquatorialCoords(ksp.ra(), ksp.dec() );
00176 showTopocentricCoords(ksp.az(), ksp.alt() );
00177
00178 }
00179
00180 void modCalcPlanets::showCoordinates( const KSMoon &ksp ) {
00181
00182 showHeliocentricEclipticCoords(ksp.helEcLong(), ksp.helEcLat(), ksp.rsun() );
00183 showGeocentricEclipticCoords(ksp.ecLong(), ksp.ecLat(), ksp.rearth() );
00184 showEquatorialCoords(ksp.ra(), ksp.dec() );
00185 showTopocentricCoords(ksp.az(), ksp.alt() );
00186
00187 }
00188 void modCalcPlanets::showCoordinates( const KSPluto &ksp ) {
00189
00190 showHeliocentricEclipticCoords(ksp.helEcLong(), ksp.helEcLat(), ksp.rsun() );
00191 showGeocentricEclipticCoords(ksp.ecLong(), ksp.ecLat(), ksp.rearth() );
00192 showEquatorialCoords(ksp.ra(), ksp.dec() );
00193 showTopocentricCoords(ksp.az(), ksp.alt() );
00194
00195 }
00196
00197 void modCalcPlanets::showCoordinates( const KSSun &ksp ) {
00198
00199 showHeliocentricEclipticCoords(ksp.helEcLong(), ksp.helEcLat(), ksp.rsun() );
00200 showGeocentricEclipticCoords(ksp.ecLong(), ksp.ecLat(), ksp.rearth() );
00201 showEquatorialCoords(ksp.ra(), ksp.dec() );
00202 showTopocentricCoords(ksp.az(), ksp.alt() );
00203
00204 }
00205
00206 void modCalcPlanets::slotClear(void){
00207 helLongBox->setText( "" );
00208 helLatBox->setText( "" );
00209 sunDistBox->setText( "" );
00210 geoLongBox->setText("");
00211 geoLatBox->setText("");
00212 earthDistBox->setText("");
00213 raBox->clearFields();
00214 decBox->clearFields();
00215 azBox->setText("");
00216 altBox->setText("");
00217 }
00218
00219 void modCalcPlanets::showHeliocentricEclipticCoords(const dms *hLong, const dms *hLat, double dist)
00220 {
00221 helLongBox->show( hLong );
00222 helLatBox->show( hLat );
00223 sunDistBox->setText( KGlobal::locale()->formatNumber( dist,6));
00224 }
00225
00226 void modCalcPlanets::showGeocentricEclipticCoords(const dms *eLong, const dms *eLat, double dist)
00227 {
00228 geoLongBox->show( eLong );
00229 geoLatBox->show( eLat );
00230 earthDistBox->setText( KGlobal::locale()->formatNumber( dist,6));
00231 }
00232
00233 void modCalcPlanets::showEquatorialCoords(const dms *ra, const dms *dec)
00234 {
00235 raBox->show( ra, FALSE );
00236 decBox->show( dec );
00237 }
00238
00239 void modCalcPlanets::showTopocentricCoords(const dms *az, const dms *el)
00240 {
00241 azBox->show( az );
00242 altBox->show( el );
00243 }
00244
00245 void modCalcPlanets::slotPlanetsCheckedBatch(){
00246
00247 if ( planetCheckBatch->isChecked() )
00248 planetComboBoxBatch->setEnabled( false );
00249 else {
00250 planetComboBoxBatch->setEnabled( true );
00251 }
00252 }
00253
00254 void modCalcPlanets::slotUtCheckedBatch(){
00255
00256 if ( utCheckBatch->isChecked() )
00257 utBoxBatch->setEnabled( false );
00258 else {
00259 utBoxBatch->setEnabled( true );
00260 }
00261 }
00262
00263 void modCalcPlanets::slotDateCheckedBatch(){
00264
00265 if ( dateCheckBatch->isChecked() )
00266 dateBoxBatch->setEnabled( false );
00267 else {
00268 dateBoxBatch->setEnabled( true );
00269 }
00270 }
00271
00272 void modCalcPlanets::slotLongCheckedBatch(){
00273
00274 if ( longCheckBatch->isChecked() )
00275 longBoxBatch->setEnabled( false );
00276 else {
00277 longBoxBatch->setEnabled( true );
00278 }
00279 }
00280
00281 void modCalcPlanets::slotLatCheckedBatch(){
00282
00283 if ( latCheckBatch->isChecked() )
00284 latBoxBatch->setEnabled( false );
00285 else {
00286 latBoxBatch->setEnabled( true );
00287 }
00288 }
00289
00290 void modCalcPlanets::slotInputFile() {
00291 QString inputFileName;
00292 inputFileName = KFileDialog::getOpenFileName( );
00293 InputLineEditBatch->setText( inputFileName );
00294 }
00295
00296 void modCalcPlanets::slotOutputFile() {
00297 QString outputFileName;
00298 outputFileName = KFileDialog::getSaveFileName( );
00299 OutputLineEditBatch->setText( outputFileName );
00300 }
00301
00302 void modCalcPlanets::slotRunBatch() {
00303
00304 QString inputFileName;
00305
00306 inputFileName = InputLineEditBatch->text();
00307
00308
00309
00310 if ( QFile::exists(inputFileName) ) {
00311 QFile f( inputFileName );
00312 if ( !f.open( IO_ReadOnly) ) {
00313 QString message = i18n( "Could not open file %1.").arg( f.name() );
00314 KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
00315 inputFileName = "";
00316 return;
00317 }
00318
00319 QTextStream istream(&f);
00320 processLines(istream);
00321 f.close();
00322 } else {
00323 QString message = i18n( "Invalid file: %1" ).arg( inputFileName );
00324 KMessageBox::sorry( 0, message, i18n( "Invalid file" ) );
00325 inputFileName = "";
00326 InputLineEditBatch->setText( inputFileName );
00327 return;
00328 }
00329 }
00330
00331 unsigned int modCalcPlanets::requiredBatchFields(void) {
00332 unsigned int i = 0;
00333
00334 if(planetCheckBatch->isChecked() )
00335 i++;
00336 if(utCheckBatch->isChecked() )
00337 i++;
00338 if(dateCheckBatch->isChecked() )
00339 i++;
00340 if (longCheckBatch->isChecked() )
00341 i++;
00342 if (latCheckBatch->isChecked() )
00343 i++;
00344
00345 return i;
00346
00347 }
00348
00349 void modCalcPlanets::processLines( QTextStream &istream ) {
00350
00351
00352
00353 QString outputFileName, lineToWrite;
00354 outputFileName = OutputLineEditBatch->text();
00355 QFile fOut( outputFileName );
00356 fOut.open(IO_WriteOnly);
00357 QTextStream ostream(&fOut);
00358 bool lineIsValid = true;
00359 QString message;
00360
00361 QString line;
00362 QString space = " ";
00363 QString planetB;
00364 unsigned int i = 0, nline = 0;
00365 QTime utB;
00366 ExtDate dtB;
00367 dms longB, latB, hlongB, hlatB, glongB, glatB, raB, decB, azmB, altB;
00368 double rSunB(0.0), rEarthB(0.0);
00369 KStarsData *kd = (KStarsData*) parent()->parent()->parent();
00370 PlanetCatalog PCat( kd );
00371 PCat.initialize();
00372
00373 QString pName[11], pNamei18n[11];
00374
00375 pName[0] = "Mercury"; pNamei18n[0]= i18n("Mercury");
00376 pName[1] = "Venus"; pNamei18n[1]= i18n("Venus");
00377 pName[2] = "Earth"; pNamei18n[2]= i18n("Earth");
00378 pName[3] = "Mars"; pNamei18n[3]= i18n("Mars");
00379 pName[4] = "Jupiter"; pNamei18n[4]= i18n("Jupiter");
00380 pName[5] = "Saturn"; pNamei18n[5]= i18n("Saturn");
00381 pName[6] = "Uranus"; pNamei18n[6]= i18n("Uranus");
00382 pName[7] = "Neptune"; pNamei18n[7]= i18n("Neptune");
00383 pName[8] = "Pluto"; pNamei18n[8]= i18n("Pluto");
00384 pName[9] = "Sun"; pNamei18n[9]= i18n("Sun");
00385 pName[10] = "Moon"; pNamei18n[10]= i18n("Moon");
00386
00387 unsigned int numberOfRequiredFields = requiredBatchFields();
00388
00389 while ( ! istream.eof() ) {
00390 lineToWrite="";
00391 line = istream.readLine();
00392 line.stripWhiteSpace();
00393
00394
00395
00396 QStringList fields = QStringList::split( " ", line );
00397
00398 if (fields.count() != numberOfRequiredFields ) {
00399 lineIsValid = false;
00400 kdWarning() << i18n( "Incorrect number of fields in line %1: " ).arg(nline)
00401 << i18n( "Present fields %1. " ).arg(fields.count())
00402 << i18n( "Required fields %1. " ).arg(numberOfRequiredFields) << endl;
00403 nline++;
00404 continue;
00405 }
00406
00407 i = 0;
00408 if(planetCheckBatch->isChecked() ) {
00409 planetB = fields[i];
00410 unsigned int result = 1;
00411 int j = 0;
00412 while (j < 11) {
00413
00414 result = QString::compare( pNamei18n[j] , planetB );
00415 if (result == 0)
00416 break;
00417 j++;
00418 }
00419 if (j == 11) {
00420 kdWarning() << i18n("Unknown planet ")
00421 << fields[i]
00422 << i18n(" in line %1: ").arg(nline) << endl;
00423 continue;
00424 }
00425 i++;
00426 } else
00427 planetB = planetComboBoxBatch->currentText( );
00428
00429 if ( allRadioBatch->isChecked() ) {
00430 lineToWrite = planetB;
00431 lineToWrite += space;
00432 }
00433 else
00434 if(planetCheckBatch->isChecked() ) {
00435 lineToWrite = planetB;
00436 lineToWrite += space;
00437 }
00438
00439
00440
00441 if(utCheckBatch->isChecked() ) {
00442 utB = QTime::fromString( fields[i] );
00443 if ( !utB.isValid() ) {
00444 kdWarning() << i18n( "Line %1 contains an invalid time" ).arg(nline) << endl;
00445 lineIsValid=false;
00446 nline++;
00447 continue;
00448 }
00449 i++;
00450 } else
00451 utB = utBoxBatch->time();
00452
00453 if ( allRadioBatch->isChecked() )
00454 lineToWrite += utB.toString().append(space);
00455 else
00456 if(utCheckBatch->isChecked() )
00457 lineToWrite += utB.toString().append(space);
00458
00459
00460
00461 if(dateCheckBatch->isChecked() ) {
00462 dtB = ExtDate::fromString( fields[i], Qt::ISODate );
00463 if ( !dtB.isValid() ) {
00464 kdWarning() << i18n( "Line %1 contains an invalid date: " ).arg(nline) <<
00465 fields[i] << endl ;
00466 lineIsValid=false;
00467 nline++;
00468 continue;
00469 }
00470 i++;
00471 } else
00472 dtB = dateBoxBatch->date();
00473 if ( allRadioBatch->isChecked() )
00474 lineToWrite += dtB.toString().append(space);
00475 else
00476 if(dateCheckBatch->isChecked() )
00477 lineToWrite += dtB.toString().append(space);
00478
00479
00480
00481 if (longCheckBatch->isChecked() ) {
00482 longB = dms::fromString( fields[i],TRUE);
00483 i++;
00484 } else
00485 longB = longBoxBatch->createDms(TRUE);
00486
00487 if ( allRadioBatch->isChecked() )
00488 lineToWrite += longB.toDMSString() + space;
00489 else
00490 if (longCheckBatch->isChecked() )
00491 lineToWrite += longB.toDMSString() + space;
00492
00493
00494
00495 if (latCheckBatch->isChecked() ) {
00496 latB = dms::fromString( fields[i], TRUE);
00497 i++;
00498 } else
00499 latB = latBoxBatch->createDms(TRUE);
00500 if ( allRadioBatch->isChecked() )
00501 lineToWrite += latB.toDMSString() + space;
00502 else
00503 if (latCheckBatch->isChecked() )
00504 lineToWrite += latB.toDMSString() + space;
00505
00506 KStarsDateTime edt( dtB, utB );
00507 dms LST = edt.gst().Degrees() + longB.Degrees();
00508
00509 KSNumbers num( edt.djd() );
00510
00511 PCat.findPosition( &num, &latB, &LST );
00512 PCat.EquatorialToHorizontal(&LST, &latB);
00513
00514 KSPlanet Earth( kd, I18N_NOOP( "Earth" ));
00515 Earth.findPosition( &num );
00516
00517 KSMoon Moon( kd );
00518 Moon.findPosition( &num, &latB, &LST, &Earth );
00519 Moon.EquatorialToHorizontal( &LST, &latB );
00520
00521 int result = 1;
00522 int jp = -1;
00523 while (result != 0 && jp < 10) {
00524 jp++;
00525 result = QString::compare( pNamei18n[jp] , planetB );
00526 }
00527
00528 if (jp < 11) {
00529 if (jp < 10) {
00530
00531 hlongB.setD(PCat.findByName( pName[jp] )->helEcLong()->Degrees());
00532 hlatB.setD( PCat.findByName( pName[jp] )->helEcLat()->Degrees());
00533 rSunB = PCat.findByName( pName[jp] )->rsun();
00534
00535
00536 glongB .setD( PCat.findByName( pName[jp] )->ecLong()->Degrees() );
00537 glatB.setD( PCat.findByName( pName[jp] )->ecLat()->Degrees() );
00538 rEarthB = PCat.findByName( pName[jp] )->rearth();
00539
00540
00541 decB.setD( PCat.findByName( pName[jp] )->dec()->Degrees() );
00542 raB.setD( PCat.findByName( pName[jp] )->ra()->Degrees() );
00543
00544
00545 azmB.setD( PCat.findByName( pName[jp] )->az()->Degrees() );
00546 altB.setD( PCat.findByName( pName[jp] )->alt()->Degrees() );
00547 } else {
00548
00549
00550 hlongB.setD( Moon.helEcLong()->Degrees() );
00551 hlatB.setD( Moon.helEcLat()->Degrees() );
00552
00553
00554 glongB .setD( Moon.ecLong()->Degrees() );
00555 glatB.setD( Moon.ecLat()->Degrees() );
00556 rEarthB = Moon.rearth();
00557
00558
00559 decB.setD( Moon.dec()->Degrees() );
00560 raB.setD( Moon.ra()->Degrees() );
00561
00562
00563 azmB.setD( Moon.az()->Degrees() );
00564 altB.setD( Moon.alt()->Degrees() );
00565 }
00566
00567
00568 ostream << lineToWrite;
00569
00570 if ( helEclCheckBatch->isChecked() )
00571 ostream << hlongB.toDMSString() << space << hlatB.toDMSString() << space << rSunB << space ;
00572 if ( geoEclCheckBatch->isChecked() )
00573 ostream << glongB.toDMSString() << space << glatB.toDMSString() << space << rEarthB << space ;
00574 if ( equGeoCheckBatch->isChecked() )
00575 ostream << raB.toHMSString() << space << decB.toDMSString() << space ;
00576 if ( topoCheckBatch->isChecked() )
00577 ostream << azmB.toDMSString() << space << altB.toDMSString() << space ;
00578
00579 ostream << endl;
00580 }
00581 nline++;
00582
00583 }
00584
00585 if (!lineIsValid) {
00586 QString message = i18n("Errors found while parsing some lines in the input file");
00587 KMessageBox::sorry( 0, message, i18n( "Errors in lines" ) );
00588 }
00589
00590 fOut.close();
00591 }