6 #-------------------------------------------------------------------------------------- 
    8 #                   Utility script for zsh library functions. 
   10 #-------------------------------------------------------------------------------------- 
   15 if [[ 
"$DEBUG" == 
"" ]]; 
then 
   20 export DEFAULT_OPTION=-               # 
default option 
 
   24 # Wild card for any valid detector identifier or run number; to be used as ${~ANY}. 
   26 export ANY_ID=
"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" 
   28 export 
PI=3.1415926535897931
 
   36 export 
TIMESTAMP=\#splitline{}{\#splitline{%
d-%m-%
y}{\ \ %
H:%M}}%F1970-01-01\ 00:00:00
 
   38 # Array of variable set / unset with set_variable / unset_variable (see below). 
   40 typeset -
A  ULIB_VARIABLES
 
   43 # Array of variables with default values defined with set_variable: or set_variable+ (see below). 
   45 typeset -
A  UDEF_VARIABLES
 
   55 function error()   { 
if (( $DEBUG >= 0 )); 
then echo -e $RED`date` 
ERROR   $* $RESET; fi }
 
   56 function notice()  { 
if (( $DEBUG >= 1 )); 
then echo -e     `date` 
NOTICE  $* ; fi }
 
   57 function status()  { 
if (( $DEBUG >= 2 )); 
then echo -e     `date` 
STATUS  $* ; fi }
 
   58 function warning() { 
if (( $DEBUG >= 2 )); 
then echo -e     `date` 
WARNING $* ; fi }
 
   63 # Method to check for usage of script. 
   65 # \param  1-N     list of command line options 
   66 # \return         0 for option -h; else 1 
   70     for variable 
in $*; 
do 
   71         if [[ 
"$variable" == 
"-h!" ]]; 
then 
   76     for variable in $*; 
do 
   77         if [[ 
"$variable" == 
"-h" || 
"$variable" == 
"-h!" ]]; 
then 
   87 # Method to print usage of script and exit. 
   95     if (( $DEBUG >= 3 )); 
then 
   96         print_array UDEF_VARIABLES
 
  104 # Method to print environment variables. 
  114 # The last parameter refers to the exit code. 
  119 function check_exit_code()
 
  131 # check exit code alias 
  133 alias CHECK_EXIT_CODE=
"check_exit_code \$0 \$* \$?" 
  139 # \param  1       first  value 
  140 # \param  2       second value 
  142 function test_equality
 
  144     if [[ 
"$1" != 
"$2" ]]; 
then 
  153 # Method to check if inside a conda env. 
  157     if [ -
n "${CONDA_PREFIX+1}" ]; 
then 
  166 # Method to check for CC Lyon. 
  168 # \return         1 for CC Lyon; else 0 
  174     if [[ -
n "${LOCALHOST}" && 
"${LOCALHOST:0:2}" =  
"cc" ]]; 
then 
  182 # Method to check if the local host corresponds to one of the Nikhef computer clusters 
  184 # \return           1 if local host corresponds to one of the Nikhef computer clusters; else false 
  186 function is_CCNikhef()
 
  190     if [[ -
n "${LOCALHOST}" && 
"${LOCALHOST:0:4}" == 
"stbc" ]]; 
then 
  199 # Method to define variable. 
  203 function define_variable()
 
  207     eval export $variable
 
  212 # Method to set variable. 
  213 # Note that a value equal to $DEFAULT_OPTION will not modify the variable. 
  216 # \param  2-N     value(s) 
  223     if [[ 
"$*" != 
"$DEFAULT_OPTION" ]]; 
then 
  224         eval export $variable=\
"$\*\" 
  227     eval ULIB_VARIABLES\[$variable\]=\"\$${variable}\" 
  232 # Method to set variable. 
  233 # The second variable corresponds to a possible default value. 
  234 # If this variable is defined, the first variable is set to its value 
  235 # else to the given value(s). 
  239 # \param  3-N     value(s) 
  241 function set_variable:() 
  249     eval value=\"\${$optional:-"$*
"}\" 
  251     set_variable $variable $value 
  253     UDEF_VARIABLES[$variable]="$optional -> $value
" 
  258 # Method to set variable. 
  259 # The second variable corresponds to a possible default value. 
  260 # If this variable is defined, the first variable is set to the given value(s) 
  261 # else to an empty value. 
  265 # \param  3-N     value(s) 
  267 function set_variable+() 
  275     eval value=\"\${$optional+"$*
"}\" 
  277     set_variable $variable $value 
  279     UDEF_VARIABLES[$variable]="$optional -> $value
