Index: /branches/haf_branches/ipp.20100512/dbconfig/minidvodb.md
===================================================================
--- /branches/haf_branches/ipp.20100512/dbconfig/minidvodb.md	(revision 28170)
+++ /branches/haf_branches/ipp.20100512/dbconfig/minidvodb.md	(revision 28171)
@@ -6,4 +6,5 @@
     mergedvodb_path STR    255
     state           STR    64
+    creation_date   UTC    0001-01-01T00:00:00Z
 END 
 
Index: /branches/haf_branches/ipp.20100512/ippScripts/scripts/minidvodb_createdb.pl
===================================================================
--- /branches/haf_branches/ipp.20100512/ippScripts/scripts/minidvodb_createdb.pl	(revision 28170)
+++ /branches/haf_branches/ipp.20100512/ippScripts/scripts/minidvodb_createdb.pl	(revision 28171)
@@ -202,7 +202,8 @@
    }
     if ($create_new == 0) {
-	my $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%dT%H:%M:%S' );
+	my $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%dT%H:%M:%S', time_zone => "HST" );
 	my $creation_dt = $parser->parse_datetime( $creation_date )->mjd;
-	if ($mjd_start- $creation_dt > $minidvodb_interval && $addRun_count > 0 ) {
+	if ($mjd_start- $creation_dt > $minidvodb_interval) { # && $addRun_count > 0 ) {
+	#if ($mjd_start- $creation_dt > $minidvodb_interval && $addRun_count > 0 ) {
 	    #db is old and has stuff in it, want to create_new 
 	    $create_new = 1;
Index: /branches/haf_branches/ipp.20100512/ippScripts/scripts/minidvodb_merge.pl
===================================================================
--- /branches/haf_branches/ipp.20100512/ippScripts/scripts/minidvodb_merge.pl	(revision 28171)
+++ /branches/haf_branches/ipp.20100512/ippScripts/scripts/minidvodb_merge.pl	(revision 28171)
@@ -0,0 +1,253 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+use Carp;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "\n\n";
+print "Starting script $0 on $host\n\n";
+
+use DateTime;
+my $mjd_start = DateTime->now->mjd;   # MJD of starting script
+my $dtime_resort;
+my $dtime_relphot;
+my $dtime_merge;
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
+use PS::IPP::Config 1.01 qw( :standard );
+use File::Temp qw( tempfile );
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $dvomerge = can_run('dvomerge') or (warn "Can't find dvomerge" and $missing_tools = 1);
+my $addtool = can_run('addtool') or (warn "Can't find addtool" and $missing_tools = 1);
+my $addstar = can_run('addstar') or (warn "Can't find addstar" and $missing_tools = 1);
+my $relphot = can_run('relphot') or (warn "Can't find relphot" and $missing_tools = 1);
+my $relastro = can_run('relastro') or (warn "Can't find relastro" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+my ( $mergedvodb, $minidvodb, $minidvodb_id, $minidvodb_group, $camera, $dbname,$verbose, $logfile, $no_op, $redirect, $save_temps);
+GetOptions(
+    'mergedvodb|w=s'         => \$mergedvodb,  # output DVO database
+    'minidvodb|w=s'     => \$minidvodb, #minidvodb database
+    'minidvodb_id|w=s'  => \$minidvodb_id, #minidvodb_id
+ 'minidvodb_group|w=s'  => \$minidvodb_group, #minidvodb_id
+    'camera|c=s'        => \$camera, # Camera
+    'dbname|d=s'        => \$dbname, # Database name
+    'verbose'           => \$verbose,   # Print to stdout
+    'no-op'             => \$no_op, # Don't do any operations?
+    'logfile=s'         => \$logfile,
+    'save-temps'        => \$save_temps, # Save temporary files?
+    ) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+          -msg => "Required options: --mergedvodb --minidvodb --minidvodb_id -- minidvodb_group --camera",
+          -exitval => 3,
+          ) unless
+    defined $mergedvodb and
+    defined $minidvodb and
+    defined $minidvodb_id and
+    defined $minidvodb_group and
+    defined $camera;
+
+my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $mergedvodb, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
+
+if ($logfile) {
+    $ipprc->redirect_output($logfile) or my_die( "Unable to redirect output", $mergedvodb, $PS_EXIT_SYS_ERROR );
+    print "\n\n";
+    print "Starting script $0 on $host\n\n";
+    print "COMMAND IS: @ARGV\n\n";
+}
+
+# convert supplied DVO database name to UNIX filename
+my $mergedvodbReal;
+if (defined $mergedvodb) {
+    $mergedvodbReal = $ipprc->dvo_catdir( $mergedvodb ); # catdir for DVO
+    $mergedvodbReal = $ipprc->convert_filename_absolute( $mergedvodbReal );
+}
+
+my $dtime_addstar = 0;
+#my $dtime_relphot = 0;
+
+unless ($no_op) {
+    if (defined $mergedvodbReal) {
+	my $mjd_relphot_start = DateTime->now->mjd;
+	my $lame = 1;
+	if ($lame ==2){
+	{
+            my $command  = "$addstar -resort";
+            $command .= " -D CAMERA $camera";
+            $command .= " -D CATDIR $mergedvodbReal";
+
+            my $mjd_addstar_start = DateTime->now->mjd;   # MJD of starting script
+
+            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+            unless ($success) {
+                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                &my_die("Unable to perform addstar: $error_code", $mergedvodb, $error_code);
+            }
+            $dtime_addstar = 86400.0*(DateTime->now->mjd - $mjd_addstar_start);  $dtime_resort = $dtime_addstar;
+	    # MJD of starting script
+            print "addstar -resort time $dtime_addstar\n";
+        }
+
+        {
+            # relphot only takes lower case gpc1
+            my $relphot_camera = lc($camera);
+            my $command  = "$relphot -averages -update";
+            $command .= " -D CAMERA $relphot_camera";
+            $command .= " -D CATDIR $mergedvodbReal";
+
+            my $mjd_relphot_start = DateTime->now->mjd;   # MJD of starting script
+
+            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+            unless ($success) {
+                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                &my_die("Unable to perform relphot: $error_code", $mergedvodb, $error_code);
+            }
+            $dtime_relphot = 86400.0*(DateTime->now->mjd - $mjd_relphot_start);   # MJD of starting script
+            print "relphot time $dtime_relphot\n";
+        }}
+	
+	{
+
+	    my $mdcParser = PS::IPP::Metadata::Config->new;
+	    
+
+	    my $command = "$addtool -listminidvodbrun -state mergedsdfsad -minidvodb_group " . $minidvodb_group;
+	    $command .= " -dbname $dbname" if defined $dbname;
+
+	    
+
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                    run(command => $command, verbose => $verbose);
+	    &my_die( "Unable to get list of minidvodbruns", $minidvodb_id, $PS_EXIT_SYS_ERROR) unless $success;
+	    my $this_is_the_first;
+	    if (scalar @$stdout_buf == 0 ) { #it lists nothing if it is the first
+		$this_is_the_first =1;
+	    } else {
+                 my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+                    &my_die("Unable to parse metadata config", $minidvodb_id, $PS_EXIT_PROG_ERROR);
+		 #this fails if there is nothing listed. I checked.
+                my $components = parse_md_list($metadata) or
+                    &my_die("Unable to parse metadata list", $minidvodb_id, $PS_EXIT_PROG_ERROR);
+                my $comp = $$components[0];
+	   my  $minidvodb_name = $comp->{minidvodb_name};
+        
+                if (!defined($minidvodb_name)) {
+                    &my_die("Unable to parse minidvodb_name", $minidvodb_id, $PS_EXIT_PROG_ERROR);
+                } #but just to make sure, have it grab a minidvodb_name, to make sure it's not junk.
+		 $this_is_the_first = 0;
+	    }
+
+	    my $merge_command;
+	    if ($this_is_the_first) {
+		
+		$merge_command = "cp -rp $minidvodb $mergedvodb"
+
+	    } else {
+
+		$merge_command = "$dvomerge $minidvodb into $mergedvodb"
+	    }
+
+
+
+	    system ($merge_command);
+
+	    my ( $success2, $error_code2, $full_buf2, $stdout_buf2, $stderr_buf2 ) =
+		run(command => $merge_command, verbose => $verbose);
+	unless ($success2) {
+	    $error_code2 = (($error_code2 >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to merge: $error_code", $mergedvodb, $error_code2);
+	}
+
+	    	   
+            $dtime_merge = 86400.0*(DateTime->now->mjd - $mjd_relphot_start);   # MJD of starting script
+            print "merge time $dtime_merge\n";
+	    
+	}
+	
+
+	{
+	my $command = "addtool -minidvodb_id $minidvodb_id";
+        $command .= " -addminidvodbprocessed";
+	$command .= " -mergedvodb_path $mergedvodbReal" if defined $mergedvodbReal;
+	$command .= " -minidvodb_group $minidvodb_group";
+        $command .= " -dtime_relphot $dtime_relphot"  if defined $dtime_relphot;
+        $command .= " -dtime_relphot $dtime_resort" if defined $dtime_resort;
+        $command .= " -dtime_relphot $dtime_merge" if defined $dtime_merge;
+        $command .= " -dbname $dbname" if defined $dbname;
+    #print $command;
+	system ($command);
+
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to add to minidvodbprocessed: $error_code", $mergedvodb, $error_code);
+	}
+
+	}
+
+    } else {
+        &my_die("dvodb: $mergedvodb not found", $mergedvodb, $PS_EXIT_UNKNOWN_ERROR);
+    }
+} else {
+    print "skipping processing for $mergedvodbReal\n";
+}
+
+exit 0;
+
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $minidbvodb_id = shift; 
+    my $exit_code = shift; # Exit code to add
+
+    print STDERR "$msg $mergedvodb\n";
+
+if (defined $minidvodb_id ) {
+    
+    my $command = "addtool -minidvodb_id $minidvodb_id";
+    $command .= " -addminidvodbprocessed";
+        $command .= " -fault $exit_code";
+	$command .= " -mergedvodb_path $mergedvodbReal" if defined $mergedvodbReal;
+    	$command .= " -minidvodb_group $minidvodb_group";
+        $command .= " -dtime_relphot $dtime_relphot" if defined $dtime_relphot;
+        $command .= " -dtime_relphot $dtime_resort" if defined $dtime_resort;
+        $command .= " -dtime_relphot $dtime_merge" if defined $dtime_merge;
+        $command .= " -dbname $dbname" if defined $dbname;
+    
+    
+
+    #print $command;
+    system ($command);
+    }
+
+
+
+
+    $exit_code = $PS_EXIT_PROG_ERROR unless defined $exit_code;
+
+    exit $exit_code;
+}
+
+__END__
Index: /branches/haf_branches/ipp.20100512/ippTasks/minidvodb.pro
===================================================================
--- /branches/haf_branches/ipp.20100512/ippTasks/minidvodb.pro	(revision 28170)
+++ /branches/haf_branches/ipp.20100512/ippTasks/minidvodb.pro	(revision 28171)
@@ -433,9 +433,12 @@
     book setword minidvodbMergelist $pageName pantaskState RUN
     book getword minidvodbMergelist $pageName minidvodb_id -var MINIDVODB_ID
+    book getword minidvodbMergelist $pageName minidvodb_group -var MINIDVODB_GROUP
+    book getword minidvodbMergelist $pageName mergedvodb_path -var MERGEDVODB_PATH
+    book getword minidvodbMergelist $pageName minidvodb_path -var MINIDVODB_PATH
     book getword minidvodbMergelist $pageName state -var STATE
     stdout $LOGDIR/minidvodb.merge.run.log
     stderr $LOGDIR/minidvodb.merge.run.log
 
-    $run = addtool -updateminidvodbrun -minidvodb_id $MINIDVODB_ID -set_state merging  
+    $run = minidvodb_merge.pl --camera GPC1 --mergedvodb $MERGEDVODB_PATH --minidvodb $MINIDVODB_PATH --minidvodb_group $MINIDVODB_GROUP --minidvodb_id $MINIDVODB_ID
     
   if ($DB:n == 0)
Index: /branches/haf_branches/ipp.20100512/ippTools/src/addtool.c
===================================================================
--- /branches/haf_branches/ipp.20100512/ippTools/src/addtool.c	(revision 28170)
+++ /branches/haf_branches/ipp.20100512/ippTools/src/addtool.c	(revision 28171)
@@ -891,5 +891,5 @@
 
     
-
+    
     if (!psDBCommit(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -1085,5 +1085,5 @@
 
     psStringAppend(&query2, " AND minidvodb_group = '%s' limit 1;", minidvodb_group);
-   
+    
 
 
@@ -1114,5 +1114,5 @@
 
     return true;
-
+   
 }
 
@@ -1309,9 +1309,19 @@
 PXOPT_LOOKUP_F32(dtime_merge, config->args, "-dtime_merge", false, false);
     PXOPT_LOOKUP_TIME(epoch, config->args, "-epoch", false, false);
-PXOPT_LOOKUP_STR(mergedvodb_path, config->args, "-mergedvodb_path", false, false);
+PXOPT_LOOKUP_STR(mergedvodb_path, config->args, "-mergedvodb_path", true, false);
+PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-minidvodb_group", true, false);
+
     PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
     //generate restrictions
 psMetadata *where = psMetadataAlloc();
 PXOPT_COPY_S64(config->args, where, "-minidvodb_id",   "minidvodbRun.minidvodb_id",   "==");
+
+
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        
+        return false;
+    }
+
 
     psString query = pxDataGet("addtool_find_pendingmergeprocess.sql");
@@ -1346,9 +1356,9 @@
     }
     
-    if (!psDBTransaction(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(output);
-        return false;
-    }
+    //if (!psDBTransaction(config->dbh)) {
+    //    psError(PS_ERR_UNKNOWN, false, "database error");
+    //    psFree(output);
+    //     return false;
+    // }
 	minidvodbRunRow *pendingRow = minidvodbRunObjectFromMetadata(output->data[0]);
 	psFree(output);
@@ -1377,29 +1387,80 @@
 	// since there is only one exp per 'new' set addRun.state = 'full'
 	
-	//	psString query2 = NULL ;
-	//
-	//	psStringAppend(&query2, "UPDATE minidvodbRun SET state = 'merging' WHERE minidvodb_name = '%s'", row->minidvodb_name); 
-
-	//if (!p_psDBRunQuery(config->dbh, query2)) {
-	//  psError(PS_ERR_UNKNOWN, false, "database error");
-	//  psFree(query2);
-	//  return false;
-	//	}
-	// psArray *output2 = p_psDBFetchResult(config->dbh);
-	//  if (!output2) {
-        //psError(PS_ERR_UNKNOWN, false, "database error");
-        //return false;
-	// }
-	// if (!psArrayLength(output2)) {
-	//    psTrace("addtool", PS_LOG_INFO, "no rows found");
-	//	//     psFree(output2);
-	//    return true;
-	//  }
-
-
+		psString query2 = NULL ;
+		psStringAppend(&query2, "UPDATE minidvodbRun SET state = 'merging' WHERE minidvodb_id = %'" PRIu64, row->minidvodb_id); 
 	
-	//	psFree(row);
-	//	psFree(pendingRow);
-	//	
+		if (!p_psDBRunQuery(config->dbh, query2)) {
+	  // rollback
+		  if (!psDBRollback(config->dbh)) {
+		    psError(PS_ERR_UNKNOWN, false, "database error");
+		  }
+		  psError(PS_ERR_UNKNOWN, false, "database error");
+		  psFree(query2);
+		  return false;
+		}
+
+		//this finds the # of merged things (for the merge order)
+		psString query3 = NULL;
+		psStringAppend(&query3, "select count(*) from minidvodbRun join minidvodbProcessed using (minidvodb_id) where state = 'merged' and minidvodb_group = '%s';", minidvodb_group);	
+	
+		if (!p_psDBRunQuery(config->dbh, query3)) {
+	  // rollback
+		  if (!psDBRollback(config->dbh)) {
+		    psError(PS_ERR_UNKNOWN, false, "database error");
+		  }
+		  psError(PS_ERR_UNKNOWN, false, "database error");
+		  psFree(query3);
+		  return false;
+		}
+		psArray *output2 = p_psDBFetchResult(config->dbh);
+		if (!output2) {
+		  psError(PS_ERR_UNKNOWN, false, "database error");
+		  return false;
+		}
+		if (!psArrayLength(output2)) {
+		  psTrace("addtool", PS_LOG_INFO, "no rows found");
+		  psFree(output);
+		  return true;
+		}
+		bool status;
+		psS64 m_order = psMetadataLookupS64(&status, output2->data[0], "count(*)");
+		if (!status) { 
+		  
+		  psAbort("failed to lookup value for count column"); 
+		  return false;
+		}
+		psString final = NULL;
+		psStringAppend(&final, "%" PRIu64, m_order);
+		printf("%s", final);
+		//return false;
+
+	
+
+		psFree(query3);	
+	
+	
+		psFree(output2);
+		
+	
+		psString query4 = NULL;
+		psStringAppend(&query4, "update minidvodbProcessed set merge_order = %"PRIu64,m_order);
+		psStringAppend(&query4," where minidvodb_id = %" PRIu64,  minidvodb_id);	
+		printf("%s", query4);
+			if (!p_psDBRunQuery(config->dbh, query4)) {
+	  // rollback
+			  if (!psDBRollback(config->dbh)) {
+			    psError(PS_ERR_UNKNOWN, false, "database error");
+			  }
+			  psError(PS_ERR_UNKNOWN, false, "database error");
+			  psFree(query4);
+			  return false;
+			}
+//	
+
+
+		psFree(query4);
+			psFree(row);
+			psFree(pendingRow);
+			
 	if (!psDBCommit(config->dbh)) {
 	  psError(PS_ERR_UNKNOWN, false, "database error");
@@ -1408,5 +1469,6 @@
 	
 
-	
+	printf("%s", final);
+	psFree(final);
 	
 	return true;
Index: /branches/haf_branches/ipp.20100512/ippTools/src/addtoolConfig.c
===================================================================
--- /branches/haf_branches/ipp.20100512/ippTools/src/addtoolConfig.c	(revision 28170)
+++ /branches/haf_branches/ipp.20100512/ippTools/src/addtoolConfig.c	(revision 28171)
@@ -196,4 +196,5 @@
     psMetadataAddTime(addminidvodbprocessedArgs, PS_LIST_TAIL, "-epoch",         0,    "time merge is finished", NULL);
     psMetadataAddStr(addminidvodbprocessedArgs, PS_LIST_TAIL, "-mergedvodb_path",0,    "path of merged dvodb", NULL);
+psMetadataAddStr(addminidvodbprocessedArgs, PS_LIST_TAIL, "-minidvodb_group",0,    "minidvodb_group", NULL);
     psMetadataAddS16(addminidvodbprocessedArgs, PS_LIST_TAIL, "-fault",          0,    "set fault code", 0);
 
