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 setRemoteViId($ViId) { $this->remoteViId = $ViId; } 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; } public function setViNoCompression($no_compression) { $this->noCompression = $no_compression; } // For Remote Data Centers public function setViInfoFromFile($base, $ds) { $infoXml = new DomDocument("1.0"); $DDBASE=getenv("DDBASE"); if (is_link($DDBASE)) { $DDBASE=readlink($DDBASE); } $infoXmlFile = $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; $fillValue = $infoXml->getElementsByTagName("FillValue"); if ($fillValue->length > 0) $this->fillValue = $fillValue->item(0)->nodeValue; $noCompression = $infoXml->getElementsByTagName("NoCompression"); if ($noCompression->length > 0) $this->noCompression = $noCompression->item(0)->nodeValue; $this->location = $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(); } public function deleteVi($ViId, $baseId) { $XPath = new DOMXpath($this->DDsysDoc); $dataSet = $XPath->query("//NAME[.='$ViId']"); if ($dataSet->item(0)->getAttribute("base") != $baseId) return; $ViToDelete = $dataSet->item(0)->parentNode; $location = $ViToDelete->getElementsByTagName('LOCATION')->item(0)->nodeValue; $ViToDelete->parentNode->removeChild($ViToDelete); $this->DDsysDoc->save($this->DDsys); system("makeDDsys"); foreach (glob($location."/*") as $file) unlink($file); rmdir($location); } 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("DDBASEBIN")."/Cache.template", $this->cache); $SED = "sed 's/NAME/".$this->brief."/g' ".getenv("DDBASEBIN")."/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)); if ($this->fillValue) $rootElement->appendChild($xml_dom->createElement("FillValue", $this->fillValue)); if ($this->noCompression) $rootElement->appendChild($xml_dom->createElement("NoCompression", $this->noCompression)); $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 getRemoteLocation($base, $remoteVi) // { // $XPath = new DOMXpath($this->DDsysDoc); // // $dataSet = $XPath->query("//NAME[.='$ViId']"); // $allVis = $this->DDsysDoc- // // } public function lockVi() { // Stamp -> this directory is being updated touch($this->ViDir."/LOCK"); // fprintf($STDERR,$ViDir." is LOCKED\n"); } public 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, $notFirst = true) { $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 ".$this->times." ".$ncFile); } if ($notFirst) system("TimesUpdateNoData ".$this->times." ".$start." ".$stop); chdir($WORKING_DIR); } public function setInfo($infoFile) { $DDBASE=getenv("DDBASE"); if (is_link($DDBASE)) { $DDBASE=readlink($DDBASE); } $fullInfoName = $DDBASE."/../INFO/bases/".$this->base."/$infoFile"; if (file_exists($fullInfoName)) unlink($fullInfoName); //TODO errors //if (!file_exists($infoFile)) $WORKING_DIR = getcwd(); rename("$WORKING_DIR/$infoFile", $fullInfoName); } public function updateRemoteStart() { $startStamp = shell_exec("GetStartTime ".$this->location.$this->times); $startIso = date("Y-m-d\TH:i:s", intval($startStamp)).substr(number_format(fmod(floatval($startStamp),1),3, '.', ''),1,4)."Z"; $xml_dom = new DomDocument("1.0"); $xml_dom->load($this->location."/".$this->info_xml); $startNode = $xml_dom->getElementsByTagName("GlobalStart")->item(0); $startNode->nodeValue = $startIso; $xml_dom->save($this->location."/".$this->info_xml); $currDir = getcwd(); chdir($this->location); system("infoLocal2nc ".$this->info_xml." ".$this->info); chdir($currDir); } public function updateViStop($globalStop) { $xml_dom = new DomDocument("1.0"); $xml_dom->load($this->location."/".$this->info_xml); $stopNode = $xml_dom->getElementsByTagName("GlobalStop")->item(0); $stopNode->nodeValue = $globalStop; $xml_dom->save($this->location."/".$this->info_xml); $currDir = getcwd(); chdir($this->location); system("infoLocal2nc ".$this->info_xml." ".$this->info); chdir($currDir); } public function cleanRemote($opt = NULL) { $res_before = disk_free_space(getenv("DDBASE")); $Days = 10; // default $SecDay = 3600*24; $nodata = false; if (isset($opt)) { if ($opt == "NODATA") $nodata = true; else { $Days = is_numeric($opt) ? $opt : 10; settype($Days, "int"); echo "!!!!!!!!!!!!!!!! Days ".$Days.PHP_EOL; } } $GlobalDelta = $Days*$SecDay; $xp = new domxpath($this->DDsysDoc); $dataSets = $xp->query("//NAME[attribute::base!='LOCAL']"); for ($i = 0; $i < $dataSets->length; $i++) { $parent = $dataSets->item($i)->parentNode; $location = $parent->getElementsByTagName("LOCATION")->item(0)->nodeValue; if (!is_dir($location)) { $parent->parentNode->removeChild($parent); } else { $time_info = $parent->getElementsByTagName("TIMES")->item(0)->nodeValue; chdir($location); system("./clean"); touch($location."/LOCK"); fprintf(STDERR,"In the ".$location."\n"); /* Delete No Data Intervals */ if ($nodata) { system("CleanNoData ".$time_info); } /* Delete Old Files */ else { $isFirst = true; foreach (glob("*.gz") as $filename) { if ($isFirst) { // we should always keep first file touch($filename); $isFirst = false; } $info = stat($filename); if ((time() - $info['atime']) > $GlobalDelta) { // fprintf(STDERR, $filename."\n"); system("TimesUpdate -d ".$time_info." ".$filename); } //fprintf(STDERR, date("Y-m-d",$info['atime'])." - ".date("Y-m-d",$info['mtime'])."\n"); } } system("DDClearTimes ".$time_info); unlink($location."/LOCK"); } } /* $res_after = disk_free_space(getenv("DDBASE")); $difference = ($res_after - $res_before)/1024.0; $res_after /= 1024.0; fprintf(STDERR,"+ Free Space ".$difference." kB\n"); fprintf(STDERR," Free Space ".$res_after." kB\n");*/ $this->DDsysDoc->save($this->DDsys); system("makeDDsys"); } function convertVIDDTimeToDouble($ViId) { $XPath = new DOMXpath($this->DDsysDoc); $dataSet = $XPath->query("//NAME[.='$ViId']"); if ($dataSet->length == 0) { echo "[ERROR] NO $ViId in DDsys.xml".PHP_EOL; return FALSE; } $dataSet = $dataSet->item(0); $locationNodes = $dataSet->parentNode->getElementsByTagName('LOCATION'); if ($locationNodes->length == 0) { echo "[ERROR] Cannot retrieve LOCATION for $ViId in DDsys.xml".PHP_EOL; return FALSE; } $location = $locationNodes->item(0)->nodeValue; if (file_exists($location."/LOCK")) { echo "[ERROR] VI $ViId already locked by another process".PHP_EOL; return FALSE; } echo "[INFO] Lock VI $ViId".PHP_EOL; touch($location."/LOCK"); $WORKING_DIR = getcwd(); chdir($location); echo "[INFO] Cleanup VI $ViId".PHP_EOL; system("./clean"); echo "[INFO] Convert data files".PHP_EOL; system("ConvertDDTimeToDouble *.nc.gz"); unlink($location."/LOCK"); } } ?>