Index: /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_catalog_mef.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_catalog_mef.c	(revision 38218)
+++ /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_catalog_mef.c	(revision 38219)
@@ -41,10 +41,11 @@
   
   /* get the components and sizes from the header */
-  GET_TABLE_SIZES ("NSTARS",   average, Naves_disk,    TRUE);
-  GET_TABLE_SIZES ("NMEAS",    measure, Nmeas_disk,    TRUE);
-  GET_TABLE_SIZES ("NMISS",    missing, Nmiss_disk,    FALSE);
-  GET_TABLE_SIZES ("NLENSING", lensing, Nlensing_disk, FALSE);
-  GET_TABLE_SIZES ("NLENSOBJ", lensobj, Nlensobj_disk, FALSE);
-  GET_TABLE_SIZES ("NSTARPAR", starpar, Nstarpar_disk, FALSE);
+  GET_TABLE_SIZES ("NSTARS",    average,  Naves_disk,     TRUE);
+  GET_TABLE_SIZES ("NMEAS",     measure,  Nmeas_disk,     TRUE);
+  GET_TABLE_SIZES ("NMISS",     missing,  Nmiss_disk,     FALSE);
+  GET_TABLE_SIZES ("NLENSING",  lensing,  Nlensing_disk,  FALSE);
+  GET_TABLE_SIZES ("NLENSOBJ",  lensobj,  Nlensobj_disk,  FALSE);
+  GET_TABLE_SIZES ("NSTARPAR",  starpar,  Nstarpar_disk,  FALSE);
+  GET_TABLE_SIZES ("NGALSHAPE", galshape, Ngalshape_disk, FALSE);
 
   /**  Nsecfilt is unusual: it does not list the number of data items in the table
@@ -279,4 +280,29 @@
   }
 
+  /* read GalShape table header */
+  if (!gfits_fread_header (catalog[0].f, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read table galshape header");
+    return (FALSE);
+  }
+  /* read GalShape table data */
+  if (catalog[0].catflags & LOAD_GALSHAPE) {
+    if (!gfits_fread_ftable_data (catalog[0].f, &ftable, FALSE)) {
+      if (VERBOSE) fprintf (stderr, "can't read table galshape data");
+      return (FALSE);
+    }
+    catalog[0].galshape = FtableToGalShape (&ftable, &catalog[0].Ngalshape, &catalog[0].catformat);
+    if (Ngalshape != catalog[0].Ngalshape_disk) {
+      fprintf (stderr, "Warning: mismatch between Ngalshape 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;
+  }
+
   return (TRUE);
 }
Index: /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_convert.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_convert.c	(revision 38218)
+++ /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_convert.c	(revision 38219)
@@ -771,4 +771,107 @@
     default:
       fprintf (stderr, "table format unknown (starpar)\n");
+      return (FALSE);
+  }
+  return (TRUE);
+}
+
+/*** GalShape / FTable conversion functions ***/
+
+GalShape *FtableToGalShape (FTable *ftable, off_t *Ngalshape, char *format) {
+
+  GalShape *galshape;
+  char extname[80];
+
+  /* convert to the internal format */
+  if (!gfits_scan (ftable[0].header, "EXTNAME", "%s", 1, extname)) {
+    fprintf (stderr, "EXTNAME missing for galshape table\n");
+    return (FALSE);
+  }
+
+# define SKIPPING_FORMAT(NAME, FORMAT, TYPE)		\
+  if (!strcmp (extname, NAME)) {					\
+    fprintf (stderr, "ERROR: format %s not defined for galshape, skipping\n", NAME); \
+    *Ngalshape = 0; \
+    return NULL; \
+  }
+
+# define CONVERT_FORMAT(NAME, FORMAT, TYPE)		\
+  if (!strcmp (extname, NAME)) {					\
+    GalShape_##TYPE *tmpGalShape;						\
+    tmpGalShape = gfits_table_get_GalShape_##TYPE (ftable, Ngalshape, NULL); \
+    if (!tmpGalShape) {							\
+      fprintf (stderr, "ERROR: failed to read galshapes\n");		\
+      exit (2);								\
+    }									\
+    galshape = GalShape_##TYPE##_ToInternal (tmpGalShape, *Ngalshape); \
+    free (tmpGalShape);							\
+    *format = DVO_FORMAT_##FORMAT;					\
+    return (galshape); }
+
+  if (!strcmp (extname, "DVO_GALSHAPE")) {
+    galshape = gfits_table_get_GalShape (ftable, Ngalshape, NULL);
+    if (!galshape) {
+      fprintf (stderr, "ERROR: failed to read galshapes\n");
+      exit (2);
+    }
+    *format = DVO_FORMAT_INTERNAL;
+    return (galshape);
+  }
+
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PS1_REF",         PS1_REF,         PS1_REF);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_ELIXIR", 	  ELIXIR,  	   Elixir);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_LONEOS", 	  LONEOS,          Loneos);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PANSTARRS_DEV_0", PANSTARRS_DEV_0, Panstarrs_DEV_0);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PANSTARRS_DEV_1", PANSTARRS_DEV_1, Panstarrs_DEV_1);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PS1_DEV_1",       PS1_DEV_1,       PS1_DEV_1);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PS1_DEV_2",       PS1_DEV_2,       PS1_DEV_2);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PS1_V1",          PS1_V1,          PS1_V1);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PS1_V2",          PS1_V2,          PS1_V2);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PS1_V3",          PS1_V3,          PS1_V3);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PS1_V4",          PS1_V4,          PS1_V4);
+  CONVERT_FORMAT  ("DVO_GALSHAPE_PS1_V5",          PS1_V5,          PS1_V5);
+  SKIPPING_FORMAT ("DVO_GALSHAPE_PS1_SIM",         PS1_SIM,         PS1_SIM);
+# undef CONVERT_FORMAT
+# undef SKIPPING_FORMAT
+
+  fprintf (stderr, "table format unknown: %s\n", extname);
+
+  *Ngalshape = 0;
+  return (NULL);
+}
+
+// GalShapeToFtable needs the Average since old formats stored galshape.dR,dD only
+int GalShapeToFtable (FTable *ftable, GalShape *galshape, off_t Ngalshape, char format) {
+
+# define FORMAT_CASE(FORMAT, TYPE)		\
+    case DVO_FORMAT_##FORMAT: { \
+      GalShape_##TYPE *tmpGalShape; \
+      tmpGalShape = GalShapeInternalTo_##TYPE (galshape, Ngalshape); \
+      gfits_table_set_GalShape_##TYPE (ftable, tmpGalShape, Ngalshape); \
+      free (tmpGalShape); \
+      break; }
+
+  /* convert from the internal format */
+  switch (format) {
+    case DVO_FORMAT_INTERNAL: {
+      gfits_table_set_GalShape (ftable, galshape, Ngalshape);
+      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 (galshape)\n");
       return (FALSE);
   }
Index: /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_convert_PS1_V5.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 38218)
+++ /branches/eam_branches/ohana.20150429/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 38219)
@@ -196,5 +196,5 @@
     out[i].Nlensobj      = in[i].Nlensobj;     
     out[i].Nstarpar      = in[i].Nstarpar;     
