[Kst] branches/kstars/summer/kdeedu/kstars/kstars

Akarsh Simha akarshsimha at gmail.com
Mon Jun 2 10:01:41 CEST 2008


SVN commit 815560 by asimha:

Reimplementing StarComponent::readData to read data from the binary
files.

This may have created several bugs, which still need to be hunted down
and fixed.

Note that byteswapping isn't yet implemented, which means the data
files provided work only on Little Endian systems for now. For other
systems, the data files need to be recreated using the utilities in
kstars/data/tools

CCMAIL:kstars-devel at kde.org



 M  +11 -4     binfilehelper.cpp  
 M  +113 -46   skycomponents/starcomponent.cpp  
 M  +25 -1     skycomponents/starcomponent.h  
 M  +1 -1      starobject.cpp  


--- branches/kstars/summer/kdeedu/kstars/kstars/binfilehelper.cpp #815559:815560
@@ -120,11 +120,17 @@
 	    errorMessage.sprintf("Found ID %u, at the location where ID %u was expected", ID, i);
 	    return ERR_INDEX_IDMISMATCH;
 	}
-	fread(&offset, 4, 1, fileHandle);
-	fread(&nrecs, 2, 1, fileHandle);
+	if(!fread(&offset, 4, 1, fileHandle)) {
+	    errorMessage.sprintf("Table truncated before expected! Read i = %d index entries so far", i);
+	    return ERR_BADSEEK;
+	}
+	if(!fread(&nrecs, 2, 1, fileHandle)) {
+	    errorMessage.sprintf("Table truncated before expected! Read i = %d index entries so far", i);
+	    return ERR_BADSEEK;
+	}
 	if(prev_offset != 0 && prev_nrecs != (-prev_offset + offset)/recordSize) { 
-	    errorMessage.sprintf("Expected %u  = (%X - %x) / %d records, but found %u, in index entry %u", 
-				    (offset - prev_offset) / recordSize, offset, prev_offset, recordSize, nrecs, i);
+	    errorMessage.sprintf("Expected %u  = (%X - %x) / %x records, but found %u, in index entry %u", 
+				    (offset - prev_offset) / recordSize, offset, prev_offset, recordSize, prev_nrecs, i - 1);
 	    return ERR_INDEX_BADOFFSET;
 	}
 	indexOffset.append( offset );
@@ -154,6 +160,7 @@
     case ERR_INDEX_TRUNC:
     case ERR_INDEX_BADID:
     case ERR_INDEX_IDMISMATCH:
