root/branches/mcstas-1.x/mcplot.pl

Revision 2822, 15.4 KB (checked in by farhi, 2 years ago)

Import main SVN McCode/trunk into 1.x branch. mcrun --test OK. compilation of all example instruments OK.

  • Property svn:executable set to *
Line 
1#! /usr/bin/perl
2#
3# Implements perl interface for plotting McStas data output using PGPLOT,
4# gnuplot, Matlab or Scilab
5#
6#   This file is part of the McStas neutron ray-trace simulation package
7#   Copyright (C) 1997-2004, All rights reserved
8#   Risoe National Laborartory, Roskilde, Denmark
9#   Institut Laue Langevin, Grenoble, France
10#
11#   This program is free software; you can redistribute it and/or modify
12#   it under the terms of the GNU General Public License as published by
13#   the Free Software Foundation; version 2 of the License.
14#
15#   This program is distributed in the hope that it will be useful,
16#   but WITHOUT ANY WARRANTY; without even the implied warranty of
17#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18#   GNU General Public License for more details.
19#
20#   You should have received a copy of the GNU General Public License
21#   along with this program; if not, write to the Free Software
22#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24use FileHandle;
25use File::Basename;
26
27# Determine the path to the McStas system directory. This must be done
28# in the BEGIN block so that it can be used in a "use lib" statement
29# afterwards.
30
31use Config;
32BEGIN {
33  # default configuration (for all high level perl scripts)
34  if($ENV{"MCSTAS"}) {
35    $MCSTAS::sys_dir = $ENV{"MCSTAS"};
36  } else {
37    if ($Config{'osname'} eq 'MSWin32') {
38      $MCSTAS::sys_dir = "c:\\mcstas\\lib";
39    } else {
40      $MCSTAS::sys_dir = "/usr/local/lib/mcstas";
41    }
42  }
43  $MCSTAS::perl_dir = "$MCSTAS::sys_dir/tools/perl";
44  $MCSTAS::perl_modules = "$MCSTAS::perl_dir/modules";
45
46  # custom configuration (this script)
47  END {
48    if (-f $tmp_file ) {
49        if (!($plotter =~ /PGPLOT|McStas/i)) {
50            print "mcplot: Removing temporary $tmp_file (10 sec)\n";
51            sleep 10;
52        }
53        unlink($tmp_file) or die "mcplot: Couldn't unlink $tmp_file : $!";
54      }
55    }
56}
57
58
59
60use lib $MCSTAS::perl_dir;
61use lib $MCSTAS::perl_modules;
62require "mcstas_config.perl";
63
64# Overload with user's personal config
65if ($ENV{"HOME"} && -e $ENV{"HOME"}."/.mcstas/mcstas_config.perl") {
66  require $ENV{"HOME"}."/.mcstas/mcstas_config.perl";
67}
68
69
70# ADD/MOD: E. Farhi Sep 21th, 2001 : handle -ps and -psc for automatic
71# print and exit
72my ($default_ext);
73our ($file, $files);
74my $index =0;
75my $passed_arg_str = "";
76my $passed_arg_str_quit = "";
77my $inspect = "";
78our ($plotter);
79my $nowindow = 0;
80my $do_swap=0;
81my $daemon=0;
82my $wait=10;
83my $logmode=0;
84my $contourmode=0;
85my $global_dev = -1;
86my $global_spec ="";
87our $tmp_file = "";
88
89$plotter = $MCSTAS::mcstas_config{'PLOTTER'};
90
91for($i = 0; $i < @ARGV; $i++) {
92  $_ = $ARGV[$i];
93  # Options specific to mcplot.
94  if(/^-plot$/i) {
95      $do_plot = 1;
96  } elsif(/^-overview$/i) {
97      $do_overview = 1;
98  } elsif(/^-png$/i || /^-ps$/i || /^-cps$/i || /^-psc$/i || /^-ppm$/i || /^-scg$/i || /^-fig$/i) {
99      $passed_arg_str_quit .= "$_ ";
100  } elsif(/^-p([a-zA-Z0-9_]+)$/ || /^--plotter=([a-zA-Z0-9_]+)$/ || /^--format=([a-zA-Z0-9_]+)$/) {
101        $plotter = $1;
102  } elsif(/^-i([a-zA-Z0-9_]+)$/ || /^--inspect=([a-zA-Z0-9_]+)$/) {
103      $inspect = $1;
104  } elsif(/^-d$/ || /^--daemon$/) {
105      $daemon = 1;
106  } elsif(/^-w([0-9\.]+)$/ || /^--wait=([0-9\.]+)$/) {
107      $wait = $1;
108  } elsif(/^\+nw$/i || /^\+tk$/i || /^\+java$/i || /^--withwindow$/i) {
109      $nowindow = 0;
110  } elsif(/^-nw$/i || /^-nojvm$/i || /^--nowindow$/i) {
111      $nowindow = 1;
112  } elsif(/^-swap$/i) {
113      $do_swap = 1;
114  } elsif(/^-log/i) {
115      $logmode = 1;
116  } elsif(/^-contour/i) {
117      $contourmode = 1;
118  } elsif(/^--help$/i || /^-h$/i || /^-v$/i) {
119      print "mcplot [-ps|-psc|-gif] <simfile | detector_file>\n";
120      print "       [-pPLOTTER] Output graphics using {PGPLOT,gnuplot,Scilab,Matlab,HTML}\n";
121      print "                   The file extension will also set the PLOTTER\n";
122      print "       [-overview] Show all plots in a single window\n";
123      print "       [-plot]     Show all plots in separate window(s)\n";
124      print "       [-iCOMP]    Only show monitors whos name match COMP\n";
125      print "       [+nw]       Open {Scilab,Matlab} command window (with Tcl/Java)\n";
126      print "       [-nw]       Open {Scilab,Matlab} command window (without Tcl/Java)\n";
127      print "       [-log]      Plot results in log10 scale\n";
128      print "       [-contour]  Display matrix/images as contour plots\n";
129      print "  Plots all monitor data from a simulation, or a single data file.\n";
130      print "  When using -ps -psc -gif, the program writes the hardcopy file\n";
131      print "  and then exits.\n";
132      print "SEE ALSO: mcstas, mcdoc, mcplot, mcrun, mcgui, mcresplot, mcstas2vitess\n";
133      print "DOC:      Please visit http://www.mcstas.org\n";
134      exit;
135  } elsif(/^-([a-zA-Z0-9_]+)$/) {
136      $passed_arg_str_quit .= "-$1 ";
137  } else {
138      $files[$index] = $ARGV[$i];
139      $index++;
140  }
141}
142
143if ($do_plot)     { $passed_arg_str .= "-plot "; }
144if ($do_overview) { $passed_arg_str .= "-overview "; }
145if ($do_swap)     { $passed_arg_str .= "-swap "; }
146if ($logmode)     { $passed_arg_str .= "-log "; }
147
148if ($index == 0) {
149  $file = "mcstas";
150} else { $file = $files[0]; }
151if (-d $file) { # check if dir containing result file
152  my $newfile = "$file/mcstas";
153  if (-e "$newfile.m" || -e "$newfile.sci" || -e "$newfile.sim" || -e "$newfile.html" || -e "$newfile.nxs") {
154    $file = $newfile; }
155}
156
157# look if there is only one file type and set plotter to use
158if (-e "$file.m" and not -e "$file.sci" and not -e "$file.sim" and not -e "$file.html") { $plotter = "Matlab"; }
159if (-e "$file.sci" and not -e "$file.m" and not -e "$file.sim" and not -e "$file.html") { $plotter = "Scilab"; }
160if (-e "$file.sim" and not -e "$file.m" and not -e "$file.sci" and not -e "$file.html" and not ($plotter =~ /gnuplot/i)) { $plotter = "PGPLOT"; }
161if (-e "$file.html" and not -e "$file.m" and not -e "$file.sci" and not -e "$file.sim") { $plotter = "HTML";   }
162if (-e "$file.nxs") { $plotter = "NeXus";   }
163
164# set default extension from plotter
165if    ($plotter =~ /Scilab/i) { $default_ext = ".sci"; }
166elsif ($plotter =~ /Matlab/i) { $default_ext = ".m"; }
167elsif ($plotter =~ /PGPLOT|McStas|gnuplot/i) { $default_ext = ".sim"; }
168elsif ($plotter =~ /HTML/i) { $default_ext = ".html"; }
169elsif ($plotter =~ /NeXus/i) { $default_ext = ".nxs"; }
170
171# if no extension in file name, add default extension.
172if ($file !~ m'\.[^/]*$' && $default_ext) { $file .= $default_ext; }
173
174# set plotter from extension
175if ($file =~ m'\.m$')    { $plotter = "Matlab"; }
176if ($file =~ m'\.sci$' || $file =~ m'\.sce$') {$plotter = "Scilab"; }
177if ($file =~ m'\.sim$' and not($plotter =~ /gnuplot/i))  { $plotter = "PGPLOT"; }
178if ($file =~ m'\.html$') { $plotter = "HTML"; }
179if ($file =~ m'\.nxs$') { $plotter = "NeXus"; }
180
181# On Win32, chdir to directory containing base filename
182if ($Config{'osname'} eq 'MSWin32') {
183    my ($basename,$dirname)=fileparse($file);
184    $file = $basename;
185    if (-d $dirname) {
186  chdir $dirname;
187    }
188}
189
190# Added E. Farhi, March 2003. plotter (pgplot, scilab, matlab, html) -> $file
191if ($plotter =~ /Scilab/i && $MCSTAS::mcstas_config{'SCILAB'} ne "no") {
192  my $fh;
193  # create a temporary scilab execution script
194  if ($MCSTAS::mcstas_config{'TEMP'} ne "no") {
195    require File::Temp;
196    ($fh, $tmp_file) = File::Temp::tempfile("mcplot_tmpXXXXXX", SUFFIX => '.sce');
197    if (not defined $fh) { $tmp_file=""; }
198  }
199  if ($tmp_file eq "") {
200    $tmp_file="mcplot_tmp000000.sce";
201    $fh = new FileHandle "> $tmp_file";
202  }
203  if (not defined $fh) { die "Could not open temporary Scilab script $tmp_file\n"; }
204  autoflush $fh 1;
205  # write the scilab script
206  printf $fh "if execstr('stacksize(1e8);','errcatch') then execstr('stacksize(1e7);','errcatch'); end\n";
207  printf $fh "exec('$MCSTAS::sys_dir/tools/scilab/mcplot.sci',-1);\n";
208  printf $fh "global McPlotTempFile;\nMcPlotTempFile='$tmp_file';\n";
209  printf $fh "s=mcplot('$file');\n";
210  printf $fh "mprintf('s=mcplot(''$file'')\\n');\n";
211  if ($passed_arg_str_quit) {
212    printf $fh "quit\n";
213  } else {
214    printf $fh "if length(s)\n";
215    printf $fh "mprintf('mcplot: Simulation data structure from file $file\\n');\n";
216    printf $fh "mprintf('        is stored into variable s. Type in ''s'' at prompt to see it !\\n');\n";
217    printf $fh "end\n";
218  }
219
220  close($fh);
221  if ($nowindow) { system("$MCSTAS::mcstas_config{'SCILAB'} -nw -f $tmp_file\n"); }
222  else { system("$MCSTAS::mcstas_config{'SCILAB'} -f $tmp_file\n"); }
223
224} elsif ($plotter =~ /Matlab/i && $MCSTAS::mcstas_config{'MATLAB'} ne "no") {
225  my $tosend = "$MCSTAS::mcstas_config{'MATLAB'} ";
226  if ($nowindow) { $tosend .= "-nojvm -nosplash "; }
227  $tosend .= "-r \"addpath('$MCSTAS::sys_dir/tools/matlab');addpath(pwd);s=mcplot('$file','$passed_arg_str $passed_arg_str_quit','$inspect');";
228  $tosend .= "disp('s=mcplot(''$file'',''$passed_arg_str $passed_arg_str_quit'',''$inspect'')');";
229
230  if ($passed_arg_str_quit) {
231    $tosend .= "exit;\"\n";
232  } else {
233      $tosend .= "if length(s),";
234      $tosend .= "disp('type: help mcplot for this function usage.');";
235      $tosend .= "disp('mcplot: Simulation data structure from file $file');";
236      $tosend .= "disp('        is stored into variable s. Type in ''s'' at prompt to see it !');";
237      $tosend .= "end;\"\n";
238    }
239  system($tosend);
240} elsif ($plotter =~ /HTML|VRML/i && $MCSTAS::mcstas_config{'BROWSER'}) {
241  system("$MCSTAS::mcstas_config{'BROWSER'} $file");
242} elsif ($plotter =~ /HDF|NeXus/i && $MCSTAS::mcstas_config{'HDFVIEW'} ne "no") {
243  system("$MCSTAS::mcstas_config{'HDFVIEW'} $file");
244} elsif ($plotter =~ /gnuplot/i) {
245  # McStas original mcplot format using GNUPLOT
246  require "mcgnuplot.pl";
247  gnuplotit($file);
248} elsif ($plotter =~ /PGPLOT|McStas/i) {
249  # McStas original mcplot using perl/PGPLOT
250
251  require "mcfrontlib2D.pl";
252  require "mcplotlib.pl";
253
254  # ADD/MOD: E. Farhi/V. Hugouvieux Feb 18th, 2002 : handle detector files
255
256  # daemon mode is for PGPLOT mode only - quick plot, then exit
257  if (!($daemon eq 0)) {
258          pgplotit();
259          exit 1;
260  }  else {
261      pgplotit();
262  }
263
264} else {
265  if ($plotter =~ /PGPLOT|McStas/i && $MCSTAS::mcstas_config{'PGPLOT'} eq "no") {
266    print STDERR "\n******************************************************\n";
267    print STDERR "Default / selected PLOTTER is PGPLOT - Problems:\n\n";
268    print STDERR "PGPLOT.pm not found on Perl \@INC path\n\nSolutions:\n\n";
269    print STDERR "1) Install pgplot + pgperl packages (Unix/Linux/Cygwin) \n";
270    print STDERR "2) Rerun mcplot with -p/--plotter set to Scilab/Matlab/VRML \n";
271    print STDERR "3) Modify $MCSTAS::perl_dir/mcstas_config.perl\n";
272    print STDERR "   to set a different default plotter\n";
273    print STDERR "4) Set your env variable MCSTAS_FORMAT to set the default\n";
274    print STDERR "   data format and plotter\n";
275    print STDERR "5) Convert your PGPLOT/McStas files into an other format\n";
276    print STDERR "   using the mcformat tool\n";
277    print STDERR "******************************************************\n\n";
278  }
279  print STDERR "Using default Browser $MCSTAS::mcstas_config{'BROWSER'} to view result file $file\n";
280  system("$MCSTAS::mcstas_config{'BROWSER'} $file");
281}
282
283
284sub pgplotit {
285  my ($sim_file) = $file;
286  my ($instr_inf, $sim_inf, $datalist, $sim_error) = read_sim_file($file);
287  die "No data in simulation file '$file'"
288      unless @$datalist;
289  if ($passed_arg_str_quit =~ /-cps|-psc/i) {
290    $global_dev = get_device("$file.ps/cps");
291    overview_plot($datalist, $passed_arg_str);
292          die "Wrote color postscript file '$file.ps' (cps)\n";
293  } elsif ($passed_arg_str_quit =~ /-ps/) {
294    $global_dev = get_device("$file.ps/ps");
295    overview_plot($datalist, $passed_arg_str);
296          die "Wrote BW postscript file '$file.ps' (ps)\n";
297  } elsif ($passed_arg_str_quit =~ /-ppm/) {
298    $global_dev = get_device("$file.ppm/ppm");
299    overview_plot($datalist, $passed_arg_str);
300          die "Wrote PPM file '$file.ppm' (ppm)\n";
301  } elsif ($passed_arg_str_quit =~ /-png/) {
302    $global_dev = get_device("$file.png/png");
303    overview_plot($datalist, $passed_arg_str);
304          die "Wrote PNG file '$file.png' (png)\n";
305  } elsif ($passed_arg_str_quit =~ /-gif/) {
306    $global_dev = get_device("$file.gif/gif");
307    overview_plot($datalist, $passed_arg_str);
308          die "Wrote GIF file '$file.gif' (gif)\n";
309  }
310
311  if ($daemon eq 0) {
312      print "Click on a plot for full-window view.\n" if @$datalist > 1;
313      print "Press key for hardcopy (in graphics window), 'Q' to quit
314  'P' BW postscript
315  'C' color postscript
316  'N' PNG file
317  'M' PPM file
318  'G' GIF file
319  'L' Toggle log10 plotting mode
320  'T' Toggle contour plotting mode
321  'Q' quit\n";
322  } else {
323      $global_spec = "$ENV{'PGPLOT_DEV'}";
324      $global_dev = get_device($global_spec);
325      PGPLOT::pgask(0);
326      overview_plot($datalist, $passed_arg_str);
327  }
328  my $global_open = 0;
329  if ($daemon eq 0) {
330      for(;;) {
331          if ($global_open == 0) {
332              if ($Config{'osname'} eq 'MSWin32') {
333                  $global_open = 1;
334              }
335              $global_spec = "$ENV{'PGPLOT_DEV'}";
336              $global_dev = get_device($global_spec);
337              PGPLOT::pgask(0);
338          }
339          my ($cc,$cx,$cy,$idx);
340          if ($logmode == 1) { if ($passed_arg_str !~ /-log/i) { $passed_arg_str .= "-log "; } }
341          else { $passed_arg_str =~ s|-log||; }
342          if ($contourmode == 1) { if ($passed_arg_str !~ /-contour/i) { $passed_arg_str .= "-contour "; } }
343          else { $passed_arg_str =~ s|-contour||; }
344          # Do overview plot, letting user select a plot for full-screen zoom.
345          ($cc,$idx) = overview_plot($datalist, "$passed_arg_str -interactive ");
346          last if $cc =~ /[xq]/i;        # Quit?
347          if($cc =~ /[pcngm]/i) {        # Hardcopy?
348              my $ext="ps";
349              my $dev = ($cc =~ /c/i) ? "cps" : "ps";
350              if($cc =~ /g/i) { $dev = "gif"; $ext="gif"; }
351              if($cc =~ /n/i) { $dev = "png"; $ext="png"; }
352              if($cc =~ /m/i) { $dev = "ppm"; $ext="ppm"; }
353              system("mcplot$MCSTAS::mcstas_config{'suffix'} --format=McStas $passed_arg_str -$dev $file");
354              next;
355          }
356          elsif($cc =~ /[l]/i) {        # toggle log mode
357            if ($logmode == 0) { $logmode=1; }
358            else { $logmode=0; $passed_arg_str =~ s|-log||; }
359            next;
360          }
361          elsif($cc =~ /[t]/i) {        # toggle contour plot mode
362            if ($contourmode == 0) { $contourmode=1; }
363            else { $contourmode=0; $passed_arg_str =~ s|-contour||; }
364            next;
365          }
366          # now do a full-screen version of the plot selected by the user.
367          ($cc, $cx, $cy) = single_plot($datalist->[$idx], "$passed_arg_str -interactive ");
368          last if $cc =~ /[xq]/i;        # Quit?
369          if($cc =~ /[pcngm]/i) {        # Hardcopy?
370              my $ext="ps";
371              my $dev = ($cc =~ /c/i) ? "cps" : "ps";
372              if($cc =~ /g/i) { $dev = "gif"; $ext="gif"; }
373              if($cc =~ /n/i) { $dev = "png"; $ext="png"; }
374              if($cc =~ /m/i) { $dev = "ppm"; $ext="ppm"; }
375              my $filename = "$datalist->[$idx]{'Filename'}";
376              print "Spawning plot of $filename \n";
377              system("mcplot$MCSTAS::mcstas_config{'suffix'} --format=McStas $passed_arg_str $filename -$dev");
378          }
379          if($cc =~ /[l]/i) {        # toggle log mode
380            if ($logmode == 0) { $logmode=1; }
381            else { $logmode=0; $passed_arg_str =~ s|-log||; }
382            next;
383          }
384          if($cc =~ /[t]/i) {        # toggle contour plot mode
385            if ($contourmode == 0) { $contourmode=1; }
386            else { $contourmode=0; $passed_arg_str =~ s|-contour||; }
387            next;
388          }
389      }
390  }
391}
Note: See TracBrowser for help on using the browser.