Give me a list of all pending exposures which could have phase 4 run:
	p4tool -query -survey_mode <str> -filter <str> -ra <f64> -decl <f64> (plus other search options)

The "p4tool -definerun" command creates a new p4Run for an exposure. A
unique identifier for the run, or a p4Run ID, is returned:

	p4tool -definerun -exp_tag <str> -mode <str> -workdir <str>
	p4tool -addinputscfile ...
	p4tool -updaterun -p4_id <s32> -mode run

Give me all p4runs to have overlaps calculated:
        p4tool -tooverlap

Calculate overlaps for each exposure:
        phase4_overlap.pl --p4_id <s32>
{
        Get list of component imfiles for exposure:
                p4tool -imfile -p4_id <s32>

        (Calculates, in some as-yet unknown way the overlaps between imfiles
and skycells)

        Set the relationship between imfiles and skycells:
                p4tool -addoverlap -p4_id <s32> -info FILE
        where FILE contains:

        p4SkyCellMap MUTLI

        p4SkyCellMap METDATA
            p4_id           S32     1
            skycell_id      STR     foo
            tess_id         STR     bar
            exp_tag         STR     baz
            p3_version      S32     0
            class_id        STR     quix
        END

        ...
}

Give me all skycells that need to have data warped to them:
        p4tool -towarped

Do the warp for each skycell:
        phase4_warp.pl --p4_id <s32> --skycell <str>
{
        Get list of imfiles for skycell:
                p4tool -scmap -p4_id <s32> -skycell <str>

        Do the warping:
                pswarp <something>

        Register the skycell as done warping:
                p4tool -addwarped -p4_id <s32> -skycell <str> -uri <str>
}

p4tool -updaterun -p4_id <s32> -mode stop

================================================================================

Give me a list of all skycells which could have subtractions performed
(more than two warps):
#	p5tool -query (some search parameters, including RA, Dec, seeing)

Register an exposure for subtraction processing.  We need to specify which warp
is the input and which is the template.

	p5tool -definerun -workdir <str>

    p5tool -addinputscfile -p5_id <s32> -p4_id <s32> -skycell_id <str> -tess_id <str> -kinds <str> [-template]

	p5tool -updaterun -p5_id <s32> -state run

Give me all skycells that need to be subtracted:
        p5tool -todiffscfile

Do the subtraction for each skycell:
        phase5_subtract.pl --p5_id <s32>
{
        p5tool -inputscfile -p5_id <s32>

        Do the subtraction:
                pois in_uri.fits template_uri sub.fits <other stuff>

        register the resulting difference image:

        p5tool -adddiffscfile -p5_id <s32> -skycell_id <str> -tess_id <str>
-uri <str> ...

        Register the subtraction as done:
                p5tool -updaterun -p5_id <s32> -state stop
}

================================================================================

[Here I make some assumptions about how magic works]

Get a list of exposures for which magic processing is possible
	magictool -query (some search options)

There may be multiple subtractions generated out of each exposure.
Given an exposure, what subtractions do I have available?

	magictool -options -exp_tag <str>

Register an exposure for magic processing, specifying a subtraction set:
	magictool -definerun -exp_tag <str> -subs FILE

	where FILE contains something like:
		skycell.123	  STR	    p5id.1
		skycell.456	  STR	    p5id.2
		skycell.789	  STR	    p5id.3

Give me all skycells that need to have magic performed:
        magictool -pendingskycell

Do magic for each skycell:
        magic_skycell.pl --magic_id <s32> --skycell <str> --uri <str>
{
        Do streak finding on skycell

        Register sky cell as done:
                magictool -doneskycell -magic_id <s32> -skycell <str> -uri <str>
}

Give me all imfiles that need to have magic performed:
        magictool -pendingimfile

Do magic for each imfile:
        magic_imfile.pl --magic_id <s32> --class_id <str>
{
        Get list of all magic1 output relevant to this imfile
                magictool -skycell -magic_id <s32> -class_id <str>

        Do streak finding for imfile

        Register imfile as done:
                magictool -doneimfile -magic_id <s32> -class_id <str> -uri <str>
}

[Might be a mid-level process or processes, halfway between imfiles and
full exposures, but for now, assume there's not, or that it can all be
done quickly at the exposure level.]

Give me all exposures that need to have magic performed:
        magictool -pendingexp

Do magic for each exposure:
        magic_exp.pl --magic_id <s32>
{
        Get list of all magic2 output relevant to this exposure
                magictool -imfile -magic_id <s32>

        Do streak finding on exposure

        Register exposure as done:
                magictool -doneexp -magic_id <s32> -uri <str>
}


[Need to generate masks, but we need to decide at what level: imfile,
or skycell?  If imfile, we need to do the warp over again.  If
skycell, we can never give out the flattened CCD images unless they've
been warped.  Let's do both --- it's not too expensive, and means we
can release both without further work.]


Give me all imfiles that need a magic mask:
	magictool -pendingmaskimfile

Generate mask for each imfile:
	magic_mask_imfile.pl --magic_id <s32> --class_id <str> --uri <str>
{
	Make mask

	Register mask:
		magictool -donemaskimfile -magic_id <s32> -class_id <str> -uri <str>
}

Give me all skycells that need a magic mask:
	magictool -pendingmaskskycell

Generate mask for each skycell:
	magic_mask_skycell.pl --magic_id <s32> --skycell <str> --uri <str>
{
	Make mask

	Register mask:
		magictool -donemaskskycell -magic_id <s32> -skycell <str> -uri <str>
}

Give me a list of all exposures which are clean (so we can release):
	magictool -cleanexp (some search criteria)

Give me a list of all imfiles which are clean (so we can release):
	magictool -cleanimfile (some search criteria)

Give me a list of all skycells which are clean (so we can release):
	magictool -cleanskycell (some search criteria)

================================================================================

Give me a list of all skycells for which combination processing is possible:
#	p6tool -query -min <s32> (some other search parameters, including ra, dec, time range)

Register skycells for combination processing:
	p6tool -definerun -skycell <stR>

	p6tool -addinputscfile -p4_id <s32> -skycell_id <str> -tess_id <str> 

Give me all pending operations/runs
	p6tool -tosum

Run phase 6:
	phase6.pl -p6_id <s32>
{
	Give me the list of all inputs for this run:
		p6tool -inputscfile -p6_id <s32>

	Combine:
		ppStac output.fits input1.fits input2.fits ...

	Register the completion:
		p6tool -addsumscfile -p6_id <s32> -uri <str>

		p6tool -updaterun -p6_id <s32> -state stop
}

Give me a list of all stacks (for release):
	p6tool -sumscfile <some search criteria>

