File Coverage

blib/lib/Weather/NOAA/GFS.pm
Criterion Covered Total %
statement 30 848 3.5
branch 0 276 0.0
condition 0 51 0.0
subroutine 10 35 28.5
pod 0 21 0.0
total 40 1231 3.2


line stmt bran cond sub pod time code
1             package Weather::NOAA::GFS;
2              
3              
4 1     1   31760 use strict;
  1         3  
  1         270  
5 1     1   7 use warnings;
  1         1  
  1         40  
6              
7 1     1   2120 use LWP::UserAgent;
  1         106360  
  1         30  
8 1     1   1027 use Net::FTP;
  1         49186  
  1         73  
9 1     1   1094 use HTML::LinkExtractor;
  1         16442  
  1         34  
10 1     1   1113 use Data::Dumper;
  1         8477  
  1         86  
11 1     1   11 use Time::Local;
  1         2  
  1         59  
12 1     1   7 use Cwd;
  1         1  
  1         1702  
13              
14             require Exporter;
15              
16             our @ISA = qw(Exporter);
17             our @EXPORT_OK = qw ( idrisi2png ascii2idrisi downloadGribFiles grib2ascii);
18             our $VERSION = "0.10";
19              
20             ## VERSIONS INFOS
21             #0.10 Octuber 26 2005
22             # - documentation corrections
23             #
24             #0.09 Octuber 26 2005
25             # - added server array check to find the active one
26             #
27             #0.08 Octuber 25 2005
28             # - documentation corrections
29             #
30             #0.07 October 25 2005
31             # - added timeout control to prevent server overload and never ending scripts.
32             #
33             # 0.06 May 11 2005
34             # - correction on download string to adapt to nomad's page name change
35             # - correction on 'glab.t*z.pgrbf*' to 'gfs.t*z.pgrb*'
36             #
37             # 0.05 Jan 04 2005
38             # - added downscale function (idrisiDownscale)
39             # - vector wind grafic output
40             # - corrections on rain png infos
41             # - corrections on rain dayly rains calculation bug
42             # - cleanUp function added
43             # 0.04 Dec 14 2004
44             # - added gradsc_path parameter
45             # - added wgrib_path parameter
46             # - documentation corrections
47             #
48              
49              
50             my $LOGFILE = "forecast.log";
51              
52             #
53             # OLD SERVER VARIABLES - to be deleted
54             #
55             #my $SERVER_1 = 'nomad5.ncep.noaa.gov';
56             #my $SERVER_2 = 'nomad3.ncep.noaa.gov';
57             #my $URL_NOMAD_1_SH = "http://nomad5.ncep.noaa.gov/cgi-bin/ftp2u_gfs.sh";
58             #my $CERCO_FTP = 'ftp://nomad5.ncep.noaa.gov/pub/NOMAD_1hr/';
59              
60             #------------------------------------------------------------------------
61             # Constructor
62             #------------------------------------------------------------------------
63             sub new {
64 0     0 0   my $proto = shift;
65 0   0       my $class = ref($proto) || $proto;
66 0           my $self = {};
67              
68             # some general attributes
69 0           $self->{PROXY} = "none";
70 0           $self->{TIMEOUT} = 180;
71 0           $self->{DEBUG} = 0;
72 0           $self->{LOGFILE} = undef;
73 0           $self->{TEMP_DIR} = "./";#working dir
74 0           $self->{DEST_DIR} = "./";#png images destination dir
75 0           $self->{MAIL_ANONYMOUS} = undef;#obbligatorio
76 0           $self->{SERVER_LIST} = "nomad1.ncep.noaa.gov,nomad5.ncep.noaa.gov,nomad3.ncep.noaa.gov,nomad2.ncep.noaa.gov";#obbligatorio
77              
78             # quadro
79 0           $self->{MINLON} = undef;#obbligatorio
80 0           $self->{MAXLON} = undef;#obbligatorio
81 0           $self->{MINLAT} = undef;#obbligatorio
82 0           $self->{MAXLAT} = undef;#obbligatorio
83 0           $self->{D_LAT} = undef;#Delta Lat
84 0           $self->{D_LON} = undef;#Delta Lon
85            
86 0           $self->{RESOLUTION} = 1;
87 0           $self->{GRIB_FILES} = {};
88 0           $self->{START_TIME} = time;# serve per cronometrare il tempo del processo
89 0           $self->{SETUP} = 0; # definisce se l'istanza è andata a buon fine e a superato i check
90            
91             # parameters provided by new method
92 0           my %parameters = ();
93 0 0         if ( ref( $_[0] ) eq "HASH" ) {
94 0           %parameters = %{ $_[0] };
  0            
95             } else {
96 0           %parameters = @_;
97             }
98              
99            
100             # set attributes as in %parameters
101 0 0         $self->{PROXY} = $parameters{proxy} if ( $parameters{proxy} );
102 0 0         $self->{TIMEOUT} = $parameters{timeout} if ( $parameters{timeout} );
103 0 0         $self->{DEBUG} = $parameters{debug} if ( $parameters{debug} );
104 0 0         $self->{MINLON} = $parameters{minlon} if ( $parameters{minlon} );
105 0 0         $self->{MAXLON} = $parameters{maxlon} if ( $parameters{maxlon} );
106 0 0         $self->{MINLAT} = $parameters{minlat} if ( $parameters{minlat} );
107 0 0         $self->{MAXLAT} = $parameters{maxlat} if ( $parameters{maxlat} );
108 0 0         $self->{LOGFILE} = $parameters{logfile} if ( $parameters{logfile} );
109 0 0         $self->{TEMP_DIR} = $parameters{temp_dir} if ( $parameters{temp_dir} );
110 0 0         $self->{DEST_DIR} = $parameters{dest_dir} if ( $parameters{dest_dir} );
111 0 0         $self->{MAIL_ANONYMOUS} = $parameters{mail_anonymous} if ( $parameters{mail_anonymous} );
112 0 0         $self->{CBARN_PATH} = $parameters{cbarn_path} if ( $parameters{cbarn_path} );
113 0 0         $self->{R_PATH} = $parameters{r_path} if ( $parameters{r_path} );
114 0 0         $self->{GRADSC_PATH} = $parameters{gradsc_path} if ( $parameters{gradsc_path} );
115 0 0         $self->{WGRIB_PATH} = $parameters{wgrib_path} if ( $parameters{wgrib_path} );
116 0 0         $self->{SERVER_LIST} = $parameters{server_list} if ( $parameters{server_list} );
117              
118              
119              
120              
121 0           bless( $self, $class );
122 0 0         if($self->{MAIL_ANONYMOUS}){
123 0           $self->_debug( "mail Ok!");
124             } else {
125 0           $self->_debug( "'mail_anonymous' is a mandatory parameter!");
126             exit
127 0           }
128 0 0         if($self->{GRADSC_PATH}){
129 0           $self->_debug( "gradsc_path Ok!");
130             } else {
131 0           $self->_debug( "'gradsc_path' is a mandatory parameter!");
132             exit
133 0           }
134 0 0         if($self->{WGRIB_PATH}){
135 0           $self->_debug( "wgrib_path Ok!");
136             } else {
137 0           $self->_debug( "'wgrib_path' is a mandatory parameter!");
138             exit
139 0           }
140            
141 0 0         if($self->_check_area_size()) {
142 0           $self->_debug("area check Ok!");
143 0           $self->{SETUP} = 1;
144             # if($self->check_string_on_url("mages","http://www.google.com")){
145             # $self->_debug( "string checked!");
146             # } else {
147             # $self->_debug( "string check FAILED!");
148             # }
149              
150             #inizio procedura di scarico
151             # if($self->_grib_download()){
152             # $self->_debug( "download succeded!");
153             # #go on
154             # } else {
155             # $self->_debug( "download FAILED!");
156             # }
157            
158             #procedura grib2r
159             # $self->_grib2r();
160            
161             } else {
162 0           $self->_debug( "area check FAILED!");
163             exit
164 0           }
165            
166 0           $self->_debug( Dumper($self) );
167              
168 0           return $self;
169             }
170              
171             #------------------------------------------------------------------------
172             # other internals
173             #------------------------------------------------------------------------
174             sub _debug {
175 0     0     my $self = shift;
176 0           my $notice = shift;
177 0           my $now = $self->data_formattata_forecast(time);
178 0 0         if ( $self->{LOGFILE} ) {
179 0           my $filename = $self->{LOGFILE};
180 0           open(LOGFILE, ">>$filename");
181 0           print LOGFILE "$now - $notice\n";
182             }
183 0 0         if ( $self->{DEBUG} ) {
184             #warn ref($self) . " - $now - DEBUG NOTE: $notice\n";
185 0           warn "$now - $notice\n";
186 0           return 1;
187             }
188 0           return 0;
189             }
190              
191             sub _check_timeout {
192 0     0     my $self = shift;
193 0           my $start = $self->{START_TIME};
194 0           my $timeout = $self->{TIMEOUT} * 60;
195 0           my $now = time;
196 0 0         if(($now-$start)<=$timeout){
197             return
198 0           } else {
199 0           $self->_debug("Timeout!");
200             exit
201 0           }
202             }
203              
204             sub _check_area_size {
205 0     0     my $self = shift;
206 0           my $error = 0;
207             #estraggo i valori assoluti delle coordinate
208 0           my $a_minlat = $self->absolute_integer_value($self->{MINLAT});
209 0           my $a_minlon = $self->absolute_integer_value($self->{MINLON});
210 0           my $a_maxlat = $self->absolute_integer_value($self->{MAXLAT});
211 0           my $a_maxlon = $self->absolute_integer_value($self->{MAXLON});
212 0           my $d_lat = $self->absolute_integer_value($self->{MAXLAT} - $self->{MINLAT}) + 1;
213 0           my $d_lon = $self->absolute_integer_value($self->{MAXLON} - $self->{MINLON}) +1;
214            
215             #$self->_debug("Vars:".$self->{MINLAT}."-".$self->{MINLON}."-".$self->{MAXLAT}."-".$self->{MAXLON}."-".$d_lat."-".$d_lon);
216             #$self->_debug("Vars:".$a_minlat."-".$a_minlon."-".$a_maxlat."-".$a_maxlon."-".$d_lat."-".$d_lon);
217            
218             #controllo che minimi e massimi siano rispettati;
219 0 0         if($self->{MINLAT}>=$self->{MAXLAT}) {
220 0           $self->_debug("Minlat non puo' essere maggiore di Maxlat");
221 0           $error = 1;
222             }
223 0 0         if($self->{MINLON}>=$self->{MAXLON}) {
224 0           $self->_debug("Minlon non puo' essere maggiore di Maxlon");
225 0           $error = 1;
226             }
227            
228            
229             #controllo che le coordinate cadano nel range delle coordinate sferiche
230 0 0         if($a_minlat>90) {
231 0           $self->_debug("Minlat non puo' avere un valore assoluto superiore a 90");
232 0           $error = 1;
233             }
234 0 0         if($a_maxlat>90) {
235 0           $self->_debug("Maxlat non puo' avere un valore assoluto superiore a 90");
236 0           $error = 1;
237             }
238 0 0         if($a_minlon>180) {
239 0           $self->_debug("Minlon non puo' avere un valore assoluto superiore a 180");
240 0           $error = 1;
241             }
242 0 0         if($a_maxlon>180) {
243 0           $self->_debug("Maxlon non puo' avere un valore assoluto superiore a 180");
244 0           $error = 1;
245             }
246             #controllo che il valore assoluto fra massimi e minimi sia superiore a ...
247             ## NOTA -> Controllare
248 0 0         if($d_lat<10) {
249 0           $self->_debug("Il valore assoluto della differenza fra Maxlat e Minlat deve essere superiore a 10");
250 0           $error = 1;
251             }
252 0 0         if($d_lon<10) {
253 0           $self->_debug("Il valore assoluto della differenza fra Maxlon e Minlon deve essere superiore a 10");
254 0           $error = 1;
255             }
256            
257             #controllo che l'area richiesta abbiamo un'estensione minima superiore a 100 pixel
258 0 0         if($d_lat*$d_lon<200) {
259 0           $self->_debug("l'area richiesta deve essere superiore a 200 pixel");
260 0           $error = 1;
261             } else {
262 0           $self->{D_LAT} = $d_lat;
263 0           $self->{D_LON} = $d_lon;
264             }
265            
266            
267            
268 0 0         if($error==1){
269 0           return 0;
270             } else {
271             #$self->_debug("Area size is OK");
272 0           return 1;
273             }
274             }
275              
276             sub checkSetup {
277 0     0 0   my $self = shift;
278            
279 0 0         if(!$self->{SETUP}){
280 0           return 0;
281             }
282            
283 0           return 1;
284             }
285              
286             #net stuff
287              
288             sub get_server {
289 0     0 0   my $self = shift;
290 0           my @servers = split(/,/,$self->{SERVER_LIST});
291              
292 0           $self->_check_timeout();
293 0           foreach my $server (@servers) {
294 0           $self->_debug("Checking: ".$server);
295 0 0         if($self->check_string_on_url('FTP2U','http://'.$server.'/cgi-bin/ftp2u_gfs.sh')){
296 0           return $server;
297             }
298             }
299 0           $self->_debug("No server available!");
300             exit
301             # -> could be nice to make it recursive, like this it doesn't work.
302             #get_server($self);
303 0           }
304              
305             sub check_string_on_url {
306 0     0 0   my $self = shift;
307 0           my $string = shift;#arg0
308 0           my $url = shift;#arg1
309            
310 1     1   9 use LWP;
  1         3  
  1         446  
311 0           my $useragent = LWP::UserAgent->new;
312 0           my $request = new HTTP::Request('GET',$url);
313 0           my $response = $useragent->request($request);
314 0           my $stringa_html = $response->as_string();
315             #if ( $self->{DEBUG} ) {$self->_debug($stringa_html);}
316 0 0         if(index($stringa_html,$string) > 0){
317 0           return 1
318             } else {
319             return
320 0           }
321             }
322              
323              
324             sub get_ftp_dir {
325 0     0 0   my $self = shift;
326 0           my $ftp_da_cercare = shift;
327 0           my $url = shift;
328            
329 0           my $ftp_founded = undef;
330 0           my $useragent = LWP::UserAgent->new;
331 0           my $request = new HTTP::Request('GET',$url);
332 0           my $response = $useragent->request($request);
333 0           my $html = $response->as_string();
334            
335 0           my $LX = new HTML::LinkExtractor();
336 0           $LX->parse(\$html);
337              
338 0           foreach my $Link (@{$LX->links} ){
  0            
339             ## becco solo l'ftp che presenta la sola directory (ovvero non contiene il file "gfs*")
340 0 0 0       if( ($$Link{href}=~ /^ftp:\/\//) && ($$Link{href}!~ /gfs/) ) {
341 0           $ftp_founded = $$Link{_TEXT};
342 0           $ftp_founded =~ s/<([a-z][a-z0-9]*)[^>]*>(.*?)<\/a>/$2/;
343             }
344             }
345            
346 0           undef $LX;
347             #
348             ##RITORNA OUTPUT FUNCTION
349 0 0         if ($ftp_founded) {
350 0           return $ftp_founded;
351             } else {
352 0           return;
353             }
354             }
355              
356              
357             sub _ftpDownload {
358             #
359             ##ARGV
360 0     0     my $self = shift;
361 0           my $ftp_site = shift;
362             #
363            
364             ##variables
365 0           my @lista_grib = undef;
366 0           my $ftp = undef;
367            
368             ##Module
369 1     1   7 use Net::FTP;
  1         3  
  1         9568  
370 0           $self->_debug("_ftpDownload- ftpsite: ".$ftp_site);
371             ###############################################################
372 0           my $ftp_senza_ftp = $ftp_site;
373 0           my $prefisso_ftp = 'ftp://';
374 0           $ftp_senza_ftp =~ s/$prefisso_ftp//g;
375 0           my @lista_dir = split(/\//,$ftp_senza_ftp);
376             ###############################################################
377              
378             #
379             ##ISTANZIA OGGETTO FTP
380 0 0         if (!($ftp = Net::FTP->new($lista_dir[0], timeout=>3600))) {
381 0           $self->_debug("_ftpDownload: Problems connecting to ftp site: $lista_dir[0]");
382             }
383             ####### $ftp = Net::FTP->new("$lista_dir[0]", timeout=>3600) || $self->_debug("Non riesco a collegarmi con ftp $lista_dir[0]");
384             #
385             ##CONNECT & LOGIN
386 0 0         if (!($ftp->login('anonymous',$self->{MAIL_ANONYMOUS}))) {
387 0           $self->_debug("_ftpDownload: Error loggin: $lista_dir[0]");
388             } else {
389 0           $self->_debug("_ftpDownload: Connected and logged on $lista_dir[0]. downloading grib files...");
390             }
391             ####### $ftp->login('anonymous','pippo@topolino.org')|| $self->_debug("Non riesco login con ftp $lista_dir[0]");
392             ####### ###print STDOUT "\n***\tCOLLEGATO CON $lista_dir[0]\t***\n";
393             ####### $self->_debug("Collegato e loggato su ftp $lista_dir[0] per scaricare grib files");
394             #
395             ##CHANGE DIR
396 0           my $new_dir='/'."$lista_dir[1]".'/'."$lista_dir[2]".'/'."$lista_dir[3]".'/';
397 0 0         if (!($ftp->cwd("$new_dir"))) {
398 0           $self->_debug("_ftpDownload: Can't change dir in $lista_dir[0]");
399             } else {
400 0           $self->_debug("_ftpDownload: Dir changed in $new_dir");
401             }
402             ####### $ftp->cwd("$new_dir") || $self->_debug("Non riesco a cambiare dir in $lista_dir[0]");
403             ####### $self->_debug("Cambiata directory in $new_dir");
404            
405             ##GET FILES
406 0 0         if (!($ftp->binary)) {
407 0           $self->_debug("_ftpDownload: Can't change in binary mode");
408             } else {
409 0           $self->_debug("_ftpDownload: Switch to binary mode");
410             }
411 0 0         if (!( @lista_grib= $ftp->ls("gfs*pgrbf*"))) {
412 0           $self->_debug("_ftpDownload: Can't retrieve grib files array");
413             } else {
414 0           $self->_debug("_ftpDownload: Retrieve grib files array");
415             }
416             ####### $ftp->binary;
417             ####### @lista_grib=$ftp->ls("gfs*pgrbf*");
418 0           my $tot_gfiles= $#lista_grib+1;
419 0           my $prog=0;
420 0           foreach my $gfile (@lista_grib) {
421             #prova cambio directory
422 0           while (!($ftp->get("$gfile","$self->{TEMP_DIR}$gfile"))) {
423 0 0         if (!($ftp = Net::FTP->new("$lista_dir[0]", timeout=>3600))) {
424 0           $self->_debug("_ftpDownload: Can't connect to ftp $lista_dir[0]");
425 0           return;
426             }
427 0 0         if (!( $ftp->login('anonymous',$self->{MAIL_ANONYMOUS}))) {
428 0           $self->_debug("_ftpDownload: Can't login ftp $lista_dir[0]");
429 0           return;
430             } else {
431 0           $self->_debug("_ftpDownload: Connected and logged on ftp $lista_dir[0]. Downloading grib files...");
432             }
433 0 0         if (!($ftp->cwd("$new_dir"))) {
434 0           $self->_debug("_ftpDownload: Can't change dir in $lista_dir[0]");
435 0           return;
436             } else {
437 0           $self->_debug("_ftpDownload: Dir changed in $new_dir");
438             }
439 0 0         if (!($ftp->binary)) {
440 0           $self->_debug("_ftpDownload: Can't change to binary mode");
441 0           return;
442             } else {
443 0           $self->_debug("_ftpDownload: Switch to binary mode");
444             }
445 0 0         if (!($ftp->get("$gfile","$self->{TEMP_DIR}$gfile"))) {
446 0           $self->_debug("_ftpDownload: Can't download grib file $gfile");
447 0           return;
448             }
449             ####### $ftp = Net::FTP->new("$lista_dir[0]", timeout=>3600) || $self->_debug("Non riesco a collegarmi con ftp $lista_dir[0]");
450             ####### $ftp->login('anonymous','pippo@topolino.org')|| $self->_debug("Non riesco login con ftp $lista_dir[0]");
451             ####### $ftp->cwd("$new_dir") || $self->_debug("Non riesco a cambiare dir in $lista_dir[0]");
452             ####### $ftp->binary;
453             ####### $ftp->get("$gfile");
454             }
455 0           $self->_debug("_ftpDownload: $gfile downloaded");
456 0           my $rimanenti = $#lista_grib-$prog;
457             ###print STDOUT "***\tRimangono da scaricare $rimanenti files\t***\n\n";
458 0           $prog++;
459             }
460             #
461             ##QUIT
462 0           $ftp->quit;
463              
464             }
465              
466              
467             sub downloadGribFiles {
468 0     0 0   my $self = shift;
469            
470 0 0         if($self->{SETUP}!=1){
471 0           $self->_debug( "downloadGribFiles: Setup is not proper. Control input data and try again.");
472 0           return 0;
473             }
474 0           my @gribs = glob 'gfs.t*z.pgrbf*'; #elenca tutti i grib files presenti nella cartella corrente
475              
476             ## VARS
477 0           my $ftp_trovato = undef;
478            
479 0           my $server = $self->get_server();
480 0           my $server_string = 'http://'.$server.'/cgi-bin/ftp2u_gfs.sh';
481 0           my $STRINGA_URL = "$server_string?file=gfs\.t00z\.pgrbf03&file=gfs\.t00z\.pgrbf06&file=gfs\.t00z\.pgrbf09&file=gfs\.t00z\.pgrbf12&file=gfs\.t00z\.pgrbf15&file=gfs\.t00z\.pgrbf18&file=gfs\.t00z\.pgrbf21&file=gfs\.t00z\.pgrbf24&file=gfs\.t00z\.pgrbf27&file=gfs\.t00z\.pgrbf30&file=gfs\.t00z\.pgrbf33&file=gfs\.t00z\.pgrbf36&file=gfs\.t00z\.pgrbf39&file=gfs\.t00z\.pgrbf42&file=gfs\.t00z\.pgrbf45&file=gfs\.t00z\.pgrbf48&file=gfs\.t00z\.pgrbf51&file=gfs\.t00z\.pgrbf54&file=gfs\.t00z\.pgrbf57&file=gfs\.t00z\.pgrbf60&file=gfs\.t00z\.pgrbf63&file=gfs\.t00z\.pgrbf66&file=gfs\.t00z\.pgrbf69&file=gfs\.t00z\.pgrbf72&file=gfs\.t00z\.pgrbf75&file=gfs\.t00z\.pgrbf78&file=gfs\.t00z\.pgrbf81&file=gfs\.t00z\.pgrbf84&file=gfs\.t00z\.pgrbf87&file=gfs\.t00z\.pgrbf90&file=gfs\.t00z\.pgrbf93&file=gfs\.t00z\.pgrbf96&file=gfs\.t00z\.pgrbf99&file=gfs\.t00z\.pgrbf102&file=gfs\.t00z\.pgrbf105&file=gfs\.t00z\.pgrbf108&file=gfs\.t00z\.pgrbf111&file=gfs\.t00z\.pgrbf114&file=gfs\.t00z\.pgrbf117&file=gfs\.t00z\.pgrbf120&file=gfs\.t00z\.pgrbf123&file=gfs\.t00z\.pgrbf126&file=gfs\.t00z\.pgrbf129&file=gfs\.t00z\.pgrbf132&file=gfs\.t00z\.pgrbf135&file=gfs\.t00z\.pgrbf138&file=gfs\.t00z\.pgrbf141&file=gfs\.t00z\.pgrbf144&file=gfs\.t00z\.pgrbf147&file=gfs\.t00z\.pgrbf150&file=gfs\.t00z\.pgrbf153&file=gfs\.t00z\.pgrbf156&file=gfs\.t00z\.pgrbf159&file=gfs\.t00z\.pgrbf162&file=gfs\.t00z\.pgrbf165&file=gfs\.t00z\.pgrbf168&file=gfs\.t00z\.pgrbf171&file=gfs\.t00z\.pgrbf174&file=gfs\.t00z\.pgrbf177&file=gfs\.t00z\.pgrbf180&wildcard=&lev_sfc=on&lev_1000_mb=on&lev_925_mb=on&lev_850_mb=on&var_APCP=on&var_PRES=on&var_RH=on&var_UGRD=on&var_VGRD=on&var_TMP=on&subregion=on&leftlon=$self->{MINLON}&rightlon=$self->{MAXLON}&toplat=$self->{MAXLAT}&bottomlat=$self->{MINLAT}&results=SAVE&rtime=3hr&machine=149.139.16.204&user=anonymous&passwd=&ftpdir=%2Fincoming_1hr&prefix=&dir=";
482 0           my $ftp_server = 'ftp://'.$server.'/pub/NOMAD_1hr/';
483 0           $self->_debug("Stringa Url: ".$STRINGA_URL);
484            
485 0           while ($#gribs<59) {
486 0           $self->_check_timeout();
487 0           my $tot_gribs=$#gribs+1;
488 0           $self->_debug( "GRIB files in dir: $tot_gribs:60");
489            
490 0 0         if($self->check_string_on_url("transferred 60 out of 60 files",$STRINGA_URL)){
491 0           $ftp_trovato = $self->get_ftp_dir($ftp_server,$STRINGA_URL);
492 0           $self->_debug("Ftp url from get_ftp_url: ".$ftp_trovato);
493 0 0         if (length($ftp_trovato) > 0 ) {
494 0           $self->_ftpDownload($ftp_trovato);
495             } else {
496 0           $self->_debug("Ftp url from get_ftp_url is not an url.");
497             }
498             } else {
499 0 0         if ($self->check_string_on_url("Sorry, machine is overloaded",$STRINGA_URL)) {
    0          
    0          
500 0           $self->_debug("Server $server_string overloaded");
501             } elsif ($self->check_string_on_url("out of disk space",$STRINGA_URL)) {
502 0           $self->_debug("Server $server_string ran out of disk space");
503             } elsif ($self->check_string_on_url("too many ftp2u jobs now",$STRINGA_URL)) {
504 0           $self->_debug("Server $server_string too many ftp2u jobs now");
505             } else {
506 0           $self->_debug("Unknown error in download procedure.");
507             }
508            
509             }
510            
511 0           @gribs = glob 'gfs.t*z.pgrbf*';
512 0           $tot_gribs=$#gribs+1;
513 0           $self->_debug( "GRIB files in dir: $tot_gribs:60");
514             ## LORE -> note -> Ci vuole un delay parametrizzato per non stressare il server
515            
516             }
517            
518             ## LORE -> note ->Ci vuole un temporizzatore che capisca quando il server non ne vuole sapere di darci i file. DOpo qualche ora
519             # dobbiamo abbozzarla di tentare lo scarico.
520            
521 0 0         if($#gribs==59){
522 0           $self->{GRIB_FILES} = 'gfs.t*z.pgrbf*';
523 0           return 1;
524             } else {
525 0           return 0;
526             }
527              
528             }
529              
530             sub ascii2idrisi {
531              
532 0     0 0   my $self = shift;
533            
534 0 0         if(!$self->checkSetup()){
535 0           $self->_debug( "ascii2idrisi: Setup is not proper. Control input data and try again.");
536 0           return 0;
537             }
538            
539 0           my %chiaveValore= ();
540             #$self->{GRIB_FILES} = 'gfs.t*z.pgrbf*';
541 0           my @grib_files = glob 'gfs.t*z.pgrbf*';
542             #estraggo lo header del grib_file riga per riga
543 0           my $wgrib_path = $self->{WGRIB_PATH};
544 0           my @grib_vars = `$wgrib_path -v $grib_files[0]`;
545            
546 0           foreach my $line (@grib_vars) {
547 0 0         if($#grib_vars==0) {
548 0           next; #la prima riga deve essere saltata ("OUTPUT WGRIB -V")
549             }
550 0           my @elementi = split /:/,$line;
551 0           my $i = undef;
552 0           my $key = undef;
553 0           my $value = undef;
554 0           for($i=0;$i<=$#elementi;$i++){
555             ## NOTA -> LORE -> attento al valore "sfc" (ma forse non è un problema)
556 0 0         if($i==3){
557             #CHIAVE
558 0           $key = $elementi[$i];
559             }
560 0 0         if($i==4){
561             #VALORE
562 0           my @valori = split / /,$elementi[$i];
563 0           $value = $valori[0];# becco solo il primo valore (es: "850 mb" -> 850; "sfc" -> sfc )
564             }
565             }
566            
567 0           $self->_debug( " ascii2idrisi -chiave: $key, value: $value\n");
568 0 0         if($key=~/APCP/){
569 0           $self->ascii2idrisi_avarage($key,$value);
570 0           for(my $a=1;$a<=7;$a++){
571 0           my $key2 = $key.$a;
572 0           $self->_debug( "ascii2idrisi - chiave: $key2, value: $value\n");
573 0           $self->ascii2idrisi_avarage($key2,$value);
574             }
575             } else {
576 0           $self->ascii2idrisi_avarage($key,$value);
577             }
578             #$chiaveValore{$key}=$value;
579             }
580            
581             #print "totale: ".@sgribbed_files."\n\n";
582 0           return 1;
583             }
584              
585             sub idrisi2png {
586              
587 0     0 0   my $self = shift;
588            
589 0 0         if(!$self->checkSetup()){
590 0           return 0;
591             }
592            
593 0           my @idrisi_files = glob 'media_*.rdc';
594             #$self->_debug( "idrisi2png");
595 0           foreach my $idrisi_file (@idrisi_files) {
596             #$self->_debug( "$idrisi_file");
597 0           my @elementi = split /_/,$idrisi_file;
598 0           my $key = undef;
599 0           my $value = undef;
600 0           for(my $i=0;$i<=$#elementi;$i++){
601            
602 0 0         if($i==1){
603 0           $key = $elementi[$i];
604             }
605            
606 0 0         if($i==2){
607             #my @elementi2 = split /./,$idrisi_file;
608 0           $value = $elementi[$i];
609 0           $value =~ s/[\.\,][a-z]+//;
610             }
611            
612             }
613 0           $self->_debug( "idrisi2png - key:$key - value:$value");
614 0 0         if($key=~m/GRD/){
615             #faccio il match di solo una delle variabili vento per non duplicare l'ouput
616 0 0         if($key=~m/VGRD/){$self->idrisi_grd2png_exe($key,$value);}
  0            
617             } else {
618 0           $self->idrisi2png_exe($key,$value);
619             }
620            
621             }
622 0           return 1;
623             }
624              
625              
626              
627             sub idrisiDownscale {
628              
629 0     0 0   my $self = shift;
630            
631 0 0         if(!$self->checkSetup()){
632 0           return 0;
633             }
634            
635 0           my @idrisi_files = glob 'media_*.rst';
636             #$self->_debug( "idrisi2png");
637 0           foreach my $idrisi_file (@idrisi_files) {
638             #$self->_debug( "$idrisi_file");
639 0           my @elementi = split /_/,$idrisi_file;
640 0           my $key = undef;
641 0           my $value = undef;
642 0           for(my $i=0;$i<=$#elementi;$i++){
643            
644 0 0         if($i==1){
645 0           $key = $elementi[$i];
646             }
647            
648 0 0         if($i==2){
649             #my @elementi2 = split /./,$idrisi_file;
650 0           $value = $elementi[$i];
651 0           $value =~ s/[\.\,][a-z]+//;
652             }
653            
654             }
655 0           $self->_debug( "idrisiDownscale - key:$key - value:$value");
656 0 0         if($key=~m/GRD/){
657             #non serve fare il daownscale del vento
658            
659             } else {
660 0           $self->idrisiDownscale_exe($key,$value);
661             }
662            
663             }
664 0           return 1;
665             }
666              
667              
668             sub grib2ascii {
669              
670 0     0 0   my $self = shift;
671            
672 0 0         if(!$self->checkSetup()){
673 0           return 0;
674             }
675            
676             #$self->{GRIB_FILES} = 'gfs.t*z.pgrbf*';
677 0           my @grib_files = glob 'gfs.t*z.pgrbf*';
678             #estraggo lo header del grib_file riga per riga
679 0           my $wgrib_path = $self->{WGRIB_PATH};
680 0           my @grib_vars = `$wgrib_path -v $grib_files[0]`;
681             #my @grib_vars = `wgrib -v $grib_files[0]`;
682              
683             #VARS
684 0           my @text_files;
685             # OUTPUT WGRIB -V
686             # 1:0:D=2004111700:TMP:1000 mb:kpds=11,100,1000:3hr fcst:"Temp. [K]
687             # 2:1852:D=2004111700:TMP:925 mb:kpds=11,100,925:3hr fcst:"Temp. [K]
688             # 3:3704:D=2004111700:TMP:850 mb:kpds=11,100,850:3hr fcst:"Temp. [K]
689             # 4:5556:D=2004111700:RH:1000 mb:kpds=52,100,1000:3hr fcst:"Relative humidity [%]
690             # 5:7186:D=2004111700:RH:925 mb:kpds=52,100,925:3hr fcst:"Relative humidity [%]
691             # 6:8816:D=2004111700:RH:850 mb:kpds=52,100,850:3hr fcst:"Relative humidity [%]
692             # 7:10446:D=2004111700:UGRD:1000 mb:kpds=33,100,1000:3hr fcst:"u wind [m/s]
693             # 8:12298:D=2004111700:UGRD:925 mb:kpds=33,100,925:3hr fcst:"u wind [m/s]
694             # 9:14150:D=2004111700:UGRD:850 mb:kpds=33,100,850:3hr fcst:"u wind [m/s]
695             # 10:16002:D=2004111700:VGRD:1000 mb:kpds=34,100,1000:3hr fcst:"v wind [m/s]
696             # 11:17854:D=2004111700:VGRD:925 mb:kpds=34,100,925:3hr fcst:"v wind [m/s]
697             # 12:19926:D=2004111700:VGRD:850 mb:kpds=34,100,850:3hr fcst:"v wind [m/s]
698             # 13:21778:D=2004111700:PRES:sfc:kpds=1,1,0:3hr fcst:"Pressure [Pa]
699             # 14:25176:D=2004111700:TMP:sfc:kpds=11,1,0:3hr fcst:"Temp. [K]
700             # 15:27248:D=2004111700:APCP:sfc:kpds=61,1,0:0-3hr acc:"Total precipitation [kg/m^2]
701            
702 0           my $index = 0;
703 0           foreach my $grib_file (@grib_files) {
704 0           foreach my $line (@grib_vars) {
705             #$self->_debug($line);
706 0 0         if($#grib_vars==0) {
707 0           next; #la prima riga deve essere saltata ("OUTPUT WGRIB -V")
708             }
709 0           my @elementi = split /:/,$line;
710 0           my $i = undef;
711 0           my $key = undef;
712 0           my $value = undef;
713 0           for($i=0;$i<=$#elementi;$i++){
714             ## NOTA -> LORE -> attento al valore "sfc" (ma forse non è un problema)
715 0 0         if($i==3){
716             #CHIAVE
717 0           $key = $elementi[$i];
718             }
719 0 0         if($i==4){
720             #VALORE
721 0           my @valori = split / /,$elementi[$i];
722 0           $value = $valori[0];# becco solo il primo valore (es: "850 mb" -> 850; "sfc" -> sfc )
723             }
724             }
725 0           $self->_debug("Grib2ascii: $key-> $value");
726             ## Creo i files temporanei
727 0           my $txt_file=$grib_file;
728 0           $txt_file =~ s/\./_/g;
729 0           $txt_file=$txt_file."_".$key."-".$value."\.txt";
730             #$self->_debug("nome file: ".$txt_file);
731 0           push(@text_files,$txt_file);
732             #$self->_debug("wgrib -s $grib_file | egrep \":$key:$value\" | wgrib -i -grib $grib_file -text -o $txt_file");
733 0           system($self->{WGRIB_PATH}." -s $grib_file | egrep \":$key:$value\" | ".$self->{WGRIB_PATH}." -i -grib $grib_file -text -o $txt_file");
734            
735             #all'ultimo giro creo i valori aggregati
736             # if($index==@friends){
737             # #$self->_agregated_values($key,$value);
738             # }
739              
740            
741             }
742 0           $index++;
743             }
744 0           return 1;
745             }
746              
747              
748              
749              
750             sub ascii2idrisi_avarage {
751              
752 0     0 0   my $self = shift;
753 0           my $key = shift;
754 0           my $value = shift;
755             # my $key = @_[0];
756             # my $value = @_[1];
757 0           my $real_key = undef;
758              
759            
760 0 0         if($key =~ /APCP/) {
761 0           $real_key = 'APCP';
762             } else {
763 0           $real_key = $key;
764             }
765 0           my $glob_match = 'gfs_t*z_pgrbf*_'.$real_key.'-'.$value.'.txt';
766             #print $glob_match."\n";
767 0           my @sgribbed_files = glob $glob_match;
768              
769            
770             # apro il file di output finale -> aggregazione dati
771 0           my $nome_file_out = "media_".$key."_".$value."\.rst";#binario
772 0           my $nome_file_rdc = "media_".$key."_".$value."\.rdc";#ascii infos (raster documentation file)
773            
774              
775 0           my $index = 0;
776 0           my $index2 = 0;
777 0           my @values;
778            
779             #Praparo l'array dei files->valori
780 0           foreach my $sgribbed_file (@sgribbed_files) {
781 0           open (FIN,"<$sgribbed_file");
782 0           $index2=0;
783            
784 0           while () {
785 0           $values[$index][$index2] = $_;
786 0           $index2++;
787             }
788 0           close(FIN);
789 0           $index++;
790             }
791            
792 0 0         open(FOUT,">$nome_file_out") || print "Non apre file out ($nome_file_out) \n";
793            
794 0           binmode(FOUT);
795            
796            
797             #variabili coordinate
798 0           my $lon_i = 0;
799 0           my $col = $self->{D_LON};
800 0           my $rig = $self->{D_LAT};
801              
802 0           my $minlon= $self->{MINLON};
803 0           my $maxlon= $self->{MAXLON};
804 0           my $minlat= $self->{MINLAT};
805 0           my $maxlat = $self->{MAXLAT};
806            
807 0           my $res = $self->{RESOLUTION};
808            
809 0           my $lon = $minlon;
810 0           my $lat = $maxlat;
811 0           my $min_value = 1000000;
812 0           my $max_value = -100000;
813              
814 0           my $test_i = 0;
815 0           for (my $i1=0;$i1<$index2;$i1++) {
816             ##NOTA -> LORE -> per output binary non mettere lo header
817 0 0         if($i1==0) {
818             #stampo lo header per R solo al primo ciclo dove ho un grib file
819             # my $header="x\ty\tvariab";
820             # print FOUT "$header\n";
821             # next;
822            
823             } else {
824 0           my $tot = 0;
825 0           my $i3 = 0;
826 0           my $tot_apcp1 = 0;
827 0           my $tot_apcp2 = 0;
828 0           my $tot_apcp3 = 0;
829 0           my $tot_apcp4 = 0;
830 0           my $tot_apcp5 = 0;
831 0           my $tot_apcp6 = 0;
832 0           my $tot_apcp7 = 0;
833            
834 0           for (my $i2=0;$i2<$index;$i2++) {
835 0           my $value_line = $values[$i2][$i1];
836             #$value=sprintf("%5.1f",$value);
837            
838 0           $tot = $tot + $value_line;
839 0 0 0       if($i2>=0 && $i2 <=7) {
840 0           $tot_apcp1 = $tot;
841             }
842 0 0 0       if($i2>=8 && $i2 <=15) {
843 0           $tot_apcp2 = $tot-$tot_apcp1;
844             }
845 0 0 0       if($i2>=16 && $i2 <=23) {
846 0           $tot_apcp3 = $tot-$tot_apcp2;
847             }
848 0 0 0       if($i2>=24 && $i2 <=31) {
849 0           $tot_apcp4 = $tot-$tot_apcp3;
850             }
851 0 0 0       if($i2>=32 && $i2 <=39) {
852 0           $tot_apcp5 = $tot-$tot_apcp4;
853             }
854 0 0 0       if($i2>=40 && $i2 <=47) {
855 0           $tot_apcp6 = $tot-$tot_apcp5;
856             }
857 0 0 0       if($i2>=48 && $i2 <=55) {
858 0           $tot_apcp7 = $tot-$tot_apcp6;
859             }
860 0           $i3++;
861             }
862             #print "key aggragated: $key";
863 0 0         if ($key eq 'APCP') {
864             #sommo tutto e non non divido
865 0           $tot = $tot;
866             # print $tot." ";
867             }
868 0 0         if ($key eq 'APCP1') {
869             #somma della pioggia del prima giorno
870 0           $tot = $tot_apcp1;
871             #print $tot." ";
872             }
873 0 0         if ($key eq 'APCP2') {
874             #sommo tutto e non non divido
875 0           $tot = $tot_apcp2;
876            
877             }
878 0 0         if ($key eq 'APCP3') {
879             #sommo tutto e non non divido
880 0           $tot = $tot_apcp3;
881            
882             }
883 0 0         if ($key eq 'APCP4') {
884             #sommo tutto e non non divido
885 0           $tot = $tot_apcp4;
886            
887             }
888 0 0         if ($key eq 'APCP5') {
889             #sommo tutto e non non divido
890 0           $tot = $tot_apcp5;
891            
892             }
893 0 0         if ($key eq 'APCP6') {
894             #sommo tutto e non non divido
895 0           $tot = $tot_apcp6;
896            
897             }
898 0 0         if ($key eq 'APCP7') {
899             #sommo tutto e non non divido
900 0           $tot = $tot_apcp7;
901            
902             }
903 0 0         if ($key eq 'PRES') {
904             #sommo tutto, fo la media e divido per 100 (hpascal)
905 0           $tot = $tot/$i3/100;
906             }
907 0 0         if ($key eq 'TMP') {
908             #sommo tutto, fo la media e sommo 273
909 0           $tot = $tot/$i3-273;
910             }
911 0 0 0       if ($key eq 'VGRD' || $key eq 'UGRD' || $key eq 'RH') {
      0        
912             #sommo tutto e la media
913 0           $tot = $tot/$i3;
914             }
915 0           $test_i++;
916             #print FOUT "$test_i\t$lon\t$lat\t$tot\n";
917 0           my $valbin = pack ('f',$tot);
918 0           print FOUT $valbin;
919            
920             #creo le coordinate punto punto
921             #
922 0 0 0       if ($lon==$maxlon && $index2>1) {
923 0           $lon = $minlon;
924 0           $lat = $lat-$res;
925             } else {
926             #print "lon1: $lon1\n";
927 0           $lon++;
928 0           $lon_i++;
929             }
930            
931             #Massimo e minimo
932             #print "$tot\n";
933 0 0         if ($min_value>$tot) {
934 0           $min_value=$tot;
935             }
936 0 0         if ($max_value<$tot) {
937 0           $max_value=$tot;
938             }
939            
940             #print "lon1: $lon_i \tlon: $lon \t lat: $lat\n";
941             }
942              
943              
944             }
945              
946 0           chomp($min_value);
947 0           chomp($max_value);
948              
949 0           $self->_debug( "min val ($min_value):: max val ($max_value)");
950             #print "test_i ($test_i):: index2 ($index2)\n";
951              
952 0           close(FOUT);#chiudo il file di aggregazione dati
953            
954            
955            
956             ##NOTA -> LORE -> per output binary
957 0           open(SCRIVI_RDC,">$nome_file_rdc");
958 0           print SCRIVI_RDC "file format : IDRISI Raster A.1\n";
959 0           print SCRIVI_RDC "file title : $nome_file_out\n";
960 0           print SCRIVI_RDC "data type : real\n";
961 0           print SCRIVI_RDC "file type : binary\n";
962 0           print SCRIVI_RDC "columns : $col\n";
963 0           print SCRIVI_RDC "rows : $rig\n";
964 0           print SCRIVI_RDC "ref. system : latlong\n";
965 0           print SCRIVI_RDC "ref. units : deg\n";
966 0           print SCRIVI_RDC "unit dist. : 1.0000000\n";
967 0           print SCRIVI_RDC "min. X : $minlon\n";
968             #$maxlon=($ncol*$res)+$minlon;
969 0           print SCRIVI_RDC "max. X : $maxlon\n";
970 0           print SCRIVI_RDC "min. Y : $minlat\n";
971             #$maxlat=($nrig*$res)+$minlat;
972 0           print SCRIVI_RDC "max. Y : $maxlat\n";
973 0           print SCRIVI_RDC "pos'n error : unknown\n";
974 0           print SCRIVI_RDC "resolution : $res\n";
975 0           print SCRIVI_RDC "min. value : $min_value\n";
976 0           print SCRIVI_RDC "max. value : $max_value\n";
977 0           print SCRIVI_RDC "display min : $min_value\n";
978 0           print SCRIVI_RDC "display max : $max_value\n";
979 0           print SCRIVI_RDC "value units : unknown\n";
980 0           print SCRIVI_RDC "value error : unknown\n";
981 0           print SCRIVI_RDC "flag value : none\n";
982 0           print SCRIVI_RDC "flag def'n : none\n";
983 0           print SCRIVI_RDC "legend cats : 0";
984            
985             #elimanates useless files
986             #system("rm temp.txt");
987            
988             #closes files
989 0           close(SCRIVI_RDC);
990              
991             }
992              
993              
994              
995             sub idrisiDownscale_exe {
996 0     0 0   my $self = shift;
997 0           my $key = shift;
998 0           my $value = shift;
999              
1000 0           my $nrig = $self->{D_LAT};
1001 0           my $ncol = $self->{D_LON};
1002            
1003            
1004 0           my $minlon= $self->{MINLON};
1005 0           my $maxlat = $self->{MAXLAT};
1006 0           my $minlat = $self->{MINLAT};
1007 0           my $maxlon = $self->{MAXLON};
1008 0           my $res = 1;
1009              
1010 0           my $fileout = $key."_".$value;
1011            
1012             #APRO il file IDRISI e lo formatto il ASCII come vuole R
1013             #-------------------------------------------------------------------------------
1014              
1015              
1016              
1017             #preso da grib2r.pl
1018             #-------------------------------------------------------------------------------
1019             #integrazione dello script GFS2R.pl; PREPARA IL FILE PER R
1020            
1021 0           my $file_in = "media_".$fileout."\.rst";
1022 0           my $nome_file_rdc = "media_".$fileout."\.rdc";
1023            
1024 0 0         open(IN,"<$file_in") or die "non apre $file_in";
1025 0           print "file in : ".$file_in."\n";
1026 0           binmode(IN);
1027 0           my $file_temp =$fileout."\_r.tmp";
1028 0 0         open (OUT,">$file_temp") or die "non apre $file_temp";
1029            
1030 0           my $header="x\ty\tvariab";
1031 0           print OUT "$header\n";
1032            
1033 0           my $val_lat=$maxlat;
1034 0           my $kx=1;
1035 0           my $leggi = undef;
1036 0           my $valore = undef;
1037 0           for(my $i=0;$i<$nrig;$i++) {
1038 0           my $val_lon = $minlon;
1039 0           for(my $j=0;$j<$ncol;$j++) {
1040             #my $leggi = ;
1041 0           read (IN,$valore,4);
1042 0           $leggi=unpack('f2',$valore);
1043             #chomp($leggi);
1044             #print "leggi: $leggi\n";
1045 0           print OUT "$kx\t$val_lon\t$val_lat\t$leggi\n";
1046 0           $kx++;
1047 0           $val_lon = $val_lon+$res;
1048             }
1049 0           $val_lat = $val_lat-$res;
1050             }
1051 0           close(IN);
1052 0           close(OUT);
1053             #-------------------------------------------------------------------------------
1054              
1055              
1056             #preso da scrivi_out.pl
1057             #-------------------------------------------------------------------------------
1058             #creo la griglia di output a 0.1 degree
1059            
1060 0 0         open(FOUT,'>out_01degree.txt') or die "Non apre file out_01degree.txt!!";
1061             #
1062             ##SCRIVE HEADER & INIZIALIZZA VARIABILI
1063 0           print FOUT "\tx\ty\n";
1064 0           $val_lat=$maxlat;
1065 0           my $val_lon=$minlon-$res;
1066 0           my $prog=1;
1067 0           my $res10=0.1;
1068 0           my $nrig10=$nrig*10;
1069 0           my $ncol10 = $ncol*10;
1070             #
1071             ##SCRIVE FILE OUT IN IDRISI MODE (DALL'ANGOLO IN ALTO A SINISTRA, QUINDI VERSO DESTRA E VERSO IL BASSO!)
1072 0           for(my $i=0;$i<$nrig10;$i++) {
1073 0           for(my $j=0;$j<$ncol10;$j++) {
1074 0           $val_lon=$val_lon+$res10;
1075 0           print FOUT "$prog\t$val_lon\t$val_lat\n";
1076 0           $prog++;
1077             }
1078 0           $val_lat=$val_lat-$res10;
1079 0           $val_lon=$minlon-$res10;
1080             }
1081             #
1082             ##CHIUDE FILE OUT
1083 0           close(FOUT);
1084             #-------------------------------------------------------------------------------
1085              
1086            
1087             #preso da kriging.pl
1088             #-------------------------------------------------------------------------------
1089            
1090            
1091             #($filein, $variogramma, $distanza)=@ARGV;
1092            
1093            
1094 0           my $file_r = $fileout."\.r";
1095             # $datiin = "$filein"."\.txt";
1096 0           my $datiin = $file_temp;
1097 0           my $variogramma = "Exp";# valore standard del Kriging
1098 0           my $distanza = "300";# valore standard del Kriging
1099 0           my $curdir = cwd();
1100              
1101            
1102             #scrive script di R
1103 0 0         open(FOUT,">$file_r") || die "Non apre file output R\n";
1104            
1105 0           print FOUT "\n";
1106 0           print FOUT '#Carica le librerie necessarie'."\n";
1107 0           print FOUT 'library(gstat, logical.return = T)'."\n\n";
1108 0           print FOUT '#setta la directory di lavoro'."\n";
1109 0           print FOUT "setwd(\'$curdir\')"."\n\n";
1110 0           print FOUT '#lettura dati'."\n";
1111 0           print FOUT "datiin <- read\.table(\"$datiin\")"."\n";
1112 0           print FOUT "datiout <- read\.table(\"out_01degree\.txt\")"."\n\n";
1113 0           print FOUT '#spazializza (kriging ordinario) rispetto alla colonna con nome variab'."\n";
1114 0           print FOUT "mdlvgm <- vgm(10, \"$variogramma\", $distanza)"."\n";
1115 0           print FOUT 'kriout <- krige(variab~1, ~x+y, data = datiin, newd = datiout, model = mdlvgm, nmax = 10, nmin = 5)'."\n\n";
1116 0           print FOUT '#salva il contenuto della variabile predetta in un file txt (vettore colonna)'."\n";
1117             #print FOUT "write\.table(kriout, file = \'$curdir/temp\.txt\', append = FALSE, quote = FALSE, sep = \"\\t\", "."\n";
1118 0           print FOUT "write\.table(kriout, file = \'temp\.txt\', append = FALSE, quote = FALSE, sep = \"\\t\", "."\n";
1119 0           print FOUT "\teol = \"\\n\", na = \'-999\', dec = \'\.\', row\.names = TRUE, col\.names = FALSE)"."\n\n";
1120 0           print FOUT '#esce'."\n";
1121 0           print FOUT 'quit(save="no")'."\n";
1122            
1123 0           close(FOUT);
1124            
1125 0           system($self->{R_PATH}." --no-save < $file_r");
1126             #system('del out_01degree.txt');
1127             #-------------------------------------------------------------------------------
1128              
1129             #trasformo il file temp.txt in IDRISI
1130 0           open (IN, "
1131 0 0         open (OUT, ">$file_in") or die "il file $file_in non si apre!!";
1132 0           binmode(OUT);
1133 0           while(){
1134 0           my $rigo = $_;
1135 0           chomp($rigo);
1136             #print "rigo 1075: $rigo\n";
1137 0           (my $a1,my $a2,my $a3,my $valore)=split(/\t/,$rigo);
1138 0           $valore=sprintf("%5.1f",$valore);
1139             #$valbin=pack('f',$valore);
1140 0           my $valbin = pack ('f',$valore);
1141 0           print OUT $valbin;
1142             }
1143 0           close(IN);
1144 0           close(OUT);
1145 0           my $min_value = $self->rdcGetValue($nome_file_rdc,"min. value");
1146 0           my $max_value = $self->rdcGetValue($nome_file_rdc,"max. value");
1147              
1148            
1149 0           open(SCRIVI_RDC,">$nome_file_rdc");
1150 0           print SCRIVI_RDC "file format : IDRISI Raster A.1\n";
1151 0           print SCRIVI_RDC "file title : $file_in\n";
1152 0           print SCRIVI_RDC "data type : real\n";
1153 0           print SCRIVI_RDC "file type : binary\n";
1154 0           print SCRIVI_RDC "columns : $ncol10\n";
1155 0           print SCRIVI_RDC "rows : $nrig10\n";
1156 0           print SCRIVI_RDC "ref. system : latlong\n";
1157 0           print SCRIVI_RDC "ref. units : deg\n";
1158 0           print SCRIVI_RDC "unit dist. : 1.0000000\n";
1159 0           print SCRIVI_RDC "min. X : $minlon\n";
1160              
1161 0           print SCRIVI_RDC "max. X : $maxlon\n";
1162 0           print SCRIVI_RDC "min. Y : $minlat\n";
1163              
1164 0           print SCRIVI_RDC "max. Y : $maxlat\n";
1165 0           print SCRIVI_RDC "pos'n error : unknown\n";
1166 0           print SCRIVI_RDC "resolution : $res10\n";
1167 0           print SCRIVI_RDC "min. value : $min_value\n";
1168 0           print SCRIVI_RDC "max. value : $max_value\n";
1169 0           print SCRIVI_RDC "display min : $min_value\n";
1170 0           print SCRIVI_RDC "display max : $max_value\n";
1171 0           print SCRIVI_RDC "value units : unknown\n";
1172 0           print SCRIVI_RDC "value error : unknown\n";
1173 0           print SCRIVI_RDC "flag value : none\n";
1174 0           print SCRIVI_RDC "flag def'n : none\n";
1175 0           print SCRIVI_RDC "legend cats : 0";
1176            
1177             #elimanates useless files
1178             #system("rm temp.txt");
1179            
1180             #closes files
1181 0           close(SCRIVI_RDC);
1182            
1183             }
1184              
1185              
1186             sub idrisi2png_exe {
1187 0     0 0   my $self = shift;
1188 0           my $key = shift;
1189 0           my $value = shift;
1190             # my $key = @_[0];
1191             # my $value = @_[1];
1192            
1193            
1194             # ($fileout, $nrig, $ncol, $minlon, $minlat, $res)=@ARGV;
1195             # ($key, $value)=@ARGV;
1196             # $nrig = 26;
1197             # $ncol = 68;
1198             # $minlon=-18;
1199             # $minlat = 3;
1200             # $res = 1;
1201            
1202            
1203              
1204              
1205 0           my $fileout = $key."_".$value;
1206            
1207            
1208              
1209 0           my $data = $self->forecast_db_date(time);
1210 0           my $fra7gg=(time+518400);
1211 0           my $data_fra7gg= $self->forecast_db_date($fra7gg);
1212 0           my $file_rst = "media_".$fileout."\.rst";
1213 0           my $nome_file_rdc = "media_".$fileout."\.rdc";
1214 0           my $file_png = $fileout."_"."$data"."\.png";
1215 0           my $file_ctl = $fileout."_"."$data"."\.ctl";
1216 0           my $file_gs = $fileout."_"."$data"."\.gs";
1217             #$file_gra = $fileout."_"."$data"."_gra"."\.rst";
1218 0           my $file_gra = $file_rst;
1219            
1220            
1221 0           my $nrig = $self->rdcGetValue($nome_file_rdc,"rows");
1222 0           my $ncol = $self->rdcGetValue($nome_file_rdc,"columns");
1223             # my $nrig = $self->{D_LAT};
1224             # my $ncol = $self->{D_LON};
1225            
1226            
1227 0           my $minlon= $self->{MINLON};
1228 0           my $minlat = $self->{MINLAT};
1229             #my $res = 1;
1230 0           my $res = $self->rdcGetValue($nome_file_rdc,"resolution");
1231            
1232            
1233             #
1234             ##CREA CTL
1235 0 0         open(CTL,">$file_ctl") || die "Non apre file ctl ($file_ctl)\n";
1236 0           print CTL "dset ^$file_gra"."\n";
1237 0           print CTL "title \"titolo_mancante Date:"."\n";
1238 0           print CTL "OPTIONS yrev"."\n"; #rovescia le Y
1239 0           print CTL "Undef -999"."\n";
1240 0           print CTL "xdef $ncol linear $minlon $res"."\n";
1241 0           print CTL "ydef $nrig linear $minlat $res"."\n";
1242 0           print CTL "zdef 1 levels 500hpa"."\n";
1243 0           print CTL "TDEF 1 LINEAR 00Z1aug1982 10dy"."\n";
1244 0           print CTL "vars 1"."\n";
1245 0           print CTL "$fileout\t0 99 Trend"."\n"; #qua va messo il nome della variabile da visualizzare
1246 0           print CTL "endvars"."\n";
1247 0           close(CTL);
1248            
1249             #
1250             ##CREA GS
1251            
1252 0 0         open(OUT,">muletto\.gs") || die "Non apre file $file_gs\n";
1253            
1254            
1255 0           print OUT "'open $file_ctl'\n";
1256 0           print OUT "'set mpdset hires'\n";
1257 0 0         if ($fileout=~m/PRES/) {
    0          
1258 0           print OUT "'set gxout contour'\n";
1259             } elsif ($fileout=~m/GRD/) {
1260 0           print OUT "'set gxout vector'\n";
1261             } else {
1262 0           print OUT "'set gxout shaded'\n";
1263             }
1264 0           print OUT "'set grads off'\n";
1265 0           print OUT "'set grid off'\n";
1266             #
1267             ##PALETTE
1268 0 0         if ($fileout=~m/APCP/) {
1269 0 0         if ($fileout=~m/hr/) {
1270 0           print OUT "
1271             ' set rgb 20 255 255 255'
1272             ' set rgb 21 180 240 250'
1273             ' set rgb 22 120 185 250'
1274             ' set rgb 23 80 165 245'
1275             ' set rgb 24 40 130 240'
1276             ' set rgb 25 30 110 235'
1277             ' set rgb 26 255 232 120'
1278             ' set rgb 27 255 192 60'
1279             ' set rgb 28 255 96 0'
1280             ' set rgb 29 255 50 0'
1281             ' set rgb 30 192 0 0'
1282             ' set rgb 31 165 0 0'
1283             ' set rgb 32 240 220 210'
1284             ' set rgb 33 200 255 190'
1285             ' set rgb 34 150 245 140'
1286              
1287             'set ccols 20 32 33 34 21 22 23 24 25 26 27 28 29 30 31'
1288             'set clevs 0 1 2 4 6 12 16 20 25 30 40 50 80 100'
1289             ";
1290             } else {
1291 0           print OUT "
1292             ' set rgb 20 255 255 255'
1293             ' set rgb 21 180 240 250'
1294             ' set rgb 22 120 185 250'
1295             ' set rgb 23 80 165 245'
1296             ' set rgb 24 40 130 240'
1297             ' set rgb 25 30 110 235'
1298             ' set rgb 26 255 232 120'
1299             ' set rgb 27 255 192 60'
1300             ' set rgb 28 255 96 0'
1301             ' set rgb 29 255 50 0'
1302             ' set rgb 30 192 0 0'
1303             ' set rgb 31 165 0 0'
1304             ' set rgb 32 240 220 210'
1305             ' set rgb 33 200 255 190'
1306             ' set rgb 34 150 245 140'
1307              
1308             'set ccols 20 32 33 34 21 22 23 24 25 26 27 28 29 30 31'
1309             'set clevs 0 5 10 20 40 80 100 120 150 200 250 300 400'
1310             ";
1311             }
1312             }
1313 0 0         if ($fileout=~m/TMP/) {
1314 0           print OUT "
1315             ' set rgb 20 50 0 50'
1316             ' set rgb 21 100 0 100'
1317             ' set rgb 22 150 0 150'
1318             ' set rgb 23 200 0 200'
1319             ' set rgb 24 250 0 250'
1320             ' set rgb 25 200 0 250'
1321             ' set rgb 26 150 0 250'
1322             ' set rgb 27 100 0 250'
1323             ' set rgb 28 50 0 250'
1324             ' set rgb 29 0 50 250'
1325             ' set rgb 30 0 100 250'
1326             ' set rgb 31 0 150 250'
1327             ' set rgb 32 0 200 250'
1328             ' set rgb 33 0 230 240'
1329             ' set rgb 34 0 230 160'
1330             ' set rgb 35 0 230 120'
1331             ' set rgb 36 0 230 80'
1332             ' set rgb 37 0 240 40'
1333             ' set rgb 38 0 250 0'
1334             ' set rgb 39 254 254 0'
1335             ' set rgb 40 254 225 0'
1336             ' set rgb 41 254 200 0'
1337             ' set rgb 42 254 175 0'
1338             ' set rgb 43 254 150 0'
1339             ' set rgb 44 230 125 0'
1340             ' set rgb 45 230 100 0'
1341             ' set rgb 46 220 75 30'
1342             ' set rgb 47 200 50 30'
1343             ' set rgb 48 180 25 30'
1344             ' set rgb 49 170 0 30'
1345             ' set rgb 50 180 0 50'
1346             ' set rgb 51 200 0 100'
1347             ' set rgb 52 254 0 150'
1348             ' set rgb 53 254 0 200'
1349              
1350             'set ccols 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'
1351             'set clevs -42 -39 -36 -33 -30 -27 -24 -21 -18 -15 -12 -9 -6 -3 0 3 6 9 12 15 18 21 24 27 30 33 36 39 42'
1352             ";
1353             }
1354 0 0         if ($fileout=~m/RH/) {
1355 0           print OUT "
1356             ' set rgb 20 255 232 120'
1357             ' set rgb 21 255 250 170'
1358             ' set rgb 22 230 255 225'
1359             ' set rgb 23 200 255 190'
1360             ' set rgb 24 180 250 170'
1361             ' set rgb 25 150 210 250'
1362             ' set rgb 26 120 185 250'
1363             ' set rgb 27 80 165 245'
1364             ' set rgb 28 160 140 255'
1365             ' set rgb 29 128 112 235'
1366             ' set rgb 30 72 60 200'
1367              
1368             'set ccols 20 21 22 23 24 25 26 27 28 29 30'
1369             'set clevs 10 20 30 40 50 60 70 80 90'
1370             ";
1371             }
1372             #
1373             ##DISPLAY VARIABLE
1374              
1375 0           print OUT "'display $fileout'\n";
1376            
1377             #se non hanno inserito il parametro cbarn non stampo la palette
1378 0 0         if ($self->{CBARN_PATH}) {
1379 0           print OUT "'run ".$self->{CBARN_PATH}."'\n";
1380             }
1381              
1382            
1383             ##TITLE
1384 0           my $subtitle = undef;
1385            
1386 0 0         if ($fileout=~m/1000/) {
    0          
    0          
1387 0           $subtitle='Level 1000 mb -';
1388             } elsif ($fileout=~m/925/) {
1389 0           $subtitle='Level 925 mb -';
1390             } elsif ($fileout=~m/850/) {
1391 0           $subtitle='Level 850 mb -';
1392             } else {
1393 0           $subtitle='Level Surface -';
1394             }
1395             #################VALIDITA' PREVISIONE#################
1396            
1397 0 0         if ($fileout=~m/APCP[1-9]/) {
1398 0           my $previ = $fileout;
1399 0           $previ =~s /APCP([1-9])+_[a-z]+/$1/g;
1400 0           $previ--;
1401 0           $subtitle="$subtitle"." Forecast $data 00Z+ $previ dy";
1402             } else {
1403 0           $subtitle="$subtitle"." Forecast $data 00Z valid until $data_fra7gg";
1404             }
1405             #################VALIDITA' PREVISIONE#################
1406 0 0         if ($fileout=~m/APCP/) {
    0          
    0          
    0          
    0          
    0          
1407 0           print OUT "'draw title TOTAL PRECIPITATION [mm]\\$subtitle'\n";
1408             } elsif ($fileout=~m/RH/) {
1409 0           print OUT "'draw title RELATIVE HUMIDITY [%]\\$subtitle'\n";
1410             } elsif ($fileout=~m/TMP/) {
1411 0           print OUT "'draw title TEMPERATURE [C]\\$subtitle'\n";
1412             } elsif ($fileout=~m/PRES/) {
1413 0           print OUT "'draw title PRESSURE [mb]\\$subtitle'\n";
1414             } elsif ($fileout=~m/UGRD/) {
1415 0           print OUT "'draw title ZONAL WIND [m/s]\\$subtitle'\n";
1416             } elsif ($fileout=~m/VGRD/) {
1417 0           print OUT "'draw title MERIDIONAL WIND [m/s]\\$subtitle'\n";
1418             }
1419             #
1420             ##SCRITTE VARIE
1421 0 0 0       if (($fileout=~m/TMP/) || ($fileout=~m/VGRD/) || ($fileout=~m/UGRD/) || ($fileout=~m/RH/)) {
      0        
      0        
1422 0           print OUT "'set gxout contour'\n";
1423 0           print OUT "'display $fileout'\n";
1424             }
1425             #
1426             ##SAVES PNG & QUIT
1427             # print OUT "'printim $curdir\\$file_png x800 y600 white'\n";
1428 0           print OUT "'printim $file_png x800 y600 white'\n";
1429              
1430            
1431             #print OUT "'clear'\n";
1432 0           print OUT "'quit'\n";
1433             # print OUT " return\n";
1434 0           close(OUT);
1435            
1436             ##
1437 0           system($self->{GRADSC_PATH}." -blc muletto\.gs");
1438             # print "idrisi2png conpleted\n";
1439             }
1440              
1441              
1442             sub idrisi_grd2png_exe {
1443 0     0 0   my $self = shift;
1444 0           my $key = shift;
1445 0           my $value = shift;
1446             # my $key = @_[0];
1447             # my $value = @_[1];
1448            
1449            
1450             # ($fileout, $nrig, $ncol, $minlon, $minlat, $res)=@ARGV;
1451             # ($key, $value)=@ARGV;
1452             # $nrig = 26;
1453             # $ncol = 68;
1454             # $minlon=-18;
1455             # $minlat = 3;
1456             # $res = 1;
1457 0           my $nrig = $self->{D_LAT};
1458 0           my $ncol = $self->{D_LON};
1459            
1460            
1461 0           my $minlon= $self->{MINLON};
1462 0           my $minlat = $self->{MINLAT};
1463 0           my $res = 1;
1464              
1465             #my $fileout = $key."_".$value;
1466 0           my $fileout = "WIND_".$value;
1467            
1468            
1469              
1470 0           my $data = $self->forecast_db_date(time);
1471 0           my $fra7gg=(time+518400);
1472 0           my $data_fra7gg= $self->forecast_db_date($fra7gg);
1473             # my $file_rst = "media_".$fileout."\.rst";
1474 0           my $file_png = $fileout."_"."$data"."\.png";
1475             # my $file_ctl = $fileout."_"."$data"."\.ctl";
1476 0           my $file_gs = $fileout."_"."$data"."\.gs";
1477             # #$file_gra = $fileout."_"."$data"."_gra"."\.rst";
1478             # my $file_gra = $file_rst;
1479            
1480             #NOMI FILE u
1481 0           my $file_rst_u = "media_UGRD_".$value."\.rst";
1482 0           my $file_ctl_u = $fileout."_u\.ctl";
1483 0           my $file_gs_u = $fileout."_u\.gs";
1484 0           my $file_gra_u = $file_rst_u;
1485            
1486             #NOMI FILE v
1487 0           my $file_rst_v = "media_VGRD_".$value."\.rst";
1488 0           my $file_ctl_v = $fileout."_v\.ctl";
1489 0           my $file_gs_v = $fileout."_v\.gs";
1490 0           my $file_gra_v = $file_rst_v;
1491            
1492             #
1493             ##CREA CTL U
1494 0 0         open(CTL,">$file_ctl_u") || die "Non apre file ctl ($file_ctl_u)\n";
1495 0           print CTL "dset ^$file_gra_u"."\n";
1496 0           print CTL "title \"titolo_mancante Date:"."\n";
1497 0           print CTL "OPTIONS yrev"."\n"; #rovescia le Y
1498 0           print CTL "Undef -999"."\n";
1499 0           print CTL "xdef $ncol linear $minlon $res"."\n";
1500 0           print CTL "ydef $nrig linear $minlat $res"."\n";
1501 0           print CTL "zdef 1 levels 500hpa"."\n";
1502 0           print CTL "TDEF 1 LINEAR 00Z1aug1982 10dy"."\n";
1503 0           print CTL "vars 1"."\n";
1504 0           print CTL "$fileout\t0 99 Trend"."\n"; #qua va messo il nome della variabile da visualizzare
1505 0           print CTL "endvars"."\n";
1506 0           close(CTL);
1507            
1508             ##CREA CTL V
1509 0 0         open(CTL,">$file_ctl_v") || die "Non apre file ctl ($file_ctl_v)\n";
1510 0           print CTL "dset ^$file_gra_v"."\n";
1511 0           print CTL "title \"titolo_mancante Date:"."\n";
1512 0           print CTL "OPTIONS yrev"."\n"; #rovescia le Y
1513 0           print CTL "Undef -999"."\n";
1514 0           print CTL "xdef $ncol linear $minlon $res"."\n";
1515 0           print CTL "ydef $nrig linear $minlat $res"."\n";
1516 0           print CTL "zdef 1 levels 500hpa"."\n";
1517 0           print CTL "TDEF 1 LINEAR 00Z1aug1982 10dy"."\n";
1518 0           print CTL "vars 1"."\n";
1519 0           print CTL "$fileout\t0 99 Trend"."\n"; #qua va messo il nome della variabile da visualizzare
1520 0           print CTL "endvars"."\n";
1521 0           close(CTL);
1522            
1523             #
1524             ##CREA GS
1525            
1526 0 0         open(OUT,">muletto\.gs") || die "Non apre file $file_gs\n";
1527            
1528            
1529 0           print OUT "'open $file_ctl_u'\n";
1530 0           print OUT "'open $file_ctl_v'\n";
1531 0           print OUT "'set mpdset hires'\n";
1532            
1533 0           print OUT "'set gxout vector'\n";
1534            
1535 0           print OUT "'set grads off'\n";
1536 0           print OUT "'set grid off'\n";
1537            
1538             ##DISPLAY VARIABLE
1539              
1540 0           print OUT "'display $fileout.1;$fileout.2'\n";
1541              
1542              
1543            
1544             ##TITLE
1545 0           my $subtitle = undef;
1546            
1547 0 0         if ($fileout=~m/1000/) {
    0          
    0          
1548 0           $subtitle='Level 1000 mb -';
1549             } elsif ($fileout=~m/925/) {
1550 0           $subtitle='Level 925 mb -';
1551             } elsif ($fileout=~m/850/) {
1552 0           $subtitle='Level 850 mb -';
1553             } else {
1554 0           $subtitle='Level Surface -';
1555             }
1556             #################VALIDITA' PREVISIONE#################
1557            
1558              
1559 0           $subtitle="$subtitle"." Forecast $data 00Z valid until $data_fra7gg";
1560            
1561             #################VALIDITA' PREVISIONE#################
1562              
1563 0           print OUT "'draw title WIND [m/s]\\$subtitle'\n";
1564            
1565              
1566             ##SAVES PNG & QUIT
1567             # print OUT "'printim $curdir\\$file_png x800 y600 white'\n";
1568 0           print OUT "'printim $file_png x800 y600 white'\n";
1569              
1570            
1571             #print OUT "'clear'\n";
1572 0           print OUT "'quit'\n";
1573             # print OUT " return\n";
1574 0           close(OUT);
1575            
1576             ##
1577 0           system($self->{GRADSC_PATH}." -blc muletto\.gs");
1578             # print "idrisi2png conpleted\n";
1579             }
1580              
1581             sub cleanUp {
1582 0     0 0   my $self = shift;
1583            
1584 0           my @parameters = {};
1585 0 0         if ( ref( $_[0] ) eq "ARRAY" ) {
1586 0           @parameters = @{ $_[0] };
  0            
1587             } else {
1588 0           @parameters = @_;
1589             }
1590              
1591            
1592 0           foreach my $parameter (@parameters){
1593 0           print "parameter: $parameter\n";
1594 0 0         if($parameter eq 'temp'){
1595 0           $self->_debug( "deleting: *.txt | *.r | *.tmp | *.ctl | muletto.gs\n");
1596 0           unlink (<*.txt>) ;
1597 0           unlink (<*.r>) ;
1598 0           unlink (<*.tmp> );
1599 0           unlink (<*.ctl>) ;
1600 0           unlink () ;
1601            
1602             }
1603 0 0         if($parameter eq 'grib'){
1604 0           $self->_debug( "deleting: gfs*\n");
1605 0           unlink () ;
1606            
1607             }
1608 0 0         if($parameter eq 'png'){
1609 0           $self->_debug( "deleting: *.png\n");
1610 0           unlink (<*.png>) ;
1611            
1612             }
1613 0 0         if($parameter eq 'idrisi'){
1614 0           $self->_debug( "deleting: *.rdc | *.rst\n");
1615 0           unlink (<*.rdc>) ;
1616 0           unlink (<*.rst>) ;
1617             }
1618            
1619            
1620             }
1621            
1622            
1623            
1624             }
1625              
1626             #########################################################################
1627             #
1628             # STATIC methods go here
1629             #
1630             #------------------------------------------------------------------------
1631             sub is_integer {
1632 0     0 0   my $self = shift;
1633 0           my $value = shift;
1634 0 0         if ("".$value =~ /[-+]?[0-9]*[^a-z\.]/ ) {
1635 0           $self->_debug("Value is: ".$value);
1636 0           return 1;
1637             }
1638             else {
1639 0           $self->_debug("Value is: null ");
1640 0           return 0;
1641             }
1642             }
1643              
1644            
1645             sub absolute_integer_value {
1646 0     0 0   my $self = shift;
1647 0           my $value = shift;
1648             #$self->_debug("Value in: ".$value);
1649            
1650             #elimino qualsiasi decimale.
1651 0           $value =~ s/([1-9]*)[\.\,][1-9]+/$1/g;
1652              
1653             #tolgo tutti i caratteri AlfaBetici, punti e virgole
1654 0           $value =~ s/[A-Za-z-+\.\,]//g;
1655            
1656             #$self->_debug("Value out: ".$value);
1657            
1658 0           return $value;
1659             }
1660              
1661              
1662            
1663             sub data_formattata_forecast {
1664             #questa subroutine si aspetta la funzione "time"
1665             #in entrata oppure un'altro valure di data similare
1666 0     0 0   my $self = shift;
1667 0           my $adesso = shift;
1668 0           my ($sec,$min,$hour,$mday,$mon,$year)=localtime($adesso);
1669            
1670 0           $sec = $self->number_format_00($sec);
1671 0           $min = $self->number_format_00($min);
1672 0           $hour = $self->number_format_00($hour);
1673 0           $mday = $self->number_format_00($mday);
1674 0           $mon = $self->number_format_00($mon+1);
1675 0           $year = $self->number_format_00($year);
1676            
1677 0           return "$mday/$mon/$year - $hour:$min:$sec";
1678             }
1679              
1680            
1681             sub forecast_db_date {
1682             #questa subroutine si aspetta la funzione "time"
1683             #in entrata oppure un'altro valure di data similare
1684 0     0 0   my $self = shift;
1685 0           my $adesso = shift;
1686 0           my ($sec,$min,$hour,$mday,$mon,$year)=localtime($adesso);
1687            
1688 0           $sec = $self->number_format_00($sec);
1689 0           $min = $self->number_format_00($min);
1690 0           $hour = $self->number_format_00($hour);
1691 0           $mday = $self->number_format_00($mday);
1692 0           $mon = $self->number_format_00($mon+1);
1693 0           $year = $self->number_format_00($year);
1694            
1695 0           return "$mday$mon$year";
1696             }
1697              
1698              
1699             sub number_format_00 {
1700 0     0 0   my $self = shift;
1701 0           my $num = shift;
1702 0           my $len = length($num);
1703             #print $len;
1704 0 0         if($len > 2){
1705 0           my $inizio = $len - 2;
1706 0           $num = substr($num,$inizio);
1707             }
1708 0 0         if($len <2){
1709 0           $num = "0".$num;
1710             }
1711 0           return $num;
1712             }
1713              
1714              
1715              
1716             sub rdcGetValue {
1717             # $self->_rdcGetValue($rdc_file,$variable_name)
1718 0     0 0   my $self = shift;
1719 0           my $rdc_file = shift;
1720 0           my $variable_name = shift;
1721            
1722 0           my $return = undef;
1723            
1724 0           open(RDC,"<$rdc_file");
1725 0           while () {
1726 0           chomp($_);
1727 0           my $rigo = $_;
1728 0           my @elementi = split / : /,$rigo;
1729 0           my $var = $elementi[0];
1730 0           my $var1 = $var;
1731 0           my $var2 = $var;
1732 0           $var1 =~ s/([a-zA-Z]+\s?[a-zA-Z]+)\s+/$1/g;
1733 0           $var2 =~ s/(\S+\s*\S*)\s+/$1/g;
1734             # print "var1 : '$var1'='$variable_name'\n";
1735             # print "var2 : '$var2'='$variable_name'\n";
1736             # if (length($var1)>0){$var = $var1}
1737             # if (length($var2)>0){$var = $var2}
1738             # print "var2 : $var2\n";
1739 0 0 0       if($variable_name eq $var1 || $variable_name eq $var2 || $variable_name eq $var){$return = $elementi[1];}
  0   0        
1740             }
1741 0           close(RDC);
1742 0           return $return;
1743             }
1744              
1745              
1746              
1747              
1748             1;
1749             __END__