Blame view

src/CALLEXT/createVI.php 10.7 KB
bd9dfce2   Benjamin Renard   Add CALLEXT and D...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/*----------------------------------------------------------------------
 *                   DD SERVER TOOLS
 *                   data update collection 
 *                    createVI.php
 *   17 Jun 2007 V.1.0
 *   01 Aug 2007 V.1.1 Only one argument in info2nc call
 *   02 Aug 2007 V.1.2 rebuild, clean & cash from templates in DDLIB
 *   05 Sep 2007 V.1.3 creates empty *times.nc
 *   24 Nov 2007 V.1.4 DDrehash -> TimesUpdate
 *   14 Feb 2008 V.1.5 added Mission & Instrument tags ; and createHeader if neccessary
 *   11 Sep 2012 V.1.6 get dataset info from remote DB to keep it at server side in the only one place
 *-----------------------------------------------------------------------*/

function getCDF($remSetID){

      $client =   new SoapClient(wsdlCDAWEB);
/* Get "VIEW" descriptions, select "istp_public" and set endPoint address*/
      $metaData = $client->__soapCall("getAllViewDescriptions", array());

      for ($k = 0; $k < count($metaData); $k++) {
	if ($metaData[$k]->id != viewID) continue;
	    try {
	      $client->__setLocation($metaData[$k]->endpointAddress);
	      break;          
	    }
	  catch (SoapFault $exception){          
	    error_log($exception.PHP_EOL, 3, err); 	      
	    return false;                     
	    }
	}
      $fileXML = INFODIR.'CDAWEB/'.$remSetID.'.xml';
      if (!file_exists($fileXML)) {
	    error_log('No DDServer XML description '.$remSetID.PHP_EOL, 3, err);
	    return false;
      }
      $XML= new DomDocument("1.0");
      $XML->load($fileXML);

      $Start = $XML->getElementsByTagName('GlobalStart')->item(0)->nodeValue;
      $fileName = strtolower($remSetID)."_00000000_v01.cdf";
      $Stop =  strftime("%Y-%m-%dT%H:%M:%S.000Z",strtotime($Start) + 82800);
      $nFiles = 0;
      $INFO_DIR = INFODIR."../CDAWEB/DATASETS/";

      while ($nFiles == 0){
	    $dataFileDesc = $client->__soapCall("getDataFiles", array($remSetID, $Start, $Stop));
	    $nFiles = count($dataFileDesc);
	    if ($nFiles > 0) {
	      if (!copy($dataFileDesc[0]->name, $INFO_DIR.$fileName)){
		error_log('Cannot copy '.$dataFileDesc[0]->name.PHP_EOL, 3, err);
		return false;
	      }
	    }
	  $Start =  strftime("%Y-%m-%dT%H:%M:%S.000Z",strtotime($Stop) + 82800);
	  $Stop =  strftime("%Y-%m-%dT%H:%M:%S.000Z",strtotime($Start) + 82800);  
      }
      if ($nFiles == 0) {
	      error_log('No data file '.$remSetID.' '.$Start.PHP_EOL, 3, err);
	    return false;
      }

    return true;
}

function getInfoFromRemoteBase($dataBaseID, $remSetID){

    $INFO_DIR = INFODIR."../".$dataBaseID."/DATASETS/";
    if (!is_dir($INFO_DIR)) mkdir($INFO_DIR, 0755, true);

    switch ($dataBaseID) {
      case 'CDAWEB' : 
	      require_once "ftpWrapper.php"; 
	      require_once "cdaweb_cfg.php"; 
 
              $fileName = strtolower($remSetID)."_00000000_v01.cdf";
	      if (file_exists($INFO_DIR.$fileName)) return true;
      	  	 
	// Get master CDF 	 
	      if (!ftpWrapper(ftp_server, ftp_user_name, ftp_user_pass,
					  ftp_dir.$fileName, $INFO_DIR.$fileName)) {
		  error_log('No master CDF for '.$remSetID.PHP_EOL, 3, err);
        // No master CDF - try regular file
                  return getCDF($remSetID);
	      }
							   
	      return true;

	      break;
	case "MAPSKP":

                require_once "mapskp_cfg.php";
            
		$XML_NAME = $remSetID.".xml";
		if (file_exists($INFO_DIR.$XML_NAME)) return true;

		$args = array('service'=>'getInfoUrl', 'datasetId'=>$remSetID);                                  
               
		$url = url.http_build_query($args);
 
		$infoDom = new DOMDocument();
		$infoDom->load($url);
		$File = $infoDom->getElementsByTagName("infoUrl")->item(0)->nodeValue;

		$fileName_ = explode("ftp://",$File);
		$fileName = $fileName_[0].passwd.$fileName_[1];
		 
		if (!copy($fileName,$INFO_DIR.$XML_NAME)) return false;
		return true;

	      break;

	case "VEXGRAZ" :
		  $fileName = "MAG_VSO.xml";
		  if (file_exists($INFO_DIR.$fileName)) return true;
		  return false;
	      break;

	case "CDPP" :

	      break;

	default:
    }
 
    return true;

};

