getDataMAPSKP.php
3.21 KB
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
<?php
/**
* @file getDataMAPSKP.php
* @version $Id: getDataMAPSKP.php,v 1.6 2009/08/26 09:32:56 budnik Exp $
* @brief DD_Server tools <br> Gets Data from MAPSKP
*
* Args: remDataSetID ddVIdir StartTime StopTime \n
* Env Vars: $DDBASE $DDBASEBIN \n
* Executables: ascii2nc(DECODERS) TimesUpdate TimesUpdateNoData clean
*
*
* @todo General getData.php(dataBaseID)
*/
function name2DD($fileName) {
$pairs = array(".tab" => ".nc","__" => "_");
return strtr(strtolower($fileName),$pairs) ;
}
// Args
$id = $argv[1];
$ViDir = $argv[2];
$Start = $argv[3];
$Stop = $argv[4];
// Env variables
$DDBASEBIN = getenv("DDBASEBIN");
$DDBASE = getenv("DDBASE");
$MAX_NAME_LENGTH = 31;
$MAX_VI_NAME_LENGTH = 16;
// MAPSKP STUFF
$passwd = "//amda:dd;mapskp@";
$url = "http://planetoweb.cesr.fr/mapskp-amda/MapskpService.php?";
$args = array('service'=>'getDataUrl',
'datasetId'=> $id,
'dateBegin'=> strtr($Start,".000Z","Z"),
'dateEnd'=> strtr($Stop,".000Z","Z"));
// Open STDERR stream
$STDERR = fopen("php://stderr","w");
// Lock VI directory for Updating
// Stamp -> this directory is being updated
$LockName = $ViDir."/LOCK";
touch($LockName);
fprintf($STDERR,$ViDir." is LOCKED\n");
// Send - Receive query
$url .= http_build_query($args);
$fileList = new DOMDocument();
$fileList->load($url);
$Files = $fileList->getElementsByTagName("url");
fprintf($STDERR, "FILES RETURN ". $Files->length."\n");
// Clean VI directory
system("cd ".$ViDir."; ./clean");
// If name is too long....
$nc_prefix = strlen($id) > $MAX_VI_NAME_LENGTH ? substr(strtolower($id),0,15): strtolower($id);
if ($Files->length > 0) {
for ($i = 0; $i < $Files->length; $i++) {
copy($ViDir."/header.nc","temp.nc");
$fileName_ = explode("//",$Files->item($i)->nodeValue);
$fileName = $fileName_[0].$passwd.$fileName_[1];
$fileArr = split("/",$fileName);
// Copy URL file to DD_Server dir
$ok = copy($fileName, $fileArr[count($fileArr)-1]);
if ($ok) {
// Convert ascii TAB to nc using header.nc
system($DDBASEBIN."/ascii2nc ".$fileArr[count($fileArr)-1]);
fprintf($STDERR, "FILE $i TRANSLATED \n");
unlink($fileArr[count($fileArr)-1]);
$ncFile = name2DD($fileArr[count($fileArr)-1]);
// Truncate file if it is too long
$ncFileTrue = ($strLength = strlen($ncFile)) > $MAX_NAME_LENGTH ?
$nc_prefix."_".time().$i.".nc" : $ncFile;
// Move nc file to VIdir
rename("temp.nc", $ViDir."/".$ncFileTrue);
// Updates VI time info
system("cd ".$ViDir."; ".$DDBASEBIN."/TimesUpdate -u ".strtolower($id)."_times.nc ".$ncFileTrue);
}
}
fprintf($STDERR, "TIMES IS UPDATED \n");
}
system("cd ".$ViDir."; ".$DDBASEBIN."/TimesUpdateNoData ".strtolower($id)."_times.nc ".$argv[3]." ".$argv[4]);
// Unlock VI dir
unlink($LockName);
fprintf($STDERR, "UNLOCK \n");
?>