I'm currently trying to read some star data from the BSC. I've managed to read in the header and that shows up more or less correct, but I'm having trouble reading in the star data itself. The specification states that values are stored as 4/8-byte "Real" numbers, which I assumed meant floats/doubles, but the Ascension and Declination I get are all wrong, a good bit above the trillions for one and zero for the other. The magnitude is also wrong, despite it just being an integer, which I could read fine in the header. Here's and image of the output thus far. Any know what I'm doing wrong?
Asked
Active
Viewed 100 times
-2
-
2You might have a [byte order](https://en.wikipedia.org/wiki/Endianness) problem. – Steve Summit May 27 '22 at 17:08
-
Doesn't seem so. Importing the `byteswap.h` header and running `__bswap_64` on the Ascension and Declination variables just causes both of them to become 0. – ramstaandy May 27 '22 at 17:56
-
Okay, well, sorry, but it was worth a shot. The next possibility is simply that you're not quite reading the right bytes. If you're off by even one, that would obviously totally perturb the value. – Steve Summit May 27 '22 at 19:02
-
That's certainly possible. but if so, I'm not sure where it could happen. As said, the header section is read fine, after which there ought to only be entries (unless there's some kind of undocumented filler). The file pointer should be progressed just past the header before beginning to read the entry, so there shouldn't be any opportunity for misalignment. Maybe there's first a newline of some sort? But that wouldn't make sense for a binary file. Also, sorry if my previous response came across as aggressive, that wasn't my intention. – ramstaandy May 27 '22 at 21:57
-
I took a look at those files, and extracted fields from them using a general-purpose binary file reader I have. The data looks okay. I can't quite make sense of the RA values, though. So I suggest that you keep trying, and focus on the other values first. Are you getting reasonable values for the Spectral Type and V Magnitude? The next thing to look at would be the catalog number, which is stored as a float, but is less problematic to interpret than the angles. – Steve Summit May 27 '22 at 22:27
1 Answers
0
Alright, after some more testing, I managed to solve my problem. The crucial step was to abandon the binary file altogether and use the ASCII file instead. I had some problems reading from it before due to how it was formatted, but I came up with a method that worked:
/* Struct to store all the attributes I'm interested in */
struct StarData_t{
char Name[11];
char SpType[21];
float GLON, GLAT, Vmag;
};
int main()
{
/* Allocate a list of the structs
(the BSC has 9110 entries) */
struct StarData_t stars[9110];
/* Open the catalog */
FILE *fptr = fopen("catalog", "r");
if(fptr != NULL){
/* Create a buffer for storing the star entries.
The ASCII file has one entry per line.
Each line has a max length of 197,
which becomes 199 with the newline and null terminator,
so I round up to 200. */
size_t star_size = 200;
char *star_buffer;
star_buffer = (char *)malloc(star_size * sizeof(char));
/* Create a buffer for reading in the numbers.
The catalog has no numbers longer than 6 characters,
So I allocate 7 to account for the newline. */
char data_buffer[7];
/* For each entry in the BSC... */
for(int i = 0; i < 9110; i++){
/* Read the line to the buffer */
getline(&star_buffer, &star_size, fptr);
/* And put the data in the matching index,
Using the data buffer to create the floats */
// GLON
strncpy(data_buffer, &(star_buffer[90]), 6);
data_buffer[6] = '\0';
stars[i].GLON = fmod(atof(data_buffer)+180, 360)-180;
// GLAT
strncpy(data_buffer, &(star_buffer[96]), 6);
data_buffer[6] = '\0';
stars[i].GLAT = atof(data_buffer);
// Vmag
strncpy(data_buffer, &(star_buffer[102]), 5);
data_buffer[5] = '\0';
stars[i].Vmag = atof(data_buffer);
// Name
strncpy(stars[i].Name, &(star_buffer[4]), 10);
stars[i].Name[10] = '\0';
// Spectral Type
strncpy(stars[i].SpType, &(star_buffer[127]), 20);
stars[i].SpType[20] = '\0';
printf("Name: %s, Long: %7.2f, Lat: %6.2f, Vmag: %4.2f, SpType: %s\n", stars[i].Name, stars[i].GLON, stars[i].GLAT, stars[i].Vmag, stars[i].SpType);
}
free(star_buffer);
}
}
Hope this is useful!

ramstaandy
- 1
- 2