# include <dvo.h>

int dvo_catalog_load_split (Catalog *catalog, int VERBOSE) {

  int Nitems, Nexpect, Naverage, Nmeasure, Nmissing, Nsecfilt, status;
  char *path, string[80];
  Header header;
  Matrix matrix;
  FTable ftable;
  Catalog *measure, *missing, *secfilt;

  /* ftable header storage for below */
  ftable.header = &header;

  /* needed to find the split files below */
  path = pathname (catalog[0].filename);

  /* get the components from the header - these duplicate information in the split files (NAXIS2) */
  if (!gfits_scan (&catalog[0].header, "NSTARS",   "%d", 1, &Naverage)) return (FALSE);
  if (!gfits_scan (&catalog[0].header, "NMEAS",    "%d", 1, &Nmeasure)) return (FALSE);
  if (!gfits_scan (&catalog[0].header, "NMISS",    "%d", 1, &Nmissing)) return (FALSE);
  if (!gfits_scan (&catalog[0].header, "NSECFILT", "%d", 1, &Nsecfilt)) Nsecfilt = 0;

  /**  Nsecfilt is unusual: it does not list the number of data items in the table
       instead, the number of items is Nsecfilt * Naverage.  **/
  catalog[0].Nsecfilt = Nsecfilt;

  /* default values */
  catalog[0].average = NULL;
  catalog[0].measure = NULL;
  catalog[0].missing = NULL;
  catalog[0].secfilt = NULL;

  /* XXX EAM : validate table mode */

  /*** Average Table ***/

  if (catalog[0].catflags & LOAD_AVES) {
    /* move pointer past header -- must be already read (load_catalog) */
    fseek (catalog[0].f, catalog[0].header.size, SEEK_SET);
    /* matrix should be empty */
    if (!gfits_fread_matrix (catalog[0].f, &matrix, &catalog[0].header)) {
      if (VERBOSE) fprintf (stderr, "can't read primary matrix");
      return (FALSE);
    }
    /* read Average table header */
    if (!gfits_fread_header (catalog[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table average header");
      return (FALSE);
    }
    /* read Average table data */
    if (!gfits_fread_ftable_data (catalog[0].f, &ftable)) {
      if (VERBOSE) fprintf (stderr, "can't read table average data");
      return (FALSE);
    }
    catalog[0].average = FtableToAverage (&ftable, &catalog[0].Naverage, &catalog[0].catformat);
    if (Naverage != catalog[0].Naverage) {
      fprintf (stderr, "Warning: mismatch between Naverage in PHU and Table headers (%d vs %d)\n", Naverage, catalog[0].Naverage);
    }
    gfits_free_header (&header);
  } 

  /*** Measure Table ***/

  measure = NULL;

  /* (Full Load) */
  if (catalog[0].catflags & LOAD_MEAS) {
    ALLOCATE (measure, Catalog, 1);
    dvo_catalog_init (measure, TRUE);

    /* get split filename from main header (paths relative to cpt file) */
    if (!gfits_scan (&catalog[0].header, "MEASURE",  "%s", 1, string)) return (FALSE);
    ALLOCATE (measure[0].filename, char, strlen(path) + strlen(string) + 2);
    sprintf (measure[0].filename, "%s/%s", path, string);

    /* lock & open catalog file */
    if (dvo_catalog_lock (measure, catalog[0].lockmode) != 1) {
      fprintf (stderr, "cannot access measure file %s\n", measure[0].filename);
      exit (2);
    }

    /* read PHU */
    if (!gfits_load_header (measure[0].f, &measure[0].header)) {
      if (VERBOSE) fprintf (stderr, "catalog file does not exist: %s\n", measure[0].filename);
      return (FALSE);
    }
    /* matrix should be empty */
    if (!gfits_fread_matrix (measure[0].f, &matrix, &measure[0].header)) {
      if (VERBOSE) fprintf (stderr, "can't read primary matrix\n");
      return (FALSE);
    }
    /* read Measure table header */
    if (!gfits_fread_header (measure[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read measure PHU header\n");
      return (FALSE);
    }
    /* read Measure table data */
    if (!gfits_fread_ftable_data (measure[0].f, &ftable)) {
      if (VERBOSE) fprintf (stderr, "can't read table measure data\n");
      return (FALSE);
    }
    /* convert data format to internal */
    catalog[0].measure = FtableToMeasure (&ftable, &catalog[0].Nmeasure, &catalog[0].catformat);
    if (Nmeasure != catalog[0].Nmeasure) {
      fprintf (stderr, "Warning: mismatch between Nmeasure in PHU and Table headers (%d vs %d)\n", Nmeasure, catalog[0].Nmeasure);
    }
    catalog[0].Nmeas_off = 0;
    gfits_free_header (&header);
    gfits_free_matrix (&matrix);
  }

  /* (Meta Load) */
  if (catalog[0].catflags & LOAD_MEAS_META) {
    ALLOCATE (measure, Catalog, 1);
    dvo_catalog_init (measure, TRUE);

    /* get split filename from main header (paths relative to cpt file) */
    if (!gfits_scan (&catalog[0].header, "MEASURE",  "%s", 1, string)) return (FALSE);
    ALLOCATE (measure[0].filename, char, strlen(path) + strlen(string) + 2);
    sprintf (measure[0].filename, "%s/%s", path, string);

    /* lock & open catalog file */
    if (dvo_catalog_lock (measure, catalog[0].lockmode) != 1) {
      fprintf (stderr, "cannot access measure file %s\n", measure[0].filename);
      exit (2);
    }

    /* read PHU */
    if (!gfits_load_header (measure[0].f, &measure[0].header)) {
      if (VERBOSE) fprintf (stderr, "catalog file does not exist: %s\n", measure[0].filename);
      return (FALSE);
    }

    /* allocate dummy data for valid realloc */
    ALLOCATE (catalog[0].measure, Measure, 1);

    /* set up the table size information */
    catalog[0].Nmeasure  = 0;        /* no rows loaded */
    catalog[0].Nmeas_off = Nmeasure; /* start of new entries */
  }
  catalog[0].measure_catalog = measure;

  /*** Missing Table ***/

  missing = NULL;
  if (catalog[0].catflags & LOAD_MISS) {
    ALLOCATE (missing, Catalog, 1);
    dvo_catalog_init (missing, TRUE);

    /* get split filename from main header (paths relative to cpt file) */
    if (!gfits_scan (&catalog[0].header, "MISSING",  "%s", 1, string)) return (FALSE);
    ALLOCATE (missing[0].filename, char, strlen(path) + strlen(string) + 2);
    sprintf (missing[0].filename, "%s/%s", path, string);

    /* lock & open catalog file */
    status = dvo_catalog_lock (missing, catalog[0].lockmode);
    if (!status) {
      fprintf (stderr, "ERROR: cannot access missing file %s\n", missing[0].filename);
      exit (2);
    }
    if (status == 2) {
	/* MISSING table is empty (this is not an error) */
	gfits_create_header (&missing[0].header);
	goto missing_empty;
    }

    /* read PHU */
    if (!gfits_load_header (missing[0].f, &missing[0].header)) {
      if (VERBOSE) fprintf (stderr, "catalog file does not exist: %s\n", missing[0].filename);
      return (FALSE);
    }
    /* matrix should be empty */
    if (!gfits_fread_matrix (missing[0].f, &matrix, &missing[0].header)) {
      if (VERBOSE) fprintf (stderr, "can't read primary matrix\n");
      return (FALSE);
    }
    /* read Missing table header */
    if (!gfits_fread_header (missing[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table missing header\n");
      return (FALSE);
    }
    /* read Missing table data */
    if (!gfits_fread_ftable_data (missing[0].f, &ftable)) {
      if (VERBOSE) fprintf (stderr, "can't read table missing data\n");
      return (FALSE);
    }
    /* no conversions currently defined */
    catalog[0].missing = gfits_table_get_Missing (&ftable, &catalog[0].Nmissing, NULL);
    if (Nmissing != catalog[0].Nmissing) {
      fprintf (stderr, "Warning: mismatch between Nmissing in PHU and Table headers (%d vs %d)\n", Nmissing, catalog[0].Nmissing);
    }
    gfits_free_header (&header);
    gfits_free_matrix (&matrix);
  } 
missing_empty:
  catalog[0].missing_catalog = missing;

  /*** Secfilt Table ***/

  secfilt = NULL;
  if (catalog[0].catflags & LOAD_SECF) {
    ALLOCATE (secfilt, Catalog, 1);
    dvo_catalog_init (secfilt, TRUE);

    /* get split filename from main header (paths relative to cpt file) */
    if (!gfits_scan (&catalog[0].header, "SECFILT",  "%s", 1, string)) return (FALSE);
    ALLOCATE (secfilt[0].filename, char, strlen(path) + strlen(string) + 2);
    sprintf (secfilt[0].filename, "%s/%s", path, string);

    /* lock & open catalog file */
    if (dvo_catalog_lock (secfilt, catalog[0].lockmode) != 1) {
      fprintf (stderr, "cannot access secfilt file %s\n", secfilt[0].filename);
      exit (2);
    }

    /* read PHU */
    if (!gfits_load_header (secfilt[0].f, &secfilt[0].header)) {
      if (VERBOSE) fprintf (stderr, "catalog file does not exist: %s\n", secfilt[0].filename);
      return (FALSE);
    }
    /* matrix should be empty */
    if (!gfits_fread_matrix (secfilt[0].f, &matrix, &secfilt[0].header)) {
      if (VERBOSE) fprintf (stderr, "can't read primary matrix\n");
      return (FALSE);
    }
    /* read secfilt table header */
    if (!gfits_fread_header (secfilt[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table secfilt header\n");
      return (FALSE);
    }
    /* read secfilt table data */
    if (!gfits_fread_ftable_data (secfilt[0].f, &ftable)) {
      if (VERBOSE) fprintf (stderr, "can't read table secfilt data\n");
      return (FALSE);
    }
    Nexpect = catalog[0].Nsecfilt * catalog[0].Naverage;
    catalog[0].secfilt = FtableToSecFilt (&ftable, &Nitems, &catalog[0].catformat);
    if (Nexpect != Nitems) {
      fprintf (stderr, "Warning: mismatch between Nsecfilt items in PHU and Table headers (%d vs %d)\n", Nexpect, Nitems);
    }
    gfits_free_header (&header);
    gfits_free_matrix (&matrix);
  } 
  catalog[0].secfilt_catalog = secfilt;

  /* save the current number so we can do partial updates */
  catalog[0].Nave_disk  = Naverage;
  catalog[0].Nmeas_disk = Nmeasure;
  catalog[0].Nmiss_disk = Nmissing;

  return (TRUE);
}

/* save_catalog_split writes complete new files from scratch */

int dvo_catalog_save_split (Catalog *catalog, char VERBOSE) {

  int Nitems;
  Matrix matrix;
  Header header;
  FTable ftable;
  Catalog *measure, *missing, *secfilt;

  ftable.header = &header;

  if (catalog[0].Naverage == 0) {
    if (VERBOSE) fprintf (stderr, "no stars in catalog, skipping\n");
    return (TRUE);
  }

  /* make sure header is consistent with data */
  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure);
  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, catalog[0].Nsecfilt);
  gfits_modify (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);

  /* rewind file pointers and truncate (file is still open) */
  fseek (catalog[0].f, 0, SEEK_SET);

  /* write table PHU header - always write this out */
  /* XXX EAM : check if disk file size has changed */
  if (!gfits_fwrite_header  (catalog[0].f, &catalog[0].header)) {
    fprintf (stderr, "can't write primary header");
    return (FALSE);
  }

  /* in split mode, we can save only part of the data */ 

  /*** Average Table ***/

  if (catalog[0].average != NULL) {
    ftruncate (fileno (catalog[0].f), catalog[0].header.size);

    /* this is probably a NOP, do I have to keep it in? */
    gfits_create_matrix (&catalog[0].header, &matrix);
    if (!gfits_fwrite_matrix  (catalog[0].f, &matrix)) {
      fprintf (stderr, "can't write primary matrix");
      return (FALSE);
    }
    gfits_free_matrix (&matrix);

    /* write out Average table (convert to FITS table format) */
    AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat);
    if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
      fprintf (stderr, "can't write table header");
      return (FALSE);
    }
    if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
      fprintf (stderr, "can't write table data");
      return (FALSE);
    }
    gfits_free_table (&ftable);
    gfits_free_header (&header);
  }

  /*** Measure Table ***/

  if (catalog[0].measure != NULL) {

    measure = catalog[0].measure_catalog;

    /* XXX EAM : warn about this condition; add code to handle? */
    if (catalog[0].Nmeas_off != 0) {
      fprintf (stderr, "WARNING: LOAD_MEAS_META mixed with save??\n");
      fprintf (stderr, "WARNING: this should not be allowed to happen!\n");
    }

    /* rewind file pointers and truncate (file is still open) */
    fseek (measure[0].f, 0, SEEK_SET);
    ftruncate (fileno (measure[0].f), 0);

    /* write table PHU header */
    if (!gfits_fwrite_header  (measure[0].f, &measure[0].header)) {
      fprintf (stderr, "can't write primary header");
      return (FALSE);
    }

    /* this is probably a NOP, do I have to keep it in? */
    gfits_create_matrix (&measure[0].header, &matrix);
    if (!gfits_fwrite_matrix  (measure[0].f, &matrix)) {
      fprintf (stderr, "can't write primary matrix");
      return (FALSE);
    }
    gfits_free_matrix (&matrix);

    /* write out Measure table (convert to FITS table format) */
    MeasureToFtable (&ftable, catalog[0].measure, catalog[0].Nmeasure, catalog[0].catformat);
    if (!gfits_fwrite_Theader (measure[0].f, &header)) {
      fprintf (stderr, "can't write table header");
      return (FALSE);
    }
    if (!gfits_fwrite_table (measure[0].f, &ftable)) {
      fprintf (stderr, "can't write table data");
      return (FALSE);
    }
    gfits_free_table (&ftable);
    gfits_free_header (&header);
  }

  /*** Missing Table ***/

  if (catalog[0].missing != NULL) {

    missing = catalog[0].missing_catalog;

    /* rewind file pointers and truncate (file is still open) */
    fseek (missing[0].f, 0, SEEK_SET);
    ftruncate (fileno (missing[0].f), 0);

    /* write table PHU header */
    if (!gfits_fwrite_header  (missing[0].f, &missing[0].header)) {
      fprintf (stderr, "can't write primary header");
      return (FALSE);
    }

    /* this is probably a NOP, do I have to keep it in? */
    gfits_create_matrix (&missing[0].header, &matrix);
    if (!gfits_fwrite_matrix  (missing[0].f, &matrix)) {
      fprintf (stderr, "can't write primary matrix");
      return (FALSE);
    }
    gfits_free_matrix (&matrix);

    /* write out Missing table (convert to FITS table format) */
    gfits_table_set_Missing (&ftable, catalog[0].missing, catalog[0].Nmissing);
    if (!gfits_fwrite_Theader (missing[0].f, &header)) {
      fprintf (stderr, "can't write table header");
      return (FALSE);
    }
    if (!gfits_fwrite_table (missing[0].f, &ftable)) {
      fprintf (stderr, "can't write table data");
      return (FALSE);
    }
    gfits_free_table (&ftable);
    gfits_free_header (&header);
  }

  /*** Secfilt Table ***/

  if (catalog[0].secfilt != NULL) {

    secfilt = catalog[0].secfilt_catalog;

    /* rewind file pointers and truncate (file is still open) */
    fseek (secfilt[0].f, 0, SEEK_SET);
    ftruncate (fileno (secfilt[0].f), 0);

    /* write table PHU header */
    if (!gfits_fwrite_header  (secfilt[0].f, &secfilt[0].header)) {
      fprintf (stderr, "can't write primary header");
      return (FALSE);
    }

    /* this is probably a NOP, do I have to keep it in? */
    gfits_create_matrix (&secfilt[0].header, &matrix);
    if (!gfits_fwrite_matrix  (secfilt[0].f, &matrix)) {
      fprintf (stderr, "can't write primary matrix");
      return (FALSE);
    }
    gfits_free_matrix (&matrix);

    /* write out SecFilt table (convert to FITS table format) */
    Nitems = catalog[0].Naverage * catalog[0].Nsecfilt;
    SecFiltToFtable (&ftable, catalog[0].secfilt, Nitems, catalog[0].catformat);
    if (!gfits_fwrite_Theader (secfilt[0].f, &header)) {
      fprintf (stderr, "can't write table header");
      return (FALSE);
    }
    if (!gfits_fwrite_table (secfilt[0].f, &ftable)) {
      fprintf (stderr, "can't write table data");
      return (FALSE);
    }
    gfits_free_table (&ftable);
    gfits_free_header (&header);
  }

  return (TRUE);
}

/* update_catalog_split only writes new lines to file.  
 * if file is empty, call save_catalog_split instead.
 * XXX EAM : save_catalog SHOULD do this
 */

int dvo_catalog_update_split (Catalog *catalog, char VERBOSE) {

  int i, Nx, Ny, Nlines;
  int Nitems, Nskip, Nout, Ndisk, Nstart;
  Matrix matrix;
  Header header;
  FTable ftable;
  VTable vtable;
  Catalog *measure, *missing, *secfilt;

  ftable.header = &header;
  vtable.header = &header;

  if (catalog[0].Naverage == 0) {
    if (VERBOSE) fprintf (stderr, "no stars in catalog, skipping\n");
    return (TRUE);
  }

  /* make sure header is consistent with data */
  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure + catalog[0].Nmeas_off);
  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, catalog[0].Nsecfilt);
  gfits_modify (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);

  /* rewind file pointers and truncate (file is still open) */
  fseek (catalog[0].f, 0, SEEK_SET);

  /* write table PHU header - always write this out */
  /* XXX EAM : check if disk file size has changed */
  if (!gfits_fwrite_header  (catalog[0].f, &catalog[0].header)) {
    fprintf (stderr, "can't write primary header");
    return (FALSE);
  }

  /*** Average Table ***/

  if (catalog[0].average != NULL) {

    /* skip past matrix (already at end of header) */
    Nskip = gfits_matrix_size (&catalog[0].header);
    fseek (catalog[0].f, Nskip, SEEK_CUR);

    /* how many lines to write out? */
    Nout = catalog[0].Naverage - catalog[0].Nave_disk;

    /* write out Average table (convert to FITS table format) */
    AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat);
    /* convert only output rows to vtable */
    gfits_table_to_vtable (&ftable, &vtable, catalog[0].Nave_disk, Nout);

    if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
      fprintf (stderr, "can't write table header");
      return (FALSE);
    }
    if (!gfits_fwrite_vtable (catalog[0].f, &vtable)) {
      fprintf (stderr, "can't write table data");
      return (FALSE);
    }
    gfits_free_vtable (&vtable);
    gfits_free_table (&ftable);
    gfits_free_header (&header);
  }

  /*** Measure Table ***/

  if (catalog[0].measure != NULL) {

    measure = catalog[0].measure_catalog;

    /* skip past PHU header and matrix */
    Nskip = measure[0].header.size + gfits_matrix_size (&measure[0].header);
    fseek (measure[0].f, Nskip, SEEK_SET);

    Ndisk  = catalog[0].Nmeas_disk;
    Nstart = catalog[0].Nmeas_disk - catalog[0].Nmeas_off; /* where is first new line? */
    Nout   = catalog[0].Nmeasure - Nstart;                   /* how many lines to write out? */
    Nlines = catalog[0].Nmeasure + catalog[0].Nmeas_off;   /* how many lines total in file */

    /* convert to output format FITS table (only rows for output : 0 - Nout) */
    MeasureToFtable (&ftable, &catalog[0].measure[Nstart], Nout, catalog[0].catformat);

    gfits_scan (&header, "NAXIS1", "%d", 1, &Nx);
    gfits_scan (&header, "NAXIS2", "%d", 1, &Ny);

    /* convert all output rows to vtable */
    ALLOCATE (vtable.row, int, MAX (1, Nout));
    ALLOCATE (vtable.buffer, char *, MAX (1, Nout));
    for (i = 0; i < Nout; i++) {
      ALLOCATE (vtable.buffer[i], char, MAX (1, Nx));
      memcpy (vtable.buffer[i], &ftable.buffer[i*Nx], Nx);
      vtable.row[i] = i + Ndisk;
    }

    /* modify vtable to represent full disk table */
    gfits_modify (&header, "NAXIS2", "%d", 1, Nlines);
    header.Naxis[1] = Nlines;

    vtable.size = gfits_matrix_size (&header);
    vtable.Nrow = Nout;
    vtable.pad = vtable.size - Nx*Ny;

    if (!gfits_fwrite_Theader (measure[0].f, &header)) {
      fprintf (stderr, "can't write table header");
      return (FALSE);
    }
    if (!gfits_fwrite_vtable (measure[0].f, &vtable)) {
      fprintf (stderr, "can't write table data");
      return (FALSE);
    }
    gfits_free_vtable (&vtable);
    gfits_free_table (&ftable);
    gfits_free_header (&header);
  }

  /*** Missing Table ***/
  /* missing table CANNOT be written unsorted, thus it is always written 
     out in full */

  if (catalog[0].missing != NULL) {

    missing = catalog[0].missing_catalog;

    /* rewind file pointers and truncate (file is still open) */
    fseek (missing[0].f, 0, SEEK_SET);
    ftruncate (fileno (missing[0].f), 0);

    /* write table PHU header */
    if (!gfits_fwrite_header  (missing[0].f, &missing[0].header)) {
      fprintf (stderr, "can't write primary header");
      return (FALSE);
    }

    /* this is probably a NOP, do I have to keep it in? */
    gfits_create_matrix (&missing[0].header, &matrix);
    if (!gfits_fwrite_matrix  (missing[0].f, &matrix)) {
      fprintf (stderr, "can't write primary matrix");
      return (FALSE);
    }
    gfits_free_matrix (&matrix);

    /* write out Missing table (convert to FITS table format) */
    gfits_table_set_Missing (&ftable, catalog[0].missing, catalog[0].Nmissing);
    if (!gfits_fwrite_Theader (missing[0].f, &header)) {
      fprintf (stderr, "can't write table header");
      return (FALSE);
    }
    if (!gfits_fwrite_table (missing[0].f, &ftable)) {
      fprintf (stderr, "can't write table data");
      return (FALSE);
    }
    gfits_free_table (&ftable);
    gfits_free_header (&header);
  }

  /*** Secfilt Table ***/

  if (catalog[0].secfilt != NULL) {

    secfilt = catalog[0].secfilt_catalog;

    /* skip past PHU header and matrix */
    Nskip = secfilt[0].header.size + gfits_matrix_size (&secfilt[0].header);
    fseek (secfilt[0].f, Nskip, SEEK_SET);

    /* how many lines to write out? */
    Nout  = catalog[0].Nsecfilt * (catalog[0].Naverage - catalog[0].Nave_disk);
    Ndisk = catalog[0].Nsecfilt * catalog[0].Nave_disk;

    /* convert to output format FITS table */
    Nitems = catalog[0].Naverage * catalog[0].Nsecfilt;
    SecFiltToFtable (&ftable, catalog[0].secfilt, Nitems, catalog[0].catformat);
    /* convert only output rows to vtable */
    gfits_table_to_vtable (&ftable, &vtable, Ndisk, Nout);

    if (!gfits_fwrite_Theader (secfilt[0].f, &header)) {
      fprintf (stderr, "can't write table header");
      return (FALSE);
    }
    if (!gfits_fwrite_vtable (secfilt[0].f, &vtable)) {
      fprintf (stderr, "can't write table data");
      return (FALSE);
    }
    gfits_free_vtable (&vtable);
    gfits_free_table (&ftable);
    gfits_free_header (&header);
  }

  return (TRUE);
}

/* in split mode, extra files are linked to catalog->measure_catalog, etc.  Each
   has a valid filename, f, header.  The primary catalog data pointers are set
   to point at the corresponding entries from the elements on the chain. An
   unloaded entry has a null pointer here.
*/

/* XXX EAM : this file needs work on the error exit conditions and memory leaks, esp under errors */

/* XXX EAM : update is not efficient.  MeasureToFtable should only 
   convert the new rows (Nmeas_disk to Nmeasure). the resulting
   table represents the end rows of the vtable.  we need to define
   the vtable based on the ftable, but with Ny = Nmeasure */  
  
