在Perl中将像素值从pgm文件写入带有纬度、经度和深度的文本文件
我对Perl非常陌生,对如何进行这项工作不太了解 我遇到了一些代码,它读取名为在Perl中将像素值从pgm文件写入带有纬度、经度和深度的文本文件,perl,pgm,Perl,Pgm,我对Perl非常陌生,对如何进行这项工作不太了解 我遇到了一些代码,它读取名为sewaifs_median_depth.35N.35S.180W.180E.pgm的.pgm文件,并将文件中的一些像素值重新缩放为纬度、经度和深度,并在运行脚本时在屏幕上打印它们 my $depth_file = 'SeaWiFS_median_depth.35N.35S.180W.180E.pgm'; open F,$depth_file or die "Could not open $depth_file ($
sewaifs_median_depth.35N.35S.180W.180E.pgm的.pgm文件,并将文件中的一些像素值重新缩放为纬度、经度和深度,并在运行脚本时在屏幕上打印它们
my $depth_file = 'SeaWiFS_median_depth.35N.35S.180W.180E.pgm';
open F,$depth_file or die "Could not open $depth_file ($!)";
<F>; # skip header line
my($wid,$hgt) = split ' ',scalar <F>;
<F>; # skip another header line
$wid == 36000 or die "Unexpected width in depth file\n";
$hgt == 7000 or die "Unexpected height in depth file\n";
my $inc = 360/$wid;
my $log500 = log(100/0.2);
for($lat = 35 - $inc/2; $lat > -35; $lat -= $inc){
for($lon = -180 + $inc/2; $lon < 180; $lon += $inc){
read(F,$pixel,1) == 1 or die "Error reading $depth_file ($!)";
$pixel = unpack "C",$pixel;
printf "%7.3f %8.3f ",$lat,$lon;
if($pixel == 0){
print "no data\n";
}
elsif($pixel == 1){
print "land\n";
}
elsif($pixel == 255){
print "cloud or other masking condition\n";
}
else{
$depth = 0.2*exp($log500*($pixel - 2)/252);
printf "%6.2f\n",$depth; # depth in meters
}
}
}
my$depth_file='seavifs_median_depth.35N.35S.180W.180E.pgm';
打开F、$depth_文件或模具“无法打开$depth_文件($!)”;
; # 跳过标题行
my($wid,$hgt)=拆分“”,标量;
; # 跳过另一个标题行
$wid==36000或die“深度文件中的意外宽度\n”;
$hgt==7000或“深度文件中的意外高度\n”;
my$inc=360/$wid;
my$log500=对数(100/0.2);
对于($lat=35-$inc/2;$lat>-35;$lat-=$inc){
对于($lon=-180+$inc/2;$lon<180;$lon+=$inc){
读取(F,$pixel,1)==1或“读取$depth_文件($!)时出错”;
$pixel=解包“C”,即$pixel;
printf“%7.3f%8.3f”,$lat,$lon;
如果($pixel==0){
打印“无数据\n”;
}
elsif($pixel==1){
打印“土地”\n;
}
elsif($pixel==255){
打印“云或其他掩蔽条件\n”;
}
否则{
$depth=0.2*exp($log500*($pixel-2)/252);
printf“%6.2f\n”,$depth;#以米为单位的深度
}
}
}
然而,我想做的是将屏幕上打印的值-纬度、经度和深度写入文本(制表符分隔)或csv文件,以便我可以在另一个程序(如R或GIS软件包)中使用它进行进一步分析
有人能帮我一下吗?您可以将脚本的输出直接导入到一个文件中
使用“>>”操作符。
像这样使用:perl/your/script.pl>/your/text/file.txt
如果要动态创建新文件,请使用“>>”。
perl/your/script.pl>/your/text/file.txt
第二个选项是在Perl中打开文件句柄,如下所示:
打开我的$fh,“>>”、“/yout/text/file.txt”或“无法打开文件:$!”代码>。
在第一个open
语句下方添加此行
这将创建要写入的文件。
要将内容打印到文件中,请添加文件句柄($fh
)
要打印语句,请执行以下操作:
printf$fh”%6.2f\n“,$depth;#深度(米)
要操作数据的delimeter,请替换\n
(即
换行符)与所需换行符(\t
用于制表符)
完整的Perl示例:
my $depth_file = 'SeaWiFS_median_depth.35N.35S.180W.180E.pgm';
open F,$depth_file or die "Could not open $depth_file ($!)";
open my $fh, ">>", "/yout/text/file.txt" or die "Failed to open file: $!";
<F>; # skip header line
my($wid,$hgt) = split ' ',scalar <F>;
<F>; # skip another header line
$wid == 36000 or die "Unexpected width in depth file\n";
$hgt == 7000 or die "Unexpected height in depth file\n";
my $inc = 360/$wid;
my $log500 = log(100/0.2);
for($lat = 35 - $inc/2; $lat > -35; $lat -= $inc){
for($lon = -180 + $inc/2; $lon < 180; $lon += $inc){
read(F,$pixel,1) == 1 or die "Error reading $depth_file ($!)";
$pixel = unpack "C",$pixel;
printf "%7.3f %8.3f ",$lat,$lon;
if($pixel == 0){
print "no data\t";
}
elsif($pixel == 1){
print "land\t";
}
elsif($pixel == 255){
print "cloud or other masking condition\t";
}
else{
$depth = 0.2*exp($log500*($pixel - 2)/252);
printf $fh "%6.2f\t",$depth; # depth in meters
}
}
print $fh, "\n"; # new line after each line of the image
}
my$depth_file='seavifs_median_depth.35N.35S.180W.180E.pgm';
打开F、$depth_文件或模具“无法打开$depth_文件($!)”;
打开我的$fh、“>>”、“/yout/text/file.txt”或“无法打开文件:$!”;
; # 跳过标题行
my($wid,$hgt)=拆分“”,标量;
; # 跳过另一个标题行
$wid==36000或die“深度文件中的意外宽度\n”;
$hgt==7000或“深度文件中的意外高度\n”;
my$inc=360/$wid;
my$log500=对数(100/0.2);
对于($lat=35-$inc/2;$lat>-35;$lat-=$inc){
对于($lon=-180+$inc/2;$lon<180;$lon+=$inc){
读取(F,$pixel,1)==1或“读取$depth_文件($!)时出错”;
$pixel=解包“C”,即$pixel;
printf“%7.3f%8.3f”,$lat,$lon;
如果($pixel==0){
打印“无数据\t”;
}
elsif($pixel==1){
打印“land\t”;
}
elsif($pixel==255){
打印“云或其他掩蔽条件\t”;
}
否则{
$depth=0.2*exp($log500*($pixel-2)/252);
printf$fh“%6.2f\t”,$depth;#以米为单位的深度
}
}
打印$fh,“\n”#图像每行后的新行
}
/path/to/script>/path/to/output/file.txt
谢谢@ThisSuitesBlacknot