Blame view

php/classes/FilesMgr.php 34.4 KB
16035364   Benjamin Renard   First commit
1
2
<?php
/**
bf74fc2d   Elena.Budnik   IMPEX
3
 * @class NewFilesMgr
16035364   Benjamin Renard   First commit
4
5
6
7
8
9
10
11
 * @brief
 * @author Elena
 * @version $Id: FilesMgr.php 2830 2015-03-26 10:33:05Z elena $ 
 */

// private $userDirs = array('USERWSDIR' => 'WS', 'USERREQDIR' => 'REQ', 'USERDATADIR' => 'DATA',
//			     'USERWORKINGDIR' =>'RES', 'USERTTDIR' => 'TT', 'USERJOBDIR' => 'JOBS');	

bf74fc2d   Elena.Budnik   IMPEX
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
class FilesMgr extends AmdaObjectMgr 
{

	private  $fileName, $fileId, $theFile;

	public $xp;
	private $fileMgr = NULL; //used when a file format needs to use a specific manager
	private $sampling = -100.0, $maxSampling = -100.0;
	private $ncID = NULL;
	private $simuContentRootId;

	function __construct() 
	{   
		parent::__construct('Files.xml');
		$this->contentRootId = 'myData-treeRootNode';
		$this->contentRootTag = 'fileList';
		$this->attributes = array('name' => '', 'start' => '', 'stop' => '');
		$this->optionalAttributes = array();
		$this->objTagName = 'file';

		if (!file_exists($this->xmlName)) 
		{
			$this->createDom();
			$this->xp = new domxpath($this->contentDom); 
		} 
	}
16035364   Benjamin Renard   First commit
38
39
40
41

/*
*       Create Content Dom 'Files.xml' if it doesn't exist 	
*/	
bf74fc2d   Elena.Budnik   IMPEX
42
43
44
45
46
47
48
49
50
51
52
53
	protected function createDom() 
	{                            
		$rootElement = $this->contentDom->createElement('ws');   
		
		$typeElement = $this->contentDom->createElement($this->contentRootTag);
		$typeElement->setAttribute('xml:id', $this->contentRootId);
		$rootElement->appendChild($typeElement);		
		$this->contentDom->appendChild($rootElement);
	 		
		$this->contentDom->save($this->xmlName);          
	}
	
16035364   Benjamin Renard   First commit
54
55
56
/*
 *    VOTable format processing done by an instance of VOTableMgr
 */
bf74fc2d   Elena.Budnik   IMPEX
57
58
59
60
61
62
63
64
65
66
	protected function getVotFileMgr()
	{
		if (!$this->fileMgr)
		{
			$this->fileMgr = new VOTableMgr();
			$this->fileMgr->load($this->fileName);
		}
		return $this->fileMgr;
	}
     
16035364   Benjamin Renard   First commit
67
68
69
/*
*     CDF format processing
*/      
bf74fc2d   Elena.Budnik   IMPEX
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
	protected function getCdfStartStop() 
	{  
		exec('cdfstartstopfromdata '.$this->fileName, $start_stop); 
		return $start_stop[0];  
	}

	protected function getCdfVars() 
	{
		exec('cdfinfo '.$this->fileName, $results);
		return $results;
	}

	public function getCdfVarInfo($cdfVarId) 
	{
		exec('cdfvarinfo '.$this->fileName.' '.$cdfVarId, $results);
		$tempArr = explode(' ', $results[0]);
		// data type
		switch ($tempArr[0]) 
		{
			case "CDF_FLOAT" :
			case "CDF_REAL4" : $data_type = 'FLOAT';
									break;
			case "CDF_INT2":  
			case "CDF_UCHAR":  
			case "CDF_UINT1" : $data_type = 'SHORT';
									break;
			case "CDF_UINT2":
			case "CDF_INT4" : $data_type = 'INTEGER';
									break;
			case "CDF_DOUBLE":  
			case "CDF_REAL8":   
			case "CDF_UINT4":  $data_type = 'DOUBLE'; 
									break;
			default : $data_type = 'FLOAT';
		}
		// data dimensions & number of records
		$n_recs = $tempArr[2];

		if ($tempArr[1] != 0) {		 
			$size = $tempArr[3];
			//TODO 2D,3D,4D array no processing	
			//   if ($tempArr[1] > 1) 
		}
		else {
			$size = 1;		  
		} 
3d7c42ed   Elena.Budnik   disable fields pr...
116
117
118
119
120
		
		$units =  exec('cdfvar_attr '.$this->fileName.' '.$cdfVarId.' UNITS');
		$fillval =  exec('cdfvar_attr '.$this->fileName.' '.$cdfVarId.' FILLVAL');
		
		return array('type' => $data_type, 'size' => $size, 'n_records' => $n_recs, 'units' => $units, 'fillvalue' => $fillval);
bf74fc2d   Elena.Budnik   IMPEX
121
	}
16035364   Benjamin Renard   First commit
122

49ea93b2   Benjamin Renard   Allow https for u...
123
	public function getCdfSampling() {
23fba7a9   Elena.Budnik   no temp file for ...
124
		exec('cdfsamplingfromdata '.$this->fileName, $results);
bf74fc2d   Elena.Budnik   IMPEX
125
126
		return $results;
	}
7d539a8c   Elena.Budnik   server side modifs
127
	
49ea93b2   Benjamin Renard   Allow https for u...
128
	public function getCdfSamplings() {      
23fba7a9   Elena.Budnik   no temp file for ...
129
		exec('cdfsamplingsfromdata '.$this->fileName, $results);
7d539a8c   Elena.Budnik   server side modifs
130
131
		return $results;
	}
16035364   Benjamin Renard   First commit
132
133
134
/*
*       CEF format processing
*/
bf74fc2d   Elena.Budnik   IMPEX
135
136
137
138
139
	protected function getcefstartstop() 
	{  
		exec("cefstartstop ".$this->filename, $start_stop);
		return $start_stop[0];
	}
16035364   Benjamin Renard   First commit
140

bf74fc2d   Elena.Budnik   IMPEX
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
	protected function getCefTimeInfo() 
	{  
		$timeinfo = exec("ceftimeinfo ".$this->fileName);
		return $timeinfo;
	}

	protected function getCefVars() 
	{
		exec('cefinfo '.$this->fileName, $results);
		return $results;
	}

	protected function getCefVarInfo($cdfVarId) 
	{
		exec('cefvarinfo '.$this->fileName." ".$cdfVarId, $results);

		$tempArr = explode(' ', $results[0]);
		// data type
		switch ($tempArr[0]) {
			case "CEF_FLOAT" : $data_type = 'FLOAT';
					break;
			case "CEF_SHORT":  $data_type = 'SHORT';
					break;
			case "CEF_INT":  $data_type = 'INTEGER';
					break;
			case "CEF_DOUBLE": $data_type = 'DOUBLE'; 
					break;
			default : $data_type = 'FLOAT';
			}
		// data dimensions & number of records3
		$n_recs = $tempArr[2];

		if ($tempArr[1] != 0) {		 
			$size = $tempArr[3];
		//TODO 2D,3D,4D array no processing	
		//   if ($tempArr[1] > 1) 
		}
		else {
			$size = 1;		  
		} 
		return array('type' => $data_type, 'size' => $size, 'n_records' => 'TBD');       
	}

