目次
前
次
計測データ集計スクリプト
EEPROMから吸い上げたデータを、集計するために
AWKスクリプトを使いました。
バッチファイルあるいはシェルスクリプトを使い
CSV形式ファイルまで変換することを目標にして
2種のスクリプトを用意していました。
ビニルハウスの中に入れた温度計測装置、温湿度計測装置
があるので、2種のスクリプトが必要でした。
温度計測装置は、2カ所に設置して、EEPROMの容量を
変えていたため、スクリプトも2種用意。
温度計測装置のうち、手前と奥でデータ集計する
バッチファイルは、別々に用意。
手前(温度)
使っているバッチファイルの内容は、以下。
gawk -f cx.awk xdat.txt | gawk -f cy.awk | gawk -f cz.awk > xdat1.txt
gawk -f cu.awk xdat1.txt > xdat2.txt
copy head.txt+xdat2.txt xdatx.txt
del xdat1.txt
del xdat2.txt
UNIX系OSであれば、次のように記述。
awk -f cx.awk xdat.txt | awk -f cy.awk | awk -f cz.awk > xdat1.txt
awk -f cu.awk xdat1.txt > xdat2.txt
cat head.txt xdat2.txt > xdatx.txt
rm xdat1.txt
rm xdat2.txt
使っているAWKスクリプトは、後で説明します。
奥(温度)
使っているバッチファイルの内容は、以下。
gawk -f cx.awk ydat.txt | gawk -f cy.awk | gawk -f cz.awk > ydat1.txt
gawk -f cu.awk ydat1.txt > ydat2.txt
copy head.txt+ydat2.txt ydaty.txt
del ydat1.txt
del ydat2.txt
UNIX系OSであれば、次のように記述。
awk -f cx.awk ydat.txt | awk -f cy.awk | awk -f cz.awk > ydat1.txt
awk -f cu.awk ydat1.txt > ydat2.txt
cat head.txt ydat2.txt > ydaty.txt
rm ydat1.txt
rm ydat2.txt
2カ所の温度計測データを集計するために
用意したAWKスクリプトは、4種作成して
パイプで接続して、CSV形式ファイルまで
作成しています。
AWKスクリプトを紹介していきます。
cx.awk
EEPROMには、1行に2レコードを入れてあるので
1行16バイトの数値から、月日、時刻、温度、換算
日照値を分離します。
リアルタイムクロックが、ズレた情報を入れている
可能性もあるので、開始月日と時刻を与えて補正
するのが、このスクリプトの役目としました。
内容は、以下。
function getValue(x)
{
result = 0
if ( x == "1" ) { result = 1 }
if ( x == "2" ) { result = 2 }
if ( x == "3" ) { result = 3 }
if ( x == "4" ) { result = 4 }
if ( x == "5" ) { result = 5 }
if ( x == "6" ) { result = 6 }
if ( x == "7" ) { result = 7 }
if ( x == "8" ) { result = 8 }
if ( x == "9" ) { result = 9 }
if ( x == "A" ) { result = 10 }
if ( x == "B" ) { result = 11 }
if ( x == "C" ) { result = 12 }
if ( x == "D" ) { result = 13 }
if ( x == "E" ) { result = 14 }
if ( x == "F" ) { result = 15 }
return result
}
{
if ( $1 != "FF" && $9 != "FF" ) {
for ( i = 1 ; i < 17 ; i++ ) {
# month
if ( i == 1 || i == 9 ) {
cur = $i + 4
printf("%02d/",cur)
}
# day
if ( i == 2 || i == 10 ) {
cur = $i + 0
printf("%02d,",cur)
}
# hour
if ( i == 3 || i == 11 ) {
cur = $i + 18
cur = cur % 24
printf("%02d:",cur)
}
# minute
if ( i == 4 || i == 12 ) {
cur = $i + 48
cur = cur % 60
printf("%02d,",cur)
}
if ( 4 < i && i < 8 ) { printf("%s,",$i) }
if ( 12 < i && i < 16 ) { printf("%s,",$i) }
if ( i == 8 || i == 16 ) {
num = getValue(substr($i,1,1)) * 16 + getValue(substr($i,2,1))
printf("%d\n",4*num)
}
}
}
}
cy.awk
一つ前のスクリプトでは、時間と日付の変化が
処理できなかったので、CSV形式になっている
レコードから、時間だけを補正していきます。
また、1行に1レコードが含まれていくように
修正してあります。
BEGIN {
FS = ","
}
{
#
tarx = $2
flag = 0
if ( substr(tarx,4,2) == "58" ) {
pre = substr(tarx,1,2)
# this record
flag = 1
}
#
car = substr(tarx,1,2)
cdr = substr(tarx,4,2)
result = $2
if ( pre == car && flag == 0 ) {
car = (car+1) % 24
# generate 2 charactor
if ( car < 10 ) { car = "0" car }
result = car ":" cdr
}
# generate calculated value
xx = int( $6 / 20)
# show
printf("%s,%s,%s,%s,%s,%s,%s\n",$1,result,$3,$4,$5,xx,$6)
}
cz.awk
一つ前のスクリプトで、時間の補正が完了して
いるので、日付の補正をしています。
BEGIN {
FS = ","
}
{
tarx = $2
flag = 0
if ( tarx == "23:58" ) {
pre = $1
# this record
flag = 1
}
car = substr($1,1,2)
cdr = substr($1,4,2)
result = $1
if ( pre == $1 && flag == 0 ) {
# ? odd or even
man = car % 2
# odd
if ( man == 1 ) {
cdr = (cdr+1) % 32
}
# even without Feb
if ( man == 0 && car != 2 ) {
cdr = (cdr+1) % 31
}
# Feb
if ( man == 0 && car == 2 ) {
cdr = (cdr+1) % 29
}
# generate 2 charactor
if ( cdr < 10 ) { cdr = "0" cdr }
# concatenate
result = car "/" cdr
}
printf("%s,%s,%s,%s,%s,%s,%s\n",result,$2,$3,$4,$5,$6,$7)
}
cu.awk
氷点下の温度が、90度以上の高温とされる
ことがあったので、最後に補正します。
BEGIN {
FS = ","
}
{
if ( NR == 1 ) {
printf("%s\n",$0)
} else {
x3 = $3
if ( $3 > 60 ) { x3 = $3 - 96 }
x4 = $4
if ( $4 > 60 ) { x4 = $4 - 96 }
x5 = $5
if ( $5 > 60 ) { x5 = $5 - 96 }4
printf("%s,%s,%02d,%02d,%02d,%s,%s\n",$1,$2,x3,x4,x5,$6,$7)
}
}
温湿度計測装置では、EEPROMに1レコードを13バイトとして
扱うので、次のバッチファイルを使ってCSV形式ファイルまで
変換しました。
gawk -f ha.awk hdat.txt | gawk -f hb.awk | gawk -f hc.awk > hdat1.txt
copy headx.txt+hdat1.txt hdath.txt
del hdat1.txt
UNIX系OSであれば、次のように記述。
awk -f ha.awk hdat.txt | awk -f hb.awk | awk -f hc.awk > hdat1.txt
cat headx.txt hdat1.txt > hdath.txt
rm hdat1.txt
バッチファイル内部で利用している
AWKスクリプトの動作を説明します。
ha.awk
リアルタイムクロックが、ズレた情報を入れている
可能性もあるので、開始月日と時刻を与えて補正。
function getValue(x)
{
result = 0
if ( x == "1" ) { result = 1 }
if ( x == "2" ) { result = 2 }
if ( x == "3" ) { result = 3 }
if ( x == "4" ) { result = 4 }
if ( x == "5" ) { result = 5 }
if ( x == "6" ) { result = 6 }
if ( x == "7" ) { result = 7 }
if ( x == "8" ) { result = 8 }
if ( x == "9" ) { result = 9 }
if ( x == "A" ) { result = 10 }
if ( x == "B" ) { result = 11 }
if ( x == "C" ) { result = 12 }
if ( x == "D" ) { result = 13 }
if ( x == "E" ) { result = 14 }
if ( x == "F" ) { result = 15 }
return result
}
{
# month day
ym = $3 + 3
yd = $4 + 29
# hour minute
hh = $5 + 15
mm = $6 + 48
if ( mm >= 60 ) {
mm = mm % 60
hh = hh + 1
}
if ( hh >= 24 ) {
hh = hh % 24
yd = yd + 1
}
# sun value
num = getValue(substr($10,1,1)) * 16 + getValue(substr($10,2,1))
xnum = num * 4
printf("%02d/%02d,%02d:%02d,",ym,yd,hh,mm)
printf("%s,%s,%s,%s,%s,%s,%d\n",$7,$8,$9,$11,$12,$13,xnum)
}
hb.awk
一つ前のスクリプトで、時間を補正したので
このスクリプトでは、日付を補正します。
BEGIN {
FS = ","
}
{
tarx = $2
flag = 0
if ( tarx == "23:58" ) {
pre = $1
# this record
flag = 1
}
car = substr($1,1,2)
cdr = substr($1,4,2)
result = $1
if ( pre == $1 && flag == 0 ) {
# ? odd or even
man = car % 2
# odd
if ( man == 1 ) {
cdr = (cdr+1) % 32
}
# even without Feb
if ( man == 0 && car != 2 ) {
cdr = (cdr+1) % 31
}
# Feb
if ( man == 0 && car == 2 ) {
cdr = (cdr+1) % 29
}
# generate 2 charactor
if ( cdr < 10 ) { cdr = "0" cdr }
# concatenate
result = car "/" cdr
}
printf("%s,%s,%s,%s,%s,%s,%s,%s,%d,%s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9/20,$9)
}
hc.awk
氷点下の温度が、90度以上の高温とされる
ことがあったので、補正しています。
BEGIN {
FS = ","
}
{
if ( NR == 1 ) {
printf("%s\n",$0)
} else {
x3 = $3
if ( $3 > 60 ) { x3 = $3 - 96 }
x4 = $4
if ( $4 > 60 ) { x4 = $4 - 96 }
x5 = $5
if ( $5 > 60 ) { x5 = $5 - 96 }
printf("%s,%s,%02d,%02d,%02d,%s,%s,%s,%s,%s\n",$1,$2,x3,x4,x5,$6,$7,$8,$9,$10)
}
}
CSV形式ファイルにするために、項目名をつけて
おかないと、グラフ表示がしにくいため、項目
をテキストファイルに入れて、合体できるよう
にしておきました。
テキストファイルの内容は、単純です。
温度計測のためのテキストファイルは、以下。
日付,時刻,上段,中段,下段,換算日照値,日照値
温湿度計測のためのテキストファイルは、以下。
日付,時刻,上段温度,中段温度,下段温度,上段湿度,中段湿度,下段湿度,換算日照値,日照値
目次
前
次