RDI_PD0_IO.pl
changeset 28 7c7da52363c2
parent 26 02776c168135
child 29 aeb26e966b71
equal deleted inserted replaced
27:1c128aaaca6f 28:7c7da52363c2
     1 #======================================================================
     1 #======================================================================
     2 #                    R D I _ B B _ R E A D . P L 
     2 #                    R D I _ P D 0 _ I O . P L 
     3 #                    doc: Sat Jan 18 14:54:43 2003
     3 #                    doc: Sat Jan 18 14:54:43 2003
     4 #                    dlm: Fri Oct  2 19:17:15 2015
     4 #                    dlm: Fri Dec 18 18:01:45 2015
     5 #                    (c) 2003 A.M. Thurnherr
     5 #                    (c) 2003 A.M. Thurnherr
     6 #                    uE-Info: 66 47 NIL 0 0 72 2 2 4 NIL ofnI
     6 #                    uE-Info: 1106 0 NIL 0 0 72 2 2 4 NIL ofnI
     7 #======================================================================
     7 #======================================================================
     8 
     8 
     9 # Read RDI BroadBand Binary Data Files (*.[0-9][0-9][0-9])
     9 # Read RDI BroadBand Binary Data Files (*.[0-9][0-9][0-9])
    10 
    10 
    11 # HISTORY:
    11 # HISTORY:
    62 #	Sep  6, 2014: - adapted WBRhdr to >7 data types
    62 #	Sep  6, 2014: - adapted WBRhdr to >7 data types
    63 #	Oct 15, 2014: - implemented work-around for readData() not recognizing
    63 #	Oct 15, 2014: - implemented work-around for readData() not recognizing
    64 #					incomplete ensemble at the end, which seems to imply that there is
    64 #					incomplete ensemble at the end, which seems to imply that there is
    65 #				    a garbage final ensemble that passes the checksum test???
    65 #				    a garbage final ensemble that passes the checksum test???
    66 #	Oct  2, 2015: - added &skip_initial_trash()
    66 #	Oct  2, 2015: - added &skip_initial_trash()
       
    67 #	Dec 18, 2015: - added most data types to WBPofs()
       
    68 #				  - BUG: WBPens() requires round() for scaled values
    67 
    69 
    68 # FIRMWARE VERSIONS:
    70 # FIRMWARE VERSIONS:
    69 #	It appears that different firmware versions generate different file
    71 #	It appears that different firmware versions generate different file
    70 #	structures. Currently (Sep 2005) these routines have been tested
    72 #	structures. Currently (Sep 2005) these routines have been tested
    71 #	with the following firmware versions (as reported by [listHdr]):
    73 #	with the following firmware versions (as reported by [listHdr]):
   940 		}
   942 		}
   941 	} # ens loop
   943 	} # ens loop
   942 }
   944 }
   943 
   945 
   944 #----------------------------------------------------------------------
   946 #----------------------------------------------------------------------
   945 # WBPens(nbins,fixed_leader_bytes,^data)
   947 # writeData(output_file_name,^data) WBPens(nbins,fixed_leader_bytes,^data)
   946 # 	- patch PD0 file with new data
   948 #	- writeData() copies file previously read with readData() to output_file_name
   947 #	- file must already exist and have correct structure
   949 # 	- WBPens() patches new PD0 file with ^data
   948 #	- currently only does part of variable leader data, esp. attitude
   950 #		- ^data is modified!!!!
       
   951 #		- output file must already exist and have correct structure
       
   952 #		- only subset of data structure is patched:
       
   953 #			- Header: Data Source Id
       
   954 #			- Var Ldr: Soundspeed, Depth, Heading, Pitch, Roll, Temp, Salin
       
   955 #			- Data: Velocity, Correlation, Echo Amp, %-Good, 
   949 #----------------------------------------------------------------------
   956 #----------------------------------------------------------------------
   950 
   957 
   951 sub writeData(@)
   958 sub writeData(@)
   952 {
   959 {
   953 	my($fn,$dta) = @_;
   960 	my($fn,$dta) = @_;
   959 
   966 
   960 	open(WBPF,"+<$WBPcfn") || die("$WBPcfn: $!");
   967 	open(WBPF,"+<$WBPcfn") || die("$WBPcfn: $!");
   961     WBPens($dta->{N_BINS},$dta->{FIXED_LEADER_BYTES},
   968     WBPens($dta->{N_BINS},$dta->{FIXED_LEADER_BYTES},
   962 	                   \@{$dta->{ENSEMBLE}});
   969 	                   \@{$dta->{ENSEMBLE}});
   963 }
   970 }
       
   971 
       
   972 sub round(@)
       
   973 {
       
   974 	return $_[0] >= 0 ? int($_[0] + 0.5)
       
   975 					  : int($_[0] - 0.5);
       
   976 }
       
   977 
   964 
   978 
   965 sub WBPens($$$)
   979 sub WBPens($$$)
   966 {
   980 {
   967 	my($nbins,$fixed_leader_bytes,$E) = @_;
   981 	my($nbins,$fixed_leader_bytes,$E) = @_;
   968 	my($start_ens,$B1,$B2,$B3,$B4,$I,$id,$bin,$beam,$buf,$dummy,@dta,$i,$cs,@WBPofs);
   982 	my($start_ens,$B1,$B2,$B3,$B4,$I,$id,$bin,$beam,$buf,$dummy,@dta,$i,$cs,@WBPofs);
   996 		# Variable Leader
  1010 		# Variable Leader
   997 		#------------------------------
  1011 		#------------------------------
   998 	
  1012 	
   999 		sysseek(WBPF,$start_ens+$WBPofs[1]+12,0) || die("$WBPcfn: $!");
  1013 		sysseek(WBPF,$start_ens+$WBPofs[1]+12,0) || die("$WBPcfn: $!");
  1000 		
  1014 		
  1001 		${$E}[$ens]->{XDUCER_DEPTH} *= 10;
  1015 		${$E}[$ens]->{XDUCER_DEPTH} = round(${$E}[$ens]->{XDUCER_DEPTH}*10);
  1002 
  1016 
  1003 		# IMP EXTENSIONS
  1017 		#-----------------------------
  1004 		#---------------
  1018 		# IMP allows for missing value
       
  1019 		#-----------------------------
       
  1020 
  1005 		${$E}[$ens]->{HEADING} = defined(${$E}[$ens]->{HEADING})
  1021 		${$E}[$ens]->{HEADING} = defined(${$E}[$ens]->{HEADING})
  1006 							   ? ${$E}[$ens]->{HEADING} * 100
  1022 							   ? round(${$E}[$ens]->{HEADING}*100)
  1007 							   : 0xF000;
  1023 							   : 0xF000;
  1008 		${$E}[$ens]->{PITCH} = defined(${$E}[$ens]->{PITCH})
  1024 		${$E}[$ens]->{PITCH} = defined(${$E}[$ens]->{PITCH})
  1009 							 ? unpack('S',pack('s',${$E}[$ens]->{PITCH}*100))
  1025 							 ? unpack('S',pack('s',round(${$E}[$ens]->{PITCH}*100)))
  1010 							 : 0x8000;
  1026 							 : 0x8000;
  1011 		${$E}[$ens]->{ROLL} = defined(${$E}[$ens]->{ROLL})
  1027 		${$E}[$ens]->{ROLL} = defined(${$E}[$ens]->{ROLL})
  1012 						    ? unpack('S',pack('s',${$E}[$ens]->{ROLL}*100))
  1028 						    ? unpack('S',pack('s',round(${$E}[$ens]->{ROLL}*100)))
  1013 						    : 0x8000;
  1029 						    : 0x8000;
  1014 
  1030 
  1015 		${$E}[$ens]->{TEMPERATURE} =
  1031 		${$E}[$ens]->{TEMPERATURE} =
  1016 			unpack('S',pack('s',${$E}[$ens]->{TEMPERATURE}*100));
  1032 			unpack('S',pack('s',round(${$E}[$ens]->{TEMPERATURE}*100)));
  1017 
  1033 
  1018 		sysseek(WBPF,2,1);			# skip built-in test which reads as 0 but is usually undef		
  1034 		sysseek(WBPF,2,1);			# skip built-in test which reads as 0 but is usually undef		
  1019 									# this was found not to matter, but there is no reason to edit
  1035 									# this was found not to matter, but there is no reason to edit
  1020 #		my($b1);					# this field
  1036 #		my($b1);					# this field
  1021 #		sysread(WBPF,$b1,14);
  1037 #		sysread(WBPF,$b1,14);
  1029 			 ${$E}[$ens]->{SALINITY},${$E}[$ens]->{TEMPERATURE});
  1045 			 ${$E}[$ens]->{SALINITY},${$E}[$ens]->{TEMPERATURE});
  1030 
  1046 
  1031 		my($nw) = syswrite(WBPF,$buf,14);
  1047 		my($nw) = syswrite(WBPF,$buf,14);
  1032 		$nw == 14 || die("$WBPcfn: $nw bytes written ($!)");
  1048 		$nw == 14 || die("$WBPcfn: $nw bytes written ($!)");
  1033 
  1049 
       
  1050 
       
  1051 		#--------------------
       
  1052 		# Velocity Data
       
  1053 		#--------------------
       
  1054 
       
  1055 		sysseek(WBPF,$start_ens+$WBPofs[2]+2,0) || die("$WBRcfn: $!");	# skip velocity data id (assume it is correct)
       
  1056 		for ($bin=0; $bin<$nbins; $bin++) {
       
  1057 			for ($beam=0; $beam<4; $beam++) {
       
  1058 				${$E}[$ens]->{VELOCITY}[$bin][$beam] = defined(${$E}[$ens]->{VELOCITY}[$bin][$beam])
       
  1059 							   						 ? round(${$E}[$ens]->{VELOCITY}[$bin][$beam]*1000)
       
  1060 							   						 : 0x8000;
       
  1061 				$buf = pack('v',unpack('S',pack('s',${$E}[$ens]->{VELOCITY}[$bin][$beam])));
       
  1062 				my($nw) = syswrite(WBPF,$buf,2);
       
  1063 				$nw == 2 || die("$WBPcfn: $nw bytes written ($!)");
       
  1064 			}
       
  1065 		}
       
  1066 
       
  1067 		#--------------------
       
  1068 		# Correlation Data
       
  1069 		#--------------------
       
  1070 
       
  1071 		sysseek(WBPF,$start_ens+$WBPofs[3]+2,0) || die("$WBRcfn: $!");
       
  1072 		for ($bin=0; $bin<$nbins; $bin++) {
       
  1073 			for ($beam=0; $beam<4; $beam++) {
       
  1074 				$buf = pack('C',${$E}[$ens]->{CORRELATION}[$bin][$beam]);
       
  1075 				my($nw) = syswrite(WBPF,$buf,1);
       
  1076 				$nw == 1 || die("$WBPcfn: $nw bytes written ($!)");
       
  1077 			}
       
  1078 		}
       
  1079 
       
  1080 		#--------------------
       
  1081 		# Echo Intensity Data
       
  1082 		#--------------------
       
  1083 
       
  1084 		sysseek(WBPF,$start_ens+$WBPofs[4]+2,0) || die("$WBRcfn: $!");
       
  1085 
       
  1086 		for ($bin=0; $bin<$nbins; $bin++) {
       
  1087 			for ($beam=0; $beam<4; $beam++) {
       
  1088 				$buf = pack('C',${$E}[$ens]->{ECHO_AMPLITUDE}[$bin][$beam]);
       
  1089 				my($nw) = syswrite(WBPF,$buf,1);
       
  1090 				$nw == 1 || die("$WBPcfn: $nw bytes written ($!)");
       
  1091 			}
       
  1092 		}
       
  1093 
       
  1094 		#--------------------
       
  1095 		# Percent Good Data
       
  1096 		#--------------------
       
  1097 
       
  1098 		sysseek(WBPF,$start_ens+$WBPofs[5]+2,0) || die("$WBRcfn: $!");
       
  1099 
       
  1100 		for ($i=0,$bin=0; $bin<$nbins; $bin++) {
       
  1101 			for ($beam=0; $beam<4; $beam++,$i++) {
       
  1102 				$buf = pack('C',${$E}[$ens]->{PERCENT_GOOD}[$bin][$beam]);
       
  1103 				my($nw) = syswrite(WBPF,$buf,1);
       
  1104 				$nw == 1 || die("$WBPcfn: $nw bytes written ($!)");
       
  1105 			}
       
  1106 		}
       
  1107 
       
  1108 		#-----------------------------------------
       
  1109 		# Bottom-Track Data
       
  1110 		#	- scan through remaining data types
       
  1111 		#-----------------------------------------
       
  1112 
       
  1113 		my($nxt);
       
  1114 		for ($nxt=6; $nxt<$ndt; $nxt++) {										# scan until BT found
       
  1115 			sysseek(WBPF,$start_ens+$WBPofs[$nxt],0) || die("$WBRcfn: $!");
       
  1116 			sysread(WBPF,$buf,2) == 2 || die("$WBRcfn: $!");
       
  1117 			$id = unpack('v',$buf);
       
  1118 			last if ($id == 0x0600);
       
  1119 		}
       
  1120 
       
  1121 		unless ($nxt == $ndt) {													# BT found
       
  1122 			sysseek(WBPF,14,1) || die("$WBRcfn: $!");							# skip BT config
       
  1123 			# NOT YET IMPLEMENTED
       
  1124 		}
       
  1125 
       
  1126 
  1034 		#----------------
  1127 		#----------------
  1035 		# Update Checksum
  1128 		# Update Checksum
  1036 		#----------------
  1129 		#----------------
  1037 
  1130 
  1038 		sysseek(WBPF,$start_ens,0) || die("$WBPcfn: $!");
  1131 		sysseek(WBPF,$start_ens,0) || die("$WBPcfn: $!");