	protected function getCefSampling() 
	{
		exec('cefsampling '.$this->fileName, $results);        
		return $results;
	}
16035364   Benjamin Renard   First commit
189
190
191
192

/*
*       netCDF format processing if needed
*/
bf74fc2d   Elena.Budnik   IMPEX
193
194
195
196
197
	protected function reformatNcTime() 
	{
		exec('nctimestring2double '.$this->fileName, $results); 
		return $results[0]; 
	}
16035364   Benjamin Renard   First commit
198
        
bf74fc2d   Elena.Budnik   IMPEX
199
200
201
202
203
204
205
206
207
208
209
210
211
	protected function getNcTimeInfo() 
	{
		exec('nctimeinfo '.$this->fileName, $result); 
		return $result[0];  
	}

	protected function  getNcVars()
	{
		exec('getncvars '.$this->fileName, $result);
		
		if ($result[0] < 0)
			return $result[0];
		$varsArr = explode("#",$result[0]);
91892b01   Elena.Budnik   netcdf4 x-hdf
212

bf74fc2d   Elena.Budnik   IMPEX
213
214
215
216
217
218
219
		return  $varsArr;
	}

	protected function getNcVarInfo($varId) 
	{
		exec('ncvarinfo '.$this->fileName.' '.$this->param2dd($varId), $results);
		$tempArr = explode(' ', $results[0]); 
fe516999   Elena.Budnik   nc attributes
220
		// data type
bf74fc2d   Elena.Budnik   IMPEX
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
		switch ($tempArr[0]) {
			case "5": $data_type = 'FLOAT';
						break;
			case "3": $data_type = 'SHORT';
						break;
			case "4" : $data_type = 'INTEGER';
						break;
			case "6": $data_type = 'DOUBLE';               
						break;
			default: $data_type = 'FLOAT';
			}
		// data dimensions & number of records
		$n_recs = $tempArr[2];

		if (count($tempArr) > 3) {		 
				$size = $tempArr[3];
		//TODO 2D,3D,4D array no processing	
		//   if ($tempArr[1] > 1) 
		}
		else {
				$size = 1;		  
		}  
		
fe516999   Elena.Budnik   nc attributes
244
245
246
247
248
249
250
251
252
		$units =  exec('ncvar_attr '.$this->fileName.' '.$this->param2dd($varId).' UNITS');  
		if (substr($units, 0,5) == 'error') $units = '';
		
		$fillval =  exec('ncvar_attr '.$this->fileName.' '.$this->param2dd($varId).' _Fillvalue'); // _Fillvalue
		if (substr($fillval, 0,5) == 'error') $fillval =  exec('ncvar_attr '.$this->fileName.' '.$this->param2dd($varId).' Fillvalue');
		if (substr($fillval, 0,5) == 'error') $fillval =  exec('ncvar_attr '.$this->fileName.' '.$this->param2dd($varId).' Fillval');
		if (substr($fillval, 0,5) == 'error') $fillval = '';
		
		return array('type' => $data_type, 'size' => $size, 'n_records' => $n_recs, 'units' => $units, 'fillvalue' => $fillval);
bf74fc2d   Elena.Budnik   IMPEX
253
254
255
256
257
258
259
	}
	
