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

# ifdef ANSI
#   define F_SETFL      4   
#   define O_NONBLOCK   0200000  
#   define AF_UNIX      1          
#   define SOCK_STREAM  1          
#   define ENOENT       2       /* No such file or directory    */
# endif /* ANSI */

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

typedef struct {
  void *tgroup;
  void *sgroup;
} ImageLink;

typedef struct {
  char label[64];
  float M;
  float dM;
  float dMsub;
  double v1, v2;
  Image **image;
  ImageLink **imlink;
  int Nimage, Ngood;
} Group;

/* global variables set in parameter file */
char         ImageCat[256];
char         CATDIR[256];
char         CATMODE[16];    /* raw, mef, split, mysql */
char         CATFORMAT[16];  /* internal, elixir, loneos, panstarrs */
char         STATMODE[64];
int          VERBOSE;
int          NLOOP;
int          TimeSelect;
int          VERBOSE;
int          UPDATE;
int          IMAGE_BAD;
double       RADIUS;
double       TRANGE;
time_t 	     TSTART;
time_t 	     TSTOP;
PhotCode    *photcode;

enum {black, white, red, orange, yellow, green, blue, indigo, violet};

typedef struct {
  double xmin, xmax, ymin, ymax;
  int style, ptype, ltype, etype, color;
  double lweight, size;
} Graphdata;

/***** prototypes ****/
void          ConfigInit         PROTO((int *argc, char **argv));
void          DonePlotting       PROTO((Graphdata *graphmode, int N));
void          JpegPlot           PROTO((Graphdata *graphmode, int N, char *filename));
void          PSPlot             PROTO((Graphdata *graphmode, int N, char *filename));
void          PlotLabel          PROTO((char *string, int N));
void          PlotVector         PROTO((int Npts, double *vect, int mode, int N));
void          PrepPlotting       PROTO((int Npts, Graphdata *graphmode, int N));
void          XDead              PROTO(());
int           args               PROTO((int argc, char **argv));
void          dumpresult         PROTO(());
Group        *find_image_sgroups PROTO((FITS_DB *db, ImageLink **imlink, int *Nsgroup));
Group        *find_image_tgroups PROTO((FITS_DB *db, ImageLink **imlink, int *Ntgroup));
void          fit_sgroup         PROTO((Group *sgroup, int Nsgroup));
void          fit_tgroup         PROTO((Group *tgroup, int Ntgroup));
int           gcatalog           PROTO((Catalog *catalog));
void          initialize         PROTO((int argc, char **argv));
void          initstats          PROTO((char *mode));
int           liststats          PROTO((double *value, double *dvalue, int N, StatType *stats));
int           load_images        PROTO((FITS_DB *db));
int           main               PROTO((int argc, char **argv));
int           open_graph         PROTO((int N));
void          sort               PROTO((unsigned int *X, int N));
void          sortB              PROTO((double *X, double *Y, int N));
void          sortD              PROTO((double *X, double *Y, double *Z, int N));
void          update_dvo_catalog PROTO((Catalog *catalog, Group *sgroup, int warn));
void          wcatalog           PROTO((Catalog *catalog));
void          wimages            PROTO((Image *image, int Nimage));
void check_permissions (char *basefile);
void lock_image_db (FITS_DB *db, char *filename);
void unlock_image_db (FITS_DB *db);
void create_image_db (FITS_DB *db);
void set_db (FITS_DB *in);
int Shutdown (char *format, ...);
void TrapSignal (int sig);
void SetProtect (int mode);
int SetSignals ();
int subset_images (FITS_DB *db);
void update (FITS_DB *db, Group *sgroup, int Nsgroup);
