# include <signal.h>

enum {
  SPMODE_UKN,				/* unknown mode */
  SPMODE_PHU,				/* one spectrum, primary header unit */
  SPMODE_MEF,				/* spectrum group, extensions */
  SPMODE_EXT,				/* one of a spectrum group, extension */
  SPMODE_N
};

enum {
  SPSTATE_UKN,				/* unknown state */
  SPSTATE_RAW,				/* raw image counts */
  SPSTATE_FLT,				/* flattened image counts */
  SPSTATE_CLN,				/* clean extraction */
  SPSTATE_WAV,				/* wavelength calibrated */
  SPSTATE_FLX,				/* flux calibrated (implies WAV) */
  SPSTATE_N
};

struct {
  int Ntimes;           unsigned long *tstart, *tstop;
  int ModeSelect;       int Mode;
  int StateSelect;      int State;
  int ExptimeSelect;    float Exptime;
  int FilenameSelect;   char *Filename;
  int ObjectSelect;     char *Object;
  int TelescopeSelect;  char *Telescope;
  int InstrumentSelect; char *Instrument;
  int MatchNumber;
} criteria;

struct {
  int delete;
  int modify;

  int unique;
  int modify_path;
  char *oldpath, *newpath;

  int modify_mode, mode;
  int modify_state, state;

  int HST;
  int verbose;
  char *table;
  char *bintable;
} output;

typedef struct {
  int *ccd;
  int Nccd;
  char name[64];
} MosaicRegion;

typedef struct {
  MosaicRegion center;
  MosaicRegion outer;
  MosaicRegion top;
  MosaicRegion bottom;
  MosaicRegion left;
  MosaicRegion right;
} MosaicLayout;

enum {UNLOCK, LOCK, IGNORE};

int SingleIsSplit;
int NeedType;
int NoReg;
int IMSORT;
int CLIENT;
char *PIDFILE;
int LOOP_DELAY;

char SpectrumDB[64];
char ObjectKeyword[64];
char TelescopeKeyword[64];


int args (int argc, char **argv);
Spectrum *get_spectrums (int *N);
int set_spectrums (Spectrum *new, int Nnew);
int load_db ();
int save_db ();
int update_db (int *match, int Nmatch);
int append_db (Spectrum *new, int Nnew);
int create_db ();
int close_db ();
void DeleteSubset (int *match, int Nmatch);
Spectrum *spinfo (char *filename);
int *match_criteria (int *Nmatch);
int *match_spectrums (Spectrum *subset, int Nsubset, int *Nmatch);
void ModifySubset (int *match, int Nmatch);
void SetOutputMode (char *mode);
void OutputSubset (int *match, int Nmatch);
void DumpFitsBintable (char *filename, Spectrum *spectrum, int *match, int Nmatch);
void DumpFitsTable (char *filename, Spectrum *spectrum, int *match, int Nmatch);
int PrintSubset (Spectrum *spectrum, int *match, int Nmatch);
int dump_data (Spectrum *spectrum, int Nspectrum);
int SubmitSpectrums (Spectrum *spectrum);
int load_probes (char *filename, unsigned long tzero, int *wantprobe, double *values, int Nprobe);
int define_table (Header *header, Matrix *matrix, Header *theader, FTable *table);
void set_timezone (double dt);

void DumpCADCTable (char *filename, Spectrum *spectrum, int *match, int Nmatch);
int *GetObsIDSubset (Spectrum *spectrum, int start, int *index, int *entry, int Nindex, int *Nsubset);
int *GetUniqueObsID (Spectrum *spectrum, int *index, int *entry, int Nindex, int *Nmatch);
void GetObsIDIndex (Spectrum *spectrum, int *match, int Nmatch, int **Index, int **Entry);
double SigmaClipList (double *list, int N);
double MosaicIQStats (Spectrum *spectrum, int *match, int Nmatch, MosaicRegion *region);
MosaicLayout *CreateCFH12K ();
MosaicLayout *CreateMegaCam ();
int GetREFCCD (Spectrum *spectrum, int *index, int *entry, int Nindex, int start);
