# include <dvo.h>

// return options: 
// * error (cannot lock, open, read, etc)
// * empty (file is not found)
// * ok

int dvo_catalog_secfilt_to_primary (Catalog *catalog, SecFilt **myPrimary, SecFilt **mySecfilt, int *myNsecfilt) {

  off_t i, j, Nallfilt, Nsecfilt, Ntotal;

  SecFilt *primary;
  SecFilt *secfilt;

  if (catalog[0].secfilt == NULL) {       
    fprintf (stderr, "missing secfilt, cannot build output averages (dvo_catalog_split.c)\n");
    exit (1);
  }
  secfilt = catalog[0].secfilt;

  // XXX this translation only works if we have loaded / created a matched average/secfilt set
  assert (catalog[0].Nsecf_mem == catalog[0].Nsecfilt*catalog[0].Naverage);

  Nallfilt = catalog[0].Nsecfilt;
  Nsecfilt = catalog[0].Nsecfilt - 1;
  Ntotal = Nsecfilt * catalog[0].Naverage;
  ALLOCATE (primary, SecFilt, catalog[0].Naverage);
  ALLOCATE (secfilt, SecFilt, Ntotal);

  for (i = 0; i < catalog[0].Naverage; i++) {
    primary[i] = secfilt[i*Nallfilt + 0];
    for (j = 0; j < Nsecfilt; j++) {
      secfilt[i*Nsecfilt + j] = catalog[0].secfilt[i*Nallfilt + j + 1];
    }
  }		
  catalog[0].Nsecfilt --;
  catalog[0].Nsecf_mem = catalog[0].Naverage*catalog[0].Nsecfilt;

  *myPrimary = primary;
  *mySecfilt = secfilt;
  *myNsecfilt = Nsecfilt;
  return (TRUE);
}

int dvo_catalog_primary_to_secfilt (Catalog *catalog, SecFilt *primary, off_t Naves) {

  off_t Ntmpfilt, Nsecfilt, Ntotal, i, j;
  SecFilt *tmpfilt;

  tmpfilt  = catalog[0].secfilt;
  Ntmpfilt = catalog[0].Nsecfilt;

  // we do NOT modify Nsecf_disk; this operation only modifies in in-memory values

  catalog[0].Nsecfilt ++;
  Nsecfilt = catalog[0].Nsecfilt;
  Ntotal = Nsecfilt * Naves;

  catalog[0].Nsecf_mem = Ntotal;

  ALLOCATE (catalog[0].secfilt, SecFilt, Ntotal);
  for (i = 0; i < Naves; i++) {
    catalog[0].secfilt[i*Nsecfilt + 0] = primary[i];
    for (j = 0; j < Ntmpfilt; j++) {
      catalog[0].secfilt[i*Nsecfilt + j + 1] = tmpfilt[i*Ntmpfilt + j];
    }
  }		
  free (tmpfilt);
  free (primary);
  return (TRUE);
}

int dvo_catalog_save_subcat (Catalog *catalog, FTable *ftable, off_t start, off_t Nrows, off_t Ndisk, off_t Ntotal) {

  Matrix matrix;

  // ohana_memcheck_func (TRUE);
  

  if (0) {
    // XXX a TEST : is the data corrupted or the file?
    char name[1024];
    snprintf (name, 1024, "%s.phu.v2", catalog->filename);
    FILE *f = fopen (name, "w");
    fwrite (catalog->header.buffer, sizeof(char), catalog->header.datasize, f);
    // fprintf (stderr, "save %d bytes in %s (%zx)\n", (int) catalog->header.datasize, name, (size_t) catalog->header.buffer);
    // fprintf (stderr, "first bytes are %c%c%c%c%c%c%c%c\n", 
    // 	     catalog->header.buffer[0], catalog->header.buffer[1], catalog->header.buffer[2], catalog->header.buffer[3], 
    // 	     catalog->header.buffer[4], catalog->header.buffer[5], catalog->header.buffer[6], catalog->header.buffer[7]);
    fclose (f);
  }
  if (0) {
    // XXX a TEST : is the data corrupted or the file?
    char name[1024];
    snprintf (name, 1024, "%s.phu", catalog->filename);
    FILE *f = fopen (name, "w");
    fwrite (catalog->header.buffer, sizeof(char), catalog->header.datasize, f);
    // fprintf (stderr, "save %d bytes in %s (%zx)\n", (int) catalog->header.datasize, name, (size_t) catalog->header.buffer);
    // fprintf (stderr, "first bytes are %c%c%c%c%c%c%c%c\n", 
    // 	     catalog->header.buffer[0], catalog->header.buffer[1], catalog->header.buffer[2], catalog->header.buffer[3], 
    // 	     catalog->header.buffer[4], catalog->header.buffer[5], catalog->header.buffer[6], catalog->header.buffer[7]);
    fclose (f);
  }
# if (0)
  {
    // XXX a TEST : is the data corrupted or the file?
    char name[1024];
    snprintf (name, 1024, "%s.phu.v2", catalog->filename);
    FILE *f = fopen (name, "w");
    fwrite (catalog->header.buffer, sizeof(char), catalog->header.datasize, f);
    fprintf (stderr, "save %d bytes in %s (%zx)\n", (int) catalog->header.datasize, name, (size_t) catalog->header.buffer);
    fprintf (stderr, "first bytes are %c%c%c%c%c%c%c%c\n", 
	     catalog->header.buffer[0], catalog->header.buffer[1], catalog->header.buffer[2], catalog->header.buffer[3], 
	     catalog->header.buffer[4], catalog->header.buffer[5], catalog->header.buffer[6], catalog->header.buffer[7]);
    fclose (f);
  }
  {
    // XXX a TEST : is the data corrupted or the file?
    char name[1024];
    snprintf (name, 1024, "%s.phu.v3", catalog->filename);
    FILE *f = fopen (name, "w");
    fwrite (catalog->header.buffer, sizeof(char), catalog->header.datasize, f);
    fprintf (stderr, "save %d bytes in %s (%zx)\n", (int) catalog->header.datasize, name, (size_t) catalog->header.buffer);
    fprintf (stderr, "first bytes are %c%c%c%c%c%c%c%c\n", 
	     catalog->header.buffer[0], catalog->header.buffer[1], catalog->header.buffer[2], catalog->header.buffer[3], 
	     catalog->header.buffer[4], catalog->header.buffer[5], catalog->header.buffer[6], catalog->header.buffer[7]);
    fclose (f);
  }
# endif
  /* rewind file pointers and truncate (file is still open) */
  if (fseeko (catalog->f, 0LL, SEEK_SET)) {
    perror ("dvo_catalog_save_subset: ");
    fprintf (stderr, "failed to seek to beginning\n");
  }

  // write PHU header
  if (!gfits_fwrite_header  (catalog->f, &catalog->header)) {
    fprintf (stderr, "can't write primary header\n");
    return (FALSE);
  }
  off_t fullsize = catalog->header.datasize;

  // write the PHU matrix; this is probably a NOP, do I have to keep it in?
  gfits_create_matrix (&catalog->header, &matrix);
  if (!gfits_fwrite_matrix  (catalog->f, &matrix)) {
    fprintf (stderr, "can't write primary matrix\n");
    gfits_free_matrix (&matrix);
    return (FALSE);
  }
  fullsize += matrix.datasize;
  gfits_free_matrix (&matrix);

  // write the table data
  if (!gfits_fwrite_ftable_range (catalog->f, ftable, start, Nrows, Ndisk, Ntotal)) {
    fprintf (stderr, "can't write table data\n");
    return (FALSE);
  }
  fullsize += ftable->datasize + ftable->header->datasize;

  // if the output file will be completely re-written, truncate to total datasize
  if ((start == 0) && (Nrows == Ntotal)) {
    int fd = fileno (catalog->f);
    if (ftruncate (fd, fullsize)) {
      perror ("dvo_catalog_save_subset: ");
      return (FALSE);
    }
  }

  return (TRUE);
}