	protected function param2dd($paramID)
	{
		$pairs = array("-" => "_", "%" => "_","\\" => "_","$" => "_",":" => "_","+" =>" _","-" => "_","#" => "_","@" => "_", "." => "_",">" => "_", "<" => "_");    
		return strtr($paramID, $pairs); 
	}
16035364   Benjamin Renard   First commit
260
261
262
263

/*
*      ascii format processing
*/    
bf74fc2d   Elena.Budnik   IMPEX
264
265
266
267
268
269
270
271
272
273
	protected function getTxtSampling() 
	{		
		$StartTime = null;
		if (!file_exists($this->fileName)) return -100;     
		$i = 0;
		$dt = -10;
		
		$handler = fopen($this->fileName, 'r');
		if ($handler) {
				while (!feof($handler) && !$StartTime) {
e43d3d59   Nathanaël Jourdane   Fix errors when i...
274
275
						$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
						$elems = explode(' ', $oneLine);
74f90d45   Elena.Budnik   correct time form...
276
						if ((strlen($elems[0]) < 15) || !($time = strtotime($elems[0]))) {
bf74fc2d   Elena.Budnik   IMPEX
277
278
279
280
281
282
283
284
285
								$i++;
								continue;
					}                   
					$StartTime = $time;                    
					if (is_numeric($elems[1])) $dt = $elems[1];  
					//else Array for sure !!!
				}
		}

57f8eda0   Benjamin Renard   Fix some errors d...
286
		if (feof($handler)) return -10;
bf74fc2d   Elena.Budnik   IMPEX
287

e43d3d59   Nathanaël Jourdane   Fix errors when i...
288
		$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
ed15f51c   Nathanaël Jourdane   import file: remo...
289
		$elems = explode(' ',$oneLine);
e43d3d59   Nathanaël Jourdane   Fix errors when i...
290

bf74fc2d   Elena.Budnik   IMPEX
291
292
293
294
295
296
297
298
299
		if (count($elems) < 2) return -10;

		// dT else Array
		if (!($time = strtotime($elems[0]))) {
				return $dt;
		}
		
		return $time  - $StartTime;          
	}
16035364   Benjamin Renard   First commit
300
301

 
bf74fc2d   Elena.Budnik   IMPEX
302
303
304
305
306
307
308
309
310
	public function getTxtSamplings() 
	{    
		$StartTime = null;
		if (!file_exists($this->fileName)) return array(-100);

		$handler = fopen($this->fileName, 'r');
		if ($handler) {
			$i = 0;
			while (!feof($handler) && !$StartTime) {
ed15f51c   Nathanaël Jourdane   import file: remo...
311
312
						$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
						$elems = explode(' ',$oneLine);
0b0d0e28   Elena.Budnik   time format secon...
313
						if (strlen($elems[0]) < 15 || !($time = strtotime($elems[0]))) { 
bf74fc2d   Elena.Budnik   IMPEX
314
315
316
317
318
319
320
321
322
323
324
									$i++;
									continue;
					}   
			
						$StartTime = $time;
						if (!(is_numeric($elems[1]))) return array(-10); 
						$dt = $elems[1];                  
			} 
		} 
		
		if (feof($handle)) return array(-10);
ed15f51c   Nathanaël Jourdane   import file: remo...
325
326
		$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
		$elems = explode(' ',$oneLine);
bf74fc2d   Elena.Budnik   IMPEX
327
		if (count($elems) < 2) return array (-1);
16035364   Benjamin Renard   First commit
328
        // dT else Array
bf74fc2d   Elena.Budnik   IMPEX
329
330
331
332
333
334
335
336
337
		if (!($time = strtotime($elems[0]))) 
			return array($dt);
		else 
		{
			$minSampling = +1.e31;
			$maxSampling = 0.0;

			while (!feof($handler)) 
			{
ed15f51c   Nathanaël Jourdane   import file: remo...
338
339
340
				$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
				$elems = explode(' ',$oneLine);

bf74fc2d   Elena.Budnik   IMPEX
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
				if (count($elems) < 2) continue;
				$StartTime = $time;
				if (!($time = strtotime($elems[0]))) return array(-1);                       
				$deltaT[$time  - $StartTime]++;                                       
			}

			foreach ($deltaT as $key => $value) 
			{
							if ($value < 5) continue;
							if ($key < $minSampling) $minSampling = $key;
							if ($key > $maxSampling) $maxSampling = $key;
			} 

	return array($minSampling,$maxSampling); 
		}         
	}
16035364   Benjamin Renard   First commit
357
358
359
360

/*
*        Delete comments and convert time into double
*/
bf74fc2d   Elena.Budnik   IMPEX
361
362
363
364
365
366
367
368
	public function reformatTxt() 
	{        
		$StartTime = null;
		if (!file_exists($this->fileName)) return  -100;

		$handler = fopen($this->fileName, 'r');
		if ($handler) 
		{  
57f8eda0   Benjamin Renard   Fix some errors d...
369
			$offset = ftell($handler);
bf74fc2d   Elena.Budnik   IMPEX
370
371
372
			$i = 0;
			while (!$StartTime && !feof($handler)) 
			{
ed15f51c   Nathanaël Jourdane   import file: remo...
373
374
375
				$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
				$elems = explode(' ',$oneLine);

bf74fc2d   Elena.Budnik   IMPEX
376
377
				if (!($time = strtotime($elems[0]))) 
				{
57f8eda0   Benjamin Renard   Fix some errors d...
378
						$offset = ftell($handler);
bf74fc2d   Elena.Budnik   IMPEX
379
380
381
382
383
384
385
						$i++;
						continue;
				}            
				$StartTime = $time;
				$dt = $elems[1];
			}  
		}
16035364   Benjamin Renard   First commit
386
                 
bf74fc2d   Elena.Budnik   IMPEX
387
		$newfile = fopen(USERDATADIR."temp", "w");
ed15f51c   Nathanaël Jourdane   import file: remo...
388
389
390
391

		$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
		$elems = explode(' ',$oneLine);

bf74fc2d   Elena.Budnik   IMPEX
392
393
394
395
396
397
398
399
400
		// dT
		if (!($time = strtotime($elems[0])))
		{ 
			$newtime = $StartTime;  
			settype($dt, "float"); 
			if ($oneLine != PHP_EOL && count($elems) > 0) fwrite($newfile,  $newtime." ".$oneLine.PHP_EOL);
			$newtime += $dt; 

			while (!feof($handler)) 
ed15f51c   Nathanaël Jourdane   import file: remo...
401
402
			{
				$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
bf74fc2d   Elena.Budnik   IMPEX
403
404
405
406
407
408
409
410
411
				if ($oneLine != PHP_EOL && count($elems) > 0) fwrite($newfile,  $newtime." ".$oneLine.PHP_EOL);
				$newtime += $dt;  
			}
		}
		// Array
		else 
		{ 
			fseek($handler, $offset);
			while (!feof($handler)) 
ed15f51c   Nathanaël Jourdane   import file: remo...
412
413
414
			{
				$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handler)));
				$elems = explode(' ',$oneLine);
bf74fc2d   Elena.Budnik   IMPEX
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
				// empty lines
				if (count($elems) < 2) continue;
				$newtime = strtotime($elems[0]); 
				//TODO Error message if wrong time format...???
				if (!$newtime) continue; 
				unset($elems[0]); 
				$newline = join($elems," ");
				fwrite($newfile,$newtime." ". $newline.PHP_EOL);                                   
			}
		}
		fclose($newfile);

		rename(USERDATADIR."temp",$this->fileName);     

		return $StartTime." ".$newtime; 
	}
16035364   Benjamin Renard   First commit
431
432
433
434

/*
*  Convert time into ISO
*/
bf74fc2d   Elena.Budnik   IMPEX
435
436
437
	protected function reformatTime($timeFormat, $timeLength, $doy) 
	{                   
		$formatLength = $timeLength == 'auto' ? strlen($timeFormat) + (strpos($timeFormat,"Y") !== false)*3 + (strpos($timeFormat,"y") !== false)
39b3dd70   Elena.Budnik   Megan bug : no z ...
438
                        + (strpos($timeFormat,"z") !== false)*2
bf74fc2d   Elena.Budnik   IMPEX
439
440
441
442
						+ (strpos($timeFormat,"M") !== false)*2  + (strpos($timeFormat,"m") !== false)
						+ (strpos($timeFormat,"d") !== false) + (strpos($timeFormat,"H") !== false)
						+ (strpos($timeFormat,"i") !== false) + (strpos($timeFormat,"s") !== false) 
						+ (strpos($timeFormat,".u") !== false)*2 : $timeLength;
16035364   Benjamin Renard   First commit
443
 
bf74fc2d   Elena.Budnik   IMPEX
444
445
446
447
448
		if (strpos($timeFormat,".u") !== false) 
		{
			$formatLength = $formatLength - 4;
			$timeFormat = substr($timeFormat, 0, strlen($timeFormat)-2);
		}
16035364   Benjamin Renard   First commit
449

bf74fc2d   Elena.Budnik   IMPEX
450
451
452
453
454
		if (strpos($timeFormat,".k") !== false) 
		{
			$formatLength = $formatLength - 2;
			$timeFormat = substr($timeFormat, 0, strlen($timeFormat)-2);
		}
16035364   Benjamin Renard   First commit
455

bf74fc2d   Elena.Budnik   IMPEX
456
		$handle = fopen($this->fileName,'r');
16035364   Benjamin Renard   First commit
457
   
bf74fc2d   Elena.Budnik   IMPEX
458
459
460
461
462
		if ($handle) 
		{    
			$newfile = fopen(USERDATADIR."temp", "w");
			while (!feof($handle)) 
			{
ed15f51c   Nathanaël Jourdane   import file: remo...
463
464
465
				$oneLine = trim(preg_replace('/\s+/', ' ', fgets($handle)));

				if ($oneLine !== false)
bf74fc2d   Elena.Budnik   IMPEX
466
				{
ed15f51c   Nathanaël Jourdane   import file: remo...
467
					if (strlen($oneLine) > $formatLength)
bf74fc2d   Elena.Budnik   IMPEX
468
469
470
					{   
						try 
						{
ed15f51c   Nathanaël Jourdane   import file: remo...
471
							$date = DateTime::createFromFormat($timeFormat, trim(substr($oneLine,0,$formatLength)));
bf74fc2d   Elena.Budnik   IMPEX
472
473
							if (!$date) 
							{
ed15f51c   Nathanaël Jourdane   import file: remo...
474
								if (strpos($oneLine,'#') === 0)  fwrite($newfile, $oneLine.PHP_EOL);
39b3dd70   Elena.Budnik   Megan bug : no z ...
475
								else return false;
bf74fc2d   Elena.Budnik   IMPEX
476
477
478
479
480
481
482
							}
							else 
							{
								// DOY starts from 1
								if ($doy) {
									$date->modify('-1 day');
								}
ed15f51c   Nathanaël Jourdane   import file: remo...
483
								$suffix =  preg_replace('/\s+/', ' ',substr($oneLine, $formatLength));
bf74fc2d   Elena.Budnik   IMPEX
484
485
486
487
488
								fwrite($newfile,$date->format('Y-m-d')."T".$date->format('H:i:s').$suffix.PHP_EOL);
							}
						}
						catch (Exception $e) 
						{		  			  
39b3dd70   Elena.Budnik   Megan bug : no z ...
489
							return false;		   
bf74fc2d   Elena.Budnik   IMPEX
490
491
492
						}               	       
					} 
					else 
ed15f51c   Nathanaël Jourdane   import file: remo...
493
						if (strpos($oneLine,'#') === 0) fwrite($newfile,$oneLine.PHP_EOL);
bf74fc2d   Elena.Budnik   IMPEX
494
495
496
497
498
499
500
501
502
503
				}
			}
		}
		else 
		{
			if (file_exists($this->fileName)) 
			{
				fclose($handle); 
				unlink($this->fileName);
			}
ed15f51c   Nathanaël Jourdane   import file: remo...
504
			return false;
bf74fc2d   Elena.Budnik   IMPEX
505
506
507
508
509
510
511
512
		}

		fclose($newfile); 
		fclose($handle);

		rename(USERDATADIR."temp", $this->fileName);  
		return true; 
	}
