/*****************************************************************************
******************************************************************************
**<AUTO>
**
** FILE:	tclTargets.cc
**
**<HTML>
**	This file contains TCL functions to select spectroscopic targets.
**</HTML>
**</AUTO>
**
**
** ENVIRONMENT:
**	C++.
**
** REQUIRED PRODUCTS:
**	photoSch
**
******************************************************************************
******************************************************************************
*/

#include <arProduct.h>
#include <arChunk.h>
#include <arSegment.h>
#include <dervish.h>
#include <taCosmic.h>
#include "tsTarget.h"

/*============================================================================
**<AUTO EXTRACT>
**
** TCL VERB: dbTargetSelectFromSegment
**
**<HTML>
**	Select spectroscopic targets from all primary objects in a segment.
**	Optionally, a frames pipeline run handle can be passed in for the
**	segment handle, in which case targets are selected from all OK_RUN
**	objects in the frames pipeline run.  When selecting from a frames
**	pipeline run, the "-noUpdate" option must be specified.
**</HTML>
**
**</AUTO>
**============================================================================
*/
static char dbTargetSelectFromSegmentCmd[] = "dbTargetSelectFromSegment";
static int dbTargetSelectFromSegmentFlg = FTCL_ARGV_NO_LEFTOVERS;
static ftclArgvInfo dbTargetSelectFromSegmentTbl[] = {
   {NULL, FTCL_ARGV_HELP, NULL, NULL,
    "Select spectroscopic targets from all primary objects in a segment/run\n"},
   {"<segment>", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to the segment/frames-pipeline-run"},
   {"-galaxies", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to Galaxies tunable parameters (TA_GALAXIES_PARAMS"},
   {"-quasars", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to Quasars tunable parameters (TA_QUASARS_PARAMS"},
   {"-serendipity", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to Serendipity tunable parameters (TA_SERENDIPITY_PARAMS"},
   {"-rosat", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to ROSAT tunable parameters (TA_ROSAT_PARAMS"},
   {"-stars", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to Stars tunable parameters (TA_STARS_PARAMS"},
   {"-standards", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to Standards tunable parameters (TA_STANDARDS_PARAMS"},
   {"-first", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to FIRST catalog (TA_FIRST_CATALOG)"},
   {"-crosat", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to ROSAT catalog (TA_ROSAT_CATALOG)"},
   {"-usno", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to USNO catalog (TA_USNO_CATALOG)"},
   {"-export", FTCL_ARGV_INT, NULL, NULL,
    "Export tsObjc files --- 0=field files only, 1=targets only, 2=primaries only, 3=primaries and secondaries, 4=everything"},
   {"-parents", FTCL_ARGV_CONSTANT, (void *) 1, NULL,
    "If exporting, export parent objects as well"},
   {"-noSplit", FTCL_ARGV_CONSTANT, (void *) 1, NULL,
    "If exporting, export one file, rather than one file per field (defaults to 1 file per field)"},
   {"-outputDir", FTCL_ARGV_STRING, NULL, NULL,
    "Directory to create exported tsObjc files in (defaults to current)"},
   {"-force", FTCL_ARGV_CONSTANT, (void *) 1, NULL,
    "Force re-selection if chunk previously through target selection"},
   {"-field0", FTCL_ARGV_INT, NULL, NULL,
    "First field to select on (defaults to first field in the segment/run) --- use for debugging only"},
   {"-nFields", FTCL_ARGV_INT, NULL, NULL,
    "Select on this number of fields only (starting with first field, default=0=all) --- use for debugging only"},
   {"-noUpdate", FTCL_ARGV_CONSTANT, (void *) 1, NULL,
    "Don't update the OPDB --- useful for generating test tsObj files"},
   {NULL, FTCL_ARGV_END, NULL, NULL, NULL}
};

static int
tclDbTargetSelectFromSegment (ClientData clientData,
		   Tcl_Interp *interp,
		   int argc,
		   char **argv)
{
  ooHandle(arSegment) *segment = NULL;
  ooHandle(arFramesPipeRun) *fpr = NULL;
  char *segmentHandle = NULL;
  TA_GALAXIES_PARAMS *galaxies = NULL;
  char *galaxiesHandle = NULL;
  TA_QUASARS_PARAMS *quasars = NULL;
  char *quasarsHandle = NULL;
  TA_SERENDIPITY_PARAMS *serendipity = NULL;
  char *serendipityHandle = NULL;
  TA_ROSAT_PARAMS *rosat = NULL;
  char *rosatHandle = NULL;
  TA_STARS_PARAMS *stars = NULL;
  char *starsHandle = NULL;
  TA_STANDARDS_PARAMS *standards = NULL;
  char *standardsHandle = NULL;
  TA_FIRST_CATALOG *firstCat = NULL;
  char *firstCatHandle = NULL;
  TA_ROSAT_CATALOG *rosatCat = NULL;
  char *rosatCatHandle = NULL;
  TA_USNO_CATALOG *usnoCat = NULL;
  char *usnoCatHandle = NULL;
  int export = -1;
  int parents = 0;
  int noSplit = 0;
  char *outputDir = NULL;
  int force = 0;
  int field0 = -1;
  int nFields = 0;
  int noUpdate = 0;
  int status;

  // Parse the arguments
  dbTargetSelectFromSegmentTbl[1].dst = &segmentHandle;
  dbTargetSelectFromSegmentTbl[2].dst = &galaxiesHandle;
  dbTargetSelectFromSegmentTbl[3].dst = &quasarsHandle;
  dbTargetSelectFromSegmentTbl[4].dst = &serendipityHandle;
  dbTargetSelectFromSegmentTbl[5].dst = &rosatHandle;
  dbTargetSelectFromSegmentTbl[6].dst = &starsHandle;
  dbTargetSelectFromSegmentTbl[7].dst = &standardsHandle;
  dbTargetSelectFromSegmentTbl[8].dst = &firstCatHandle;
  dbTargetSelectFromSegmentTbl[9].dst = &rosatCatHandle;
  dbTargetSelectFromSegmentTbl[10].dst = &usnoCatHandle;
  dbTargetSelectFromSegmentTbl[11].dst = &export;
  dbTargetSelectFromSegmentTbl[12].dst = &parents;
  dbTargetSelectFromSegmentTbl[13].dst = &noSplit;
  dbTargetSelectFromSegmentTbl[14].dst = &outputDir;
  dbTargetSelectFromSegmentTbl[15].dst = &force;
  dbTargetSelectFromSegmentTbl[16].dst = &field0;
  dbTargetSelectFromSegmentTbl[17].dst = &nFields;
  dbTargetSelectFromSegmentTbl[18].dst = &noUpdate;
  if ((status = shTclParseArgv(interp, &argc, argv, dbTargetSelectFromSegmentTbl,
			       dbTargetSelectFromSegmentFlg,
			       dbTargetSelectFromSegmentCmd))
      != FTCL_ARGV_SUCCESS)
    return status;
   
  // Get handle to segment/run
  ooHandle(arSegment) dummySegment;
  ooHandle(arFramesPipeRun) dummyFPR;
  if (shTclAddrGetFromName (interp, segmentHandle, (void **)&segment,
			    "ooHandle(arSegment)") != TCL_OK)
    {
      // Not a segment.  Must be a frames pipeline run.
      if (shTclAddrGetFromName (interp, segmentHandle, (void **)&fpr,
				"ooHandle(arFramesPipeRun)") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to segment/run", NULL);
	  return TCL_ERROR;
	}
      segment = &dummySegment;
    }
  else
    fpr = &dummyFPR;
   
  /* Get handle for each specified set of tunable parameters */
  if (ftcl_ArgIsPresent(argc, argv,"-galaxies",dbTargetSelectFromSegmentTbl) ==1)
    {
      /* Get handle to galaxies parameters */
      if (shTclAddrGetFromName (interp, galaxiesHandle, (void **)&galaxies,
				"TA_GALAXIES_PARAMS") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to galaxies params", NULL);
	  return TCL_ERROR;
	}
    }
  if (ftcl_ArgIsPresent(argc, argv,"-quasars",dbTargetSelectFromSegmentTbl) == 1)
    {
      /* Get handle to quasars parameters */
      if (shTclAddrGetFromName (interp, quasarsHandle, (void **)&quasars,
				"TA_QUASARS_PARAMS") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to quasars params", NULL);
	  return TCL_ERROR;
	}
    }
  if (ftcl_ArgIsPresent(argc,argv,"-serendipity",dbTargetSelectFromSegmentTbl)
      == 1)
    {
      /* Get handle to serendipity parameters */
      if (shTclAddrGetFromName (interp,serendipityHandle,(void **)&serendipity,
				"TA_SERENDIPITY_PARAMS") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to serendipity params",
			   NULL);
	  return TCL_ERROR;
	}
    }
  if (ftcl_ArgIsPresent(argc,argv,"-rosat",dbTargetSelectFromSegmentTbl)== 1)
    {
      /* Get handle to rosat parameters */
      if (shTclAddrGetFromName (interp,rosatHandle,(void **)&rosat,
				"TA_ROSAT_PARAMS") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to rosat params",
			   NULL);
	  return TCL_ERROR;
	}
    }
  if (ftcl_ArgIsPresent(argc, argv, "-stars", dbTargetSelectFromSegmentTbl) == 1)
    {
      /* Get handle to stars parameters */
      if (shTclAddrGetFromName (interp, starsHandle, (void **)&stars,
				"TA_STARS_PARAMS") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to stars params", NULL);
	  return TCL_ERROR;
	}
    }
  if (ftcl_ArgIsPresent(argc,argv,"-standards",dbTargetSelectFromSegmentTbl)== 1)
    {
      /* Get handle to standards parameters */
      if (shTclAddrGetFromName (interp, standardsHandle, (void **)&standards,
				"TA_STANDARDS_PARAMS") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to standards params", NULL);
	  return TCL_ERROR;
	}
    }
  if (ftcl_ArgIsPresent(argc, argv, "-first", dbTargetSelectFromSegmentTbl) == 1)
    {
      /* Get handle to FIRST catalog */
      if (shTclAddrGetFromName (interp, firstCatHandle, (void **)&firstCat,
				"TA_FIRST_CATALOG") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to FIRST catalog", NULL);
	  return TCL_ERROR;
	}
    }
  if (ftcl_ArgIsPresent(argc, argv, "-crosat",dbTargetSelectFromSegmentTbl) == 1)
    {
      /* Get handle to ROSAT catalog */
      if (shTclAddrGetFromName (interp, rosatCatHandle, (void **)&rosatCat,
				"TA_ROSAT_CATALOG") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to ROSAT catalog", NULL);
	  return TCL_ERROR;
	}
    }
  if (ftcl_ArgIsPresent(argc, argv, "-usno",dbTargetSelectFromSegmentTbl) == 1)
    {
      /* Get handle to USNO catalog */
      if (shTclAddrGetFromName (interp, usnoCatHandle, (void **)&usnoCat,
				"TA_USNO_CATALOG") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
			   ": error getting handle to USNO catalog", NULL);
	  return TCL_ERROR;
	}
    }

  // Fetch the global TCL variable "photo_targets" for container with all
  // spectroscopic targets.  Then fetch the associated handle.  We'll use
  // this as a clustering directive when creating new targets.
  char *photo_targets = Tcl_GetVar(interp, "photo_targets", TCL_GLOBAL_ONLY);
  if (photo_targets == NULL)
    {
      Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
		       ": global variable $photo_targets doesn't exist",
		       NULL);
      return TCL_ERROR;
    }
  ooHandle(ooContObj) *targetCluster;
  if (shTclAddrGetFromName (interp, photo_targets, (void **)&targetCluster,
			    "ooHandle(ooContObj)") != TCL_OK)
    {
      Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
		       ": error getting handle to $photo_targets", NULL);
      return TCL_ERROR;
    }

  // Fetch the global TCL variable "photo_first" for container with all
  // FIRST composite objects.  Then fetch the associated handle.  We'll use
  // this as a clustering directive when creating new FIRST objects.
  char *photo_first = Tcl_GetVar(interp, "photo_first", TCL_GLOBAL_ONLY);
  if (photo_first == NULL)
    {
      Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
		       ": global variable $photo_first doesn't exist",
		       NULL);
      return TCL_ERROR;
    }
  ooHandle(ooContObj) *firstCluster;
  if (shTclAddrGetFromName (interp, photo_first, (void **)&firstCluster,
			    "ooHandle(ooContObj)") != TCL_OK)
    {
      Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
		       ": error getting handle to $photo_first", NULL);
      return TCL_ERROR;
    }

  // Fetch the global TCL variable "photo_rosat" for container with all
  // ROSAT objects.  Then fetch the associated handle.  We'll use
  // this to fetch handles to matched ROSAT objects.
  char *photo_rosat = Tcl_GetVar(interp, "photo_rosat", TCL_GLOBAL_ONLY);
  if (photo_rosat == NULL)
    {
      Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
		       ": global variable $photo_rosat doesn't exist",
		       NULL);
      return TCL_ERROR;
    }
  ooHandle(ooContObj) *rosatCluster;
  if (shTclAddrGetFromName (interp, photo_rosat, (void **)&rosatCluster,
			    "ooHandle(ooContObj)") != TCL_OK)
    {
      Tcl_AppendResult(interp, dbTargetSelectFromSegmentCmd,
		       ": error getting handle to $photo_rosat", NULL);
      return TCL_ERROR;
    }

  // Set up cosmic color for use by atTransApply().
  taTransCosmicSet(cosmicColor, cosmicScatter);

  // Select targets.
  int select = 1;
  if (tsSegmentCalibrate(*segment, *fpr, field0, nFields, export, parents,
			 !noSplit, select, force, noUpdate, outputDir,
			 targetCluster, firstCluster, rosatCluster,
			 galaxies, quasars, serendipity,
			 rosat, stars, standards, firstCat, rosatCat,
			 usnoCat)
      != SH_SUCCESS)
    {
      shTclInterpAppendWithErrStack(interp);
      return TCL_ERROR;
    }

  // Return
  Tcl_SetResult(interp, "", TCL_VOLATILE);
  return TCL_OK;
}

/*============================================================================
**<AUTO EXTRACT>
**
** TCL VERB: dbTargetUnselectFromChunk
**
**<HTML>
**	Unselect spectroscopic targets previously selected in a chunk.
**</HTML>
**
**</AUTO>
**============================================================================
*/
static char dbTargetUnselectFromChunkCmd[] = "dbTargetUnselectFromChunk";
static int dbTargetUnselectFromChunkFlg = FTCL_ARGV_NO_LEFTOVERS;
static ftclArgvInfo dbTargetUnselectFromChunkTbl[] = {
   {NULL, FTCL_ARGV_HELP, NULL, NULL,
    "Unselect spectroscopic targets previously selected in a chunk\n"},
   {"<chunk>", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to chunk"},
   {"-force", FTCL_ARGV_CONSTANT, (void *) 1, NULL,
    "Force unselection regardless"},
   {NULL, FTCL_ARGV_END, NULL, NULL, NULL}
};

static int
tclDbTargetUnselectFromChunk (ClientData clientData,
			      Tcl_Interp *interp,
			      int argc,
			      char **argv)
{
  ooHandle(arChunk) *chunk = NULL;
  char *chunkHandle = NULL;
  int force = 0;
  int status;

  // Parse the arguments
  dbTargetUnselectFromChunkTbl[1].dst = &chunkHandle;
  dbTargetUnselectFromChunkTbl[2].dst = &force;
  if ((status = shTclParseArgv(interp, &argc, argv,
			       dbTargetUnselectFromChunkTbl,
			       dbTargetUnselectFromChunkFlg,
			       dbTargetUnselectFromChunkCmd))
      != FTCL_ARGV_SUCCESS)
    return status;
   
  // Get handle to chunk
  if (shTclAddrGetFromName (interp, chunkHandle, (void **)&chunk,
			    "ooHandle(arChunk)") != TCL_OK)
    {
      if (shTclAddrGetFromName (interp, chunkHandle, (void **)&chunk,
				"ooItr(arChunk)") != TCL_OK)
	{
	  Tcl_AppendResult(interp, dbTargetUnselectFromChunkCmd,
			   ": error getting handle to chunk", NULL);
	  return TCL_ERROR;
	}
    }
  
  // Verify that this chunk has been through target selection
  if (! force)
    if ((*chunk)->selectTargets().isValid() == oocFalse)
      {
	Tcl_AppendResult(interp, dbTargetUnselectFromChunkCmd,
			 ": chunk has not been through target selection",
			 NULL);
	return TCL_ERROR;
      }
   
  // Delete targeting report.
  ooHandle(arProduct) report = (*chunk)->selectTargets();
  ooDelete(report);

  // Initialize iterator over segments
  ooItr(arSegment) itr;
  if ((*chunk)->segments(itr, oocUpdate) != oocSuccess)
    {
      Tcl_AppendResult(interp, dbTargetUnselectFromChunkCmd,
		       ": couldn't initialize iterator over segments");
      return TCL_ERROR;
    }

  // For each segment ...
  while (itr.next())
    {
      // Targets selected in primary segments only.
      if (itr->status() != AR_SEGMENT_STATUS_PRIMARY)
	continue;
      
      // Unselect targets.
      if (tsSegmentTargetUnselect(itr) != SH_SUCCESS)
	{
	  shTclInterpAppendWithErrStack(interp);
	  return TCL_ERROR;
	}
    }

  // Return
  Tcl_SetResult(interp, "", TCL_VOLATILE);
  return TCL_OK;
}

/*============================================================================
**<AUTO EXTRACT>
**
** TCL VERB: segmentExport
**
**<HTML>
**	Export calibrated versions of all primary objects in a segment
**	to FITS files.  Optionally, a handle to a frames pipeline run may
**	be specified rather than a segment, in which case that run is
**	exported (all OK_RUN objects are exported).
**</HTML>
**
**</AUTO>
**============================================================================
*/
static char segmentExportCmd[] = "segmentExport";
static int segmentExportFlg = FTCL_ARGV_NO_LEFTOVERS;
static ftclArgvInfo segmentExportTbl[] = {
   {NULL, FTCL_ARGV_HELP, NULL, NULL,
    "Export calibrated versions of all primary objects in a segment to FITS files\n"},
   {"<segment>", FTCL_ARGV_STRING, NULL, NULL,
    "Handle to segment or frames pipeline run"},
   {"<outputDir>", FTCL_ARGV_STRING, NULL, NULL,
    "Output directory for tsObj files"},
   {"[export]", FTCL_ARGV_INT, NULL, NULL,
    "Export option --- 0=field files only, 1=targets only, 2=primaries only (default), 3=primaries and secondaries, 4=everything"},
   {"-parents", FTCL_ARGV_CONSTANT, (void *) 1, NULL,
    "Export parent objects as well"},
   {"-noSplit", FTCL_ARGV_CONSTANT, (void *) 1, NULL,
    "One file, rather than one file per field (defaults to 1 file per field)"},
   {"-field0", FTCL_ARGV_INT, NULL, NULL,
    "First field to export (defaults to first field in the segment/run)"},
   {"-nFields", FTCL_ARGV_INT, NULL, NULL,
    "Export this number of fields only (starting with first field, default=0=all)"},
   {NULL, FTCL_ARGV_END, NULL, NULL, NULL}
};

static int
tclSegmentExport (ClientData clientData,
		  Tcl_Interp *interp,
		  int argc,
		  char **argv)
{
  ooHandle(arSegment) *segment = NULL;
  ooHandle(arFramesPipeRun) *fpr = NULL;
  char *segmentHandle = NULL;
  char *outputDir = NULL;
  int export = 2;
  int parents = 0;
  int noSplit = 0;
  int field0 = -1;
  int nFields = 0;
  int status;

  // Parse the arguments
  segmentExportTbl[1].dst = &segmentHandle;
  segmentExportTbl[2].dst = &outputDir;
  segmentExportTbl[3].dst = &export;
  segmentExportTbl[4].dst = &parents;
  segmentExportTbl[5].dst = &noSplit;
  segmentExportTbl[6].dst = &field0;
  segmentExportTbl[7].dst = &nFields;
  if ((status = shTclParseArgv(interp, &argc, argv, segmentExportTbl,
			       segmentExportFlg, segmentExportCmd))
      != FTCL_ARGV_SUCCESS)
    return status;
   
  // Get handle to segment/run
  ooHandle(arSegment) dummySegment;
  ooHandle(arFramesPipeRun) dummyFPR;
  if (shTclAddrGetFromName (interp, segmentHandle, (void **)&segment,
			    "ooHandle(arSegment)") != TCL_OK)
    {
      // Not a segment.  Must be a frames pipeline run.
      if (shTclAddrGetFromName (interp, segmentHandle, (void **)&fpr,
				"ooHandle(arFramesPipeRun)") != TCL_OK)
	{
	  Tcl_AppendResult(interp, segmentExportCmd,
			   ": error getting handle to segment/run", NULL);
	  return TCL_ERROR;
	}
      segment = &dummySegment;
    }
  else
    fpr = &dummyFPR;

  // Set up cosmic color for use by atTransApply().
   taTransCosmicSet(cosmicColor, cosmicScatter);

  // Export calibrated versions of the primary objects
  int select = 0;
  int force = 0;
  int noUpdate = 1;
  if (tsSegmentCalibrate(*segment, *fpr, field0, nFields, export, parents,
			 !noSplit, select, force, noUpdate, outputDir)
      != SH_SUCCESS)
    {
      shTclInterpAppendWithErrStack(interp);
      return TCL_ERROR;
    }

  // Return
  Tcl_SetResult(interp, "", TCL_VOLATILE);
  return TCL_OK;
}

//****************************************************************************/
//
// Declare my new tcl verbs to tcl
//
void
tsTclTargetsDeclare(Tcl_Interp *interp) 
{
  char *tclHelpFacil = "ts";
  shTclDeclare(interp, dbTargetSelectFromSegmentCmd,
	       (Tcl_CmdProc *)tclDbTargetSelectFromSegment,
	       (ClientData) 0,	(Tcl_CmdDeleteProc *)NULL, tclHelpFacil,
	       shTclGetArgInfo(interp, dbTargetSelectFromSegmentTbl,
			       dbTargetSelectFromSegmentFlg,
			       dbTargetSelectFromSegmentCmd),
	       shTclGetUsage(interp, dbTargetSelectFromSegmentTbl,
			     dbTargetSelectFromSegmentFlg,
			     dbTargetSelectFromSegmentCmd));
  shTclDeclare(interp, dbTargetUnselectFromChunkCmd,
	       (Tcl_CmdProc *)tclDbTargetUnselectFromChunk,
	       (ClientData) 0,	(Tcl_CmdDeleteProc *)NULL, tclHelpFacil,
	       shTclGetArgInfo(interp, dbTargetUnselectFromChunkTbl,
			       dbTargetUnselectFromChunkFlg,
			       dbTargetUnselectFromChunkCmd),
	       shTclGetUsage(interp, dbTargetUnselectFromChunkTbl,
			     dbTargetUnselectFromChunkFlg,
			     dbTargetUnselectFromChunkCmd));
  shTclDeclare(interp, segmentExportCmd,
	       (Tcl_CmdProc *)tclSegmentExport,
	       (ClientData) 0,	(Tcl_CmdDeleteProc *)NULL, tclHelpFacil,
	       shTclGetArgInfo(interp, segmentExportTbl,
			       segmentExportFlg,
			       segmentExportCmd),
	       shTclGetUsage(interp, segmentExportTbl,
			     segmentExportFlg,
			     segmentExportCmd));
}