function getInfoFromAmda($dataBaseID, $remSetID)
{

   global $DDsysInfo;

   if (file_exists(BASEDIR.'info.xml')) unlink(BASEDIR.'info.xml');

         $DDinfo = new DomDocument("1.0"); 
         $VI = $DDinfo->createElement("VI"); 
       
          $InfoArr = $DDsysInfo->getElementsByTagName("VI")->item(0)->childNodes;
          for ($i = 0; $i < $InfoArr->length; $i++) 
             if ($InfoArr->item($i)->nodeName != '#text' && $InfoArr->item($i)->nodeName != 'PARAMETER') 
                                           $VI->appendChild($DDinfo->importNode($InfoArr->item($i), true));  
             $VI->appendChild($DDinfo->createElement("LocalStart"));
             $VI->appendChild($DDinfo->createElement("LocalStop"));
             $DDinfo->appendChild($VI);
             $DDinfo->save(BASEDIR.'info.xml');
         
          return true;
}
 
/**
*  @file  createVI.php
*  @brief DD_Server tool <br> Creates New Virtual Instrument
*  @version $Id: createVI.php,v 1.11 2013/09/23 09:02:28 budnik Exp $
*   Creates New VI at DD_Server Side \n
*   Args: dataBaseID  ddDataSetID  remoteDataSetID \n
*   Env Vars:  $DDBASE $DDBASEBIN $DDLIB \n
*   Executables: makeDDsys info2nc TimesUpdate \n
*   Functions:  getInfoFromAmda
*
* @arg Checks if VI already exists in dataBase desctiptor $DDBASE/DDsys.xml
* @arg Adds non-existing VI to DDres.xml 
* @arg $DDBASEBIN/makeDDsys creats refer.nc file from DDsys.xml
* @arg Gets dataSet Info from INFO/DDServer dir
* @arg Creates dir, *_cash.nc, *_info.nc and empty *_times.nc
*
*/
  
 global $DDsysInfo;

 define("verbose", true);


  define("BASEDIR",getenv("DDBASE")."/");
  define("INFODIR",BASEDIR."../INFO/DDServer/");
  define("DDBASEBIN",getenv("DDBASEBIN"));
  define("DDLIB", getenv("DDLIB")."/");

 define ("log",BASEDIR."../INFO/addVI.log");
 define ("err",BASEDIR."../INFO/addVI.err");


 if ($argc != 4) {
      error_log("Usage: php createVI.php <BaseID> <RemSetID> <NewVIName>".PHP_EOL, 3, err);
      exit("Usage: php createVI.php <BaseID> <RemSetID> <NewVIName>".PHP_EOL);
 }
 
  $dataBaseID = strtoupper($argv[1]);
  $remSetID = $argv[2];
  $VIID = strtolower($argv[3]);  
 


    $MAX_VI_NAME_LENGTH = 16;
    $nc_prefix = strlen($VIID) > $MAX_VI_NAME_LENGTH ? substr($VIID,0,15): $VIID;

    $newDataSet = false;
    $DDsysRefer = BASEDIR."DDsys.xml";
    if (verbose)
	error_log("request to add Data Set ".$dataBaseID.":".$remSetID." as ".$VIID.PHP_EOL, 3, log); 
//  Check if dataSet exists already
     $DDsys = new DomDocument("1.0");
     $DDsys->preserveWhiteSpace = false;
     $DDsys->formatOutput = true;
     $DDsys->load($DDsysRefer);

     $xp = new domxpath($DDsys);
     $dataSet = $xp->query("//NAME[.='".$VIID."']"); 
      
     if ($dataSet->item(0) != NULL)      
          if ($dataSet->item(0)->getAttribute("base") != $dataBaseID) {  
             error_log(" Data Set ".$VIID." exists! Corresponding BaseID is "
            .$dataSet->item(0)->getAttribute("base")." not ".$dataBaseID.PHP_EOL, 3, err);
            
            $newDataSet = true;          
     }
     if ($dataSet->item(0) == NULL) $newDataSet = true;

