Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/test/simple.dvo
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/test/simple.dvo	(revision 36910)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/test/simple.dvo	(revision 36911)
@@ -7,29 +7,29 @@
   test.fields PS1_DEV_0 PS1_V1
   test.fields PS1_DEV_1 PS1_V1
-  test.fields PS1_V1 	PS1_V1
-  test.fields PS1_V2 	PS1_V1
-  test.fields PS1_V3 	PS1_V1
-  test.fields PS1_V4 	PS1_V1
+  test.fields PS1_V1    PS1_V1
+  test.fields PS1_V2    PS1_V1
+  test.fields PS1_V3    PS1_V1
+  test.fields PS1_V4    PS1_V1
 
   test.fields PS1_DEV_0 PS1_V2
   test.fields PS1_DEV_1 PS1_V2
-  test.fields PS1_V1 	PS1_V2
-  test.fields PS1_V2 	PS1_V2
-  test.fields PS1_V3 	PS1_V2
-  test.fields PS1_V4 	PS1_V4
+  test.fields PS1_V1    PS1_V2
+  test.fields PS1_V2    PS1_V2
+  test.fields PS1_V3    PS1_V2
+  test.fields PS1_V4    PS1_V4
 
   test.fields PS1_DEV_0 PS1_V3
   test.fields PS1_DEV_1 PS1_V3
-  test.fields PS1_V1 	PS1_V3
-  test.fields PS1_V2 	PS1_V3
-  test.fields PS1_V3 	PS1_V3
-  test.fields PS1_V4 	PS1_V4
+  test.fields PS1_V1    PS1_V3
+  test.fields PS1_V2    PS1_V3
+  test.fields PS1_V3    PS1_V3
+  test.fields PS1_V4    PS1_V4
 
   test.fields PS1_DEV_0 PS1_V4
   test.fields PS1_DEV_1 PS1_V4
-  test.fields PS1_V1 	PS1_V4
-  test.fields PS1_V2 	PS1_V4
-  test.fields PS1_V3 	PS1_V4
-  test.fields PS1_V4 	PS1_V4
+  test.fields PS1_V1    PS1_V4
+  test.fields PS1_V2    PS1_V4
+  test.fields PS1_V3    PS1_V4
+  test.fields PS1_V4    PS1_V4
 end  
 
@@ -50,4 +50,5 @@
 
   mkinput
+  echo mkcmf test.in.txt test.cmf -date 2008/1/1 -time 01:00:00 -radec $RA $DEC -type $1
   exec mkcmf test.in.txt test.cmf -date 2008/1/1 -time 01:00:00 -radec $RA $DEC -type $1
   if ($TAP_VERBOSE)
@@ -206,7 +207,7 @@
 # this list is good for PS1_V1
 list testfields_PS1_V1
-  IPP_IDET          : detid	
-  X_PSF             : xccd	
-  Y_PSF             : yccd	
+  IPP_IDET          : detid     
+  X_PSF             : xccd      
+  Y_PSF             : yccd      
   X_PSF_SIG         : xccd:err #  FAIL
   Y_PSF_SIG         : yccd:err # FAIL
@@ -215,6 +216,6 @@
   POSANGLE          : SKIP # astrometry is not calibrated in the cmf
   PLTSCALE          : SKIP # astrometry is not calibrated in the cmf
-  PSF_INST_MAG      : mag:inst	
-  PSF_INST_MAG_SIG  : mag:err	
+  PSF_INST_MAG      : mag:inst  
+  PSF_INST_MAG_SIG  : mag:err   
   AP_MAG_STANDARD   : mag:ap # FAIL
   AP_MAG_RADIUS     : SKIP # no accessor
@@ -222,13 +223,13 @@
   CAL_PSF_MAG       : SKIP # photometry is not calibrated in the cmf
   CAL_PSF_MAG_SIG   : SKIP # photometry is not calibrated in the cmf
-  SKY               : sky	
-  SKY_SIG           : sky_err	
-  PSF_CHISQ         : psf_chisq	
-  CR_NSIGMA         : cr_nsigma	
+  SKY               : sky       
+  SKY_SIG           : sky_err   
+  PSF_CHISQ         : psf_chisq 
+  CR_NSIGMA         : cr_nsigma 
   EXT_NSIGMA        : ext_nsigma
-  PSF_MAJOR         : FWHM_MAJ	
-  PSF_MINOR         : FWHM_MIN	
+  PSF_MAJOR         : FWHM_MAJ  
+  PSF_MINOR         : FWHM_MIN  
   PSF_THETA         : THETA # FAIL
-  PSF_QF            : PSF_QF	
+  PSF_QF            : PSF_QF    
   PSF_NDOF          : SKIP # no accessor
   PSF_NPIX          : SKIP # no accessor
@@ -237,12 +238,12 @@
   MOMENTS_YY        : SKIP # no accessor
   FLAGS             : phot_flags
-  N_FRAMES          : SKIP # no accessor	
+  N_FRAMES          : SKIP # no accessor        
 end
 
 # this list is good for PS1_V2
 list testfields_PS1_V2
-  IPP_IDET          : detid	
-  X_PSF             : xccd	
-  Y_PSF             : yccd	
+  IPP_IDET          : detid     
+  X_PSF             : xccd      
+  Y_PSF             : yccd      
   X_PSF_SIG         : xccd:err # FAIL
   Y_PSF_SIG         : yccd:err # FAIL
@@ -251,6 +252,6 @@
   POSANGLE          : SKIP # astrometry is not calibrated in the cmf
   PLTSCALE          : SKIP # astrometry is not calibrated in the cmf
