patchPD0
changeset 43 b63fa355644c
parent 42 80d039881d2c
child 50 6bfec705d25e
equal deleted inserted replaced
42:80d039881d2c 43:b63fa355644c
     1 #!/usr/bin/perl
     1 #!/usr/bin/perl
     2 #======================================================================
     2 #======================================================================
     3 #                    P A T C H P D 0 
     3 #                    P A T C H P D 0 
     4 #                    doc: Tue Aug 23 20:00:15 2016
     4 #                    doc: Tue Aug 23 20:00:15 2016
     5 #                    dlm: Sat Dec 23 16:12:01 2017
     5 #                    dlm: Wed Jun 13 20:35:05 2018
     6 #                    (c) 2010 A.M. Thurnherr
     6 #                    (c) 2010 A.M. Thurnherr
     7 #                    uE-Info: 51 24 NIL 0 0 72 0 2 4 NIL ofnI
     7 #                    uE-Info: 184 88 NIL 0 0 72 2 2 4 NIL ofnI
     8 #======================================================================
     8 #======================================================================
     9 
     9 
    10 $antsSummary = 'patch TRDI PD0 file with external attitude data';
    10 $antsSummary = 'patch TRDI PD0 file with external attitude data';
    11 
    11 
    12 # History:
    12 # History:
    15 #	Nov 20, 2017: - major code cleanup
    15 #	Nov 20, 2017: - major code cleanup
    16 #				  - added -d) to keep original data source id
    16 #				  - added -d) to keep original data source id
    17 #	Dec  9, 2017: - added $antsSuppressCommonOptions = 1;
    17 #	Dec  9, 2017: - added $antsSuppressCommonOptions = 1;
    18 #	Dec 23, 2017: - added support for -c
    18 #	Dec 23, 2017: - added support for -c
    19 #				  - BUG: not backward compatible with old IMP files any more
    19 #				  - BUG: not backward compatible with old IMP files any more
       
    20 #	Jun 13, 2017: - added pitch and roll to -o
       
    21 #				  - BUG: ??? does -o handle pitch and roll ANOMALIES correctly?
    20 
    22 
    21 # PATCH-FILE REQUIREMENTS (ANTS format)
    23 # PATCH-FILE REQUIREMENTS (ANTS format)
    22 #	- %LADCP_pitch.mu %LADCP_roll.mu		mean LADCP pitch and roll
    24 #	- %LADCP_pitch.mu %LADCP_roll.mu		mean LADCP pitch and roll
    23 #	- %IMU_hdg_offset						heading offset of external IMU
    25 #	- %IMU_hdg_offset						heading offset of external IMU
    24 #	- LADCP_ens								ADCP ensemble number
    26 #	- LADCP_ens								ADCP ensemble number
    58 
    60 
    59 $antsParseHeader = 0;
    61 $antsParseHeader = 0;
    60 $antsSuppressCommonOptions = 1;
    62 $antsSuppressCommonOptions = 1;
    61 &antsUsage('cdhko:pr',2,
    63 &antsUsage('cdhko:pr',2,
    62 	'[patch -p)itch] [-r)oll] [-h)eading] (none patches all)',
    64 	'[patch -p)itch] [-r)oll] [-h)eading] (none patches all)',
    63 	'[patch -c)lock with pre-Y2K RTC calues]',
    65 	'[patch -c)lock with pre-Y2K RTC values]',
    64 	'[-o) <heading-offset>] [-k)eep velocities of unpatched ensembles]',
    66 	'[-o) <[pitch,roll,]heading-offset>] [-k)eep velocities of unpatched ensembles]',
    65 	'[keep original -d)ata-source id]',
    67 	'[keep original -d)ata-source id]',
    66 	'<original PD0 file> <patched PD0 file> [external attitude file]');
    68 	'<original PD0 file> <patched PD0 file> [external attitude file]');
    67 
    69 
    68 $opt_p = $opt_r = $opt_h = 1
    70 $opt_p = $opt_r = $opt_h = 1
    69 	unless ($opt_p || $opt_r || $opt_h);
    71 	unless ($opt_p || $opt_r || $opt_h);
    75 
    77 
    76 #----------------------------------------------------------------------
    78 #----------------------------------------------------------------------
    77 # Step 1: Read LADCP Data
    79 # Step 1: Read LADCP Data
    78 #----------------------------------------------------------------------
    80 #----------------------------------------------------------------------
    79 
    81 
    80 readData($LADCP_file,\%LADCP);
    82 readData($LADCP_file,\%LADCP);												# TRDI PD0 file
    81 
    83 
    82 #----------------------------------------------------------------------
    84 #----------------------------------------------------------------------
    83 # Step 2: Process External Attidue Input to Patch PD0 file
    85 # Step 2: Process External Attidue Input to Patch PD0 file
    84 #----------------------------------------------------------------------
    86 #----------------------------------------------------------------------
    85 
    87 
    86 my($pr_missing,$hdg_missing) = (0,0);
    88 my($pr_missing,$hdg_missing) = (0,0);
    87 
    89 
    88 &antsIn();
    90 &antsIn();																	# load first IMP record
    89 
    91 
    90 my($ensF) 	= &fnr('LADCP_ens');
    92 my($ensF) 	= &fnr('LADCP_ens');
    91 my($pitchF) = &fnr('pitch');
    93 my($pitchF) = &fnr('pitch');
    92 my($rollF)	= &fnr('roll');
    94 my($rollF)	= &fnr('roll');
    93 my($hdgF)	= &fnr('hdg');
    95 my($hdgF)	= &fnr('hdg');
    94 my($LADCP_pitch_mean)	= &antsRequireParam('LADCP_pitch.mu');
    96 my($LADCP_pitch_mean)	= &antsRequireParam('LADCP_pitch.mu');
    95 my($LADCP_roll_mean)	= &antsRequireParam('LADCP_roll.mu');
    97 my($LADCP_roll_mean)	= &antsRequireParam('LADCP_roll.mu');
    96 
    98 
    97 my($rho,$crho,$srho);
    99 
       
   100 my($pofs,$rofs) = (0,0);													# apply externally supplied offset(s)
       
   101 my($rho,$crho,$srho);												
    98 if (defined($opt_o)) {
   102 if (defined($opt_o)) {
       
   103 	my($pofs,$rofs,$hofs) = split(/,/,$opt_o);
       
   104 
       
   105 	if (defined($pofs)) {													# pitch and roll offsets supplied
       
   106 		croak("$0: cannot decode -o $opt_o\n")
       
   107 			unless numbersp($pofs,$rofs);
       
   108 	} else {																# no pitch and roll, only heading
       
   109 		$hofs = $pofs; 
       
   110 		$pofs = undef;
       
   111     }
       
   112 	croak("$0: cannot decode -o $opt_o\n")									
       
   113 		unless numberp($hofs);
       
   114 																			# set up heading correction
    99 	&antsAddParams('IMU_hdg_offset',$P{IMP_hdg_offset})						# backward compatibility
   115 	&antsAddParams('IMU_hdg_offset',$P{IMP_hdg_offset})						# backward compatibility
   100 		if defined($P{IMP_hdg_offset});
   116 		if defined($P{IMP_hdg_offset});
   101 	$rho  = $opt_o - &antsRequireParam('IMU_hdg_offset');
   117 	$rho  = $hofs - &antsRequireParam('IMU_hdg_offset');					# calculate correction relative to already applied one 
   102 	$crho = cos(rad($rho));
   118 	$crho = cos(rad($rho));
   103 	$srho = sin(rad($rho));
   119 	$srho = sin(rad($rho));
       
   120 
       
   121 	if (defined($pofs)) {													# rotate IMP pitch and roll offsets into new LADCP frame
       
   122 		my($IMP_pitch_mean) =  &antsRequireParam('IMP_pitch.mu') * $crho
       
   123 									+ &antsRequireParam('IMP_roll.mu') * $srho;
       
   124 		my($IMP_roll_mean)  = -&antsRequireParam('IMP_pitch.mu') * $srho
       
   125 									+ &antsRequireParam('IMP_roll.mu') * $crho;
       
   126 		$LADCP_pitch_mean = $IMP_pitch_mean - $pofs;						# apply externally supplied offsets
       
   127 		$LADCP_roll_mean  = $IMP_roll_mean  - $rofs;
       
   128 	}
   104 }
   129 }
   105 
   130 
   106 do {
   131 do {
   107 	my($ens) = $P{RECNO};
   132 	my($ens) = $P{RECNO};
   108 	die("assertion failed [$ants_[0][$ensF] != $LADCP{ENSEMBLE}[$ens]->{NUMBER} --- 1-$LADCP{ENSEMBLE}[0]->{NUMBER} + $P{RECNO} + $d]")
   133 	die("assertion failed [$ants_[0][$ensF] != $LADCP{ENSEMBLE}[$ens]->{NUMBER} --- 1-$LADCP{ENSEMBLE}[0]->{NUMBER} + $P{RECNO} + $d]")
   109 		unless ($ants_[0][$ensF] == $LADCP{ENSEMBLE}[$ens]->{NUMBER});
   134 		unless ($ants_[0][$ensF] == $LADCP{ENSEMBLE}[$ens]->{NUMBER});
   110 	$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} = 0xA0;
   135 	$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} = 0xA0;
   111 	
   136 	
   112 	if (numbersp($ants_[0][$pitchF],$ants_[0][$rollF])) {
   137 	if (numbersp($ants_[0][$pitchF],$ants_[0][$rollF])) {					# valid IMP data -> patch LADCP ensemble
   113 		if (defined($opt_o)) {
   138 		if (defined($opt_o)) {												# -o set: rotate pitch and roll into correct coordinates
   114 			my($rot_p) = ($ants_[$r][$pitchF]  * $crho +
   139 			my($rot_p) = ($ants_[$r][$pitchF]  * $crho +
   115 						  $ants_[$r][$rollF]   * $srho);
   140 						  $ants_[$r][$rollF]   * $srho);
   116 			my($rot_r) = (-$ants_[$r][$pitchF] * $srho +
   141 			my($rot_r) = (-$ants_[$r][$pitchF] * $srho +
   117 						   $ants_[$r][$rollF]  * $crho);
   142 						   $ants_[$r][$rollF]  * $crho);
   118 			$ants_[$r][$pitchF] = $rot_p;
   143 			$ants_[$r][$pitchF] = $rot_p;
   119 			$ants_[$r][$rollF]	= $rot_r;
   144 			$ants_[$r][$rollF]	= $rot_r;
   120         } 
   145         } 
   121 		if ($opt_p) {
   146 		if ($opt_p) {														# patch pitch
   122 			$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= ($opt_p<<2);
   147 			$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= ($opt_p<<2);
   123 			$LADCP{ENSEMBLE}[$ens]->{PITCH} = RDI_pitch($LADCP_pitch_mean + $ants_[0][$pitchF],
   148 			$LADCP{ENSEMBLE}[$ens]->{PITCH} = RDI_pitch($LADCP_pitch_mean + $ants_[0][$pitchF],
   124 														$LADCP_roll_mean  + $ants_[0][$rollF]);
   149 														$LADCP_roll_mean  + $ants_[0][$rollF]);
   125 		}
   150 		}
   126 		if ($opt_r) {
   151 		if ($opt_r) {														# patch roll
   127 			$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= ($opt_r<<1);
   152 			$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= ($opt_r<<1);
   128 			$LADCP{ENSEMBLE}[$ens]->{ROLL} = $LADCP_roll_mean + $ants_[0][$rollF];
   153 			$LADCP{ENSEMBLE}[$ens]->{ROLL} = $LADCP_roll_mean + $ants_[0][$rollF];
   129 		}
   154 		}
   130     } else {
   155     } else {																# no valid IMP pitch and roll => invalidate LADCP data
   131     	$pr_missing++;
   156     	$pr_missing++;
   132 		unless ($opt_k)  {
   157 		unless ($opt_k)  {
   133 	    	clearEns(\%LADCP,$ens);
   158 	    	clearEns(\%LADCP,$ens);
   134 	    	$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID}= 0xA0;
   159 	    	$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID}= 0xA0;
   135 	    }
   160 	    }
   136     }
   161     }
   137 	    
   162 	    
   138     if (numberp($ants_[0][$hdgF])) {
   163     if (numberp($ants_[0][$hdgF])) {										# valid IMP heading
   139     	$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= $opt_h;
   164     	$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= $opt_h;
   140    		if (defined($opt_o)) {
   165    		if (defined($opt_o)) {												# apply offset on -o; otherwise, data are correctly rotated
   141 	    	$ants_[0][$hdgF] -= $rho;
   166 	    	$ants_[0][$hdgF] -= $rho;
   142 	    	$ants_[0][$hdgF] += 360 if ($ants_[0][$hdgF] < 0);
   167 	    	$ants_[0][$hdgF] += 360 if ($ants_[0][$hdgF] < 0);
   143 	    }
   168 	    }
   144 		$LADCP{ENSEMBLE}[$ens]->{HEADING} = $ants_[0][$hdgF]
   169 		$LADCP{ENSEMBLE}[$ens]->{HEADING} = $ants_[0][$hdgF]				# patch heading
   145 			if $opt_h;
   170 			if $opt_h;
   146 	} else {
   171 	} else {																# no valid IMP heading => invalidate LADCP data
   147 		$hdg_missing++;
   172 		$hdg_missing++;
   148 		unless ($opt_k)  {
   173 		unless ($opt_k)  {
   149 	    	clearEns(\%LADCP,$ens);
   174 	    	clearEns(\%LADCP,$ens);
   150 	    	$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID}= 0xA0;
   175 	    	$LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID}= 0xA0;
   151 	    }
   176 	    }
   152 	}
   177 	}
   153 } while (&antsIn());
   178 } while (&antsIn());
   154 
   179 
   155 $LADCP{ENSEMBLE}[0]->{DATA_SOURCE_ID} = 0x7F;				# ensure correct DSID (1st ens: orig; 2nd ens: this prog)
   180 $LADCP{ENSEMBLE}[0]->{DATA_SOURCE_ID} = 0x7F;								# ensure correct DSID (1st ens: orig; 2nd ens: this prog)
   156 $LADCP{ENSEMBLE}[1]->{DATA_SOURCE_ID} = 0xA0
   181 $LADCP{ENSEMBLE}[1]->{DATA_SOURCE_ID} = 0xA0
   157 	unless ($LADCP{ENSEMBLE}[1]->{DATA_SOURCE_ID}&0xF0 == 0xA0);
   182 	unless ($LADCP{ENSEMBLE}[1]->{DATA_SOURCE_ID}&0xF0 == 0xA0);
   158 
   183 
   159 writeData($outPD0,\%LADCP);
   184 writeData($outPD0,\%LADCP);													# write new PD0
   160 
   185 
   161 my($verb) = $opt_k ? 'retained' : 'cleared';
   186 my($verb) = $opt_k ? 'retained' : 'cleared';
   162 printf(STDERR "$outPD0: %d pitch/roll & %d heading values $verb\n",$pr_missing,$hdg_missing)
   187 printf(STDERR "$outPD0: %d pitch/roll & %d heading values $verb\n",$pr_missing,$hdg_missing)
   163        if ($pr_missing+$hdg_missing);
   188        if ($pr_missing+$hdg_missing);
   164         
   189