PHPGrads

  • Creatore Discussione Creatore Discussione uniluigi
  • Data di inizio Data di inizio

uniluigi

Nuovo Utente
6 Gen 2009
1
0
1
Salve a tutto il forum,
stavo cercando di creare uno script in php che apra Grads e esegua tutte le funzioni.

Ho scaricato il file zippato di PHPGrads nel quale si trova il file grads.php che è il seguente:

PHP:
<?php

//// MAIN CLASS ////
class Grads {
 var $pref ; // process reference
 var $pipes; // stdio
 var $buffer; // output buffer

//  Main function
 function Grads() {
  $this->pref = 0;
  $this->buffer = "";
  $this->pipes = (array)NULL;
 }

//  Start function
/// initiate a GrADS instance
 function Start ($Args="") {
  global $Echo,$Verb;
  $Bin="C:\Documents and Settings\Administrator\Desktop\GrADS19\win32\gradsnc.exe"; $Echo=0; $Verb=0; $Port=0; $Window=0; $Opts="";
  if($Args) {
   if(array_key_exists('Bin', $Args))   {$Bin=$Args["Bin"];}
   if(array_key_exists('Echo', $Args))  {$Echo=$Args["Echo"];}
   if(array_key_exists('Verb', $Args))  {$Verb=$Args["Verb"];}
   if(array_key_exists('Port', $Args))  {$Port=$Args["Port"];}
   if(array_key_exists('Window', $Args)){$Window=$Args["Window"];}
   if(array_key_exists('Opts', $Args))  {$Opts=$Args["Opts"];}
  }
  if($Port>0) {$Port=" -p";} else {$Port=" -l";}
  if($Window==0) {$Window=" -b";} else {$Window="";}
  $cmd = $Bin.$Window.$Port.$Opts;
  $t = new Grads;
  $descriptor = array (0 => array ("pipe", "r"), 1 => array ("pipe", "w"), 2 => array ("pipe", "w"));
  $t->pref = proc_open ("$cmd ", $descriptor, $t->pipes);
  stream_set_blocking ($t->pipes[1], 0);
  echo " Started <".$cmd."> with pid = ".$t->getPID()."\n";
  $t->tell("start");
  return $t;
 }

//  getPID function
/// gets the PID for a GrADS instance
 function getPID () {
  $status=proc_get_status($this->pref);
  return $status["pid"];
 }

//  getError function
/// Im already working on it. It listens the pipe with error msgs
 function getError () {
  $buffer = "";
  while ($r = fgets ($this->pipes[2], 1024)) { $buffer .= $r; }
  return $buffer;
 }

//  Close function
/// it kills the pipe of a Grads instance
 function Close () {
  $r = proc_close ($this->pref);
  $this->pref = NULL;
  return $r;
 }

//  tell function
/// is the heart of the pipe comunication system between PHP and GrADS. it formats the output and waits for it to go on
/// output = "output" => returns the whole output just like in GrADS command line
///        = "array"  => returns the output "sliced" in an array[LINES][WORDS]
 function tell ($ga_cmd,$output="") {
  global $Echo,$Verb;
  if($ga_cmd!="start") {
   fwrite ($this->pipes[0], $ga_cmd."\n");
  }
  $buffer = $this->buffer; $this->buffer = "";
  $r = fgets($this->pipes[1]);
  if(strpos($r, "</RC>")!==FALSE){ $rcok=explode(" ",$r); }
  while (strpos($r, "</IPC>")===FALSE) {
   $r = fgets($this->pipes[1]);
   if(strpos($r, "</RC>")!==FALSE){ $rcok=explode(" ",$r); }
   $buffer .= $r;
  }
  if($ga_cmd=="quit") {sleep(1);}
  $r = fgets($this->pipes[1]);
  if(strpos($r, "</RC>")!==FALSE){ $rcok=explode(" ",$r); }
  $buffer .= $r;
  if($ga_cmd!="start" & $Verb=="1") {
   echo "\n\n rc = ".$rcok[1]." for <".$ga_cmd.">\n";
  }
  $bfok0=explode("\n",$buffer);
  array_shift($bfok0);array_shift($bfok0); // jump 2 first lines
  array_pop($bfok0);array_pop($bfok0);array_pop($bfok0);array_pop($bfok0); // jump 5 last lines
  $bfok=implode("\n",$bfok0);
  if($Echo=="1") {
   if($bfok!=""){echo "\n".$bfok."\n";}
  }
  if($output!=""){ return $bfok; }
 }

//  quit function
/// quits Grads, holds until all output have arrived from the pipe and then closes the connection between PHP and GrADS
 function quit() {
  global $Echo,$Verb;
  $this->tell("quit");
  echo "\n Destroyed Grads with pid = ".$this->getPID()."\n";
  $this->Close();
 }

//  cmd function
/// executes any command in GrADS, just like it was in the command line
 function cmd($cmd) {
  global $Echo,$Verb;
  $rc=$this->tell($cmd);
  if($output=="array") { return $this->arrange($rc); }
  return $rc;
 }

//  set function
/// execute set commands.
/// output = "output" => returns the whole output just like in GrADS command line
///        = "array"  => returns the output "sliced" in an array like array[LINES][WORDS]
 function set($name,$opt,$output="") {
  global $Echo,$Verb;
  $rc=$this->tell("set ".$name." ".$opt,$output);
  if($output=="array") { return $this->arrange($rc); }
  return $rc;
 }

//  open function
/// open files
 function open($file,$type) {
  global $Echo,$Verb;
  if($type=="ctl") { $this->tell("open ".$file); }
  if($type=="sdf") { $this->tell("sdfopen ".$file); }
  if($type=="xdf") { $this->tell("xdfopen ".$file); }
 }

//  arrange function
/// it "slices" the output of a GrADS command into and array like array[LINES][WORDS]
 function arrange($rc) {
   $rcarray=$rcline=explode("\n",$rc);
   if($rcline[sizeof($rcline)-1]=="" || $rcline[sizeof($rcline)-1]==" ") { array_pop($rcline); }
   for ($i=0;$i<=sizeof($rcline)-1;$i+=1) {
     $rcarray[$i]=explode(" ",$rcline[$i]);
   }
   return $rcarray;
 }

//  display function
/// display information in GrADS windows or in the output.
/// output = "output" => returns the whole output just like in GrADS command line
///        = "array"  => returns the output "sliced" in an array like array[LINES][WORDS]
 function display($var,$output="") {
  global $Echo,$Verb;
  $rc=$this->tell("display ".$var,$output);
  if($output=="array") { return $this->arrange($rc); }
  return $rc;
 }

//  draw function
 function draw($type,$content) {
  global $Echo,$Verb;
  $this->tell("draw ".$type." ".$content);
 }

//  query function
 function query($opt,$output="") {
  global $Echo,$Verb;
  $rc=$this->tell("query ".$opt,$output);
  if($output=="array") { return $this->arrange($rc); }
  return $rc;
 }

//  gxyat function
 function gxyat($name) {
  global $Echo,$Verb;
  $this->tell("gxyat ".$name);
 }

//  font function
/// configures some font attributes in just one function
 function font($Args="") {
  $Font="0";
  $Size=".35";
  $Color="1";
  if($Args) {
   if(array_key_exists('Font', $Args))  {$Font=$Args["Font"];}
   if(array_key_exists('Size', $Args))  {$Size=$Args["Size"];}
   if(array_key_exists('Color', $Args)) {$Color=$Args["Color"];}
  }
  $this->tell("set font ".$Font);
  $this->tell("set string ".$Color." bc");
  $this->tell("set strsiz ".$Size);
 }

//  title function
/// draws the title, with the ability to move the title up or down
 function title($content,$Args="") {
  global $Echo,$Verb;
  $YOffset="0.2";
  if($Args) {
   if(array_key_exists('Font', $Args))    {$Font=$Args["Font"];}
  }
  $rc=$this->tell("query gxinfo","output");
  $rc0=explode("X Limits = ",$rc); $rc1=explode(" ",$rc0[1]);
  $ytop=explode("\n",$rc1[7]);$ytop=$ytop[0]+$YOffset; $ybottom=$rc1[5];
  $xleft=$rc1[0]; $xright=explode("\n",$rc1[2]);$xright=$xright[0];
  $xmid=$xleft+($xright-$xleft)/2;
  $this->tell("draw string ".$xmid." ".$ytop." ".$content);
 }
}
//// MAIN CLASS ////

?>

Ora facendo partire lo script, che ha il seguente codice:

PHP:
<?php
include ("grads.php");
$ga = Grads::start($opt=array("Verb"=>"0","Echo"=>"0","Window"=>"0"));


$ga->open("C:\Documents and Settings\Administrator\Desktop\GrADS19\win32\data\model.nc","sdf");

$ga->set("lat","16 16");
$ga->set("lon","41 41");


$pressure = $ga->display("ps");

$testo = fopen("prova2.txt","w");
fwrite($testo,$pressure);
fclose($testo);


$ga->quit();
?>

su un server in locale Apache, mi arriva un errore:

"Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Group\Apache\htdocs\grads.php on line 78"

La line che da l'errore è questa:
PHP:
"if(strpos($r, "</RC>")!==FALSE){ $rcok=explode(" ",$r); }"

Non so proprio come fare....
C'è qualcuno che usa Grads e che mi potrebbe dare un consiglio?

Grazie mille!:)
 
non è un problema di Grads, il fatto è che l'esecuzione dello script richiede tempi di escuzione più lunghi di quelli previsti per uno script dal tuo PHP.ini.