[Kstars-devel] branches/kstars/summer/kdeedu/kstars/kstars

Akarsh Simha akarshsimha at gmail.com
Sun Jul 6 16:05:39 CEST 2008


SVN commit 828746 by asimha:


+ Fixing serious bug with regard to byte-swapping in binfiletester.c

+ Adding more fields to the data files, to store HTMesh level and
  Maximum Stars per Trixel.

Permitting for easy change of HTMesh level in mysql2bin.c and
binfiletester.c:

+ Making trixel2number and number2trixel independent of HTMesh level.

+ Consolidating all HTMesh dependence in terms of #define directives,
  making it easier to change HTMesh level

CCMAIL: kstars-devel at kde.org



 M             data/deepstars.dat  
 M             data/shallowstars.dat  
 M  +1 -1      data/tools/Makefile  
 M  +47 -25    data/tools/binfiletester.c  
 M  +54 -21    data/tools/mysql2bin.c  
 M  +12 -0     skycomponents/starcomponent.cpp  
 M  +1 -0      skycomponents/starcomponent.h  


--- branches/kstars/summer/kdeedu/kstars/kstars/data/tools/Makefile #828745:828746
@@ -19,7 +19,7 @@
 	$(CC) $(CFLAGS) `$(MYSQL_CONFIG) --cflags` $@.c $(LDFLAGS) `$(MYSQL_CONFIG) --libs` -o $@
 
 binfiletester: binfiletester.c
-	$(CC) $(CFLAGS) $@.c $(LDFLAGS) -o $@
+	$(CC) $(CFLAGS) $@.c $(LDFLAGS) -lm -o $@
 
 clean:
 	-rm binfiletester mysql2bin
--- branches/kstars/summer/kdeedu/kstars/kstars/data/tools/binfiletester.c #828745:828746
@@ -11,6 +11,8 @@
 #include <string.h>
 #include "byteswap.h"
 
+#define HTM_LEVEL 3
+
 /*
  * struct to store star data, to be written in this format, into the binary file.
  */
@@ -124,28 +126,43 @@
 
 }
 