16035364   Benjamin Renard   First commit
513
514
515
   

/*
bf74fc2d   Elena.Budnik   IMPEX
516
*    Returns number of columns in text file to create data model fields in MyDataUI.js 
16035364   Benjamin Renard   First commit
517
*/
bf74fc2d   Elena.Budnik   IMPEX
518
519
520
521
522
523
524
525
526
527
528
529
	protected function getTxtColNumber($timeDouble) 
	{	  
		if (!file_exists($this->fileName)) return  -100;
		$handle = fopen($this->fileName,'r');

		if ($handle) 
		{
			$found = false;
			$i = 0;
			while (($line = trim(fgets($handle))) !== false && $i < 100) 
			{
				$elems = explode(" ", preg_replace('/\s+/', ' ', $line));
bf74fc2d   Elena.Budnik   IMPEX
530
				if (count($elems) < 2 || strpos($elems[0], '#') === 0) continue;
a087d0b3   Benjamin Renard   Fix bug when an u...
531
532

				$i++;		
bf74fc2d   Elena.Budnik   IMPEX
533
534
				if (!$timeDouble) 
				{
74f90d45   Elena.Budnik   correct time form...
535
					if (strlen($elems[0]) >= 15 && ($time = strtotime($elems[0]))) 
bf74fc2d   Elena.Budnik   IMPEX
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
					{
						$found = true;
						break; 
					}
				}
				else 
				{
					$found = true;
					break; 
				}
			}

			if (!feof($handle) && !$found) 
			{
				fclose($handle);
				unlink($this->fileName);
				return -1;
			}
		}       
		fclose($handle);

		if (!$found) 
		{
			unlink($this->fileName);
			return 0; 
		}
		
		return count($elems);
	}
16035364   Benjamin Renard   First commit
565
566
567
568
    
/*
*      new file tag in Files.xml
*/      
bf74fc2d   Elena.Budnik   IMPEX
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
	protected function createSimuFile($format) 
	{              
		$newFile = $this->contentDom->createElement('file');
		$newFile->setAttribute('xml:id', $this->fileId);
		$newFile->setAttribute('name', $this->fileId);
		$newFile->setAttribute('format', $format);

		$start_stop = explode(" ",$this->getVotFileMgr()->getStartStop());
		$samplings = $this->getVotFileMgr()->getSamplings();
		$minSamp = $samplings["minSampling"];
		$maxSamp = $samplings["maxSampling"];
		$desc = $this->getVotFileMgr()->getDescription();

		$start =  $start_stop[0];
		$stop =  $start_stop[1];
16035364   Benjamin Renard   First commit
584
                
bf74fc2d   Elena.Budnik   IMPEX
585
586
587
588
589
		$newFile->setAttribute("start",$start);
		$newFile->setAttribute("stop",$stop);
		$newFile->setAttribute('minsampling',$minSamp);
		$newFile->setAttribute('maxsampling',$maxSamp);
		$newFile->setAttribute('uploaded', date('Y-m-d H:i:s'));
16035364   Benjamin Renard   First commit
590
		  
bf74fc2d   Elena.Budnik   IMPEX
591
592
593
594
595
596
597
598
599
600
601
602
		// generic MASK without datas for simu params
		if ((strncmp($this->fileId, "impex___", 8) == 0) || (strncmp($this->fileId, "spase___", 8) == 0))
			$newFile->setAttribute('mask', $this->getGenericMask());

		$newFile->nodeValue = date('Y-m-d',$start)."T".date('H:i:s',$start)."-".date('Y-m-d',$stop)."T".date('H:i:s',$stop);
		if ($desc != '')
				$newFile->nodeValue .= (PHP_EOL.$desc);
				
		return $newFile;   
	}


7d539a8c   Elena.Budnik   server side modifs
603
	protected function createFile($format, $samplingType) 
