# include "imregister.h"
# include "spreg.h"

static int RegTimeMode = FALSE;
static int PTstyle = FALSE;

void SetOutputMode (char *mode) {

  if (!strcmp (mode, "RegTimeMode")) {
    RegTimeMode = TRUE;
    return;
  }
  if (!strcmp (mode, "PTstyle")) {
    PTstyle = TRUE;
    return;
  }

}

/* given a subset list, write out the selected spectra, if desired */
void OutputSubset (int *match, int Nmatch) {

  int Nspectrum;
  Spectrum *spectrum;

  spectrum = get_spectra (&Nspectrum);

  if (output.table != (char *) NULL) {
    DumpFitsTable (output.table, spectrum, match, Nmatch);
  } 

  if (output.bintable != (char *) NULL) {
    DumpFitsBintable (output.bintable, spectrum, match, Nmatch);
  } 

  PrintSubset (spectrum, match, Nmatch);
  exit (0);
}

/* write out complete binary FITS table in format of db */
void DumpFitsBintable (char *filename, Spectrum *spectrum, int *match, int Nmatch) {

  int i, j, Nx;
  Header header, theader;
  Matrix matrix;
  FTable table;
  Spectrum *subset;

  /* define table layout */
  define_table (&header, &matrix, &theader, &table);

  ALLOCATE (subset, Spectrum, MAX (1, Nmatch));
  for (i = 0; i < Nmatch; i++){
    j = match[i];
    memcpy (&subset[i], &spectrum[j], sizeof (Spectrum));
  }
  ConvertStruct ((char *) subset, sizeof (Spectrum), Nmatch, "spectrum");
  fits_add_rows (&table, subset, Nmatch, sizeof (Spectrum));

  fits_write_header  (filename, &header);
  fits_write_matrix  (filename, &matrix);
  fits_write_Theader (filename, &theader);
  fits_write_table   (filename, &table);
  exit (0);
}