int dvo_catalog_open_subcat (Catalog *catalog, Catalog **Subcat, Header *header, char *name, int VERBOSE) {

  off_t Nskip;
  int status;
  char *path, string[80];
  Catalog *subcat;

  /* in split mode, we need to init & open the corresponding measure file (even if we do not read
   * any data in at this stage) */
  ALLOCATE (subcat, Catalog, 1);
  dvo_catalog_init (subcat, TRUE);

  *Subcat = subcat;

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

  /* get split filename from main header (paths relative to cpt file) */
  if (!gfits_scan (&catalog[0].header, name,  "%s", 1, string)) {
    free (path);
    // databases created prior to ~2014.07.01 did not have the LENSING, LENSOBJ, STARPAR paths in their headers.
    // in these cases, we do not try to lock or open the relevant file
    if (!strcmp (name, "LENSING")) return (DVO_CAT_OPEN_EMPTY);
    if (!strcmp (name, "LENSOBJ")) return (DVO_CAT_OPEN_EMPTY);
    if (!strcmp (name, "STARPAR")) return (DVO_CAT_OPEN_EMPTY);
    return (DVO_CAT_OPEN_FAIL);
  }
  ALLOCATE (subcat[0].filename, char, strlen(path) + strlen(string) + 2);
  sprintf (subcat[0].filename, "%s/%s", path, string);
  free (path);

  /* lock & open catalog file */
  status = dvo_catalog_lock (subcat, catalog[0].lockmode);
  if (status != DVO_CAT_OPEN_OK) {
    if (VERBOSE) {
      if (status == DVO_CAT_OPEN_EMPTY) {
	fprintf (stderr, "%s (%s) is empty\n", name, subcat[0].filename);
      } else {
	fprintf (stderr, "failure to lock %s (%s)\n", name, subcat[0].filename);
      }
    }
    return (status);
  }

  /* read PHU */
  if (!gfits_load_header (subcat[0].f, &subcat[0].header)) {
    if (VERBOSE) fprintf (stderr, "error reading %s header: %s\n", name, subcat[0].filename);
    return (DVO_CAT_OPEN_FAIL);
  }
  Nskip = gfits_data_size (&subcat[0].header);
  fseeko (subcat[0].f, Nskip, SEEK_CUR);

  /* read table header */
  if (!gfits_fread_header (subcat[0].f, header)) {
    if (VERBOSE) fprintf (stderr, "can't read %s PHU header\n", name);
    gfits_free_header (&subcat[0].header);
    return (DVO_CAT_OPEN_FAIL);
  }
  return (DVO_CAT_OPEN_OK);
}

