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

typedef struct {
  int measure;          // pointer to measure entry
  unsigned int averef;  // old measures come from multiple averages
  float R, D;           // actual coordinates of this measure
} Mpointer;

typedef struct {
  double median;
  double mean;
  double sigma;
  double error;
  double chisq;
  double min;
  double max;
  double total;
  int    Nmeas;
} StatType;

// need to get RADIUS from Config 

/* global variables */
int    SHOW_PARAMS;
int    VERBOSE;
double JOIN_RADIUS;
double UNIQ_RADIUS;
double DMCAL_MIN;;
char   ImageCat[256];
char   GSCFILE[256];
char   CATDIR[256];
char   CATMODE[16];    /* raw, mef, split, mysql */
char   CATFORMAT[16];  /* internal, elixir, loneos, panstarrs */
char   PhotCodeFile[256];

double RMIN;
double RMAX;
double DMIN;
double DMAX;
 
double XMIN;
double XMAX;
double YMIN;
double YMAX;
double MMIN;
double MMAX;

double DMSYS;
double DMGAIN;
double CHISQ_MAX;
double SIGMA_MAX;
int    NMEAS_MIN;
double ZERO_POINT;

int ExcludeByInstMag;
double INST_MAG_MIN;
double INST_MAG_MAX;

int ExcludeByMaxMinMag;
double MAX_MIN_MAG;

SkyRegion REGION;
PhotCodeData photcodes;

# define FLAG_AREA            0X0001
# define FLAG_MINST           0X0002
# define FLAG_DMCAL           0x0004
# define FLAG_SIGCLIP         0X0008
# define FLAG_CHISQ           0x0010
# define FLAG_TOOFEW          0x0020
# define FLAG_DUPMEAS         0x0040

Image        *find_images (FITS_DB *db, GSCRegion *region, int Nregion, int *Nimage, int **LineNum);
GSCRegion    *find_regions (Image *image, int Nimage, int *Nregions, GSCRegion *fullregion); 
GSCRegion    *get_regions (double minRa, double maxRa, double minDec, double maxDec, int *Nregions); 
Catalog      *load_catalogs (GSCRegion *region, int Nregion); 
GSCRegion    *load_images (FITS_DB *db, char *seed, int *nregion);
GSCRegion    *name_region (char *name, int *Nregions); 
void          ConfigInit (int *argc, char **argv); 
int           args (int argc, char **argv); 
int           corner_check (double *x1, double *y1, double *x2, double *y2); 
int           edge_check (double *x1, double *y1, double *x2, double *y2); 
void          flag_measures (FITS_DB *db, Catalog *catalog); 
int           gcatalog (Catalog *catalog); 
void          get_mags (Catalog *catalog); 
void          getfullregion (Image *image, int Nimage, GSCRegion *fullregion); 
void          initialize (int argc, char **argv); 
void          initstats (char *mode); 
void          join_stars (Catalog *catalog); 
int           liststats (double *value, double *dvalue, int N, StatType *stats); 
double        opening_angle (double x1, double y1, double x2, double y2, double x3, double y3); 
void          set_ZP (double ZERO); 
void          sortA (double *X, int N); 
void          sortB (double *X, double *Y, int N); 
void          sortC (double *X, double *Y, double *F1, double *F2, int N); 
void          sortD (double *X, double *Y, double *Z, int N); 
void          sort_lists (float *X, float *Y, int *index, int N); 
void          sort_time (unsigned int *X, int *Y, int N); 
void          unique_measures (Catalog *catalog);
void          wcatalog (Catalog *catalog);
int           make_subcatalog (Catalog *subcatalog, Catalog *catalog);
void          check_directory (char *basefile);

int Shutdown (char *format, ...);
void set_db (FITS_DB *in);
void lock_image_db (FITS_DB *db, char *filename);
void unlock_image_db (FITS_DB *db);
void check_permissions (char *basefile);
void TrapSignal (int sig);
void SetProtect (int mode);
int SetSignals ();


int copy_images (char *outdir);
