diff --git a/src/DATA/MANAGER/AddLocalVI.php b/src/DATA/MANAGER/AddLocalVI.php
index b7b22cc..9685adb 100644
--- a/src/DATA/MANAGER/AddLocalVI.php
+++ b/src/DATA/MANAGER/AddLocalVI.php
@@ -2,99 +2,47 @@
 
 /**
 *  @file AddLocalVI.php
-*  @version $Id: AddLocalVI.php,v 1.3 2010/10/29 14:03:53 budnik Exp $
+*  @version 
 *  @brief DD Server Tools: 
 *  @arg    
 */
 
- function makeInfoXml() {
-
-    $xml_dom = new DomDocument("1.0");
-    $rootElement = $xml_dom -> createElement("VI");
-    $rootElement -> appendChild($xml_dom -> createElement("Mission",mission));
-    $rootElement -> appendChild($xml_dom -> createElement("Instrument",instrument));
-    $rootElement -> appendChild($xml_dom -> createElement("GlobalStart"));
-    $rootElement -> appendChild($xml_dom -> createElement("GlobalStop"));
-    $rootElement -> appendChild($xml_dom -> createElement("MinSampling", MinSampling));
-    if (MaxSampling > MinSampling)  $rootElement -> appendChild($xml_dom -> createElement("MaxSampling", MaxSampling));
-    $rootElement -> appendChild($xml_dom -> createElement("LocalStart"));
-    $rootElement -> appendChild($xml_dom -> createElement("LocalStop"));
-    $xml_dom -> appendChild($rootElement);
-    $xml_dom -> save(brief."_info.xml");
-    
-}
-
-   require_once 'mgr_ini.php';
-   define("CURRDIR",dirname(__FILE__));
+	if (!function_exists('__autoload')) 
+	{
+		function __autoload($class_name) {
+				require_once $class_name . '.php';
+		}
+	}
+   
+   putenv("LD_LIBRARY_PATH=".getenv("LD_LIBRARY_PATH"));
+	putenv("PATH=./:".getenv("DDBASEBIN").":/bin:/usr/bin");
+	set_include_path("./:".getenv("DATAMANAGER").":".getenv("REMOTEDATA").":".getenv("CALLEXT")); 
 
 /*
 *  DEFINITIONS TO FILL
 */
-   define("brief", "jedi"); // prefix
-   define("ViId", "juno_jedi_i180");
-   define("mission", "JUNO");
-   define("instrument", "JEDI");
-   define("MISSION_DIR", DDBASE."JUNO/");
-   define("location", MISSION_DIR."JEDI/I_180");
-   define("MinSampling", 2);
-   define("MaxSampling", 3); // 0 if constant sampling
+   $brief = "jedi"; // prefix $argv[1]
+   $ViId = "juno_jedi_i180"; // $argv[2]
+   $mission = "JUNO";  // $argv[3]
+   $instrument= "JEDI"; // $argv[4]
+   $mission_dir = "JUNO/"; // $argv[5]
+   $location = "JEDI/I_180"; // $argv[6]
+   $min_sampling = 2;// $argv[7]
+   $max_sampling = 3;// $argv[8]  // 0 if constant sampling
 /*
 *  DO NOT TOUCH
 */
-      
-   $times = brief."_times.nc";
-   $info =  brief."_info.nc";
-   $info_xml =  brief."_info.xml";
-   $cache = brief."_cache.nc";
+   $base = "LOCAL";
+	$baseMgr = new DDBaseMgr();
 
