Index: /branches/eam_branches/relphot.20200519/test/relphot.tgroups.dvo
===================================================================
--- /branches/eam_branches/relphot.20200519/test/relphot.tgroups.dvo	(revision 41385)
+++ /branches/eam_branches/relphot.20200519/test/relphot.tgroups.dvo	(revision 41386)
@@ -47,32 +47,29 @@
   tapPLAN {((mjd_uc[]*4 + mjd_nc[]*4) + (mjd_uc[] + mjd_nc[]) + (mjd_uc[] + mjd_nc[])) / 3}
 
+  # XXX first, run the analysis with only ubercal data (photometric data)
+  # we should be able to recover the nightly zero points fitting only tgroups
+
   # generate the basic images and check they were correctly ingested by dvo
   for i 0 mjd_uc[]
-    mkexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] $filt_uc:$i $fileroot $RA_CENTER $DEC_CENTER
-    ckexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] $filt_uc:$i raw
+    sprintf filerootout "%s.%02d" $fileroot $i
+    mkexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] secz_uc[$i] $FILTER $filerootout $RA_CENTER $DEC_CENTER
+    ckexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] secz_uc[$i] $FILTER raw
+  end
+
+  break
+  
+  # run relphot on the db and check that the images now match the expected values
+  tapEXEC relphot -tgroup-fit-airmass -tgroups tgroups.dat -images g -v -region $RA_MIN $RA_MAX $DEC_MIN $DEC_MAX -D CATDIR $catdir -D STAR_TOOFEW 1 -statmode WT_MEAN -cloud-limit 0.5 -update -nloop 12
+
+  break
+
+  # reset the per-filter ZPT_OFF:$filter values
+  $ZPT_OFF:$FILTER = NAN
+
+  for i 0 mjd_uc[]
+    ckexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] $FILTER relphot_uc
   end
   for i 0 mjd_nc[]
-    mkexposure $catdir mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i $fileroot $RA_CENTER $DEC_CENTER
-    ckexposure $catdir mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i raw
-  end
-
-  # run relphot on the db and check that the images now match the expected values
-  tapEXEC relphot -images g,r -v -region 9.5 10.5 19.5 20.5 -D CATDIR $catdir -D STAR_TOOFEW 1 -statmode WT_MEAN -cloud-limit 0.5 -update -nloop 12
-
-  # reset the per-filter ZPT_OFF:$filter values
-  for i 0 $filt_uc:n
-    $filter = $filt_uc:$i
-    $ZPT_OFF:$filter = NAN
-  end
-  for i 0 $filt_nc:n
-    $filter = $filt_nc:$i
-    $ZPT_OFF:$filter = NAN
-  end
-
-  for i 0 mjd_uc[]
-    ckexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] $filt_uc:$i relphot_uc
-  end
-  for i 0 mjd_nc[]
-    ckexposure $catdir mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i relphot_nc
+    ckexposure $catdir mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $FILTER relphot_nc
   end
   tapDONE
@@ -88,15 +85,9 @@
   $DEC_CENTER = 20.0
   $PLATE_SCALE = 0.25
-  $NSEASON = 3
-  $NFILTER = 3
-  $NCHIP_X = 2
-  $NCHIP_Y = 2
-  $NCELL_X = 2
-  $NCELL_Y = 2
+  $NCHIP_X = 1
+  $NCHIP_Y = 1
   $CHIP_DX = 1000
   $CHIP_DY = 1000
-  $DEFECT_FRAC = 0.05
-  $OFFSET_FRAC_UC = 0.0
-  $OFFSET_FRAC_NC = 0.5
+  $DEFECT_FRAC = 0.00
 
   # we have two sets of images: ubercal (photometric) and not-ubercaled
@@ -104,52 +95,64 @@
   # ********* UBERCAL IMAGES ***********
 
