libgpiv so version 4: GpivFt. Show git hash (if available) in output and
[gpivtools.git] / src / misc / piv2vec.pl
blobbb6855a8254ce91b4052cbd635a0bb5421e80f46
1 #!/usr/bin/perl -w
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)
12 # any later version.
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]
28 < input > output
30 keys:
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
35 -g: launches plotmtv
36 -h: on-line help
37 -nd: combined with -g; adds option -nodate (only ps output doesn't
38 contain date!)
39 -k: keep plot-file
40 -s: vector scale (default: 1 px)
41 -tl: defines top label (title) (with -f default: \"file_name\")
42 -v: prints version
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 ----------
50 #$opt_fn = 0;
51 # $opt_an = 0;
52 $opt_g = 0;
53 $opt_h = 0;
54 $opt_k = 0;
55 $opt_v = 0;
56 # $opt_s = 0;
57 $filename_logic = 0;
58 # $flipx = 0;
59 # $flipy = 0;
60 $opt_nd = 0;
62 use Getopt::Long;
63 #$result =
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,
69 'nd|no_date',
70 'al|annotation_length=f' => \$length_an,
71 'au|annotation_unit=s' => \$unit_an,
72 "flipx", "flipy",
76 if ($opt_h) {
77 print ("$HELP\n");
78 print ("$USAGE\n");
79 exit;
81 if ($opt_v) {
82 print ("$VERSION\n");
83 exit;
87 if ($#ARGV == 0) {
88 $filename_logic = 1;
89 $filename = shift (@ARGV);
90 } elsif ($#ARGV == -1) {
91 printf ("\nUsage: $USAGE\n");
92 exit;
95 if ($opt_g && !$filename_logic) {
96 print ("graphic output needs filename");
97 exit;
101 sub FileBase {
102 #--------------- extracts file basename from a full filename
103 my ($file_base_name, $file_extension);
104 @file_fields = split(/\./,$_[0]);
105 pop @file_fields;
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
124 $line = 0;
125 $x_max = -10000;
126 $x_min = 10000;
127 $y_max = 0;
128 $y_max = -1000;
129 $y_min = 1000;
132 #-------------------- Defines location and value of third component
133 $insert_3_loc = 0.0;
134 $insert_3_val = 0.0;
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");
149 if ($top_label) {
150 print("\% toplabel = \"$top_label\"\n");
151 } else {
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");}
158 while (<STDIN>) {
159 chomp;
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
176 if ($#words == 5 ) {
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];
187 } else {
188 if (($px[$line] =~ /\d/) && ($py[$line] =~ /\d/) && ($dx[$line] =~ /\d/) &&
189 ($dy[$line] =~ /\d/)) {
192 #----------------------- If vector annotation; calculate min and max values
193 if ($length_an) {
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];}
200 $line++;
204 $nlines = $line;
205 #----------------------- If vector annotation; print to stdout
206 if ($length_an) {
207 $x1_an = $x_min+($x_max-$x_min)/2.;
208 $x2_an = $x1_an + $length_an*$vscale;
209 if ($opt_flipy){
210 $y1_an = $y_min-$y_max*0.1;
211 $y2_an = $y_min-$y_max*0.1;
212 } else {
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++) {
223 if ($#words == 5 ) {
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");
227 } else {
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");
248 if ($top_label) {
249 print(OUT "\% toplabel = \"$top_label\"\n");
250 } else {
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");}
259 while (<IN>) {
260 chomp;
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
277 if ($#words == 5 ) {
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];
288 } else {
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
295 if ($length_an) {
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];}
302 $line++;
307 $nlines = $line;
308 #----------------------- If vector annotation; print to file
309 if ($length_an) {
310 $x1_an = $x_min+($x_max-$x_min)/2.;
311 $x2_an = $x1_an + $length_an*$vscale;
312 if ($opt_flipy){
313 $y1_an = $y_min-$y_max*0.1;
314 $y2_an = $y_min-$y_max*0.1;
315 } else {
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++) {
327 if ($#words == 5 ) {
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");
334 } else {
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
343 if ($opt_g) {
344 if ($opt_nd) {
345 @args=("plotmtv", "-nodate", $file_name_vec);
346 } else {
347 @args=("plotmtv", $file_name_vec);
349 system (@args) == 0 || die "PIV2VEC error: system @args failed: $?";
350 if (!$opt_k) {
351 @args=("rm", $file_name_vec);
352 system (@args) == 0 || die "PIV2VEC error: system @args failed: $?";
355 print ("\n");