bf74fc2d   Elena.Budnik   IMPEX
604
605
606
607
608
	{              
		$newFile = $this->contentDom->createElement('file');
		$newFile->setAttribute('xml:id', $this->fileId);
		$newFile->setAttribute('name', $this->fileId);
		$newFile->setAttribute('format', $format);
7d539a8c   Elena.Budnik   server side modifs
609
610
611
612
		
		$minSamp = $this->sampling;
		$maxSamp = $this->maxSampling;
		
bf74fc2d   Elena.Budnik   IMPEX
613
614
615
616
		switch ($format)
		{
			case "cdf":  
					$start_stop = explode(" ",$this->getCdfStartStop());
7d539a8c   Elena.Budnik   server side modifs
617
618
619
620
621
622
623
624
625
626
					if ($minSamp < 0) {
						if ($samplingType == 'constant') {
							$result = $this->getCdfSampling();
							//TODO process errors
							$minSamp = $result[count($result)-1];
							$maxSamp =  $minSamp;
						} else {
							// $result = $this->getCdfSamplings();
						}
					}
bf74fc2d   Elena.Budnik   IMPEX
627
628
629
630
631
632
633
634
635
636
637
				break;            
			case "txt":   
					$start_stop =  explode(" ",$this->reformatTxt());
					if ($start_stop[0] < 0)  return $start_stop;
					$minSamp = $this->sampling;
					$maxSamp = $this->maxSampling;
				break; 
			case "cef":
					// Test if 'standard' meta exist in CEF
					$cefMetaTest = $this->getCefStartStop(); 

7d539a8c   Elena.Budnik   server side modifs
638
					if ($cefMetaTest != -1) {
bf74fc2d   Elena.Budnik   IMPEX
639
640
641
642
643
644
						$cefStartStop = explode("/",$cefMetaTest); 
						$start_stop[] = strtotime($cefStartStop[0]);
						$start_stop[] = strtotime($cefStartStop[1]); 			   
						$result = $this->getCefSampling();
						$minSamp = $result[count($result)-1];
					}
7d539a8c   Elena.Budnik   server side modifs
645
					else {
bf74fc2d   Elena.Budnik   IMPEX
646
647
648
649
650
651
652
653
654
655
656
657
658
						// no META data - so time processing
						$timeInfo = $this->getCefTimeInfo();
						$timeInfoArr = explode(" ", $timeInfo);
						$start_stop[] =  $timeInfoArr[0];
						$start_stop[] =  $timeInfoArr[1];
						$minSamp = $timeInfoArr[2];
					}
					//TODO if min & max
					$maxSamp =  $minSamp;
				break;
			case "vot" :
					$start_stop = explode(" ",$this->getVotFileMgr()->getStartStop());
					$samplings = $this->getVotFileMgr()->getSamplings();
7d539a8c   Elena.Budnik   server side modifs
659
660
661
662
					if ($minSamp < 0) { 
						$minSamp = $samplings["minSampling"];
						$maxSamp = $samplings["maxSampling"];
					}
bf74fc2d   Elena.Budnik   IMPEX
663
664
665
666
667
668
669
670
671
					$desc = $this->getVotFileMgr()->getDescription();
				break;  
			case "nc":  
					$ncInfo = $this->getNcTimeInfo();
					if ($ncInfo < 0) {
							unlink($this->fileName);
							return $ncInfo;
					}
					$ncInfoArr = explode("#",$ncInfo);
7d539a8c   Elena.Budnik   server side modifs
672
673
674
675
676
677
678
					$start_stop = explode(":",$ncInfoArr[0]);
					if ($minSamp < 0) {
						//TODO process errors
						$minSamp = $ncInfoArr[1];
						//TODO if min & max
						$maxSamp =  $minSamp;
					}
bf74fc2d   Elena.Budnik   IMPEX
679
680
681
682
683
684
685
686
687
688
689
				break;   
			default: 
		}
		$start =  $start_stop[0];
		$stop =  $start_stop[1];
					
		$newFile->setAttribute("start",$start);
		$newFile->setAttribute("stop",$stop);
		$newFile->setAttribute('minsampling',$minSamp);
		$newFile->setAttribute('maxsampling',$maxSamp);
		$newFile->setAttribute('uploaded', date('Y-m-d H:i:s'));
16035364   Benjamin Renard   First commit
690
		  
bf74fc2d   Elena.Budnik   IMPEX
691
692
693
694
695
696
697
		// generic MASK without datas for simu params		  
		if ((strncmp($this->fileId, "impex___", 8) === 0) || (strncmp($this->fileId, "spase___", 8) === 0))
			$newFile->setAttribute('mask', $this->getGenericMask());
		else
			$newFile->setAttribute('mask', $this->fileId);

		$newFile->nodeValue = date('Y-m-d',$start)."T".date('H:i:s',$start)."-".date('Y-m-d',$stop)."T".date('H:i:s',$stop);
57f8eda0   Benjamin Renard   Fix some errors d...
698
		if (!empty($desc))
bf74fc2d   Elena.Budnik   IMPEX
699
700
701
702
			$newFile->nodeValue .= (PHP_EOL.$desc);
		
		return $newFile;   
	}
16035364   Benjamin Renard   First commit
703
     
bf74fc2d   Elena.Budnik   IMPEX
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
	protected function getGenericMask() 
	{
		if (strncmp($this->fileId, "impex___", 8) == 0) {
			$prefix = "impex___";
			$tmpFileName = str_replace ("impex___", "", $this->fileId);
		}
		elseif (strncmp($this->fileId, "spase___", 8) == 0) {
			$prefix = "spase___";
			$tmpFileName = str_replace ("spase___", "", $this->fileId);
		}
		$tmpFileArray = explode( '.', $tmpFileName);
		$tmpParamArray = explode( '_', $tmpFileArray[0]);
		array_pop($tmpParamArray);
		array_pop($tmpParamArray);
		$tmpName =  implode("_", $tmpParamArray);
		
		$mask = $prefix.$tmpName."_*.xml";
		return $mask;
	}
16035364   Benjamin Renard   First commit
723
724
725
726

/*
//TODO  check how to upgrade this
*/
bf74fc2d   Elena.Budnik   IMPEX
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
	protected function getFormat() 
	{         
		$ext = pathinfo($this->fileName, PATHINFO_EXTENSION);   

		if (PHP_VERSION_ID < 50300) 
		{
			$finfo = new finfo(FILEINFO_MIME, MAGIC_FILE);    
			$file_info =   explode(";", $finfo->file($this->fileName));
		}
		else 
		{
			// PHP v >= 5.3 comes with its own magic file  !!!!   
			$finfo = finfo_open(FILEINFO_MIME); 
			$file_info =  explode(";",finfo_file($finfo, $this->fileName));
		}
16035364   Benjamin Renard   First commit
742

bf74fc2d   Elena.Budnik   IMPEX
743
744
745
746
747
		if (strpos($file_info[0],"plain") !== false) 
		{
			if (strpos($ext,"cef") !== false) return "cef";
			return "txt";
		}
16035364   Benjamin Renard   First commit
748

bf74fc2d   Elena.Budnik   IMPEX
749
750
751
752
753
		if (strpos($file_info[0],"application/x-gzip") !== false) 
		{
			if (strpos($this->fileName,"cef.gz") !== false) return "cef";  
		}

0fc65398   Benjamin Renard   Fix detection of ...
754
		if ((strpos($file_info[0],"application/xml") !== false) || (strpos($file_info[0],"text/xml") !== false))
bf74fc2d   Elena.Budnik   IMPEX
755
756
757
758
759
		{
			//if ($this->getVotFileMgr()->isValidSchema()) //BRE : No schema validation for the moment due to some trouble with Topcat (it can create not valid VOTable)
			return "vot";
		}
	
91892b01   Elena.Budnik   netcdf4 x-hdf
760
761
		// netcdf4 - application/x-hdf
		if (strpos($file_info[0],"octet-stream") !== false || strpos($file_info[0],"x-hdf") !== false) 
bf74fc2d   Elena.Budnik   IMPEX
762
763
764
765
766
767
768
		{
			if (strpos($ext,"cdf") !== false) return "cdf"; 
			if (strpos($ext,"nc") !== false) return "nc";		
		}
		
		return "unknown";      
	}
16035364   Benjamin Renard   First commit
769
770
771