-   $DDsysDoc = new DomDocument();
-   $DDsysDoc->preserveWhiteSpace = false;
-   $DDsysDoc->formatOutput = true;
-   $DDsysDoc->load(DDBASE.DDsys);
-   $dataSet = $DDsysDoc->getElementsByTagName("VI");
-/*
-*   Check if there is already such VI
-*/
-   foreach ($dataSet as $theDataSet) 
-           if (strcmp($theDataSet->getElementsByTagName("NAME")->item(0)->nodeValue, ViId) == 0) 
-            die(ViId." Already Exists!!!\n");
-    
-    $refNode = $dataSet->item(0);
-    $newNode = $DDsysDoc->createElement("VI");
-    $name = $newNode->appendChild($DDsysDoc->createElement("NAME",ViId));
-    $name->setAttributeNode(new DOMAttr('base', 'LOCAL'));
-    $name->setAttributeNode(new DOMAttr('mission', mission));
-    $name->setAttributeNode(new DOMAttr('instrument', instrument));
+	if ($baseMgr->viExists($ViId, $base)) die("$ViId Already Exists!!!".PHP_EOL);
 
-    if (substr(location, -1) !== "/") $newNode->appendChild($DDsysDoc->createElement("LOCATION",location."/")); 
-    else $newNode->appendChild($DDsysDoc->createElement("LOCATION",location));
-
-    $newNode->appendChild($DDsysDoc->createElement("TIMES",$times));
-    $newNode->appendChild($DDsysDoc->createElement("INFO",$info));
-    $newNode->appendChild($DDsysDoc->createElement("CACHE",$cache));
-
-    $newNode = $refNode->parentNode->insertBefore($newNode, $refNode);
-         
-    $DDsysDoc->save(DDBASE.DDsys);
-    system(DDBASEBIN."/makeDDsys");  
-          
-/*
-*   Create Real Stuff
-*
-*/   
+	$baseMgr->setViId($ViId);
+	$baseMgr->setViInfo($brief);
+	$baseMgr->setViParentsInfo($base, $mission, $instrument);
+	$baseMgr->setViLocation($mission_dir, $location);
+	$baseMgr->setViSampling($min_sampling, $max_sampling);
 
-  if (!is_dir(MISSION_DIR)) mkdir(MISSION_DIR);
-  if (!is_dir(location)) mkdir(location, 0775, true);
-  echo location."\n";
-  chdir(location);
-   if (!file_exists($times)) system(DDBASEBIN."/TimesUpdate -r ".$times." ".brief."_[0-9]*.nc");
-   if (!file_exists($info_xml)) makeInfoXml();
-   if (!file_exists($info)) system(DDBASEBIN."/infoLocal2nc ".$info_xml." ".$info);
-   if (!file_exists("clean")) {
-           copy (DDLIB."/Cash.template", $cache);         
-           $SED = "sed 's/NAME/".brief."/g' ".DDLIB."/Clean.template > clean";
-           exec($SED);
-           exec('chmod ugo+x clean');
-    } 
-  chdir(CURRDIR);
+	$baseMgr->createVi();
+		 
 ?>