-  PSF_INST_MAG      : mag:inst	
-  PSF_INST_MAG_SIG  : mag:err	
+  PSF_INST_MAG      : mag:inst  
+  PSF_INST_MAG_SIG  : mag:err   
   AP_MAG_STANDARD   : mag:ap # FAIL
   AP_MAG_RADIUS     : SKIP # no accessor
@@ -258,13 +259,13 @@
   CAL_PSF_MAG       : SKIP # photometry is not calibrated in the cmf
   CAL_PSF_MAG_SIG   : SKIP # photometry is not calibrated in the cmf
-  SKY               : sky	
-  SKY_SIG           : sky_err	
-  PSF_CHISQ         : psf_chisq	
-  CR_NSIGMA         : cr_nsigma	
+  SKY               : sky       
+  SKY_SIG           : sky_err   
+  PSF_CHISQ         : psf_chisq 
+  CR_NSIGMA         : cr_nsigma 
   EXT_NSIGMA        : ext_nsigma
-  PSF_MAJOR         : FWHM_MAJ	
-  PSF_MINOR         : FWHM_MIN	
+  PSF_MAJOR         : FWHM_MAJ  
+  PSF_MINOR         : FWHM_MIN  
   PSF_THETA         : THETA # FAIL
-  PSF_QF            : PSF_QF	
+  PSF_QF            : PSF_QF    
   PSF_NDOF          : SKIP # no accessor
   PSF_NPIX          : SKIP # no accessor
@@ -273,18 +274,18 @@
   MOMENTS_YY        : SKIP # no accessor
   FLAGS             : phot_flags
-  N_FRAMES          : SKIP # no accessor	
+  N_FRAMES          : SKIP # no accessor        
 end
 
 # this list is good for PS1_V3
 list testfields_PS1_V3
-  IPP_IDET          : detid	
-  X_PSF             : xccd	
-  Y_PSF             : yccd	
+  IPP_IDET          : detid     
+  X_PSF             : xccd      
+  Y_PSF             : yccd      
   X_PSF_SIG         : xccd:err # FAIL
   Y_PSF_SIG         : yccd:err # FAIL
   POSANGLE          : SKIP # astrometry is not calibrated in the cmf
   PLTSCALE          : SKIP # astrometry is not calibrated in the cmf
-  PSF_INST_MAG      : mag:inst	
-  PSF_INST_MAG_SIG  : mag:err	
+  PSF_INST_MAG      : mag:inst  
+  PSF_INST_MAG_SIG  : mag:err   
   PSF_INST_FLUX     : SKIP # not ingested into DVO
   PSF_INST_FLUX_SIG : SKIP # not ingested into DVO
@@ -297,13 +298,13 @@
   DEC_PSF           : SKIP # astrometry is not calibrated in the cmf
   PEAK_FLUX_AS_MAG  : SKIP # not ingested into DVO
-  SKY               : sky	
-  SKY_SIG           : sky_err	
-  PSF_CHISQ         : psf_chisq	
-  CR_NSIGMA         : cr_nsigma	
+  SKY               : sky       
+  SKY_SIG           : sky_err   
+  PSF_CHISQ         : psf_chisq 
+  CR_NSIGMA         : cr_nsigma 
   EXT_NSIGMA        : ext_nsigma
-  PSF_MAJOR         : FWHM_MAJ	
-  PSF_MINOR         : FWHM_MIN	
+  PSF_MAJOR         : FWHM_MAJ  
+  PSF_MINOR         : FWHM_MIN  
   PSF_THETA         : THETA # FAIL
-  PSF_QF            : PSF_QF	
+  PSF_QF            : PSF_QF    
   PSF_QF_PERFECT    : SKIP # not ingested into DVO
   PSF_NDOF          : PSF_NDOF
@@ -326,51 +327,108 @@
 end
 
-# this list is good for PS1_V4
-list testfields_PS1_V4
-  IPP_IDET          : detid	
-  X_PSF             : xccd	
-  Y_PSF             : yccd	
-  X_PSF_SIG         : xccd:err # FAIL
-  Y_PSF_SIG         : yccd:err # FAIL
-  POSANGLE          : SKIP # astrometry is not calibrated in the cmf
-  PLTSCALE          : SKIP # astrometry is not calibrated in the cmf
-  PSF_INST_MAG      : mag:inst	
-  PSF_INST_MAG_SIG  : mag:err	
-  PSF_INST_FLUX     : SKIP # not ingested into DVO
-  PSF_INST_FLUX_SIG : SKIP # not ingested into DVO
-  AP_MAG            : mag:aperinst # FAIL
+# this list is good for PS1_V5
+list testfields_PS1_V5
+  IPP_IDET          : detid     
+  X_PSF             : xccd      
+  Y_PSF             : yccd      
+  X_PSF_SIG         : xccd:err
+  Y_PSF_SIG         : yccd:err
+  POSANGLE          : posangle
+  PLTSCALE          : pltscale
+  PSF_INST_MAG      : mag:inst  
+  PSF_INST_MAG_SIG  : mag:err   
+  PSF_INST_FLUX     : flux:inst
+  PSF_INST_FLUX_SIG : flux:inst:err
+  AP_MAG            : mag:aper:inst
   AP_MAG_RAW        : SKIP # not ingested into DVO
   AP_MAG_RADIUS     : SKIP # not ingested into DVO