+    case ERR_BADSEEK:
     case ERR_INDEX_BADOFFSET: {
 	indexOffset.clear();
 	indexCount.clear();
--- branches/kstars/summer/kdeedu/kstars/kstars/skycomponents/starcomponent.cpp #815559:815560
@@ -36,6 +36,8 @@
 #include "skylabeler.h"
 #include "kstarssplash.h"
 
+#include "binfilehelper.h"
+
 StarComponent::StarComponent(SkyComponent *parent )
         : ListComponent(parent), m_reindexNum(J2000), m_FaintMagnitude(-5.0)
 {
@@ -44,6 +46,7 @@
     m_starIndex = new StarIndex();
     for (int i = 0; i < m_skyMesh->size(); i++) {
         m_starIndex->append( new StarList() );
+	m_readOffset.append( 0 );
     }
     m_highPMStars.append( new HighPMStarList( 840.0 ) );
     m_highPMStars.append( new HighPMStarList( 304.0 ) );
@@ -73,7 +76,7 @@
     emitProgressText( i18n("Loading stars" ) );
     m_Data = data;
 
-    readLineNumbers();
+    //    readLineNumbers();
     readData( Options::magLimitDrawStar() );
 
     //adjust maglimit for ZoomLevel
@@ -165,8 +168,9 @@
         return;
 
     // bail out if there are no more lines to read
-    if ( m_lastLineNum >= m_lineNumber[ MAX_LINENUMBER_MAG ] )
-        return;
+    //    if ( m_lastLineNum >= m_lineNumber[ MAX_LINENUMBER_MAG ] )
+    //    return;
+    return;
 
     m_reloadSplash = new KStarsSplash( 0,
                                        i18n("Please wait while loading faint stars ...") );
@@ -300,8 +304,11 @@
 
 }
 
+// TODO: Come up with a way of implementing quick magnitude searches with binary data
+/*
 void StarComponent::readLineNumbers()
 {
+  
     KSFileReader fileReader;
     if ( ! fileReader.open( "starlnum.idx" ) ) return;
 
@@ -317,7 +324,9 @@
         m_lineNumber[ mag ] = lineNumber;
     }
     m_validLineNums = true;
+  
 }
+*/
 
 int StarComponent::lineNumber( float magF )
 {
@@ -329,69 +338,126 @@
     return m_lineNumber[ mag ];
 }
 
-void StarComponent::readData( float newMagnitude )
+void StarComponent::readData( float newMagnitude ) 
 {
-    // only load star data if the new magnitude is fainter than we've seen so far
-    if ( newMagnitude <= m_FaintMagnitude ) return;
-    float currentMag = m_FaintMagnitude;
-    m_FaintMagnitude = newMagnitude;  // store new highest magnitude level
+    // TODO: We may want to remove the newMagnitude argument. Currently, it is deprecated
 
+    // We break from Qt / KDE API and use traditional file handling here, to obtain speed
+    FILE *dataFile, *nameFile;
+    bool swapBytes = false;
+    dms RA, Dec;
+    BinFileHelper dataReader, nameReader;
+    unsigned long nrecords;
+    QString name, gname, visibleName;
+    kDebug() << "In readData" << endl;
+    // DEPRECATED : only load star data if the new magnitude is fainter than we've seen so far
+    if ( newMagnitude <= m_FaintMagnitude || newMagnitude > 8.0 ) return; // TODO: Create the solution to handle > 8.0 mag stars
+    //float currentMag = m_FaintMagnitude;
+    m_FaintMagnitude = 8.00;  // TODO: Find out if there could be a solution to load shallow stars dynamically
+    kDebug() << "Not out of readData! ========================================" << endl;
     // prepare to index stars to this date
     m_skyMesh->setKSNumbers( &m_reindexNum );
 
-    KSFileReader fileReader;
-    if ( ! fileReader.open( "stars.dat" ) ) return;
+    if((dataFile = dataReader.openFile("shallowstars.dat")) == NULL) {
+	kDebug() << "Could not open data file shallowstars.dat" << endl;
+	return;
+    }
 
-    int totalLines = lineNumber( newMagnitude );
-    totalLines -= lineNumber( currentMag );
-    int updates;
-    if ( totalLines > 50000 )
-        updates = 100;
-    else if ( totalLines > 20000 )
-        updates =  50;
-    else if ( totalLines > 10000 )
-        updates =  10;
-    else
-        updates =   5;
+    if(!(nameFile = nameReader.openFile("starnames.dat"))) {
+	kDebug() << "Could not open data file starnames.dat" << endl;
+	return;
+    }
 
-    // only show progress with valid line numbers
-    if ( m_validLineNums )
-        fileReader.setProgress( i18n("Loading stars"), totalLines, updates );
+    if(!dataReader.readHeader()) {
+	kDebug() << "Error reading shallowstars.dat header : " << dataReader.getErrorNumber() << " : " << dataReader.getError() << endl;
+	return;
+    }
 
-    if (m_lastFilePos > 0 ) fileReader.seek( m_lastFilePos );
+    if(!nameReader.readHeader()) {
+	kDebug() << "Error reading starnames.dat header : " << nameReader.getErrorNumber() << " : " << nameReader.getError() << endl;
+	return;
+    }
 
-    while ( fileReader.hasMoreLines() ) {
-        QString line = fileReader.readLine();
+    fseek(nameFile, nameReader.getDataOffset(), SEEK_SET);
 
-        // DIY because QTextStream::pos() can take many seconds!
-        m_lastFilePos += line.length() + 1;
+    long int nstars = 0;
+    Trixel expectedTrixelId = -1;
 
-        if ( line.isEmpty() ) continue;       // ignore blank lines
-        if ( line.at(0) == '#' ) continue;    // ignore comments
+    for(int i = 0; i < m_skyMesh -> size(); ++i) {
 
-        StarObject* star = processStar( line );
-        currentMag = star->mag();
+	if(fseek(dataFile, dataReader.getOffset(i), SEEK_SET))
+	    kDebug() << "ERROR: Could not seek to offset " << dataReader.getOffset(i) << " to find trixel #" << i << endl;
 
-        objectList().append( star );
-        Trixel trixel = m_skyMesh->indexStar( star );
-        m_starIndex->at( trixel )->append( star );
-        double pm = star->pmMagnitude();
+	for(unsigned j = 0; j < (unsigned long)dataReader.getRecordCount(i); ++j) {
+	    
+	    if(!fread(&stardata, sizeof(starData), 1, dataFile)){
+		kDebug() << "ERROR: Could not read starData structure for star #" << j << " under trixel #" << i << endl;
+	    }
 
-        for (int j = 0; j < m_highPMStars.size(); j++ ) {
-            HighPMStarList* list = m_highPMStars.at( j );
-            if ( list->append( trixel, star, pm ) ) break;
-        }
+	    // TODO: Add byteswapping code
 
-        fileReader.showProgress();
+	    RA.setH(stardata.RA/1000000.0);
+	    Dec.setD(stardata.Dec/100000.0);
 
-        if ( currentMag > m_FaintMagnitude )   // Done!
-            break;
+	    gname = "";
+	    name = "";
+	    visibleName = "";
+
+	    if(stardata.flags & 0x01) {
+		// Named Star - Read one Name
+		if(!fread(&starname, sizeof(starName), 1, nameFile))
+		    kDebug() << "ERROR: fread() call on nameFile failed in trixel " << i << " star " << j << endl;
+		name = QByteArray(starname.longName, 32);
+		gname = QByteArray(starname.bayerName, 8);
+		if ( ! gname.isEmpty() && gname.at(0) != '.')
+		    visibleName = gname;
+	    }
+	    // HEV: look up star name in internationalization filesource
+
+	    if ( name.isEmpty() ) name = i18n("star");
+	    name = i18nc("star name", name.toLocal8Bit().data());
+
+	    StarObject *star = new StarObject( RA, Dec, stardata.mag/100.0, name, visibleName, QByteArray(stardata.spec_type, 2), 
+					       stardata.dRA/10.0, stardata.dDec/10.0, stardata.parallax/10.0, 
+					       stardata.flags & 0x02, stardata.flags & 0x04 );
+	    star->EquatorialToHorizontal( data()->lst(), data()->geo()->lat() );
+	    ++nstars;
+
+	    if ( ! gname.isEmpty() ) m_genName.insert( gname, star );
+	    
+	    if ( ! name.isEmpty() && name != i18n("star") ) {
+		objectNames(SkyObject::STAR).append( name );
+	    }
+	    if ( ! gname.isEmpty() && gname != name ) {
+		objectNames(SkyObject::STAR).append( star -> gname(false) );
+	    }
+	    
+	    objectList().append( star );
+
+	    Trixel trixel = m_skyMesh->indexStar( star );
+	    m_starIndex->at( trixel )->append( star );
+	    double pm = star->pmMagnitude();
+	    if(j == 0)
+		expectedTrixelId = trixel;
+	    else
+		if(expectedTrixelId != trixel)
+		    kDebug() << "ERROR: Expected trixel Id = " << expectedTrixelId << " but found " << trixel << " instead at trixel #" << i <<", star #" << j << endl;
+	    
+	    for (int j = 0; j < m_highPMStars.size(); j++ ) {
+		HighPMStarList* list = m_highPMStars.at( j );
+		if ( list->append( trixel, star, pm ) ) break;
+	    }
+
+	}
     }
-
-    m_lastLineNum += fileReader.lineNumber();
+    dataReader.closeFile();
+    nameReader.closeFile();
+    kDebug() << "Loaded " << nstars << " stars" << endl;
 }
 
 
+// DEPRECATED
+/*
 StarObject* StarComponent::processStar( const QString &line ) {
     QString name, gname, SpType, visibleName;
     int rah, ram, ras, ras2, dd, dm, ds, ds2;
@@ -481,6 +547,7 @@
     }
     return o;
 }
+*/
 
 SkyObject* StarComponent::findStarByGenetiveName( const QString name ) {
     return m_genName.value( name );
--- branches/kstars/summer/kdeedu/kstars/kstars/skycomponents/starcomponent.h #815559:815560
@@ -116,7 +116,7 @@
     	* different magnitudes.  This allows us to estimate the number of
     	* lines that need to get read when partially reading stars.dat.
     	*/
-    void readLineNumbers();
+    //    void readLineNumbers();        // TODO: Find a way to do it now!
 
     /* @short returns an estimate of the stars.dat line number for a given
     	* star magnitude.
@@ -147,6 +147,7 @@
 
     QVector<HighPMStarList*> m_highPMStars;
     QHash<QString, SkyObject*> m_genName;
+    QVector<qint16> m_readOffset;
 
     /** 
      *@short adds a label to the lists of labels to be drawn prioritized
@@ -187,6 +188,29 @@
     	*/
     StarObject* processStar( const QString &line );
 
+    typedef struct starData {
+        int32_t RA;
+        int32_t Dec;
+        int32_t dRA;
+        int32_t dDec;
+        int32_t parallax;
+        int32_t HD;
+        int16_t mag;
+        int16_t bv_index;
+        char spec_type[2];
+        char flags;
+        char unused;
+    } starData;
+
+    typedef struct starName {
+	char bayerName[8];
+	char longName[32];
+    } starName;
+
+    starData stardata;
+    starName starname;
+    
+
 };
 
 #endif
--- branches/kstars/summer/kdeedu/kstars/kstars/starobject.cpp #815559:815560
@@ -403,7 +403,7 @@
         isize = 14;
     }
 
-    QString imKey = SpType.at(0)+QString("%1").arg(isize);
+    QString imKey = ((SpType != "") ? SpType.at(0) : (QChar)' ') + QString("%1").arg(isize);
     float offset = 0.5*StarImage[imKey].width();
     psky.drawPixmap( QPointF(x-offset, y-offset), StarImage[imKey] );
 


More information about the Kst mailing list