# include "dvo.h"

SkyTable *SkyTableLoad (char *filename, int VERBOSE) {

  Header header;
  Matrix matrix;
  Header theader;
  FTable ftable;
  SkyTable *skytable;
  FILE *f;
  int i;
  
  f = fopen (filename, "r");
  if (f == NULL) {
    if (VERBOSE) fprintf (stderr, "can't find Sky Region file %s\n", filename);
    return (NULL);
  }

  /* load in table data */
  ftable.header = &theader;
  if (!gfits_fread_header (f, &header)) {
    if (VERBOSE) fprintf (stderr, "can't read Sky Region header\n");
    fclose (f);
    return (NULL);
  }
  if (!gfits_fread_matrix (f, &matrix, &header)) {
    if (VERBOSE) fprintf (stderr, "can't read Sky Region matrix\n");
    gfits_free_header (&header);
    fclose (f);
    return (NULL);
  }
  if (!gfits_fread_ftable (f, &ftable, "SKY_REGION")) {
    if (VERBOSE) fprintf (stderr, "can't read Sky Region table\n");
    gfits_free_header (&header);
    gfits_free_matrix (&matrix);
    fclose (f);
    return (NULL);
  }

  ALLOCATE (skytable, SkyTable, 1);
  skytable[0].regions = gfits_table_get_SkyRegion (&ftable, &skytable[0].Nregions, NULL);
  ALLOCATE (skytable[0].filename, char *, skytable[0].Nregions);
  for (i = 0; i < skytable[0].Nregions; i++) {
    skytable[0].filename[i] = NULL;
  }
  
  gfits_free_header (&header);
  gfits_free_matrix (&matrix);
  gfits_free_header (&theader);

  return (skytable);
}

int SkyTableSave (SkyTable *skytable, char *filename) {

  Header header;
  Matrix matrix;
  Header theader;
  FTable ftable;
  FILE *f;

  /* make phu header (no matrix needed) */
  ftable.header = &theader;
  gfits_init_header (&header);
  header.extend = TRUE;
  gfits_create_header (&header);
  gfits_create_matrix (&header, &matrix);

  gfits_table_set_SkyRegion (&ftable, skytable[0].regions, skytable[0].Nregions);

  f = fopen (filename, "w");
  if (f == (FILE *) NULL) { 
    fprintf (stderr, "cannot open %s for output\n", filename);
    return (FALSE);
  }
  
  gfits_fwrite_header  (f, &header);
  gfits_fwrite_matrix  (f, &matrix);
  gfits_fwrite_Theader (f, &theader);
  gfits_fwrite_table  (f, &ftable);
  fclose (f);

  return (TRUE);
}

SkyTable *SkyTableLoadOptimal (char *catdir, char *skyfile, char *gscfile, int depth, int verbose) {

  char filename[256];
  struct stat filestat;
  SkyTable *sky;

  /* first option: CATDIR/SkyTable.fits */
  sprintf (filename, "%s/SkyTable.fits", catdir);
  if (stat (filename, &filestat)) goto SKYFILE;
  if (!check_file_access (filename, FALSE, verbose)) goto SKYFILE;
  sky = SkyTableLoad (filename, verbose);
  if (sky == NULL) {
    fprintf (stderr, "error loading sky table\n");
    exit (1);
  }
  return (sky);

SKYFILE:
  /* second option: SKYFILE */
  if (skyfile == NULL) goto GSCFILE;
  if (skyfile[0] != 0) goto GSCFILE;
  if (stat (skyfile, &filestat)) goto GSCFILE;
  if (!check_file_access (skyfile, FALSE, verbose)) goto GSCFILE;
  sky = SkyTableLoad (skyfile, verbose);
  if (sky == NULL) {
    fprintf (stderr, "error loading sky table\n");
    return (NULL);
  }
  /* set the depths to the default depth */
  SkyTableSetDepth (sky, depth);

  /* create CATDIR copy */
  sprintf (filename, "%s/SkyTable.fits", catdir);
  check_file_access (filename, FALSE, verbose);
  if (!SkyTableSave (sky, filename)) return NULL;

  gfits_convert_SkyRegion (sky[0].regions, sizeof (SkyTable), sky[0].Nregions);
  return (sky);

GSCFILE:
  /* third option: GSCFILE */
  sky = SkyTableFromGSC (gscfile, depth, verbose);
  if (sky == NULL) {
    fprintf (stderr, "error loading sky table\n");
    return (NULL);
  }

  /* create CATDIR copy */
  sprintf (filename, "%s/SkyTable.fits", catdir);
  check_file_access (filename, FALSE, verbose);
  if (!SkyTableSave (sky, filename)) return NULL;

  gfits_convert_SkyRegion (sky[0].regions, sizeof (SkyRegion), sky[0].Nregions);
  return (sky);
}

int SkyListSetFilenames (SkyList *list, char *path, char *ext) {

  int i;
  char line[256];

  // this generates the names, be sure to free when not needed
  for (i = 0; i < list[0].Nregions; i++) {
    sprintf (line, "%s/%s.%s", path, list[0].regions[i][0].name, ext);
    list[0].filename[i] = strcreate (line);
  }

  return (TRUE);
}

int SkyTableSetFilenames (SkyTable *sky, char *path, char *ext) {

  int i;
  char line[256];

  // this generates the names, be sure to free when not needed
  for (i = 0; i < sky[0].Nregions; i++) {
    sprintf (line, "%s/%s.%s", path, sky[0].regions[i].name, ext);
    sky[0].filename[i] = strcreate (line);
  }

  return (TRUE);
}

