
	/* ask for a position in equinox 1950.0 coords, and then
	   for an offset in arcseconds.  add the offset to the position,
	   and the spit out the results in J2000.0.
	 */

#include <stdio.h>
#include <math.h>
#include <ctype.h>

#ifndef M_PI
#define M_PI        3.141592654
#endif

#define NORTH    0
#define SOUTH    1
#define EAST     2
#define WEST     3

	/* multiply by the following factor to convert degrees to radians */
#define DEGTORAD	(M_PI/180.0)

static void 
off_to_arcsec();

char progname[] = "offset";

main()
{
	double oldra, olddec, epoch, newra, newdec, off_ra, off_dec;
	double hh, deg, seconds;
	int hours, minutes;
	char line[100], off1[10], off2[10];

	printf("gimme an position in 1950.0 coords, then an offset\n");
	printf("gimme the RA and Dec:  RA  (HH MM SS.sss) ");
	scanf("%d %d %lf", &hours, &minutes, &seconds);
	fflush(stdout);
	ratodeg(hours, minutes, seconds, &oldra);
	printf("                       Dec (DD MM SS.sss) ");
	scanf("%d %d %lf", &hours, &minutes, &seconds);
	fflush(stdout);
	hmstohhh(hours, minutes, seconds, &olddec);
	printf("gimme offset in form 90E 124S (any NSEW OK): ");
	scanf("%s %s", off1, off2);
	off_to_arcsec(off1, off2, &off_ra, &off_dec, olddec);
	oldra += off_ra;
	olddec += off_dec;

	to_j2000(1950.0, oldra, olddec, &newra, &newdec);
	degtora(newra, &hours, &minutes, &seconds);
	format_hms(hours, minutes, seconds, line);
	printf("%6lf -> J2000.0, RA %s ", epoch, line);
	hhhtohms(newdec, &hours, &minutes, &seconds);
	format_hms(hours, minutes, seconds, line);
	printf(" Dec %s\n", line);
		
}


	/* given two strings, each of which should have the form of a 
	   number followed by a character 'N','S','E','W', decode the
	   the strings, make sure that one of 'N','S' is specified and
	   one of 'E','W'.  convert the numbers, which are in arcseconds,
	   to degrees (keeping in mind the dependence of RA on declination),
	   and return then in the two arguments */

static void
off_to_arcsec(off1, off2, off_ra, off_dec, dec)
char *off1, *off2;
double *off_ra, *off_dec, dec;
{
	int i, k, n, s, e, w, direc[2], num[2];
	char *cp[2];

	n = 0; s = 0; e = 0; w = 0;

	cp[0] = off1;
	cp[1] = off2;
	for (k = 0; k < 2; k++) {
		for (i = 0; i < strlen(cp[k]); i++) {
			if ((isspace(cp[k][i])) || (isdigit(cp[k][i])))
				continue;
			switch (cp[k][i]) {
			case 'N': case 'n':
				n++;
				direc[k] = NORTH;
				cp[k][i] = '\0';
				break;	
			case 'S': case 's':
				s++;
				direc[k] = SOUTH;
				cp[k][i] = '\0';
				break;	
			case 'E': case 'e':
				e++;
				direc[k] = EAST;
				cp[k][i] = '\0';
				break;
			case 'W': case 'w':
				w++;
				direc[k] = WEST;
				cp[k][i] = '\0';
				break;
			default:
				fprintf(stderr, "offset argument has no direction %s\n", cp[k]);
				exit(-1);
			}
			if (sscanf(cp[k], "%d", &(num[k])) != 1) {
				fprintf(stderr, "offset argument has no number %s\n", cp[k]);
				exit(-1);
			}
		}
	}
	if ((n + s != 1) || (e + w != 1)) {
		fprintf(stderr, "not given N/S and E/W offsets\n");
		exit(1);
	}
	if ((direc[0] == NORTH) || (direc[0] == SOUTH)) {
		*off_dec = num[0]/3600.0;
		*off_ra = (num[1]/cos(dec*DEGTORAD))/3600.0;
	}
	else {
		*off_dec = num[1]/3600.0;
		*off_ra = (num[0]/cos(dec*DEGTORAD))/3600.0;
	}
}