-  CAL_PSF_MAG       : SKIP # photometry is not calibrated in the cmf
-  CAL_PSF_MAG_SIG   : SKIP # photometry is not calibrated in the cmf
-  RA_PSF            : SKIP # astrometry is not calibrated in the cmf
-  DEC_PSF           : SKIP # astrometry is not calibrated in the cmf
-  PEAK_FLUX_AS_MAG  : SKIP # not ingested into DVO
-  SKY               : sky	
-  SKY_SIGMA         : sky_err	
-  PSF_CHISQ         : psf_chisq	
-  CR_NSIGMA         : cr_nsigma	
-  EXT_NSIGMA        : ext_nsigma
-  PSF_MAJOR         : FWHM_MAJ	
-  PSF_MINOR         : FWHM_MIN	
-  PSF_THETA         : THETA # FAIL
-  PSF_QF            : PSF_QF	
-  PSF_QF_PERFECT    : SKIP # not ingested into DVO
-  PSF_NDOF          : PSF_NDOF
-  PSF_NPIX          : PSF_NPIX
-  MOMENTS_XX        : MXX
-  MOMENTS_XY        : MXY
-  MOMENTS_YY        : MYY
-  MOMENTS_M3C       : SKIP # not ingested into DVO
-  MOMENTS_M3S       : SKIP # not ingested into DVO
-  MOMENTS_M4C       : SKIP # not ingested into DVO
-  MOMENTS_M4S       : SKIP # not ingested into DVO
-  MOMENTS_R1        : SKIP # not ingested into DVO
-  MOMENTS_RH        : SKIP # not ingested into DVO
-  KRON_FLUX         : mag:kroninst
-  KRON_FLUX_ERR     : mag:kronerr
-  KRON_FLUX_INNER   : SKIP # not ingested into DVO
-  KRON_FLUX_OUTER   : SKIP # not ingested into DVO
-  FLAGS             : phot_flags
-  N_FRAMES          : SKIP # not ingested into DVO
-end
+  AP_FLUX           : flux:aper:inst  
+  AP_FLUX_SIG       : flux:aper:inst:err
+  CAL_PSF_MAG       : mag:rel
+  CAL_PSF_MAG_SIG   : mag:rel:err
+  RA_PSF            : ra
+  DEC_PSF           : dec
+  AP_NPIX           : SKIP # not ingested into DVO		      
+  PEAK_FLUX_AS_MAG  : SKIP # not ingested into DVO		      
+  SKY               : sky       				      
+  SKY_SIGMA         : sky_err   				      
+  PSF_CHISQ         : psf_chisq 				      
+  CR_NSIGMA         : cr_nsigma 				      
+  EXT_NSIGMA        : ext_nsigma				      
+  PSF_MAJOR         : fwhm_maj # XXX technically, this is not FWHM but Sigma
+  PSF_MINOR         : fwhm_min # XXX technically, this is not FWHM but Sigma
+  PSF_THETA         : theta # FAIL				      
+  PSF_CORE          : SKIP # not ingested into DVO 
+  PSF_FWHM_MAJ      : SKIP # not ingested into DVO 
+  PSF_FWHM_MIN      : SKIP # not ingested into DVO 
+  PSF_QF            : psf_qf    				      
+  PSF_QF_PERFECT    : SKIP # not ingested into DVO		      
+  PSF_NDOF          : psf_ndof				      
+  PSF_NPIX          : psf_npix				      
+  MOMENTS_XX        : Mxx					      
+  MOMENTS_XY        : Mxy					      
+  MOMENTS_YY        : Myy					      
+  MOMENTS_M3C       : SKIP # not ingested into DVO		      
+  MOMENTS_M3S       : SKIP # not ingested into DVO		      
+  MOMENTS_M4C       : SKIP # not ingested into DVO		      
+  MOMENTS_M4S       : SKIP # not ingested into DVO		      
+  MOMENTS_R1        : SKIP # not ingested into DVO		      
+  MOMENTS_RH        : SKIP # not ingested into DVO		      
+  KRON_FLUX         : flux:kron:inst				      
+  KRON_FLUX_ERR     : flux:kron:inst:err				      
+  KRON_FLUX_INNER   : SKIP # not ingested into DVO		          
+  KRON_FLUX_OUTER   : SKIP # not ingested into DVO		      
+  SKY_LIMIT_RAD     : SKIP # not ingested into DVO		          
+  SKY_LIMIT_FLUX    : SKIP # not ingested into DVO		          
+  SKY_LIMIT_SLOPE   : SKIP # not ingested into DVO		          
+  FLAGS             : phot_flags				      
+  FLAGS2            : SKIP # not ingested into DVO		          
+  N_FRAMES          : SKIP # not ingested into DVO		      
+end								      
+
+  
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h	(revision 36910)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h	(revision 36911)
@@ -673,11 +673,24 @@
 float PhotAveErr (PhotCode *code, Average *average, SecFilt *secfilt, dvoMagClassType class, dvoMagSourceType source);
 
+float PhotInstTiny (MeasureTiny *measure, dvoMagClassType class);
+float PhotCatTiny (MeasureTiny *measure, dvoMagClassType class);
+float PhotSysTiny (MeasureTiny *measure, AverageTiny *average, SecFilt *secfilt, dvoMagClassType class);
+float PhotRelTiny (MeasureTiny *measure, AverageTiny *average, SecFilt *secfilt, dvoMagClassType class);
+float PhotCalTiny (MeasureTiny *thisone, AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, PhotCode *code, dvoMagClassType class);
+
+float PhotAveTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt, dvoMagClassType class, dvoMagSourceType source);
+float PhotRefTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, dvoMagClassType class, dvoMagSourceType source);
+
+float PhotFluxInst (Measure *measure, dvoMagClassType class);
+float PhotFluxCat (Measure *measure, dvoMagClassType class);
+float PhotFluxSys (Measure *measure, Average *average, SecFilt *secfilt, dvoMagClassType class);
+float PhotFluxRel (Measure *measure, Average *average, SecFilt *secfilt, dvoMagClassType class);
+float PhotFluxCal (Measure *thisone, Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code, dvoMagClassType class);
+
+float PhotFluxAve (PhotCode *code, Average *average, SecFilt *secfilt, dvoMagClassType class, dvoMagSourceType source);
+float PhotFluxRef (PhotCode *code, Average *average, SecFilt *secfilt, Measure *measure, dvoMagClassType class, dvoMagSourceType source);
+
 float PhotXm (PhotCode *code, Average *average, SecFilt *secfilt);
