I am very new to Perl and have not much idea about how to go about doing this.
I have come across some code that reads a .pgm file named SeaWiFS_median_depth.35N.35S.180W.180E.pgm
and rescales some of the pixel values in the file into latitudes, longitudes and depths and prints them on screen when I run the script.
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
}
}
}
However, what i am want to do is to write the values printed on screen - latitude, longitude and depth to a text (tab delimited) or csv file so I can use it in another program like R or a GIS software package to do further analysis.
Can someone please help me out here please?