-  # the number of exposures is defined by filt_uc:n
-  list filt_uc -split g g g g r r r r
-
-  # tmpseq is used to generate vectors in this function
-  create tmpseq 0 $filt_uc:n
-
-  # exptime and airmasses for uc images
-  set exptime_uc = 10.0 + zero(tmpseq)
-
-# XXX range of airmass
-  set airmass_uc =  1.3 + zero(tmpseq)
-  set klam_uc = zero(tmpseq)
-  set zpt_uc = zero(tmpseq)
-
-  # ubercal zero points are defined as ZP_nominal + 2.5log(exptime) + K*(airmass - 1.0)
-  # note that K is defined as a negative value (is this sensible?)
-
-  # zero points and airmass slopes for these sequences
-  for i 0 $filt_uc:n
-    klam_uc[$i] = $klam_nominal:$filt_uc:$i 
-    zpt_uc[$i]  = $zpt_nominal:$filt_uc:$i + 2.5*log(exptime_uc[$i]) + klam_uc[$i]*(airmass_uc[$i] - 1.0) + $UC_CLOUDS*(rnd(klam_uc[$i]) - 0.5)
-    $ZPT_OFF:$filt_uc:$i = NAN
+  # ubercal nights each have a distinct zero point and slope: 
+  vlist night_uc 55000.0 55010.0 55030.0 55060.0
+  vlist dz_nt_uc +0.0000 +0.0300 -0.0200 -0.0400
+# vlist dk_nt_uc +0.0000 +0.0005 -0.0005 -0.0002
+  vlist dk_nt_uc +0.0000 +0.0200 -0.0200 +0.0300
+
+  write tgroups.dat night_uc  
+
+  $EXPTIME = 10.0
+  $FILTER = g
+  $ZPT_OFF:$FILTER = NAN
+
+  delete -q mjd_uc seq_uc zpt_uc klam_uc secz_uc exptime_uc
+
+  # generate 5 exposures for each night
+  for i 0 5
+    set secz_tmp = rnd(night_uc) + 1.0; # airmass in range 1.0 - 2.0
+    set klam_tmp = $klam_nominal:$FILTER + dk_nt_uc
+    set exp_tmp  = zero(night_uc) + $EXPTIME
+    set zpt_tmp  = $zpt_nominal:$FILTER  + dz_nt_uc + 2.5*log(exp_tmp) + klam_tmp*(secz_tmp - 1.0)
+
+    set seq_tmp  = ramp(night_uc)
+    set mjd_tmp  = night_uc + 0.33 + 0.01*$i; # exposures are 14 minutes apart starting at 10pm HST
+
+    concat klam_tmp klam_uc
+    concat secz_tmp secz_uc
+    concat  zpt_tmp  zpt_uc
+    concat  seq_tmp  seq_uc
+    concat  mjd_tmp  mjd_uc
+
+    concat  exp_tmp  exptime_uc
   end    
-  set mjd_uc = 55000.00 + 0.01*(tmpseq % 3) + 10.0*int(tmpseq / 3)
 
   # ********* NON-UBERCAL IMAGES ***********
 
-  # mjd and zpt values for not-ubercal'ed images
-  list filt_nc -split g g g g r r r r
-
-  # tmpseq is used to generate vectors in this function
-  create tmpseq 0 $filt_nc:n
-
-  # place this within a valid season (55000.0 - 55010.0 - 55020.0 - 55030.0)
-  set exptime_nc = 10.0 + zero(tmpseq)
-  set airmass_nc =  1.6 + zero(tmpseq)
-  set klam_nc = zero(tmpseq)
-  set zpt_nc = zero(tmpseq)
-
-  # airmass slopes for these sequnece
-  for i 0 $filt_nc:n
-    klam_nc[$i] = $klam_nominal:$filt_nc:$i 
-    zpt_nc[$i]  = $zpt_nominal:$filt_nc:$i + 2.5*log(exptime_nc[$i]) + klam_nc[$i]*(airmass_nc[$i] - 1.0) + $NC_CLOUDS*(rnd(klam_nc[$i]) - 0.5)
-    $ZPT_OFF:$filt_uc:$i = NAN
+  # non-ubercal nights each have a random zero points and fixed slope (klam_nominal)
+  # non-ubercal nights must not be the same set as the ubercal nights above
+  vlist night_nc 55005.0 55015.0 55035.0 55065.0
+
+  delete -q mjd_nc seq_nc zpt_nc klam_nc secz_nc
+
+  # generate 5 exposures for each night
+  for i 0 5
+    set secz_tmp = rnd(night_nc)  + 1.0; # airmass in range 1.0 - 2.0
+    set klam_tmp = zero(night_nc) + $klam_nominal:$FILTER
+    set exp_tmp  = zero(night_nc) + $EXPTIME
+    set zpt_tmp  = $zpt_nominal:$FILTER + 2.5*log(exp_tmp) + klam_tmp*(secz_tmp - 1.0) - $NC_CLOUDS*(0.1 + rnd(secz_tmp))
+    # observed zero points for nc nights are 0.1 - 1.1 lower than the ubercal nights
+
+    set seq_tmp  = ramp(night_nc)
+    set mjd_tmp  = night_nc + 0.33 + 0.01*$i; # exposures are 14 minutes apart starting at 10pm HST
+
+    concat klam_tmp klam_nc
+    concat secz_tmp secz_nc
+    concat  zpt_tmp  zpt_nc
+    concat  seq_tmp  seq_nc
+    concat  mjd_tmp  mjd_nc
+
+    concat  exp_tmp  exptime_nc
   end    
-  set mjd_nc = 55000.10 + 0.01*(tmpseq % 3) + 10.0*int(tmpseq / 3)
-
-  delete tmpseq
 end
 
@@ -169,20 +172,9 @@
 
   # reset the per-filter ZPT_OFF:$filter values
-  for i 0 $filt_uc:n
-    $filter = $filt_uc:$i
-    $ZPT_OFF:$filter = NAN
-  end
-  for i 0 $filt_nc:n
-    $filter = $filt_nc:$i
-    $ZPT_OFF:$filter = NAN
-  end
+  $ZPT_OFF:$FILTER = NAN
 
   for i 0 mjd_uc[]
-    # ckexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] $filt_uc:$i relphot_uc
-      ckexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] $filt_uc:$i relphot_uc
-  end
-  for i 0 mjd_nc[]
-    # ckexposure $catdir mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i relphot_nc
-      ckexposure $catdir mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i relphot_nc
+    # ckexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] $FILTER relphot_uc
+      ckexposure $catdir mjd_uc[$i] zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] $FILTER relphot_uc
   end
 end
@@ -222,5 +214,6 @@
   end
 
-  skyregion {$RA_CENTER - 0.2/dcos($DEC_CENTER)} {$RA_CENTER + 0.2/dcos($DEC_CENTER)} {$DEC_CENTER - 0.2} {$DEC_CENTER + 0.2} 
+  # RA_MIN, etc are defined by mkstars
+  skyregion $RA_MIN $RA_MAX $DEC_MIN $DEC_MAX
 
   ## calculate the average zero point offset
@@ -331,5 +324,5 @@
     $dec = $DECo - $dy / 3600.0
     # echo $ra $dec $dx $dy
-    mkinput test.in.txt $ra $dec $ZPT 
+    mkinput $ROOT.$ix.$iy.in.txt $ra $dec $ZPT 
     
     # ra,dec is the center of this chip
@@ -353,5 +346,5 @@
     $options = $options -exptime $EXPTIME
 
-    tapEXEC mkcmf test.in.txt $ROOT.$ix.$iy.cmf $options
+    tapEXEC mkcmf  $ROOT.$ix.$iy.in.txt $ROOT.$ix.$iy.cmf $options
 
     # the fake images have inconsistent ra,dec and airmass,sidtime values
@@ -437,11 +430,16 @@
   # the images are oriented along N-S, E-W lines
 
-  # size of region of interest in linear arcseconds
-  $RA_RANGE  = 1.25 * $CHIP_DX * $NCHIP_X * $PLATE_SCALE
-  $DEC_RANGE = 1.25 * $CHIP_DY * $NCHIP_Y * $PLATE_SCALE 
+  # size of region of interest in linear degrees
+  $RA_RANGE  = 1.25 * $CHIP_DX * $NCHIP_X * $PLATE_SCALE / 3600
+  $DEC_RANGE = 1.25 * $CHIP_DY * $NCHIP_Y * $PLATE_SCALE / 3600 
+
+  $RA_MIN  =  $RA_CENTER - 0.5*$RA_RANGE/dcos($DEC_CENTER)
+  $RA_MAX  =  $RA_CENTER + 0.5*$RA_RANGE/dcos($DEC_CENTER)
+  $DEC_MIN = $DEC_CENTER - 0.5*$DEC_RANGE
+  $DEC_MAX = $DEC_CENTER + 0.5*$DEC_RANGE
 
   create tmp 0 $1
-  set stars_ra  = $RA_CENTER  + $RA_RANGE  * (rnd(tmp) - 0.5) / 3600 / dcos ($DEC_CENTER)
-  set stars_dec = $DEC_CENTER + $DEC_RANGE * (rnd(tmp) - 0.5) / 3600
+  set stars_ra  = $RA_CENTER  + $RA_RANGE  * (rnd(tmp) - 0.5) / dcos ($DEC_CENTER)
+  set stars_dec = $DEC_CENTER + $DEC_RANGE * (rnd(tmp) - 0.5)
   set stars_mag = 10.0 + 10.0 * rnd(tmp) 
   set stars_dmag = 0.02 + 0.1 * rnd(tmp)
