private void btnStartRun_Click(object sender, EventArgs e) { btnStartRun.Text = "Scan Started..."; this.Update(); // Adjust Chart area to scan area chartSpectrum.ChartAreas[0].AxisX.ScaleView.Zoomable = true; chartSpectrum.ChartAreas[0].AxisX.Minimum = Convert.ToInt32(numericUpDownMonoScanStartWL.Value); chartSpectrum.ChartAreas[0].AxisX.Maximum = Convert.ToInt32(numericUpDownMonoScanEndWL.Value); chartSpectrum.ChartAreas[0].AxisY.Minimum = -100; // reset variables to remove data from previous rounds saturationevents = 0; // reset saturationevents from previous runs SpectrumTable.Reset(); // Clear datatable from data of previous runs chartSpectrum.Series.Clear(); // Clear chart from data of previous runs // read variables from interface integrationTime = Convert.ToInt32(numericUpDownSpectrometerIntegrationTime.Value * 1000); // read integration time from input field wrapper.setIntegrationTime(selectedSpectrometer, integrationTime); // apply integration time lblStatus.Text = wrapper.getFirmwareModel(selectedSpectrometer) + " Spectrometer selected."; // display selected spectrometer wrapper.setCorrectForElectricalDark(selectedSpectrometer, Convert.ToInt32(checkBoxElectricDarkCorrection.Checked)); wrapper.setCorrectForDetectorNonlinearity(selectedSpectrometer, Convert.ToInt32(checkBoxNonLinearityCorrection.Checked)); wrapper.setScansToAverage(selectedSpectrometer, Convert.ToInt32(numericUpDownSpectrometerScansToAverage.Value)); ///lblStatus.Text = Convert.ToString(Convert.ToInt32(checkBoxNonLinearityCorrection.Checked)); // get spectrum from spectrometer wavelengthArray = (double[])wrapper.getWavelengths(selectedSpectrometer); // get wavelenthts from spectrometer spectrumArray = (double[])wrapper.getSpectrum(selectedSpectrometer); // get spectrum from spectrometer //numberOfPixels = spectrumArray.GetLength(0); // get spectrum length // fill Datatable start SpectrumTable.Columns.Add("Wavelength"); // add first column for wavelengths // insert first row for integration time data START row = SpectrumTable.NewRow(); row["Wavelength"] = "integrationTime in ms"; SpectrumTable.Rows.Add(row); // insert first row for integration time data END // add wavelength values in first column START for (int i = 0; i < spectrumArray.GetLength(0); i++) { row = SpectrumTable.NewRow(); row["Wavelength"] = wavelengthArray[i]; //row["Wavelength"] = Math.Round(wavelengthArray[i], 2); SpectrumTable.Rows.Add(row); } // add wavelength values in first column END // add spectral data columns to datatable START for (decimal k = numericUpDownMonoScanStartWL.Value; k <= numericUpDownMonoScanEndWL.Value; k = k + numericUpDownMonoScanInterval.Value) { myMonoScan.setPositionNM(Convert.ToInt32(k)); System.Threading.Thread.Sleep(millisecondsTimeout: integrationTime / 1000); // while(myMonoScan.positionReached == 0) // { System.Threading.Thread.Sleep(millisecondsTimeout: 5); } spectrumArray = (double[])wrapper.getSpectrum(selectedSpectrometer); int specmax = wrapper.getMaximumIntensity(selectedSpectrometer); int specpercentil = Convert.ToInt32(specmax - (specmax * 0.05)); if (spectrumArray.Max() > specpercentil) { saturationevents = saturationevents + 1; } lblStatus.Text = saturationevents.ToString() + " Saturation events occured during scan."; SpectrumTable.Columns.Add(k.ToString()); SpectrumTable.Rows[0][k.ToString()] = numericUpDownSpectrometerIntegrationTime.Value; SpectrumTable.AcceptChanges(); for (int i = 0; i < spectrumArray.GetLength(0); i++) { SpectrumTable.Rows[i + 1][k.ToString()] = spectrumArray[i]; SpectrumTable.AcceptChanges(); } // add spectral data into series for chart START chartSpectrum.Series.Add(k.ToString()); chartSpectrum.Series[k.ToString()].ChartType = SeriesChartType.Line; for (int l = 1; l < spectrumArray.GetLength(0); l++) { chartSpectrum.Series[k.ToString()].Points.AddXY(Math.Round(wavelengthArray[l], 0), spectrumArray[l]); } this.Update(); // Update Interface to dispaly chart // add spectral data into series for chart END } // add spectral data columns to datatable END // give warning when saturations occured during run if (saturationevents > 0) { MessageBox.Show("There have been " + saturationevents.ToString() + " scans that were saturated!"); } //Write Datatable to csv file with comma separator if (saveFileDialogMonoSeq.ShowDialog() == System.Windows.Forms.DialogResult.OK) // write Datatable to file when dialog was accepted { StringBuilder spectrumString = new StringBuilder(); IEnumerable <string> columnNames = SpectrumTable.Columns.Cast <DataColumn>().Select(column => column.ColumnName); spectrumString.AppendLine(string.Join(",", columnNames)); foreach (DataRow row in SpectrumTable.Rows) { IEnumerable <string> fields = row.ItemArray.Select(field => field.ToString()); spectrumString.AppendLine(string.Join(",", fields)); } File.WriteAllText(saveFileDialogMonoSeq.FileName, spectrumString.ToString()); // Write Datatable end // Write Metadata to txt file, same name as csv but with *.ini extension StringBuilder metadataString = new StringBuilder(); metadataString.AppendLine("Timestamp, " + DateTime.Now.ToString()); metadataString.AppendLine("Spectrometer, " + wrapper.getFirmwareModel(selectedSpectrometer)); metadataString.AppendLine("Scans to Average, " + wrapper.getScansToAverage(selectedSpectrometer).ToString()); metadataString.AppendLine("Boxcar, " + wrapper.getBoxcarWidth(selectedSpectrometer).ToString()); metadataString.AppendLine("EDC, " + wrapper.getCorrectForElectricalDark(selectedSpectrometer).ToString()); metadataString.AppendLine("NLC, " + wrapper.getCorrectForDetectorNonlinearity(selectedSpectrometer).ToString()); metadataString.AppendLine("Itegration Time (as milliseconds), " + (wrapper.getIntegrationTime(selectedSpectrometer) / 1000).ToString()); metadataString.AppendLine("Saturationevents, " + saturationevents.ToString()); metadataString.AppendLine("MonoScan Start, " + numericUpDownMonoScanStartWL.Value.ToString()); metadataString.AppendLine("MonoScan END, " + numericUpDownMonoScanEndWL.Value.ToString()); metadataString.AppendLine("MonoScan Interval, " + numericUpDownMonoScanInterval.Value.ToString()); File.WriteAllText(Path.ChangeExtension(saveFileDialogMonoSeq.FileName, ".txt"), metadataString.ToString()); // Write Metadata End } btnStartRun.Text = "Start"; this.Update(); } // main routine in in here
public AcquisitionThread(ref CCoWrapper w, int spN) { this.spectroNumber = spN; this.w = w; this.acquisitionTime = w.getIntegrationTime(this.spectroNumber); }