# include <ohana.h>
# include <loneos.h>
# include <signal.h>

typedef struct {
  double X;
  double Y;
  double R;
  double D;
  double M, dM;
  char   dophot;
  double sky;
  double fx, fy, df;
  double Mgal, Map;
  int found;
} Stars;

/* global variables set in parameter file */
char   ImageCat[256];
char   ImageTemplate[256];
char   CatTemplate[256];
char   GSCFILE[256];
char   CATDIR[256];
double NSIGMA, SNLIMIT, DEFAULT_RADIUS;
double ALPHA;
int    VERBOSE;
int    XOVERSCAN, YOVERSCAN;
char   DBServer[1024];
PhotCode *thiscode;

int REMOTE;
int ONLY_MATCH;
int REPLACE;

unsigned int TIMEREF;

Stars *find_subset (GSCRegion *region, Stars *stars, int Nstars, int **Subref, int *NSTARS);
Image *gimages (Image *image, int *Npimage);
GSCRegion *gregions (Image *image, int *Nregions);
Stars *gstars (char *file, int *Nstars);
char *sec_to_date (unsigned long second);
void ConfigInit (int *argc, char **argv);
void FindCalibration (Image *image);
void InitCalibration ();
void SaveCalibration (float M, float dM, float Mr, float Mc, float A, int N, float ra, float dec, float x, float y);
int SetSignals ();
int Shutdown ();
void TrapSignal (int sig);
void aregion (GSCRegion *region, FILE *f, double ra, double dec);
int args (int argc, char **argv);
void check_permissions (char *basefile);
int chk_time (char *line);
int dms_to_ddd (double *Value, char *string);
int edge_check (double *x1, double *y1, double *x2, double *y2);
void find_matches (GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog);
void fsort (float *X, int N);
int gcatalog (Catalog *catalog);
void gregion (Stars *star, GSCRegion *region);
void help ();
int hms_to_deg (double *h0, double *h1, double *d0, double *d1, char *string);
int in_image (double r, double d, Image *image);
unsigned long jd_to_sec (double jd);
unsigned long date_to_sec (char *date);
int main (int argc, char **argv);
void make_backup (char *filename);
void mkcatalog (GSCRegion *region, Catalog *catalog);
double opening_angle (double x1, double y1, double x2, double y2, double x3, double y3);
int parse_time (Header *header);
double sec_to_jd (unsigned long second);
void sort_lists (float *X, float *Y, int *S, int N);
int str_to_dtime (char *line, double *second);
int str_to_radec (double *ra, double *dec, char *str1, char *str2);
int str_to_time (char *line, unsigned int *second);
void uppercase (char *string);
void wcatalog (Catalog *catalog);
void wimage (char *filename, Image *image, int Nstars);
