Alpine3D

Alpine3D Svn Source Tree

Root/trunk/tests/SnExtract.sh

  • Property svn:executable set to *
1#!/bin/bash
2#This script extracts a given column out of a met or pro file
3
4if [ $# -lt 1 ]; then
5echo "Extract parameters out of .pro or .met files, even if they are compressed with bzip2."
6echo "$0 <filename> \t to get the list of available parameters with their number"
7echo "$0 <filename> <param_number>\t to extract a given parameter"
8exit
9fi
10
11#determine if we are dealing with a pro or met file
12ext=`echo ${1##*.}`
13if [ "${ext}" = "bz2" ]; then
14is_compressed=1
15base=`basename $1 .${ext}`
16ext=`echo ${base##*.}`
17fi
18
19cat_head() {
20if [ "${is_compressed}" ]; then
21bzcat $1 | head -50
22else
23head -50 $1
24fi
25}
26
27list_met() {
28char_width=`tput cols`
29cat_head $1 | grep -E "^ID," | tr "," "\n" | nl | pr --columns=2 --omit-pagination --width=${char_width}
30}
31
32cat_all() {
33if [ "${is_compressed}" ]; then
34bzcat $1
35else
36cat $1
37fi
38}
39
40extract_met() {
41station=`basename $1 .met`
42cat_all $1 | awk -F, '
43BEGIN {
44param="'"$2"'"
45}
46/^ID/ {
47printf("#'${station}'\n")
48printf("#Date %s\n", $(param))
49}
50/^\[DATA\]/ {
51in_data=1
52next
53}
54in_data==1 {
55date=$2
56gsub(" ",".",date)
57gsub(":",".",date)
58split(date,d,".")
59printf("%04d-%02d-%02dT%02d:%02d %g\n", d[3], d[2], d[1], d[4], d[5], $(param))
60}
61END {
62printf("\n")
63}
64'
65}
66
67list_pro() {
68cat_head $1 | awk -F, '
69BEGIN {
70OFS=" "
71}
72/^\[HEADER\]/ {
73in_header=1
74next
75}
76/^\[DATA\]/ {
77exit
78}
79/^#/ {
80next
81}
82in_header==1 {
83if($2=="Date") printf("0500 Date\n")
84else {
85$2=""
86print $0
87}
88}
89'
90}
91
92extract_pro() {
93station=`basename $1 .pro`
94cat_all $1 | awk -F, '
95BEGIN {
96param="'"$2"'"
97}
98/^#/ {
99next
100}
101/^\[HEADER\]/ {
102in_header=1
103next
104}
105in_header==1 && $1==param {
106$1=""
107$2=""
108printf("#'${station}'\n")
109printf("#Date %s\n", $0)
110in_header=0
111next
112}
113/^\[DATA\]/ {
114in_header=0
115in_data=1
116next
117}
118in_data==1 && $1=="0500" {
119date=$2
120gsub(" ",".",date)
121gsub(":",".",date)
122split(date,d,".")
123date=sprintf("%04d-%02d-%02dT%02d:%02d", d[3], d[2], d[1], d[4], d[5])
124}
125in_data==1 && $1==param {
126$1=""
127$2=""
128printf("%s %s\n", date, $0)
129}
130END {
131printf("\n")
132}
133'
134}
135
136################################################
137
138if [ $# -eq 1 ]; then
139if [ "$ext" = "met" ]; then
140list_met $1
141fi
142if [ "$ext" = "pro" ]; then
143list_pro $1
144fi
145fi
146if [ $# -eq 2 ]; then
147if [ "$ext" = "met" ]; then
148extract_met $1 $2
149fi
150if [ "$ext" = "pro" ]; then
151extract_pro $1 $2
152fi
153fi
154

Archive Download this file

Revision: HEAD