diff --git a/src/DATA/MANAGER/DDBaseMgr.php b/src/DATA/MANAGER/DDBaseMgr.php
new file mode 100644
index 0000000..f24995e
--- /dev/null
+++ b/src/DATA/MANAGER/DDBaseMgr.php
@@ -0,0 +1,235 @@
+<?php
+
+/**
+*  @class DDBaseMgr
+*  @brief  
+*  @arg    
+*/
+class DDBaseMgr 
+{
+ 
+	private $brief, $times, $info, $info_xml, $cache;
+	private $ViId, $remoteViId;
+	private $DDsysDoc, $DDsys;
+	private $base, $mission, $instrument;
+	private $min_sampling, $max_sampling;	
+	private $location, $ViDir;
+	public $globalStart = null, $globalStop = null;
+	
+   function __construct()
+	{	
+	   $this->DDsys = getenv("DDBASE")."/DDsys.xml";
+		$this->DDsysDoc = new DomDocument();
+		$this->DDsysDoc->preserveWhiteSpace = false;
+		$this->DDsysDoc->formatOutput = true;
+		$this->DDsysDoc->load($this->DDsys); 	
+	}	
+	
+	public function setViInfo($brief)
+	{
+		$this->brief = $brief;
+		$this->times = $brief."_times.nc";
+		$this->info = $brief."_info.nc";
+		$this->info_xml = $brief."_info.xml";
+		$this->cache = $brief."_cache.nc";
+	}
+	
+	public function setViDir($ViDir)
+	{
+		$this->ViDir = $ViDir;
+	}
+	
+	public function setViId($ViId)
+	{
+		$this->ViId = $ViId;
+	}
+	
+	public function setViParentsInfo($base, $mission, $instrument)
+	{
+		$this->base = $base;
+		$this->mission = $mission;
+		$this->instrument = $instrument;
+	}
+	
+	public function setViSampling($min_sampling, $max_sampling)
+	{
+		$this->min_sampling = $min_sampling;
+		$this->max_sampling = $max_sampling;		
+	}
+	
+	public function setViLocation($mission_dir, $location)
+	{
+		$this->location = getenv("DDBASE")."/".$mission_dir."/".$location;	
+		$this->ViDir = $this->location;
+	}
+	
+	// For Remote Data Centers
+	public function setViInfoFromFile($base, $ds)
+	{
+		$infoXml = new DomDocument("1.0");
+		$infoXmlFile = getenv("DDBASE")."/../INFO/DDServer/$base/$ds.xml";
+		
+		if (!file_exists($infoXmlFile))
+				return -100;
+		
+		if (!$infoXml->load($infoXmlFile))
+				return -10;
+		$this->base = $base;	
+		$this->mission = $infoXml->getElementsByTagName("Mission")->item(0)->nodeValue;
+		$this->instrument = $infoXml->getElementsByTagName("Instrument")->item(0)->nodeValue;
+		$this->min_sampling = $infoXml->getElementsByTagName("MinSampling")->item(0)->nodeValue;
+		$this->remoteViId = $ds;
+		$this->globalStart = $infoXml->getElementsByTagName("GlobalStart")->item(0)->nodeValue;
+		$this->globalStop = $infoXml->getElementsByTagName("GlobalStop")->item(0)->nodeValue;
+
+		$this->location = getenv("DDBASE")."/".$this->base."/".strtoupper($this->ViId)."/";
+		$this->ViDir = $this->location;
+		//--------- Check if additional Info should be added to VI
+// 		$theFormat = $infoXml->getElementsByTagName("Format")->item(0);
+// 		if ($theFormat != NULL)
+// 			$AddInfo = ($theFormat->nodeValue == "Text");
+// 		if (!$AddInfo) 
+// 			$AddInfo = ($infoXml->getElementsByTagName("AdditionalInfoNeed")->item(0) != NULL);
+		
+		return 0;
+	}
+	
+	public function viExists($ViId, $baseId)
+	{
+		$XPath = new DOMXpath($this->DDsysDoc);
+		
+		$dataSet = $XPath->query("//NAME[.='$ViId']");
+		
+		if ($dataSet->item(0) != NULL)
+		{						
+			if ($dataSet->item(0)->getAttribute("base") != $baseId) return false;
+			else return true;			
+		}
+		
+		return false;	
+	}
+	
+	public function createVi()
+	{
+		 $this->makeDDsys();
+		 $this->makeAllInLocation();		
+	}
+	
+	private function makeAllInLocation()
+	{
+		if (!is_dir($this->location)) 
+			mkdir($this->location, 0775, true);
+      $currDir = getcwd();
+      
+		chdir($this->location);
+		if (!file_exists($this->times)) system("TimesUpdate -r ".$this->times." ".$this->brief."_[0-9]*.nc");
+		if (!file_exists($this->info_xml)) $this->makeInfoXml();
+		if (!file_exists($this->info)) system("infoLocal2nc ".$this->info_xml." ".$this->info);
+		if (!file_exists("clean")) 
+		{
+			copy(getenv("DDBASE")."/Cache.template", $this->cache);         
+			$SED = "sed 's/NAME/".$this->brief."/g' ".getenv("DDBASE")."/Clean.template > clean";
+			exec($SED);
+			chmod("clean", 0755);
+		} 
+		
+		chdir($currDir);
+	}
+	
+	private function makeInfoXml() 
+	{
+		$xml_dom = new DomDocument("1.0");
+		$rootElement = $xml_dom->createElement("VI");
+		if ($this->base != "LOCAL") 
+			$rootElement->appendChild($xml_dom->createElement("DataBaseID",$this->base));
+		$rootElement->appendChild($xml_dom->createElement("Mission",$this->mission));
+		$rootElement->appendChild($xml_dom->createElement("Instrument",$this->instrument));
+		if ($this->base != "LOCAL") 
+			$rootElement->appendChild($xml_dom->createElement("DataSetID",$this->remoteViId));
+		$rootElement->appendChild($xml_dom->createElement("GlobalStart", $this->globalStart));
+		$rootElement->appendChild($xml_dom->createElement("GlobalStop", $this->globalStop));
+		$rootElement->appendChild($xml_dom->createElement("MinSampling", $this->min_sampling));
+		if ($this->max_sampling > $this->min_sampling)  
+			$rootElement->appendChild($xml_dom->createElement("MaxSampling", $this->max_sampling));
+		$rootElement->appendChild($xml_dom->createElement("LocalStart"));
+		$rootElement->appendChild($xml_dom->createElement("LocalStop"));
+		$xml_dom->appendChild($rootElement);
+		$xml_dom->save($this->location."/".$this->info_xml);		
+	}
+
+	private function makeDDsys()
+	{
+		$newNode = $this->DDsysDoc->createElement("VI");
+		$name = $newNode->appendChild($this->DDsysDoc->createElement("NAME",$this->ViId));
+		$name->setAttribute('base', $this->base);
+		$name->setAttribute('mission', $this->mission);
+		$name->setAttribute('instrument', $this->instrument);
+
+		if (substr($this->location, -1) !== "/") 
+			$newNode->appendChild($this->DDsysDoc->createElement("LOCATION",$this->location."/")); 
+		else 
+			$newNode->appendChild($this->DDsysDoc->createElement("LOCATION",$this->location));
+
+		$newNode->appendChild($this->DDsysDoc->createElement("TIMES", $this->times));
+		$newNode->appendChild($this->DDsysDoc->createElement("INFO", $this->info));
+		$newNode->appendChild($this->DDsysDoc->createElement("CACHE", $this->cache));
+
+		$this->DDsysDoc->documentElement->appendChild($newNode);
+			
+		$this->DDsysDoc->save($this->DDsys);
+		system("makeDDsys");  	
+	}
+	
+	public function deleteVi($ViId, $baseId)
+	{
+	
+	}
+	
+// 	public function getRemoteLocation($base, $remoteVi)
+// 	{
+// 		$XPath = new DOMXpath($this->DDsysDoc);
+// 		
+// 		$dataSet = $XPath->query("//NAME[.='$ViId']");
+// 		$allVis = $this->DDsysDoc-	
+// 	
+//	}
+	
+	protected function lockVi()
+	{
+		//  Stamp -> this directory is being updated	
+		
+		touch($this->ViDir."/LOCK");
+  
+		//	fprintf($STDERR,$ViDir." is LOCKED\n");
+	}
+	
+	protected function unlockVi()
+	{
+		if (file_exists($this->ViDir."/LOCK")) unlink($this->ViDir."/LOCK");  
+		//	fprintf($STDERR,$ViDir." is UNLOCKED\n");
+	}
+	
+	public function addRemoteData($id, $ncFiles, $start, $stop)
+	{
+		$this->lockVi();
+		
+		$WORKING_DIR = getcwd();
+		chdir($this->ViDir);
+		system("./clean");
+		
+		foreach ($ncFiles as $ncFile)
+		{
+			//TODO errors		
+			//if (!file_exists($ncFile))			   
+			  rename($WORKING_DIR."/".$ncFile,$this->ViDir."/".$ncFile);
+			  system("TimesUpdate -u ".strtolower($id)."_times.nc ".$ncFile);
+		}
+		
+		system("TimesUpdateNoData ".strtolower($id)."_times.nc ".$start." ".$stop);
+		
+		chdir($WORKING_DIR);
+		$this->unlockVi();
+	}
+} 
+?>   
+  
\ No newline at end of file
--
libgit2 0.21.2