-    out[i].Nextend       = in[i].Nextend;     
+    out[i].Ngalshape     = in[i].Ngalshape;     
 
     out[i].measureOffset = in[i].measureOffset; 
@@ -203,5 +203,5 @@
     out[i].lensobjOffset = in[i].lensobjOffset;
     out[i].starparOffset = in[i].starparOffset;
-    out[i].extendOffset  = in[i].extendOffset;
+    out[i].galshapeOffset  = in[i].galshapeOffset;
 
     out[i].refColorBlue  = in[i].refColorBlue;
@@ -264,5 +264,5 @@
     out[i].Nlensobj       = in[i].Nlensobj;     
     out[i].Nstarpar       = in[i].Nstarpar;     
-    out[i].Nextend        = in[i].Nextend;     
+    out[i].Ngalshape        = in[i].Ngalshape;     
 			  
     out[i].measureOffset  = in[i].measureOffset; 
@@ -271,5 +271,5 @@
     out[i].lensobjOffset  = in[i].lensobjOffset;
     out[i].starparOffset  = in[i].starparOffset;
-    out[i].extendOffset   = in[i].extendOffset;
+    out[i].galshapeOffset   = in[i].galshapeOffset;
 			  
     out[i].refColorBlue   = in[i].refColorBlue;
@@ -975,4 +975,68 @@
     out[i].objID   = in[i].objID ;
     out[i].catID   = in[i].catID ;
