CRUISE_SETUP.expect
author A.M. Thurnherr <athurnherr@yahoo.com>
Fri, 03 Jul 2020 10:25:08 -0400
changeset 12 5e67754f6457
parent 6 1ab5767c18dd
permissions -rwxr-xr-x
V1.6: no more master/slave terminology

#======================================================================
#                    C R U I S E _ S E T U P . E X P E C T 
#                    doc: Wed Mar 10 21:14:18 2004
#                    dlm: Fri Jul  3 10:21:32 2020
#                    (c) 2004 A.M. Thurnherr
#                    uE-Info: 39 32 NIL 0 0 72 2 2 8 NIL ofnI
#======================================================================

#--------
# HISTORY
#--------

# Mar 10, 2004: - created during NBP0402
# Apr  4, 2004: - final version NBP0402
# Jun 15, 2004: - adapted to BB150/Workhorse setup
# Jun 16, 2004: - ditto
# Jun 19, 2004: - BUG: could not handle multiple BB150 data files
# Jan 19, 2006: - updated documentation
#		- allowed for empty-string time_server
# Nov  4, 2006: - changed backup dir structure
# Aug 25, 2010: - rename* funs modified to allow them to be used to ensure
#		  that downladed file names have correct case
#		- renamed from DEFAULTS.expect to CRUISE_SETUP.expect
# Aug 26, 2010: - added ymodem_receive_cmd
#		- changed default string quotes to {} to prevent Tcl variable
#		  and command substitutions
# Oct 18, 2010: - changed comment for ymodem_receive_cmd to account for the
#		  fact that bbabble now uses the -u option by default
# Dec 10, 2010: - updated comments
#		- made mkProfile default file-checking program
#		- made backup dir structure consistent with raw dir structure
# Jul  3, 2020: - expunged master/slave terminology

#-----------
# TWEAKABLES
#-----------

# At the beginning of each cast, the clock of the acquisition computer should
# synchronized with the primary clock used for timestamping the GPS information.
# The best way to accomplish this is to run a NTP daemon (e.g. ntpd) polling
# an NTP server on the ship.
# If running ntpd is inconvenient or not possible, the follwing variable can
# be set to the name of the NTP server, in which case the time is sync'ed
# at the beginning of each cast with the function set_computer_time (defined
# below).
# If there is no NTP server available, the variable should be set to the
# empty string and the clock must be sync'ed manually.

set time_server	{};

# bbabble needs to know the tty device name(s) of the serial connection(s)
# to the ADCP head(s). If only one head is connected, tty1 is ignored.
# If two heads are connected, the device names are exchangeable, i.e.
# either instrument can be connected to either port.

set tty0 {/dev/ttyS0};
set tty1 {/dev/ttyS1};

# In case of 2 ADCP heads, babble needs to know which is of the instruments
# sends the sync pulses and which instrument receives them. 
# This is accomplished by setting the following variables
# to the corresponding instrument serial numbers (PS0 output). By convention,
# the downlooker sends the sync pulses. If only a single head is connected, both
# variables are ignored.

set DL_sn 7877;
set UL_sn  1412;

# Before deployment, every ADCP head has to be programmed by sending it
# a corresponding command file, defined by the following variables. If only
# a single head is connected, the UL_* variable is ignored.

set DL_cmd_file 	{DOWNLOOKER.cmd};	
set UL_cmd_file  	{UPLOOKER.cmd};

# For convenience, it is best to use the station number to name the ADCP data
# files, and to indicate whether the data file comes from the DL or
# UL. The following variables variables define the printf(3) format
# that is used to create the files names from the station number. If only
# a single head is connected, the UL_* variable is ignored.

set DL_deployment_name_fmt	{%03dDL};
set UL_deployment_name_fmt	{%03dUL};

# Older RDI instruments (e.g. the BB150) do not allow the data-file name
# to be defined in the instrument, in which case the files have to be
# renamed after downloading. The following variables define the shell
# globbing patterns for the DL and UL. For example, the UH BB150
# used during CLIVAR P02 generated files with extension .612; the coresponding
# globbing pattern is {*.612}. If only a single head is connected, the
# UL_* variable is ignored.
# NB: For dual-headed LADCP systems it is important that the globs do not
#     match both DL and UL data files. {*.000} therefore will not
#     work for dual-head Workhorse systems.

set DL_download_glob 	{};
set UL_download_glob		{};

# For consistency it is nicest if low station numbers contain leading
# zeroes. The following variable defines the corresponding printf(3) format.

set stn_format {%03d};

# The following directory is used to store command files, instrument
# configurations and ADCP data. Separate subdirectories (named according to
# stn_format) are used for each cast.

set raw_dir {../data/raw};

# After downloading, the data directory is backed up to the following
# directory. Make sure that it resides on a different disk than raw_dir.
# Empty string disables backup for testing purposes.
# NOTE: RELATIVE PATHNAMES MUST START FROM CAST DIR (NOT ACQUISITION DIR)

