File: table_average.sh

package info (click to toggle)
votca 2025.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 132,420 kB
  • sloc: xml: 345,964; cpp: 80,067; python: 15,957; sh: 4,580; perl: 2,169; javascript: 202; makefile: 34
file content (126 lines) | stat: -rwxr-xr-x 3,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#! /usr/bin/env bash
#
# Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

show_help () {
  cat <<EOF
${0##*/}, version %version%
This script creates averages tables and also calculates the error. 

Usage: ${0##*/} [options] table1 table2 table3 ....

Allowed options:
-h, --help                    show this help
-o, --output NANE             output file name
    --cols NUM                Number of columns per file
                              Default: $cols
    --col-y NUM               y-data column
                              Default: $coly
    --col-x NUM               x-data column
                              Default: $colx
    --clean		      Clean intermediate files

Examples:
* ${0##*/} --output CG-CG.dist.new CG-CG*.dist.new 
EOF
}

#default
out=""
cols=3
colx=1
coly=2
clean="no"

shopt -s extglob
while [[ ${1#-} != $1 ]]; do
 if [[ ${1#--} = $1 && -n ${1:2} ]]; then
    #short opt with arguments here: o
    if [[ ${1#-[o]} != ${1} ]]; then
       set -- "${1:0:2}" "${1:2}" "${@:2}"
    else
       set -- "${1:0:2}" "-${1:2}" "${@:2}"
    fi
 fi
 case $1 in
   -o | --output)
    out="$2"
    shift 2;;
   --cols)
    is_int "$2" || die "${0##*/}: argument after --cols should be an integer"
    cols="$2"
    shift 2;;
   --col-x)
    is_int "$2" || die "${0##*/}: argument after --col-x should be an integer"
    colx="$2"
    shift 2;;
   --col-y)
    is_int "$2" || die "${0##*/}: argument after --col-y should be an integer"
    coly="$2"
    shift 2;;
   --clean)
    clean="yes"
    shift;;
   -h | --help)
    show_help
    exit 0;;
  *)
   die "Unknown option '$1'";;
 esac
done
### end parsing options

[[ -z $1 || -z $2 ]] && die "${0##*/}: Missing arguments"
[[ -z "$out" ]] && die "${0##*/}: No output file given add --output option"

l=""
c=0
tables=()
for f in "$@"; do
  [[ -f $f ]] || die "Could not find table $f"
  t=$(critical mktemp "$f.XXXX")
  critical sed '/^[#@]/d' $f > $t
  [[ -z $l ]] && l=$(critical sed -n '$=' $t)
  [[ $l -eq $(critical sed -n '$=' $t) ]] || die "Number of lines (after comments have been striped) mismatches in $f from $1"
  tables[$c]="$t"
  ((c++))
done
t=$(critical mktemp "table_all.XXXX")
critical paste "${tables[@]}" > "${t}"
#no critical here to avoid huge error message
awk -v c1="$colx" -v c2="$coly" -v s="$cols" '
function isnum(x){return(x==x+0)}
{
  sum=0;
  sum2=0;
  c=0;
  for (i=0;i<NF;i+=s) {
    sum+=$(i+c2);
    sum2+=$(i+c2)*$(i+c2);
    c++;
    if ($(c1) != $(c1+i)) {
      print "x-value mismatch",$(c1),"vs. ", $(c1+i), " in line",NR > "/dev/stderr";
      exit 1;
    }
  }
  flag="u"
  if (isnum(sum)&&isnum(sum2)) { flag="i" }
  print $1,sum/c,sqrt((sum2-sum*sum/c)/(c*(c-1))),flag;
}' $t > $out || die "${0##*/}: averaging with awk failed"

if [[ $clean = "yes" ]]; then
  rm -f "${tables[@]}" "$t"
fi