Index: trunk/psastro/src/psastroLoadRefstars.c
===================================================================
--- trunk/psastro/src/psastroLoadRefstars.c	(revision 20269)
+++ trunk/psastro/src/psastroLoadRefstars.c	(revision 20270)
@@ -2,5 +2,5 @@
 # define ELIXIR_MODE 1
 
-// XXX other comment
+char *psastroSetMagLimit (float *minMag, float *maxRho, pmConfig *config);
 
 psArray *psastroLoadRefstars (pmConfig *config) {
@@ -53,8 +53,10 @@
     PS_ASSERT (outformat, NULL);
 
-    char *photcode = psMetadataLookupStr(NULL, recipe, "DVO.GETSTAR.PHOTCODE");
+    // we have an exposure with a certain filter and exposure time: choose the maximum
+    // reference star density for stars fainter than an equivalent instrumental mag limit
+    float minMag;
+    float maxRho;
+    char *photcode = psastroSetMagLimit (&minMag, &maxRho, config);
     PS_ASSERT (photcode, NULL);
-
-    float MAGmax = psMetadataLookupF32(NULL, recipe, "DVO.GETSTAR.MAG.MAX");
 
     // issue the following command:
@@ -86,8 +88,6 @@
     // supply the max magnitude, the output format, and the photcode
     if (strcasecmp (photcode, "NONE")) {
-        psStringAppend (&getstarCommand, " -maglim %f", MAGmax);
-    }
-    if (strcasecmp (photcode, "NONE")) {
-        psStringAppend (&getstarCommand, " -photcode %s", photcode);
+
+        psStringAppend (&getstarCommand, " -photcode %s -minmag %f -max-density %f", photcode, minMag, maxRho);
     }
     psStringAppend (&getstarCommand, " -format %s", outformat);
@@ -222,2 +222,89 @@
     return refstars;
 }
+
+# define ESCAPE(MSG) { \
+  psLogMsg ("psastro", PS_LOG_INFO, MSG); \
+  goto escape; }
+
+char *psastroSetMagLimit (float *minMag, float *maxRho, pmConfig *config) {
+
+    bool status;
+    char *photcode;
+
+    // select the current recipe
+    psMetadata *recipe  = psMetadataLookupPtr (NULL, config->recipes, PSASTRO_RECIPE);
+
+    // select the input data sources
+    pmFPAfile *input = psMetadataLookupPtr (NULL, config->files, "PSASTRO.INPUT");
+    if (!input) {
+	psError(PSASTRO_ERR_CONFIG, true, "Can't find input data");
+	return NULL;
+    }
+    assert (input->fpa);
+
+    *maxRho = psMetadataLookupF32(&status, recipe, "DVO.GETSTAR.MAX.RHO");
+    if (!status) {
+	psError(PSASTRO_ERR_CONFIG, false, "DVO.GETSTAR.MAX.RHO missing from recipe");
+	return NULL;
+    }
+
+    // select the filter; default to fixed photcode and mag limit otherwise
+    char *filter = psMetadataLookupStr (&status, input->fpa->concepts, "FPA.FILTERID");
+    if (!status) ESCAPE ("missing FPA.FILTER in concepts");
+
+    float exptime = psMetadataLookupF32 (&status, input->fpa->concepts, "FPA.EXPOSURE");
+    if (!status) ESCAPE ("missing FPA.EXPOSURE in concepts");
+
+    // we need to select the PHOTCODE.DATA folder that matches our filter
+    psMetadataItem *item = psMetadataLookup (recipe, "PHOTCODE.DATA");
+    if (!item) ESCAPE ("PHOTCODE.DATA folders missing");
+    if (item->type != PS_DATA_METADATA_MULTI) ESCAPE ("PHOTCODE.DATA not a multi");
+
+    float minInst = psMetadataLookupF32(&status, recipe, "DVO.GETSTAR.MIN.MAG.INST");
+    if (!status) ESCAPE ("missing DVO.GETSTAR.MIN.MAG.INST");
+
+    // PHOTCODE.DATA is a multi of metadata items 
+    psListIterator *iter = psListIteratorAlloc(item->data.list, PS_LIST_HEAD, false);
+
+    psMetadataItem *refItem = NULL;
+    while ((refItem = psListGetAndIncrement (iter))) {
+	if (refItem->type != PS_DATA_METADATA) ESCAPE ("PHOTCODE.DATA entry is not a metadata folder");
+    
+	char *refFilter = psMetadataLookupStr (&status, refItem->data.md, "FILTER");
+	if (!status) {
+	    psLogMsg ("psastro", PS_LOG_INFO, "a PHOTCODE.DATA recipe folder is missing FILTER");
+	    continue;
+	}
+
+	// does this entry match the current filter?
+	if (strcmp (refFilter, filter)) continue;
+
+	psLogMsg ("psastro", PS_LOG_DETAIL, "PHOTCODE.DATA found for filter %s", filter);
+
+	float zeropt = psMetadataLookupF32 (&status, refItem->data.md, "ZEROPT");
+	if (!status) {
+	    psLogMsg ("psastro", PS_LOG_INFO, "a PHOTCODE.DATA recipe folder is missing FILTER");
+	    continue;
+	}
+	photcode = psMetadataLookupStr (&status, refItem->data.md, "PHOTCODE");
+	if (!status) {
+	    psLogMsg ("psastro", PS_LOG_INFO, "a PHOTCODE.DATA recipe folder is missing FILTER");
+	    continue;
+	}
+
+	// convert the minInst to a calibrated minimum magnitude
+	*minMag = minInst + 2.5*log10(exptime) + zeropt;
+
+	psFree (iter);
+	return photcode;
+    }	
+    psFree (iter);
+
+  escape:
+    photcode = psMetadataLookupStr(NULL, recipe, "DVO.GETSTAR.PHOTCODE");
+    PS_ASSERT (photcode, NULL);
+	
+    // give up and use fixed value
+    *minMag = psMetadataLookupF32(NULL, recipe, "DVO.GETSTAR.MIN.MAG");
+    return photcode;
+}
