3 # piv2vec - Inserts a third and fifth column of a PIV data stream in order
4 # to prepare a data file for plotmtv vector representation.
6 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
7 # Gerber van der Graaf <gerber_graaf@users.sourceforge.net
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software Foundation,
21 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #------------------------------------------------------------------
24 $VERSION = q
$Id: piv2vec
.pl
,v
1.8 2007-01-29 14:26:54 gerber Exp
$;
25 $HELP = "Adds header to piv data for a vector plot with Plotmtv";
26 $USAGE = "gpiv_piv2vec [-al length][-au \"units\"[-flipx][-flipy][-g][-h]
27 [-p][-s scale][-tl label][-v][-xl label][-yl label] ][filename]
31 -al: plots a vector annotation of 'length'
32 -au: plots a vector annotation of 'units'
33 -flipx: flips the plot in x-direction; x data from high to low
34 -flipy: flips the plot in y-direction; y data from high to low
37 -nd: combined with -g; adds option -nodate (only ps output doesn't
40 -s: vector scale (default: 1 px)
41 -tl: defines top label (title) (with -f default: \"file_name\")
43 -xl: defines x-label (default: \"x (pixels)\")
44 -yl: defines y-label (default: \"y (pixels)\")
45 filename: input PIV data file. Substitutes stdin and stdout
49 #----------------- Command line arguments handling ----------
64 GetOptions
("g|graphic", "h|help", "k|keep", "v|version",
65 "s|scale=f" => \
$vscale,
66 'tl|toplabel=s' => \
$top_label,
67 'xl|xlabel=s' => \
$x_label,
68 'yl|ylabel=s' => \
$y_label,
70 'al|annotation_length=f' => \
$length_an,
71 'au|annotation_unit=s' => \
$unit_an,
89 $filename = shift (@ARGV);
90 } elsif ($#ARGV == -1) {
91 printf ("\nUsage: $USAGE\n");
95 if ($opt_g && !$filename_logic) {
96 print ("graphic output needs filename");
102 #--------------- extracts file basename from a full filename
103 my ($file_base_name, $file_extension);
104 @file_fields = split(/\./,$_[0]);
106 $file_base_name = join(".", @file_fields);
107 return $file_base_name;
110 $file_basename = FileBase
($filename);
113 #------------------------------- default parameters
114 if (!$unit_an) {$unit_an = "px"}
115 if (!$vscale) {$vscale = 1.0}
117 if ($file_basename && !$top_label) {$top_label = $file_basename}
118 if (!$x_label) {$x_label = "x (pixels)"}
119 if (!$y_label) {$x_label = "y (pixels)"}
122 #--------------- Initializing variables
132 #-------------------- Defines location and value of third component
137 if ($filename_logic == 0) {
138 } elsif ($filename_logic == 1) {
142 #--------------- Inserts data with third component (necesary for Plotmtv)
144 if ($filename_logic == 0) {
145 #------------------------------- Inserts header
146 print("\$ DATA=VECTOR\n");
147 print("\% vscale=$vscale\n");
148 print("\% equalscale\n");
150 print("\% toplabel = \"$top_label\"\n");
152 print("\% toplabel = \" \"\n");
154 if ($x_label) {print("\% xlabel = \"$x_label\"\n");}
155 if ($y_label) {print("\% ylabel = \"$y_label\"\n");}
156 if ($opt_flipx) {print("\% xflip\n");}
157 if ($opt_flipy) {print("\% yflip\n");}
161 if ($_ ne "" && (!/^(\#|;)/)) { #skip blank lines, # or ; signs at first col
163 s/,/./g; #substitutes eventually komma's by dot
164 #(for example at LaVision's code)
165 s/^ *//; #removes eventually leading space
167 @words = split(/\s+/,$_); #splitting line $_ up in words
168 #same as @words = split
169 $px[$line]=$words[0];
170 $py[$line]=$words[1];
171 $dx[$line]=$words[2];
172 $dy[$line]=$words[3];
175 #------------------------------- Testing if they are numbers
177 if (($px[$line] =~ /\d/) && ($py[$line] =~ /\d/) && ($dx[$line] =~ /\d/) &&
178 ($dy[$line] =~ /\d/) && ($snr[$line] =~ /\d/)) {
179 $snr[$line]=$words[4];
181 } elsif ($#words == 6 ) {
182 if (($px[$line] =~ /\d/) && ($py[$line] =~ /\d/) && ($dx[$line] =~ /\d/) &&
183 ($dy[$line] =~ /\d/) && ($snr[$line] =~ /\d/) && ($peak[$line] =~ /\d/) ) {
184 $snr[$line]=$words[4];
185 $peak[$line]=$words[5];
188 if (($px[$line] =~ /\d/) && ($py[$line] =~ /\d/) && ($dx[$line] =~ /\d/) &&
189 ($dy[$line] =~ /\d/)) {
192 #----------------------- If vector annotation; calculate min and max values
194 if ($px[$line] > $x_max) {$x_max = $px[$line];}
195 if ($px[$line] < $x_min) {$x_min = $px[$line];}
196 if ($py[$line] > $y_max) {$y_max = $py[$line];}
197 if ($py[$line] < $y_min) {$y_min = $py[$line];}
205 #----------------------- If vector annotation; print to stdout
207 $x1_an = $x_min+($x_max-$x_min)/2.;
208 $x2_an = $x1_an + $length_an*$vscale;
210 $y1_an = $y_min-$y_max*0.1;
211 $y2_an = $y_min-$y_max*0.1;
213 $y1_an = $y_max+$y_max*0.1;
214 $y2_an = $y_max+$y_max*0.1;
216 printf("\n@ arrow x1=%f y1=%f z1=0.0 x2=%f y2=%f z2=0.0 \\
217 linetype=1 linecolor=0 clip=false linelabel=\"%6.2f %s\"\n\n",
218 $x1_an, $y1_an, $x2_an, $y2_an, $length_an, $unit_an);
221 #----------------------- Print data
222 for ($line=0; $line < $nlines; $line++) {
224 print("$px[$line] $py[$line] $insert_3_loc $dx[$line] $dy[$line] $insert_3_val $snr[$line]\n");
225 } elsif ($#words == 6 ) {
226 print("$px[$line] $py[$line] $insert_3_loc $dx[$line] $dy[$line] $insert_3_val $snr[$line] $peak[$line]\n");
228 print("$px[$line] $py[$line] $insert_3_loc $dx[$line] $dy[$line] $insert_3_val\n");
234 } elsif ($filename_logic == 1) {
235 #------------------------------- filename handling
236 $file_name_piv=$file_basename.".piv";
237 $file_name_vec=$file_basename.".vec";
238 $infile=$file_name_piv;
239 $outfile=$file_name_vec;
240 # print "\ninfile=$infile outfile=$outfile";
241 open (IN
,"$infile") || die 'PIV2VEC error: can\'t open $infile';
242 open (OUT
,">$outfile") || die 'PIV2VEC error: can\'t open $outfile';
244 #------------------------------- Inserts header
245 print(OUT
"\$ DATA=VECTOR\n");
246 print(OUT
"\% vscale=$vscale\n");
247 print(OUT
"\% equalscale\n");
249 print(OUT
"\% toplabel = \"$top_label\"\n");
251 print(OUT
"\% toplabel = \" \"\n");
253 if ($x_label) {print(OUT
"\% xlabel = \"$x_label\"\n");}
254 if ($y_label) {print(OUT
"\% ylabel = \"$y_label\"\n");}
255 if ($opt_flipx) {print(OUT
"\% xflip\n");}
256 if ($opt_flipy) {print(OUT
"\% yflip\n");}
261 if ($_ ne "" && (!/^(\#|;)/)) { #skip blank lines, # or ; signs at first col
263 s/,/./g; #substitutes eventually komma's by dot
264 #(for example at LaVision's code)
265 s/^ *//; #removes eventually leading space
266 @words = split(/\s+/,$_); #splitting line $_ up in words
267 #same as @words = split
268 $px[$line] = $words[0];
269 $py[$line] = $words[1];
270 $dx[$line] = $words[2];
271 $dy[$line] = $words[3];
272 $snr[$line] = $words[4];
273 $peak[$line] = $words[5];
274 ###printf("\n** AA:: peak = $peak[$line] ** \n");
276 #------------------------------- Testing if they are numbers
278 if (($px[$line] =~ /\d/) && ($py[$line] =~ /\d/) && ($dx[$line] =~ /\d/) &&
279 ($dy[$line] =~ /\d/) && ($snr[$line] =~ /\d/)) {
280 # $snr[$line]=$words[4];
282 } elsif ($#words == 6 ) {
283 if (($px[$line] =~ /\d/) && ($py[$line] =~ /\d/) && ($dx[$line] =~ /\d/) &&
284 ($dy[$line] =~ /\d/) && ($snr[$line] =~ /\d/) && ($peak[$line] =~ /\d/) ) {
285 # $snr[$line]=$words[4];
286 # $peak[$line]=$words[5];
289 if (($px[$line] =~ /\d/) && ($py[$line] =~ /\d/) && ($dx[$line] =~ /\d/) &&
290 ($dy[$line] =~ /\d/)) {
293 #----------------------- If vector annotation; calculate min and max values
294 #----------------------- of location for placing annotation
296 if ($px[$line] > $x_max) {$x_max = $px[$line];}
297 if ($px[$line] < $x_min) {$x_min = $px[$line];}
298 if ($py[$line] > $y_max) {$y_max = $py[$line];}
299 if ($py[$line] < $y_min) {$y_min = $py[$line];}
308 #----------------------- If vector annotation; print to file
310 $x1_an = $x_min+($x_max-$x_min)/2.;
311 $x2_an = $x1_an + $length_an*$vscale;
313 $y1_an = $y_min-$y_max*0.1;
314 $y2_an = $y_min-$y_max*0.1;
316 $y1_an = $y_max+$y_max*0.1;
317 $y2_an = $y_max+$y_max*0.1;
319 print("\nx1_an=$x1_an x2_an=$x2_an");
320 printf(OUT
"\n@ arrow x1=%f y1=%f z1=0.0 x2=%f y2=%f z2=0.0 \\
321 linetype=1 linecolor=0 clip=false linelabel=\"%6.2f %s\"\n\n",
322 $x1_an, $y1_an, $x2_an, $y2_an, $length_an, $unit_an);
325 #----------------------- Print data
326 for ($line=0; $line < $nlines; $line++) {
328 ###printf("\n** BB:: NO words == $#words snr = $snr[$line] peak = $peak[$line]**\n");
329 if ($peak[$line] != -1) { # $peak[$line] != 0 &&
330 print(OUT
"$px[$line] $py[$line] $insert_3_loc $dx[$line] $dy[$line] $insert_3_val $snr[$line]\n");
332 } elsif ($#words == 6 ) {
333 print(OUT
"$px[$line] $py[$line] $insert_3_loc $dx[$line] $dy[$line] $insert_3_val $snr[$line] $peak[$line]\n");
335 print(OUT
"$px[$line] $py[$line] $insert_3_loc $dx[$line] $dy[$line] $insert_3_val\n");
338 close (IN
) || die 'PIV2VEC error: can\'t close $infile';
339 close (OUT
) || die 'PIV2VEC error: can\'t close $outfile';
342 #------------------------------- Launches Plotmtv and cleans
345 @args=("plotmtv", "-nodate", $file_name_vec);
347 @args=("plotmtv", $file_name_vec);
349 system (@args) == 0 || die "PIV2VEC error: system @args failed: $?";
351 @args=("rm", $file_name_vec);
352 system (@args) == 0 || die "PIV2VEC error: system @args failed: $?";