r654: *** empty log message ***
[ctsim.git] / cgi-bin / ctsim.cgi.in
1 #!/usr/bin/perl
2 # @configure_input@
3
4 use strict;
5 use CGI;
6 use File::Basename;
7 use Fcntl ':flock';
8
9 require './ctsim.conf';
10
11 my $opt_d = 0;
12 $ENV{'PATH'} = "$::bindir:$::lamrundir:/bin";
13
14 my $fromhost = $ENV{'REMOTE_HOST'};
15
16 my $gmt_start = time();
17 my $gmt_end;
18 my $execution_time;
19
20 my %in;
21 CGI::ReadParse(\%in);
22
23 # Incoming variables 
24 #   Phantom_Name, Phantom_Nx, Phantom_Ny, Phantom_NSample
25 #   PJ_NDet, PJ_NRot, PJ_NRay, PJ_RotAngle,
26 #   IR_Nx, IR_Ny, IR_Filter, IR_Filter_Param
27
28 my $error = "";
29
30 if ($::single_process_only > 0) {
31   my @processes = `/bin/ps -A`;
32   my $running = 0;
33   foreach my $p (@processes) {
34     if ($p =~ m/ctsimtext/) {
35       $error .= "Another online simulation is currently running.<br>You may wait a moment and then click your browser's <b>Refresh</b> button to resubmit your simulation<br>";
36       last;
37     }
38   }
39 }
40
41 my $Phantom_Name = FilterMetaChars($in{'Phantom_Name'});
42 my $Phantom_Nx = FilterToNumber($in{'Phantom_Nx'});
43 my $Phantom_Ny = FilterToNumber($in{'Phantom_Ny'});
44 my $Phantom_NSample = FilterToNumber($in{'Phantom_NSample'});
45 $error .= "Phantom name must not be blank<br>" if ($Phantom_Name eq "");
46 $error .= "Phantom NX and NY must be between 5 and 512<br>" if ($Phantom_Nx < 5 || $Phantom_Nx > 512 || $Phantom_Ny < 5 || $Phantom_Ny > 512);
47 $error .= "Phantom NSample must be between 1 and 5<br>" if ($Phantom_NSample < 1 || $Phantom_NSample > 5);
48
49 my $PJ_Geometry = FilterMetaChars($in{'PJ_Geometry'});
50 my $PJ_NDet = FilterToNumber($in{'PJ_NDet'});
51 my $PJ_NRot = FilterToNumber($in{'PJ_NRot'});
52 my $PJ_FocalLength = FilterToNumber($in{'PJ_FocalLength'});
53 my $PJ_NRay = FilterToNumber($in{'PJ_NRay'});
54 my $PJ_RotAngle = FilterToNumber($in{'PJ_RotAngle'});
55 $error .= "Projection NDet must be between 5 and 1000<br>" if ($PJ_NDet < 5 || $PJ_NDet > 1000);
56 $error .= "Projection NRot must be between 5 and 1000<br>" if ($PJ_NRot < 5 || $PJ_NRot > 1000);
57 $error .= "Projection NRay must be between 1 and 5<br>" if ($PJ_NRay < 1 || $PJ_NRay > 5);
58 $error .= "Projection RotAngle must be between 0.1 and 2<br>" if ($PJ_RotAngle < 0.1 || $PJ_RotAngle > 2);
59 $error .= "Focal length must be between 1.01 and 10<b>" if ($PJ_FocalLength <= 1 || $PJ_FocalLength > 10);
60
61 #my $IR_Nx = FilterToNumber($in{'IR_Nx'});
62 #my $IR_Ny = FilterToNumber($in{'IR_Ny'});
63 my $IR_Nx = $Phantom_Nx;
64 my $IR_Ny = $Phantom_Ny;
65 my $IR_FilterMethod = FilterMetaChars($in{'IR_FilterMethod'});
66 my $IR_Zeropad = FilterToNumber($in{'IR_Zeropad'});
67 my $IR_Filter = FilterMetaChars($in{'IR_Filter'});
68 my $IR_Filter_Param = FilterToNumber($in{'IR_Filter_Param'});
69 my $IR_Interp = FilterMetaChars($in{'IR_Interp'});
70 my $IR_Backproj = FilterMetaChars($in{'IR_Backproj'});
71
72 my $Disp_Min = "auto";
73 my $Disp_Max = "auto";
74 $Disp_Min = FilterToNumber($in{'Disp_Min'}) if ($in{'Disp_Min'} ne "auto" && $in{'Disp_Min'} ne "");
75 $Disp_Max = FilterToNumber($in{'Disp_Max'}) if ($in{'Disp_Max'} ne "auto" && $in{'Disp_Max'} ne "");
76 if ($Disp_Min ne 'auto' && ! ($Disp_Min =~ /^[\d\.\-]+$/)) {
77     $error .= "Display Minimum must be 'auto' or numeric (received $Disp_Min) <br>";
78 }
79 if ($Disp_Max ne 'auto' && ! ($Disp_Max =~ /^[\d\.\-]+$/)) {
80     $error .= "Display Maximum must be 'auto' or numeric (received $Disp_Max) <br>";
81 }
82
83 my $MPI_Str = FilterMetaChars($in{'MPI'});
84 my $MPI = 0;
85 $MPI = 1 if ($MPI_Str eq "yes" && $::mpi_enable ne "");
86
87 $error .= "IR Nx and Ny must be between 5 and 1024<br>" if ($IR_Nx < 5 || $IR_Nx > 1024 || $IR_Ny < 5 || $IR_Ny > 1024);
88 $error .= "IR Filter Parameter must be between 0 and 1<br>" if ($IR_Filter_Param < 0 || $IR_Filter_Param > 1);
89
90
91 my $tmpid = $$;
92 my $auto_window_img = "std0.1";
93 my $auto_window_diff = "std1";
94 my $auto_window_pj = "full";
95 my $logfile = "$::jobdir/ctsim.log";
96
97 my $result_fname = "$::datadir/result-$tmpid.html";
98 my $phantom_fname = "$::datadir/phantom-$tmpid.if";
99 my $pj_fname = "$::datadir/pj-$tmpid.pj";
100 my $ir_fname = "$::datadir/ir-$tmpid.if";
101 my $pj_if_fname = "$::datadir/pj-$tmpid.if";
102 my $sub_fname = "$::datadir/sub-$tmpid.if";
103 my $phantom_png = "$::datadir/phantom-$tmpid.png";
104 my $ir_png = "$::datadir/ir-$tmpid.png";
105 my $pj_png = "$::datadir/pj-$tmpid.png";
106 my $sub_png = "$::datadir/sub-$tmpid.png";
107
108 my $result_url = "$::url_datadir/result-$tmpid.html";
109 my $phantom_png_url = "$::url_datadir/phantom-$tmpid.png";
110 my $ir_png_url = "$::url_datadir/ir-$tmpid.png";
111 my $pj_png_url = "$::url_datadir/pj-$tmpid.png";
112 my $sub_png_url = "$::url_datadir/sub-$tmpid.png";
113
114 my $pjrec_ver = "$::bindir/ctsimtext pjrec";
115 my $phm2pj_ver = "$::bindir/ctsimtext phm2pj";
116 my $phm2if_ver = "$::bindir/ctsimtext phm2if";
117 my $diff_ver = "$::bindir/ctsimtext if2";
118
119 $pjrec_ver = "/opt/lam/bin/mpirun N N $::lamrundir/pjrec-lam" if $MPI;
120 $phm2pj_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2pj-lam" if $MPI;
121 $phm2if_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2if-lam" if $MPI;
122
123 my $gp_cmd = "$phm2if_ver $phantom_fname $Phantom_Nx $Phantom_Ny --phantom $Phantom_Name --nsample $Phantom_NSample";
124 my $pj_cmd = "$phm2pj_ver $pj_fname $PJ_NDet $PJ_NRot --phantom $Phantom_Name --nray $PJ_NRay --rotangle $PJ_RotAngle --geometry $PJ_Geometry --focal-length $PJ_FocalLength";
125 my $pj_if_cmd = "$::bindir/pj2if $pj_fname $pj_if_fname";
126 my $pjrec_cmd = "$pjrec_ver $pj_fname $ir_fname $IR_Nx $IR_Ny --filter $IR_Filter --filter-param $IR_Filter_Param --interp $IR_Interp --backproj $IR_Backproj --filter-method $IR_FilterMethod --zeropad $IR_Zeropad";
127 my $sub_cmd = "$diff_ver $phantom_fname $ir_fname $sub_fname --sub";
128 my $diff_cmd = "$diff_ver $phantom_fname $ir_fname --comp";
129
130 my $window_options = "--auto $auto_window_img";
131 if ($Disp_Min ne 'auto') {
132     $window_options .= " --min $Disp_Min";
133 }
134 if ($Disp_Max ne 'auto') {
135     $window_options .= " --max $Disp_Max";
136 }
137
138 my $png1_cmd = "$::bindir/ifexport $phantom_fname $phantom_png $window_options --stats --format png";
139 my $png2_cmd = "$::bindir/ifexport $ir_fname $ir_png $window_options --stats --format png";
140 my $png3_cmd = "$::bindir/ifexport $pj_if_fname $pj_png --auto $auto_window_pj --stats --format png";
141 my $png4_cmd = "$::bindir/ifexport $sub_fname $sub_png --auto $auto_window_diff --stats --format png";
142
143 my $title = "CT Simulation Results";
144
145 my $out = head();
146 $out .= "<HTML> <HEAD><TITLE> $title </TITLE></HEAD>\n";
147 $out .= "<BODY  BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"  VLINK=\"#800020\" LINK=\"#0000FF\">\n";
148 $out .= "<H1>$title</H1><HR>\n";
149
150 if ($opt_d) {
151     $out .= "<H2>Commands</H2>\n";
152     $out .= "$gp_cmd<br>\n$pj_cmd<br>\n$pj_if_cmd<br>\n$pjrec_cmd<br>\n$diff_cmd<br>\n$png1_cmd<br>\n$png2_cmd<br>\n" .
153             "$png3_cmd<br>\n$png4_cmd<br>\n";
154 }
155
156 my $cmdout = "";
157 if ($error ne "") {
158     $out .= "<FONT COLOR=\"#FF0000\">The following errors were present in your request.<br>\n";
159     $out .= "Please correct them and try submitting your request again.</FONT><br>\n";
160     $out .= "<i>$error</i>";
161 } else {
162   my $gp_out;
163   my $pj_out;
164   my $pj_if_out;
165   my $pjrec_out;
166   my $sub_out;
167   my $diff_out;
168   my $png_gp_out;
169   my $png_pjrec_out;
170   my $png_pj_out;
171   my $png_sub_out;
172   $gp_out = `$gp_cmd`;
173   if (-s $phantom_fname) {
174     $pj_out .= `$pj_cmd`;
175     $png_gp_out .= `$png1_cmd`;
176     if (-s $pj_fname) {
177       $pj_if_out .= `$pj_if_cmd`;
178       $png_pj_out .= `$png3_cmd`;
179       $pjrec_out .= `$pjrec_cmd`;
180       if (-s $ir_fname) {
181         $png_pjrec_out .= `$png2_cmd`;
182         $sub_out .= `$sub_cmd`;
183         $diff_out .= `$diff_cmd`;
184         $png_sub_out .= `$png4_cmd`;
185       }
186     }
187   }
188   # Delete image files and projection files
189   unlink($phantom_fname);
190   unlink($ir_fname);
191   unlink($pj_fname);
192   unlink($pj_if_fname);
193   unlink($sub_fname);
194
195   $cmdout = "$gp_cmd\n $gp_out $pj_cmd\n $pj_out $pj_if_cmd\n $pj_if_out $pjrec_cmd\n $pjrec_out $diff_cmd\n $diff_out $png1_cmd\n $png_gp_out $png2_cmd\n $png_pjrec_out $png3_cmd\n $png_pj_out $png4_cmd\n $png_sub_out";
196   if (open(LOGFILE,">> $logfile")) {
197     flock(LOGFILE,LOCK_EX);
198     seek(LOGFILE, 0, 2);
199     print LOGFILE "Job $tmpid\n";
200     print LOGFILE $cmdout;
201     print LOGFILE "----------------------------------------------------\n";
202     flock(LOGFILE,LOCK_UN);
203     close(LOGFILE);
204   }
205   $gmt_end = time();
206   $execution_time = $gmt_end - $gmt_start;
207   if ($opt_d) {
208     $out .= "<H3>Command Output</H3>$cmdout<HR>\n";
209   }
210   my $png_gp_out_html = $png_gp_out;
211   my $png_pjrec_out_html = $png_pjrec_out;
212   my $png_pj_out_html = $png_pj_out;
213   my $png_sub_out_html = $png_sub_out;
214   $png_gp_out_html =~ s/\n/<br>/gms;
215   $png_pjrec_out_html =~ s/\n/<br>/gms;
216   $png_pj_out_html =~ s/\n/<br>/gms;
217   $png_sub_out_html =~ s/\n/<br>/gms;
218   $out .= "<TABLE><TR><TD>Phantom Image</TD><TD>Reconstructed Image</TD></TR>\n";
219   $out .= "<TR><TD><IMG SRC=\"$phantom_png_url\"><br><FONT SIZE=1>$png_gp_out</FONT></TD>\n";
220   $out .= "<TD><IMG SRC=\"$ir_png_url\"><br><FONT SIZE=1>$png_pjrec_out</FONT></TD></TR>\n";
221   $out .= "<TR><TD>Projection Sinusoid</TD><TD>Phantom/Reconst Error</TD></TR>\n";
222   $out .= "<TR><TD><IMG SRC=\"$pj_png_url\"><br><FONT SIZE=1>$png_pj_out</FONT></TD>\n";
223   $out .= "<TD><IMG SRC=\"$sub_png_url\"><br><FONT SIZE=2>$sub_out</FONT><br><FONT SIZE=1>$png_sub_out</FONT></TD></TR>\n";
224   $out .= "</TABLE>";
225   $out .= "Execution time: $execution_time seconds\n";
226   $out .= "<p>\nStatistics<br>";
227   $out .= "$diff_out";
228 }
229
230 $out .= "<HR>\n";
231 $out .= "Return to <A HREF=\"http://www.ctsim.org\">CTSim</a>.<br>\n";
232 $out .= "</BODY> </HTML>";
233 $out .= "\n";
234
235 if (open(OUTFILE,"> $result_fname"))
236 {
237     flock(OUTFILE,LOCK_EX);
238     print OUTFILE $out;
239     flock(OUTFILE,LOCK_UN);
240     close OUTFILE;
241     print "Location: $result_url\n\n";
242 }
243 else
244 {
245     print "Content-type: text/plain\n\n";
246     print "The simulator was unable to create an result file.\n";
247 }
248 if (open(JOBFILES,"> $::jobdir/$tmpid"))
249 {
250     flock(JOBFILES,LOCK_EX);
251     print JOBFILES "gmt_start=$gmt_start\n";
252     print JOBFILES "gmt_end=$gmt_end\n";
253     print JOBFILES "execution_time=$execution_time\n";
254     print JOBFILES "Phantom_Name=$Phantom_Name\n";
255     print JOBFILES "Phantom_Nx=$Phantom_Nx\n";
256     print JOBFILES "Phantom_Ny=$Phantom_Nx\n";
257     print JOBFILES "Phantom_NSample=$Phantom_NSample\n";
258     print JOBFILES "PJ_NDet=$PJ_NDet\n";
259     print JOBFILES "PJ_NRot=$PJ_NRot\n";
260     print JOBFILES "PJ_NRay=$PJ_NRay\n";
261     print JOBFILES "PJ_RotAngle=$PJ_RotAngle\n";
262     print JOBFILES "IR_Nx=$IR_Nx\n";
263     print JOBFILES "IR_Ny=$IR_Ny\n";
264     print JOBFILES "IR_Interp=$IR_Interp\n";
265     print JOBFILES "IR_Filter=$IR_Filter\n";
266     print JOBFILES "IR_Filter_Param=$IR_Filter_Param\n";
267     print JOBFILES "IR_Backproj=$IR_Backproj\n";
268     print JOBFILES "Disp_Min=$Disp_Min\n";
269     print JOBFILES "Disp_Max=$Disp_Max\n";
270     print JOBFILES "MPI=$MPI\n";
271     print JOBFILES "Files=$result_fname,$phantom_fname,$pj_fname,$ir_fname,$phantom_png,$ir_png,$pj_if_fname,$pj_png\n" if ($error eq "");
272     printf JOBFILES "cmdout=$cmdout\n";
273     flock(JOBFILES,LOCK_UN);
274     close JOBFILES;
275 }
276
277
278 exit;
279
280
281 sub internal_error
282 {
283   my $out = head();
284   $out .= "<H1>Internal error</H1>
285   Please notify <A HREF=mailto:webmaster\@med-info.com>webmaster\@med-info.com</A>
286   </BODY>";
287   print $out;
288   exit;
289 }
290
291 sub head
292 {
293 #  "Content: text/html\n\n";
294 }
295
296
297 sub FilterMetaChars
298 {
299    my $var = pop(@_);
300    $var =~ /^(\w+)$/;  
301    $1;
302 }
303
304 sub FilterToNumber
305 {
306    my $var = pop(@_);
307    $var =~ /^(\-*\d*\.*\d*)$/;  
308    $1;
309 }