+  }
+  return (out);
+}
+
+GalShape *GalShape_PS1_V5_ToInternal (GalShape_PS1_V5 *in, off_t Nvalues) {
+
+  off_t i;
+  GalShape *out;
+
+  ALLOCATE_ZERO (out, GalShape, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    dvo_galshape_init (&out[i]);
+
+    out[i].Xfit       	 = in[i].Xfit;
+    out[i]. Yfit      	 = in[i]. Yfit;
+    out[i]. mag       	 = in[i]. mag;
+    out[i]. magErr    	 = in[i]. magErr;
+    out[i]. majorAxis 	 = in[i]. majorAxis;
+    out[i]. minorAxis 	 = in[i]. minorAxis;
+    out[i]. majorAxisErr = in[i]. majorAxisErr;
+    out[i]. minorAxisErr = in[i]. minorAxisErr;
+    out[i]. theta        = in[i]. theta;
+    out[i]. theta_err    = in[i]. theta_err;
+    out[i]. index        = in[i]. index;
+    out[i]. chisq	 = in[i]. chisq;
+    out[i]. Npix 	 = in[i]. Npix;
+    out[i]. objID	 = in[i]. objID;
+    out[i]. catID        = in[i]. catID;
+    out[i]. detID	 = in[i]. detID;
+    out[i]. imageID	 = in[i]. imageID;
+    out[i]. photcode	 = in[i]. photcode;
+    out[i]. modelType	 = in[i]. modelType;
+  }
+  return (out);
+}
+
+GalShape_PS1_V5 *GalShapeInternalTo_PS1_V5 (GalShape *in, off_t Nvalues) {
+
+  off_t i;
+  GalShape_PS1_V5 *out;
+
+  ALLOCATE_ZERO (out, GalShape_PS1_V5, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].Xfit       	 = in[i].Xfit;
+    out[i].Yfit      	 = in[i].Yfit;
+    out[i].mag       	 = in[i].mag;
+    out[i].magErr    	 = in[i].magErr;
+    out[i].majorAxis 	 = in[i].majorAxis;
+    out[i].minorAxis 	 = in[i].minorAxis;
+    out[i].majorAxisErr  = in[i].majorAxisErr;
+    out[i].minorAxisErr  = in[i].minorAxisErr;
+    out[i].theta         = in[i].theta;
+    out[i].theta_err     = in[i].theta_err;
+    out[i].index         = in[i].index;
+    out[i].chisq	 = in[i].chisq;
+    out[i].Npix 	 = in[i].Npix;
+    out[i].objID	 = in[i].objID;
+    out[i].catID         = in[i].catID;
+    out[i].detID	 = in[i].detID;
+    out[i].imageID	 = in[i].imageID;
+    out[i].photcode	 = in[i].photcode;
+    out[i].modelType	 = in[i].modelType;
   }
   return (out);
@@ -1234,5 +1298,5 @@
     out[i].Nlensing       = in[i].Nlensing;     
     out[i].Nlensobj       = in[i].Nlensobj;     
-    out[i].Nextend        = in[i].Nextend;     
+    out[i].Ngalshape        = in[i].Ngalshape;     
 			  
     out[i].measureOffset  = in[i].measureOffset; 
@@ -1241,5 +1305,5 @@
     out[i].lensobjOffset  = in[i].lensobjOffset;
     out[i].starparOffset  = in[i].starparOffset;
-    out[i].extendOffset   = in[i].extendOffset;
+    out[i].galshapeOffset   = in[i].galshapeOffset;
 			  
     out[i].refColorBlue   = in[i].refColorBlue;
@@ -1302,10 +1366,10 @@
     SWAP_BYTE (110); // NLENSING
     SWAP_BYTE (112); // NLENSOBJ
-    SWAP_BYTE (114); // NEXTEND
+    SWAP_BYTE (114); // NGALSHAPE
     SWAP_WORD (116); // OFF_MEASURE
     SWAP_WORD (120); // OFF_MISSING
     SWAP_WORD (124); // OFF_LENSING
     SWAP_WORD (128); // OFF_LENSOBJ
-    SWAP_WORD (132); // OFF_EXTEND
+    SWAP_WORD (132); // OFF_GALSHAPE
     SWAP_WORD (136); // OFF_PARAMS
     SWAP_WORD (140); // REF_COLOR_BLUE
