#!/usr/bin/perl -w use strict; # This script removes the headers of each XBT file. # Done with UNIX Power Tools in a Perl shell # The last line of the header starts with "^Depth (m)" (^ is in a # Regex meaning), so the idea is to # grep -n "^Depth (m)" -- to find the the end of the header, then # wc -- to find the total file length, then # head -- to pull off the header part, and # tail -- to pull off the nonheader part # new addition -- make info file of known format that can be loaded into matlab # lines are: year, month, day, hour, minute, second, longitude, latitude my $data_directory = $ARGV[0]; my $tempval; my $file; my @files; opendir(DIR, $data_directory); @files = grep(/\.EDF$/,readdir(DIR)); closedir(DIR); my $grepline; my @parts; my $header_length; my $file_length; my $data_length; my ($year, $month, $day, $hour, $minute, $second); my ($lon, $lat); foreach $file (@files) { # get total number of lines ($file_length) $tempval = `wc -l $data_directory$file`; @parts = split(/ /, $tempval); $file_length = $parts[0]; # get header length ($header_length) $tempval = 'grep -n "^Depth (m)" ' . $data_directory . $file; $grepline = `$tempval`; @parts = split(/:/, $grepline); $header_length = $parts[0]; # get data length $data_length = $file_length - $header_length; system("head -$header_length $data_directory$file > " . $data_directory . $file . "_hdr"); system("tail -$data_length $data_directory$file > " . $data_directory . $file . "_dat"); # parse out latitude $tempval = `grep -n Latitude $data_directory$file`; @parts = split(/:/,$tempval); $tempval = $parts[2]; @parts = split(/ /,$tempval); $tempval = $parts[1]; @parts = split(/ /, $tempval); $lat = $parts[0]; $tempval = $parts[1]; @parts = split(/[NS]/,$tempval); $lat = $lat + $parts[0]/60; # flip sign if in southern hemisphere if ($tempval !~ /S/) { $lat = - $lat; } #parse out longitude, very similar to latitude $tempval = `grep -n Longitude $data_directory$file`; @parts = split(/:/,$tempval); $tempval = $parts[2]; @parts = split(/ /,$tempval); $tempval = $parts[1]; @parts = split(/ /, $tempval); $lon = $parts[0]; $tempval = $parts[1]; @parts = split(/[WE]/,$tempval); $lon = $lon + $parts[0]/60; # flip sign if in southern hemisphere if ($tempval !~ /E/) { $lon = - $lon; } # parse out date $tempval = `grep -n Date $data_directory$file`; @parts = split(/:/,$tempval); $tempval = $parts[2]; @parts = split(/\//, $tempval); $month = $parts[0]; $month =~ s/\s+//g; $day = $parts[1]; $year = $parts[2]; $year =~ s/\s+//; # parse out time $tempval = `grep -n Time $data_directory$file`; @parts = split(/: /,$tempval); $tempval = $parts[1]; @parts = split(/:/, $tempval); $hour = $parts[0]; $hour =~ s/\s+//g; $minute = $parts[1]; $second = $parts[2]; $second =~ s/\s+//; open(FOUT, "> $data_directory$file"."_info"); print FOUT "$year\n$month\n$day\n$hour\n$minute\n$second\n"; print FOUT "$lon\n$lat\n"; close(FOUT); }