- 论坛徽章:
- 0
|
因为工作需要,使用一个perl脚本,下载noaa的气象数据,但应用中需要将文件更名为时间、层序和元素命名的文件,由于以前没有接触过perl,求助各位大大!
具体代码如下:
if ($action ne 'data' && $action ne 'inv') {
print "action must be inv or data, not $action\n";
exit( ;
}
if ($YYYY < 2006 || $YYYY > 2030) {
print "bad date (year) code $time\n";
exit( ;
}
if ($MM < 1 || $MM > 12) {
print "bad date (month) code $time\n";
exit( ;
}
if ($DD < 1 || $DD > 31) {
print "bad date (day) code $time\n";
exit( ;
}
if ($HH < 0 || $HH > 23) {
print "bad date (hour) code $time\n";
exit( ;
}
if ($hr0 == $hr1) {
$dhr = 3;
}
if ($dhr != 3 && $dhr != 6 && $dhr != 12 && $dhr != 24) {
print "dhr must be 3, 6, 12 or 24, not $dhr\n";
exit( ;
}
if ($hr0 > $hr1) {
print "hr0 needs to be <= hr1\n";
exit( ;
}
if ($dhr <= 0) {
print "dhr needs to be > 0\n";
exit( ;
}
if (! -d $OUTDIR && $action ne 'inv') {
print "Directory $OUTDIR does not exist\n";
exit( ;
}
$VARS =~ tr/:_/| /;
if( $VARS =~ m/ALL/ig ) { $VARS = "."; }
else { $VARS = " $VARS):"; }
$LEVS =~ tr/:_/| /;
if( $LEVS =~ m/ALL/ig ) { $LEVS = "."; }
else { $LEVS = " $LEVS)" ; }
$URL =~ s/\$YYYY/$YYYY/g;
$URL =~ s/\${YYYY}/$YYYY/g;
$URL =~ s/\$MM/$MM/g;
$URL =~ s/\${MM}/$MM/g;
$URL =~ s/\$DD/$DD/g;
$URL =~ s/\${DD}/$DD/g;
$URL =~ s/\$HH/$HH/g;
$URL =~ s/\${HH}/$HH/g;
$output = '';
$fhr=$hr0;
while ($fhr <= $hr1) {
if ($fhr <= 9) { $fhr="0$fhr"; }
$fhr3=$fhr;
if ($fhr <= 99) { $fhr3="0$fhr"; }
$url = $URL;
$url =~ s/\$FHR3/$fhr3/g;
$url =~ s/\${FHR3}/$fhr3/g;
$url =~ s/\$FHR/$fhr/g;
$url =~ s/\${FHR}/$fhr/g;
$file = $url;
$file =~ s/^.*\///;
#
# read the inventory
# $line[] = wgrib inventory, $start[] = start of record (column two of $line[])
#
if ($windows eq 'yes') {
$err = system("$curl -f -s $url$inv -o $OUTDIR/$file.tmp" ;
$err = $err >> 8;
if ($err) {
print STDERR "error code=$err, problem reading $url$inv\n";
sleep(10);
exit( ;
}
open (In, "$OUTDIR/$file.tmp" ;
}
else {
open (In, "$curl -f -s $url$inv |" ;
}
$n=0;
while (<In> {
chomp;
$line[$n] = $_;
s/^[^:]*://;
s/:.*//;
$start[$n] = $_;
$n++;
}
close(In);
if ($n == 0) {
print STDERR " roblem reading file $url$inv\n";
sleep(10);
exit(8);
}
#
# find end of record: $last[]
#
$lastnum = $start[$n-1];
for ($i = 0; $i < $n; $i++) {
$num = $start[$i];
if ($num < $lastnum) {
$j = $i + 1;
while ($start[$j] == $num) { $j++; }
$last[$i] = $start[$j] - 1;
}
else {
$last[$i] = '';
}
}
if ($action eq 'inv') {
for ($i = 0; $i < $n; $i++) {
print "$line[$i]:range=$start[$i]-$last[$i]\n";
}
exit(0);
}
#
# make the range field for Curl
#
$range = '';
$lastfrom = '';
$lastto = '-100';
for ($i = 0; $i < $n; $i++) {
$_ = $line[$i];
if (/$LEVS/i && /$VARS/i) {
$from=$start[$i];
$to=$last[$i];
if ($lastto + 1 == $from) {
$lastto = $to;
}
elsif ($lastto ne $to) {
if ($lastfrom ne '') {
if ($range eq '') { $range = "$lastfrom-$lastto"; }
else { $range = "$range,$lastfrom-$lastto"; }
}
$lastfrom = $from;
$lastto = $to;
}
}
}
if ($lastfrom ne '') {
if ($range eq '') { $range="$lastfrom-$lastto"; }
else { $range="$range,$lastfrom-$lastto"; }
}
if ($range ne '') {
$err = system("$curl -f -v -s -r \"$range\" $url$grb -o $OUTDIR/$file.tmp" ;
$err = $err >> 8;
if ($err != 0) {
print STDERR "error in getting file $err $url$grb\n";
sleep(20);
exit $err;
}
rename "$OUTDIR/$file.tmp", "$OUTDIR/$file";
$output = "$output $OUTDIR/$file $VARS";
}
else {
print "no matches (no download) for $file\n";
}
$fhr += $dhr;
}
print "\n\nfinished download\n\n$output $VARS\n";
exit(0); |
|