#!/usr/bin/perl


$datacnv = "/cygdrive/c/Program\\ Files/Sea-Bird/SBEDataProcessing-Win32/DatCnvW.exe";
$BaseDir = "/cygdrive/u/MOOS Vertical Profiling/SSDS_Downloads";
$conFile = "4491-homegrown.con";
$psaFile = "DatCnv.psa";
$outDir = "SeaHorseFiles";


open(HEXFILES, "ls $outDir/*.HEX|");
    while (<HEXFILES>){
    ($HexFilenm) = split;
    print STDOUT "Converting: ".$HexFilenm."\n";
    #Strip blank lines from .hex files (Not sure how they get in there, but anyway)
    # There must be a better way to do this in perl.
    open(STRIP,"mv $HexFilenm tempfile; grep '.'  tempfile > $HexFilenm|");
    <STRIP>;
    close(STRIP);

    $CnvCmd = "$datacnv /p$psaFile /c$conFile /i$HexFilenm /o$outDir /s /m ";
    print STDOUT $CnvCmd."\n";
    open(CONVERT, "$CnvCmd 2> /dev/null |");
    <CONVERT>;
    close(CONVERT);
    }
close(HEXFILES);


# Modify # comments to *# comments in all .cnv files for ease of reading into matlab
open(CNVFILES, "ls $outDir/*.cnv|");
    while (<CNVFILES>){
      ($CnvFilenm) = split;
      open(RECOMMENT, "mv $CnvFilenm tempfile; cat tempfile | sed  's/^#/*#/' > $CnvFilenm|");
      <RECOMMENT>;
      close(RECOMMENT);
  }
close(CNVFILES);
