/// <summary> /// checks that the open file dialog compelted succesfully. if it did thenthe first 5 lines are populated into the preview window. /// if not show a messagebox to the user showing that no file was selected /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ChooseCSVButton_Click(object sender, EventArgs e) { string CSVFile = string.Empty; //checks to see if the open file dialogue was successful if (openCSVFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { //local variable for storing the filepath and name CSVFile = openCSVFileDialog.FileName; //instantiates a stream reader to preview the file contents System.IO.StreamReader sr = new System.IO.StreamReader(openCSVFileDialog.FileName); FilePreviewTextBox.Clear(); FilePreviewTextBox.Text += " "; for (int i = 0; i < sr.ReadLine().Split(',').Length; i++) { FilePreviewTextBox.Text += "Col" + (i + 1); FilePreviewTextBox.Text += " "; } FilePreviewTextBox.AppendText(Environment.NewLine); sr.BaseStream.Position = 0; //populates the sample data window with the first 4 lines from the file for (int i = 0; i < 4; i++) { var line = sr.ReadLine(); var values = line.Split(','); int columnCount = line.Split(',').Length; FilePreviewTextBox.Text += "Row "; FilePreviewTextBox.Text += i; FilePreviewTextBox.Text += " | "; //internal for loop that loops through each line adding a tab for each column in the file for (int k = 0; k < columnCount; k++) { FilePreviewTextBox.Text += values[k]; FilePreviewTextBox.Text += " "; } FilePreviewTextBox.AppendText(Environment.NewLine); } //closes the streamreader sr.Close(); fullFilePath = openCSVFileDialog.FileName; } else { MessageBox.Show("No file selected!"); } fullFilePath = CSVFile; }
/// <summary> /// computes all the values needed for each observation and then adds them to a list. /// populates the information from eachobservation into th preview textbox /// validate data MUST be run prior to this as this does not check for valid data /// once averages are computed they are populated to the results textboxes /// </summary> public void ComputeValues() { //creates the lists for the various items for each observation and the counter to display how many observations were used List <string> timeStamp = new List <string>(); List <double> observedVesselHDG = new List <double>(); List <double> observedROVHDG = new List <double>(); List <double> computedROVHDG = new List <double>(); List <double> cMinusO = new List <double>(); int ROVHDGColumn = Convert.ToInt32(ROVHDGColumnTextBox.Text); int VesselHDGColumn = Convert.ToInt32(vesselHDGColumnTextBox.Text); int observationCounter = 0; int errorCounter = 0; int rowsToSkip = 0; //creates a double that will hold the average C-O value double averageCMinusO; //creates a double that will hold the average vessel heading double averageObservedVesselHDG = 0; //creates a doubld that will hold the average observed ROV HDG double averageObservedROVHDG = 0; //creates a double that will hold the average computed ROV HDG double averageComputedROVHDG = 0; //creates a double and sets its value to the ROV Fwd measurement textbox double ROVFwdMeasurement = Convert.ToDouble(ROVFwdMeasurementTextBox.Text); //creates a double and sets its value to the ROV aft measurement textbox double ROVAftMeasurement = Convert.ToDouble(ROVAftMeasurementTextBox.Text); //creates a double and sets its value to the ROV aft measurement textbox double BaselineMeasurement = Convert.ToDouble(BaselineMeasurementTextBox.Text); //a double that represents the side opposite the c-o angle double oppositeSide; //a double that represents the relative c-o between the reference and the ROV double ROVToReference; //a souble that represents the c-o between the ROV and the vessel heading double ROVToVessel; //double that represents the tangent value double tangent; //creates two enums whose value will be set by if statements later ROVDirection rOVDirection; ReferenceToROV referenceToROV; //empties the preview textbox in preparation for outputing the observations to it in the while loop FilePreviewTextBox.Clear(); //if statements that set the ROV direction value based on the radio button selected if (ROVFwdRadioButton.Checked) { rOVDirection = ROVDirection.Fwd; ROVToVessel = 0; } else if (ROVStbdRadioButton.Checked) { rOVDirection = ROVDirection.Stbd; ROVToVessel = 90; } else if (ROVAftRadioButton.Checked) { rOVDirection = ROVDirection.Aft; ROVToVessel = 180; } else { rOVDirection = ROVDirection.Port; ROVToVessel = 270; } //if statement that sets the refernce line relative to the ROV based on the radio button selection if (ReferenceToROVPortRadioButton.Checked) { referenceToROV = ReferenceToROV.Port; } else { referenceToROV = ReferenceToROV.Starboard; } //works out opposite side by taking the forward measurement from the aft oppositeSide = ROVFwdMeasurement - ROVAftMeasurement; //works out the tangent using the opposite side and baseline (adjacent) tangent = oppositeSide / BaselineMeasurement; //calculates the C-O between ROV and reference using the tangent of the angle ROVToReference = Math.Atan(tangent) * 180 / Math.PI; //if statment that applies the C-O the correct way round based on the if the reference is port or stbd of the ROV if (referenceToROV == ReferenceToROV.Port) { ROVToVessel = ROVToVessel + ROVToReference; } else { ROVToVessel = ROVToVessel - ROVToReference; } //if statement to make sure no values are above 360 or below 0 if (ROVToVessel < 0) { ROVToVessel = 360 + ROVToVessel; } else if (ROVToVessel > 360) { ROVToVessel = ROVToVessel - 360; } StreamReader fileReader = new StreamReader(fullFilePath); //sets the rowsToSkip int to be used for looping the streamreader to skip the desired number of rows inthe file rowsToSkip = Convert.ToInt32(rowsToSkipTextBox.Text); for (int i = 0; i < rowsToSkip; i++) { fileReader.ReadLine(); } //while loop that reads to the end of the file while (!fileReader.EndOfStream) { //sets up some variable to use as temporary stores for each line and the separated values var line = fileReader.ReadLine(); var values = line.Split(','); //adds the current timestamp to the list of timeStamp.Add(values[0]); //all attempts to parse the observation are wrapped in this try. any failed attempt will put a messagebox up showing the observation # //that cant be parsed try { //adds the current observed ROV heading to the observedROVHDG array observedROVHDG.Add(Convert.ToDouble(values[ROVHDGColumn - 1])); //adds the observed vessel heading to the observed vessel hdg array observedVesselHDG.Add(Convert.ToDouble(values[VesselHDGColumn - 1])); //checks to see if the computed ROV HDG is less than zero or above 360. if soo applies an offset of 360 (+/-) to compensate if (Convert.ToDouble(values[2]) + ROVToVessel > 360) { computedROVHDG.Add(Convert.ToDouble(values[2]) + ROVToVessel - 360); } else if (Convert.ToDouble(values[2]) + ROVToVessel < 0) { computedROVHDG.Add(Convert.ToDouble(values[2]) + ROVToVessel + 360); } else { computedROVHDG.Add(Convert.ToDouble(values[2]) + ROVToVessel); } //caluclates the C-O using the computed and observed ROV heading from the current observation) cMinusO.Add(computedROVHDG[observationCounter] - observedROVHDG[observationCounter]); //increments the observation counter on each pass //adds the current observations info to the preview textbox then stars a new line FilePreviewTextBox.Text += observationCounter + 1; FilePreviewTextBox.AppendText(", "); FilePreviewTextBox.Text += "Time, "; FilePreviewTextBox.Text += timeStamp[observationCounter]; FilePreviewTextBox.AppendText(", "); FilePreviewTextBox.Text += "Vessel (Obs), "; FilePreviewTextBox.Text += observedVesselHDG[observationCounter].ToString(format: "N3"); FilePreviewTextBox.AppendText(", "); FilePreviewTextBox.Text += "ROV (Obs), "; FilePreviewTextBox.Text += observedROVHDG[observationCounter].ToString(format: "N3"); FilePreviewTextBox.AppendText(", "); FilePreviewTextBox.Text += "ROV (Comp), "; FilePreviewTextBox.Text += computedROVHDG[observationCounter].ToString(format: "N3"); FilePreviewTextBox.AppendText(", "); FilePreviewTextBox.Text += "C-O, "; FilePreviewTextBox.Text += cMinusO[observationCounter].ToString(format: "N3"); FilePreviewTextBox.AppendText(Environment.NewLine); //increments the observation counter observationCounter++; } catch { MessageBox.Show("Data for observation " + (observationCounter + 1) + " could not be parsed!!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); errorCounter++; if (errorCounter > 5) { break; } } } if (errorCounter > 5) { MessageBox.Show("More than 5 errors have been encountered, Exiting", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } //releases all resources from the streamreder fileReader.Dispose(); //checks to see if the observed vessel list is empty. if it is it shows an error messagebox and does nothing //if there is data it calculates the averages and publishes the results to the textboxes on the form if (observedVesselHDG.Any()) { //calculates all the average values averageCMinusO = cMinusO.Average(); averageObservedVesselHDG = observedVesselHDG.Average(); averageObservedROVHDG = observedROVHDG.Average(); averageComputedROVHDG = computedROVHDG.Average(); //populates all the results text boxes ObservationsTextBox.Text = Convert.ToString(observationCounter); ROVToBaselineResultsTextBox.Text = ROVToReference.ToString(format: "N3"); AverageVesselHDGTextBox.Text = averageObservedVesselHDG.ToString(format: "N3"); AverageComputedROVHDGTextBox.Text = averageComputedROVHDG.ToString(format: "N3"); AverageCMinusOTextBox.Text = averageCMinusO.ToString(format: "N3"); AverageComputedROVHDGTextBox.Text = averageObservedROVHDG.ToString(format: "N3"); AverageROVHDGTextBox.Text = averageObservedROVHDG.ToString(format: "N3"); } else { MessageBox.Show("Warning there are no observations", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } }