我使用$ _将格式化的响应文件中的数据读入名为response的文件中;我的perl
代码的部分内容如下:我确信在我的文件中可以找到$rname
例如:RESP.IU.BILL.00.BH2
是$rname
之一
文件,所以不要以为无法找到该文件。
#!/usr/bin/perl
$high_pass_co = 0.002;
$low_pass_co = 1;
$du = 120;
$pre_ev = 20;
$sample = 1.0;
open RES, ">response";
$rname= "RESP." . $ev;
# make response file
open (IRIS, "<$rname");
while (<IRIS>) {
print "$rname \n you are processing:\n";
chop;
print "\n $_\n\n";
if ($_ =~ /Start date:/ ) {
$firis=0;$fprint=0;
$Yst=substr($_,25,4);
$Dst=substr($_,30,3);
if ($year > $Yst) {
$firis++;
}
if ($year == $Yst && $day >= $Dst) {
$firis++;
}
}
if ($_ =~ /End date:/ ) {
if ($_ =~ /No Ending Time/) {
$Yen=`date +%Y`;
chop($Yen);
$Den=365;
if ($year < $Yen) {
$firis++;
}
if ($year == $Yen && $day <= $Den) {
$firis++;
}
} else {
$Yen=substr($_,25,4);
$Den=substr($_,30,3);
if ($year < $Yen ) {
$firis++;
}
if ($year == $Yen && $day <= $Den) {
$firis++;
}
}
}
if ($_ =~ /B053F05/ ) {$UNI=substr($_,51,25);}
if ($_ =~ /B053F07/ ) {$A0=substr($_,51,25);}
if ($_ =~ /B053F09/ ) {$NZ=substr($_,51,5);}
if ($_ =~ /B053F04/ ) {
$Sn=substr($_,51,5);
}
if ($_ =~ /B053F14/ ) {
$NP=substr($_,51,5);
$fprint = 1;
if($firis == 2 && $Sn == 1) {
if($UNI =~ /Displacement/) {$id="0";}
if($UNI =~ /Velocity/) {$id="1";}
if($UNI =~ /Acceleration/) {$id="2";}
print RES "$sname\n";
print RES " A0 normalization factor: $A0\n";
print RES " Number of zeroes: $NZ\n";
print RES " Number of poles: $NP\n";
}
}
if ($_ =~ /B053F10-13/ && $firis == 2 && $fprint == 1 && $Sn == 1) {
$tmp = substr($_,11,70);
print RES "$tmp\n";
}
if ($_ =~ /B053F15-18/ && $firis == 2 && $fprint == 1 && $Sn == 1) {
$tmp = substr($_,11,70);
print RES "$tmp\n";
}
if ($_ =~ /Sensitivity/ && $_ =~ /B058F04/) {
$SE=substr($_,51,15);
if($firis == 2) {
print RES " Sensitivity: $SE\n";
}
}
}
close(IRIS);
close(RES);
我的格式化等待读取文件如下:
# << IRIS SEED Reader, Release 5.2 >>
#
# ======== CHANNEL RESPONSE DATA ========
B050F03 Station: TIXI
B050F16 Network: IU
B052F03 Location: 00
B052F04 Channel: BH1
B052F22 Start date: 2014,264,00:00:00
B052F23 End date: 2599,365,23:59:59
# =======================================
# + +--------------------------------------------+ +
# + | Response (Poles & Zeros), TIXI ch BH1 | +
# + +--------------------------------------------+ +
#
B053F03 Transfer function type: A [Laplace Transform (Rad/sec)]
B053F04 Stage sequence number: 1
B053F05 Response in units lookup: M/S - Velocity in Meters Per Second
B053F06 Response out units lookup: V - Volts
B053F07 A0 normalization factor: 3948.58
B053F08 Normalization frequency: 0.02
B053F09 Number of zeroes: 2
B053F14 Number of poles: 4
# Complex zeroes:
# i real imag real_error imag_error
B053F10-13 0 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00
B053F10-13 1 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00
# Complex poles:
# i real imag real_error imag_error
B053F15-18 0 -1.234000E-02 1.234000E-02 0.000000E+00 0.000000E+00
B053F15-18 1 -1.234000E-02 -1.234000E-02 0.000000E+00 0.000000E+00
B053F15-18 2 -3.918000E+01 4.912000E+01 0.000000E+00 0.000000E+00
B053F15-18 3 -3.918000E+01 -4.912000E+01 0.000000E+00 0.000000E+00
#
# + +---------------------------------------+ +
# + | Channel Gain, TIXI ch BH1 | +
# + +---------------------------------------+ +
#
B058F03 Stage sequence number: 1
B058F04 Gain: 2.276000E+03
B058F05 Frequency of gain: 5.000000E-02 HZ
B058F06 Number of calibrations: 0
#
# + +-------------------------------------------+ +
# + | Response (Coefficients), TIXI ch BH1 | +
# + +-------------------------------------------+ +
#
B054F03 Transfer function type: D
B054F04 Stage sequence number: 2
B054F05 Response in units lookup: V - Volts
B054F06 Response out units lookup: COUNTS - Digital Counts
B054F07 Number of numerators: 0
B054F10 Number of denominators: 0
#
# + +------------------------------+ +
# + | Decimation, TIXI ch BH1 | +
# + +------------------------------+ +
#
B057F03 Stage sequence number: 2
B057F04 Input sample rate: 2.000000E+01
B057F05 Decimation factor: 1
B057F06 Decimation offset: 0
B057F07 Estimated delay (seconds): 0.000000E+00
B057F08 Correction applied (seconds): 0.000000E+00
#
# + +---------------------------------------+ +
# + | Channel Gain, TIXI ch BH1 | +
# + +---------------------------------------+ +
#
B058F03 Stage sequence number: 2
B058F04 Gain: 1.677720E+06
B058F05 Frequency of gain: 0.000000E+00 HZ
B058F06 Number of calibrations: 0
#
# + +-------------------------------------------+ +
# + | Response (Coefficients), TIXI ch BH1 | +
# + +-------------------------------------------+ +
#
B054F03 Transfer function type: D
B054F04 Stage sequence number: 3
B054F05 Response in units lookup: COUNTS - Digital Counts
B054F06 Response out units lookup: COUNTS - Digital Counts
B054F07 Number of numerators: 67
B054F10 Number of denominators: 0
# Numerator coefficients:
# i, coefficient, error
B054F08-09 0 -3.653420E-17 0.000000E+00
B054F08-09 1 3.674880E-08 0.000000E+00
B054F08-09 2 -4.270600E-07 0.000000E+00
B054F08-09 3 1.145020E-06 0.000000E+00
B054F08-09 4 -1.875940E-07 0.000000E+00
B054F08-09 5 -3.372740E-07 0.000000E+00
B054F08-09 6 2.787470E-06 0.000000E+00
B054F08-09 7 -3.744030E-06 0.000000E+00
B054F08-09 8 5.411720E-06 0.000000E+00
B054F08-09 9 7.473360E-06 0.000000E+00
B054F08-09 10 -5.177590E-04 0.000000E+00
B054F08-09 11 2.106770E-04 0.000000E+00
B054F08-09 12 4.632580E-05 0.000000E+00
B054F08-09 13 -6.082220E-04 0.000000E+00
B054F08-09 14 1.441750E-03 0.000000E+00
B054F08-09 15 -2.406270E-03 0.000000E+00
B054F08-09 16 3.225340E-03 0.000000E+00
B054F08-09 17 -3.506390E-03 0.000000E+00
B054F08-09 18 2.814410E-03 0.000000E+00
B054F08-09 19 -7.719710E-04 0.000000E+00
B054F08-09 20 -2.805120E-03 0.000000E+00
B054F08-09 21 7.778050E-03 0.000000E+00
B054F08-09 22 -1.358150E-02 0.000000E+00
B054F08-09 23 1.917650E-02 0.000000E+00
B054F08-09 24 -2.297040E-02 0.000000E+00
B054F08-09 25 2.403980E-02 0.000000E+00
B054F08-09 26 -2.209860E-02 0.000000E+00
B054F08-09 27 8.607340E-03 0.000000E+00
B054F08-09 28 1.175250E-02 0.000000E+00
B054F08-09 29 -4.477870E-02 0.000000E+00
B054F08-09 30 9.649230E-02 0.000000E+00
B054F08-09 31 -1.917550E-01 0.000000E+00
B054F08-09 32 5.276520E-01 0.000000E+00
B054F08-09 33 7.241670E-01 0.000000E+00
B054F08-09 34 -1.569050E-01 0.000000E+00
B054F08-09 35 4.425740E-02 0.000000E+00
B054F08-09 36 3.141680E-03 0.000000E+00
B054F08-09 37 -2.667140E-02 0.000000E+00
B054F08-09 38 3.615320E-02 0.000000E+00
B054F08-09 39 -3.856870E-02 0.000000E+00
B054F08-09 40 3.108420E-02 0.000000E+00
B054F08-09 41 -2.352590E-02 0.000000E+00
B054F08-09 42 1.532110E-02 0.000000E+00
B054F08-09 43 -7.403980E-03 0.000000E+00
B054F08-09 44 1.096450E-03 0.000000E+00
B054F08-09 45 3.097970E-03 0.000000E+00
B054F08-09 46 -5.193200E-03 0.000000E+00
B054F08-09 47 5.561310E-03 0.000000E+00
B054F08-09 48 -4.761100E-03 0.000000E+00
B054F08-09 49 3.382130E-03 0.000000E+00
B054F08-09 50 -1.920520E-03 0.000000E+00
B054F08-09 51 7.152180E-04 0.000000E+00
B054F08-09 52 7.677190E-05 0.000000E+00
B054F08-09 53 -4.518970E-04 0.000000E+00
B054F08-09 54 5.027000E-04 0.000000E+00
B054F08-09 55 -5.650370E-04 0.000000E+00
B054F08-09 56 -5.568000E-05 0.000000E+00
B054F08-09 57 1.577360E-05 0.000000E+00
B054F08-09 58 -1.419850E-06 0.000000E+00
B054F08-09 59 8.149090E-07 0.000000E+00
B054F08-09 60 6.807950E-07 0.000000E+00
B054F08-09 61 -1.252730E-06 0.000000E+00
B054F08-09 62 1.524350E-06 0.000000E+00
B054F08-09 63 -2.833360E-07 0.000000E+00
B054F08-09 64 -1.063840E-08 0.000000E+00
B054F08-09 65 1.257120E-09 0.000000E+00
B054F08-09 66 -5.429540E-11 0.000000E+00
#
# + +------------------------------+ +
# + | Decimation, TIXI ch BH1 | +
# + +------------------------------+ +
#
B057F03 Stage sequence number: 3
B057F04 Input sample rate: 2.000000E+01
B057F05 Decimation factor: 1
B057F06 Decimation offset: 0
B057F07 Estimated delay (seconds): 1.630500E+00
B057F08 Correction applied (seconds): 1.630500E+00
#
# + +---------------------------------------+ +
# + | Channel Gain, TIXI ch BH1 | +
# + +---------------------------------------+ +
#
B058F03 Stage sequence number: 3
B058F04 Gain: 1.000000E+00
B058F05 Frequency of gain: 0.000000E+00 HZ
B058F06 Number of calibrations: 0
#
# + +---------------------------------------+ +
# + | Channel Sensitivity, TIXI ch BH1 | +
# + +---------------------------------------+ +
#
但是我的输出文件是空的,我的代码在哪里错了?
答案 0 :(得分:4)
我确信在我的文件中可以找到
$rname
,例如:RESP.IU.BILL.00.BH2
是$rname
文件之一,所以不要费心去认为该文件不能被发现。
如果你说文件系统中存在RESP.IU.BILL.00.BH2
,那么我相信你。但我不认为你正在寻找那个档案。
这是打开输入文件的行:
open (IRIS, "<$rname");
这是您声明变量$rname
:
$rname= "RESP." . $ev;
但是你不能在任何地方声明变量$ev
。所以你的程序正在寻找一个名为RESP.
的文件 - 带有一个空的扩展名。我猜这个文件不存在,因此您对open()
的调用无声地失败,您的while (<IRIS>)
循环永远不会被执行。
一些可以解决这个问题的建议。
use strict
会引发错误。当然,这也意味着您现在需要声明所有变量。这是一个非常好的主意。检查您对open()
的通话结果总是一个好主意
open (IRIS, "<$rname")
or die "Couldn't open $rname: $!";
use warnings
读取时,向您的代码添加IRIS
会发出警告(因为您正试图从未成功打开的文件句柄中读取) )。还有一些建议。
现在,我们使用{arner-version {3}}和词法变量作为文件句柄。
open()
如果您正在阅读文件并撰写另一个文件,那么将其作为&#34; Unix过滤器&#34;进行编写通常是一个好主意。 - 你从STDIN读取并写入STDOUT。这使您的程序更加灵活(可以与其他程序链接在一起),而且实际上更容易编写。
open my $iris_fh, '<', $rname or die(...);
通常比chomp()
更安全。
请更加注意代码的缩进。缩进应该使代码更容易理解。
如果您与chop()
匹配,那么您只需撰写$_
而不是/.../
。
答案 1 :(得分:3)
不仅$ev
而且$year
和$day
未定义。您还混淆了$year
,$Yst
和$Yen
是来自文件还是当前日期。这不会起作用
您必须先将use strict
和use warnings 'all'
添加到程序的顶部,然后使用my
声明所有变量。这将为您提供更好的工作机会