maderman has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm having a bit of trouble with the following code. The problem is when I want to plot data for the current day, the script returns a plot with nothing on it, even though the my output data file contains the right information. Otherwise it works like a dream. Is there a problem with the sorting routine (compare_dates)? I can't work it out.... Thanks, Stacy.
#!/usr/local/bin/perl -w $plotymax = 1E-10; $plotymin = 1E-05; $temp = $$; $GNU = "/usr/local/gnu/bin/gnuplot"; $gps2_log = 'gps.log'; $start_day = 01; $start_month = 'Mar'; $end_day = 11; $end_month = 'Apr'; %months = qw{Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06 Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12}; $sdate_cursor = [$months{$start_month},$start_day,0,0,0]; $edate_cursor = [$months{$end_month},$end_day,23,59,59]; $date_cursor = [0,0,0,0,0] unless(defined($date_cursor)); open(CLOCK,"$gps2_log") or die "$gps2_log not available: $!\n"; open(GPSmk4,">>/tmp/gpsmk4.dat"); while($line = <CLOCK>) { next if $line =~ /last/; ($month,$day,$time) = ( split(/\s+/,$line) )[0..2]; ($hour,$min,$sec) = split(':',$time); $month = sprintf "%02d", $months{$month}; $day = sprintf "%02d", $day; $data3 = ( split(/\s+/,$line) )[6]; last if (compare_dates([$month,$day,$hour,$min,$sec], $edate_cursor) + > 0); if (compare_dates([$month,$day,$hour,$min,$sec], $sdate_cursor) > 0) { if( $data3 > $plotymax ) { $plotymax = $data3; } if( $data3 < $plotymin ) { $plotymin = $data3; } print GPSmk4 "$month-$day $time $data3\n"; } # update the date cursor $date_cursor = [$month,$day,$hour,$min,$sec]; } #end of while close(CLOCK); print "$plotymin $plotymax\n"; @files = `ls -1 /tmp/*gps*.dat`; chomp(@files); $plotline = join('', 'plot', join "\,", map { " \"$_\" using 1:3" } @f +iles); ######### Make GIF ######### open(GNUPLOT,">/tmp/$temp-gps.plt"); print GNUPLOT <<EOF; set terminal gif set output "/tmp/$temp-gps.gif" set parametric set zero 1e-20 set xdata time set timefmt "%m-%d %H:%M:%S" set yrange [$plotymin:$plotymax] set format x "%d/%m\\n%H:%M" set xrange ["$months{$start_month}-$start_day 00:00:00":"$months{$end_ +month}-$end_day 23:59:59"] set grid $plotline exit EOF close(GNUPLOT); chdir("/tmp"); `$GNU "$temp-gps.plt"`; #-------------------- sub compare_dates { my($a, $b) = @_; foreach (0..4) { if ($$a[$_]>$$b[$_]) { return 1; } elsif ($$a[$_]<$$b[$_]) { return -1; } } return 0; } #-------------------- ##################### GPS.LOG sample input Jan 1 00:29:59 pkdesk 51909 48450 1.716667E-07 Jan 1 00:34:59 pkdesk 51909 48750 1.750000E-07 Jan 1 00:39:59 pkdesk 51909 49050 1.733333E-07 Jan 1 00:44:59 pkdesk 51909 49350 1.550000E-07 Jan 1 00:49:59 pkdesk 51909 49650 1.796667E-07 Jan 1 00:54:59 pkdesk 51909 49950 1.630000E-07 Jan 1 00:59:59 pkdesk 51909 50250 1.730000E-07 Jan 1 01:04:59 pkdesk 51909 50550 1.956667E-07 Jan 1 01:09:59 pkdesk 51909 50850 1.836667E-07 Jan 1 01:14:59 pkdesk 51909 51150 1.720000E-07 Jan 1 01:19:59 pkdesk 51909 51450 1.740000E-07 ############ Sample GPSMK4.DAT output 03-01 00:04:59 8.430000E-07 03-01 00:09:59 8.433333E-07 03-01 00:14:59 8.390000E-07 03-01 00:19:59 8.463333E-07 03-01 00:24:59 8.716667E-07 03-01 00:29:59 9.013333E-07 03-01 00:34:59 8.640000E-07 03-01 00:44:59 8.336667E-07 03-01 00:49:59 8.370000E-07 03-01 00:54:59 8.383333E-07 03-01 00:59:59 8.340000E-07 03-01 01:04:59 8.306667E-07 03-01 01:09:59 8.296667E-07 03-01 01:14:59 8.416667E-07

Replies are listed 'Best First'.
Re: Date sorting
by stefan k (Curate) on Apr 11, 2001 at 11:36 UTC
    Hi,
    the first thing that comes to my mind is that recent gnuplot versions don't support the terminal "gif" anymore. Maybe you want to try changing that to e.g. png.

    Update: Further Examination(tm) shows:

    1. No terminal "gif" in my gnuplot:
      $ ./datesort.pl 1e-05 1e-10 gnuplot> set terminal gif ^ "27968-gps.plt", line 1: unknown or ambiguous terminal type; +type just 'set terminal' for a list
    2. The resulting PNG image is a very good looking chart without any data plotted *grin*
    3. The datafile gpsmk4.dat is empty (that becomes visible easier, when you don't put your files into /tmp; not speaking of not deleting them at the end of your programm...)
    4. if (compare_dates([$month,$day,$hour,$min,$sec], $sdate_cursor) > 0)
      this statement is never true (your start-month is March and all data is from January...?)
      So maybe you want to check that logic...

    Regards Stefan K

      The problem was solved by closing the file GPsmk4 (see below). Leaving this filehandle open caused all my problems! Thanks for your suggestions ! Regards, Stacy.
      open(CLOCK,"$gps2_log") or die "$gps2_log not available: $!\n"; open(GPSmk4,">>/tmp/gpsmk4.dat"); while($line = <CLOCK>) { next if $line =~ /last/; ($month,$day,$time) = ( split(/\s+/,$line) )[0..2]; ($hour,$min,$sec) = split(':',$time); $month = sprintf "%02d", $months{$month}; $day = sprintf "%02d", $day; $data3 = ( split(/\s+/,$line) )[6]; last if(compare_dates([$month,$day,$hour,$min,$sec],$edate_cursor)>0); if (compare_dates([$month,$day,$hour,$min,$sec],$sdate_cursor)>0); { if( $data3 > $plotymax ) { $plotymax = $data3; } if( $data3 < $plotymin ) { $plotymin = $data3; } print GPSmk4 "$month-$day $time $data3\n"; } # update the date cursor $date_cursor = [$month,$day,$hour,$min,$sec]; +close(GPSmk4); } #end of while