/*
*         PUBLIC FUNCTIONS
bf74fc2d   Elena.Budnik   IMPEX
772
773
774
775
*/     
	public function getAsciiFile($name) 
	{ 
		$this->fileName = USERDATADIR.$name;
16035364   Benjamin Renard   First commit
776

bf74fc2d   Elena.Budnik   IMPEX
777
778
		if (!file_exists(USERDATADIR.$name)) 
			return  array('success' => false, 'error' => 'No such file');
16035364   Benjamin Renard   First commit
779
 	 
bf74fc2d   Elena.Budnik   IMPEX
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
		$handle = fopen($this->fileName, "r");
		$fileToReturn = array();
		$index = 0;
		$tempFileToReturn =  array();

		while (!feof($handle)) 
		{
			$line = trim(fgets($handle));
			$tempArr = explode(' ', preg_replace('/\s+/', ' ', $line));
			if (count($tempArr) > 1) 
			{
				for ($i = 0; $i < count($tempArr); $i++) 
				{
					$name = $i === 0 ? 'Time' : 'n'.$i;
					//transform double time to ISO
					if ($name == 'Time') 
					{
						$temp = $tempArr[0];
						settype($temp, 'double');
						$tempArr[0] = date('Y-m-d H:i:s', $temp);
					}
					$tempFileToReturn[$name] = $tempArr[$i];
				}
				
				$fileToReturn[$index] = $tempFileToReturn;
				$index++;
				if ($index > MAX_FILE_INDEX_TO_SHOW) break;
			}
		}
		return $fileToReturn;
	}
16035364   Benjamin Renard   First commit
811
812
813
/*
*     Get  info on parameter
*/
bf74fc2d   Elena.Budnik   IMPEX
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
	public function getParamInfo($obj)
	{
		$this->fileName = USERDATADIR.$obj->file;
		$varId = $obj->varName;
		$format = $obj->format;

		// at first info from Files.xml
		$file = $this->contentDom->getElementById($obj->file);
		if ($file) 
		{
			if ($file->parentNode->tagName == 'folder') 
				$mask = $file->parentNode->getAttribute('name');
			else 
				$mask = $file->getAttribute('mask');
				
			$dom_info =  array( 'name' => $varId,
							'minsampling' => $file->getAttribute('minsampling'),
							'maxsampling' => $file->getAttribute('maxsampling'),
							'mask' => $mask);		 
		}
		else 
		{
			if ($format == "nc")
			{ 
				$status = $this->reformatNcTime();
				if ($status <= 0) 
				{
					unlink($this->fileName);
					return array('success' => false, 'error' => 'error '.$timeFormat.PHP_EOL.'Time Format problem');	
				}
				$ncInfo = $this->getNcTimeInfo(); 
				$ncVars = $this->getNcVars();
				
				if ($ncInfo < 0) 
				{
					unlink($this->fileName);
					return $ncInfo;
				}
				
				$ncInfoArr = explode("#",$ncInfo);
				$start_stop = explode(":",$ncInfoArr[0]);			   
				//TODO process errors
				$minSamp = $ncInfoArr[1];
				//TODO if min & max
				$maxSamp =  $minSamp;
16035364   Benjamin Renard   First commit
859
			      
bf74fc2d   Elena.Budnik   IMPEX
860
861
862
863
864
865
				$dom_info =  array( 'name' => $varId,
								'minsampling' => $minSamp,
								'maxsampling' => $maxSamp,
								'mask' => $obj->mask);	
			}
		}
16035364   Benjamin Renard   First commit
866

bf74fc2d   Elena.Budnik   IMPEX
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
		switch ($format)
		{
			case "cdf":  
				$info = $this->getCdfVarInfo($varId);			      
				break;   
			case "txt":   			  
				break; 
			case "cef":   
				$info = $this->getCefVarInfo($varId);
				break; 
			case "nc":  
				$info = $this->getNcVarInfo($varId);
				break;
			case "vot":
				$info = $this->getVotFileMgr()->getFieldInfoByID($varId);
				break;
			default: 
16035364   Benjamin Renard   First commit
884

bf74fc2d   Elena.Budnik   IMPEX
885
886
887
		}
		return  array('success' => true, 'info' => array_merge($dom_info,$info));
	}
16035364   Benjamin Renard   First commit
888

16035364   Benjamin Renard   First commit
889
 
bf74fc2d   Elena.Budnik   IMPEX
890
891
892
	public function deleteObject($obj) 
	{
		$myBaseManager = new BaseManager();
16035364   Benjamin Renard   First commit
893
  
bf74fc2d   Elena.Budnik   IMPEX
894
895
		$paramMgr = new DerivedParamMgr('myDataParam');                
		$xpd = new domxpath($paramMgr->contentDom); 
16035364   Benjamin Renard   First commit
896

bf74fc2d   Elena.Budnik   IMPEX
897
898
899
900
		//File, not a folder
		if ($obj->leaf) 
		{  
			$this->fileName = USERDATADIR.$obj->id;
16035364   Benjamin Renard   First commit
901

bf74fc2d   Elena.Budnik   IMPEX
902
903
904
905
906
907
			if (file_exists($this->fileName)) 
					unlink($this->fileName);

			$theFile = $this->contentDom->getElementById($obj->id);               
			// Update data base	                   
			$fileInBase = $myBaseManager->xp->query("//file[@name='".$obj->id."']");
16035364   Benjamin Renard   First commit
908
 
bf74fc2d   Elena.Budnik   IMPEX
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
			if ($fileInBase->length > 0) 
			{ 
				$viInfo = $myBaseManager->delFile($fileInBase->item(0));

				$mask = $viInfo['mask'];
				$length = $viInfo['length'];
			
				$params = $xpd->query("//mydata[@mask='".$mask."']"); 

				if ($params->length > 0) 
				{  
					// NO FILES in the mask;  corresponding parameters are to be deleted
					if ($length == 0) 
					{
						$paramsToDelete = array();
						for ($i = $params->length; --$i >= 0; )
						{
							$id = $params->item($i)->getAttribute('xml:id');
							$paramMgr->deleteParameter($id);
							$paramMgr->deleteFromContent($params->item($i));
							$paramsToDelete[] = $id;
						}                                       
					}
					// update params description in Content Dom
					else 
					{
						$vi = $myBaseManager->getVi($mask);   
						$desc =  $myBaseManager->getStartStop($vi);
						foreach ($params as $param) $param->setAttribute("desc",$desc); 
						$paramMgr->saveContent();
					}				  		
				}                
			}  
			// WS/Files.xml
			if ($theFile) 
			{
57f8eda0   Benjamin Renard   Fix some errors d...
945
				if (isset($desc)) 
bf74fc2d   Elena.Budnik   IMPEX
946
947
948
949
						$theFile->parentNode->setAttribute('info', $desc);
				$theFile->parentNode->removeChild($theFile);               
				$this->contentDom->save($this->xmlName); 
			}
16035364   Benjamin Renard   First commit
950
		}
bf74fc2d   Elena.Budnik   IMPEX
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
		// Folder
		else 
		{
			$mask = $obj->id;
			$theMask = $this->contentDom->getElementById($obj->id);
			$files = $theMask->getElementsByTagName('file');

			foreach ($files as $file) 
			{
				$fileName = USERDATADIR.$file->getAttribute('name');  	  
				if (file_exists($fileName)) unlink($fileName); 
			}

			$this->deleteFromContent($theMask); 
			$myBaseManager->delVI($mask);  

			$params = $xpd->query("//mydata[@mask='".$mask."']");  
			$paramsToDelete = array();
			for ($i = $params->length; --$i >= 0; ) 
			{
				$id = $params->item($i)->getAttribute('xml:id');
				$paramMgr->deleteParameter($id);
				$paramMgr->deleteFromContent($params->item($i));
				$paramsToDelete[] = $id;
			}
		}
		// No more files for myDataParams	
57f8eda0   Benjamin Renard   Fix some errors d...
978
		if (isset($paramsToDelete)) 
bf74fc2d   Elena.Budnik   IMPEX
979
980
981
				return array('id' => $obj->id, 'params' => $paramsToDelete);

		// update info for myDataParams Start Stop were changed
57f8eda0   Benjamin Renard   Fix some errors d...
982
		return  array('id' => $obj->id, 'mask' => isset($mask) ? $mask : NULL, 'maskDesc' => isset($desc) ? $desc : NULL);
16035364   Benjamin Renard   First commit
983
	}