-// WARNING: The following two functions work only for a Level 3 HTM
-int trixel2number(char *trixel) {
-    // NOTE: We do not test the validity of the trixel
-    return (trixel[4] - '0') + (trixel[3] - '0') * 4 + (trixel[2] - '0') * 16 + (trixel[1] - '0') * 64 + ((trixel[0] == 'N')?0:256);
+u_int16_t trixel2number(char *trixel) {
+    int index;
+    u_int16_t id = 0;
+    for( index = HTM_LEVEL + 1; index >= 1; --index ) {
+        id += (trixel[ index ] - '0') * (u_int16_t)round( pow(4, (index - HTM_LEVEL - 1)) );
+    }
+    id += ( ( trixel[0] == 'S' ) ? round( pow(4, HTM_LEVEL + 1) ) + 1 : 0 );
+    return id;
 }
 
-char *number2trixel(char *trixel, int number) {
+char *number2trixel(char *trixel, u_int16_t number) {
 
-    trixel[0] = ((number >= 256)?('S'+((number -= 256) - number)):'N'); // Obfuscated Code Contest Winner
-    trixel[1] = number / 64 + '0';
-    number = number % 64;
-    trixel[2] = number / 16 + '0';
-    number = number % 16;
-    trixel[3] = number / 4 + '0';
-    trixel[4] = number % 4 + '0';
+    int index;
+    u_int16_t hpv = (u_int16_t)round( pow(4, HTM_LEVEL) ) * 2;
+    if( number >= hpv ) {
+        trixel[ 0 ] = 'S';
+        number -= hpv;
+    }
+    else
+        trixel[ 0 ] = 'N';
+    hpv /= 2;
+
+    for( index = 1; index < HTM_LEVEL + 2; ++index ) {
+        trixel[ index ] = (number - (number % hpv)) / hpv + '0';
+        number = number % hpv;
+        hpv /= 4;
+    }
+
+    trixel[ HTM_LEVEL + 2 ] = '\0';
+
     return trixel;
 }
 
 int verifyIndexValidity(FILE *f) {
 
     int i;
-    char str[6];
+    char str[ HTM_LEVEL + 3 ];
     u_int16_t trixel;
     u_int32_t offset;
     u_int32_t prev_offset;
@@ -159,7 +176,6 @@
 
     prev_offset = 0;
     prev_nrecs = 0;
-    str[5]='\0';
     nerr = 0;
 
     for(i = 0; i < ntrixels; ++i) {
@@ -168,7 +184,7 @@
             +nerr;
             break;
         }
-        bswap_16( trixel );
+        if( byteswap ) bswap_16( trixel );
         if(trixel >= ntrixels) {
             fprintf(stderr, "Trixel number %u is greater than the expected number of trixels %u\n", trixel, ntrixels);
             ++nerr;
@@ -180,9 +196,9 @@
             ++nerr;
         }
         fread(&offset, 4, 1, f);
-        bswap_32( offset );
+        if( byteswap ) bswap_32( offset );
         fread(&nrecs, 2, 1, f);
-        bswap_16( nrecs );
+        if( byteswap ) bswap_16( nrecs );
         if( prev_offset != 0 && prev_nrecs != (-prev_offset + offset)/sizeof(starData) ) { 
             fprintf( stderr, "Expected %u  = (%X - %x) / 32 records, but found %u, in trixel %s\n", 
                      (offset - prev_offset) / 32, offset, prev_offset, nrecs, str );
@@ -218,15 +234,15 @@
     offset = index_offset + id * 8; // CAUTION: Change if the size of each entry in the index table changes
     fseek(f, offset, SEEK_SET);
     fread(&trix, 2, 1, f);
-    bswap_16( trix );
+    if( byteswap ) bswap_16( trix );
     if(trix != id) {
         fprintf(stderr, "ERROR: Something fishy in the index. I guessed that %s would be here, but instead I find %s. Aborting.\n", trixel, number2trixel(str,trix));
         return;
     }
     fread(&offset, 4, 1, f);
-    bswap_32( offset );
+    if( byteswap ) bswap_32( offset );
     fread(&nrecs, 2, 1, f);
-    bswap_16( nrecs );
+    if( byteswap ) bswap_16( nrecs );
 
     if(fseek(f, offset, SEEK_SET)) {
         fprintf(stderr, "ERROR: Could not seek to position %X in the file. The file is either truncated or the indexes are bogus.\n", offset);
@@ -238,7 +254,7 @@
         offset = ftell(f);
         n = (offset - data_offset)/0x20;
         fread(&data, sizeof(starData), 1, f);
-        bswap_stardata( &data );
+        if( byteswap ) bswap_stardata( &data );
         printf("Entry #%d\n", id);
         printf("\tRA = %f\n", data.RA / 1000000.0);
         printf("\tDec = %f\n", data.Dec / 100000.0);
@@ -283,22 +299,28 @@
     printf("%s", ASCII_text);
 
     fread(&endian_id, 2, 1, f);
-    if(endian_id != 0x4B53)
+    if(endian_id != 0x4B53) {
+        fprintf( stdout, "Byteswapping required\n" );
         byteswap = 1;
-    else
+    }
+    else {
+        fprintf( stdout, "Byteswapping not required\n" );
         byteswap = 0;
+    }
 
     fread(&nfields, 2, 1, f);
     if( byteswap ) bswap_16( nfields );
+    fprintf( stdout, "%d fields reported\n", nfields );
 
     for(i = 0; i < nfields; ++i) {
         fread(&(de[i]), sizeof(struct dataElement), 1, f);
-        bswap_32( de->scale );
+        if( byteswap ) bswap_32( de->scale );
         displayDataElementDescription(&(de[i]));
     }
 
     fread(&ntrixels, 2, 1, f);
-    bswap_16( ntrixels );
+    if( byteswap ) bswap_16( ntrixels );
+    fprintf( stdout, "Number of trixels reported = %d\n", ntrixels );
 
     return 1;
 }
--- branches/kstars/summer/kdeedu/kstars/kstars/data/tools/mysql2bin.c #828745:828746
@@ -14,14 +14,16 @@
 #define TRIXEL_NAME_SIZE 8
 #define DB_TBL "tycho2"
 #define DB_DB "stardb"
-#define VERBOSE 0
+#define VERBOSE 1
 #define LONG_NAME_LIMIT 32
+#define FIRST_TRIXEL "N0000"                // TODO: Change if HTM Level Changes
+#define INVALID_TRIXEL "00000"              // TODO: Change if HTM Level Changes
 #define BAYER_LIMIT 8
 #define HTM_LEVEL 3
-#define NTRIXELS 512
+#define NTRIXELS 512                         // TODO: Change if HTM Level Changes
 #define INDEX_ENTRY_SIZE 8
 #define GLOBAL_MAG_LIMIT 8.00
-#define MYSQL_STARS_PER_QUERY 1100000
+#define MYSQL_STARS_PER_QUERY 1000000
 
 /*
  * struct to store star data, to be written in this format, into the binary file.
@@ -190,6 +192,25 @@
 }
 
 /*
+ * Finds the ID of a given trixel
+ * NOTE: This function does not test whether the given input is sane
+ *
+ * trixel : String representation of the trixel to be converted
+ */
+
+u_int16_t trixel2number(char *trixel) {
+    int index;
+    u_int16_t id = 0;
+    for( index = HTM_LEVEL + 1; index >= 1; --index ) {
+        id += ( trixel[ index ] - '0' ) * (u_int32_t)round( pow(4, HTM_LEVEL + 1 - index) );
+    }
+    id += ( ( trixel[0] == 'S' ) ? round( pow(4, HTM_LEVEL + 1) ) : 0 );
+    return id;
+}
+
+
+
+/*
  * Dump the data file header.
  *
  * WARNING: Must edit everytime the definition of the starData structures changes
@@ -321,19 +342,7 @@
     *ptr = '0';
 }
 
-/*
- * Finds the ID of a given trixel
- * WARNING: Implemented only for a Level 3 HTMesh
- * NOTE: This function does not test whether the given input is sane
- *
- * trixel : String representation of the trixel to be converted
- */
 
-inline int trixel2number(char *trixel) {
-  return (trixel[4] - '0') + (trixel[3] - '0') * 4 + (trixel[2] - '0') * 16 + (trixel[1] - '0') * 64 + ((trixel[0] == 'N')?0:256);
-}
-
-
 int main(int argc, char *argv[]) {
 
   /* === Declarations === */
@@ -349,6 +358,8 @@
   unsigned long nf_header_offset;
   unsigned int names_count;
   int16_t maglim;
+  u_int8_t htm_level;
+  u_int16_t MSpT_named, MSpT_unnamed;
 
   char query[512];
   char current_trixel[HTM_LEVEL + 2 + 1];
@@ -445,7 +456,7 @@
   nf_header_offset = ftell(namefile);
 
   /* Write a bogus index entry into the namefile, to be filled with correct data later */
-  writeIndexEntry(namefile, "N0000", ftell(namefile) + INDEX_ENTRY_SIZE, 0);
+  writeIndexEntry(namefile, FIRST_TRIXEL, ftell(namefile) + INDEX_ENTRY_SIZE, 0);
 
   /* Leave space for / write a deep magnitude limit specification in the data files */
   maglim = (int)(8.00 * 100);
@@ -453,10 +464,21 @@
   maglim = (int)(-5.0 * 100);
   fwrite(&maglim, 2, 1, usf); // Bogus entry
 
+  /* Write a HTM level specification in the data file */
+  htm_level = HTM_LEVEL;
+  fwrite(&htm_level, 1, 1, nsf);
+  fwrite(&htm_level, 1, 1, usf);
+
+  /* Leave space for a specification of MSpT (Maximum Stars per Trixel) in the data files */
+  MSpT_named = MSpT_unnamed = 0;
+  fwrite(&MSpT_named, 2, 1, nsf); // Bogus entry
+  fwrite(&MSpT_unnamed, 2, 1, usf); // Bogus entry
+
+
   /* Initialize some variables */
   lim = 0;
   exitflag = 0;
-  strcpy(current_trixel, "N0000");
+  strcpy(current_trixel, FIRST_TRIXEL);
   nsf_trix_count = usf_trix_count = 0;
   nsf_trix_begin = usf_trix_begin = 2; // The 2 is to leave space for deep magnitude limit specification
   ntrixels = 0;
@@ -500,12 +522,16 @@
 
       /* Write index entries if we've changed trixel */
       if(strcmp(row[0], current_trixel)) { 
-        if(VERBOSE) { fprintf(stderr, "Trixel Changed from %s to %s!\n", current_trixel, row[0]); }
+          if(VERBOSE) { fprintf(stderr, "Trixel Changed from %s to %s = %d!\n", current_trixel, row[0], trixel2number( row[0] ) ); }
         while(strcmp(row[0],current_trixel)) {
           writeIndexEntry(nsfhead, current_trixel, ns_header_offset + NTRIXELS * INDEX_ENTRY_SIZE + nsf_trix_begin, nsf_trix_count);
           writeIndexEntry(usfhead, current_trixel, us_header_offset + NTRIXELS * INDEX_ENTRY_SIZE + usf_trix_begin, usf_trix_count);
           nsf_trix_begin = ftell(nsf);
           usf_trix_begin = ftell(usf);
+          if( nsf_trix_count > MSpT_named )
+              MSpT_named = nsf_trix_count;
+          if( usf_trix_count > MSpT_unnamed )
+              MSpT_unnamed = usf_trix_count;
           nsf_trix_count = usf_trix_count = 0;
           nextTrixel(current_trixel);
           ntrixels++;
@@ -588,7 +614,6 @@
         fprintf(stderr, "Done.\n");
 
     }
-    
     mysql_free_result(result);
     lim += MYSQL_STARS_PER_QUERY;
   }
@@ -601,18 +626,26 @@
     nsf_trix_count = usf_trix_count = 0;
     nextTrixel(current_trixel);
     ntrixels++;
-  }while(strcmp(current_trixel,"00000"));
+  }while(strcmp(current_trixel, INVALID_TRIXEL));
 
   fseek(namefile, nf_header_offset, SEEK_SET);
-  writeIndexEntry(namefile, "N0000", nf_header_offset + INDEX_ENTRY_SIZE, names_count);
+  writeIndexEntry(namefile, FIRST_TRIXEL, nf_header_offset + INDEX_ENTRY_SIZE, names_count);
 
   if(ntrixels != NTRIXELS) {
     fprintf(stderr, "ERROR: Expected %u trixels, but found %u instead. Please redefine NTRIXELS in this program, or check the source database for bogus trixels\n", NTRIXELS, ntrixels);
   }
 
   rewind(usf);
+  rewind(nsf);
   fwrite(&maglim, 2, 1, usf);
+  maglim = 8.0;
+  fwrite(&maglim, 2, 1, nsf);
+  fwrite(&htm_level, 1, 1, usf);
+  fwrite(&htm_level, 1, 1, nsf);
+  fwrite(&MSpT_unnamed, 2, 1, usf);
+  fwrite(&MSpT_named, 2, 1, nsf);
 
+
   fcloseall();
   mysql_close(&link);
     
--- branches/kstars/summer/kdeedu/kstars/kstars/skycomponents/starcomponent.cpp #828745:828746
@@ -78,8 +78,13 @@
             kDebug() << "WARNING: Header read error for deep star catalog!!" << endl;
         else {
             qint16 faintmag;
+            quint8 htm_level;
             fread( &faintmag, 2, 1, deepStarReader.getFileHandle() );
             m_FaintMagnitude = faintmag / 100.0;
+            fread( &htm_level, 1, 1, deepStarReader.getFileHandle() );
+            if( htm_level != m_skyMesh->level() )
+                kDebug() << "WARNING: Trixel level in program != that in file. EXPECT TROUBLE!" << endl;
+            fread( &MSpT, 2, 1, deepStarReader.getFileHandle() );
         }
     }
 
@@ -462,9 +467,16 @@
     fseek(dataFile, dataReader.getDataOffset(), SEEK_SET);
 
     qint16 faintmag;
+    quint8 htm_level;
+    quint16 t_MSpT;
     fread( &faintmag, 2, 1, dataFile );
+    fread( &htm_level, 1, 1, dataFile );
+    fread( &t_MSpT, 2, 1, dataFile ); // Unused
     if( faintmag / 100.0 > m_FaintMagnitude )
         m_FaintMagnitude = faintmag / 100.0;
+    if( htm_level != m_skyMesh->level() ) {
+        kDebug() << "WARNING: HTM Level in shallow star data file and HTM Level in m_skyMesh do not match. EXPECT TROUBLE" << endl;
+    }
 
     for(int i = 0; i < m_skyMesh -> size(); ++i) {
 
--- branches/kstars/summer/kdeedu/kstars/kstars/skycomponents/starcomponent.h #828745:828746
@@ -171,6 +171,7 @@
     float          m_zoomMagLimit;
     float          magLim;           // Current limiting magnitude for visible stars
     unsigned long  visibleStarCount;
+    quint16        MSpT;             // Maximum number of stars in any given trixel
 
     KStarsSplash*  m_reloadSplash;
     KStarsSplash*  m_reindexSplash;


More information about the Kstars-devel mailing list