r121: *** 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";
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 my $Phantom_Name = FilterMetaChars($in{'Phantom_Name'});
31 my $Phantom_Nx = FilterToNumber($in{'Phantom_Nx'});
32 my $Phantom_Ny = FilterToNumber($in{'Phantom_Ny'});
33 my $Phantom_NSample = FilterToNumber($in{'Phantom_NSample'});
34 $error .= "Phantom name must not be blank<br>" if ($Phantom_Name eq "");
35 $error .= "Phantom NX and NY must be between 5 and 1024<br>" if ($Phantom_Nx < 5 || $Phantom_Nx > 1024 || $Phantom_Ny < 5 || $Phantom_Ny > 1024);
36 $error .= "Phantom NSample must be between 1 and 10<br>" if ($Phantom_NSample < 1 || $Phantom_NSample > 10);
37
38 my $PJ_NDet = FilterToNumber($in{'PJ_NDet'});
39 my $PJ_NRot = FilterToNumber($in{'PJ_NRot'});
40 my $PJ_NRay = FilterToNumber($in{'PJ_NRay'});
41 my $PJ_RotAngle = FilterToNumber($in{'PJ_RotAngle'});
42 $error .= "Projection NDet must be between 5 and 1800<br>" if ($PJ_NDet < 5 || $PJ_NDet > 1800);
43 $error .= "Projection NRot must be between 5 and 2048<br>" if ($PJ_NRot < 5 || $PJ_NRot > 2048);
44 $error .= "Projection RotAngle must be between 0.1 and 2<br>" if ($PJ_RotAngle < 0.1 || $PJ_RotAngle > 2);
45
46 #my $IR_Nx = FilterToNumber($in{'IR_Nx'});
47 #my $IR_Ny = FilterToNumber($in{'IR_Ny'});
48 my $IR_Nx = $Phantom_Nx;
49 my $IR_Ny = $Phantom_Ny;
50 my $IR_Filter = FilterMetaChars($in{'IR_Filter'});
51 my $IR_Filter_Param = FilterToNumber($in{'IR_Filter_Param'});
52 my $IR_Interp = FilterMetaChars($in{'IR_Interp'});
53 my $IR_Backproj = FilterMetaChars($in{'IR_Backproj'});
54
55 my $Disp_Min = "auto";
56 my $Disp_Max = "auto";
57 $Disp_Min = FilterToNumber($in{'Disp_Min'}) if ($in{'Disp_Min'} ne "auto" && $in{'Disp_Min'} ne "");
58 $Disp_Max = FilterToNumber($in{'Disp_Max'}) if ($in{'Disp_Max'} ne "auto" && $in{'Disp_Max'} ne "");
59 if ($Disp_Min ne 'auto' && ! ($Disp_Min =~ /^[\d\.\-]+$/)) {
60     $error .= "Display Minimum must be 'auto' or numeric (received $Disp_Min) <br>";
61 }
62 if ($Disp_Max ne 'auto' && ! ($Disp_Max =~ /^[\d\.\-]+$/)) {
63     $error .= "Display Maximum must be 'auto' or numeric (received $Disp_Max) <br>";
64 }
65
66 my $MPI_Str = FilterMetaChars($in{'MPI'});
67 my $MPI = 0;
68 $MPI = 1 if ($MPI_Str eq "yes" && $::mpi_enable ne "");
69
70 $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);
71 $error .= "IR Filter Parameter must be between 0 and 1<br>" if ($IR_Filter_Param < 0 || $IR_Filter_Param > 1);
72
73
74 my $tmpid = $$;
75 my $auto_window_img = "std0.1";
76 my $auto_window_diff = "std1";
77 my $auto_window_pj = "full";
78 my $logfile = "$::jobdir/ctsim.log";
79
80 my $result_fname = "$::datadir/result-$tmpid.html";
81 my $phantom_fname = "$::datadir/phantom-$tmpid.if";
82 my $pj_fname = "$::datadir/pj-$tmpid.pj";
83 my $ir_fname = "$::datadir/ir-$tmpid.if";
84 my $pj_if_fname = "$::datadir/pj-$tmpid.if";
85 my $diff_fname = "$::datadir/diff-$tmpid.if";
86 my $phantom_png = "$::datadir/phantom-$tmpid.png";
87 my $ir_png = "$::datadir/ir-$tmpid.png";
88 my $pj_png = "$::datadir/pj-$tmpid.png";
89 my $diff_png = "$::datadir/diff-$tmpid.png";
90
91 my $result_url = "$::url_datadir/result-$tmpid.html";
92 my $phantom_png_url = "$::url_datadir/phantom-$tmpid.png";
93 my $ir_png_url = "$::url_datadir/ir-$tmpid.png";
94 my $pj_png_url = "$::url_datadir/pj-$tmpid.png";
95 my $diff_png_url = "$::url_datadir/diff-$tmpid.png";
96
97 my $pjrec_ver = "$::bindir/pjrec";
98 my $phm2pj_ver = "$::bindir/phm2pj";
99 my $phm2if_ver = "$::bindir/phm2if";
100 my $diff_ver = "$::bindir/if-2";
101 my $ifinfo_ver = "$::bindir/ifinfo";
102
103 $pjrec_ver = "/opt/lam/bin/mpirun N N $::lamrundir/pjrec-lam" if $MPI;
104 $phm2pj_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2pj-lam" if $MPI;
105 $phm2if_ver = "/opt/lam/bin/mpirun N N $::lamrundir/phm2if-lam" if $MPI;
106
107 my $gp_cmd = "$phm2if_ver $phantom_fname $Phantom_Nx $Phantom_Ny --phantom $Phantom_Name --nsample $Phantom_NSample";
108 my $pj_cmd = "$phm2pj_ver $pj_fname $PJ_NDet $PJ_NRot --phantom $Phantom_Name --nray $PJ_NRay --rotangle $PJ_RotAngle";
109 my $pj_if_cmd = "$::bindir/pj2if $pj_fname $pj_if_fname";
110 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";
111 my $diff_cmd = "$diff_ver $phantom_fname $ir_fname $diff_fname --comp";
112 my $compare_cmd = "$ifinfo_ver $phantom_fname $ir_fname";
113
114 my $window_options = "--auto $auto_window_img";
115 if ($Disp_Min ne 'auto') {
116     $window_options .= " --min $Disp_Min";
117 }
118 if ($Disp_Max ne 'auto') {
119     $window_options .= " --max $Disp_Max";
120 }
121
122 my $png1_cmd = "$::bindir/if2img $phantom_fname $phantom_png $window_options --stats --format png";
123 my $png2_cmd = "$::bindir/if2img $ir_fname $ir_png $window_options --stats --format png";
124 my $png3_cmd = "$::bindir/if2img $pj_if_fname $pj_png --auto $auto_window_pj --stats --format png";
125 my $png4_cmd = "$::bindir/if2img $diff_fname $diff_png --auto $auto_window_diff --stats --format png";
126
127 my $title = "CT Simulation Results";
128
129 my $out = head();
130 $out .= "<HTML> <HEAD><TITLE> $title </TITLE></HEAD>\n";
131 $out .= "<BODY  BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"  VLINK=\"#800020\" LINK=\"#0000FF\">\n";
132 $out .= "<H1>$title</H1><HR>\n";
133
134 if ($opt_d) {
135     $out .= "<H2>Commands</H2>\n";
136     $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" .
137             "$png3_cmd<br>\n$png4_cmd<br>\n";
138 }
139
140 my $cmdout = "";
141 if ($error ne "") {
142     $out .= "<FONT COLOR=\"#FF0000\">The following errors were present in your request.<br>\n";
143     $out .= "Please correct them and try submitting your request again.</FONT><br>\n";
144     $out .= "<i>$error</i>";
145 } else {
146   my $gp_out;
147   my $pj_out;
148   my $pj_if_out;
149   my $pjrec_out;
150   my $diff_out;
151   my $png_gp_out;
152   my $png_pjrec_out;
153   my $png_pj_out;
154   my $png_diff_out;
155   my $compare_out;
156   $gp_out = `$gp_cmd`;
157   if (-s $phantom_fname) {
158     $pj_out .= `$pj_cmd`;
159     $png_gp_out .= `$png1_cmd`;
160     if (-s $pj_fname) {
161       $pj_if_out .= `$pj_if_cmd`;
162       $png_pj_out .= `$png3_cmd`;
163       $pjrec_out .= `$pjrec_cmd`;
164       if (-s $ir_fname) {
165         $png_pjrec_out .= `$png2_cmd`;
166         $diff_out .= `$diff_cmd`;
167         $png_diff_out .= `$png4_cmd`;
168         $compare_out = `$compare_cmd`;
169       }
170     }
171   }
172
173   $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_diff_out";
174   if (open(LOGFILE,">> $logfile")) {
175     flock(LOGFILE,LOCK_EX);
176     seek(LOGFILE, 0, 2);
177     print LOGFILE "Job $tmpid\n";
178     print LOGFILE $cmdout;
179     print LOGFILE "----------------------------------------------------\n";
180     flock(LOGFILE,LOCK_UN);
181     close(LOGFILE);
182   }
183   $gmt_end = time();
184   $execution_time = $gmt_end - $gmt_start;
185   if ($opt_d) {
186     $out .= "<H3>Command Output</H3>$cmdout<HR>\n";
187   }
188   my $png_gp_out_html = $png_gp_out;
189   my $png_pjrec_out_html = $png_pjrec_out;
190   my $png_pj_out_html = $png_pj_out;
191   my $png_diff_out_html = $png_diff_out;
192   $png_gp_out_html =~ s/\n/<br>/gms;
193   $png_pjrec_out_html =~ s/\n/<br>/gms;
194   $png_pj_out_html =~ s/\n/<br>/gms;
195   $png_diff_out_html =~ s/\n/<br>/gms;
196   $out .= "<TABLE><TR><TD>Phantom Image</TD><TD>Reconstructed Image</TD></TR>\n";
197   $out .= "<TR><TD><IMG SRC=\"$phantom_png_url\"><br><FONT SIZE=1>$png_gp_out</FONT></TD>\n";
198   $out .= "<TD><IMG SRC=\"$ir_png_url\"><br><FONT SIZE=1>$png_pjrec_out</FONT></TD></TR>\n";
199   $out .= "<TR><TD>Projection Sinusoid</TD><TD>Phantom/Reconst Error</TD></TR>\n";
200   $out .= "<TR><TD><IMG SRC=\"$pj_png_url\"><br><FONT SIZE=1>$png_pj_out</FONT></TD>\n";
201   $out .= "<TD><IMG SRC=\"$diff_png_url\"><br><FONT SIZE=2>$diff_out</FONT><br><FONT SIZE=1>$png_diff_out</FONT></TD></TR>\n";
202   $out .= "</TABLE>";
203   $out .= "Execution time: $execution_time seconds\n";
204   $out .= "<p>\nStatistics<br>";
205   $out .= "$compare_out";
206 }
207
208 $out .= "<HR>\n";
209 $out .= "Return to <A HREF=\"/ctsim/index.html\">CT Simulation Designer</a>.<br>\n";
210 $out .= "Return to <A HREF=\"www.med-info.com\">Med-Info</a>.\n";
211 $out .= "</BODY> </HTML>";
212 $out .= "\n";
213
214 if (open(OUTFILE,"> $result_fname"))
215 {
216     flock(OUTFILE,LOCK_EX);
217     print OUTFILE $out;
218     flock(OUTFILE,LOCK_UN);
219     close OUTFILE;
220     print "Location: $result_url\n\n";
221 }
222 else
223 {
224     print "Content-type: text/plain\n\n";
225     print "The simulator was unable to create an result file.\n";
226 }
227 if (open(JOBFILES,"> $::jobdir/$tmpid"))
228 {
229     flock(JOBFILES,LOCK_EX);
230     print JOBFILES "gmt_start=$gmt_start\n";
231     print JOBFILES "gmt_end=$gmt_end\n";
232     print JOBFILES "execution_time=$execution_time\n";
233     print JOBFILES "Phantom_Name=$Phantom_Name\n";
234     print JOBFILES "Phantom_Nx=$Phantom_Nx\n";
235     print JOBFILES "Phantom_Ny=$Phantom_Nx\n";
236     print JOBFILES "Phantom_NSample=$Phantom_NSample\n";
237     print JOBFILES "PJ_NDet=$PJ_NDet\n";
238     print JOBFILES "PJ_NRot=$PJ_NRot\n";
239     print JOBFILES "PJ_NRay=$PJ_NRay\n";
240     print JOBFILES "PJ_RotAngle=$PJ_RotAngle\n";
241     print JOBFILES "IR_Nx=$IR_Nx\n";
242     print JOBFILES "IR_Ny=$IR_Ny\n";
243     print JOBFILES "IR_Interp=$IR_Interp\n";
244     print JOBFILES "IR_Filter=$IR_Filter\n";
245     print JOBFILES "IR_Filter_Param=$IR_Filter_Param\n";
246     print JOBFILES "IR_Backproj=$IR_Backproj\n";
247     print JOBFILES "Disp_Min=$Disp_Min\n";
248     print JOBFILES "Disp_Max=$Disp_Max\n";
249     print JOBFILES "MPI=$MPI\n";
250     print JOBFILES "Files=$result_fname,$phantom_fname,$pj_fname,$ir_fname,$phantom_png,$ir_png,$pj_if_fname,$pj_png\n" if ($error eq "");
251     printf JOBFILES "cmdout=$cmdout\n";
252     flock(JOBFILES,LOCK_UN);
253     close JOBFILES;
254 }
255
256
257 exit;
258
259
260 sub internal_error
261 {
262   my $out = head();
263   $out .= "<H1>Internal error</H1>
264   Please notify <A HREF=mailto:webmaster\@med-info.com>webmaster\@med-info.com</A>
265   </BODY>";
266   print $out;
267   exit;
268 }
269
270 sub head
271 {
272 #  "Content: text/html\n\n";
273 }
274
275
276 sub FilterMetaChars
277 {
278    my $var = pop(@_);
279    $var =~ /^(\w+)$/;  
280    $1;
281 }
282
283 sub FilterToNumber
284 {
285    my $var = pop(@_);
286    $var =~ /^(\-*\d*\.*\d*)$/;  
287    $1;
288 }