//public void ComputeRadial(object sender, DoWorkEventArgs e, string BellhopConfiguration, string BottomProfile, float Bearing)

        //todo: put below in a class in esme.tl.bellhop.radialcalculator; examine what's in bellhop now and prune 
        public static TransmissionLossRadial ComputeRadial(string bellhopConfiguration, string bottomProfile, float bearing)
        {
            //ImprovedBackgroundWorker bw;

            //if ((sender == null))
            //    return;
            //bw = (ImprovedBackgroundWorker)sender;
            //System.Diagnostics.Debug.WriteLine("Worker: Beginning computation of " + bw.TaskName);

            //bw.WorkerReportsProgress = true;
            //bw.WorkerSupportsCancellation = true;

            var workingDirectory = Path.Combine(Path.GetTempPath(),
                                                   Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

            Directory.CreateDirectory(workingDirectory);

            // Write the bottom profile file that will be read by BELLHOP
            using (var writer = new StreamWriter(Path.Combine(workingDirectory, "BTYFIL")))
                writer.Write(bottomProfile);

            // Create a process, run BELLHOP, and exit
            //BellhopProcess = new TLProcess(bw);
            var bellhopProcess = new TLProcess
                                     {
                                         StartInfo = new ProcessStartInfo(
                                             Path.Combine(
                                                 Path.GetDirectoryName(
                                                     System.Reflection.Assembly.GetCallingAssembly().Location),
                                                 "Bellhop.exe"))
                                                         {
                                                             CreateNoWindow = true,
                                                             UseShellExecute = false,
                                                             RedirectStandardInput = true,
                                                             RedirectStandardOutput = true,
                                                             RedirectStandardError = true,
                                                             WorkingDirectory = workingDirectory
                                                         }
                                     };

            bellhopProcess.OutputDataReceived += OutputDataRecieved;
            _mBellhopOutputData = new StringBuilder();
            bellhopProcess.Start();
            bellhopProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
            bellhopProcess.StandardInput.WriteLine(bellhopConfiguration);
            bellhopProcess.BeginOutputReadLine();
            while (!bellhopProcess.HasExited)
            {
                Thread.Sleep(100);
#if false
                if (bw.CancellationPending)
                {
                    BellhopProcess.Kill();
                    ErrorOutput = BellhopProcess.StandardError.ReadToEnd();
                    while (!BellhopProcess.HasExited)
                        Thread.Sleep(100);
                    if (File.Exists(Path.Combine(WorkingDirectory, "BYTFIL")))
                        File.Delete(Path.Combine(WorkingDirectory, "BTYFIL"));
                    if (File.Exists(Path.Combine(WorkingDirectory, "SHDFIL")))
                        File.Delete(Path.Combine(WorkingDirectory, "SHDFIL"));
                    if (File.Exists(Path.Combine(WorkingDirectory, "ARRFIL")))
                        File.Delete(Path.Combine(WorkingDirectory, "ARRFIL"));
                    if (Directory.Exists(WorkingDirectory))
                        Directory.Delete(WorkingDirectory, true);
                    return;
                }
                
#endif               // mProgress_percent = BellhopProcess.ProgressPercent;
            }
            // mProgress_percent = BellhopProcess.ProgressPercent;
            bellhopProcess.StandardError.ReadToEnd();
            //if (BellhopOutputData.ToString() != "")
            //    MessageBox.Show(BellhopOutputData.ToString());
            //System.Diagnostics.Debug.WriteLine("BELLHOP Exit Code: " + BellhopProcess.ExitCode);

            // We don't need to keep the results files around anymore, we're finished with them
            File.Delete(Path.Combine(workingDirectory, "BTYFIL"));
            foreach (var s in Directory.GetFiles(workingDirectory, "ARRFIL_*"))
                File.Delete(s);
            //if (File.Exists(Path.Combine(WorkingDirectory, "ARRFIL")))
            //    File.Move(Path.Combine(WorkingDirectory, "ARRFIL"), "ARRFIL_" + TLParams.RadialNumber.ToString("00"));

            // Convert the Bellhop output file into a Radial binary file
            var result = new TransmissionLossRadial(bearing,
                new BellhopOutput(Path.Combine(workingDirectory, "SHDFIL")));

#if false
            
            TLParams.SoundSource.TransmissionLossVertical[TLParams.RadialNumber] =
                new TransmissionLossVertical(
                    ReaderBellhopOutput.ReadBellhop(
                        Path.Combine(WorkingDirectory, "SHDFIL")),
                        TLParams.BottomProfile,
                        TLParams.Experiment,
                        TLParams.SoundSource,
                        TLParams.SSPLocation,
                        TLParams.RadialNumber);
#endif
            File.Delete(Path.Combine(workingDirectory, "SHDFIL"));
            //bw.ReportProgress(100);
            Directory.Delete(workingDirectory, true);
            return result;
            //System.Diagnostics.Debug.WriteLine("Worker: Completed computation of " + bw.TaskName);
        }
        public void Compute(object sender, DoWorkEventArgs e)
        {
            ImprovedBackgroundWorker bw;
            TLProcess BellhopProcess;
            string ErrorOutput, WorkingDirectory;

            IsComputing = true;
            CalculationStarted = DateTime.Now;
            if ((sender == null))
                return;
            bw = (ImprovedBackgroundWorker)sender;
            //System.Diagnostics.Debug.WriteLine("Worker: Beginning computation of " + bw.TaskName);

            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;

            WorkingDirectory = Path.Combine(Path.GetTempPath(),
                Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

            Directory.CreateDirectory(WorkingDirectory);

            // Write the bottom profile file that will be read by BELLHOP
            using (StreamWriter writer = new StreamWriter(Path.Combine(WorkingDirectory, "BTYFIL")))
                writer.Write(BottomProfile);

            // Create a process, run BELLHOP, and exit
            BellhopProcess = new TLProcess(bw);
            BellhopProcess.StartInfo = new ProcessStartInfo(
                Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().Location), "Bellhop.exe"));

            BellhopProcess.StartInfo.CreateNoWindow = true;
            BellhopProcess.StartInfo.UseShellExecute = false;
            BellhopProcess.StartInfo.RedirectStandardInput = true;
            BellhopProcess.StartInfo.RedirectStandardOutput = true;
            BellhopProcess.StartInfo.RedirectStandardError = true;
            BellhopProcess.StartInfo.WorkingDirectory = WorkingDirectory;
            BellhopProcess.OutputDataReceived += new DataReceivedEventHandler(OutputDataRecieved);
            mBellhopOutputData = new StringBuilder();
            BellhopProcess.Start();
            BellhopProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
            BellhopProcess.StandardInput.WriteLine(BellhopConfiguration);
            BellhopProcess.BeginOutputReadLine();
            while (!BellhopProcess.HasExited)
            {
                Thread.Sleep(100);
                if (bw.CancellationPending)
                {
                    BellhopProcess.Kill();
                    ErrorOutput = BellhopProcess.StandardError.ReadToEnd();
                    while (!BellhopProcess.HasExited)
                        Thread.Sleep(100);
                    if (File.Exists(Path.Combine(WorkingDirectory, "BYTFIL")))
                        File.Delete(Path.Combine(WorkingDirectory, "BTYFIL"));
                    if (File.Exists(Path.Combine(WorkingDirectory, "SHDFIL")))
                        File.Delete(Path.Combine(WorkingDirectory, "SHDFIL"));
                    if (File.Exists(Path.Combine(WorkingDirectory, "ARRFIL")))
                        File.Delete(Path.Combine(WorkingDirectory, "ARRFIL"));
                    if (Directory.Exists(WorkingDirectory))
                        Directory.Delete(WorkingDirectory, true);
                    return;
                }
                mProgress_percent = BellhopProcess.ProgressPercent;
            }
            mProgress_percent = BellhopProcess.ProgressPercent;
            ErrorOutput = BellhopProcess.StandardError.ReadToEnd();
            //if (BellhopOutputData.ToString() != "")
            //    MessageBox.Show(BellhopOutputData.ToString());
            //System.Diagnostics.Debug.WriteLine("BELLHOP Exit Code: " + BellhopProcess.ExitCode);

            // We don't need to keep the results files around anymore, we're finished with them
            File.Delete(Path.Combine(WorkingDirectory, "BTYFIL"));
            foreach (string s in Directory.GetFiles(WorkingDirectory, "ARRFIL_*"))
                File.Delete(s);
            //if (File.Exists(Path.Combine(WorkingDirectory, "ARRFIL")))
            //    File.Move(Path.Combine(WorkingDirectory, "ARRFIL"), "ARRFIL_" + TLParams.RadialNumber.ToString("00"));

            // TODO: Convert the Bellhop output file into a Radial binary file
            RadialData = new RadialData(BearingFromSource_degrees, 
                new BellhopOutput(Path.Combine(WorkingDirectory, "SHDFIL")));

#if false
            
            TLParams.SoundSource.TransmissionLossVertical[TLParams.RadialNumber] =
                new TransmissionLossVertical(
                    ReaderBellhopOutput.ReadBellhop(
                        Path.Combine(WorkingDirectory, "SHDFIL")),
                        TLParams.BottomProfile,
                        TLParams.Experiment,
                        TLParams.SoundSource,
                        TLParams.SSPLocation,
                        TLParams.RadialNumber);
#endif
            File.Delete(Path.Combine(WorkingDirectory, "SHDFIL"));
            bw.ReportProgress(100);
            Directory.Delete(WorkingDirectory, true);
            //System.Diagnostics.Debug.WriteLine("Worker: Completed computation of " + bw.TaskName);
        }