-
 float PhotZeroPoint (Measure *measure, Average *average, SecFilt *secfilt);
-float PhotAveFluxPSF (PhotCode *code, Average *average, SecFilt *secfilt);
-float PhotAvedFluxPSF (PhotCode *code, Average *average, SecFilt *secfilt);
-float PhotAveFluxKron (PhotCode *code, Average *average, SecFilt *secfilt);
-float PhotAvedFluxKron (PhotCode *code, Average *average, SecFilt *secfilt);
 
 float PhotMstdev (PhotCode *code, Average *average, SecFilt *secfilt);
@@ -689,13 +702,4 @@
 float PhotColorForCode (Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code);
 int PhotColor (Average *average, SecFilt *secfilt, Measure *measure, int c1, int c2, double *color);
-
-float PhotInstTiny (MeasureTiny *measure, dvoMagClassType class);
-float PhotCatTiny (MeasureTiny *measure, dvoMagClassType class);
-float PhotSysTiny (MeasureTiny *measure, AverageTiny *average, SecFilt *secfilt, dvoMagClassType class);
-float PhotRelTiny (MeasureTiny *measure, AverageTiny *average, SecFilt *secfilt, dvoMagClassType class);
-float PhotCalTiny (MeasureTiny *thisone, AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, PhotCode *code, dvoMagClassType class);
-
-float PhotAveTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt, dvoMagClassType class, dvoMagSourceType source);
-float PhotRefTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, dvoMagClassType class, dvoMagSourceType source);
 
 float PhotXmTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt);
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvodb.h
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvodb.h	(revision 36910)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvodb.h	(revision 36911)
@@ -64,64 +64,4 @@
   MAG_CLASS_APER,
 } dvoMagClassType;
-
-# if (0)
-enum {
-  MAG_NONE, 
-
-      // these magnitude types are from the measurement table (or derived therein)
-      // the following imply PSF magnitudes:
-      MAG_INST, // -2.5*log(DN) [ie, not DN/sec]
-      MAG_CAT,  // MAG_INST + 2.5*log(exptime) + C_lambda + K_lambda*(airmass - 1)
-      MAG_SYS,  // MAG_CAT + \sum_i A_i * (color_i - color_o) [color correction for measure photcode]
-      MAG_REL,  // MAG_SYS - Mcal [specific zero point for image]
-      MAG_CAL,  // MAG_REL + \sum_i A_i * (color_i - color_o) [color correction for average photcode]
-      MAG_ERR,  // error on PSF MAG
-
-      // the following imply APERTURE magnitudes:
-      MAG_APER_INST, // -2.5*log(DN) [ie, not DN/sec]
-      MAG_APER_CAT,  // MAG_APER_INST + 2.5*log(exptime) + C_lambda + K_lambda*(airmass - 1)
-      MAG_APER_SYS,  // MAG_APER_CAT + \sum_i A_i * (color_i - color_o) [color correction for measure photcode]
-      MAG_APER_REL,  // MAG_APER_SYS - Mcal [specific zero point for image]
-      MAG_APER_CAL,  // MAG_APER_REL + \sum_i A_i * (color_i - color_o) [color correction for average photcode]
-      MAG_APER_ERR,  // error on APER MAG (if known, else psf error)
-      MAG_APER,      // in mextract -> MAG_APER_REL, in avextract -> MAG_APER_AVE
-
-      // the following imply KRON magnitudes:
-      MAG_KRON_INST, // -2.5*log(DN) [ie, not DN/sec]
-      MAG_KRON_CAT,  // MAG_KRON_INST + 2.5*log(exptime) + C_lambda + K_lambda*(airmass - 1)
-      MAG_KRON_SYS,  // MAG_KRON_CAT + \sum_i A_i * (color_i - color_o) [color correction for measure photcode]
-      MAG_KRON_REL,  // MAG_KRON_SYS - Mcal [specific zero point for image]
-      MAG_KRON_CAL,  // MAG_KRON_REL + \sum_i A_i * (color_i - color_o) [color correction for average photcode]
-      MAG_KRON_ERR,  // error on KRON MAG
-      MAG_KRON,      // in mextract -> MAG_KRON_REL, in avextract -> MAG_KRON_AVE
-
-      // these magnitude types are from the average (secfilt) table:
-      // the following imply PSF magnitudes (or fluxes)
-      MAG_STDEV, 
-      MAG_AVE_ERR, 
-      MAG_CHISQ,
-      MAG_MIN, 
-      MAG_MAX, 
-      MAG_FLUX_PSF,
-      MAG_FLUX_PSF_ERR,
-
-      // the following imply APERTURE magnitudes:
-      MAG_APER_AVE,
-      MAG_APER_REF,
-
-      // the following imply KRON magnitudes or fluxes:
-      MAG_KRON_AVE,
-      MAG_KRON_REF,
-      MAG_FLUX_KRON,
-      MAG_FLUX_KRON_ERR,
-
-      // other secfilt fields of interest
-      MAG_PHOT_FLAGS, 
-      MAG_NCODE,
-      MAG_NPHOT,
-      MAG_UC_DIST, 
-      MAG_STACK_DET_ID, 
-} ;
-# endif
 
 /* measure fields */
@@ -163,5 +103,5 @@
       MEAS_OBJ_FLAGS, 
       MEAS_SECFILT_FLAGS, 
-      MEAS_MAG, 
+      MEAS_PHOT, // photometry class of measurements
       MEAS_MINST, 
       MEAS_MCAT, 
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/LoadPhotcodesFITS.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 36910)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 36911)
@@ -106,4 +106,9 @@
       return FALSE;
     }