int dvo_catalog_load_split (Catalog *catalog, int VERBOSE) {

  off_t Nbytes;
  off_t Naverage;
  off_t Nmeasure;
  off_t Nmissing;
  off_t Nlensing;
  off_t Nlensobj;
  off_t Nstarpar;
  off_t Nitems;
  int status, Nsecfilt;
  Header header;
  FTable ftable;
  SecFilt *primary;

  /* ftable header storage for below */
  ftable.header = &header;
  ftable.buffer = NULL;
  header.buffer = NULL;
  primary = NULL;
  status = FALSE;

  /* get the components from the header - these duplicate information in the split files (NAXIS2) */
  // NSTARS, NMEAS, NMISS are required; NLENSING, NLENSOBJ, NSTARPAR are not (0 if not found)
  if (!gfits_scan (&catalog[0].header, "NSTARS",   OFF_T_FMT, 1,  &Naverage)) return (FALSE);
  if (!gfits_scan (&catalog[0].header, "NMEAS",    OFF_T_FMT, 1,  &Nmeasure)) return (FALSE);
  if (!gfits_scan (&catalog[0].header, "NMISS",    OFF_T_FMT, 1,  &Nmissing)) return (FALSE);
  if (!gfits_scan (&catalog[0].header, "NSECFILT",      "%d", 1,  &Nsecfilt)) Nsecfilt = 0;
  if (!gfits_scan (&catalog[0].header, "NLENSING", OFF_T_FMT, 1,  &Nlensing)) Nlensing = 0;
  if (!gfits_scan (&catalog[0].header, "NLENSOBJ", OFF_T_FMT, 1,  &Nlensobj)) Nlensobj = 0;
  if (!gfits_scan (&catalog[0].header, "NSTARPAR", OFF_T_FMT, 1,  &Nstarpar)) Nstarpar = 0;

  /* the OBJID is a counter that uniquely defines an average entry and never changes.  if
     it is not defined for a legacy database, we can generate them using the existing index values.
     If it is missing, give a warning and recommend the user upgrade the DB */
  if (!gfits_scan (&catalog[0].header, "OBJID",    "%u", 1, &catalog[0].objID)) {
    if (VERBOSE) fprintf (stderr, "WARNING: OBJID is not set for this database: upgrade for full feature set\n");
    catalog[0].objID = 0;
  }
  if (!gfits_scan (&catalog[0].header, "CATID",    "%u", 1, &catalog[0].catID)) {
    if (VERBOSE) fprintf (stderr, "WARNING: CATID is not set for this database: upgrade for full feature set\n");
    catalog[0].catID = 0;
  }

  /* save the current number so we can do partial updates */
  catalog[0].Naves_disk = Naverage;
  catalog[0].Nmeas_disk = Nmeasure;
  catalog[0].Nmiss_disk = Nmissing;
  catalog[0].Nsecf_disk = Naverage * Nsecfilt;
  catalog[0].Nlensing_disk = Nlensing;
  catalog[0].Nlensobj_disk = Nlensobj;
  catalog[0].Nstarpar_disk = Nstarpar;

  /**  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, but we will assign these a valid value before we exit (even if empty) */
  catalog[0].average = NULL;
  catalog[0].measure = NULL;
  catalog[0].missing = NULL;
  catalog[0].secfilt = NULL;
  catalog[0].lensing = NULL;
  catalog[0].lensobj = NULL;
  catalog[0].starpar = NULL;

  /*** Average Table ***/

  /* need to read the table header to determine format (whether or not we read averages) */
  /* move pointer past PHU header -- must be already read (load_catalog) */
  Nbytes = catalog[0].header.datasize + gfits_data_size (&catalog[0].header);
  fseeko (catalog[0].f, Nbytes, SEEK_SET);

  /* read Average table header */
  if (!gfits_fread_header (catalog[0].f, &header)) { 
    if (VERBOSE) fprintf (stderr, "can't read table average header");
    return (FALSE);
  }
  if (catalog[0].catflags & LOAD_AVES) {
    /* read Average table data : format is irrelevant here */
    if (!gfits_fread_ftable_data (catalog[0].f, &ftable, FALSE)) { 
      if (VERBOSE) fprintf (stderr, "can't read table average data");
      return (FALSE);
    }
    /* convert the saved version of the table to the internal version.  Old versions of DVO stored
     * one of the average magnitudes in Average.  We save this in case it is needed below.  NOTE:
     * primary is only used if we read in the secfilt table, otherwise it should be freed */
    catalog[0].average = FtableToAverage (&ftable, &Naverage, &catalog[0].catformat, &primary);
    if (Naverage != catalog[0].Naves_disk) {
      fprintf (stderr, "Warning: mismatch between Naverage in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Naverage,  catalog[0].Naves_disk);
    }
    gfits_free_header (&header);
    catalog[0].Naverage = catalog[0].Naves_disk;
    catalog[0].Naves_off = 0;
  } else {
    catalog[0].catformat = FtableGetFormat (&ftable);
    ALLOCATE (catalog[0].average, Average, 1);
    catalog[0].Naverage = 0;
    catalog[0].Naves_off = catalog[0].Naves_disk;
  }

  /*** Measure Table ***/
  if (!(catalog[0].catflags & SKIP_MEAS)) {
    // unless we specify 'skip', we still need to load the 
    status = dvo_catalog_open_subcat (catalog, &catalog[0].measure_catalog, ftable.header, "MEASURE", VERBOSE);
    if (status == DVO_CAT_OPEN_FAIL) {
      return (FALSE);
    }
    if ((status == DVO_CAT_OPEN_EMPTY) && (catalog[0].Nmeas_disk > 0)) {
      return (FALSE);
    }
  }
  if ((status != DVO_CAT_OPEN_EMPTY) && (catalog[0].catflags & LOAD_MEAS)) {
    // XXX this allows an empty Measure catalog with non-empty Average catalog : is that OK?
    /* read Measure table data */
    if (!gfits_fread_ftable_data (catalog[0].measure_catalog[0].f, &ftable, FALSE)) {
      if (VERBOSE) fprintf (stderr, "can't read table measure data\n");
      return (FALSE);
    }
    /* convert data format to internal : returns number of row read in Nmeasure */
    catalog[0].measure = FtableToMeasure (&ftable, catalog[0].average, &Nmeasure, &catalog[0].catformat);
    if (Nmeasure != catalog[0].Nmeas_disk) {
      fprintf (stderr, "Warning: mismatch between Nmeasure in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmeasure,  catalog[0].Nmeas_disk);
    }
    catalog[0].Nmeasure = catalog[0].Nmeas_disk;
    catalog[0].Nmeas_off = 0;
  } else {
    // XXX is it necessary to generate a template header here?
    // XXX this is a memory leak, right?
    if (catalog[0].measure_catalog) {
      gfits_free_header (&catalog[0].measure_catalog[0].header);
    } else {
      ALLOCATE (catalog[0].measure_catalog, Catalog, 1);
      dvo_catalog_init (catalog[0].measure_catalog, TRUE);
    }
    gfits_create_header (&catalog[0].measure_catalog[0].header);
    ALLOCATE (catalog[0].measure, Measure, 1);
    catalog[0].Nmeasure = 0;
    catalog[0].Nmeas_off = catalog[0].Nmeas_disk;
  }
  gfits_free_header (&header);

  /*** Missing Table ***/
  status = dvo_catalog_open_subcat (catalog, &catalog[0].missing_catalog, ftable.header, "MISSING", VERBOSE);
  if (status == DVO_CAT_OPEN_FAIL) {
    return (FALSE);
  }
  if ((status == DVO_CAT_OPEN_EMPTY) && (catalog[0].Nmiss_disk > 0)) {
    return (FALSE);
  }
  if ((status != DVO_CAT_OPEN_EMPTY) && (catalog[0].catflags & LOAD_MISS)) {
    /* read Missing table data */
    if (!gfits_fread_ftable_data (catalog[0].missing_catalog[0].f, &ftable, FALSE)) {
      if (VERBOSE) fprintf (stderr, "can't read table missing data\n");
      return (FALSE);
    }
    /* no conversions currently defined : this just does the byte swap */
    catalog[0].missing = gfits_table_get_Missing (&ftable, &Nmissing, NULL);
    if (!catalog[0].missing) {
      fprintf (stderr, "ERROR: failed to read missing\n");
      exit (2);
    }
    if (Nmissing != catalog[0].Nmiss_disk) {
      fprintf (stderr, "Warning: mismatch between Nmissing in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmissing,  catalog[0].Nmiss_disk);
    }
    catalog[0].Nmissing = catalog[0].Nmiss_disk;
    catalog[0].Nmiss_off = 0;
  } else {
    // XXX is it necessary to generate a template header here?
    gfits_create_header (&catalog[0].missing_catalog[0].header);
    ALLOCATE (catalog[0].missing, Missing, 1);
    catalog[0].Nmissing = 0;
    catalog[0].Nmiss_off = catalog[0].Nmiss_disk;
  }
  gfits_free_header (ftable.header);

  /*** Secfilt Table ***/
  status = dvo_catalog_open_subcat (catalog, &catalog[0].secfilt_catalog, ftable.header, "SECFILT", VERBOSE);
  // fprintf (stderr, "secfilt header %s : %zx\n", catalog[0].secfilt_catalog[0].filename, (size_t) catalog[0].secfilt_catalog[0].header.buffer);
  if (status == DVO_CAT_OPEN_FAIL) {
    return (FALSE);
  }
  if ((status == DVO_CAT_OPEN_EMPTY) && (catalog[0].Nsecf_disk > 0)) {
    return (FALSE);
  }
  if ((status != DVO_CAT_OPEN_EMPTY) && (catalog[0].catflags & LOAD_SECF)) {
    /* read secfilt table data */
    if (!gfits_fread_ftable_data (catalog[0].secfilt_catalog[0].f, &ftable, FALSE)) {
      if (VERBOSE) fprintf (stderr, "can't read table secfilt data\n");
      return (FALSE);
    }
    catalog[0].secfilt = FtableToSecFilt (&ftable, &Nitems, &catalog[0].catformat);
    if (Nitems != catalog[0].Nsecf_disk) {
      fprintf (stderr, "Warning: mismatch between Nsecfilt items in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nitems,  catalog[0].Nsecf_disk);
    }
    catalog[0].Nsecf_mem = catalog[0].Nsecf_disk;
    catalog[0].Nsecf_off = 0;

    /* if primary is defined, we were supplied with one additional average magnitude from Average
       we need to interleave these magnitudes with the secfilt entries just loaded */
    if (primary != NULL) {
      // this modifies catalog.Nsecf_mem,Nsecfilt
      dvo_catalog_primary_to_secfilt (catalog, primary, catalog[0].Naves_disk);
    } 
  } else {
    if (primary != NULL) {
      free (primary);
      catalog[0].Nsecfilt ++;
    }
    gfits_create_header (&catalog[0].secfilt_catalog[0].header);
    ALLOCATE (catalog[0].secfilt, SecFilt, 1);
    catalog[0].Nsecf_mem = 0;
    catalog[0].Nsecf_off = catalog[0].Nsecf_disk;
  }
  gfits_free_header (ftable.header);

  /*** Lensing Table ***/
  if (!(catalog[0].catflags & SKIP_LENSING)) {
    // unless we specify 'skip', we still need to load the Lensing table
    status = dvo_catalog_open_subcat (catalog, &catalog[0].lensing_catalog, ftable.header, "LENSING", VERBOSE);
    if (status == DVO_CAT_OPEN_FAIL) {
      return (FALSE);
    }
    // the Lensing file need not exist, unless we expect data to exist
    if ((status == DVO_CAT_OPEN_EMPTY) && (catalog[0].Nlensing_disk > 0)) {
      return (FALSE);
    }
  }
  if ((status != DVO_CAT_OPEN_EMPTY) && (catalog[0].catflags & LOAD_LENSING)) {
    // only read the Lensing table if the file actually exists
    /* read Lensing table data */
    if (!gfits_fread_ftable_data (catalog[0].lensing_catalog[0].f, &ftable, FALSE)) {
      if (VERBOSE) fprintf (stderr, "can't read table lensing data\n");
      return (FALSE);
    }
    /* convert data format to internal : returns number of row read in Nlensing */
    catalog[0].lensing = FtableToLensing (&ftable, &Nlensing, &catalog[0].catformat);
    if (Nlensing != catalog[0].Nlensing_disk) {
      fprintf (stderr, "Warning: mismatch between Nlensing in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nlensing,  catalog[0].Nlensing_disk);
    }
    catalog[0].Nlensing = catalog[0].Nlensing_disk;
    catalog[0].Nlensing_off = 0;
  } else {
    if (catalog[0].lensing_catalog) {
      gfits_free_header (&catalog[0].lensing_catalog[0].header);
    } else {
      ALLOCATE (catalog[0].lensing_catalog, Catalog, 1);
      dvo_catalog_init (catalog[0].lensing_catalog, TRUE);
    }
    gfits_create_header (&catalog[0].lensing_catalog[0].header);
    ALLOCATE (catalog[0].lensing, Lensing, 1);
    catalog[0].Nlensing = 0;
    catalog[0].Nlensing_off = catalog[0].Nlensing_disk;
  }
  gfits_free_header (&header);

  /*** Lensobj Table ***/
  if (!(catalog[0].catflags & SKIP_LENSOBJ)) {
    // unless we specify 'skip', we still need to load the 
    status = dvo_catalog_open_subcat (catalog, &catalog[0].lensobj_catalog, ftable.header, "LENSOBJ", VERBOSE);
    if (status == DVO_CAT_OPEN_FAIL) {
      return (FALSE);
    }
    // the Lensobj file need not exist, unless we expect data to exist
    if ((status == DVO_CAT_OPEN_EMPTY) && (catalog[0].Nlensobj_disk > 0)) {
      return (FALSE);
    }
  }
  if ((status != DVO_CAT_OPEN_EMPTY) && (catalog[0].catflags & LOAD_LENSOBJ)) {
    // only read the Lensobj table if the file actually exists
    /* read Lensobj table data */
    if (!gfits_fread_ftable_data (catalog[0].lensobj_catalog[0].f, &ftable, FALSE)) {
      if (VERBOSE) fprintf (stderr, "can't read table lensobj data\n");
      return (FALSE);
    }
    /* convert data format to internal : returns number of row read in Nlensobj */
    catalog[0].lensobj = FtableToLensobj (&ftable, &Nlensobj, &catalog[0].catformat);
    if (Nlensobj != catalog[0].Nlensobj_disk) {
      fprintf (stderr, "Warning: mismatch between Nlensobj in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nlensobj,  catalog[0].Nlensobj_disk);
    }
    catalog[0].Nlensobj = catalog[0].Nlensobj_disk;
    catalog[0].Nlensobj_off = 0;
  } else {
    if (catalog[0].lensobj_catalog) {
      gfits_free_header (&catalog[0].lensobj_catalog[0].header);
    } else {
      ALLOCATE (catalog[0].lensobj_catalog, Catalog, 1);
      dvo_catalog_init (catalog[0].lensobj_catalog, TRUE);
    }
    gfits_create_header (&catalog[0].lensobj_catalog[0].header);
    ALLOCATE (catalog[0].lensobj, Lensobj, 1);
    catalog[0].Nlensobj = 0;
    catalog[0].Nlensobj_off = catalog[0].Nlensobj_disk;
  }
  gfits_free_header (&header);

  /*** StarPar Table ***/
  if (!(catalog[0].catflags & SKIP_STARPAR)) {
    // unless we specify 'skip', we still need to load the 
    status = dvo_catalog_open_subcat (catalog, &catalog[0].starpar_catalog, ftable.header, "STARPAR", VERBOSE);
    if (status == DVO_CAT_OPEN_FAIL) {
      return (FALSE);
    }
    // the StarPar file need not exist, unless we expect data to exist
    if ((status == DVO_CAT_OPEN_EMPTY) && (catalog[0].Nstarpar_disk > 0)) {
      return (FALSE);
    }
  }
  if ((status != DVO_CAT_OPEN_EMPTY) && (catalog[0].catflags & LOAD_STARPAR)) {
    // only read the StarPar table if the file actually exists
    /* read StarPar table data */
    if (!gfits_fread_ftable_data (catalog[0].starpar_catalog[0].f, &ftable, FALSE)) {
      if (VERBOSE) fprintf (stderr, "can't read table starpar data\n");
      return (FALSE);
    }
    /* convert data format to internal : returns number of row read in Nstarpar */
    catalog[0].starpar = FtableToStarPar (&ftable, &Nstarpar, &catalog[0].catformat);
    if (Nstarpar != catalog[0].Nstarpar_disk) {
      fprintf (stderr, "Warning: mismatch between Nstarpar in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nstarpar,  catalog[0].Nstarpar_disk);
    }
    catalog[0].Nstarpar = catalog[0].Nstarpar_disk;
    catalog[0].Nstarpar_off = 0;
  } else {
    if (catalog[0].starpar_catalog) {
      gfits_free_header (&catalog[0].starpar_catalog[0].header);
    } else {
      ALLOCATE (catalog[0].starpar_catalog, Catalog, 1);
      dvo_catalog_init (catalog[0].starpar_catalog, TRUE);
    }
    gfits_create_header (&catalog[0].starpar_catalog[0].header);
    ALLOCATE (catalog[0].starpar, StarPar, 1);
    catalog[0].Nstarpar = 0;
    catalog[0].Nstarpar_off = catalog[0].Nstarpar_disk;
  }
  gfits_free_header (&header);

  return (TRUE);
}

// I need to always read both average and secfilt at the same time to correctly manage the
// primary secfilt values...
int dvo_catalog_load_segment_split (Catalog *catalog, int VERBOSE, off_t start, off_t Nrows) {

  off_t Nbytes;
  off_t Naverage, Nexpect, Nitems, Nmeasure, Nmissing, Nlensing, Nlensobj, Nstarpar;
  Header header;
  FTable ftable;
  SecFilt *primary;

  /* ftable header storage for below */
  ftable.header = &header;
  ftable.buffer = NULL;
  header.buffer = NULL;
  primary = NULL;

  /*** Average (& SecFilt) Table ***/
  if (catalog[0].catflags & LOAD_AVES) {

    /*** load the Average data ***/
    /* move pointer past header and matrix -- must be already read (load_catalog) */
    Nbytes = catalog[0].header.datasize + gfits_data_size (&catalog[0].header);
    fseeko (catalog[0].f, Nbytes, SEEK_SET);
    /* 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 : format is irrelevant here */
    if (!gfits_fread_ftable_range (catalog[0].f, FALSE, &ftable, start, Nrows)) {
      if (VERBOSE) fprintf (stderr, "can't read table average data");
      return (FALSE);
    }

    /* convert the saved version of the table to the internal version.  Old versions of DVO stored
     * one of the average magnitudes in Average.  We save this in case it is needed below.  NOTE:
     * primary is only used if we read in the secfilt table, otherwise it should be freed */
    catalog[0].average = FtableToAverage (&ftable, &Naverage, &catalog[0].catformat, &primary);
    if (Naverage != Nrows) {
      // XXX this condition denotes the eof has been reached; not an error or a warning
      // fprintf (stderr, "Warning: mismatch between Naverage in PHU and Table headers (%d vs %d)\n", Naverage, Nrows);
    }
    gfits_free_header (&header);
    catalog[0].Naverage = Naverage;
    catalog[0].Naves_off = start;

    /*** load the secfilt data ***/
    Catalog *subcat = catalog[0].secfilt_catalog;

    /* move pointer past header -- must be already read (load_catalog) */
    Nbytes = subcat[0].header.datasize + gfits_data_size (&subcat[0].header);
    fseeko (subcat[0].f, Nbytes, SEEK_SET);

    /* read Secfilt table header */
    if (!gfits_fread_header (subcat[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table measure header");
      return (FALSE);
    }
    /* read Secfilt table data : format is irrelevant here */
    if (!gfits_fread_ftable_range (subcat[0].f, FALSE, &ftable, start*catalog[0].Nsecfilt, catalog[0].Naverage*catalog[0].Nsecfilt)) {
      if (VERBOSE) fprintf (stderr, "can't read table measure data");
      return (FALSE);
    }

    Nexpect = catalog[0].Naverage * catalog[0].Nsecfilt;
    catalog[0].secfilt = FtableToSecFilt (&ftable, &Nitems, &catalog[0].catformat);
    if (Nitems != Nexpect) {
      fprintf (stderr, "Warning: mismatch between Nsecfilt items in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nitems,  Nexpect);
    }
    catalog[0].Nsecf_mem = catalog[0].Naverage * catalog[0].Nsecfilt;
    catalog[0].Nsecf_off = start               * catalog[0].Nsecfilt;

    /* if primary is defined, we were supplied with one additional average magnitude from Average
       we need to interleave these magnitudes with the secfilt entries just loaded */
    if (primary != NULL) {
      dvo_catalog_primary_to_secfilt (catalog, primary, Nrows);
    } 
    gfits_free_header (&header);
  }

  // XXX check the open status of the catalog
  if (catalog[0].catflags & LOAD_MEAS) {

    Catalog *subcat = catalog[0].measure_catalog;

    /* move pointer past header -- must be already read (load_catalog) */
    Nbytes = subcat[0].header.datasize + gfits_data_size (&subcat[0].header);
    fseeko (subcat[0].f, Nbytes, SEEK_SET);

    /* read Measure table header */
    if (!gfits_fread_header (subcat[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table measure header");
      return (FALSE);
    }
    /* read Measure table data : format is irrelevant here */
    if (!gfits_fread_ftable_range (subcat[0].f, FALSE, &ftable, start, Nrows)) {
      if (VERBOSE) fprintf (stderr, "can't read table measure data");
      return (FALSE);
    }

    /* convert data format to internal : returns number of row read in Nmeasure */
    catalog[0].measure = FtableToMeasure (&ftable, catalog[0].average, &Nmeasure, &catalog[0].catformat);
    if (Nmeasure != Nrows) {
      // XXX this condition denotes the eof has been reached; not an error or a warning
      // fprintf (stderr, "Warning: mismatch between Nmeasure in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmeasure,  Nrows);
    }
    gfits_free_header (&header);
    catalog[0].Nmeasure = Nmeasure;
    catalog[0].Nmeas_off = start;
  }

  // XXX check the open status of the catalog?
  if (catalog[0].catflags & LOAD_MISS) {

    Catalog *subcat = catalog[0].missing_catalog;

    /* move pointer past header -- must be already read (load_catalog) */
    Nbytes = subcat[0].header.datasize + gfits_data_size (&subcat[0].header);
    fseeko (subcat[0].f, Nbytes, SEEK_SET);

    /* read Missing table header */
    if (!gfits_fread_header (subcat[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table missing header");
      return (FALSE);
    }
    /* read Missing table data : format is irrelevant here */
    if (!gfits_fread_ftable_range (subcat[0].f, FALSE, &ftable, start, Nrows)) {
      if (VERBOSE) fprintf (stderr, "can't read table missing data");
      return (FALSE);
    }

    /* no conversions currently defined : this just does the byte swap */
    catalog[0].missing = gfits_table_get_Missing (&ftable, &Nmissing, NULL);
    if (!catalog[0].missing) {
      fprintf (stderr, "ERROR: failed to read missing\n");
      exit (2);
    }
    if (Nmissing != Nrows) {
      fprintf (stderr, "Warning: mismatch between Nmissing in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmissing,  Nrows);
    }
    gfits_free_header (&header);
    catalog[0].Nmissing = Nmissing;
    catalog[0].Nmiss_off = start;
  }

  // XXX check the open status of the catalog
  if (catalog[0].catflags & LOAD_LENSING) {

    Catalog *subcat = catalog[0].lensing_catalog;

    /* move pointer past header -- must be already read (load_catalog) */
    Nbytes = subcat[0].header.datasize + gfits_data_size (&subcat[0].header);
    fseeko (subcat[0].f, Nbytes, SEEK_SET);

    /* read Lensing table header */
    if (!gfits_fread_header (subcat[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table lensing header");
      return (FALSE);
    }
    /* read Lensing table data : format is irrelevant here */
    if (!gfits_fread_ftable_range (subcat[0].f, FALSE, &ftable, start, Nrows)) {
      if (VERBOSE) fprintf (stderr, "can't read table lensing data");
      return (FALSE);
    }

    /* convert data format to internal : returns number of row read in Nlensing */
    catalog[0].lensing = FtableToLensing (&ftable, &Nlensing, &catalog[0].catformat);
    if (Nlensing != Nrows) {
      // XXX this condition denotes the eof has been reached; not an error or a warning
      // fprintf (stderr, "Warning: mismatch between Nlensing in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nlensing,  Nrows);
    }
    gfits_free_header (&header);
    catalog[0].Nlensing = Nlensing;
    catalog[0].Nlensing_off = start;
  }

  // XXX check the open status of the catalog
  if (catalog[0].catflags & LOAD_LENSOBJ) {

    Catalog *subcat = catalog[0].lensobj_catalog;

    /* move pointer past header -- must be already read (load_catalog) */
    Nbytes = subcat[0].header.datasize + gfits_data_size (&subcat[0].header);
    fseeko (subcat[0].f, Nbytes, SEEK_SET);

    /* read Lensobj table header */
    if (!gfits_fread_header (subcat[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table lensobj header");
      return (FALSE);
    }
    /* read Lensobj table data : format is irrelevant here */
    if (!gfits_fread_ftable_range (subcat[0].f, FALSE, &ftable, start, Nrows)) {
      if (VERBOSE) fprintf (stderr, "can't read table lensobj data");
      return (FALSE);
    }

    /* convert data format to internal : returns number of row read in Nlensobj */
    catalog[0].lensobj = FtableToLensobj (&ftable, &Nlensobj, &catalog[0].catformat);
    if (Nlensobj != Nrows) {
      // XXX this condition denotes the eof has been reached; not an error or a warning
      // fprintf (stderr, "Warning: mismatch between Nlensobj in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nlensobj,  Nrows);
    }
    gfits_free_header (&header);
    catalog[0].Nlensobj = Nlensobj;
    catalog[0].Nlensobj_off = start;
  }

  // XXX check the open status of the catalog
  if (catalog[0].catflags & LOAD_STARPAR) {

    Catalog *subcat = catalog[0].starpar_catalog;

    /* move pointer past header -- must be already read (load_catalog) */
    Nbytes = subcat[0].header.datasize + gfits_data_size (&subcat[0].header);
    fseeko (subcat[0].f, Nbytes, SEEK_SET);

    /* read StarPar table header */
    if (!gfits_fread_header (subcat[0].f, &header)) {
      if (VERBOSE) fprintf (stderr, "can't read table starpar header");
      return (FALSE);
    }
    /* read StarPar table data : format is irrelevant here */
    if (!gfits_fread_ftable_range (subcat[0].f, FALSE, &ftable, start, Nrows)) {
      if (VERBOSE) fprintf (stderr, "can't read table starpar data");
      return (FALSE);
    }

    /* convert data format to internal : returns number of row read in Nstarpar */
    catalog[0].starpar = FtableToStarPar (&ftable, &Nstarpar, &catalog[0].catformat);
    if (Nstarpar != Nrows) {
      // XXX this condition denotes the eof has been reached; not an error or a warning
      // fprintf (stderr, "Warning: mismatch between Nstarpar in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nstarpar,  Nrows);
    }
    gfits_free_header (&header);
    catalog[0].Nstarpar = Nstarpar;
    catalog[0].Nstarpar_off = start;
  }

  return (TRUE);
}

/* save_catalog_split writes all data currently in memory to disk */
int dvo_catalog_save_split (Catalog *catalog, char VERBOSE) {

  off_t Nitems;
  Header header;
  FTable ftable;
  SecFilt *primary, *secfilt;
  int Nsecfilt;
  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new, Nlensing_disk_new, Nlensobj_disk_new, Nstarpar_disk_new;
  off_t first, start, Nrows;

  ftable.header = &header;
  ftable.buffer = NULL;
  header.buffer = NULL;
  primary = NULL;
  
  // skip empty catalogs: it is illegal to have Measures without corresponding Averages
  Naves_disk_new = MAX (catalog[0].Naves_disk, catalog[0].Naverage + catalog[0].Naves_off);
  if (Naves_disk_new == 0) {
    if (VERBOSE) fprintf (stderr, "no stars in catalog, skipping\n");
    return (TRUE);
  }

  // for the appropriate types, pull out the first secfilt and pass to AverageToFtable as primary
  switch (catalog[0].catformat) {
    case DVO_FORMAT_ELIXIR: // special case for ELIXIR
    case DVO_FORMAT_LONEOS: // special case for LONEOS
      dvo_catalog_secfilt_to_primary (catalog, &primary, &secfilt, &Nsecfilt);
      break;
    default:
      primary = NULL;
      secfilt = catalog[0].secfilt;
      Nsecfilt = catalog[0].Nsecfilt;
      break;
  }

  Nmeas_disk_new = MAX (catalog[0].Nmeas_disk, catalog[0].Nmeasure + catalog[0].Nmeas_off);
  Nmiss_disk_new = MAX (catalog[0].Nmiss_disk, catalog[0].Nmissing + catalog[0].Nmiss_off);
  Nsecf_disk_new = MAX (catalog[0].Nsecf_disk, catalog[0].Naverage*Nsecfilt + catalog[0].Nsecf_off);
  Nlensing_disk_new = MAX (catalog[0].Nlensing_disk, catalog[0].Nlensing + catalog[0].Nlensing_off);
  Nlensobj_disk_new = MAX (catalog[0].Nlensobj_disk, catalog[0].Nlensobj + catalog[0].Nlensobj_off);
  Nstarpar_disk_new = MAX (catalog[0].Nstarpar_disk, catalog[0].Nstarpar + catalog[0].Nstarpar_off);

  /* make sure header is consistent with data */
  gfits_modify (&catalog[0].header, "NSTARS",   OFF_T_FMT, 1,  Naves_disk_new);
  gfits_modify (&catalog[0].header, "NMEAS",    OFF_T_FMT, 1,  Nmeas_disk_new);
  gfits_modify (&catalog[0].header, "NMISS",    OFF_T_FMT, 1,  Nmiss_disk_new);
  gfits_modify (&catalog[0].header, "NSECFILT", "%d",   1, Nsecfilt);
  gfits_modify (&catalog[0].header, "NLENSING", OFF_T_FMT, 1,  Nlensing_disk_new);
  gfits_modify (&catalog[0].header, "NLENSOBJ", OFF_T_FMT, 1,  Nlensobj_disk_new);
  gfits_modify (&catalog[0].header, "NSTARPAR", OFF_T_FMT, 1,  Nstarpar_disk_new);
  gfits_modify_alt (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
  gfits_modify (&catalog[0].header, "OBJID",    "%d", 1, catalog[0].objID);

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

  /*** Average Table ***/
  if ((catalog[0].catflags & LOAD_AVES) && (catalog[0].average != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Naves_off; // first disk row to write
    Nrows  = catalog[0].Naverage - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Naverage);
    assert (catalog[0].Naves_disk >= catalog[0].Naves_off);

    /* convert internal to external format */
    if (!AverageToFtable (&ftable, &catalog[0].average[first], Nrows, catalog[0].catformat, primary)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    if (!dvo_catalog_save_subcat (catalog, &ftable, start, Nrows, catalog[0].Naves_disk, Naves_disk_new)) {
      fprintf (stderr, "failure writing Average table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  } else {
    // even if we do not save the average table, we need to keep the header in sync
    /* rewind file pointers and truncate (file is still open) */
    fseeko (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");
      goto failure;
    }
  }

  /*** Measure Table ***/
  if ((catalog[0].catflags & LOAD_MEAS) && (catalog[0].measure != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nmeas_off; // first disk row to write
    Nrows  = catalog[0].Nmeasure - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nmeasure);
    assert (catalog[0].Nmeas_disk >= catalog[0].Nmeas_off);

    // convert to external table format (note that the block above does not damage or free catalog.average) 
    if (!MeasureToFtable (&ftable, catalog[0].average, &catalog[0].measure[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Measure table
    if (!dvo_catalog_save_subcat (catalog[0].measure_catalog, &ftable, start, Nrows, catalog[0].Nmeas_disk, Nmeas_disk_new)) {
      fprintf (stderr, "trouble writing Measure table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Missing Table ***/
  if ((catalog[0].catflags & LOAD_MISS) && (catalog[0].missing != NULL)) {

    if (catalog[0].Nmiss_off != 0) {
      fprintf (stderr, "inconsistency: Missing table cannot be written in segments\n");
      goto failure;
    }

    // convert to external table format
    if (!gfits_table_set_Missing (&ftable, catalog[0].missing, catalog[0].Nmissing)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Missing table (must write out entire table)
    if (!dvo_catalog_save_subcat (catalog[0].missing_catalog, &ftable, 0, catalog[0].Nmissing, catalog[0].Nmissing, catalog[0].Nmissing)) {
      fprintf (stderr, "trouble writing Missing Table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Secfilt Table ***/
  if ((catalog[0].catflags & LOAD_SECF) && (catalog[0].secfilt != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nsecf_off; // first disk row to write
    Nitems = catalog[0].Nsecf_mem;
    Nrows  = Nitems - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= Nitems);
    assert (catalog[0].Nsecf_disk >= catalog[0].Nsecf_off);
    // XXX check these for consistency...

    // convert to external table format
    SecFiltToFtable (&ftable, &secfilt[first], Nrows, catalog[0].catformat);

    // write out SecFilt table
    if (!dvo_catalog_save_subcat (catalog[0].secfilt_catalog, &ftable, start, Nrows, catalog[0].Nsecf_disk, Nsecf_disk_new)) {
      fprintf (stderr, "failure writing SecFilt table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Lensing Table (optional, do not save if not loaded) ***/
  if ((catalog[0].catflags & LOAD_LENSING) && catalog[0].lensing_catalog->f && catalog[0].lensing) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nlensing_off; // first disk row to write
    Nrows  = catalog[0].Nlensing - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nlensing);
    assert (catalog[0].Nlensing_disk >= catalog[0].Nlensing_off);

    if (catalog[0].Nlensing) {
      // convert to external table format (note that the block above does not damage or free catalog.average) 
      if (!LensingToFtable (&ftable, &catalog[0].lensing[first], Nrows, catalog[0].catformat)) {
	fprintf (stderr, "trouble converting format\n");
	goto failure;
      }

      // write out Lensing table
      if (!dvo_catalog_save_subcat (catalog[0].lensing_catalog, &ftable, start, Nrows, catalog[0].Nlensing_disk, Nlensing_disk_new)) {
	fprintf (stderr, "trouble writing Lensing table\n");
	goto failure;
      }
      gfits_free_header (&header);
      gfits_free_table (&ftable);
    }
  }

  /*** Lensobj Table (optional, do not save if not loaded) ***/
  if ((catalog[0].catflags & LOAD_LENSOBJ) && catalog[0].lensobj_catalog->f && catalog[0].lensobj) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nlensobj_off; // first disk row to write
    Nrows  = catalog[0].Nlensobj - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nlensobj);
    assert (catalog[0].Nlensobj_disk >= catalog[0].Nlensobj_off);

    if (catalog[0].Nlensobj) {
      // convert to external table format (note that the block above does not damage or free catalog.average) 
      if (!LensobjToFtable (&ftable, &catalog[0].lensobj[first], Nrows, catalog[0].catformat)) {
	fprintf (stderr, "trouble converting format\n");
	goto failure;
      }

      // write out Lensobj table
      if (!dvo_catalog_save_subcat (catalog[0].lensobj_catalog, &ftable, start, Nrows, catalog[0].Nlensobj_disk, Nlensobj_disk_new)) {
	fprintf (stderr, "trouble writing Lensobj table\n");
	goto failure;
      }
      gfits_free_header (&header);
      gfits_free_table (&ftable);
    }
  }

  /*** StarPar Table (optional, do not save if not loaded) ***/
    if ((catalog[0].catflags & LOAD_STARPAR) && catalog[0].starpar_catalog->f && catalog[0].starpar) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nstarpar_off; // first disk row to write
    Nrows  = catalog[0].Nstarpar - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nstarpar);
    assert (catalog[0].Nstarpar_disk >= catalog[0].Nstarpar_off);

    if (catalog[0].Nstarpar) {
      // convert to external table format (note that the block above does not damage or free catalog.average) 
      if (!StarParToFtable (&ftable, &catalog[0].starpar[first], Nrows, catalog[0].catformat)) {
	fprintf (stderr, "trouble converting format\n");
	goto failure;
      }

      // write out StarPar table
      if (!dvo_catalog_save_subcat (catalog[0].starpar_catalog, &ftable, start, Nrows, catalog[0].Nstarpar_disk, Nstarpar_disk_new)) {
	fprintf (stderr, "trouble writing StarPar table\n");
	goto failure;
      }
      gfits_free_header (&header);
      gfits_free_table (&ftable);
    }
  }

  /* free temp storage */
  if (primary != NULL) {
    free (primary);
    free (secfilt);
  }
  return (TRUE);

failure:
  /* free temp storage */
  gfits_free_header (&header);
  gfits_free_table (&ftable);
  if (primary != NULL) {
    free (primary);
    free (secfilt);
  }
  return (FALSE);
}

/* save_catalog_split writes all data currently in memory to disk, truncating if the disk file was larger */
int dvo_catalog_save_split_complete (Catalog *catalog, char VERBOSE) {

  off_t Nitems;
  Header header;
  FTable ftable;
  SecFilt *primary, *secfilt;
  int Nsecfilt;
  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new, Nlensing_disk_new, Nlensobj_disk_new, Nstarpar_disk_new;
  off_t first, start, Nrows;

  ftable.header = &header;
  ftable.buffer = NULL;
  header.buffer = NULL;
  primary = NULL;
  
  // skip empty catalogs: it is illegal to have Measures without corresponding Averages
  if (catalog[0].Naves_off > 0) {
    fprintf (stderr, "ERROR: only partial catalog (Average) was loaded\n");
    goto failure;
  }
  if (catalog[0].Nmeas_off > 0) {
    fprintf (stderr, "ERROR: only partial catalog (Measure) was loaded\n");
    goto failure;
  }
  if (catalog[0].Nmiss_off > 0) {
    fprintf (stderr, "ERROR: only partial catalog (Missing) was loaded\n");
    goto failure;
  }
  if (catalog[0].Nsecf_off > 0) {
    fprintf (stderr, "ERROR: only partial catalog (Secfilt) was loaded\n");
    goto failure;
  }
  if (catalog[0].Nlensing_off > 0) {
    fprintf (stderr, "ERROR: only partial catalog (Lensing) was loaded\n");
    goto failure;
  }
  if (catalog[0].Nlensobj_off > 0) {
    fprintf (stderr, "ERROR: only partial catalog (Lensobj) was loaded\n");
    goto failure;
  }
  if (catalog[0].Nstarpar_off > 0) {
    fprintf (stderr, "ERROR: only partial catalog (StarPar) was loaded\n");
    goto failure;
  }

  Naves_disk_new = catalog[0].Naverage;
  if (Naves_disk_new == 0) {
    if (VERBOSE) fprintf (stderr, "resulting catalog is empty; delete it\n");
    // unlink ();
    return (TRUE);
  }

  // for the appropriate types, pull out the first secfilt and pass to AverageToFtable as primary
  switch (catalog[0].catformat) {
    case DVO_FORMAT_ELIXIR: // special case for ELIXIR
    case DVO_FORMAT_LONEOS: // special case for LONEOS
      dvo_catalog_secfilt_to_primary (catalog, &primary, &secfilt, &Nsecfilt);
      break;
    default:
      primary = NULL;
      secfilt = catalog[0].secfilt;
      Nsecfilt = catalog[0].Nsecfilt;
      break;
  }

  Nmeas_disk_new = catalog[0].Nmeasure;
  Nmiss_disk_new = catalog[0].Nmissing;
  Nsecf_disk_new = catalog[0].Naverage*Nsecfilt;
  Nlensing_disk_new = catalog[0].Nlensing;
  Nlensobj_disk_new = catalog[0].Nlensobj;
  Nstarpar_disk_new = catalog[0].Nstarpar;

  /* make sure header is consistent with data */
  gfits_modify (&catalog[0].header, "NSTARS",   OFF_T_FMT, 1,  Naves_disk_new);
  gfits_modify (&catalog[0].header, "NMEAS",    OFF_T_FMT, 1,  Nmeas_disk_new);
  gfits_modify (&catalog[0].header, "NMISS",    OFF_T_FMT, 1,  Nmiss_disk_new);
  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, Nsecfilt);
  gfits_modify (&catalog[0].header, "NLENSING", OFF_T_FMT, 1,  Nlensing_disk_new);
  gfits_modify (&catalog[0].header, "NLENSOBJ", OFF_T_FMT, 1,  Nlensobj_disk_new);
  gfits_modify (&catalog[0].header, "NSTARPAR", OFF_T_FMT, 1,  Nstarpar_disk_new);
  gfits_modify_alt (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
  gfits_modify (&catalog[0].header, "OBJID",    "%d", 1, catalog[0].objID);

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

  /*** Average Table ***/
  if ((catalog[0].catflags & LOAD_AVES) && (catalog[0].average != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Naves_off; // first disk row to write
    Nrows  = catalog[0].Naverage - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Naverage);
    assert (catalog[0].Naves_disk >= catalog[0].Naves_off);

    /* convert internal to external format */
    if (!AverageToFtable (&ftable, &catalog[0].average[first], Nrows, catalog[0].catformat, primary)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    if (!dvo_catalog_save_subcat (catalog, &ftable, start, Nrows, catalog[0].Naves_disk, Naves_disk_new)) {
      fprintf (stderr, "failure writing Average table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  } else {
    // even if we do not save the average table, we need to keep the header in sync
    /* rewind file pointers and truncate (file is still open) */
    fseeko (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");
      goto failure;
    }
  }

  /*** Measure Table ***/
  if ((catalog[0].catflags & LOAD_MEAS) && (catalog[0].measure != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nmeas_off; // first disk row to write
    Nrows  = catalog[0].Nmeasure - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nmeasure);
    assert (catalog[0].Nmeas_disk >= catalog[0].Nmeas_off);

    // convert to external table format
    if (!MeasureToFtable (&ftable, catalog[0].average, &catalog[0].measure[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Measure table
    if (!dvo_catalog_save_subcat (catalog[0].measure_catalog, &ftable, start, Nrows, catalog[0].Nmeas_disk, Nmeas_disk_new)) {
      fprintf (stderr, "trouble writing Measure table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Missing Table ***/
  if ((catalog[0].catflags & LOAD_MISS) && (catalog[0].missing != NULL)) {

    if (catalog[0].Nmiss_off != 0) {
      fprintf (stderr, "inconsistency: Missing table cannot be written in segments\n");
      goto failure;
    }

    // convert to external table format
    if (!gfits_table_set_Missing (&ftable, catalog[0].missing, catalog[0].Nmissing)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Missing table (must write out entire table)
    if (!dvo_catalog_save_subcat (catalog[0].missing_catalog, &ftable, 0, catalog[0].Nmissing, catalog[0].Nmissing, catalog[0].Nmissing)) {
      fprintf (stderr, "trouble writing Missing Table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Secfilt Table ***/
  if ((catalog[0].catflags & LOAD_SECF) && (catalog[0].secfilt != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nsecf_off; // first disk row to write
    Nitems = catalog[0].Naverage*Nsecfilt;
    Nrows  = Nitems - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= Nitems);
    assert (catalog[0].Nsecf_disk >= catalog[0].Nsecf_off);
    // XXX check these for consistency...

    // convert to external table format
    SecFiltToFtable (&ftable, &secfilt[first], Nrows, catalog[0].catformat);

    // write out SecFilt table
    if (!dvo_catalog_save_subcat (catalog[0].secfilt_catalog, &ftable, start, Nrows, catalog[0].Nsecf_disk, Nsecf_disk_new)) {
      fprintf (stderr, "failure writing SecFilt table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Lensing Table ***/
  if ((catalog[0].catflags & LOAD_LENSING) && (catalog[0].lensing != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nlensing_off; // first disk row to write
    Nrows  = catalog[0].Nlensing - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nlensing);
    assert (catalog[0].Nlensing_disk >= catalog[0].Nlensing_off);

    // convert to external table format
    if (!LensingToFtable (&ftable, &catalog[0].lensing[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Lensing table
    if (!dvo_catalog_save_subcat (catalog[0].lensing_catalog, &ftable, start, Nrows, catalog[0].Nlensing_disk, Nlensing_disk_new)) {
      fprintf (stderr, "trouble writing Lensing table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Lensobj Table ***/
  if ((catalog[0].catflags & LOAD_LENSOBJ) && (catalog[0].lensobj != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nlensobj_off; // first disk row to write
    Nrows  = catalog[0].Nlensobj - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nlensobj);
    assert (catalog[0].Nlensobj_disk >= catalog[0].Nlensobj_off);

    // convert to external table format
    if (!LensobjToFtable (&ftable, &catalog[0].lensobj[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Lensobj table
    if (!dvo_catalog_save_subcat (catalog[0].lensobj_catalog, &ftable, start, Nrows, catalog[0].Nlensobj_disk, Nlensobj_disk_new)) {
      fprintf (stderr, "trouble writing Lensobj table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** StarPar Table ***/
  if ((catalog[0].catflags & LOAD_STARPAR) && (catalog[0].starpar != NULL)) {

    first  = 0;                    // first row in memory to write
    start  = catalog[0].Nstarpar_off; // first disk row to write
    Nrows  = catalog[0].Nstarpar - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nstarpar);
    assert (catalog[0].Nstarpar_disk >= catalog[0].Nstarpar_off);

    // convert to external table format
    if (!StarParToFtable (&ftable, &catalog[0].starpar[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out StarPar table
    if (!dvo_catalog_save_subcat (catalog[0].starpar_catalog, &ftable, start, Nrows, catalog[0].Nstarpar_disk, Nstarpar_disk_new)) {
      fprintf (stderr, "trouble writing StarPar table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /* free temp storage */
  if (primary != NULL) {
    free (primary);
    free (secfilt);
  }
  return (TRUE);

failure:
  /* free temp storage */
  gfits_free_header (&header);
  gfits_free_table (&ftable);
  if (primary != NULL) {
    free (primary);
    free (secfilt);
  }
  return (FALSE);
}

/* update_catalog_split only writes new lines to file. */
int dvo_catalog_update_split (Catalog *catalog, char VERBOSE) {

  Header header;
  FTable ftable;
  SecFilt *primary, *secfilt;
  int Nsecfilt;
  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new, Nlensing_disk_new, Nlensobj_disk_new, Nstarpar_disk_new;
  off_t first, start, Nrows;

  ftable.header = &header;
  ftable.buffer = NULL;
  header.buffer = NULL;

  // skip empty catalogs: it is illegal to have Measures without corresponding Averages
  Naves_disk_new = MAX (catalog[0].Naves_disk, catalog[0].Naverage + catalog[0].Naves_off);
  if (Naves_disk_new == 0) {
    if (VERBOSE) fprintf (stderr, "no stars in catalog, skipping\n");
    return (TRUE);
  }

  // for the appropriate types, pull out the first secfilt and pass to AverageToFtable as primary 
  switch (catalog[0].catformat) {
    case DVO_FORMAT_ELIXIR: // special case for ELIXIR
    case DVO_FORMAT_LONEOS: // special case for LONEOS
      dvo_catalog_secfilt_to_primary (catalog, &primary, &secfilt, &Nsecfilt);
      break;
    default:
      primary = NULL;
      secfilt = catalog[0].secfilt;
      Nsecfilt = catalog[0].Nsecfilt;
      break;
  }

  Nmeas_disk_new = MAX (catalog[0].Nmeas_disk, catalog[0].Nmeasure + catalog[0].Nmeas_off);
  Nmiss_disk_new = MAX (catalog[0].Nmiss_disk, catalog[0].Nmissing + catalog[0].Nmiss_off);
  Nsecf_disk_new = MAX (catalog[0].Nsecf_disk, catalog[0].Naverage*Nsecfilt + catalog[0].Nsecf_off);
  Nlensing_disk_new = MAX (catalog[0].Nlensing_disk, catalog[0].Nlensing + catalog[0].Nlensing_off);
  Nlensobj_disk_new = MAX (catalog[0].Nlensobj_disk, catalog[0].Nlensobj + catalog[0].Nlensobj_off);
  Nstarpar_disk_new = MAX (catalog[0].Nstarpar_disk, catalog[0].Nstarpar + catalog[0].Nstarpar_off);

  /* make sure header is consistent with data */
  gfits_modify (&catalog[0].header, "NSTARS",   OFF_T_FMT, 1,  Naves_disk_new);
  gfits_modify (&catalog[0].header, "NMEAS",    OFF_T_FMT, 1,  Nmeas_disk_new);
  gfits_modify (&catalog[0].header, "NMISS",    OFF_T_FMT, 1,  Nmiss_disk_new);
  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, Nsecfilt);
  gfits_modify (&catalog[0].header, "NLENSING", OFF_T_FMT, 1,  Nlensing_disk_new);
  gfits_modify (&catalog[0].header, "NLENSOBJ", OFF_T_FMT, 1,  Nlensobj_disk_new);
  gfits_modify (&catalog[0].header, "NSTARPAR", OFF_T_FMT, 1,  Nstarpar_disk_new);
  gfits_modify_alt (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
  gfits_modify (&catalog[0].header, "OBJID",    "%d", 1, catalog[0].objID);

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

  /*** Average Table ***/
  if (catalog[0].average != NULL) {

    first  = catalog[0].Naves_disk - catalog[0].Naves_off; // first row to write (memory)
    start  = catalog[0].Naves_disk;                        // first row to write (disk)
    Nrows  = catalog[0].Naverage - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Naverage);
    assert (catalog[0].Naves_disk >= catalog[0].Naves_off);

    /* convert internal to external format */
    if (!AverageToFtable (&ftable, &catalog[0].average[first], Nrows, catalog[0].catformat, primary)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    if (!dvo_catalog_save_subcat (catalog, &ftable, start, Nrows, catalog[0].Naves_disk, Naves_disk_new)) {
      fprintf (stderr, "failure writing Average table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  } else {
    // even if we do not save the average table, we need to keep the header in sync
    /* rewind file pointers and truncate (file is still open) */
    fseeko (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");
      goto failure;
    }
  }

  /*** Measure Table ***/
  if (catalog[0].measure != NULL) {

    first  = catalog[0].Nmeas_disk - catalog[0].Nmeas_off;  // first row in memory to write
    start  = catalog[0].Nmeas_disk; // first disk row to write
    Nrows  = catalog[0].Nmeasure - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nmeasure);
    assert (catalog[0].Nmeas_disk >= catalog[0].Nmeas_off);

    // convert to external table format
    // XXX does catalog.measure have averef correctly set up?
    if (!MeasureToFtable (&ftable, catalog[0].average, &catalog[0].measure[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Measure table
    if (!dvo_catalog_save_subcat (catalog[0].measure_catalog, &ftable, start, Nrows, catalog[0].Nmeas_disk, Nmeas_disk_new)) {
      fprintf (stderr, "trouble writing Measure table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

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

  /*** Missing Table ***/

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

    if (catalog[0].Nmiss_off != 0) {
      fprintf (stderr, "inconsistency: Missing table cannot be written in segments\n");
      goto failure;
    }

    // convert to external table format
    if (!gfits_table_set_Missing (&ftable, catalog[0].missing, catalog[0].Nmissing)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Missing table (must write out entire table)
    if (!dvo_catalog_save_subcat (catalog[0].missing_catalog, &ftable, 0, catalog[0].Nmissing, catalog[0].Nmissing, catalog[0].Nmissing)) {
      fprintf (stderr, "trouble writing Missing Table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Secfilt Table ***/
  if (catalog[0].secfilt != NULL) {

    first  = catalog[0].Nsecf_disk - catalog[0].Nsecf_off;  // first row in memory to write
    start  = catalog[0].Nsecf_disk; // first disk row to write
    Nrows  = catalog[0].Nsecf_mem - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (catalog[0].Nsecf_disk >= catalog[0].Nsecf_off);

    // convert to external table format
    SecFiltToFtable (&ftable, &secfilt[first], Nrows, catalog[0].catformat);

    // write out SecFilt table
    if (!dvo_catalog_save_subcat (catalog[0].secfilt_catalog, &ftable, start, Nrows, catalog[0].Nsecf_disk, Nsecf_disk_new)) {
      fprintf (stderr, "failure writing SecFilt table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Lensing Table ***/
  if (catalog[0].lensing != NULL) {

    first  = catalog[0].Nlensing_disk - catalog[0].Nlensing_off;  // first row in memory to write
    start  = catalog[0].Nlensing_disk; // first disk row to write
    Nrows  = catalog[0].Nlensing - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nlensing);
    assert (catalog[0].Nlensing_disk >= catalog[0].Nlensing_off);

    // convert to external table format
    // XXX does catalog.lensing have averef correctly set up?
    if (!LensingToFtable (&ftable, &catalog[0].lensing[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Lensing table
    if (!dvo_catalog_save_subcat (catalog[0].lensing_catalog, &ftable, start, Nrows, catalog[0].Nlensing_disk, Nlensing_disk_new)) {
      fprintf (stderr, "trouble writing Lensing table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** Lensobj Table ***/
  if (catalog[0].lensobj != NULL) {

    first  = catalog[0].Nlensobj_disk - catalog[0].Nlensobj_off;  // first row in memory to write
    start  = catalog[0].Nlensobj_disk; // first disk row to write
    Nrows  = catalog[0].Nlensobj - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nlensobj);
    assert (catalog[0].Nlensobj_disk >= catalog[0].Nlensobj_off);

    // convert to external table format
    // XXX does catalog.lensobj have averef correctly set up?
    if (!LensobjToFtable (&ftable, &catalog[0].lensobj[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out Lensobj table
    if (!dvo_catalog_save_subcat (catalog[0].lensobj_catalog, &ftable, start, Nrows, catalog[0].Nlensobj_disk, Nlensobj_disk_new)) {
      fprintf (stderr, "trouble writing Lensobj table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /*** StarPar Table ***/
  if (catalog[0].starpar != NULL) {

    first  = catalog[0].Nstarpar_disk - catalog[0].Nstarpar_off;  // first row in memory to write
    start  = catalog[0].Nstarpar_disk; // first disk row to write
    Nrows  = catalog[0].Nstarpar - first;

    assert (Nrows >= 0);
    assert (first >= 0);
    assert (first <= catalog[0].Nstarpar);
    assert (catalog[0].Nstarpar_disk >= catalog[0].Nstarpar_off);

    // convert to external table format
    // XXX does catalog.starpar have averef correctly set up?
    if (!StarParToFtable (&ftable, &catalog[0].starpar[first], Nrows, catalog[0].catformat)) {
      fprintf (stderr, "trouble converting format\n");
      goto failure;
    }

    // write out StarPar table
    if (!dvo_catalog_save_subcat (catalog[0].starpar_catalog, &ftable, start, Nrows, catalog[0].Nstarpar_disk, Nstarpar_disk_new)) {
      fprintf (stderr, "trouble writing StarPar table\n");
      goto failure;
    }
    gfits_free_header (&header);
    gfits_free_table (&ftable);
  }

  /* free temp storage */
  if (primary != NULL) {
    free (primary);
    free (secfilt);
  }

  return (TRUE);

failure:
  /* free temp storage */
  if (primary != NULL) {
    free (primary);
    free (secfilt);
  }
  return (FALSE);
}

/* 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 ftable.  we need to define
   the vtable based on the ftable, but with Ny = Nmeasure */  
  


    // * convert to an ftable
    // * optionally write the PHU header/matrix
    // * advance to the start of the output data block:
    // ** Nx * catalog[0].Nmeas_off
    // * write out the ftable data block
    // * if Nmeas_off + Nmeasure >= Nmeas_disk, update padding
    // ** start = Nmeas_off
    // ** Nrows = Nmeasure

