diff --git a/src/sgpsdp/sgp4sdp4.h b/src/sgpsdp/sgp4sdp4.h index 58c36131..389d40d7 100644 --- a/src/sgpsdp/sgp4sdp4.h +++ b/src/sgpsdp/sgp4sdp4.h @@ -295,6 +295,7 @@ void ClearFlag(int flag); /* sgp_in.c */ int Checksum_Good(char *tle_set); int Good_Elements(char *tle_set); +int Convert_Sat_Catnr(const char *catstr); void Convert_Satellite_Data(char *tle_set, tle_t * tle); int Get_Next_Tle_Set(char lines[3][80], tle_t * tle); void select_ephemeris(sat_t * sat); diff --git a/src/sgpsdp/sgp_in.c b/src/sgpsdp/sgp_in.c index a794312a..716621aa 100644 --- a/src/sgpsdp/sgp_in.c +++ b/src/sgpsdp/sgp_in.c @@ -146,6 +146,41 @@ int Good_Elements(char *tle_set) return (1); } +/* Decode a 5-character NORAD catalog field. + All-digit fields (leading spaces allowed) keep the historic + atoi() behavior. IDs >= 100000 use Alpha-5; I and O are + skipped as first characters so they are not confused with 1 and 0. */ +int Convert_Sat_Catnr(const char *catstr) +{ + char first; + int base, i; + + if (catstr == NULL) + return 0; + + first = catstr[0]; + if (g_ascii_isdigit(first) || (first == ' ')) + return atoi(catstr); + + first = g_ascii_toupper(first); + if ((first >= 'A') && (first <= 'H')) + base = 10 + (first - 'A'); + else if ((first >= 'J') && (first <= 'N')) + base = 18 + (first - 'J'); + else if ((first >= 'P') && (first <= 'Z')) + base = 23 + (first - 'P'); + else + return 0; + + for (i = 1; i < 5; i++) + { + if (!g_ascii_isdigit(catstr[i])) + return 0; + } + + return (base * 10000 + atoi(&catstr[1])); +} + /* Converts the strings in a raw two-line element set */ /* to their intended numerical values. No processing */ /* of these values is done, e.g. from deg to rads etc. */ @@ -157,7 +192,7 @@ void Convert_Satellite_Data(char *tle_set, tle_t * tle) /* Satellite's catalogue number */ strncpy(buff, &tle_set[2], 5); buff[5] = '\0'; - tle->catnr = atoi(buff); + tle->catnr = Convert_Sat_Catnr(buff); /* International Designator for satellite */ strncpy(tle->idesg, &tle_set[9], 8); diff --git a/src/sgpsdp/test-001.c b/src/sgpsdp/test-001.c index 30440859..12f2cabb 100644 --- a/src/sgpsdp/test-001.c +++ b/src/sgpsdp/test-001.c @@ -176,6 +176,42 @@ int main(void) } } + /* Alpha-5 catalog numbers (IDs >= 100000). */ + { + char a5_str[3][80]; + tle_t a5_tle; + + if (Convert_Sat_Catnr("25544") != 25544) + { + printf("Convert_Sat_Catnr(25544) returned %d\n", + Convert_Sat_Catnr("25544")); + return 1; + } + if (Convert_Sat_Catnr("a1286") != 101286) + { + printf("Convert_Sat_Catnr(a1286) returned %d (expected 101286)\n", + Convert_Sat_Catnr("a1286")); + return 1; + } + + strcpy(a5_str[0], "OBJECT A1286"); + strcpy(a5_str[1], + "1 A1286U 26249D 26310.97532605 .00001678 00000-0 14252-3 0 9998"); + strcpy(a5_str[2], + "2 A1286 45.2801 232.2047 0090652 210.3603 149.2004 14.99515474 207"); + if (Get_Next_Tle_Set(a5_str, &a5_tle) != 1) + { + printf("Get_Next_Tle_Set() rejected an Alpha-5 catalog TLE\n"); + return 1; + } + if (a5_tle.catnr != 101286) + { + printf("Alpha-5 catalog A1286 read as %d (expected 101286)\n", + a5_tle.catnr); + return 1; + } + } + select_ephemeris(&sat); printf("\nDEEP_SPACE_EPHEM: %d (expected 0)\n\n", diff --git a/src/tle-update.c b/src/tle-update.c index b725a815..69c0e16a 100644 --- a/src/tle-update.c +++ b/src/tle-update.c @@ -998,7 +998,7 @@ static gint read_fresh_tle(const gchar * dir, const gchar * fnam, catstr[i - 2] = tle_str[1][i]; } catstr[5] = '\0'; - catnr = (guint) g_ascii_strtod(catstr, NULL); + catnr = (guint) Convert_Sat_Catnr(catstr); if (Get_Next_Tle_Set(tle_str, &tle) != 1)