Skip to content

Commit

Permalink
Moved code to format angles and to do text-search-and-replace to the …
Browse files Browse the repository at this point in the history
…'lunar' library (see commit 771d8a4c5f4c06cabba2e1ad19ab063 therein). These functions are of general utility, not just in Find_Orb.
  • Loading branch information
Bill-Gray committed Mar 7, 2024
1 parent bd27092 commit f7ecabd
Showing 1 changed file with 1 addition and 155 deletions.
156 changes: 1 addition & 155 deletions ephem0.cpp
Expand Up @@ -78,8 +78,6 @@ void remove_trailing_cr_lf( char *buff); /* ephem0.cpp */
const char *get_environment_ptr( const char *env_ptr); /* mpc_obs.cpp */
void set_environment_ptr( const char *env_ptr, const char *new_value);
uint64_t parse_bit_string( const char *istr); /* miscell.cpp */
int text_search_and_replace( char FAR *str, const char *oldstr,
const char *newstr); /* ephem0.cpp */
void format_dist_in_buff( char *buff, const double dist_in_au); /* ephem0.c */
int debug_printf( const char *format, ...) /* mpc_obs.cpp */
#ifdef __GNUC__
Expand All @@ -106,10 +104,6 @@ void calc_approx_planet_orientation( const int planet, /* runge.cpp */
char *mpc_station_name( char *station_data); /* mpc_obs.cpp */
FILE *fopen_ext( const char *filename, const char *permits); /* miscell.cpp */
int remove_rgb_code( char *buff); /* ephem0.cpp */
static void output_signed_angle_to_buff( char *obuff, const double angle,
const int precision); /* ephem0.cpp */
static void output_angle_to_buff( char *obuff, const double angle,
const int precision); /* ephem0.cpp */
static void put_residual_into_text( char *text, const double resid,
const int resid_format); /* ephem0.cpp */
FILE *open_json_file( char *filename, const char *env_ptr, const char *default_name,
Expand Down Expand Up @@ -3819,129 +3813,6 @@ int ephemeris_in_a_file_from_mpc_code( const char *filename,
return( rval);
}

static int64_t ten_to_the_nth( int n)
{
int64_t rval = 1;

while( n--)
rval *= 10;
return( rval);
}

/* See comments for get_ra_dec() in mpc_fmt.cpp for info on the meaning */
/* of 'precision' in this function. */

static void output_angle_to_buff( char *obuff, double angle, int precision)
{
int n_digits_to_show = 0;
int64_t power_mul, fraction;
size_t i, full_len = 12;

if( (precision >= 100 && precision <= 116) /* decimal quantity, dd.dd... */
|| ( precision >= 200 && precision <= 215)) /* decimal ddd.dd... */
{
const int two_digits = (precision <= 200);

n_digits_to_show = precision % 100;
power_mul = ten_to_the_nth( n_digits_to_show);
fraction = (int64_t)( angle * (two_digits ? 1. : 15.) * (double)power_mul + .5);

snprintf( obuff, 4, (two_digits ? "%02u" : "%03u"),
(int)( fraction / power_mul));
fraction %= power_mul;
}
else
switch( precision)
{
case -1: /* hh mm, integer minutes */
case -2: /* hh mm.m, tenths of minutes */
case -3: /* hh mm.mm, hundredths of minutes */
case -4: /* hh mm.mmm, milliminutes */
case -5: /* hh mm.mmmm */
case -6: /* hh mm.mmmmm */
case -7: /* hh mm.mmmmmm */
{
n_digits_to_show = -1 - precision;
power_mul = ten_to_the_nth( n_digits_to_show);
fraction = (int64_t)( angle * 60. * (double)power_mul + .5);
snprintf( obuff, 6, "%02u %02u", (unsigned)( fraction / ((int64_t)60 * power_mul)),
(unsigned)( fraction / power_mul) % 60);
fraction %= power_mul;
}
break;
case 0: /* hh mm ss, integer seconds */
case 1: /* hh mm ss.s, tenths of seconds */
case 2: /* hh mm ss.ss, hundredths of seconds */
case 3: /* hh mm ss.sss, thousands of seconds */
case 4: case 5: case 6: /* possible extra digits in ephems */
case 7: case 8: case 9:
case 307: /* hhmmsss, all packed together: tenths */
case 308: /* hhmmssss, all packed together: hundredths */
case 309: /* milliseconds (or milliarcseconds) */
case 310: /* formats 307-312 are the 'super-precise' formats */
case 311:
case 312: /* microseconds (or microarcseconds) */
case 400: /* (RA) ddd mm ss, integer arcseconds */
case 401: /* (RA) ddd mm ss.s, tenths of arcseconds */
case 402: /* (RA) ddd mm ss.ss, centiarcsec */
{
const char *format = "%02u %02u %02u";

if( precision >= 400)
{
format = "%03u %02u %02u";
precision -= 400;
angle *= 15.;
}
n_digits_to_show = precision % 306;
power_mul = ten_to_the_nth( n_digits_to_show);
fraction = (int64_t)( angle * 3600. * (double)power_mul + .5);
snprintf( obuff, 10, format,
(unsigned)( fraction / ((int64_t)3600 * power_mul)),
(unsigned)( fraction / ((int64_t)60 * power_mul)) % 60,
(unsigned)( fraction / power_mul) % 60);
fraction %= power_mul;
if( precision > 306) /* remove spaces: */
text_search_and_replace( obuff, " ", "");
}
break;
default: /* try to show the angle, but indicate */
if( angle > -1000. && angle < 1000.) /* the format is weird */
snprintf( obuff, 10, "?%.5f", angle);
else
strlcpy_error( obuff, "?");
fraction = 0; /* not really necessary; evades nuisance GCC warning */
break;
}
/* Formats not used in astrometry -- they don't fit the */
/* field size for punched-card data, but are used in ephems */
if( precision >= 4 && precision <= 11) /* 'overlong' dd mm ss.ssss... */
full_len += (size_t)( precision - 3);
if( precision >= 209 && precision <= 215) /* 'overlong' ddd.ddddd... */
full_len += (size_t)( precision - 208);
if( precision >= 110 && precision <= 116) /* 'overlong' dd.dddddd... */
full_len += (size_t)( precision - 109);
if( n_digits_to_show)
{
char format[8];

if( precision < 307 || precision > 312) /* omit decimal point for */
strcat( obuff, "."); /* super-precise formats */
assert( n_digits_to_show > 0 && n_digits_to_show < 20);
snprintf_err( format, sizeof( format), "%%0%dld", n_digits_to_show);
snprintf_append( obuff, full_len + 1, format, (long)fraction);
}
for( i = strlen( obuff); i < full_len; i++)
obuff[i] = ' ';
obuff[full_len] = '\0';
}

static void output_signed_angle_to_buff( char *obuff, const double angle,
const int precision)
{
*obuff++ = (angle < 0. ? '-' : '+');
output_angle_to_buff( obuff, fabs( angle), precision);
}

/* 'put_residual_into_text( )' expresses a residual, from 0 to 180 degrees, */
/* such that the text starts with a space, followed by four characters, */
Expand Down Expand Up @@ -3972,8 +3843,7 @@ static void put_residual_into_text( char *text, const double resid,

if( resid_format & RESIDUAL_FORMAT_COMPUTER_FRIENDLY)
{ /* resids in arcseconds at all times, with */
if( resid > -9.9999 && resid < 9.9999)
snprintf_err( text, 11, " %+8.6f", resid); /* some added precision */
snprintf( text, 11, " %+8.6f", resid); /* some added precision */
return;
}
if( zval > 999. * 3600.) /* >999 degrees: error must have occurred */
Expand Down Expand Up @@ -5127,30 +4997,6 @@ static bool line_must_be_redacted( const char *mpc_line,
return( false);
}

int text_search_and_replace( char FAR *str, const char *oldstr,
const char *newstr)
{
size_t ilen = FSTRLEN( str), rval = 0;
const size_t oldlen = strlen( oldstr);
const size_t newlen = strlen( newstr);

while( ilen >= oldlen)
if( !FMEMCMP( str, oldstr, oldlen))
{
FMEMMOVE( str + newlen, str + oldlen, ilen - oldlen + 1);
FMEMCPY( str, newstr, newlen);
str += newlen;
ilen -= oldlen;
rval++;
}
else
{
str++;
ilen--;
}
return( (int)rval);
}

static long round_off( const double ival, const double prec)
{
long rval = 0L, digit;
Expand Down

0 comments on commit f7ecabd

Please sign in to comment.