//-------------- Get Full Dataset Info file from distant database-------------
//TODO error processing here
    $res = getInfoFromRemoteBase($dataBaseID, $remSetID);
    if (!$res) 
	error_log("Error: Can't get dataset info for ".$remSetID." from ".$dataBaseID.PHP_EOL, 3, err);

    if (verbose) error_log("Get dataset info for ".$remSetID." from ".$dataBaseID." returns ".$res.PHP_EOL, 3, log);
    if ($newDataSet) { 
 
//----------- Gets INFO from INFO/DDServer -------------------
	if (verbose) error_log("Ready to Add ".$VIID.PHP_EOL, 3, log); 
        $DDsysInfo = new DomDocument("1.0");
  
//----------- Getting INFO file for DataSet
        $XmlName = INFODIR.$dataBaseID."/".$remSetID.".xml";

        if (file_exists($XmlName)){
                
            $DDsysInfo->load($XmlName); 
            $mission = $DDsysInfo->getElementsByTagName("Mission")->item(0)->nodeValue;
            $instrument = $DDsysInfo->getElementsByTagName("Instrument")->item(0)->nodeValue;

//--------- Check if additional Info should be added to VI

            $theFormat = $DDsysInfo->getElementsByTagName("Format")->item(0);
            if ($theFormat != NULL)
                   $AddInfo = ($theFormat->nodeValue == "Text");
            if (!$AddInfo) 
                   $AddInfo = ($DDsysInfo->getElementsByTagName("AdditionalInfoNeed")->item(0) != NULL);

  //---------------------------------------------------------
           $newVIdir = BASEDIR.$dataBaseID."/".strtoupper($VIID)."/";
// New Data Set - add to DDsys.xml
           $base = $xp->query("/DD_BASE")->item(0);
           $newVI= $DDsys->createElement("VI"); 
           $newName = $DDsys->createElement("NAME",$VIID);
           $newattrBase = new DomAttr('base', $dataBaseID);
           $newattrMission = new DomAttr('mission', $mission);
           $newattrIns = new DomAttr('instrument', $instrument);
           $newName->appendChild($newattrBase);
           $newName->appendChild($newattrMission);
           $newName->appendChild($newattrIns);
            $newVI->appendChild($newName);
            $newVI->appendChild($DDsys->createElement("LOCATION", $newVIdir));
            $newVI->appendChild($DDsys->createElement("TIMES", $VIID."_times.nc"));
            $newVI->appendChild($DDsys->createElement("INFO", $VIID."_info.nc"));
            $newVI->appendChild($DDsys->createElement("CACHE", $VIID."_cash.nc"));
          $base->appendChild($newVI);

           $DDsys->save($DDsysRefer);
//  xml to refer.nc
          exec(DDBASEBIN.'/makeDDsys');
      
//   Now make new dir .....
          if (!is_dir(BASEDIR.$dataBaseID)) mkdir(BASEDIR.$dataBaseID."/", 0755, true);
          if (!is_dir($newVIdir)) mkdir($newVIdir, 0755, true);
           copy (DDLIB."Cash.template", $newVIdir."/".$VIID."_cash.nc");         
           $SED = "sed 's/NAME/".$nc_prefix."/g' ".DDLIB."Clean.template | sed 's/CACHE/".$VIID."/g' > ".$newVIdir."/clean";
           exec($SED);
           exec('chmod ugo+x '.$newVIdir.'/clean');
//   Now info.nc from metadonnees          
           $err = getInfoFromAmda($dataBaseID, $remSetID);
           if ($err) exec(DDBASEBIN.'/info2nc '.$newVIdir.'/'.$VIID.'_info.nc');
// Create empty times file
           chdir($newVIdir);
           exec(DDBASEBIN.'/TimesUpdate -r '.$VIID.'_times.nc '.$VIID.'_[0-9]*.nc');

           if ($AddInfo) {
              $function_name = "createHeader".$dataBaseID;
              require_once(DDLIB.$function_name.".php");
              $function_name($remSetID); 
           }
	if (verbose) error_log("Added ".$VIID.PHP_EOL, 3, log); 
       }      
       else {
	error_log("Error: ".$VIID." not added: no XML DDServer info file ".PHP_EOL, 3, err); 
      }
 }
?>