editPD0
changeset 14 8c79b38a7086
child 28 7c7da52363c2
equal deleted inserted replaced
13:b176da8559b3 14:8c79b38a7086
       
     1 #!/usr/bin/perl
       
     2 #======================================================================
       
     3 #                    E D I T P D 0 
       
     4 #                    doc: Mon Nov 25 20:24:31 2013
       
     5 #                    dlm: Tue Nov 26 10:31:05 2013
       
     6 #                    (c) 2013 A.M. Thurnherr
       
     7 #                    uE-Info: 70 1 NIL 0 0 72 2 2 4 NIL ofnI
       
     8 #======================================================================
       
     9 
       
    10 # edit RDI PD0 file, e.g. to replace pitch/roll/heading with external values
       
    11 
       
    12 # HISTORY:
       
    13 #	Nov 25, 2013: - created
       
    14 
       
    15 $0 =~ m{(.*)/[^/]+}; 
       
    16 require "$1/RDI_PD0_IO.pl";
       
    17 require "getopts.pl";
       
    18 
       
    19 $USAGE = "$0 @ARGV";
       
    20 die("Usage: $0 " .
       
    21 	'-e) <edit-file> ' .
       
    22 	"<input file> <output file>\n")
       
    23 		unless (&Getopts('e:') && @ARGV == 2);
       
    24 
       
    25 die("$0: -e <edit-file> is required\n")
       
    26 	unless (-r $opt_e);
       
    27 
       
    28 print(STDERR "Reading $ARGV[0]...");				# read data
       
    29 readData($ARGV[0],\%dta);
       
    30 print(STDERR "done\n");
       
    31 
       
    32 #----------------------------------------------------------------------
       
    33 
       
    34 print(STDERR "Editing according to <$opt_e>...");
       
    35 
       
    36 #--------------------------------------------------
       
    37 # INTERFACE
       
    38 #	- example <edit_file> entry:
       
    39 #		162     p(3), r(4), h(3.14)
       
    40 #--------------------------------------------------
       
    41 
       
    42 sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; }
       
    43 sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; }
       
    44 sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; }
       
    45 
       
    46 #--------------------------------------------------
       
    47 # Main Routine
       
    48 #--------------------------------------------------
       
    49 
       
    50 open(EF,$opt_e) || die("$opt_e: $!\n");
       
    51 while (<EF>) {
       
    52 	s/\#.*//;
       
    53 	next if m/^\s+$/;
       
    54 	my($ens,$expr) = m/^\s*(\d+)\s+(.*)$/;
       
    55 
       
    56 	my($id) = ($expr =~ m/^([A-Z]+)\s/);							# e.g. PITCH, ROLL, HEADING
       
    57 	$expr = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
       
    58 		if defined($id);
       
    59 		
       
    60 	push(@EE,$ens);
       
    61 	push(@EX,$expr);
       
    62 }
       
    63 close(EF);
       
    64 
       
    65 for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {			# local() needed for p(), r(), h()
       
    66 	if ($EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {				# match => edit
       
    67 #		print(STDERR "\n\@ens=$EE[$eei]: $EX[$eei]");
       
    68 #		print(STDERR "\n\tp($dta{ENSEMBLE}[$e]->{PITCH}), r($dta{ENSEMBLE}[$e]->{ROLL}), h($dta{ENSEMBLE}[$e]->{HEADING})");
       
    69 		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
       
    70 #		print(STDERR "\n\tp($dta{ENSEMBLE}[$e]->{PITCH}), r($dta{ENSEMBLE}[$e]->{ROLL}), h($dta{ENSEMBLE}[$e]->{HEADING})...");
       
    71 	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {			# next edit later in file => skip
       
    72 		next;
       
    73 	} else {														# need next edit
       
    74 		$eei++;
       
    75 		last if ($eei >= @EE);
       
    76 		redo;
       
    77 	}
       
    78 }
       
    79 
       
    80 print(STDERR "done\n");
       
    81 
       
    82 #----------------------------------------------------------------------
       
    83 
       
    84 print(STDERR "Writing $ARGV[1]...");				# write data
       
    85 writeData($ARGV[1],\%dta);
       
    86 print(STDERR "done\n");
       
    87 
       
    88 exit(0);