r3: Initial revision
[ctsim.git] / cgi-bin / ctsim.cgi
1 #!/usr/bin/perl
2 # Generated automatically from ctsim.cgi.in by configure.
3
4 use strict;
5 use CGI;
6 use File::Basename;
7 use Fcntl ':flock';
8
9 my $opt_d = 0;
10 my $bindir = "/opt/ctsim/bin";
11 $ENV{'PATH'} = $bindir;
12
13 my $fromhost = $ENV{'REMOTE_HOST'};
14
15 my $gmt_start = time();
16 my $gmt_end;
17 my $execution_time;
18
19 my %in;
20 CGI::ReadParse(\%in);
21
22 # Incoming variables 
23 #   Phantom_Name, Phantom_Nx, Phantom_Ny, Phantom_NSample
24 #   RS_NDet, RS_NRot, RS_NRay, RS_RotAngle,
25 #   IR_Nx, IR_Ny, IR_Filter, IR_Filter_Param
26
27 my $error = "";
28
29 my $Phantom_Name = FilterMetaChars($in{'Phantom_Name'});
30 my $Phantom_Nx = FilterToNumber($in{'Phantom_Nx'});
31 my $Phantom_Ny = FilterToNumber($in{'Phantom_Ny'});
32 my $Phantom_NSample = FilterToNumber($in{'Phantom_NSample'});
33 $error .= "Phantom name must not be blank<br>" if ($Phantom_Name eq "");
34 $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);
35 $error .= "Phantom NSample must be between 1 and 10<br>" if ($Phantom_NSample < 1 || $Phantom_NSample > 10);
36
37 my $RS_NDet = FilterToNumber($in{'RS_NDet'});
38 my $RS_NRot = FilterToNumber($in{'RS_NRot'});
39 my $RS_NRay = FilterToNumber($in{'RS_NRay'});
40 my $RS_RotAngle = FilterToNumber($in{'RS_RotAngle'});
41 $error .= "Raysum NDet must be between 5 and 1800<br>" if ($RS_NDet < 5 || $RS_NDet > 1800);
42 $error .= "Raysum NRot must be between 5 and 2048<br>" if ($RS_NRot < 5 || $RS_NRot > 2048);
43 $error .= "Raysum RotAngle must be between 0.1 and 2<br>" if ($RS_RotAngle < 0.1 || $RS_RotAngle > 2);
44
45 #my $IR_Nx = FilterToNumber($in{'IR_Nx'});
46 #my $IR_Ny = FilterToNumber($in{'IR_Ny'});
47 my $IR_Nx = $Phantom_Nx;
48 my $IR_Ny = $Phantom_Ny;
49 my $IR_Filter = FilterMetaChars($in{'IR_Filter'});
50 my $IR_Filter_Param = FilterToNumber($in{'IR_Filter_Param'});
51 my $IR_Interp = FilterMetaChars($in{'IR_Interp'});
52 my $IR_Backproj = FilterMetaChars($in{'IR_Backproj'});
53
54 my $Disp_Min = "auto";
55 my $Disp_Max = "auto";
56 $Disp_Min = FilterToNumber($in{'Disp_Min'}) if ($in{'Disp_Min'} ne "auto" && $in{'Disp_Min'} ne "");
57 $Disp_Max = FilterToNumber($in{'Disp_Max'}) if ($in{'Disp_Max'} ne "auto" && $in{'Disp_Max'} ne "");
58 if ($Disp_Min ne 'auto' && ! ($Disp_Min =~ /^[\d\.]+$/)) {
59     $error .= "Display Minimum must be 'auto' or numeric (received '$Disp_Min') <br>";
60 }
61 if ($Disp_Max ne 'auto' && ! ($Disp_Max =~ /^[\d\.]+$/)) {
62     $error .= "Display Maximum must be 'auto' or numeric (received '$Disp_Max') <br>";
63 }
64
65 my $MPI_Str = FilterMetaChars($in{'MPI'});
66 my $MPI = 0;
67 $MPI = 1 if ($MPI_Str eq "yes");
68
69 $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);
70 $error .= "IR Filter Parameter must be between 0 and 1<br>" if ($IR_Filter_Param < 0 || $IR_Filter_Param > 1);
71
72
73 my $jobdir = "/opt/apache/htdocs/ctsim/webdata";
74 my $tmpid = $$;
75 my $auto_window_img = "std0.1";
76 my $auto_window_diff = "std1";
77 my $auto_window_rs = "full";
78 my $datadir = "/opt/apache/htdocs/ctsim/webdata";
79 my $url_datadir = "/ctsim/webdata";
80 my $logfile = "$jobdir/ctsim.log";
81
82 my $result_fname = "$datadir/result-$tmpid.html";
83 my $phantom_fname = "$datadir/phantom-$tmpid.sdf";
84 my $rs_fname = "$datadir/rs-$tmpid.rs";
85 my $ir_fname = "$datadir/ir-$tmpid.sdf";
86 my $rs_sdf_fname = "$datadir/rs-$tmpid.sdf";
87 my $diff_fname = "$datadir/diff-$tmpid.sdf";
88 my $phantom_png = "$datadir/phantom-$tmpid.png";
89 my $ir_png = "$datadir/ir-$tmpid.png";
90 my $rs_png = "$datadir/rs-$tmpid.png";
91 my $diff_png = "$datadir/diff-$tmpid.png";
92
93 my $result_url = "$url_datadir/result-$tmpid.html";
94 my $phantom_png_url = "$url_datadir/phantom-$tmpid.png";
95 my $ir_png_url = "$url_datadir/ir-$tmpid.png";
96 my $rs_png_url = "$url_datadir/rs-$tmpid.png";
97 my $diff_png_url = "$url_datadir/diff-$tmpid.png";
98
99 my $ir_ver = "$bindir/ir";
100 my $rs_ver = "$bindir/rs";
101 my $gp_ver = "$bindir/gp";
102 my $diff_ver = "$bindir/sdf-2";
103 $ir_ver = "/opt/lam/bin/mpirun N N $bindir/ir-mpi" if $MPI;
104 $rs_ver = "/opt/lam/bin/mpirun N N $bindir/rs-mpi" if $MPI;
105 $gp_ver = "/opt/lam/bin/mpirun N N $bindir/gp-mpi" if $MPI;
106
107 my $gp_cmd = "$gp_ver $phantom_fname $Phantom_Nx $Phantom_Ny --phantom $Phantom_Name --nsample $Phantom_NSample";
108 my $rs_cmd = "$rs_ver $rs_fname $RS_NDet $RS_NRot --phantom $Phantom_Name --nray $RS_NRay --rotangle $RS_RotAngle";
109 my $rs_sdf_cmd = "$bindir/rs2sdf $rs_fname $rs_sdf_fname";
110 my $ir_cmd = "$ir_ver $rs_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
113 my $window_options = "--auto $auto_window_img";
114 if ($Disp_Min ne 'auto') {
115     $window_options .= " --min $Disp_Min";
116 }
117 if ($Disp_Max ne 'auto') {
118     $window_options .= " --max $Disp_Max";
119 }
120
121 my $png1_cmd = "$bindir/sdf2img $phantom_fname $phantom_png $window_options --stats --format png";
122 my $png2_cmd = "$bindir/sdf2img $ir_fname $ir_png $window_options --stats --format png";
123 my $png3_cmd = "$bindir/sdf2img $rs_sdf_fname $rs_png --auto $auto_window_rs --stats --format png";
124 my $png4_cmd = "$bindir/sdf2img $diff_fname $diff_png --auto $auto_window_diff --stats --format png";
125
126 my $title = "CT Simulation Results";
127
128 my $out = head();
129 $out .= "<HTML> <HEAD><TITLE> $title </TITLE></HEAD>\n";
130 $out .= "<BODY  BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"  VLINK=\"#800020\" LINK=\"#0000FF\">\n";
131 $out .= "<H1>$title</H1><HR>\n";
132
133 if ($opt_d) {
134     $out .= "<H2>Commands</H2>\n";
135     $out .= "$gp_cmd<br>\n$rs_cmd<br>\n$rs_sdf_cmd<br>\n$ir_cmd<br>\n$diff_cmd<br>\n$png1_cmd<br>\n$png2_cmd<br>\n" .
136             "$png3_cmd<br>\n$png4_cmd<br>\n";
137 }
138
139 my $cmdout = "";
140 if ($error ne "") {
141     $out .= "<FONT COLOR=\"#FF0000\">The following errors were present in your request.<br>\n";
142     $out .= "Please correct them and try submitting your request again.</FONT><br>\n";
143     $out .= "<i>$error</i>";
144 } else {
145     my $gp_out;
146     my $rs_out;
147     my $rs_sdf_out;
148     my $ir_out;
149     my $diff_out;
150     my $png_gp_out;
151     my $png_ir_out;
152     my $png_rs_out;
153     my $png_diff_out;
154     if (1) {
155         $gp_out = `$gp_cmd`;
156         $rs_out .= `$rs_cmd`;
157         $rs_sdf_out .= `$rs_sdf_cmd`;
158         $ir_out .= `$ir_cmd`;
159         $diff_out .= `$diff_cmd`;
160         $png_gp_out .= `$png1_cmd`;
161         $png_ir_out .= `$png2_cmd`;
162         $png_rs_out .= `$png3_cmd`;
163         $png_diff_out .= `$png4_cmd`;
164     } else {
165         $gp_out = `$gp_cmd 2>&1`;
166         $rs_out .= `$rs_cmd 2>&1`;
167         $rs_sdf_out .= `$rs_sdf_cmd 2>&1`;
168         $ir_out .= `$ir_cmd 2>&1`;
169         $diff_out .= `$diff_cmd 2>&1`;
170         $png_gp_out .= `$png1_cmd 2>&1`;
171         $png_ir_out .= `$png2_cmd 2>&1`;
172         $png_rs_out .= `$png3_cmd 2>&1`;
173         $png_diff_out .= `$png4_cmd 2>&1`;
174     }
175     $cmdout = "$gp_cmd\n $gp_out $rs_cmd\n $rs_out $rs_sdf_cmd\n $rs_sdf_out $ir_cmd\n $ir_out $diff_cmd\n $diff_out $png1_cmd\n $png_gp_out $png2_cmd\n $png_ir_out $png3_cmd\n $png_rs_out $png4_cmd\n $png_diff_out";
176     if (open(LOGFILE,">> $logfile")) {
177         flock(LOGFILE,LOCK_EX);
178         seek(LOGFILE, 0, 2);
179         print LOGFILE "Job $tmpid\n";
180         print LOGFILE $cmdout;
181         print LOGFILE "----------------------------------------------------\n";
182         flock(LOGFILE,LOCK_UN);
183         close(LOGFILE);
184     }
185     $gmt_end = time();
186     $execution_time = $gmt_end - $gmt_start;
187     if ($opt_d)
188     {
189         $out .= "<H3>Command Output</H3>$cmdout<HR>\n";
190     }
191     my $png_gp_out_html = $png_gp_out;
192     my $png_ir_out_html = $png_ir_out;
193     my $png_rs_out_html = $png_rs_out;
194     my $png_diff_out_html = $png_diff_out;
195     $png_gp_out_html =~ s/\n/<br>/gms;
196     $png_ir_out_html =~ s/\n/<br>/gms;
197     $png_rs_out_html =~ s/\n/<br>/gms;
198     $png_diff_out_html =~ s/\n/<br>/gms;
199     $out .= "<TABLE><TR><TD>Phantom Image</TD><TD>Reconstructed Image</TD></TR>\n";
200     $out .= "<TR><TD><IMG SRC=\"$phantom_png_url\"><br><FONT SIZE=1>$png_gp_out</FONT></TD>\n";
201     $out .= "<TD><IMG SRC=\"$ir_png_url\"><br><FONT SIZE=1>$png_ir_out</FONT></TD></TR>\n";
202     $out .= "<TR><TD>Raysum Sinusoid</TD><TD>Phantom/Reconst Error</TD></TR>\n";
203     $out .= "<TR><TD><IMG SRC=\"$rs_png_url\"><br><FONT SIZE=1>$png_rs_out</FONT></TD>\n";
204     $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";
205     $out .= "</TABLE>";
206     $out .= "Execution time: $execution_time seconds\n";
207 }
208
209 $out .= "<HR>\n";
210 $out .= "Return to <A HREF=\"/ctsim/index.html\">CT Simulation Designer</a>.<br>\n";
211 $out .= "Return to <A HREF=\"www.med-info.com\">Med-Info</a>.\n";
212 $out .= "</BODY> </HTML>";
213 $out .= "\n";
214
215 if (open(OUTFILE,"> $result_fname"))
216 {
217     flock(OUTFILE,LOCK_EX);
218     print OUTFILE $out;
219     flock(OUTFILE,LOCK_UN);
220     close OUTFILE;
221     print "Location: $result_url\n\n";
222 }
223 else
224 {
225     print "Content-type: text/plain\n\n";
226     print "The simulator was unable to create an result file.\n";
227 }
228 if (open(JOBFILES,"> $jobdir/$tmpid"))
229 {
230     flock(JOBFILES,LOCK_EX);
231     print JOBFILES "gmt_start=$gmt_start\n";
232     print JOBFILES "gmt_end=$gmt_end\n";
233     print JOBFILES "execution_time=$execution_time\n";
234     print JOBFILES "Phantom_Name=$Phantom_Name\n";
235     print JOBFILES "Phantom_Nx=$Phantom_Nx\n";
236     print JOBFILES "Phantom_Ny=$Phantom_Nx\n";
237     print JOBFILES "Phantom_NSample=$Phantom_NSample\n";
238     print JOBFILES "RS_NDet=$RS_NDet\n";
239     print JOBFILES "RS_NRot=$RS_NRot\n";
240     print JOBFILES "RS_NRay=$RS_NRay\n";
241     print JOBFILES "RS_RotAngle=$RS_RotAngle\n";
242     print JOBFILES "IR_Nx=$IR_Nx\n";
243     print JOBFILES "IR_Ny=$IR_Ny\n";
244     print JOBFILES "IR_Interp=$IR_Interp\n";
245     print JOBFILES "IR_Filter=$IR_Filter\n";
246     print JOBFILES "IR_Filter_Param=$IR_Filter_Param\n";
247     print JOBFILES "IR_Backproj=$IR_Backproj\n";
248     print JOBFILES "Disp_Min=$Disp_Min\n";
249     print JOBFILES "Disp_Max=$Disp_Max\n";
250     print JOBFILES "MPI=$MPI\n";
251     print JOBFILES "Files=$result_fname,$phantom_fname,$rs_fname,$ir_fname,$phantom_png,$ir_png,$rs_sdf_fname,$rs_png\n" if ($error eq "");
252     printf JOBFILES "cmdout=$cmdout\n";
253     flock(JOBFILES,LOCK_UN);
254     close JOBFILES;
255 }
256
257
258 exit;
259
260
261 sub internal_error
262 {
263   my $out = head();
264   $out .= "<H1>Internal error</H1>
265   Please notify <A HREF=mailto:webmaster\@med-info.com>webmaster\@med-info.com</A>
266   </BODY>";
267   print $out;
268   exit;
269 }
270
271 sub head
272 {
273 #  "Content: text/html\n\n";
274 }
275
276
277 sub FilterMetaChars
278 {
279    my $var = pop(@_);
280    $var =~ /^(\w+)$/;  
281    $1;
282 }
283
284 sub FilterToNumber
285 {
286    my $var = pop(@_);
287    $var =~ /^(\d*\.*\d*)$/;  
288    $1;
289 }