Index: /branches/addstar-1-3-0/Ohana/src/addstar/include/addstar.h
===================================================================
--- /branches/addstar-1-3-0/Ohana/src/addstar/include/addstar.h	(revision 5584)
+++ /branches/addstar-1-3-0/Ohana/src/addstar/include/addstar.h	(revision 5585)
@@ -68,4 +68,6 @@
 /* these globals are used separately by both client and server (KEEP) */
 double ZeroPt;  // double check for consistency
+double CAL_INSTMAG_MAX;
+double CAL_INSTMAG_MIN;
 int    VERBOSE;
 
@@ -93,5 +95,5 @@
 void       InitCalibration        PROTO(());
 GSCRegion *LoadRegions            PROTO((int *nregions));
-void       SaveCalibration        PROTO((float M, float dM, float Mr, float dMr, float Mc, float A, int N));
+void       SaveCalibration        PROTO((float Mo, float dMo, float Mr, float dMr, float Mi, int N));
 void       SetProtect             PROTO((int mode));
 int        SetSignals             PROTO(());
Index: /branches/addstar-1-3-0/Ohana/src/addstar/src/ConfigInit.c
===================================================================
--- /branches/addstar-1-3-0/Ohana/src/addstar/src/ConfigInit.c	(revision 5584)
+++ /branches/addstar-1-3-0/Ohana/src/addstar/src/ConfigInit.c	(revision 5585)
@@ -43,4 +43,8 @@
   ScanConfig (config, "OBSERVATORY-LATITUDE",   "%lf", 0, &Latitude);
   ScanConfig (config, "SUBPIX_DATAFILE",        "%s",  0, SubpixDatafile);
+
+  /* instrumental magnitude range for calibration mode */
+  if (!ScanConfig (config, "CAL_INSTMAG_MAX",   "%lf", 0, &CAL_INSTMAG_MAX)) CAL_INSTMAG_MAX =  -9.0;
+  if (!ScanConfig (config, "CAL_INSTMAG_MIN",   "%lf", 0, &CAL_INSTMAG_MIN)) CAL_INSTMAG_MIN = -13.0;
 
   /* location of needed data sources */
Index: /branches/addstar-1-3-0/Ohana/src/addstar/src/args.c
===================================================================
--- /branches/addstar-1-3-0/Ohana/src/addstar/src/args.c	(revision 5584)
+++ /branches/addstar-1-3-0/Ohana/src/addstar/src/args.c	(revision 5585)
@@ -144,4 +144,15 @@
     options.calibrate = TRUE;
     remove_argument (N, &argc, argv);
+    InitCalibration (FALSE);
+  }
+  if ((N = get_argument (argc, argv, "-excal"))) {
+    options.calibrate = TRUE;
+    remove_argument (N, &argc, argv);
+    InitCalibration (FALSE);
+  }
+  if ((N = get_argument (argc, argv, "-incal"))) {
+    options.calibrate = TRUE;
+    remove_argument (N, &argc, argv);
+    InitCalibration (TRUE);
   }
 
Index: /branches/addstar-1-3-0/Ohana/src/addstar/src/calibrate.c
===================================================================
--- /branches/addstar-1-3-0/Ohana/src/addstar/src/calibrate.c	(revision 5584)
+++ /branches/addstar-1-3-0/Ohana/src/addstar/src/calibrate.c	(revision 5585)
@@ -1,29 +1,30 @@
 # include "addstar.h"
 
+static int InternalCal;
 static int Ncal, NCAL, *Nstar;
-static float *Mobs, *dMobs, *Mref, *dMref, *Cref, *Airm;
+static float *Mobs, *dMobs, *Mref, *dMref, *Minst;
 
