/// <summary> /// Gets the nuclides ready to write /// </summary> /// <param name="nuclide"></param> /// <param name="writeLines"></param> public void WriteNuclide(DataRow nuclide, DataTable writeLines, CombineLinesCallBack combineLinesCallBack) { if ((string)nuclide["NAME"] == "D.E." || (string)nuclide["NAME"] == "S.E." || ((string)nuclide["NAME"]).Contains("Sum")) { return; } //check for nuclide duplicates IEnumerable <DataRow> nucDup = lib.Tables["MATCHEDNUCLIDES"].AsEnumerable().Where(row => nuclide["NAME"].ToString().Trim().Equals(row.Field <string>("NAME").Trim())); if (nucDup.Count() > 0) { //check for line duplicates int ln = lib.Tables["MATCHEDLINES"].Select("NAME = '" + (string)nuclide["NAME"] + "'").Length; if (performLineComb) { MergeUnresolvableLines(writeLines, combineLinesCallBack); } int keyLine = (int)GetKeyLine(writeLines)["LINENUMBER"]; foreach (DataRow writeLine in writeLines.Rows) { IEnumerable <DataRow> lineDup = lib.Tables["MATCHEDLINES"].AsEnumerable().Where(row => Math.Abs((double)writeLine["ENERGY"] - row.Field <double>("ENERGY")) < GetPrecision((double)writeLine["ENERGY"]) && Math.Abs((double)writeLine["YIELD"] - row.Field <double>("YIELD")) < GetPrecision((double)writeLine["YIELD"])); if (lineDup.Count() > 0) { continue; } ln++; DataRow addedLine = lib.Tables["MATCHEDLINES"].NewRow(); addedLine.ItemArray = writeLine.ItemArray; addedLine["LINENUMBER"] = ln; addedLine["ISKEY"] = ln == keyLine; lib.Tables["MATCHEDLINES"].Rows.Add(addedLine); } } //if there are no duplicates else { if (performLineComb) { MergeUnresolvableLines(writeLines, combineLinesCallBack); } DataRow nuc = lib.Tables["MATCHEDNUCLIDES"].NewRow(); nuc.ItemArray = nuclide.ItemArray; int id = int.Parse(nuc["ID"].ToString()); while (lib.Tables["MATCHEDNUCLIDES"].Select("ID = '" + id + "'").Length > 0) { id++; } nuc["ID"] = id; lib.Tables["MATCHEDNUCLIDES"].Rows.Add(nuc); SetKeyLine(writeLines); int ln = 0; foreach (DataRow line in writeLines.Rows) { ln++; DataRow writeLine = lib.Tables["MATCHEDLINES"].NewRow(); writeLine.ItemArray = line.ItemArray; writeLine["LINENUMBER"] = ln; if (!(writeLine["NAME"].ToString().Equals(nuc["NAME"].ToString()))) { writeLine["NAME"] = nuc["NAME"]; } lib.Tables["MATCHEDLINES"].Rows.Add(writeLine); } } lib.AcceptChanges(); }
/// <summary> /// Merge lines that are unresolvable /// </summary> /// <param name="writeLines">DataTable of lines to check</param> /// <param name="resolutionLimt">The lower FWHM where lines are resolved</param> protected void MergeUnresolvableLines(DataTable writeLines, CombineLinesCallBack combineLinesCallBack, double resLimit = 0.5) { resolutionLimt = resLimit - resolutionLimt < 1e-6 ? ResolutionLimit : resLimit; int j = 0; while (writeLines.Rows.Count > j) { DataRow row = writeLines.Rows[j]; string expression = "ENERGY > '" + ((double)row["ENERGY"] - resolutionLimt) + "' AND ENERGY < '" + ((double)row["ENERGY"] + resolutionLimt) + "'"; DataRow[] unresLines = writeLines.Select(expression); //only do this if there are more than one lines if ((unresLines.Length < 2)) { j++; continue; } //see if the user wishes to combine energies StringBuilder energies = new StringBuilder(); foreach (DataRow srow in unresLines) { energies.Append(srow["ENERGY"].ToString() + ", "); } energies.Remove(energies.Length - 2, 2); bool?result = combineLinesCallBack(energies.ToString()); //DialogResult result = MessageBox.Show("Lines of energies: " + energies + " keV are possibly unresovable, do you wish to combine them?", "Possible Unresolvable Lines", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == false) { return; } else if (result == null) { j++; continue; } //do the combining double num = 0.0, denom = 0.0, uncnum = 0.0, Ebar = 0.0, Eunc = 0.0, yield = 0.0, yunc = 0.0; for (int i = 0; i < unresLines.Length; i++) { num += (double)unresLines[i]["ENERGY"] * (double)unresLines[i]["YIELD"]; denom += (double)unresLines[i]["YIELD"]; double unc = GetUncertainty((double)unresLines[i]["YIELD"]); yunc += Math.Pow(unc, 2); } Ebar = num / denom; yield = denom; for (int i = 0; i < unresLines.Length; i++) { uncnum += (double)unresLines[i]["YIELD"] * Math.Pow(((double)unresLines[i]["ENERGY"] - Ebar), 2); //delete all lines that arn't the row. if (unresLines[i] != row) { unresLines[i].Delete(); } } //finshup by combining and taking the square root Eunc = Math.Sqrt(uncnum / ((((double)unresLines.Length - 1.0) / (double)unresLines.Length) * denom)); yunc = Math.Sqrt(yunc); //if the energy uncertainty is zero do standard uncertainty if (Math.Abs(Eunc) < 1e-9) { Eunc = GetUncertainty(Ebar); } writeLines.AcceptChanges(); //add the numbers row["ENERGY"] = Ebar; row["YIELD"] = denom; row["ENERGYUNC"] = Eunc; row["YIELDUNC"] = yunc; j++; } }