Index: /trunk/archive/p2tools/doc/pipeline.txt
===================================================================
--- /trunk/archive/p2tools/doc/pipeline.txt	(revision 5868)
+++ /trunk/archive/p2tools/doc/pipeline.txt	(revision 5868)
@@ -0,0 +1,24 @@
+
+Phase 2 pipeline tools:
+
+p2define -time (start) (stop) -camera (camera) -region (ra,dec) (ra,dec)
+  * input: searches mddb:raw_exposures,raw_images
+  * output: updates mddb:P2_exposures_pending,P2_images_pending
+  * alternative output: identical to p2pending
+ 
+p2pending 
+  * input: searches mddb:P2_exposures_pending,P2_images_pending
+  * output: Nlines consisting of:
+    (expID) (class) (URL) 
+  * options: ?
+
+(place both of these in a single user tool, p2search)
+
+p2search -quick [options]
+p2search -define [options]
+p2search -pending [options]
+p2search -update [options]
+
+ppImage file://path/filename file://path/outroot -recipe (recipe) 
+ppImage neb://nebname neb://outroot -recipe (recipe)
+
Index: /trunk/archive/p2tools/src/p2search.c
===================================================================
--- /trunk/archive/p2tools/src/p2search.c	(revision 5868)
+++ /trunk/archive/p2tools/src/p2search.c	(revision 5868)
@@ -0,0 +1,80 @@
+# include "p2search.h"
+
+typedef enum {
+    P2_MODE_NONE,		      	// grab from raw, output for ppImage
+    P2_MODE_QUICK,		      	// grab from raw, output for ppImage
+    P2_MODE_DEFINE,			// grab from raw, create pending
+    P2_MODE_PENDING,			// grab from pending, output for ppImage
+    P2_MODE_UPDATE,			// set the current state
+} p2Mode;
+
+typedef struct {
+    p2Mode mode;
+    psTime start;
+    psTime stop;
+    psSphere r1;
+    psSphere r2;
+    char *camera;
+    char *filter;
+} p2Config;
+
+// this structure should be defined using the mddb api tools
+typedef struct {
+    char expID[32];			// free-form exposure ID string
+    char class;				// class of file: fpa (0), chip (1), cell (2), readout (3)?
+    int Nclass;				// number of files of this class
+    char camera[32];			// free-form camera ID string
+    char filter[32];			// free-form filter ID string
+    psTime time_start;			// exposure start time
+    psTime time_stop;			// exposure stop time
+    psTime exptime;			// exposure duration (seconds)
+    char exptype;			// type of exposure: object (0), bias (1), dark (2), flat (3), etc
+    float sky;				// measure sky brightness
+    float seeing;			// measured image quality 
+    float dettemp;			// temperature of detector
+} p2RawExposure;
+
+// this structure should be defined using the mddb api tools
+typedef struct {
+    char expID[32];			// free-form exposure ID string
+    char class;				// class of file: fpa (0), chip (1), cell (2), readout (3)?
+    char classID[32];			// identifier for class (chip00, cell00, etc)
+    char url[128];			// locator for file (neb / file)
+    char state;				// current load state
+} p2RawImage;
+
+typedef struct {
+    p2RawExposure *exposure;
+    psArray *images;
+} p2RawSet;
+
+int main (int argc, char **argv) {
+
+    p2SearchConfig (&config, argc, argv);
+
+    if ((config->mode == P2_MODE_QUICK) || (config->mode == P2_MODE_DEFINE)) {
+	rawSet = p2SearchRawImages (config);
+    }
+
+    if (config->mode == P2_MODE_DEFINE) {
+	p2InsertRawSet (config, rawSet);
+    }
+
+    if ((config->mode == P2_MODE_PENDING) || (config->mode == P2_MODE_UPDATE)) {
+	pendingSet = p2SearchPendingImages (config);
+    }
+
+    if (config->mode == P2_MODE_QUICK) {
+	pendingSet = p2RawToPending (config, rawSet);
+    }
+
+    if ((config->mode == P2_MODE_QUICK) || (config->mode == P2_MODE_PENDING)) {
+	p2WritePending (config, pendingSet);
+    }
+
+    if (config->mode == P2_MODE_UPDATE) {
+	p2UpdatePending (config, pendingSet);
+    }
+
+    exit (0);
+}
Index: /trunk/archive/p2tools/src/p2searchConfig.c
===================================================================
--- /trunk/archive/p2tools/src/p2searchConfig.c	(revision 5868)
+++ /trunk/archive/p2tools/src/p2searchConfig.c	(revision 5868)
@@ -0,0 +1,63 @@
+# include "p2search.h"
+
+bool p2SearchConfig (psConfig *config, int argc, char **argv) {
+
+    // Parse the configurations (re-org a la ppImage)
+    config->site = NULL;            // Site configuration
+    config->camera = NULL;          // Camera configuration
+    config->recipe = NULL;          // Recipe configuration
+    if (! pmConfigRead(&config->site, &config->camera, &config->recipe, &argc, argv, RECIPE)) {
+        psErrorStackPrint(stderr, "Can't find site configuration!\n");
+        exit(EXIT_FAILURE);
+    }
+
+    // Parse other command-line arguments
+    config->arguments = psMetadataAlloc(); // The arguments, with default values
+
+    config->mode = P2_MODE_NONE;
+    if ((N = psArgumentGet (*argc, argv, "-quick"))) {
+	psArgumentRemove (N, argc, argv);
+	if (config->mode) psAbort ("p2search", "only one mode selection is allowed");
+	config->mode = P2_MODE_QUICK;
+    }
+    if ((N = psArgumentGet (*argc, argv, "-define"))) {
+	psArgumentRemove (N, argc, argv);
+	if (config->mode) psAbort ("p2search", "only one mode selection is allowed");
+	config->mode = P2_MODE_DEFINE;
+    }
+    if ((N = psArgumentGet (*argc, argv, "-pending"))) {
+	psArgumentRemove (N, argc, argv);
+	if (config->mode) psAbort ("p2search", "only one mode selection is allowed");
+	config->mode = P2_MODE_PENDING;
+    }
+    if ((N = psArgumentGet (*argc, argv, "-update"))) {
+	psArgumentRemove (N, argc, argv);
+	if (config->mode) psAbort ("p2search", "only one mode selection is allowed");
+	config->mode = P2_MODE_UPDATE;
+    }
+
+    // paul's argument parsing convention requires: -key value 
+    psMetadataAddStr(config->arguments, PS_LIST_TAIL, "-quick",   0, "examine raw image table, write ppImage output", "");
+    psMetadataAddStr(config->arguments, PS_LIST_TAIL, "-define",  0, "examine raw image table, add to pending image table", "");
+    psMetadataAddStr(config->arguments, PS_LIST_TAIL, "-pending", 0, "examine pending image table, write ppImage output", "");
+    psMetadataAddStr(config->arguments, PS_LIST_TAIL, "-update",  0, "update pending image table", "");
+    psMetadataAddS32(config->arguments, PS_LIST_TAIL, "-time",    0, "define time range of interest", "");
+    psMetadataAddS32(config->arguments, PS_LIST_TAIL, "-camera",  0, "define camera of interest", "");
+    psMetadataAddS32(config->arguments, PS_LIST_TAIL, "-filter",  0, "define filter of interest", "");
+
+    if (! psArgumentParse(config->arguments, &argc, argv) || argc != 2) {
+        printf("\nPan-STARRS Phase 2 Search Tool\n\n");
+        printf("Usage: %s [mode] [options]\n\n", argv[0]);
+        printf(" [mode] : -quick | -define | -pending | -update\n\n");
+        psArgumentHelp(config->arguments);
+        psFree(config->arguments);
+        exit(EXIT_FAILURE);
+    }
+
+    // add the input and output images to the arguments list
+    psMetadataAddStr (config->arguments, PS_LIST_TAIL, "-output", 0, "Name of the output image", argv[1]);
+
+    // define Database handle, if used
+    config->database = pmConfigDB(config->site);
+    return true;
+} 
Index: /trunk/archive/p2tools/src/p2searchRawImages.c
===================================================================
--- /trunk/archive/p2tools/src/p2searchRawImages.c	(revision 5868)
+++ /trunk/archive/p2tools/src/p2searchRawImages.c	(revision 5868)
@@ -0,0 +1,49 @@
+# include "p2search.h"
+
+// load these values from the db in the init stage
+# define P2_TYPE_OBJECT 1
+# define P2_STATE_READY 2
+
+bool p2SearchRawImages (p2SearchConfig *config) {
+
+    // build 'where' structure
+
+    psMetadata *where = psMetadataAlloc ();
+
+    psMetadataAddU8  (where, PS_LIST_TAIL, "TYPE",  0, "==", P2_TYPE_OBJECT);
+    psMetadataAddStr (where, PS_LIST_TAIL, "STATE", 0, "==", P2_START_READY);
+
+    if (config->camera != NULL) {
+	psMetadataAddStr (where, PS_LIST_TAIL, "CAMERA", 0, "==", config->camera);
+    }
+
+    if (config->filter != NULL) {
+	psMetadataAddStr (where, PS_LIST_TAIL, "FILTER", 0, "==", config->filter);
+    }
+
+    if (!psTimeIsZero(config->start) && !psTimeIsZero(config->stop)) {
+	psMetadataAddTime (where, PS_LIST_TAIL, "TIME_START", 0, "<", config->time_stop);
+	psMetadataAddTime (where, PS_LIST_TAIL, "TIME_STOP", 0, ">", config->time_start);
+    }
+
+    psArray *exposures = p2RawExposureSelectRows (config->database, where, MAX_ROWS);
+
+    psFree (where);
+
+    psMetadata *where = psMetadataAlloc ();
+
+    for (int i = 0; i < exposures->n; i++) {
+	p2RawExposure *exposure = exposures->data[i];
+	
+	psMetadataAddStr (where, PS_LIST_TAIL, "EXP_ID", PS_META_REPLACE, "==", exposure->expID);
+
+	psArray *images = p2RawImageSelectRows (config->database, where, MAX_ROWS);
+
+	p2rawSet->exposure = exposure;
+	p2rawSet->images = images;
+
+	psArrayAdd (rawSet, 100, p2rawSet);
+    }
+
+    return true;
+} 