-void InitCalibration () {
+void InitCalibration (int mode) {
 
-    fprintf (stderr, "calibrating the image...\n");
-    Ncal = 0;
-    NCAL = 1000;
-    ALLOCATE (Mobs,  float,  NCAL);
-    ALLOCATE (dMobs, float, NCAL);
-    ALLOCATE (Mref,  float,  NCAL);
-    ALLOCATE (dMref,  float,  NCAL);
-    ALLOCATE (Cref,  float,  NCAL);
-    ALLOCATE (Airm,  float,  NCAL);
-    ALLOCATE (Nstar, int, NCAL);
+  InternalCal = mode;
+
+  fprintf (stderr, "calibrating the image...\n");
+  Ncal = 0;
+  NCAL = 1000;
+  ALLOCATE (Mobs,  float,  NCAL);
+  ALLOCATE (dMobs, float, NCAL);
+  ALLOCATE (Mref,  float,  NCAL);
+  ALLOCATE (dMref, float,  NCAL);
+  ALLOCATE (Minst, float,  NCAL);
+  ALLOCATE (Nstar, int, NCAL);
 }
   
-void SaveCalibration (float M, float dM, float Mr, float dMr, float Mc, float A, int N) {
+void SaveCalibration (float Mo, float dMo, float Mr, float dMr, float Mi, int N) {
 
-  Mobs[Ncal] = M;
-  dMobs[Ncal] = dM;
-  Mref[Ncal] = Mr;
+  Mobs[Ncal]  = Mo;
+  dMobs[Ncal] = dMo;
+  Mref[Ncal]  = Mr;
   dMref[Ncal] = dMr;
-  Cref[Ncal] = Mc;
-  Airm[Ncal] = A;
+  Minst[Ncal] = Mi;
   Nstar[Ncal] = N;
   Ncal ++;
@@ -31,10 +32,9 @@
   if (Ncal == NCAL) {
     NCAL += 1000;
+    REALLOCATE (Mobs,  float, NCAL);
     REALLOCATE (dMobs, float, NCAL);
-    REALLOCATE (Mobs,  float, NCAL);
     REALLOCATE (Mref,  float, NCAL);
-    REALLOCATE (dMref,  float, NCAL);
-    REALLOCATE (Cref,  float, NCAL);
-    REALLOCATE (Airm,  float, NCAL);
+    REALLOCATE (dMref, float, NCAL);
+    REALLOCATE (Minst, float, NCAL);
     REALLOCATE (Nstar, int,   NCAL);
   }
@@ -44,16 +44,31 @@
 void AddToCalibration (Average *average, Measure *measure, Measure *new, int *next, int Nstar) {
 
-  int i, m, found0, found1, found2;
+  int i, j, m, found0, found1, found2;
   float CalM0, CalM1, CalM2, dCalM;
+  float Mcal, color, factor;
   short CalC0, CalC1, CalC2;
-  PhotCode *code;
+
+  PhotCode *mycode;  // photcode of this measurement
+  PhotCode *incode;  // mycode.equiv (internal reference)
+  PhotCode *excode;  // incode.equiv (external reference)
 
   found0 = found1 = found2 = FALSE;
   CalM0 = CalM1 = CalM2 = dCalM = NO_MAG;
 
-  code  = GetPhotcodebyCode (new[0].source);
-  CalC0 = code[0].equiv;
-  CalC1 = code[0].c1;
-  CalC2 = code[0].c2;
+  // we have two options here: 
+  //  - calibrate to internal system (Mcal)
+  //  - calibrate to external system (Mref)
+
+  mycode = GetPhotcodebyCode (new[0].source);
+  incode = GetPhotcodebyCode (mycode[0].equiv);
+  excode = GetPhotcodebyCode (incode[0].equiv);
+
+  if (InternalCal) {
+    CalC0 = incode[0].code;
+  } else {
+    CalC0 = excode[0].code;
+  }
+  CalC1 = mycode[0].c1;
+  CalC2 = mycode[0].c2;
 
   m = average[0].offset;
@@ -73,5 +88,16 @@
     }
     if (found0 && found1 && found2) {
-      SaveCalibration (new[0].M_PS, new[0].dM_PS, CalM0, dCalM, CalM1-CalM2, new[0].airmass_PS, Nstar);
+      Mcal   = new[0].M_PS + 0.001*mycode[0].C + mycode[0].K*(new[0].airmass_PS - 1.0) - ZeroPt;
+      color  = CalM1 - CalM2 - 0.001*mycode[0].dX;
+      factor = color;
+      for (j = 0; j < mycode[0].Nc; j++) {
+	Mcal += mycode[0].X[j]*factor;
+	factor *= color;
+      }
+      if (!InternalCal) {
+	Mcal += 0.001*incode[0].C;
+      }
+      // if we want to apply a Mcal -> Mref color correction, we need the additional color term
+      SaveCalibration (Mcal, new[0].dM_PS, CalM0, dCalM, new[0].M_PS - ZeroPt - new[0].dt_PS, Nstar);
       return;
     }
@@ -84,10 +110,6 @@
 
   int i, MaxN, *Nlist, Nkeep;
-  float Mint;
-  float N, M1, M2, Klam, Clam, Xlam, Mabs, *Dmag, *dDmag;
+  float N, M1, M2, *Dmag, *dDmag;
   float dMo, dMr, Mw, Dmed, W1, W2, NSigma;
-  PhotCode *code;
-
-  code = GetPhotcodebyCode (image[0].source);
 
   /* reject multiple matched-stars */
@@ -114,18 +136,11 @@
   ALLOCATE (dDmag, float, Ncal);
   Nkeep = 0;
-  Clam = code[0].C*0.001; /* photcode.C still in millimags */
-  Klam = code[0].K;
-  Xlam = code[0].X[0];
   for (i = 0; i < Ncal; i++) {
     /* if this entry has too many (or two few?) matches, skip it */
     if (Nlist[Nstar[i]] != 1) continue;
-    Mint = Clam + Mobs[i] + Xlam*Cref[i] + Klam*(Airm[i] - 1.000);
-    Mabs = Mint - ZeroPt;
-    /* note: subpix correction is applied in gstars */
 
-    /* skip stars brighter than 8.0 */
-    /* XXX EAM : perhaps make this more flexible?? */
-    if (Mabs > 9.0) continue;
-    if (Mabs < 5.0) continue;
+    /* clip by instrumental magnitude */
+    if (Minst[i] > CAL_INSTMAG_MAX) continue;
+    if (Minst[i] < CAL_INSTMAG_MIN) continue;
     
     /* XXX EAM: note the artificial 0.005 dmag here */
@@ -133,5 +148,5 @@
     dMo = MAX (0.005, dMobs[i]);
 
-    Dmag[Nkeep] = (Mabs - Mref[i]);
+    Dmag[Nkeep] = (Mobs[i] - Mref[i]);
     dDmag[Nkeep] = (dMr*dMr + dMo*dMo);
     Nkeep ++;
Index: /trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- /trunk/Ohana/src/addstar/include/addstar.h	(revision 5584)
+++ /trunk/Ohana/src/addstar/include/addstar.h	(revision 5585)
@@ -71,4 +71,6 @@
 /* these globals are used separately by both client and server (KEEP) */
 double ZeroPt;  // double check for consistency
+double CAL_INSTMAG_MAX;
+double CAL_INSTMAG_MIN;
 int    VERBOSE;
 
@@ -91,5 +93,5 @@
 FILE      *GetDB                  PROTO((int *state));
 void       InitCalibration        PROTO(());
-void       SaveCalibration        PROTO((float M, float dM, float Mr, float dMr, float Mc, float A, int N));
+void       SaveCalibration        PROTO((float Mo, float dMo, float Mr, float dMr, float Mi, int N));
 void       SetProtect             PROTO((int mode));
 int        SetSignals             PROTO(());
Index: /trunk/Ohana/src/addstar/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ConfigInit.c	(revision 5584)
+++ /trunk/Ohana/src/addstar/src/ConfigInit.c	(revision 5585)
@@ -43,4 +43,8 @@
   ScanConfig (config, "OBSERVATORY-LATITUDE",   "%lf", 0, &Latitude);
   ScanConfig (config, "SUBPIX_DATAFILE",        "%s",  0, SubpixDatafile);
+
+  /* instrumental magnitude range for calibration mode */
+  if (!ScanConfig (config, "CAL_INSTMAG_MAX",   "%lf", 0, &CAL_INSTMAG_MAX)) CAL_INSTMAG_MAX =  -9.0;
+  if (!ScanConfig (config, "CAL_INSTMAG_MIN",   "%lf", 0, &CAL_INSTMAG_MIN)) CAL_INSTMAG_MIN = -13.0;
 
   /* location of needed data sources */
Index: /trunk/Ohana/src/addstar/src/addstar.c
===================================================================
--- /trunk/Ohana/src/addstar/src/addstar.c	(revision 5584)
+++ /trunk/Ohana/src/addstar/src/addstar.c	(revision 5585)
@@ -60,5 +60,5 @@
     SkyList *tmp;
     tmp = SkyListExistingSubset (skylist, CATDIR);
-    SkyListFree (skylist);
+    SkyListFree (skylist, FALSE);
     skylist = tmp;
   }
Index: /trunk/Ohana/src/addstar/src/args.c
===================================================================
--- /trunk/Ohana/src/addstar/src/args.c	(revision 5584)
+++ /trunk/Ohana/src/addstar/src/args.c	(revision 5585)
@@ -144,4 +144,15 @@
     options.calibrate = TRUE;
     remove_argument (N, &argc, argv);
+    InitCalibration (FALSE);
+  }
+  if ((N = get_argument (argc, argv, "-excal"))) {
+    options.calibrate = TRUE;
+    remove_argument (N, &argc, argv);
+    InitCalibration (FALSE);
+  }
+  if ((N = get_argument (argc, argv, "-incal"))) {
+    options.calibrate = TRUE;
+    remove_argument (N, &argc, argv);
+    InitCalibration (TRUE);
   }
 
Index: /trunk/Ohana/src/addstar/src/calibrate.c
===================================================================
--- /trunk/Ohana/src/addstar/src/calibrate.c	(revision 5584)
+++ /trunk/Ohana/src/addstar/src/calibrate.c	(revision 5585)
@@ -1,29 +1,30 @@
 # include "addstar.h"
 
+static int InternalCal;
 static int Ncal, NCAL, *Nstar;
-static float *Mobs, *dMobs, *Mref, *dMref, *Cref, *Airm;
+static float *Mobs, *dMobs, *Mref, *dMref, *Minst;
 
-void InitCalibration () {
+void InitCalibration (int mode) {
 
-    fprintf (stderr, "calibrating the image...\n");
-    Ncal = 0;
-    NCAL = 1000;
-    ALLOCATE (Mobs,  float,  NCAL);
-    ALLOCATE (dMobs, float, NCAL);
-    ALLOCATE (Mref,  float,  NCAL);
-    ALLOCATE (dMref,  float,  NCAL);
-    ALLOCATE (Cref,  float,  NCAL);
-    ALLOCATE (Airm,  float,  NCAL);
-    ALLOCATE (Nstar, int, NCAL);
+  InternalCal = mode;
+
+  fprintf (stderr, "calibrating the image...\n");
+  Ncal = 0;
+  NCAL = 1000;
+  ALLOCATE (Mobs,  float,  NCAL);
+  ALLOCATE (dMobs, float, NCAL);
+  ALLOCATE (Mref,  float,  NCAL);
+  ALLOCATE (dMref, float,  NCAL);
+  ALLOCATE (Minst, float,  NCAL);
+  ALLOCATE (Nstar, int, NCAL);
 }
   
-void SaveCalibration (float M, float dM, float Mr, float dMr, float Mc, float A, int N) {
+void SaveCalibration (float Mo, float dMo, float Mr, float dMr, float Mi, int N) {
 
-  Mobs[Ncal] = M;
-  dMobs[Ncal] = dM;
-  Mref[Ncal] = Mr;
+  Mobs[Ncal]  = Mo;
+  dMobs[Ncal] = dMo;
+  Mref[Ncal]  = Mr;
   dMref[Ncal] = dMr;
-  Cref[Ncal] = Mc;
-  Airm[Ncal] = A;
+  Minst[Ncal] = Mi;
   Nstar[Ncal] = N;
   Ncal ++;
@@ -31,10 +32,9 @@
   if (Ncal == NCAL) {
     NCAL += 1000;
+    REALLOCATE (Mobs,  float, NCAL);
     REALLOCATE (dMobs, float, NCAL);
-    REALLOCATE (Mobs,  float, NCAL);
     REALLOCATE (Mref,  float, NCAL);
-    REALLOCATE (dMref,  float, NCAL);
-    REALLOCATE (Cref,  float, NCAL);
-    REALLOCATE (Airm,  float, NCAL);
+    REALLOCATE (dMref, float, NCAL);
+    REALLOCATE (Minst, float, NCAL);
     REALLOCATE (Nstar, int,   NCAL);
   }
@@ -44,16 +44,31 @@
 void AddToCalibration (Average *average, Measure *measure, Measure *new, int *next, int Nstar) {
 
-  int i, m, found0, found1, found2;
+  int i, j, m, found0, found1, found2;
   float CalM0, CalM1, CalM2, dCalM;
+  float Mcal, color, factor;
   short CalC0, CalC1, CalC2;
-  PhotCode *code;
+
+  PhotCode *mycode;  // photcode of this measurement
+  PhotCode *incode;  // mycode.equiv (internal reference)
+  PhotCode *excode;  // incode.equiv (external reference)
 
   found0 = found1 = found2 = FALSE;
   CalM0 = CalM1 = CalM2 = dCalM = NO_MAG;
 
-  code  = GetPhotcodebyCode (new[0].source);
-  CalC0 = code[0].equiv;
-  CalC1 = code[0].c1;
-  CalC2 = code[0].c2;
+  // we have two options here: 
+  //  - calibrate to internal system (Mcal)
+  //  - calibrate to external system (Mref)
+
+  mycode = GetPhotcodebyCode (new[0].source);
+  incode = GetPhotcodebyCode (mycode[0].equiv);
+  excode = GetPhotcodebyCode (incode[0].equiv);
+
+  if (InternalCal) {
+    CalC0 = incode[0].code;
+  } else {
+    CalC0 = excode[0].code;
+  }
+  CalC1 = mycode[0].c1;
+  CalC2 = mycode[0].c2;
 
   m = average[0].offset;
@@ -73,5 +88,16 @@
     }
     if (found0 && found1 && found2) {
-      SaveCalibration (new[0].M_PS, new[0].dM_PS, CalM0, dCalM, CalM1-CalM2, new[0].airmass_PS, Nstar);
+      Mcal   = new[0].M_PS + 0.001*mycode[0].C + mycode[0].K*(new[0].airmass_PS - 1.0) - ZeroPt;
+      color  = CalM1 - CalM2 - 0.001*mycode[0].dX;
+      factor = color;
+      for (j = 0; j < mycode[0].Nc; j++) {
+	Mcal += mycode[0].X[j]*factor;
+	factor *= color;
+      }
+      if (!InternalCal) {
+	Mcal += 0.001*incode[0].C;
+      }
+      // if we want to apply a Mcal -> Mref color correction, we need the additional color term
+      SaveCalibration (Mcal, new[0].dM_PS, CalM0, dCalM, new[0].M_PS - ZeroPt - new[0].dt_PS, Nstar);
       return;
     }
@@ -84,10 +110,6 @@
 
   int i, MaxN, *Nlist, Nkeep;
-  float Mint;
-  float N, M1, M2, Klam, Clam, Xlam, Mabs, *Dmag, *dDmag;
+  float N, M1, M2, *Dmag, *dDmag;
   float dMo, dMr, Mw, Dmed, W1, W2, NSigma;
-  PhotCode *code;
-
-  code = GetPhotcodebyCode (image[0].source);
 
   /* reject multiple matched-stars */
@@ -114,18 +136,11 @@
   ALLOCATE (dDmag, float, Ncal);
   Nkeep = 0;
-  Clam = code[0].C*0.001; /* photcode.C still in millimags */
-  Klam = code[0].K;
-  Xlam = code[0].X[0];
   for (i = 0; i < Ncal; i++) {
     /* if this entry has too many (or two few?) matches, skip it */
     if (Nlist[Nstar[i]] != 1) continue;
-    Mint = Clam + Mobs[i] + Xlam*Cref[i] + Klam*(Airm[i] - 1.000);
-    Mabs = Mint - ZeroPt;
-    /* note: subpix correction is applied in gstars */
 
-    /* skip stars brighter than 8.0 */
-    /* XXX EAM : perhaps make this more flexible?? */
-    if (Mabs > 9.0) continue;
-    if (Mabs < 5.0) continue;
+    /* clip by instrumental magnitude */
+    if (Minst[i] > CAL_INSTMAG_MAX) continue;
+    if (Minst[i] < CAL_INSTMAG_MIN) continue;
     
     /* XXX EAM: note the artificial 0.005 dmag here */
@@ -133,5 +148,5 @@
     dMo = MAX (0.005, dMobs[i]);
 
-    Dmag[Nkeep] = (Mabs - Mref[i]);
+    Dmag[Nkeep] = (Mobs[i] - Mref[i]);
     dDmag[Nkeep] = (dMr*dMr + dMo*dMo);
     Nkeep ++;
