2

私は Perl に非常に慣れていないため、これを行う方法についてあまり考えていません。

名前の付いた.pgmファイルを読み取り、ファイルSeaWiFS_median_depth.35N.35S.180W.180E.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
    }
  }
}

ただし、私がやりたいのは、画面に印刷された値-緯度、経度、深さをテキスト(タブ区切り)またはcsvファイルに書き込んで、RやGISソフトウェアパッケージなどの別のプログラムで使用できるようにすることです。分析。

誰かがここで私を助けてくれませんか?

4

1 に答える 1

1

You can either direct the the output of the script into a file with the '>>' operator. Use it like this: perl /your/script.pl > /your/text/file.txt If you want to create a new file on the fly, use '>>'. perl /your/script.pl >> /your/text/file.txt

Your second Option is to open a Filehandle in Perl like this: open my $fh, ">>", "/yout/text/file.txt" or die "Failed to open file: $!";. Add this line below the first open statement.

This creates the file you want to write in. To print content into the file, add the filehandle ($fh) to the print statement like this: printf $fh "%6.2f\n",$depth; # depth in meters

To manipulate the delimeter of the your data, replace the \n (which is a linebreak) with the one you want (\t is for tab).

Full Perl example:

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
}
于 2015-05-19T08:26:43.820 に答える