Fix spelling mistake
[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_ViewRatio = FilterToNumber($in{'Phantom_ViewRatio'});
45 my $Phantom_NSample = FilterToNumber($in{'Phantom_NSample'});
46 $error .= "Phantom name must not be blank<br>" if ($Phantom_Name eq "");
47 $error .= "Phantom Size must be between 5 and 512<br>" if ($Phantom_Nx < 5 || $Phantom_Nx > 512 || $Phantom_Ny < 5 || $Phantom_Ny > 512);
48 $error .= "Phantom NSample must be between 1 and 5<br>" if ($Phantom_NSample < 1 || $Phantom_NSample > 5);
49 $error .= "View Ratio must be between 0 and 100<br>" if ($Phantom_ViewRatio <= 0 || $Phantom_ViewRatio > 100);
50
51 my $PJ_Geometry = FilterMetaChars($in{'PJ_Geometry'});
52 my $PJ_NDet = FilterToNumber($in{'PJ_NDet'});
53 my $PJ_NRot = FilterToNumber($in{'PJ_NRot'});
54 my $PJ_FocalLength = FilterToNumber($in{'PJ_FocalLength'});
55 my $PJ_NRay = FilterToNumber($in{'PJ_NRay'});
56 my $PJ_RotAngle = FilterToNumber($in{'PJ_RotAngle'});
57 my $PJ_ScanRatio = FilterToNumber($in{'PJ_ScanRatio'});
58 $error .= "Projection NDet must be between 5 and 1000<br>" if ($PJ_NDet < 5 || $PJ_NDet > 1000);
59 $error .= "Projection NRot must be between 5 and 1000<br>" if ($PJ_NRot < 5 || $PJ_NRot > 1000);
60 $error .= "Projection NRay must be between 1 and 5<br>" if ($PJ_NRay < 1 || $PJ_NRay > 5);
61 $error .= "Projection RotAngle must be between 0.1 and 2<br>" if ($PJ_RotAngle < 0.1 || $PJ_RotAngle > 2);
62 $error .= "Focal length must be between 1.01 and 10<b>" if ($PJ_FocalLength <= 1 || $PJ_FocalLength > 10);
63 $error .= "Scan Ratio must be between 0 and 10<br>" if ($PJ_ScanRatio <= 0 || $PJ_ScanRatio > 10);
64
65 #my $IR_Nx = FilterToNumber($in{'IR_Nx'});
66 #my $IR_Ny = FilterToNumber($in{'IR_Ny'});
67 my $IR_Nx = $Phantom_Nx;
68 my $IR_Ny = $Phantom_Ny;
69 my $IR_FilterMethod = FilterMetaChars($in{'IR_FilterMethod'});
70 my $IR_Zeropad = FilterToNumber($in{'IR_Zeropad'});
71 my $IR_Filter = FilterMetaChars($in{'IR_Filter'});
72 my $IR_Filter_Param = FilterToNumber($in{'IR_Filter_Param'});
73 my $IR_Interp = FilterMetaChars($in{'IR_Interp'});
74 my $IR_Backproj = FilterMetaChars($in{'IR_Backproj'});
75
76 my $Disp_Min = "auto";
77 my $Disp_Max = "auto";
78 $Disp_Min = FilterToNumber($in{'Disp_Min'}) if ($in{'Disp_Min'} ne "auto" && $in{'Disp_Min'} ne "");
79 $Disp_Max = FilterToNumber($in{'Disp_Max'}) if ($in{'Disp_Max'} ne "auto" && $in{'Disp_Max'} ne "");
80 if ($Disp_Min ne 'auto' && ! ($Disp_Min =~ /^[\d\.\-]+$/)) {
81     $error .= "Display Minimum must be 'auto' or numeric (received $Disp_Min) <br>";
82 }
83 if ($Disp_Max ne 'auto' && ! ($Disp_Max =~ /^[\d\.\-]+$/)) {
84     $error .= "Display Maximum must be 'auto' or numeric (received $Disp_Max) <br>";
85 }
86
87 my $MPI_Str = FilterMetaChars($in{'MPI'});
88 my $MPI = 0;
89 $MPI = 1 if ($MPI_Str eq "yes" && $::mpi_enable ne "");
90
91 $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);
92 $error .= "IR Filter Parameter must be between 0 and 1<br>" if ($IR_Filter_Param < 0 || $IR_Filter_Param > 1);
93
94
95 my $tmpid = $$;
96 my $auto_window_img = "std0.1";
97 my $auto_window_diff = "std1";
98 my $auto_window_pj = "full";
99 my $logfile = "$::jobdir/ctsim.log";
100
101 my $result_fname = "$::datadir/result-$tmpid.html";
102 my $phantom_fname = "$::datadir/phantom-$tmpid.if";
103 my $pj_fname = "$::datadir/pj-$tmpid.pj";
104 my $ir_fname = "$::datadir/ir-$tmpid.if";
105 my $pj_if_fname = "$::datadir/pj-$tmpid.if";
106 my $sub_fname = "$::datadir/sub-$tmpid.if";
107 my $phantom_png = "$::datadir/phantom-$tmpid.png";
108 my $ir_png = "$::datadir/ir-$tmpid.png";
109 my $pj_png = "$::datadir/pj-$tmpid.png";
110 my $sub_png = "$::datadir/sub-$tmpid.png";
111
112 my $result_url = "$::url_datadir/result-$tmpid.html";
113 my $phantom_png_url = "$::url_datadir/phantom-$tmpid.png";
114 my $ir_png_url = "$::url_datadir/ir-$tmpid.png";
115 my $pj_png_url = "$::url_datadir/pj-$tmpid.png";
116 my $sub_png_url = "$::url_datadir/sub-$tmpid.png";
117
118 my $pjrec_ver = "$::bindir/ctsimtext pjrec";
119 my $phm2pj_ver = "$::bindir/ctsimtext phm2pj";
120 my $phm2if_ver = "$::bindir/ctsimtext phm2if";
121 my $diff_ver = "$::bindir/ctsimtext if2";
122
123 $pjrec_ver = "/opt/lam/bin/mpirun N N $::lamrundir/pjrec-lam" if $MPI;
124 $phm2pj_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2pj-lam" if $MPI;
125 $phm2if_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2if-lam" if $MPI;
126
127 my $gp_cmd = "$phm2if_ver $phantom_fname $Phantom_Nx $Phantom_Ny --phantom $Phantom_Name --nsample $Phantom_NSample --view-ratio $Phantom_ViewRatio";
128 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 --view-ratio $Phantom_ViewRatio --scan-ratio $PJ_ScanRatio";
129 my $pj_if_cmd = "$::bindir/pj2if $pj_fname $pj_if_fname";
130 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";
131 my $sub_cmd = "$diff_ver $phantom_fname $ir_fname $sub_fname --sub";
132 my $diff_cmd = "$diff_ver $phantom_fname $ir_fname --comp";
133
134 my $window_options = "--auto $auto_window_img";
135 if ($Disp_Min ne 'auto') {
136     $window_options .= " --min $Disp_Min";
137 }
138 if ($Disp_Max ne 'auto') {
139     $window_options .= " --max $Disp_Max";
140 }
141
142 my $png1_cmd = "$::bindir/ifexport $phantom_fname $phantom_png $window_options --stats --format png --center mode";
143 my $png2_cmd = "$::bindir/ifexport $ir_fname $ir_png $window_options --stats --format png --center mode";
144 my $png3_cmd = "$::bindir/ifexport $pj_if_fname $pj_png --auto $auto_window_pj --stats --format png --center mode";
145 my $png4_cmd = "$::bindir/ifexport $sub_fname $sub_png --auto $auto_window_diff --stats --format png --center mode";
146
147 my $title = "CTSim Results";
148
149 my $out = head();
150 $out .= "<HTML> <HEAD><TITLE> $title </TITLE><LINK rel=\"stylesheet\" href=\"http://www.ctsim.org/main.css\" type=\"text/css\">
151 </HEAD>\n";
152 $out .= "<BODY  BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"  VLINK=\"#800020\" LINK=\"#0000FF\">\n";
153 $out .= "<H1>$title</H1><HR>\n";
154
155 if ($opt_d) {
156     $out .= "<H2>Commands</H2>\n";
157     $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" .
158             "$png3_cmd<br>\n$png4_cmd<br>\n";
159 }
160
161 my $cmdout = "";
162 if ($error ne "") {
163     $out .= "<FONT COLOR=\"#FF0000\">The following errors were present in your request.<br>\n";
164     $out .= "Please correct them and try submitting your request again.</FONT><br>\n";
165     $out .= "<i>$error</i>";
166 } else {
167   my $gp_out;
168   my $pj_out;
169   my $pj_if_out;
170   my $pjrec_out;
171   my $sub_out;
172   my $diff_out;
173   my $png_gp_out;
174   my $png_pjrec_out;
175   my $png_pj_out;
176   my $png_sub_out;
177   $gp_out = `$gp_cmd`;
178   if (-s $phantom_fname) {
179     $pj_out .= `$pj_cmd`;
180     $png_gp_out .= `$png1_cmd`;
181     if (-s $pj_fname) {
182       $pj_if_out .= `$pj_if_cmd`;
183       $png_pj_out .= `$png3_cmd`;
184       $pjrec_out .= `$pjrec_cmd`;
185       if (-s $ir_fname) {
186         $png_pjrec_out .= `$png2_cmd`;
187         $sub_out .= `$sub_cmd`;
188         $diff_out .= `$diff_cmd`;
189         $png_sub_out .= `$png4_cmd`;
190       }
191     }
192   }
193   # Delete image files and projection files
194   unlink($phantom_fname);
195   unlink($ir_fname);
196   unlink($pj_fname);
197   unlink($pj_if_fname);
198   unlink($sub_fname);
199
200   $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";
201   if (open(LOGFILE,">> $logfile")) {
202     flock(LOGFILE,LOCK_EX);
203     seek(LOGFILE, 0, 2);
204     print LOGFILE "Client Address: $ENV{'REMOTE_ADDR'}\n";
205     print LOGFILE "Job $tmpid\n";
206     print LOGFILE $cmdout;
207     print LOGFILE "----------------------------------------------------\n";
208     flock(LOGFILE,LOCK_UN);
209     close(LOGFILE);
210   }
211   $gmt_end = time();
212   $execution_time = $gmt_end - $gmt_start;
213   if ($opt_d) {
214     $out .= "<H3>Command Output</H3>$cmdout<HR>\n";
215   }
216   my $png_gp_out_html = $png_gp_out;
217   my $png_pjrec_out_html = $png_pjrec_out;
218   my $png_pj_out_html = $png_pj_out;
219   my $png_sub_out_html = $png_sub_out;
220   $png_gp_out_html =~ s/\n/<br>/gms;
221   $png_pjrec_out_html =~ s/\n/<br>/gms;
222   $png_pj_out_html =~ s/\n/<br>/gms;
223   $png_sub_out_html =~ s/\n/<br>/gms;
224   $out .= "<TABLE><TR><TD><b>Phantom Image</b></TD><TD><b>Reconstructed Image</b></TD></TR>\n";
225   $out .= "<TR><TD><IMG SRC=\"$phantom_png_url\"><br><FONT SIZE=1>$png_gp_out</FONT></TD>\n";
226   $out .= "<TD><IMG SRC=\"$ir_png_url\"><br><FONT SIZE=1>$png_pjrec_out</FONT></TD></TR>\n";
227   $out .= "<TR><TD><b>Projection Sinogram</b></TD><TD><b>Reconstruction Error</b></TD></TR>\n";
228   $out .= "<TR><TD><IMG SRC=\"$pj_png_url\"><br><FONT SIZE=1>$png_pj_out</FONT></TD>\n";
229   $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";
230   $out .= "</TABLE>";
231   $out .= "<p><b>Error Measurements</b><br>";
232   $out .= "$diff_out";
233   $out .= "<p>Execution time: $execution_time seconds\n";
234 }
235
236 $out .= "<HR>\n";
237 $out .= "Specify another <a href=\"http://www.ctsim.org/simulate.shtml\">simulation</a>.<br>";
238 $out .= "Return to CTSim's <A HREF=\"http://www.ctsim.org\">home</a>.<br>\n";
239 $out .= "</BODY> </HTML>";
240 $out .= "\n";
241
242 if (open(OUTFILE,"> $result_fname"))
243 {
244     flock(OUTFILE,LOCK_EX);
245     print OUTFILE $out;
246     flock(OUTFILE,LOCK_UN);
247     close OUTFILE;
248     print "Location: $result_url\n\n";
249 }
250 else
251 {
252     print "Content-type: text/plain\n\n";
253     print "The simulator was unable to create an result file.\n";
254 }
255 if (open(JOBFILES,"> $::jobdir/$tmpid"))
256 {
257     flock(JOBFILES,LOCK_EX);
258     print JOBFILES "execution_time=$execution_time\n";
259     print JOBFILES "Phantom_Name=$Phantom_Name\n";
260     print JOBFILES "Phantom_Nx=$Phantom_Nx\n";
261     print JOBFILES "Phantom_Ny=$Phantom_Nx\n";
262     print JOBFILES "Phantom_NSample=$Phantom_NSample\n";
263     print JOBFILES "PJ_NDet=$PJ_NDet\n";
264     print JOBFILES "PJ_NRot=$PJ_NRot\n";
265     print JOBFILES "PJ_NRay=$PJ_NRay\n";
266     print JOBFILES "PJ_RotAngle=$PJ_RotAngle\n";
267     print JOBFILES "IR_Nx=$IR_Nx\n";
268     print JOBFILES "IR_Ny=$IR_Ny\n";
269     print JOBFILES "IR_Interp=$IR_Interp\n";
270     print JOBFILES "IR_Filter=$IR_Filter\n";
271     print JOBFILES "IR_Filter_Param=$IR_Filter_Param\n";
272     print JOBFILES "IR_Backproj=$IR_Backproj\n";
273     print JOBFILES "Disp_Min=$Disp_Min\n";
274     print JOBFILES "Disp_Max=$Disp_Max\n";
275     print JOBFILES "MPI=$MPI\n";
276     print JOBFILES "Files=$result_fname,$phantom_fname,$pj_fname,$ir_fname,$phantom_png,$ir_png,$pj_if_fname,$pj_png\n" if ($error eq "");
277     printf JOBFILES "cmdout=$cmdout\n";
278     flock(JOBFILES,LOCK_UN);
279     close JOBFILES;
280 }
281
282
283 exit;
284
285
286 sub internal_error
287 {
288   my $out = head();
289   $out .= "<H1>Internal error</H1>
290   Please notify <A HREF=mailto:webmaster\@med-info.com>webmaster\@med-info.com</A>
291   </BODY>";
292   print $out;
293   exit;
294 }
295
296 sub head
297 {
298 #  "Content: text/html\n\n";
299 }
300
301
302 sub FilterMetaChars
303 {
304    my $var = pop(@_);
305    $var =~ /^([-\w]+)$/;  
306    $1;
307 }
308
309 sub FilterToNumber
310 {
311    my $var = pop(@_);
312    $var =~ /^(\-*\d*\.*\d*)$/;  
313    $1;
314 }