set backup_dir {../../backup/raw};

# By default, bbabble will look for standard UN*X ymodem receive
# programs (rb and lrb) and call the first one it finds with the
# -u option. If neither of the standard programs exist, if neither is in the
# current $PATH, or if non-standard options have to be passed the following
# variable can be set to a non-empty string, such as {/sw/bin/rb -u}

set ymodem_receive_cmd {}

# bbabble can be made to abort downloading after a preset amount of time,
# defined by the following variable. 

set download_timeout 999999;

#----------------------------------------------------------------------
# Semi-Standard Functions
#----------------------------------------------------------------------

# The program for polling an NTP server is different on different
# UN*X systems. Either rdate or ntpdate should be available. Note
# that sudo will propt for the login password of the user running
# the LADCP acquisition, unless it is explicitly set to allow execution
# of the command without a password.

proc set_computer_time {} {				# set computer clock
	global time_server;
	if {[string length $time_server] == 0} {return}
	exec sudo rdate -s $time_server;
#	exec sudo ntpdate -s $time_server;
}

#----------------------------------------------------------------------

# After the LADCP data files have been downloaded they should be
# checked. A simple yet effective check consists in estimating the
# bottom depth (zmax) and end depth (zend) by time-integrating the
# vertical-velocity measurements. There are two different publicly
# available programs to accomplish this: scanbb from the UH LADCP
# software suite, and mkProfile from the perl-utilities available
# from the LDEO LADCP repository.

proc check_data {file} {				# check data file
	exec sh -c "mkProfile -Q $file 2> `basename $file .000`.profinfo";
	exec sh -c "cat `basename $file .000`.profinfo > /dev/tty";
#	exec sh -c "scanbb $file";
#	exec sh -c "sed -n '/^zmax/,/^zend/s/ at.*/m/p' `basename $file .000`.scn > /dev/tty";
}

#----------------------------------------------------------------------

# The following two routines backup the DL and UL files from the
# raw directory. 

proc backup_DL_data {stn} {
	global stn_format DL_deployment_name_fmt DL_cmd_file backup_dir;
	if {[string length $backup_dir] == 0} {return}

	if {![file isdirectory $backup_dir]} {
		error "$backup_dir does not exist relative to [pwd]";
	}

	set stnstr [format $stn_format $stn];
	set cast_dir [format %s/%s $backup_dir $stnstr];
	if {![file isdirectory $cast_dir]} {exec mkdir $cast_dir};
	exec sh -c "cp [format $DL_deployment_name_fmt $stn]* $cast_dir";
	exec sh -c "cp $DL_cmd_file* $cast_dir";
	exec sh -c "cp *log $cast_dir";
}
	
proc backup_UL_data {stn} {				# copy data to network
	global stn_format UL_deployment_name_fmt UL_cmd_file backup_dir;
	if {[string length $backup_dir] == 0} {return}

	if {![file isdirectory $backup_dir]} {
		error "$backup_dir does not exist relative to [pwd]";
	}

	set stnstr [format $stn_format $stn];
	set cast_dir [format %s/%s $backup_dir $stnstr];
	if {![file isdirectory $cast_dir]} {exec mkdir $cast_dir};
	exec sh -c "cp [format $UL_deployment_name_fmt $stn]* $cast_dir";
	exec sh -c "cp $UL_cmd_file* $cast_dir";
	exec sh -c "cp *log $cast_dir";
}

#----------------------------------------------------------------------

# See comments on DL_download_glob and UL_download_glob above.

proc rename_DL_download_file {target} {		# rename after download 
	global DL_download_glob;
	
	if {[string length $DL_download_glob] == 0} {return}
	set files [glob -nocomplain $DL_download_glob];
	set nfiles [llength $files];
	if {$nfiles == 0} {
		if {![file exists $target]} {
			error {can't find downloaded DL file};
		}
		return;
        }
        if {$nfiles > 1} {
	        send_user {WARNING: Multiple files downloaded from DL --- rename, backup, check manually!};
	}
	if {![string equal [lindex $files end] $target]} {
		exec mv [lindex $files end] $target;
	}
}

proc rename_UL_download_file {target} {		# rename after download
	global UL_download_glob;
	
	if {[string length $UL_download_glob] == 0} {return}
	set files [glob -nocomplain $UL_download_glob];
	set nfiles [llength $files];
	if {$nfiles == 0} {
		if {![file exists $target]} {
			error {can't find downloaded UL file};
		}
		return;
        }
        if {$nfiles > 1} {
	        send_user {WARNING: Multiple files downloaded from UL --- rename, backup, check manually!};
	}
	if {![string equal [lindex $files end] $target]} {
		exec mv [lindex $files end] $target;
	}
}