bf74fc2d   Elena.Budnik   IMPEX
984
         
16035364   Benjamin Renard   First commit
985
986
987
/*
*      recombine files in Files.xml according to new Mask
*/
57f8eda0   Benjamin Renard   Fix some errors d...
988
	public function addMask($fileMask) 
bf74fc2d   Elena.Budnik   IMPEX
989
990
991
992
993
994
	{ 
		if ($this->contentDom ->getElementById($fileMask) != null) return false; 
		$filesInRoot = $this->xp->query("/ws/fileList/file");                                   
		$newMask = $this->contentDom->createElement("folder");
		$newMask->setAttribute('xml:id', $fileMask);
		$newMask->setAttribute('name', $fileMask);
16035364   Benjamin Renard   First commit
995
     
bf74fc2d   Elena.Budnik   IMPEX
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
		for ($i =  $filesInRoot->length; --$i >= 0;)  
		{
			$aFile =  $filesInRoot->item($i);  
			// mask corresponds to file name		 
			if ($this->fileMask($aFile->getAttribute("name"),$fileMask))
			{                          
				$newMask->appendChild($aFile); 
			}                          
		}

		$files = $newMask->getElementsByTagName("file");
		if ($files->length == 0) return false;
16035364   Benjamin Renard   First commit
1008
 
bf74fc2d   Elena.Budnik   IMPEX
1009
1010
1011
1012
1013
		foreach ($files as $file) 
		{
			$starts[] =  $file->getAttribute("start");
			$stops[] =  $file->getAttribute("stop");
		}
16035364   Benjamin Renard   First commit
1014
 
bf74fc2d   Elena.Budnik   IMPEX
1015
		$newMask->setAttribute('info',date('Y-m-d',min($starts))."T".date('H:i:s',min($starts))."-".date('Y-m-d',max($stops))."T".date('H:i:s',max($stops)));
16035364   Benjamin Renard   First commit
1016

bf74fc2d   Elena.Budnik   IMPEX
1017
		$fileList = $this->contentDom->getElementById($this->contentRootId);
16035364   Benjamin Renard   First commit
1018
	      
bf74fc2d   Elena.Budnik   IMPEX
1019
1020
1021
1022
		$fileList->appendChild($newMask);
		$this->contentDom->save($this->xmlName); 
		return true;
	}
16035364   Benjamin Renard   First commit
1023
1024
1025
1026
1027

/*
*    check correspondence FleName <=> Mask
*    returns true if Mask fits FileName
*/
bf74fc2d   Elena.Budnik   IMPEX
1028
1029
1030
1031
1032
	public function fileMask($fileName, $maskName) 
	{    
		$mask = explode("*", $maskName); 
		if (strpos($fileName, $mask[0]) !== 0) return false; 
		if (count($mask) == 1) return true;
16035364   Benjamin Renard   First commit
1033

bf74fc2d   Elena.Budnik   IMPEX
1034
1035
1036
		for ($i = 1; $i < count($mask); $i++)  
			if ($mask[$i] != null)
				if (strpos($fileName, $mask[$i]) === false) return false;  
16035364   Benjamin Renard   First commit
1037

bf74fc2d   Elena.Budnik   IMPEX
1038
1039
		return true;
	} 
16035364   Benjamin Renard   First commit
1040
1041
1042
1043

/*
*    Delete Mask
*/
bf74fc2d   Elena.Budnik   IMPEX
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
	public function delMask($oldMask) 
	{ 
		$mask = $this->contentDom->getElementById($oldMask);

		if ($mask != null && $mask->tagName == "folder") 
		{                                                                                                               
			$filesInMask = $mask->getElementsByTagName("file");
			$fileList = $this->contentDom->getElementById($this->contentRootId); 

			while ($filesInMask->length > 0) 
				$fileList->appendChild($filesInMask->item(0));
					
			$fileList->removeChild($mask); 
			$this->contentDom->save($this->xmlName);

			return true;
		}
16035364   Benjamin Renard   First commit
1061

bf74fc2d   Elena.Budnik   IMPEX
1062
1063
		return false;
	}
16035364   Benjamin Renard   First commit
1064
1065
1066
1067

/*
*     mask or file
*/
bf74fc2d   Elena.Budnik   IMPEX
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
	public function getObject($name) 
	{
		$object = $this->contentDom->getElementById($name);
		
		if (is_object($object)) 
		{   
			// mask
			if ($object->tagName == 'folder') 
			{
				$file = $object->getElementsByTagName('file');
				if ($file->length > 0) 
					return $this->getUploadedObject($file->item(0)->getAttribute('name'));
				else 
					return array('success' => false, 'error' => 'Mask is empty');
			}
		}
		else 
			return array('success' => false, 'error' => 'No such mask in DOM');
		// file
		return $this->getUploadedObject($name);
	}
16035364   Benjamin Renard   First commit
1089
1090
1091
1092

/*
*  Get generic info on uploaded file
*/
bf74fc2d   Elena.Budnik   IMPEX
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
	public function getUploadedObject($name) 
	{
		$this->fileName = USERDATADIR.$name;
		$file = $this->contentDom->getElementById($name);
		if (!$file) 
				return array('success' => false, 'error' => 'No such file in DOM');

		$format = $file->getAttribute('format');
		$minSamp =  $file->getAttribute('minsampling');
		$maxSamp =  $file->getAttribute('maxsampling');

		$mask = $file->parentNode->tagName  == "folder" ? $file->parentNode->getAttribute("name") : null;
		$maskDesc = $mask ? $file->parentNode->getAttribute('info') : null;
		$desc = $file->nodeValue;
		
		$foundTime = true;
16035364   Benjamin Renard   First commit
1109
                
bf74fc2d   Elena.Budnik   IMPEX
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
		switch ($format)
		{
			case "cdf":  
							$vars = $this->getCdfVars(); 
							break;            
			case "txt":   
							$vars = $this->getTxtColNumber(true);                                 
							break; 
			case "nc" :
							$vars = $this->getNcVars();
							break;
			case "cef":   
							$vars = $this->getCefVars(); 
							break;
			case "vot" :
							$vars = $this->getVotFileMgr()->getFieldsInfo();
							$foundTime = ($this->getVotFileMgr()->getTimeFieldIndex() > -1);
							break;          
			default: 
		}

			return  array('success' => true, 'format' => $format, 'vars' => $vars, 'fileName' => $name, 
							'minsampling' => $minSamp, 'maxsampling' => $maxSamp, 'mask' => $mask, 'maskDesc' => $maskDesc,
							'description' => $desc, 'foundTime' => $foundTime);
	}
