using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace CPFtoKML { public partial class Form1 : Form { private struct Position { public String timeStamp; public double lat; public double lon; public double elevation; public string profileNum; } private const int QDepth = 256; private static Position[] cpfPositions = new Position[256]; public Form1() { InitializeComponent(); } private static StreamReader sbdFile; private static void readSBDs() { Console.WriteLine("Starting read SBDs"); string[] files = Directory.GetFiles(selectedPath, "*.sbd"); for (int i = 0; i < files.Length; i++) { sbdFile = new StreamReader(files[i]); string inLine = sbdFile.ReadLine(); string[] fields = inLine.Split(','); int index = int.Parse(parseProfileNum(fields[22])); cpfPositions[index].profileNum = index.ToString(); cpfPositions[index].timeStamp = fields[0]; cpfPositions[index].lat = parseLat(fields[2], fields[3]); cpfPositions[index].lon = parseLon(fields[5], fields[6]); cpfPositions[index].elevation = 0.0; } } private static string[] of = new string[]{"of"}; private static string parseProfileNum(string inString) { string[] fields = inString.Split(of, StringSplitOptions.None); return (fields[0]); } private static double parseLat(string inLat, string inNS) { int decPointIndex; double min; double lat; decPointIndex = inLat.IndexOf('.'); min = double.Parse(inLat.Substring(decPointIndex - 2)); lat = double.Parse(inLat.Substring(0, decPointIndex - 2)); lat = lat + (min / 60.0); if (String.Compare("S", inNS) == 0) lat = -1.0 * lat; return(lat); } private static double parseLon(string inLon, string inEW) { int decPointIndex; double min; double lon; decPointIndex = inLon.IndexOf('.'); min = double.Parse(inLon.Substring(decPointIndex - 2)); lon = double.Parse(inLon.Substring(0, decPointIndex - 2)); lon = lon + (min / 60.0); if (String.Compare("W", inEW) == 0) lon = -1.0 * lon; return (lon); } private static StreamWriter kmlFile; public static void writeKMLTrack() { Console.WriteLine("Starting writeKML"); kmlFile = new StreamWriter(selectedPath + @"\CPFTrack.kml"); kmlFile.WriteLine(@""); kmlFile.WriteLine(@""); kmlFile.WriteLine(""); kmlFile.WriteLine(" "); kmlFile.WriteLine(" CPF Track"); kmlFile.WriteLine(" "); kmlFile.WriteLine(" "); kmlFile.WriteLine(" absolute"); foreach (Position value in cpfPositions) { if (value.timeStamp != null) { kmlFile.WriteLine(" 0.0"); kmlFile.WriteLine(" " + value.timeStamp + ""); kmlFile.WriteLine(" " + value.lon.ToString("f6") + " " + value.lat.ToString("f6") + " " + "0.0" + ""); } } kmlFile.WriteLine(" "); kmlFile.WriteLine(" "); foreach (Position value in cpfPositions) { if (value.timeStamp != null) { kmlFile.WriteLine(" "); if(int.Parse(value.profileNum) == 0) kmlFile.WriteLine(" Deploy" + ""); else kmlFile.WriteLine(" P-" + value.profileNum + ""); kmlFile.WriteLine(" "); kmlFile.WriteLine(" " + value.lon.ToString("f6") + " " + value.lat.ToString("f6") + " " + "0.0" + ""); kmlFile.WriteLine(" "); kmlFile.WriteLine(" "); } } kmlFile.WriteLine(""); kmlFile.WriteLine(""); kmlFile.Close(); Console.WriteLine("Finished writeKML"); } private void showData() { StringBuilder sbText = new StringBuilder(16000); int index = 1; sbText.Clear(); sbText.Append("SBD\tProfile Num\tDate Time\t\t\tLatitude\tLongitude\r\n"); foreach (Position value in cpfPositions) { if (value.timeStamp != null) { sbText.Append(index.ToString() + "\t\t" + value.profileNum + "\t" + value.timeStamp + "\t\t" + value.lat.ToString("F6") + "\t" + value.lon.ToString("F6") + "\r\n"); index++; } } rtbDataTable.Text = sbText.ToString(); } private static string selectedPath; private void Form1_Shown(object sender, EventArgs e) { folderBrowserDialog1.SelectedPath = @"\\atlas.shore.mbari.org\ProjectLibrary\CPF\Operations\2017Mar09"; selectedPath = folderBrowserDialog1.SelectedPath; tbPath.Text = selectedPath; readSBDs(); writeKMLTrack(); showData(); //Application.Exit(); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } }