Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/Makefile
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/Makefile	(revision 37470)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/Makefile	(revision 37471)
@@ -42,4 +42,5 @@
 $(SRC)/load_template_images.$(ARCH).o \
 $(SRC)/make_fake_images.$(ARCH).o \
+$(SRC)/airmass.$(ARCH).o \
 $(SRC)/get_image_patch.$(ARCH).o \
 $(SRC)/load_fake_stars.$(ARCH).o \
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h	(revision 37470)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/include/fakeastro.h	(revision 37471)
@@ -47,4 +47,6 @@
 char  *CPT_FILE;
 char  *INPUT;
+
+int    IMAGE_ID;
 
 char  *IMAGES_INPUT;
@@ -130,2 +132,5 @@
 
 int InitStar (Stars *star);
+
+float airmass (float secz_image, double ra, double dec, double st, double latitude);
+float azimuth (double ha, double dec, double latitude);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/airmass.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/airmass.c	(revision 37471)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/airmass.c	(revision 37471)
@@ -0,0 +1,52 @@
+# include "fakeastro.h"
+
+static int Nbad_airmass = 0;
+
+float airmass (float secz_image, double ra, double dec, double st, double latitude) {
+
+  double hour, cosz, secz;
+  double rdec, rlat;
+
+  /* ra, dec, latitude in dec deg; st in dec hours */
+
+  /* hour : hour angle in degrees */
+  rdec = RAD_DEG*dec;
+  rlat = RAD_DEG*latitude;
+  hour = 15.0*st - ra;
+  cosz = sin (rdec) * sin (rlat) + cos (rdec) * cos (RAD_DEG*hour) * cos (rlat);
+  secz = 1.000 / cosz;
+  
+  if (!isfinite(secz)) {
+    Nbad_airmass ++;
+  }
+  if (Nbad_airmass > 1) {
+    fprintf (stderr, "*** WARNING *** NaN airmass for detection\n");
+    fprintf (stderr, "*** ra, dec : %f, %f | st : %f | lat : %f | cosz : %f\n", ra, dec, st, latitude, cosz);
+  }
+  if (Nbad_airmass > 10) {
+    fprintf (stderr, "*** ERROR *** more than 10 NaN airmass values for detections, giving up\n");
+    fprintf (stderr, " (use -quick-airmass to use image airmass)\n");
+    exit (2);
+  }
+
+  return (secz);
+}
+
+// ha/dec -> az
+float azimuth (double ha, double dec, double latitude) {
+
+  double rdec, rlat, rha;
+  double sinh, cosh;
+  float az;
+
+  rdec = RAD_DEG*dec;
+  rha = RAD_DEG*ha;
+  rlat = RAD_DEG*latitude;
+
+  sinh = - cos (rdec) * sin (rha);
+  cosh =   sin (rdec) * cos (rlat) - cos (rdec) * cos (rha) * sin (rlat);
+
+  az = DEG_RAD * atan2 (sinh, cosh);
+
+  return az;
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images.c	(revision 37470)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/fakeastro_images.c	(revision 37471)
@@ -32,4 +32,6 @@
 
   // subset out the DIS exposures here:
+
+  IMAGE_ID = 1;
 
   int NrefImage;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/insert_fakestar.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/insert_fakestar.c	(revision 37470)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/insert_fakestar.c	(revision 37471)
@@ -52,6 +52,26 @@
     }
 
+    // Mr vs r-i, Mr vs r-z: linear fit from Bochanski et al 2010 Fig 7 & Fig 9 
+    // http://iopscience.iop.org/1538-3881/139/6/2679/pdf/aj_139_6_2679.pdf
+    // gr, zy relations from Magnier et al 2012 Fig 3
+
+    double M_r = stars[i].starpar.M_r;
+    double m_r = M_r + stars[i].starpar.DistMag;
+    double ri = 0.215*M_r - 1.05;
+    double rz = 0.350*M_r - 1.82;
+    double gr = 2.27*ri + 0.09;
+    double zy = 0.37*(rz - ri) + 0.03; // zy vs iz
+
+    double m_g = m_r + gr;
+    double m_i = m_r - ri;
+    double m_z = m_r - rz;
+    double m_y = m_z - zy;
+
     // I need a photcode for r-band
-    catalog[0].secfilt[Nave*Nsecfilt+1].M = stars[i].starpar.M_r + stars[i].starpar.DistMag;
+    catalog[0].secfilt[Nave*Nsecfilt+0].M = m_g;
+    catalog[0].secfilt[Nave*Nsecfilt+1].M = m_r;
+    catalog[0].secfilt[Nave*Nsecfilt+2].M = m_i;
+    catalog[0].secfilt[Nave*Nsecfilt+3].M = m_z;
+    catalog[0].secfilt[Nave*Nsecfilt+4].M = m_y;
 
     catalog[0].starpar[Nstarpar]        = stars[i].starpar;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_template_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_template_images.c	(revision 37470)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/load_template_images.c	(revision 37471)
@@ -30,4 +30,6 @@
   }
 
+  BuildChipMatch (image, Nimage);
+
   int NREFIMAGE = 1000;
   int Nrefimage = 0;