16035364   Benjamin Renard   First commit
1135
1136
1137
1138
1139

/*
*      The very first processing of newly uploaded file file
*      $formats = array('fileFormat', 'timeFormat', 'timeSampling', 'nonStandard', 'doy');
*/
bf74fc2d   Elena.Budnik   IMPEX
1140
1141
1142
1143
	public function addFile($fileName, $formats) 
	{  
		$this->fileName = USERDATADIR.$fileName;
		$this->fileId = $fileName;
16035364   Benjamin Renard   First commit
1144

bf74fc2d   Elena.Budnik   IMPEX
1145
1146
		$format = $this->getFormat();

7d539a8c   Elena.Budnik   server side modifs
1147
		if ($format === 'unknown') { 	           
bf74fc2d   Elena.Budnik   IMPEX
1148
1149
1150
1151
			unlink($this->fileName);                   
			return array('success' => false, 'error' => 'Sorry, unknown format of '.$fileName);	
		}

7d539a8c   Elena.Budnik   server side modifs
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
		// if sampling is defined by user
		if ($formats["samplingType"] == "manual") {
			$this->sampling = $formats["min_sampling"];
			if ($formats["max_sampling"])
				$this->maxSampling = $formats["max_sampling"];
			else
				$this->maxSampling = $this->sampling;
		}
		
		
bf74fc2d   Elena.Budnik   IMPEX
1162
1163
		if ($format == 'txt') 
		{  
7d539a8c   Elena.Budnik   server side modifs
1164
			if ($formats["timeFormat"] == "user") {
bf74fc2d   Elena.Budnik   IMPEX
1165
1166
				$res =  $this->reformatTime($formats["nonStandard"], $formats["timeLength"], $formats["doy"]);

7d539a8c   Elena.Budnik   server side modifs
1167
				if (!$res) {
bf74fc2d   Elena.Budnik   IMPEX
1168
1169
1170
1171
1172
1173
1174
					unlink($this->fileName); 
					return array('success' => false, 'error' => 'can\'t reformat time'); 
				}
			}
			// check if file is not empty
			$vars = $this->getTxtColNumber(false);

7d539a8c   Elena.Budnik   server side modifs
1175
			if ($vars == -100) {
bf74fc2d   Elena.Budnik   IMPEX
1176
1177
1178
					return array('success' => false, 'error' => 'no such file');
			}
			
7d539a8c   Elena.Budnik   server side modifs
1179
			if ($vars == -1) { 
bf74fc2d   Elena.Budnik   IMPEX
1180
1181
1182
1183
					unlink($this->fileName); 
					return array('success' => false, 'error' => 'while reading file');
			}

7d539a8c   Elena.Budnik   server side modifs
1184
			if ($vars === 0) {
bf74fc2d   Elena.Budnik   IMPEX
1185
1186
1187
					unlink($this->fileName);
					return array('success' => false, 'error' => 'file contains no data');
			}
7d539a8c   Elena.Budnik   server side modifs
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
			
			// sampling is to be defined automatically
			if ($this->sampling < 0) {
				if ($formats["timeSampling"] == "constant") {          
					$this->sampling = $this->getTxtSampling();	 
					$this->maxSampling = $this->sampling;
				}
				else {
					$samplings = $this->getTxtSamplings();
					$this->sampling = $samplings[0];
					$this->maxSampling = $samplings[1];
				}
bf74fc2d   Elena.Budnik   IMPEX
1200

7d539a8c   Elena.Budnik   server side modifs
1201
1202
1203
1204
1205
1206
1207
				if ($this->sampling <= 0) {  
						unlink($this->fileName); 
						if ($this->sampling == -10) {
								return array('success' => false, 'error' => 'Sorry, can\'t process'.$fileName.PHP_EOL.'. Check if there are non numeric chars in the data');
						}
						return array('success' => false, 'error' => 'Sorry, can\'t process Time for '.$fileName.PHP_EOL.'. Check time format, start time (> 1970-01-01) or sampling time (>= 1s)');		           
				}
bf74fc2d   Elena.Budnik   IMPEX
1208
			}
bf74fc2d   Elena.Budnik   IMPEX
1209
1210
1211
1212
1213
		}

		if ($format == 'nc')
		{  
			$status = $this->reformatNcTime();
7d539a8c   Elena.Budnik   server side modifs
1214
			if ($status <= 0) {
bf74fc2d   Elena.Budnik   IMPEX
1215
1216
1217
1218
1219
1220
				unlink($this->fileName);
				return array('success' => false, 'error' => 'error '.$timeFormat.PHP_EOL.'Time Format problem');	
			}
		}

		//create new file tag with all attributes and add it to the content DOM   
7d539a8c   Elena.Budnik   server side modifs
1221
1222
		$newFile = $this->createFile($format, $formats["timeSampling"]);
		if (is_int($newFile) && ($newFile < 0)) { 
bf74fc2d   Elena.Budnik   IMPEX
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
			unlink($this->fileName);
			return array( 'success' => false, 'file' => $fileName);
		}

		$isMask = false;
		$masks = $this->contentDom->getElementsByTagName("folder");
 
		foreach ($masks as $mask) 
		{
			$folderMask =  $mask->getAttribute("name");  
			if ($this->fileMask($fileName, $folderMask)) 
			{                                                      
				$mask->appendChild($newFile);
				
				$files = $mask->getElementsByTagName("file");
7d539a8c   Elena.Budnik   server side modifs
1238
1239
				
				foreach ($files as $file) {
bf74fc2d   Elena.Budnik   IMPEX
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
					$starts[] = $file->getAttribute("start");
					$stops[] =  $file->getAttribute("stop");
				}
				$mask->setAttribute("info", date('Y-m-d',min($starts))."T".date('H:i:s',min($starts))."-".date('Y-m-d',max($stops))."T".date('H:i:s',max($stops)));
				
				$isMask = true;
				break;
			}	
		}                     
		// no corresponding masks => add to fileList      
7d539a8c   Elena.Budnik   server side modifs
1250
		if (!$isMask) {					  
bf74fc2d   Elena.Budnik   IMPEX
1251
1252
1253
			$filesList = $this->contentDom->getElementById($this->contentRootId); 
			$filesList->appendChild($newFile);
		}        
16035364   Benjamin Renard   First commit
1254
     
bf74fc2d   Elena.Budnik   IMPEX
1255
1256
1257
1258
1259
		$this->contentDom->save($this->xmlName);

		//if mask exists - add to data base
		$myBaseManager = new BaseManager();
		$mask = $myBaseManager->addFile($fileName); 
7d539a8c   Elena.Budnik   server side modifs
1260
		if ($mask != null) {
bf74fc2d   Elena.Budnik   IMPEX
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
			$startstop = $myBaseManager->getStartStop($myBaseManager->getVi($mask));
			$myParamMgr = new DerivedParamMgr('myDataParam');
			$myParamMgr->updateMydata($mask,$startstop);      
		}

		return array( 'success' => true, 'file' => $fileName);
	}
	
	public function setFileName($name) 
	{     
		$this->fileName = USERDATADIR.$name;
	}
16035364   Benjamin Renard   First commit
1273
1274
}
?>