" 
  284 # Method to locally set variable. 
  285 # Note that a value equal to $DEFAULT_OPTION will not modify the variable. 
  288 # \param  2-N     value(s) 
  290 function set_local_variable() 
  295     if [[ "$*
" != "$DEFAULT_OPTION
" ]]; then 
  296         eval $variable=\"$\*\" 
  302 # Method to unset variable. 
  306 function unset_variable() 
  311     eval unset \"ULIB_VARIABLES\[$variable\]\" 
  316 # Method to print variables. 
  318 # \param  1-N     list of variables 
  320 function print_variable() 
  322     for variable in $*; do 
  323         eval value=\${$variable} 
  324         printf "%-20s = %
s\n" ${variable} "${value}
" 
  330 # Method to print associative array. 
  334 function print_array() 
  340     eval keys=\(\${\(@k\)$array}\) 
  343         if (( ${#key} > $len )); then 
  349         eval printf \"%-${len}s = %s\\n\" $key \"\$${array}\[$key\]\" 
  355 # Method to check validity of variables. 
  357 # \param  1-N     list of variables 
  359 function check_variable() 
  361     for variable in $*; do 
  362         eval value=$+${variable} 
  363         if (( ${value} == 0 )); then 
  364             fatal "Variable ${variable} not defined.
" 
  371 # Method to set array. 
  373 # \param  1       array name 
  381     eval $variable=\($\*\) 
  386 # Method to expand array. 
  388 # Tokens <first>-<last> in the array are exanded to <first> <first + 1> .. <last> 
  390 # \param  1       array name 
  392 function expand_array() 
  394     eval __BUFFER__=\(\$$1\[\*\]\) 
  398     for RANGE in $__BUFFER__[*]; do 
  399         for (( i = ${RANGE%%-*}; $i <= ${RANGE##*-}; i += 1 )); do 
  407 # Method to count directory in ':' separated path list. 
  412 function count_directory() 
  416     echo -n $key | tr ":
" "\n" | grep "^\\${2}$
" | wc -w 
  421 # Method to remove directory from ':' separated path list. 
  426 function remove_directory() 
  431     if [[ -n "$key
" && -n "$value
" ]]; then 
  432         export $1=`eval echo $key | tr ":
" "\n" | grep -v "^\\\\${value}$
" | tr "\n" ":
" | sed 's/:\$//'` 
  438 # Method to remove variable from ':' separated path list. 
  443 function remove_variable() 
  448     if [[ -n "$key
" && -n "$value
" ]]; then 
  449         remove_directory $1 $value 
  455 # Method to insert directory into ':' separated path list. 
  460 function insert_directory() 
  464     if [[ -z "$key
" ]]; then 
  466     elif (( `eval echo -n $key | tr ":
" "\n" | grep "^\\\\${2}$
" | wc -w` == 0 )); then 
  473 # Method to check process path. 
  475 # \param   1-N    list of process names 
  477 function check_process() 
  479     for process in $*; do 
  481         local bin=`which $process 2>& /dev/null` 
  482         debug "Check process ${process}=${bin}
" 
  484         if [ -z "${bin}
" ]; then 
  485             fatal "Process ${process} not found.
" 
  492 # Method to check file access. 
  494 # \param   1-N    list of files 
  496 function check_input_file() 
  498     if (( $# == 0 )); then 
  499         fatal "No input 
file.
" 
  502     for file in `echo $*`; do 
  506         if   [[ ! -f ${file} ]]; then 
  507             fatal "File ${
file} not found.
" 
  508         elif [[ ! -r ${file} ]]; then 
  509             fatal "File ${
file} not readable.
" 
  516 # Method to check file access. 
  518 # \param   1-N    list of files 
  520 function check_output_file() 
  522     for file in `echo $*`; do 
  526         if [[ -f ${file} ]]; then 
  534 # Method to reuse file. 
  535 # Note that this method may require user interaction. 
  538 # \return         1 when absent; else 0 
  540 function reuse_file() 
  546     if   [[ ! -f $1 ]]; then     
  555 # Get host IP address 
  557 function get_ip_address() 
  559     hostname --all-ip-addresses | cut --delimiter=' ' -f1 
  564 # Get process identifier. 
  567 # \param          process name 
  572     if   (( $# == 1 )); then 
  573         exec ps h -o "%p
" -C $1  
  574     elif (( $# == 2 )); then 
  575         exec ssh $1 "ps h -
o "%p" -
C $2
" 
  581 # Method to get least significant digit. 
  588     printf "%
f\n" `echo $1  |  sed -n "s/[+-]\?\([0-9]*.\?[0-9]*\)[1-9]\([0]*.\?\)/\1
A\2/p
" | sed 's/[1-9]/0/g' | sed 's/A/1/'` 
  593 # Method to start timer 
  595 function timer_start() 
  597     let "TIMER = $(date +%s.%
N)
" 
  602 # Method to stop timer 
  604 function timer_stop() 
  606     let "TIMER = $(date +%s.%
N) - $TIMER
" 
  611 # Method to print timer 
  613 function timer_print() 
  615     printf "Elapsed time %1.3f s.\n
" $TIMER 
  620 # Method to print lap time 
  624     printf "Elapsed time %1.3f s.\n
" $(($(date +%s.%N) - $TIMER)) 
  629 # Method to convert associative array to list of equations 
  631 # \param   1         associative array 
  633 function make_equation() 
  639     eval array=\(\${\(kv\)${1}}\) 
  643     for key 
in ${(
k)array}; 
do 
  644         echo -
n "$key = ${array[${key}]}; " 
  652 # Test if associative array has given key. 
  654 # \param   1         associative array 
  656 # \return            1 if present; else 0 
  667 # Make communication with process. 
  670 #  - FD_O is the actual file descriptor for writing to actual process and 
  671 #  - FD_I is the actual file descriptor for reading from process. 
  673 # \param   1         process name 
  674 # \param   2-N       optional argument(s) 
  684     FIFO_O=/tmp/${PROCESS}_writing.$$               # named pipe 
for writing
 
  685     FIFO_I=/tmp/${PROCESS}_reading.$$               # named pipe 
for reading
 
  690     mkfifo $FIFO_O                                  # create 
a new FIFO 
  691     mkfifo $FIFO_I                                  # create 
a new FIFO 
  693     eval 
"exec {FD_O}<>$FIFO_O"                     # attach 
file descriptor to 
FIFO 
  694     eval 
"exec {FD_I}<>$FIFO_I"                     # attach 
file descriptor to 
FIFO 
  696     # launch process in background and cleanup afterwards 
  708 # Remove communication with process.
 
  710 # \param   1         
file descriptor 
for writing to process
 
  714     if   (( $# == 0 )); 
then 
  725 # Write to attachted prcess. 
  733 # Read from attachted prcess. 
  737     eval 
read $* \<\& $FD_I
 
  742 # alias to kill all child processes at exit of parent process 
  744 alias kill_child_processes_at_exit=
"trap  'kill_child_processes $$'  EXIT" 
  748 # recursively kill child processes for given parent identifier. 
  750 # \param  1       parent identifier 
  752 kill_child_processes()
 
  756     for child 
in `ps --ppid $pid -ho 
"%p"`; 
do 
  757         kill_child_processes $child
 
  760     if (( $pid != $$ )); 
then 
  761         kill -9 $pid >& /dev/
null 
  766 alias sftpget=
"$ULIB_DIR/sftpget.zsh" 
  767 alias sftpput=
"$ULIB_DIR/sftpput.zsh" 
then usage $script< detector identifier >< startdate\"YYYY-MM-DDHH:MM:SS\"><finaldate\"YYYY-MM-DDHH:MM:SS\"><QA/QCfile> fi case set_variable QAQC_TXT $argv[4]
 
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
 
std::istream & read(std::istream &in, JTestSummary &summary, const char delimiter= ' ')
Read test summary. 
 
then usage $script[< detector identifier >< run range >]< QA/QCfile > nExample script to produce data quality plots nWhen a detector identifier and run range are data are downloaded from the database nand subsequently stored in the given QA QC file
 
o $QUALITY_ROOT d $DEBUG!CHECK_EXIT_CODE JPlot1D f
 
*fatal Wrong number of arguments
 
static const double H
Planck constant [eV s]. 
 
then fatal Wrong number of arguments fi JConvertDetectorFormat a o
 
then echo Detector $DETECTOR aleady exists
 
static const double C
Physics constants. 
 
then echo Variable JPP_DIR undefined exit fi source $JPP_DIR setenv sh $JPP_DIR &dev null set_variable
 
static const double PI
Mathematical constants. 
 
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
 
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
 
then echo Creating output directory
 
static JNullStream null
Null I/O stream. 
 
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
 
static const char *const TIMESTAMP
Time stamp of earliest UTC time. 
 
source $JPP_DIR setenv csh $JPP_DIR &dev null eval JShellParser o a A
 
#define DEBUG(A)
Message macros.