+    if (!strcasecmp(photcode[i].name, "FLUX")) {
+      fprintf (stderr, "FLUX is not an allowed photcode name (reserved to DVO internals)\n");
+      free (photcode);
+      return FALSE;
+    }
 
     if (photcode[i].type == PHOT_DEP) {
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dbExtractMeasures.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dbExtractMeasures.c	(revision 36910)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dbExtractMeasures.c	(revision 36911)
@@ -100,5 +100,5 @@
 
   switch (field->ID) {
-    case MEAS_MAG: { /* magnitudes are already determined above */
+    case MEAS_PHOT: { /* magnitudes are already determined above */
       PhotCode *myEquiv = GetPhotcodeEquivbyCode (measure[0].photcode);
 
@@ -170,4 +170,5 @@
 	  }
 	  break;
+
 	case MAG_OPTION_ERR:
 	  switch (field->magLevel) {
@@ -189,5 +190,49 @@
 
 	case MAG_OPTION_FLUX:
+	  switch (field->magLevel) {
+	    case MAG_LEVEL_INST:
+	      value.Flt = PhotFluxInst (measure, field->magClass);  
+	      break;
+	    case MAG_LEVEL_CAT:
+	      value.Flt = PhotFluxCat  (measure, field->magClass); 
+	      break;
+	    case MAG_LEVEL_SYS:
+	      value.Flt = PhotFluxSys  (measure, average, secfilt, field->magClass); 
+	      break;
+	    case MAG_LEVEL_REL:
+	      value.Flt = PhotFluxRel  (measure, average, secfilt, field->magClass); 
+	      break;
+	    case MAG_LEVEL_CAL:
+	      value.Flt = PhotFluxCal  (measure, average, secfilt, measure, equiv, field->magClass); 
+	      break;
+	    case MAG_LEVEL_AVE:
+	      value.Flt = PhotFluxAve  (equiv, average, secfilt, field->magClass, field->magSource); 
+	      break;
+	    case MAG_LEVEL_REF:
+	      value.Flt = PhotFluxRef  (equiv, average, secfilt, measure, field->magClass, field->magSource); 
+	      break;
+	    case MAG_LEVEL_NONE:
+	      break;
+	  }
+	  break;
+
 	case MAG_OPTION_FLUX_ERR:
+	  switch (field->magLevel) {
+	    case MAG_LEVEL_INST:
+	    case MAG_LEVEL_CAT:
+	    case MAG_LEVEL_SYS:
+	    case MAG_LEVEL_REL:
+	    case MAG_LEVEL_CAL:
+	      // value.Flt = PhotFluxErr (measure, field->magClass);  
+	      break;
+	    case MAG_LEVEL_AVE:
+	    case MAG_LEVEL_REF:
+	      // value.Flt = PhotFluxAveErr (equiv, average, secfilt, field->magClass, field->magSource);  
+	      break;
+	    case MAG_LEVEL_NONE:
+	      break;
+	  }
+	  break;
+
 	case MAG_OPTION_STDEV:
 	case MAG_OPTION_CHISQ:
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dbFields.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dbFields.c	(revision 36910)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dbFields.c	(revision 36911)
@@ -67,7 +67,8 @@
 
   if (!strcasecmp (string, "mag"))          return (MAG_OPTION_MAG);
+  if (!strcasecmp (string, "err"))          return (MAG_OPTION_ERR);
+  if (!strcasecmp (string, "magerr"))       return (MAG_OPTION_ERR);
   if (!strcasecmp (string, "flux"))         return (MAG_OPTION_FLUX);
   if (!strcasecmp (string, "fluxerr"))      return (MAG_OPTION_FLUX_ERR);
-  if (!strcasecmp (string, "err"))          return (MAG_OPTION_ERR);
   if (!strcasecmp (string, "stdev"))        return (MAG_OPTION_STDEV);
   if (!strcasecmp (string, "chisq"))        return (MAG_OPTION_CHISQ);
@@ -160,5 +161,13 @@
 
   // defaults:
-  field->ID = MEAS_MAG; // change if not true in the end?
+  field->ID = MEAS_PHOT; // change if not true in the end?
+
+  // check what options are provided.  do not allow duplicate types
+  // except mag and flux may exist with err
+  dvoMagSourceType mySource = MAG_SRC_NONE;
+  dvoMagLevelType myLevel = MAG_LEVEL_NONE;
+  dvoMagClassType myClass = MAG_CLASS_NONE;
+  dvoMagOptionType myOption = MAG_OPTION_NONE;
+
   field->magSource = MAG_SRC_CHP;
   field->magLevel  = MAG_LEVEL_AVE;
@@ -174,8 +183,14 @@
   char *firstWord = getword(fieldCopy);
   
-  // firstWord may be either mag [mextract] or photcode
+  // firstWord may be either flux, mag or photcode
   if (!strcasecmp (firstWord, "MAG")) {
     // default level for 'mag' is rel
     field->magLevel  = MAG_LEVEL_REL;
+    myOption = MAG_OPTION_MAG;
+  } 
+  if (!strcasecmp (firstWord, "FLUX")) {
+    // default level for 'flux' is rel
+    field->magLevel  = MAG_LEVEL_REL;
+    myOption = MAG_OPTION_FLUX;
   } 
   PhotCode *code = GetPhotcodebyName (firstWord);
@@ -184,10 +199,14 @@
   if (!code) return FALSE;
 
-  // enforce compatibility
+  // defaults for photcode clases
+  if (code->type == PHOT_DEP) {
+    field->magLevel = MAG_LEVEL_REL;
+  }
+  if (code->type == PHOT_SEC) {
+    field->magLevel = MAG_LEVEL_AVE;
+  }
   if (code->type == PHOT_REF) {
     field->magLevel = MAG_LEVEL_CAT;
-  }
-  if (code->type == PHOT_DEP) {
-    field->magLevel = MAG_LEVEL_REL;
+    // is this one required?
   }
   
@@ -201,5 +220,6 @@
     dvoMagSourceType source = GetMagSource (word);
     if (source != MAG_SRC_NONE) {
-      field->magSource = source;
+      if (mySource != MAG_SRC_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
+      mySource = source;
       ptr = skipword (ptr);
       continue;
@@ -208,5 +228,6 @@
     dvoMagLevelType level = GetMagLevel (word);
     if (level != MAG_LEVEL_NONE) {
-      field->magLevel = level;
+      if (myLevel != MAG_LEVEL_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
+      myLevel = level;
       ptr = skipword (ptr);
       continue;
@@ -215,5 +236,6 @@
     dvoMagClassType class = GetMagClass (word);
     if (class != MAG_CLASS_NONE) {
-      field->magClass = class;
+      if (myClass != MAG_CLASS_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
+      myClass = class;
       ptr = skipword (ptr);
       continue;
@@ -222,5 +244,41 @@
     dvoMagOptionType option = GetMagOption (word);
     if (option != MAG_OPTION_NONE) {
-      field->magOption = option;
+      // some combinations are OK: mag + err -> err
+      if ((myOption == MAG_OPTION_MAG) && (option == MAG_OPTION_ERR)) {
+	myOption = option;
+	ptr = skipword (ptr);
+	continue;
+      }
+      // some combinations are OK: err + mag -> err
+      if ((myOption == MAG_OPTION_ERR) && (option == MAG_OPTION_MAG)) {
+	ptr = skipword (ptr);
+	continue;
+      }
+      // some combinations are OK: flux + err -> FLUX_ERR
+      if ((myOption == MAG_OPTION_FLUX) && (option == MAG_OPTION_ERR)) {
+	myOption = MAG_OPTION_FLUX_ERR;
+	ptr = skipword (ptr);
+	continue;
+      }
+      // some combinations are OK: err + flux -> FLUX_ERR
+      if ((myOption == MAG_OPTION_ERR) && (option == MAG_OPTION_FLUX)) {
+	myOption = MAG_OPTION_FLUX_ERR;
+	ptr = skipword (ptr);
+	continue;
+      }
+      // some combinations are OK: flux + fluxerr -> FLUX_ERR
+      if ((myOption == MAG_OPTION_FLUX) && (option == MAG_OPTION_FLUX_ERR)) {
+	myOption = MAG_OPTION_FLUX_ERR;
+	ptr = skipword (ptr);
+	continue;
+      }
+      // some combinations are OK: fluxerr + flux -> FLUX_ERR
+      if ((myOption == MAG_OPTION_FLUX_ERR) && (option == MAG_OPTION_FLUX)) {
+	myOption = MAG_OPTION_FLUX_ERR;
+	ptr = skipword (ptr);
+	continue;
+      }
+      if (myOption != MAG_OPTION_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
+      myOption = option;
       ptr = skipword (ptr);
       continue;
@@ -230,4 +288,10 @@
     free (word);
   }
+
+  // if nothing is provided, use the defaults
+  field->magSource = (mySource == MAG_SRC_NONE)    ? field->magSource : mySource;
+  field->magLevel  = (myLevel  == MAG_LEVEL_NONE)  ? field->magLevel  : myLevel;
+  field->magClass  = (myClass  == MAG_CLASS_NONE)  ? field->magClass  : myClass;
+  field->magOption = (myOption == MAG_OPTION_NONE) ? field->magOption : myOption;
 
   switch (field->magOption) {
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_photcode_ops.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_photcode_ops.c	(revision 36910)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_photcode_ops.c	(revision 36911)
@@ -18,5 +18,6 @@
 
 static PhotCodeData *photcodes = NULL;
-static PhotCode *genericCode = NULL;
+static PhotCode *genericCodeMag = NULL;
+static PhotCode *genericCodeFlux = NULL;
 
 PhotCodeData *GetPhotcodeTable () {
@@ -27,9 +28,15 @@
     photcodes[0].code = NULL;
   }
-  if (genericCode == NULL) {
-    ALLOCATE (genericCode, PhotCode, 1);
-    genericCode->code = 0;
-    strcpy (genericCode->name, "MAG");
-    genericCode->type = PHOT_MAG;
+  if (genericCodeMag == NULL) {
+    ALLOCATE (genericCodeMag, PhotCode, 1);
+    genericCodeMag->code = 0;
+    strcpy (genericCodeMag->name, "MAG");
+    genericCodeMag->type = PHOT_MAG;
+  }
+  if (genericCodeFlux == NULL) {
+    ALLOCATE (genericCodeFlux, PhotCode, 1);
+    genericCodeFlux->code = 0;
+    strcpy (genericCodeFlux->name, "MAG");
+    genericCodeFlux->type = PHOT_MAG;
   }
   return photcodes;
@@ -66,5 +73,10 @@
   // "MAG" is a special word, not allowed for a photcode name
   if (!strcasecmp (name, "MAG")) {
-    return genericCode;
+    return genericCodeMag;
+  }
+
+  // "FLUX" is a special word, not allowed for a photcode name
+  if (!strcasecmp (name, "FLUX")) {
+    return genericCodeFlux;
   }
 
@@ -83,7 +95,11 @@
   if (name == NULL) return (0);
 
-  // "MAG" is a special word, not allowed for a photcode name
+  // "MAG" is a special photcode name for internal use
   if (!strcasecmp (name, "MAG")) {
-    return genericCode->code;
+    return genericCodeMag->code;
+  }
+  // "FLUX" is a special photcode name for internal use
+  if (!strcasecmp (name, "FLUX")) {
+    return genericCodeFlux->code;
   }
 
@@ -834,4 +850,269 @@
 }
 
+float MagToFlux (float Mag) {
+  float Flux = pow(10.0, -0.4*(Mag));
+  return (Flux);
+}
+
+/** flux conversion *******************************************************************/
+float PhotFluxInst (Measure *measure, dvoMagClassType class) {
+
+  float Moff = - measure[0].dt - ZERO_POINT;
+  float Finst = NAN;
+  switch (class) {
+    case MAG_CLASS_PSF:
+      Finst = isnan (measure[0].FluxPSF)  ? MagToFlux(measure[0].M + Moff)     : measure[0].FluxPSF;
+      break;
+    case MAG_CLASS_KRON:
+      Finst = isnan (measure[0].FluxKron) ? MagToFlux(measure[0].Mkron + Moff) : measure[0].FluxKron;
+      break;
+    case MAG_CLASS_APER:
+      Finst = isnan (measure[0].FluxAp)   ? MagToFlux(measure[0].Map + Moff)   : measure[0].FluxAp;
+      break;
+    default:
+      break;
+  }
+  return (Finst);
+}
+
+// returns Jy fluxes assuming mag in AB? (needs an extra AP factor, right?)
+float PhotFluxCat (Measure *measure, dvoMagClassType class) {
+
+  int Np = photcodes[0].hashcode[measure[0].photcode];
+  if (Np == -1) return (NAN);
+  PhotCode *code = &photcodes[0].code[Np];
+
+  // measure.M has the static ZERO_POINT (25.0) applied, but not measure.Flux
+  float Mcal = code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C;
+  float Moff = Mcal - ZERO_POINT;
+  float Foff = MagToFlux(Mcal);
+  float Fcat = NAN;
+  switch (class) {
+    case MAG_CLASS_PSF:
+      Fcat = isnan (measure[0].FluxPSF)  ? MagToFlux(measure[0].M + Moff)     : measure[0].FluxPSF * Foff;
+      break;
+    case MAG_CLASS_KRON:
+      Fcat = isnan (measure[0].FluxKron) ? MagToFlux(measure[0].Mkron + Moff) : measure[0].FluxKron * Foff;
+      break;
+    case MAG_CLASS_APER:
+      Fcat = isnan (measure[0].FluxAp)   ? MagToFlux(measure[0].Map + Moff)   : measure[0].FluxAp * Foff;
+      break;
+    default:
+      break;
+  }
+  return (Fcat);
+}
+
+// corrected for color trends
+float PhotFluxSys (Measure *measure, Average *average, SecFilt *secfilt, dvoMagClassType class) {
+
+  int Np = photcodes[0].hashcode[measure[0].photcode];
+  if (Np == -1) return (NAN);
+  PhotCode *code = &photcodes[0].code[Np];
+
+  // measure.M has the static ZERO_POINT (25.0) applied, but not measure.Flux
+  float Mcal = code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C;
+  float Moff = Mcal - ZERO_POINT;
+  float Foff = MagToFlux(Mcal);
+  float Fcat = NAN;
+  switch (class) {
+    case MAG_CLASS_PSF:
+      Fcat = isnan (measure[0].FluxPSF)  ? MagToFlux(measure[0].M + Moff)     : measure[0].FluxPSF * Foff;
+      break;
+    case MAG_CLASS_KRON:
+      Fcat = isnan (measure[0].FluxKron) ? MagToFlux(measure[0].Mkron + Moff) : measure[0].FluxKron * Foff;
+      break;
+    case MAG_CLASS_APER:
+      Fcat = isnan (measure[0].FluxAp)   ? MagToFlux(measure[0].Map + Moff)   : measure[0].FluxAp * Foff;
+      break;
+    default:
+      break;
+  }
+  if (isnan(Fcat)) return (NAN);
+
+  // find the relevant color 
+  float mc = PhotColorForCode (average, secfilt, NULL, code);
+  if (isnan(mc)) return (Fcat);
+  mc -= SCALE*code[0].dX;
+
+  // find the color correction in mags
+  int i = 0;
+  double Mc = mc;
+  float Mcol = 0;
+  for (i = 0; i < code[0].Nc; i++) {
+    Mcol += code[0].X[i]*Mc;
+    Mc *= mc;
+  }
+  float Fcol = MagToFlux (Mcol);
+
+  float Fsys = Fcat * Fcol;
+  return (Fsys);
+}
+
+float PhotFluxRel (Measure *measure, Average *average, SecFilt *secfilt, dvoMagClassType class) {
+
+  int Np = photcodes[0].hashcode[measure[0].photcode];
+  if (Np == -1) return (NAN);
+  PhotCode *code = &photcodes[0].code[Np];
+
+  // measure.M has the static ZERO_POINT (25.0) applied, but not measure.Flux
+  float Mcal = code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C - measure[0].Mcal;
+  float Moff = Mcal - ZERO_POINT;
+  float Foff = MagToFlux(Mcal);
+  float Fcat = NAN;
+  switch (class) {
+    case MAG_CLASS_PSF:
+      Fcat = isnan (measure[0].FluxPSF)  ? MagToFlux(measure[0].M + Moff)     : measure[0].FluxPSF * Foff;
+      break;
+    case MAG_CLASS_KRON:
+      Fcat = isnan (measure[0].FluxKron) ? MagToFlux(measure[0].Mkron + Moff) : measure[0].FluxKron * Foff;
+      break;
+    case MAG_CLASS_APER:
+      Fcat = isnan (measure[0].FluxAp)   ? MagToFlux(measure[0].Map + Moff)   : measure[0].FluxAp * Foff;
+      break;
+    default:
+      break;
+  }
+  if (isnan(Fcat)) return (NAN);
+
+  /* for DEP, color must be made of PRI/SEC */
+  float mc = PhotColorForCode (average, secfilt, NULL, code);
+  if (isnan(mc)) return (Fcat);
+  mc -= SCALE*code[0].dX;
+
+  double Mc = mc;
+  float Mcol = 0;
+  int i = 0;
+  for (i = 0; i < code[0].Nc; i++) {
+    Mcol += code[0].X[i]*Mc;
+    Mc *= mc;
+  }
+  float Fcol = MagToFlux (Mcol);
+
+  float Frel = Fcat * Fcol;
+  return (Frel);
+}
+
+/* return calibrated magnitude from measure for given photcode */
+float PhotFluxCal (Measure *thisone, Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code, dvoMagClassType class) {
+
+  int i; 
+
+  if (code == NULL) return NAN;
+
+  /* code must be the matching PRI/SEC code for this measurement or an equivalent ALT */
+  int Np = photcodes[0].hashcode[thisone[0].photcode];
+  if (Np == -1) return (NAN);
+  PhotCode *myCode = &photcodes[0].code[Np];
+
+  if (code->code != myCode->equiv) return (NAN);
+
+  float Frel = PhotFluxRel (thisone, average, secfilt, class) + SCALE*code[0].C;
+
+  // get the relevant color term
+  float mc = PhotColorForCode (average, secfilt, measure, code);
+  if (isnan(mc)) return (Frel);
+  mc -= SCALE*code[0].dX;
+
+  // get the corresponding color correction in mags, convert to flux
+  double Mc = mc;
+  float Mcol = 0;
+  for (i = 0; i < code[0].Nc; i++) {
+    Mcol += code[0].X[i]*Mc;
+    Mc *= mc;
+  }
+  float Fcol = MagToFlux (Mcol);
+
+  float Fcal = Frel * Fcol;
+  return (Fcal);
+}
+
+/***/
+float PhotFluxAve (PhotCode *code, Average *average, SecFilt *secfilt, dvoMagClassType class, dvoMagSourceType source) {
+
+  if (code == NULL) return NAN;
+
+  int Ns = photcodes[0].hashNsec[code[0].code];
+  if (Ns == -1) return (NAN);
+
+  float Fave = NAN;
+  switch (source) {
+    case MAG_SRC_CHP:
+      switch (class) {
+	case MAG_CLASS_PSF:
+	  Fave = MagToFlux(secfilt[Ns].M);
+	  break;
+	case MAG_CLASS_KRON:
+	  Fave = MagToFlux(secfilt[Ns].Mkron);
+	  break;
+	case MAG_CLASS_APER:
+	  Fave = MagToFlux(secfilt[Ns].Map);
+	  break;
+	default:
+	  break;
+      }
+      break;
+    case MAG_SRC_WRP:
+      switch (class) {
+	case MAG_CLASS_PSF:
+	  Fave = isnan (secfilt[Ns].FpsfWrp)  ? MagToFlux(secfilt[Ns].MpsfWrp)  : secfilt[Ns].FpsfWrp;
+	  break;
+	case MAG_CLASS_KRON:
+	  Fave = isnan (secfilt[Ns].FkronWrp) ? MagToFlux(secfilt[Ns].MkronWrp) : secfilt[Ns].FkronWrp;
+	  break;
+	case MAG_CLASS_APER:
+	  Fave = isnan (secfilt[Ns].FapWrp)   ? MagToFlux(secfilt[Ns].MapWrp)   : secfilt[Ns].FapWrp;
+	  break;
+	default:
+	  break;
+      }
+      break;
+    case MAG_SRC_STK:
+      switch (class) {
+	case MAG_CLASS_PSF:
+	  Fave = isnan (secfilt[Ns].FpsfStk)  ? MagToFlux(secfilt[Ns].MpsfStk)  : secfilt[Ns].FpsfStk;
+	  break;
+	case MAG_CLASS_KRON:
+	  Fave = isnan (secfilt[Ns].FkronStk) ? MagToFlux(secfilt[Ns].MkronStk) : secfilt[Ns].FkronStk;
+	  break;
+	case MAG_CLASS_APER:
+	  Fave = isnan (secfilt[Ns].FapStk)   ? MagToFlux(secfilt[Ns].MapStk)   : secfilt[Ns].FapStk;
+	  break;
+	default:
+	  break;
+      }
+      break;
+    default:
+      break;
+  }
+  return (Fave);
+}
+
+/* return calibrated magnitude from average/secfilt for given photcode */
+float PhotFluxRef (PhotCode *code, Average *average, SecFilt *secfilt, Measure *measure, dvoMagClassType class, dvoMagSourceType source) {
+
+  int i;
+
+  float Fave = PhotFluxAve (code, average, secfilt, class, source);
+  if (isnan(Fave)) return NAN;
+
+  // correct for relative zero-point
+  float Moff = SCALE*code[0].C;
+
+  float mc = PhotColorForCode (average, secfilt, measure, code);
+  if (isnan(mc)) return (Fave * MagToFlux(Moff));
+  mc -= SCALE*code[0].dX;
+
+  // correct for color terms
+  double Mc = mc;
+  float Mcol = 0;
+  for (i = 0; i < code[0].Nc; i++) {
+    Mcol += code[0].X[i]*Mc;
+    Mc *= mc;    /* the 0.001 is needed for higher order terms to keep the units mag = mag^n */
+  }
+  float Foff = MagToFlux(Mcol);
+  return (Fave * Foff);
+}
+
 /******** alternate photometry conversion functions using MeasureTiny and AverageTiny *********/
 float PhotInstTiny (MeasureTiny *measure, dvoMagClassType class) {
