Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h	(revision 36917)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h	(revision 36918)
@@ -79,15 +79,17 @@
 
 /* catalog values to be loaded */
-# define LOAD_NONE 	0x00
-# define LOAD_AVES 	0x01
-# define LOAD_MEAS 	0x02
-# define LOAD_MISS 	0x04
-# define LOAD_SECF 	0x08 
-# define SKIP_AVES 	0x10
-# define SKIP_MEAS 	0x20
-# define SKIP_MISS 	0x40
-# define SKIP_SECF 	0x80
-
-// # define LOAD_MEAS_META 0x100 -- is this used??
+# define LOAD_NONE 	0x000
+# define LOAD_AVES 	0x001
+# define LOAD_MEAS 	0x002
+# define LOAD_MISS 	0x004
+# define LOAD_SECF 	0x008 
+# define SKIP_AVES 	0x010
+# define SKIP_MEAS 	0x020
+# define SKIP_MISS 	0x040
+# define SKIP_SECF 	0x080
+# define LOAD_LENSING 	0x100
+# define LOAD_LENSOBJ 	0x200
+# define SKIP_LENSING 	0x400
+# define SKIP_LENSOBJ 	0x800
 
 /* photometry code types */
@@ -552,4 +554,7 @@
 Measure *Measure_PS1_V4alt_ToInternal (Average *ave, Measure_PS1_V4alt *in, off_t Nvalues);
 
+/* for some reason I have merged the set of tables and the file description,
+   so I need to have an internal structure to point to the separate files */
+
 /* a catalog contains this data */
 typedef struct Catalog {
@@ -564,8 +569,12 @@
   SecFilt *secfilt;
 
+  // lensing data (optional?)
+  Lensing *lensing;
+  Lensobj *lensobj;
+
   int Nsecfilt;  /* number of secfilt entries for each average entry */
-  off_t Naverage,   Nmeasure,   Nmissing,   Nsecf_mem;  /* current number of each component in memory */
-  off_t Naves_disk, Nmeas_disk, Nmiss_disk, Nsecf_disk; /* current number of each component on disk */
-  off_t Naves_off,  Nmeas_off,  Nmiss_off,  Nsecf_off;  /* index of first loaded data value */
+  off_t Naverage,   Nmeasure,   Nmissing,   Nlensing,      Nlensobj,      Nsecf_mem;  /* current number of each component in memory */
+  off_t Naves_disk, Nmeas_disk, Nmiss_disk, Nlensing_disk, Nlensobj_disk, Nsecf_disk; /* current number of each component on disk */
+  off_t Naves_off,  Nmeas_off,  Nmiss_off,  Nlensing_off,  Nlensobj_off,  Nsecf_off;  /* index of first loaded data value */
 
   // note that we use these for the full-sky relphot analysis
@@ -586,4 +595,6 @@
   struct Catalog *missing_catalog;		/* missing catalog data (split) */
   struct Catalog *secfilt_catalog;		/* secfilt catalog data (split) */
+  struct Catalog *lensing_catalog;		/* lensing catalog data (split) */
+  struct Catalog *lensobj_catalog;		/* lensobj catalog data (split) */
 
   unsigned int objID;
@@ -790,4 +801,6 @@
 Measure *FtableToMeasure (FTable *ftable, Average *average, off_t *Nmeasure, char *format);
 SecFilt *FtableToSecFilt (FTable *ftable, off_t *Nsecfilt, char *format);
+Lensing *FtableToLensing (FTable *ftable, off_t *Nlensing, char *format);
+Lensobj *FtableToLensobj (FTable *ftable, off_t *Nlensobj, char *format);
 int      FtableToImage   (FTable *ftable, Header *theader, char *format);
 
@@ -795,4 +808,6 @@
 int MeasureToFtable (FTable *ftable, Average *average, Measure *measure, off_t Nmeasure, char format);
 int SecFiltToFtable (FTable *ftable, SecFilt *secfilt, off_t Nsecfilt, char format);
+int LensingToFtable (FTable *ftable, Lensing *lensing, off_t Nlensing, char format);
+int LensobjToFtable (FTable *ftable, Lensobj *lensobj, off_t Nlensobj, char format);
 int ImageToFtable (FTable *ftable, Header *theader, char format);
 int ImageToVtable (VTable *vtable, Header *theader, char format);
@@ -915,4 +930,7 @@
 void dvo_measureT_init (MeasureTiny *measure);
 
+void dvo_lensing_init (Lensing *lensing);
+void dvo_lensobj_init (Lensobj *lensobj);
+
 void InitRegionHosts (RegionHostInfo *hosts, int Nhosts, int NHOSTS);
 void FreeRegionHosts (RegionHostInfo *hosts, int Nhosts);
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/ps1_v5_defs.h
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/ps1_v5_defs.h	(revision 36917)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/ps1_v5_defs.h	(revision 36918)
@@ -8,4 +8,9 @@
 SecFilt_PS1_V5          *SecFiltInternalTo_PS1_V5 (SecFilt *in, off_t Nvalues);
 
+Lensing 	       	*Lensing_PS1_V5_ToInternal (Lensing_PS1_V5 *in, off_t Nvalues);
+Lensing_PS1_V5          *LensingInternalTo_PS1_V5 (Lensing *in, off_t Nvalues);
+Lensobj 	       	*Lensobj_PS1_V5_ToInternal (Lensobj_PS1_V5 *in, off_t Nvalues);
+Lensobj_PS1_V5          *LensobjInternalTo_PS1_V5 (Lensobj *in, off_t Nvalues);
+
 PhotCode                *PhotCode_PS1_V5_To_Internal (PhotCode_PS1_V5 *in, off_t Nvalues);
 PhotCode_PS1_V5         *PhotCode_Internal_To_PS1_V5 (PhotCode *in, off_t Nvalues);
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog.c	(revision 36917)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog.c	(revision 36918)
@@ -390,4 +390,93 @@
 }
 