@@ -35,5 +37,5 @@
   ALLOCATE (refimage, Image, NREFIMAGE);
 
-  int i;
+  int i, j;
   for (i = 0; i < Nimage; i++) {
 
@@ -42,4 +44,30 @@
 
     refimage[Nrefimage] = image[i];
+
+    // find a corresponding chip image:
+    Image *childImage = NULL;
+    for (j = i + 1; j < i + 65; j++) {
+      if (image[j].parent == &image[i]) {
+	childImage = &image[j];
+	break;
+      }
+    }
+    if (!childImage) {
+      fprintf (stderr, "!");
+      // skip this one?
+      continue;
+    }
+
+    refimage[Nrefimage].secz     = childImage->secz;
+    refimage[Nrefimage].Mcal     = childImage->Mcal;
+    refimage[Nrefimage].dMcal    = childImage->dMcal;
+    refimage[Nrefimage].exptime  = childImage->exptime;
+    refimage[Nrefimage].sidtime  = childImage->sidtime;
+    refimage[Nrefimage].latitude = childImage->latitude;
+    refimage[Nrefimage].fwhm_x   = childImage->fwhm_x;
+    refimage[Nrefimage].fwhm_y   = childImage->fwhm_y;
+
+    // this is a bit of a hack, but not too bad (very gpc1 specific):
+    refimage[Nrefimage].photcode = 100 * (int) (childImage->photcode / 100);
 
     Nrefimage ++;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_images.c	(revision 37470)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_images.c	(revision 37471)
@@ -1,3 +1,7 @@
 # include "fakeastro.h"
+
+// fields I need to inherit from (one of) the WRP images:
+// secz, Mcal (ensure measure.M is set right), exptime (if not hardwired), sidtime (or calculate from jd, longitude),
+// extern_id, source_id?, name (is this used in relastro?, NO: stack names are used in relphot to set the tess_id)
 
 Image *make_fake_images (Image *image, int *nfakeImage) {
@@ -14,5 +18,7 @@
 
   fakeImage[0] = image[0];
-
+  fakeImage[0].imageID = IMAGE_ID;
+  IMAGE_ID ++;
+  
   int N = 1;
 
@@ -58,12 +64,17 @@
       fakeImage[N].NY = 4850;
 
-      // XXX need a way to choose this better...
-      fakeImage[N].photcode = 10100 + iy*10 + ix;
-      fakeImage[N].exptime = 45.0;
+      fakeImage[N].imageID = IMAGE_ID;
+      IMAGE_ID ++;
+
+      // gpc1-specific (assumes grizy map to 10101, etc)
+      fakeImage[N].photcode = fakeImage[0].photcode + iy*10 + ix;
       N++;
     }
   }
 
-  *nfakeImage = NfakeImage;
+  // the PHU needs to have photcode of 0 for relphot
+  fakeImage[0].photcode = 0;
+
+  *nfakeImage = N;
   return fakeImage;
 }
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars_catalog.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars_catalog.c	(revision 37470)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fake_stars_catalog.c	(revision 37471)
@@ -122,14 +122,13 @@
     stars[Nstars].average.D = Dobs;
 
-    /* 
-       XXX need to set these?
-       stars[N].measure.airmass = airmass (image[0].secz, stars[N].average.R, stars[N].average.D, image[0].sidtime, image[0].latitude);
-       stars[N].measure.az      = azimuth (15.0*image[0].sidtime - stars[N].average.R, stars[N].average.D, image[0].latitude);
-       stars[N].measure.Mcal    = image[0].Mcal;
-       stars[N].measure.t       = image[0].tzero + 1e-4*stars[N].measure.Yccd*image[0].trate;  // trate is in 0.1 msec / row 
-       stars[N].measure.dt      = MTIME;
-    */
+    stars[Nstars].measure.airmass = airmass (image->secz, stars[Nstars].average.R, stars[Nstars].average.D, image->sidtime, image->latitude);
+    stars[Nstars].measure.az      = azimuth (15.0*image->sidtime - stars[Nstars].average.R, stars[Nstars].average.D, image->latitude);
+    stars[Nstars].measure.Mcal    = image->Mcal;
+    stars[Nstars].measure.t       = image->tzero + 1e-4*stars[Nstars].measure.Yccd*image->trate;  // trate is in 0.1 msec / row 
+    stars[Nstars].measure.dt      = Mtime;
 
-    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
+    stars[Nstars].measure.imageID = image->imageID;
+
+    // This is may optionally be replaced by the internal sequence (see FilterStars.c)
     stars[Nstars].measure.detID      = Nstars + 1;
     Nstars ++;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fakestars.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fakestars.c	(revision 37470)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/fakeastro/src/make_fakestars.c	(revision 37471)
@@ -71,5 +71,5 @@
     double uB = uB_gal + uB_sol;
 
-    // crude Mr distribution from
+    // crude Mr distribution from Bochanski et al 2010
     // http://iopscience.iop.org/1538-3881/139/6/2679/pdf/aj_139_6_2679.pdf
 