void DumpFitsTable (char *filename, Spectrum *spectrum, int *match, int Nmatch) {
  
  int i;
  char *obsstr, *regstr, *line, dummy[64];
  char *modestr, *typestr, *ccdstr, *datestr;
  unsigned long tsecond;
  Header header, theader;
  Matrix matrix;
  FTable table;
  Spectrum *subset;

  bzero (dummy, 64);
  memset (dummy, ' ', 63);

  /* create primary header */
  fits_init_header (&header);    
  header.extend = TRUE;
  fits_create_header (&header);
  fits_create_matrix (&header, &matrix);
  fits_print (&header, "NEXTEND", "%d", 1, 1);
  
  /* create table header */
  fits_create_table_header (&theader, "TABLE", "SPECTRUM_DATABASE");
      
  /* add current date/time to header */
  str_to_time ("now", &tsecond);
  datestr = sec_to_date (tsecond);
  fits_modify (&header,  "DATE", "%s", 1, datestr);
  fits_modify (&theader, "DATE", "%s", 1, datestr);

  /* define table layout */
  fits_define_table_column (&theader, "A32",  "FILENAME",   "filename in db",        "",                              1.0, 0.0);
  fits_define_table_column (&theader, "A64",  "PATHNAME",   "fullpath in db",        "",                              1.0, 0.0);
  fits_define_table_column (&theader, "A16",  "INSTRUMENT", "instrument",            "",                              1.0, 0.0);
  fits_define_table_column (&theader, "A16",  "TELESCOPE",  "telescope",             "",                              1.0, 0.0);
  fits_define_table_column (&theader, "A16",  "OBJNAME",    "object name",           "",                              1.0, 0.0);
  fits_define_table_column (&theader, "A16",  "EXTNAME",    "extname in file",       "",                              1.0, 0.0);

  fits_define_table_column (&theader, "F10.6","RA",         "ra",                    "degrees",                       1.0, 0.0); 
  fits_define_table_column (&theader, "F10.6","DEC",        "dec",                   "degrees",                       1.0, 0.0); 
  fits_define_table_column (&theader, "F6.1", "EXPTIME",    "exposure time",         "seconds",                       1.0, 0.0); 
  fits_define_table_column (&theader, "F5.3", "AIRMASS",    "airmass",               "",                              1.0, 0.0); 
  fits_define_table_column (&theader, "F7.2", "Ws",         "spectral range start",  "Angstrom",                      1.0, 0.0); 
  fits_define_table_column (&theader, "F7.2", "We",         "spectral range end",    "Angstrom",                      1.0, 0.0); 
  fits_define_table_column (&theader, "F7.2", "dW",         "spectral resolution",   "Angstrom / pix",                1.0, 0.0); 

  fits_define_table_column (&theader, "I3",   "NSPECTRA",   "number of spectra",     "",                              1.0, 0.0); 
  fits_define_table_column (&theader, "A20",  "OBS_TIME",   "time of observation",   "yyyy/mm/dd,hh:mm:ss");
  fits_define_table_column (&theader, "A20",  "REG_TIME",   "time of registration",  "yyyy/mm/dd,hh:mm:ss");

  fits_define_table_column (&theader, "I2",   "MODE",       "phu/mef/ext",           "",                              1.0, 0.0);
  fits_define_table_column (&theader, "I2",   "STATE",      "raw/wav/flx/etc",       "",                              1.0, 0.0);
  fits_define_table_column (&theader, "I3",   "FLAG",       "status flags",          "",                              1.0, 0.0);

  /* create table, add data values */
  fits_create_table (&theader, &table);
  
  /* add data to table */
  for (i = 0; i < Nmatch; i++) {
    subset = &spectrum[match[i]];
    obsstr   = sec_to_date (subset[0].obstime);
    regstr   = sec_to_date (subset[0].regtime);

    line = fits_table_print (&table, subset[0].filename, subset[0].pathname, subset[0].instrument, 
			     subset[0].telescope, subset[0].objname, subset[0].extname, 
			     subset[0].ra, subset[0].dec, subset[0].exptime, subset[0].airmass, 
			     subset[0].Ws, subset[0].We, subset[0].dW, 
			     subset[0].Nspec, obsstr, regstr,
			     subset[0].mode, subset[0].state, subset[0].flag);

    fits_add_rows (&table, line, 1, strlen(line));
    free (line);
    free (obsstr);
    free (regstr);
  }

  fits_write_header  (filename, &header);
  fits_write_matrix  (filename, &matrix);
  fits_write_Theader (filename, &theader);
  fits_write_table   (filename, &table);
  exit (0);
}

/* Select, TimeMode are global */
int PrintSubset (Spectrum *spectrum, int *match, int Nmatch) {
  
  int i, j;
  char *timestr;

  /* print the selected entries */
  for (j = 0; j < Nmatch; j++) {
    i = match[j];
    
    timestr = RegTimeMode ? sec_to_date (spectrum[i].regtime) : sec_to_date (spectrum[i].obstime);

    /* predefined subset of values */
    fprintf (stdout, "%5d %20s %s %s %s %s ", i, timestr, spectrum[i].pathname, spectrum[i].filename, spectrum[i].objname, spectrum[i].telescope);
    fprintf (stdout, "%5.1f %5.3f %.1f %.1f %.2f\n", spectrum[i].exptime, spectrum[i].airmass, spectrum[i].Ws, spectrum[i].We, spectrum[i].dW);

    free (timestr);
  }
  return (TRUE);
}

int dump_data (Spectrum *spectrum, int Nspectrum) {

  int i;

  for (i = 0; i < Nspectrum; i++) {
    fprintf (stdout, "%s %s %f %f %f %f\n", spectrum[i].filename, spectrum[i].objname, spectrum[i].exptime, spectrum[i].airmass, spectrum[i].Ws, spectrum[i].We);
  }

  fprintf (stdout, "SUCCESS\n");
  exit (0);

}