+// init all data, or just catalog data
+void dvo_lensing_init (Lensing *lensing) {
+  lensing->X11_sm_obj = NAN;
+  lensing->X12_sm_obj = NAN;
+  lensing->X22_sm_obj = NAN;
+  lensing->E1_sm_obj  = NAN;
+  lensing->E2_sm_obj  = NAN;
+
+  lensing->X11_sh_obj = NAN;
+  lensing->X12_sh_obj = NAN;
+  lensing->X22_sh_obj = NAN;
+  lensing->E1_sh_obj  = NAN;
+  lensing->E2_sh_obj  = NAN;
+
+  lensing->X11_sm_psf = NAN;
+  lensing->X12_sm_psf = NAN;
+  lensing->X22_sm_psf = NAN;
+  lensing->E1_sm_psf  = NAN;
+  lensing->E2_sm_psf  = NAN;
+
+  lensing->X11_sh_psf = NAN;
+  lensing->X12_sh_psf = NAN;
+  lensing->X22_sh_psf = NAN;
+  lensing->E1_sh_psf  = NAN;
+  lensing->E2_sh_psf  = NAN;
+
+  lensing->F_ApR5     = NAN;
+  lensing->dF_ApR5    = NAN;
+  lensing->sF_ApR5    = NAN;
+  lensing->fF_ApR5    = NAN;
+
+  lensing->F_ApR6     = NAN;
+  lensing->dF_ApR6    = NAN;
+  lensing->sF_ApR6    = NAN;
+  lensing->fF_ApR6    = NAN;
+
+  lensing->detID = -1;
+  lensing->objID = -1;
+  lensing->catID = -1;
+
+  lensing->pad = 0;
+}
+
+// init all data, or just catalog data
+void dvo_lensobj_init (Lensobj *lensobj) {
+  lensobj->X11_sm_obj = NAN;
+  lensobj->X12_sm_obj = NAN;
+  lensobj->X22_sm_obj = NAN;
+  lensobj->E1_sm_obj  = NAN;
+  lensobj->E2_sm_obj  = NAN;
+
+  lensobj->X11_sh_obj = NAN;
+  lensobj->X12_sh_obj = NAN;
+  lensobj->X22_sh_obj = NAN;
+  lensobj->E1_sh_obj  = NAN;
+  lensobj->E2_sh_obj  = NAN;
+
+  lensobj->X11_sm_psf = NAN;
+  lensobj->X12_sm_psf = NAN;
+  lensobj->X22_sm_psf = NAN;
+  lensobj->E1_sm_psf  = NAN;
+  lensobj->E2_sm_psf  = NAN;
+
+  lensobj->X11_sh_psf = NAN;
+  lensobj->X12_sh_psf = NAN;
+  lensobj->X22_sh_psf = NAN;
+  lensobj->E1_sh_psf  = NAN;
+  lensobj->E2_sh_psf  = NAN;
+
+  lensobj->F_ApR5     = NAN;
+  lensobj->dF_ApR5    = NAN;
+  lensobj->sF_ApR5    = NAN;
+  lensobj->fF_ApR5    = NAN;
+
+  lensobj->F_ApR6     = NAN;
+  lensobj->dF_ApR6    = NAN;
+  lensobj->sF_ApR6    = NAN;
+  lensobj->fF_ApR6    = NAN;
+
+  lensobj->gamma = NAN;
+  lensobj->E1 = NAN;
+  lensobj->E2 = NAN;
+
+  lensobj->objID = -1;
+  lensobj->catID = -1;
+
+  lensobj->pad = 0;
+}
+
 /* possible exit status for lock_catalog: 
    DVO_CAT_OPEN_FAIL - failure (including lock failure)
@@ -434,7 +523,9 @@
 
   if (catalog[0].catmode == DVO_MODE_SPLIT) {
-    if (catalog[0].measure_catalog != NULL) dvo_catalog_unlock (catalog[0].measure_catalog);
-    if (catalog[0].missing_catalog != NULL) dvo_catalog_unlock (catalog[0].missing_catalog);
-    if (catalog[0].secfilt_catalog != NULL) dvo_catalog_unlock (catalog[0].secfilt_catalog);
+    if (catalog[0].measure_catalog) dvo_catalog_unlock (catalog[0].measure_catalog);
+    if (catalog[0].missing_catalog) dvo_catalog_unlock (catalog[0].missing_catalog);
+    if (catalog[0].secfilt_catalog) dvo_catalog_unlock (catalog[0].secfilt_catalog);
+    if (catalog[0].lensing_catalog) dvo_catalog_unlock (catalog[0].lensing_catalog);
+    if (catalog[0].lensobj_catalog) dvo_catalog_unlock (catalog[0].lensobj_catalog);
   }
   return (1);
@@ -702,4 +793,14 @@
       free (catalog[0].secfilt_catalog);
     }
+    if (catalog[0].lensing_catalog) {
+      free (catalog[0].lensing_catalog[0].filename);
+      dvo_catalog_free (catalog[0].lensing_catalog);
+      free (catalog[0].lensing_catalog);
+    }
+    if (catalog[0].lensobj_catalog) {
+      free (catalog[0].lensobj_catalog[0].filename);
+      dvo_catalog_free (catalog[0].lensobj_catalog);
+      free (catalog[0].lensobj_catalog);
+    }
   }
   dvo_catalog_free_data (catalog);
@@ -711,23 +812,33 @@
 
   /* free, initialize data structures */
