<?php
	//connection
	include("connection.php");
    include("frames_header.php");
?>

<?php
    //ignore_user_abort(true);
    $test=$_POST["test"];
	if($test!='y'){
	    $test='n';
	}else print "test mode<br>";
	
    $av=$_POST["av"];
	if($av!='y')
	    $av='n';

    $path=$_POST["filepath"];

///// AJOUT
//$path="76c_shape.txt";
$path="./biometry/l112e2CI_BIOM.txt";



	$stage="";
	
	$file = fopen ("$path", "r");
	while ($line = fgets($file)){
		$line=rtrim($line);
		if( ereg("^#devstage=([0-9]+)", $line, $regs) ){
            $stage=$regs[1];
		}
		else if( ! ereg("^#", $line) ){
		    
		    $data=split("\t",$line);
			
			if( count($data)!=9 ){
			    print "<br><br><br> <center>WARNING: your file does not seems to be an ANISEED biometry report <br>
				use the 3D software to export the biometry report file with shape descriptors</center> <br><br><br>";
				include("frames_footer.php");
				return;
			}
			
			$cell=$data[0];
			$sphe=$data[1];
			//$cyl =$data[2];
			$elong =$data[2];
			$flat =$data[3];
			//$complex =$data[5];
			$ocup =$data[4];
			$ent =$data[5];
			$sv =$data[6];
			$conv = $data[7];
			$vv=$data[8];	


			print $cell."-".$sphe."-".$elong."-".$flat."-".$ocup."-".$ent."-".$sv."-$conv-$vv<br>\n"; 
			
			if($stage==""){
			    print "<br><br><br> <center>WARNING: your file does not seems to be the right ANISEED biometry report <br>
				use the 3D software to export the biometry report file with shape descriptors</center><br>
				ERROR: unable to find the developmental stage ID
				<br><br><br>";
				include("frames_footer.php");
				return;
			}
			
			
			$requete= "SELECT \"ANAT_ID\" from \"ANAT_ENTITY\" where \"NAME\"='$cell' and \"DEV_STAGE_ID\"='$stage'";
			$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			if( $row=pg_fetch_array($stmt) ){
				$cellid=$row["ANAT_ID"];
			}else{
				print "Error: cell anatomical id not found for $cell";
				return;
			}
			
			if( $test=='n' ){
/*Debut de la transaction*/
pg_query($c,"begin");
				$requete= "SELECT \"CELL\", \"AV\", \"SPHERICITY\", \"ELONGATION\", \"FLATNESS\", \"OCCUPENCY\", \"ENTROPY\", \"SV\", \"CONVEXITY\", \"VV\" from \"BIOMETRY_SHAPE\" where \"CELL\"='$cellid'";
				$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
				if( $row=pg_fetch_array($stmt) ){
					$oldav=$row["AV"];
					$oldsphe=$row["SPHERICITY"];
					//$oldcyl=$row["CYLINDRICITY"];
					$oldelong=$row["ELONGATION"];
					$oldflat=$row["FLATNESS"];
					//$oldcomp=$row["COMPLEXITY"];
					$oldocup=$row["OCCUPENCY"];
					$oldent=$row["ENTROPY"];
					$oldsv=$row["SV"];
					$oldconv=$row["CONVEXITY"];
					$oldvv=$row["VV"];


					//update
					if($av=='n'){
						$requete= "update \"BIOMETRY_SHAPE\" set \"AV\"='1', "
			                       ."\"SPHERICITY\"=$sphe, \"ELONGATION\"=$elong, \"FLATNESS\"=$flat, \"OCCUPENCY\"=$ocup, \"ENTROPY\"=$ent, \"SV\"=$sv \"CONVEXITY\"=$conv, \"VV\"=$vv where \"CELL\"='$cellid'";
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						
					}else{
					    $oldsphe = round( (($oldsphe*$oldav) + $sphe) /($oldav+1) );
						//$oldcyl  = round( (($oldcyl*$oldav)  + $cyl)/($oldav+1) );
						$oldelong= round( (($oldelong*$oldav)+ $elong)/($oldav+1) );
						$oldflat = round( (($oldflat*$oldav) + $flat)/($oldav+1) );
						//$oldcomp = round( (($oldcomp*$oldav) + $complex)/($oldav+1) );
						$oldocup = round( (($oldocup*$oldav) + $ocup)/($oldav+1) );
						$oldent  = round( (($oldent*$oldav)  + $ent)/($oldav+1) );
						$oldsv   = round( (($oldsv*$oldav)   + $sv)/($oldav+1) );
						$oldconv   = round( (($oldconv*$oldav)   + $conv)/($oldav+1) );
					    $oldav += 1;
						
						//$requete= "update ciona_design.biometry set volume='$oldvol', surface='$oldsurf', av='$oldav' where cell='$cellid'";	
						$requete= "update \"BIOMETRY_SHAPE\" set \"AV\"=$oldav, "
			                       ."\"SPHERICITY\"=$oldsphe,  \"ELONGATION\"=$oldelong, \"FLATNESS\"=$oldflat, \"OCCUPENCY\"=$oldocup, \"ENTROPY\"=$oldent, \"SV\"=$oldsv, \"CONVEXITY\"=$oldconv,\"VV\"=$oldvv where \"CELL\"='$cellid'";
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						
						
					}
					
				}else{
                    //insert
					//$requete= "insert into ciona_design.biometry (cell,volume,surface,av) values ('$cellid','$vol','$surf','1')";
                    
					$requete= "insert into \"BIOMETRY_SHAPE\" "
					."(\"CELL\", \"SPHERICITY\",  \"ELONGATION\", \"FLATNESS\", \"OCCUPENCY\", \"ENTROPY\", \"SV\", \"CONVEXITY\", \"VV\", \"AV\") "
					."values ($cellid, $sphe, $elong, $flat, $ocup, $ent, $sv,$conv,$vv, '1')";
					print $requete;
					$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
					
				}



/*Fin de la transaction*/
pg_query($c,"commit");

				$cell="";
				$sphe="";
				//$cyl ="";
				$elong ="";
				$flat ="";
				//$complex ="";
				$ocup ="";
				$ent ="";
				$sv ="";
				$conv="";
				$vv="";
				
			}
			
		}
		
	}
    print "stage: $stage";
    print "<br>-- Ok --<br>";
?>

<?php
    include("frames_footer.php");
?>