-  if (catalog[0].average != NULL) {
+  if (catalog[0].average) {
     free (catalog[0].average); 
     catalog[0].Naverage = 0;
     catalog[0].average = NULL;
   }
-  if (catalog[0].measure != NULL) {
+  if (catalog[0].measure) {
     free (catalog[0].measure); 
     catalog[0].Nmeasure = 0;
     catalog[0].measure = NULL;
   }
-  if (catalog[0].missing != NULL) {
+  if (catalog[0].missing) {
     free (catalog[0].missing); 
     catalog[0].Nmissing = 0;
     catalog[0].missing = NULL;
   }
-  if (catalog[0].secfilt != NULL) {
+  if (catalog[0].secfilt) {
     free (catalog[0].secfilt); 
     catalog[0].Nsecf_mem = 0;
     catalog[0].secfilt = NULL;
+  }
+  if (catalog[0].lensing) {
+    free (catalog[0].lensing); 
+    catalog[0].Nlensing = 0;
+    catalog[0].lensing = NULL;
+  }
+  if (catalog[0].lensobj) {
+    free (catalog[0].lensobj); 
+    catalog[0].Nlensobj = 0;
+    catalog[0].lensobj = NULL;
   }
 }
@@ -819,4 +930,14 @@
       }
     }
+    if (catalog[0].lensing_catalog != NULL) {
+      if (!dvo_catalog_backup (catalog[0].lensing_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
+    if (catalog[0].lensobj_catalog != NULL) {
+      if (!dvo_catalog_backup (catalog[0].lensobj_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
   }
   return TRUE;
@@ -860,4 +981,14 @@
       }
     }
+    if (catalog[0].lensing_catalog != NULL) {
+      if (!dvo_catalog_unlink_backup (catalog[0].lensing_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
+    if (catalog[0].lensobj_catalog != NULL) {
+      if (!dvo_catalog_unlink_backup (catalog[0].lensobj_catalog, FALSE)) {
+	return FALSE;
+      }
+    }
   }
   return TRUE;
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_create.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_create.c	(revision 36917)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_create.c	(revision 36918)
@@ -70,4 +70,29 @@
     gfits_modify (&catalog[0].header, "SECFILT", "%s", 1, file);
     free (file);
+
+    /* define lensing catalog file */
+    ALLOCATE (catalog[0].lensing_catalog, Catalog, 1);
+    dvo_catalog_init (catalog[0].lensing_catalog, TRUE);
+
+    /* create basic data for lensing catalog file */
+    gfits_create_header (&catalog[0].lensing_catalog[0].header);
+    ALLOCATE (catalog[0].lensing_catalog[0].filename, char, length);
+    sprintf (catalog[0].lensing_catalog[0].filename, "%s/%s.cpx", path, root);
+    file = filebasename (catalog[0].lensing_catalog[0].filename);
+    gfits_modify (&catalog[0].header, "LENSING", "%s", 1, file);
+    free (file);
+
+    /* define lensobj catalog file */
+    ALLOCATE (catalog[0].lensobj_catalog, Catalog, 1);
+    dvo_catalog_init (catalog[0].lensobj_catalog, TRUE);
+
+    /* create basic data for lensobj catalog file */
+    gfits_create_header (&catalog[0].lensobj_catalog[0].header);
+    ALLOCATE (catalog[0].lensobj_catalog[0].filename, char, length);
+    sprintf (catalog[0].lensobj_catalog[0].filename, "%s/%s.cpy", path, root);
+    file = filebasename (catalog[0].lensobj_catalog[0].filename);
+    gfits_modify (&catalog[0].header, "LENSOBJ", "%s", 1, file);
+    free (file);
+
     free (path);
     free (root);
@@ -84,4 +109,12 @@
     }
     if (dvo_catalog_lock (catalog[0].secfilt_catalog, catalog[0].lockmode) != DVO_CAT_OPEN_EMPTY) {
+      fprintf (stderr, "error with file lock\n");
+      exit (2);
+    }
+    if (dvo_catalog_lock (catalog[0].lensing_catalog, catalog[0].lockmode) != DVO_CAT_OPEN_EMPTY) {
+      fprintf (stderr, "error with file lock\n");
+      exit (2);
+    }
+    if (dvo_catalog_lock (catalog[0].lensobj_catalog, catalog[0].lockmode) != DVO_CAT_OPEN_EMPTY) {
       fprintf (stderr, "error with file lock\n");
       exit (2);
@@ -111,4 +144,6 @@
   ALLOCATE (catalog[0].missing, Missing, 1);
   ALLOCATE (catalog[0].secfilt, SecFilt, 1);
+  ALLOCATE (catalog[0].lensing, Lensing, 1);
+  ALLOCATE (catalog[0].lensobj, Lensobj, 1);
 
   /* setup secondary filters to match photcodes:
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_mef.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_mef.c	(revision 36917)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_mef.c	(revision 36918)
@@ -7,4 +7,6 @@
   off_t Nmeasure;
   off_t Nmissing;
+  off_t Nlensing;
+  off_t Nlensobj;
   off_t Nitems, Nexpect;
   int Nsecfilt;
@@ -22,4 +24,6 @@
   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)) return (FALSE);
+  if (!gfits_scan (&catalog[0].header, "NLENSOBJ", OFF_T_FMT, 1,  &Nlensobj)) return (FALSE);
 
   /* the OBJID is a counter that uniquely defines an average entry and never changes.  if
@@ -40,4 +44,6 @@
   catalog[0].Nmiss_disk = Nmissing;
   catalog[0].Nsecf_disk = Naverage * Nsecfilt;
+  catalog[0].Nlensing_disk = Nlensing;
+  catalog[0].Nlensobj_disk = Nlensobj;
 
   /**  Nsecfilt is unusual: it does not list the number of data items in the table
@@ -50,4 +56,6 @@
   catalog[0].missing = NULL;
   catalog[0].secfilt = NULL;
+  catalog[0].lensing = NULL;
+  catalog[0].lensobj = NULL;
 
   /* validate table mode ?*/
@@ -201,4 +209,54 @@
   }
 
+  /* read Lensing table header */
+  if (!gfits_fread_header (catalog[0].f, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read table lensing header");
+    return (FALSE);
+  }
+  /* read Lensing table data */
+  if (catalog[0].catflags & LOAD_LENSING) {
+    if (!gfits_fread_ftable_data (catalog[0].f, &ftable, FALSE)) {
+      if (VERBOSE) fprintf (stderr, "can't read table lensing data");
+      return (FALSE);
+    }
+    catalog[0].lensing = FtableToLensing (&ftable, &catalog[0].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 {
+    Nbytes = gfits_data_size (&header);
+    fseeko (catalog[0].f, Nbytes, SEEK_CUR);
+    ALLOCATE (catalog[0].lensing, Lensing, 1);
+    catalog[0].Nlensing = 0;
+    catalog[0].Nlensing_off = catalog[0].Nlensing_disk;
+  }
+
+  /* read Lensobj table header */
+  if (!gfits_fread_header (catalog[0].f, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read table lensobj header");
+    return (FALSE);
+  }
+  /* read Lensobj table data */
+  if (catalog[0].catflags & LOAD_LENSOBJ) {
+    if (!gfits_fread_ftable_data (catalog[0].f, &ftable, FALSE)) {
+      if (VERBOSE) fprintf (stderr, "can't read table lensobj data");
+      return (FALSE);
+    }
+    catalog[0].lensobj = FtableToLensobj (&ftable, &catalog[0].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 {
+    Nbytes = gfits_data_size (&header);
+    fseeko (catalog[0].f, Nbytes, SEEK_CUR);
+    ALLOCATE (catalog[0].lensobj, Lensobj, 1);
+    catalog[0].Nlensobj = 0;
+    catalog[0].Nlensobj_off = catalog[0].Nlensobj_disk;
+  }
+
   return (TRUE);
 }
@@ -249,4 +307,6 @@
   gfits_modify (&catalog[0].header, "NMISS",    OFF_T_FMT, 1,  catalog[0].Nmissing);
   gfits_modify (&catalog[0].header, "NSECFILT", "%d",   1,                        Nsecfilt);
+  gfits_modify (&catalog[0].header, "NLENSING", OFF_T_FMT, 1,  catalog[0].Nlensing);
+  gfits_modify (&catalog[0].header, "NLENSOBJ", OFF_T_FMT, 1,  catalog[0].Nlensobj);
   gfits_modify_alt (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
   gfits_modify (&catalog[0].header, "OBJID",    "%d", 1, catalog[0].objID);
@@ -317,4 +377,30 @@
   Nitems = catalog[0].Naverage * Nsecfilt;
   SecFiltToFtable (&ftable, secfilt, Nitems, catalog[0].catformat);
+  if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
+    fprintf (stderr, "can't write table header");
+    goto failure;
+  }
+  if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
+    fprintf (stderr, "can't write table data");
+    goto failure;
+  }
+  gfits_free_table (&ftable);
+  gfits_free_header (&header);
+
+  /* write out Lensing table (convert to FITS table format) */
+  LensingToFtable (&ftable, catalog[0].lensing, catalog[0].Nlensing, catalog[0].catformat);
+  if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
+    fprintf (stderr, "can't write table header");
+    goto failure;
+  }
+  if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
+    fprintf (stderr, "can't write table data");
+    goto failure;
+  }
+  gfits_free_table (&ftable);
+  gfits_free_header (&header);
+
+  /* write out Lensobj table (convert to FITS table format) */
+  LensobjToFtable (&ftable, catalog[0].lensobj, catalog[0].Nlensobj, catalog[0].catformat);
   if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
     fprintf (stderr, "can't write table header");
@@ -356,4 +442,8 @@
   secfilt header
   secfilt table
+  lensing header
+  lensing table
+  lensobj header
+  lensobj table
 */
    
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_split.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 36917)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 36918)
@@ -161,5 +161,5 @@
   fseeko (subcat[0].f, Nskip, SEEK_CUR);
 
-  /* read Measure table header */
+  /* read table header */
   if (!gfits_fread_header (subcat[0].f, header)) {
     if (VERBOSE) fprintf (stderr, "can't read %s PHU header\n", name);
@@ -176,4 +176,6 @@
   off_t Nmeasure;
   off_t Nmissing;
+  off_t Nlensing;
+  off_t Nlensobj;
   off_t Nitems;
   int status, Nsecfilt;
@@ -194,4 +196,6 @@
   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)) return (FALSE);
+  if (!gfits_scan (&catalog[0].header, "NLENSOBJ", OFF_T_FMT, 1,  &Nlensobj)) return (FALSE);
 
   /* the OBJID is a counter that uniquely defines an average entry and never changes.  if
@@ -212,4 +216,6 @@
   catalog[0].Nmiss_disk = Nmissing;
   catalog[0].Nsecf_disk = Naverage * Nsecfilt;
+  catalog[0].Nlensing_disk = Nlensing;
+  catalog[0].Nlensobj_disk = Nlensobj;
 
   /**  Nsecfilt is unusual: it does not list the number of data items in the table
@@ -222,4 +228,6 @@
   catalog[0].missing = NULL;
   catalog[0].secfilt = NULL;
+  catalog[0].lensing = NULL;
+  catalog[0].lensobj = NULL;
 
   /*** Average Table ***/
@@ -372,4 +380,86 @@
   gfits_free_header (ftable.header);
 
+  /*** Lensing Table ***/
+  if (!(catalog[0].catflags & SKIP_LENSING)) {
+    // unless we specify 'skip', we still need to load the 
+    status = dvo_catalog_open_subcat (catalog, &catalog[0].lensing_catalog, ftable.header, "LENSING", VERBOSE);
+    if (status == DVO_CAT_OPEN_FAIL) {
+      return (FALSE);
+    }
+    if ((status == DVO_CAT_OPEN_EMPTY) && (catalog[0].Nlensing_disk > 0)) {
+      return (FALSE);
+    }
+  }
+  if ((status != DVO_CAT_OPEN_EMPTY) && (catalog[0].catflags & LOAD_LENSING)) {
+    // XXX this allows an empty Lensing catalog with non-empty Average catalog : is that OK?
+    /* 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 {
+    // XXX is it necessary to generate a template header here?
+    // XXX this is a memory leak, right?
+    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);
+    }
+    if ((status == DVO_CAT_OPEN_EMPTY) && (catalog[0].Nlensobj_disk > 0)) {
+      return (FALSE);
+    }
+  }
+  if ((status != DVO_CAT_OPEN_EMPTY) && (catalog[0].catflags & LOAD_LENSOBJ)) {
+    // XXX this allows an empty Lensobj catalog with non-empty Average catalog : is that OK?
+    /* 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 {
+    // XXX is it necessary to generate a template header here?
+    // XXX this is a memory leak, right?
+    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);
+
   return (TRUE);
 }
@@ -380,5 +470,5 @@
 
   off_t Nbytes;
-  off_t Naverage, Nexpect, Nitems, Nmeasure, Nmissing;
+  off_t Naverage, Nexpect, Nitems, Nmeasure, Nmissing, Nlensing, Nlensobj;
   Header header;
   FTable ftable;
@@ -519,4 +609,67 @@
     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, &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, &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;
+  }
+
   return (TRUE);
 }
@@ -530,5 +683,5 @@
   SecFilt *primary, *secfilt;
   int Nsecfilt;
-  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new;
+  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new, Nlensing_disk_new, Nlensobj_disk_new;
   off_t first, start, Nrows;
 
@@ -561,4 +714,6 @@
   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);
 
   /* make sure header is consistent with data */
@@ -567,4 +722,6 @@
   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_alt (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
   gfits_modify (&catalog[0].header, "OBJID",    "%d", 1, catalog[0].objID);
@@ -679,4 +836,58 @@
     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 (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 ***/
+  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 (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;
     }
@@ -711,5 +922,5 @@
   SecFilt *primary, *secfilt;
   int Nsecfilt;
-  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new;
+  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new, Nlensing_disk_new, Nlensobj_disk_new;
   off_t first, start, Nrows;
 
@@ -734,4 +945,12 @@
   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;
   }
@@ -760,4 +979,6 @@
   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;
 
   /* make sure header is consistent with data */
@@ -766,4 +987,6 @@
   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_alt (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
   gfits_modify (&catalog[0].header, "OBJID",    "%d", 1, catalog[0].objID);
@@ -878,4 +1101,58 @@
     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;
     }
@@ -909,5 +1186,5 @@
   SecFilt *primary, *secfilt;
   int Nsecfilt;
-  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new;
+  off_t Naves_disk_new, Nmeas_disk_new, Nmiss_disk_new, Nsecf_disk_new, Nlensing_disk_new, Nlensobj_disk_new;
   off_t first, start, Nrows;
 
@@ -939,4 +1216,6 @@
   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);
 
   /* make sure header is consistent with data */
@@ -945,4 +1224,6 @@
   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_alt (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
   gfits_modify (&catalog[0].header, "OBJID",    "%d", 1, catalog[0].objID);
@@ -1059,4 +1340,60 @@
     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;
     }
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert.c	(revision 36917)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert.c	(revision 36918)
@@ -361,4 +361,192 @@
     default:
       fprintf (stderr, "table format unknown (secfilt)\n");
+      return (FALSE);
+  }
+  return (TRUE);
+}
+
+/*** Lensing / FTable conversion functions ***/
+
+Lensing *FtableToLensing (FTable *ftable, off_t *Nlensing, char *format) {
+
+  Lensing *lensing;
+  char extname[80];
+
+  /* convert to the internal format */
+  if (!gfits_scan (ftable[0].header, "EXTNAME", "%s", 1, extname)) {
+    fprintf (stderr, "EXTNAME missing for lensing table\n");
+    return (FALSE);
+  }
+
+# define CONVERT_FORMAT(NAME, FORMAT, TYPE)		\
+  if (!strcmp (extname, NAME)) {					\
+    Lensing_##TYPE *tmpLensing;						\
+    tmpLensing = gfits_table_get_Lensing_##TYPE (ftable, Nlensing, NULL); \
+    if (!tmpLensing) {							\
+      fprintf (stderr, "ERROR: failed to read lensings\n");		\
+      exit (2);								\
+    }									\
+    lensing = Lensing_##TYPE##_ToInternal (tmpLensing, *Nlensing); \
+    free (tmpLensing);							\
+    *format = DVO_FORMAT_##FORMAT;					\
+    return (lensing); }
+
+  if (!strcmp (extname, "DVO_LENSING")) {
+    lensing = gfits_table_get_Lensing (ftable, Nlensing, NULL);
+    if (!lensing) {
+      fprintf (stderr, "ERROR: failed to read lensings\n");
+      exit (2);
+    }
+    *format = DVO_FORMAT_INTERNAL;
+    return (lensing);
+  }
+
+// CONVERT_FORMAT ("DVO_LENSING_PS1_REF",         PS1_REF,         PS1_REF);
+// CONVERT_FORMAT ("DVO_LENSING_ELIXIR", 	  ELIXIR,  	   Elixir);
+// CONVERT_FORMAT ("DVO_LENSING_LONEOS", 	  LONEOS,          Loneos);
+// CONVERT_FORMAT ("DVO_LENSING_PANSTARRS_DEV_0", PANSTARRS_DEV_0, Panstarrs_DEV_0);
+// CONVERT_FORMAT ("DVO_LENSING_PANSTARRS_DEV_1", PANSTARRS_DEV_1, Panstarrs_DEV_1);
+// CONVERT_FORMAT ("DVO_LENSING_PS1_DEV_1",       PS1_DEV_1,       PS1_DEV_1);
+// CONVERT_FORMAT ("DVO_LENSING_PS1_DEV_2",       PS1_DEV_2,       PS1_DEV_2);
+// CONVERT_FORMAT ("DVO_LENSING_PS1_V1",          PS1_V1,          PS1_V1);
+// CONVERT_FORMAT ("DVO_LENSING_PS1_V2",          PS1_V2,          PS1_V2);
+// CONVERT_FORMAT ("DVO_LENSING_PS1_V3",          PS1_V3,          PS1_V3);
+// CONVERT_FORMAT ("DVO_LENSING_PS1_V4",          PS1_V4,          PS1_V4);
+  CONVERT_FORMAT ("DVO_LENSING_PS1_V5",          PS1_V5,          PS1_V5);
+# undef CONVERT_FORMAT
+
+  fprintf (stderr, "table format unknown: %s\n", extname);
+
+  *Nlensing = 0;
+  return (NULL);
+}
+
+// LensingToFtable needs the Average since old formats stored lensing.dR,dD only
+int LensingToFtable (FTable *ftable, Lensing *lensing, off_t Nlensing, char format) {
+
+# define FORMAT_CASE(FORMAT, TYPE)		\
+    case DVO_FORMAT_##FORMAT: { \
+      Lensing_##TYPE *tmpLensing; \
+      tmpLensing = LensingInternalTo_##TYPE (lensing, Nlensing); \
+      gfits_table_set_Lensing_##TYPE (ftable, tmpLensing, Nlensing); \
+      free (tmpLensing); \
+      break; }
+
+  /* convert from the internal format */
+  switch (format) {
+    case DVO_FORMAT_INTERNAL: {
+      gfits_table_set_Lensing (ftable, lensing, Nlensing);
+      break; }
+
+//    FORMAT_CASE (PS1_REF,         PS1_REF);
+//    FORMAT_CASE (ELIXIR, 	    Elixir);
+//    FORMAT_CASE (LONEOS, 	    Loneos);
+//    FORMAT_CASE (PANSTARRS_DEV_0, Panstarrs_DEV_0);
+//    FORMAT_CASE (PANSTARRS_DEV_1, Panstarrs_DEV_1);
+//    FORMAT_CASE (PS1_DEV_1,       PS1_DEV_1);
+//    FORMAT_CASE (PS1_DEV_2,       PS1_DEV_2);
+//    FORMAT_CASE (PS1_V1,          PS1_V1);
+//    FORMAT_CASE (PS1_V2,          PS1_V2);
+//    FORMAT_CASE (PS1_V3,          PS1_V3);
+//    FORMAT_CASE (PS1_V4,          PS1_V4);
+      FORMAT_CASE (PS1_V5,          PS1_V5);
+# undef FORMAT_CASE
+
+    default:
+      fprintf (stderr, "table format unknown (lensing)\n");
+      return (FALSE);
+  }
+  return (TRUE);
+}
+
+/*** Lensobj / FTable conversion functions ***/
+
+Lensobj *FtableToLensobj (FTable *ftable, off_t *Nlensobj, char *format) {
+
+  Lensobj *lensobj;
+  char extname[80];
+
+  /* convert to the internal format */
+  if (!gfits_scan (ftable[0].header, "EXTNAME", "%s", 1, extname)) {
+    fprintf (stderr, "EXTNAME missing for lensobj table\n");
+    return (FALSE);
+  }
+
+# define CONVERT_FORMAT(NAME, FORMAT, TYPE)		\
+  if (!strcmp (extname, NAME)) {					\
+    Lensobj_##TYPE *tmpLensobj;						\
+    tmpLensobj = gfits_table_get_Lensobj_##TYPE (ftable, Nlensobj, NULL); \
+    if (!tmpLensobj) {							\
+      fprintf (stderr, "ERROR: failed to read lensobjs\n");		\
+      exit (2);								\
+    }									\
+    lensobj = Lensobj_##TYPE##_ToInternal (tmpLensobj, *Nlensobj); \
+    free (tmpLensobj);							\
+    *format = DVO_FORMAT_##FORMAT;					\
+    return (lensobj); }
+
+  if (!strcmp (extname, "DVO_LENSOBJ")) {
+    lensobj = gfits_table_get_Lensobj (ftable, Nlensobj, NULL);
+    if (!lensobj) {
+      fprintf (stderr, "ERROR: failed to read lensobjs\n");
+      exit (2);
+    }
+    *format = DVO_FORMAT_INTERNAL;
+    return (lensobj);
+  }
+
+// CONVERT_FORMAT ("DVO_LENSOBJ_PS1_REF",         PS1_REF,         PS1_REF);
+// CONVERT_FORMAT ("DVO_LENSOBJ_ELIXIR", 	  ELIXIR,  	   Elixir);
+// CONVERT_FORMAT ("DVO_LENSOBJ_LONEOS", 	  LONEOS,          Loneos);
+// CONVERT_FORMAT ("DVO_LENSOBJ_PANSTARRS_DEV_0", PANSTARRS_DEV_0, Panstarrs_DEV_0);
+// CONVERT_FORMAT ("DVO_LENSOBJ_PANSTARRS_DEV_1", PANSTARRS_DEV_1, Panstarrs_DEV_1);
+// CONVERT_FORMAT ("DVO_LENSOBJ_PS1_DEV_1",       PS1_DEV_1,       PS1_DEV_1);
+// CONVERT_FORMAT ("DVO_LENSOBJ_PS1_DEV_2",       PS1_DEV_2,       PS1_DEV_2);
+// CONVERT_FORMAT ("DVO_LENSOBJ_PS1_V1",          PS1_V1,          PS1_V1);
+// CONVERT_FORMAT ("DVO_LENSOBJ_PS1_V2",          PS1_V2,          PS1_V2);
+// CONVERT_FORMAT ("DVO_LENSOBJ_PS1_V3",          PS1_V3,          PS1_V3);
+// CONVERT_FORMAT ("DVO_LENSOBJ_PS1_V4",          PS1_V4,          PS1_V4);
+  CONVERT_FORMAT ("DVO_LENSOBJ_PS1_V5",          PS1_V5,          PS1_V5);
+# undef CONVERT_FORMAT
+
+  fprintf (stderr, "table format unknown: %s\n", extname);
+
+  *Nlensobj = 0;
+  return (NULL);
+}
+
+// LensobjToFtable needs the Average since old formats stored lensobj.dR,dD only
+int LensobjToFtable (FTable *ftable, Lensobj *lensobj, off_t Nlensobj, char format) {
+
+# define FORMAT_CASE(FORMAT, TYPE)		\
+    case DVO_FORMAT_##FORMAT: { \
+      Lensobj_##TYPE *tmpLensobj; \
+      tmpLensobj = LensobjInternalTo_##TYPE (lensobj, Nlensobj); \
+      gfits_table_set_Lensobj_##TYPE (ftable, tmpLensobj, Nlensobj); \
+      free (tmpLensobj); \
+      break; }
+
+  /* convert from the internal format */
+  switch (format) {
+    case DVO_FORMAT_INTERNAL: {
+      gfits_table_set_Lensobj (ftable, lensobj, Nlensobj);
+      break; }
+
+//    FORMAT_CASE (PS1_REF,         PS1_REF);
+//    FORMAT_CASE (ELIXIR, 	    Elixir);
+//    FORMAT_CASE (LONEOS, 	    Loneos);
+//    FORMAT_CASE (PANSTARRS_DEV_0, Panstarrs_DEV_0);
+//    FORMAT_CASE (PANSTARRS_DEV_1, Panstarrs_DEV_1);
+//    FORMAT_CASE (PS1_DEV_1,       PS1_DEV_1);
+//    FORMAT_CASE (PS1_DEV_2,       PS1_DEV_2);
+//    FORMAT_CASE (PS1_V1,          PS1_V1);
+//    FORMAT_CASE (PS1_V2,          PS1_V2);
+//    FORMAT_CASE (PS1_V3,          PS1_V3);
+//    FORMAT_CASE (PS1_V4,          PS1_V4);
+      FORMAT_CASE (PS1_V5,          PS1_V5);
+# undef FORMAT_CASE
+
+    default:
+      fprintf (stderr, "table format unknown (lensobj)\n");
       return (FALSE);
   }
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 36917)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 36918)
@@ -407,5 +407,4 @@
 }
 
-# if (0)
 Lensing *Lensing_PS1_V5_ToInternal (Lensing_PS1_V5 *in, off_t Nvalues) {
 
@@ -616,5 +615,4 @@
   return (out);
 }
-# endif
 
 Image *Image_PS1_V5_ToInternal (Image_PS1_V5 *in, off_t Nvalues, off_t Nalloc) {
