(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 10.2' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       158,          7]
NotebookDataLength[    149495,       3658]
NotebookOptionsPosition[    144286,       3555]
NotebookOutlinePosition[    144773,       3573]
CellTagsIndexPosition[    144730,       3570]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["CompositionalAnalysis", "Title"],

Cell[CellGroupData[{

Cell["Begin package", "Section"],

Cell[BoxData[
 RowBox[{
  RowBox[{"BeginPackage", "[", 
   RowBox[{"\"\<DataAnalysis`CompositionalAnalysis`\>\"", ",", 
    RowBox[{"{", "\[IndentingNewLine]", 
     RowBox[{
     "\"\<SciDraw`\>\"", ",", "\[IndentingNewLine]", 
      "\"\<DataAnalysis`\>\""}], "\[IndentingNewLine]", "}"}]}], "]"}], 
  ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"bf162c25-922f-412c-ba6b-98deeb45f15e"],

Cell[BoxData[
 RowBox[{
  RowBox[{"Unprotect", "[", 
   RowBox[{"Evaluate", "[", 
    RowBox[{"$Context", "<>", "\"\<*\>\""}], "]"}], "]"}], ";"}]], "Input",
 InitializationCell->True]
}, Open  ]],

Cell[CellGroupData[{

Cell["Messages", "Section"],

Cell[CellGroupData[{

Cell["Functions", "Subsection"],

Cell[BoxData[{
 RowBox[{"CalibrationPlot", ";"}], "\[IndentingNewLine]", 
 RowBox[{"CalibrateCompositionalData", ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"RemoveNonDetectableValues", ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"FilterOutliers", ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{"SSBSubstractBlanks", ";"}], "\[IndentingNewLine]", 
 RowBox[{"SSBCalculateDeltas", ";"}], "\[IndentingNewLine]", 
 RowBox[{"SSBCalculateMeanRatios", ";"}], "\[IndentingNewLine]", 
 RowBox[{"SSBCalculateCalibrationFit", ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"54dddeb2-fca2-4fb5-8f12-060647fd05eb"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Options", "Subsection"],

Cell[BoxData[{
 RowBox[{"IncludeQuadraticTerm", ";"}], "\[IndentingNewLine]", 
 RowBox[{"DriftStandard", ";"}], "\[IndentingNewLine]", 
 RowBox[{"BlankPattern", ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"398ecc10-42f3-4937-9d24-52234dc16099"]
}, Open  ]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Begin private context", "Section"],

Cell[BoxData[
 RowBox[{
  RowBox[{"Begin", "[", "\"\<`Private`\>\"", "]"}], ";"}]], "Input",
 InitializationCell->True]
}, Closed]],

Cell[CellGroupData[{

Cell["Error handling", "Section"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"SetAttributes", "[", 
   RowBox[{"ErrorMessage", ",", "HoldFirst"}], "]"}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"ErrorMessage", "[", "Args___", "]"}], ":=", 
   RowBox[{"Module", "[", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"Message", "[", "Args", "]"}], ";", "\[IndentingNewLine]", 
      RowBox[{"Abort", "[", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], 
  ";"}]}], "Input",
 InitializationCell->True],

Cell[BoxData[{
 RowBox[{
  RowBox[{"SetAttributes", "[", 
   RowBox[{"CatchError", ",", "HoldAll"}], "]"}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CatchError", "[", "Body_", "]"}], ":=", 
   RowBox[{"(", 
    RowBox[{
     RowBox[{"Catch", "[", "Body", "]"}], "===", "$ErrorFlag"}], ")"}]}], 
  ";"}]}], "Input",
 InitializationCell->True]
}, Closed]],

Cell[CellGroupData[{

Cell["Normal Calibration", "Section"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "CalibrationPlot", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{
     RowBox[{"IncludeConstantBasis", "\[Rule]", "False"}], ",", 
     RowBox[{"IncludeQuadraticTerm", "\[Rule]", "False"}], ",", 
     RowBox[{"BlankPattern", "\[Rule]", "Null"}]}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"BlankPattern", "::", "usage"}], "=", 
    "\"\<BlankPattern is an option to specify a string pattern that matches \
the row keys (IDs) of all blank measurements.\>\""}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CalibrationPlot", "::", "nostandards"}], "=", 
   "\"\<No standards were found for this element in the provided standard \
data.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CalibrationPlot", "::", "nostandarddata"}], "=", 
   "\"\<No data was found for these standards and element in the provided \
input data.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"CalibrationPlot", "::", "noisotope"}], "=", 
    "\"\<No element/isotope with the provided ID was measured.\>\""}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CalibrationPlot", "::", "usage"}], "=", 
   "\"\<CalibrationPlot[datatable,standardData,column,{\!\(\*SubscriptBox[\(\
standardPatttern\), \(1\)]\),\!\(\*SubscriptBox[\(standardPatttern\), \
\(2\)]\),...}] plots the measurements in a specific column (e.g. compound) of \
the DataTable for the standards matched by each \
\!\(\*SubscriptBox[\(standardPatttern\), \(i\)]\) vs. the corresponding \
concentrations specified by the standards.\n\
CalibrationPlot[datatable,standardData,{\!\(\*SubscriptBox[\(column\), \(i\)]\
\),\!\(\*SubscriptBox[\(column\), \
\(2\)]\),...},{\!\(\*SubscriptBox[\(standardPatttern\), \
\(1\)]\),\!\(\*SubscriptBox[\(standardPatttern\), \(2\)]\),...}] plots the \
measurements in a series of columns (e.g. compounds).\>\""}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"CalibrationPlot", "[", 
     RowBox[{
     "intData_DataTable", ",", "standardConc_DataTable", ",", "column_String",
       ",", "stdPatterns_List", ",", 
      RowBox[{"opts", ":", 
       RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
    RowBox[{"Module", "[", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"{", 
       RowBox[{"FigDat", ",", "YMax", ",", "XMax", ",", "FitF"}], "}"}], ",", 
      "\[IndentingNewLine]", 
      RowBox[{"(", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{"FigDat", "=", 
         RowBox[{
          RowBox[{
           RowBox[{"CalibrationPlotData", "[", 
            RowBox[{
            "intData", ",", "standardConc", ",", "column", ",", "#", ",", 
             "opts"}], "]"}], "&"}], "/@", "stdPatterns"}]}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"FitF", "=", 
         RowBox[{"FigDat", "[", 
          RowBox[{"[", 
           RowBox[{"All", ",", "2"}], "]"}], "]"}]}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"FigDat", "=", 
         RowBox[{"FigDat", "[", 
          RowBox[{"[", 
           RowBox[{"All", ",", "1"}], "]"}], "]"}]}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"FigDat", "=", 
         RowBox[{
          RowBox[{
           RowBox[{"DeleteMissing", "[", 
            RowBox[{"#", ",", "1", ",", "Infinity"}], "]"}], "&"}], "/@", 
          "FigDat"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
        RowBox[{"XMax", "=", 
         RowBox[{"Max", "[", 
          RowBox[{
           RowBox[{
            RowBox[{"(", 
             RowBox[{"Max", "@", 
              RowBox[{"#", "[", 
               RowBox[{"All", ",", "\"\<Concentration\>\"", ",", "DVMeans"}], 
               "]"}]}], ")"}], "&"}], "/@", "FigDat"}], "]"}]}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"XMax", "=", 
         RowBox[{"Ceiling", "[", 
          RowBox[{"XMax", ",", 
           RowBox[{"10", "^", 
            RowBox[{"(", 
             RowBox[{"(", 
              RowBox[{
               RowBox[{"Log10", "[", "XMax", "]"}], "//", "Floor"}], ")"}], 
             ")"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", 
        "\[IndentingNewLine]", 
        RowBox[{"YMax", "=", 
         RowBox[{"Max", "[", 
          RowBox[{
           RowBox[{
            RowBox[{"(", 
             RowBox[{"Max", "@", 
              RowBox[{"#", "[", 
               RowBox[{"All", ",", "\"\<Measurement\>\"", ",", "DVMeans"}], 
               "]"}]}], ")"}], "&"}], "/@", "FigDat"}], "]"}]}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"YMax", "=", 
         RowBox[{"Ceiling", "[", 
          RowBox[{"YMax", ",", 
           RowBox[{"10", "^", 
            RowBox[{"(", 
             RowBox[{"(", 
              RowBox[{
               RowBox[{"Log10", "[", "YMax", "]"}], "//", "Floor"}], ")"}], 
             ")"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", 
        "\[IndentingNewLine]", 
        RowBox[{"Show", "[", "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"DTPlot", "[", "\[IndentingNewLine]", 
           RowBox[{"FigDat", ",", "\[IndentingNewLine]", 
            RowBox[{"PlotRange", "\[Rule]", 
             RowBox[{"{", 
              RowBox[{
               RowBox[{"{", 
                RowBox[{"0", ",", "XMax"}], "}"}], ",", 
               RowBox[{"{", 
                RowBox[{"0", ",", "YMax"}], "}"}]}], "}"}]}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{"PlotLegends", "\[Rule]", "stdPatterns"}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{"PlotLabel", "\[Rule]", 
             RowBox[{"Style", "[", 
              RowBox[{"column", ",", "16", ",", "Black"}], "]"}]}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{"ImageSize", "\[Rule]", "500"}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{"PlotRangePadding", "\[Rule]", 
             RowBox[{"Scaled", "[", "0.05", "]"}]}]}], "\[IndentingNewLine]", 
           "]"}], ",", "\[IndentingNewLine]", 
          RowBox[{"MapIndexed", "[", 
           RowBox[{
            RowBox[{
             RowBox[{"Plot", "[", 
              RowBox[{
               RowBox[{
                RowBox[{"If", "[", 
                 RowBox[{
                  RowBox[{"TrueQ", "[", 
                   RowBox[{"OptionValue", "[", "IncludeQuadraticTerm", "]"}], 
                   "]"}], ",", "\[IndentingNewLine]", 
                  RowBox[{"If", "[", 
                   RowBox[{
                    RowBox[{"TrueQ", "[", 
                    RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}],
                     "]"}], ",", "\[IndentingNewLine]", 
                    RowBox[{
                    RowBox[{"a", "*", 
                    RowBox[{"x", "^", "2"}]}], "+", 
                    RowBox[{"b", "*", "x"}], "+", "c"}], ",", 
                    "\[IndentingNewLine]", 
                    RowBox[{
                    RowBox[{"a", "*", 
                    RowBox[{"x", "^", "2"}]}], "+", 
                    RowBox[{"b", "*", "x"}]}]}], "\[IndentingNewLine]", "]"}],
                   ",", "\[IndentingNewLine]", 
                  RowBox[{"If", "[", 
                   RowBox[{
                    RowBox[{"TrueQ", "[", 
                    RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}],
                     "]"}], ",", "\[IndentingNewLine]", 
                    RowBox[{
                    RowBox[{"b", "*", "x"}], "+", "c"}], ",", 
                    "\[IndentingNewLine]", 
                    RowBox[{"b", "*", "x"}]}], "\[IndentingNewLine]", "]"}]}],
                  "\[IndentingNewLine]", "]"}], "/.", 
                RowBox[{"#", "[", "\"\<BestFitParameters\>\"", "]"}]}], ",", 
               RowBox[{"{", 
                RowBox[{"x", ",", "0", ",", "XMax"}], "}"}], ",", 
               RowBox[{"PlotStyle", "\[Rule]", "Black"}]}], "]"}], "&"}], ",",
             "FitF"}], "]"}]}], "\[IndentingNewLine]", "]"}]}], 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"(*", 
        RowBox[{"DataAnalysis`Fig", "[", "\[IndentingNewLine]", 
         RowBox[{"Join", "[", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"MapIndexed", "[", 
            RowBox[{
             RowBox[{
              RowBox[{"Plot", "[", 
               RowBox[{
                RowBox[{
                 RowBox[{"If", "[", 
                  RowBox[{
                   RowBox[{"TrueQ", "[", 
                    RowBox[{"OptionValue", "[", "IncludeQuadraticTerm", "]"}],
                     "]"}], ",", "\[IndentingNewLine]", 
                   RowBox[{"If", "[", 
                    RowBox[{
                    RowBox[{"TrueQ", "[", 
                    RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}],
                     "]"}], ",", "\[IndentingNewLine]", 
                    RowBox[{
                    RowBox[{"a", "*", 
                    RowBox[{"x", "^", "2"}]}], "+", 
                    RowBox[{"b", "*", "x"}], "+", "c"}], ",", 
                    "\[IndentingNewLine]", 
                    RowBox[{
                    RowBox[{"a", "*", 
                    RowBox[{"x", "^", "2"}]}], "+", 
                    RowBox[{"b", "*", "x"}]}]}], "\[IndentingNewLine]", "]"}],
                    ",", "\[IndentingNewLine]", 
                   RowBox[{"If", "[", 
                    RowBox[{
                    RowBox[{"TrueQ", "[", 
                    RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}],
                     "]"}], ",", "\[IndentingNewLine]", 
                    RowBox[{
                    RowBox[{"b", "*", "x"}], "+", "c"}], ",", 
                    "\[IndentingNewLine]", 
                    RowBox[{"b", "*", "x"}]}], "\[IndentingNewLine]", "]"}]}],
                   "\[IndentingNewLine]", "]"}], "/.", 
                 RowBox[{"#", "[", "\"\<BestFitParameters\>\"", "]"}]}], ",", 
                RowBox[{"{", 
                 RowBox[{"x", ",", "0", ",", "XMax"}], "}"}], ",", 
                RowBox[{"PlotStyle", "\[Rule]", "Black"}]}], "]"}], "&"}], 
             ",", "FitF"}], "]"}], ",", "\[IndentingNewLine]", "FigDat"}], 
          "\[IndentingNewLine]", "]"}], "\[IndentingNewLine]", "]"}], "*)"}], 
       "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"CalibrationPlot", "[", 
   RowBox[{
   "intData_DataTable", ",", "standardConc_DataTable", ",", "columns_List", 
    ",", "stdPatterns_List", ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{
     "PlotDat", ",", "FigDat", ",", "YMax", ",", "XMax", ",", "FitF"}], "}"}],
     ",", "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{"Column", "[", 
      RowBox[{
       RowBox[{"(", 
        RowBox[{
         RowBox[{
          RowBox[{"CalibrationPlot", "[", 
           RowBox[{
           "intData", ",", "standardConc", ",", "#", ",", "stdPatterns", ",", 
            "opts"}], "]"}], "&"}], "/@", "columns"}], ")"}], ",", "Center", 
       ",", "5"}], "]"}], "\[IndentingNewLine]", ")"}]}], 
   "\[IndentingNewLine]", "]"}]}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"26fe29de-4be2-43e1-9819-c1b6864d0d99"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "CalibrationPlotData", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{
     RowBox[{"IncludeConstantBasis", "\[Rule]", "False"}], ",", 
     RowBox[{"IncludeQuadraticTerm", "\[Rule]", "False"}], ",", 
     RowBox[{"BlankPattern", "\[Rule]", "Null"}]}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"CalibrationPlotData", "[", 
   RowBox[{
   "intData_DataTable", ",", "standardConc_DataTable", ",", "column_String", 
    ",", "stdPattern_", ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{
     "Element", ",", "StandardConc", ",", "StandardInt", ",", "Standards", 
      ",", "FitDat", ",", "FitDatE", ",", "FitF", ",", "IDat", ",", "OutDat", 
      ",", "StandardsFound", ",", "FD"}], "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"Off", "[", 
       StyleBox[
        RowBox[{"InterpolatingFunction", "::", "dmval"}], "MessageName"], 
       "]"}], ";", "\[IndentingNewLine]", 
      RowBox[{"Off", "[", 
       RowBox[{"FittedModel", "::", "varzero"}], "]"}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"Off", "[", 
       StyleBox[
        RowBox[{"Interpolation", "::", "inhr"}], "MessageName"], "]"}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"Element", "=", 
       RowBox[{
        RowBox[{"StringCases", "[", 
         RowBox[{"column", ",", 
          RowBox[{"LetterCharacter", ".."}]}], "]"}], "//", "First"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"StandardConc", "=", 
       RowBox[{"standardConc", "[", 
        RowBox[{"All", ",", 
         RowBox[{"Element", "~~", "___"}]}], "]"}]}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"FailureQ", "[", "StandardConc", "]"}], "||", 
         RowBox[{"(", 
          RowBox[{
           RowBox[{
            RowBox[{"StandardConc", "[", 
             RowBox[{"All", ",", "Length"}], "]"}], "[", "Max", "]"}], 
           "\[Equal]", "0"}], ")"}]}], ",", 
        RowBox[{"ErrorMessage", "[", 
         RowBox[{"CalibrationPlot", "::", "nostandards"}], "]"}]}], "]"}], 
      ";", "\[IndentingNewLine]", 
      RowBox[{"(*", 
       RowBox[{
        RowBox[{"StandardConc", "=", 
         RowBox[{"ReplaceRight", "[", 
          RowBox[{"StandardConc", ",", 
           RowBox[{
            RowBox[{"(", 
             RowBox[{
              RowBox[{"Select", "[", 
               RowBox[{
                RowBox[{"Transpose", "[", "#", "]"}], ",", 
                RowBox[{
                 RowBox[{"DVMeans", "[", 
                  RowBox[{
                   RowBox[{"#", "[", 
                    RowBox[{"[", "2", "]"}], "]"}], ">", "0"}], "]"}], 
                 "&"}]}], "]"}], "//", "Transpose"}], ")"}], "&"}]}], "]"}]}],
         ";"}], "*)"}], "\[IndentingNewLine]", 
      RowBox[{"Standards", "=", 
       RowBox[{"Normal", "@", 
        RowBox[{"Keys", "@", "StandardConc"}]}]}], ";", "\[IndentingNewLine]",
       "\[IndentingNewLine]", 
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"Length", "[", 
          RowBox[{"intData", "[", "stdPattern", "]"}], "]"}], "\[Equal]", 
         "0"}], ",", 
        RowBox[{"ErrorMessage", "[", 
         RowBox[{"CalibrationPlot", "::", "nostandarddata"}], "]"}]}], "]"}], 
      ";", "\[IndentingNewLine]", 
      RowBox[{"StandardInt", "=", 
       RowBox[{"Flatten", "@", 
        RowBox[{"Map", "[", 
         RowBox[{
          RowBox[{
           RowBox[{"Normal", "@", 
            RowBox[{"Values", "@", 
             RowBox[{
              RowBox[{"intData", "[", 
               RowBox[{
                RowBox[{"(", 
                 RowBox[{"___", "~~", "#", "~~", "___"}], ")"}], ",", 
                "column"}], "]"}], "[", 
              RowBox[{"KeyFind", "[", "stdPattern", "]"}], "]"}]}]}], "&"}], 
          ",", "Standards"}], "]"}]}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{"MatchQ", "[", 
         RowBox[{"StandardInt", ",", 
          RowBox[{"{", 
           RowBox[{"Null", ".."}], "}"}]}], "]"}], ",", 
        RowBox[{"ErrorMessage", "[", 
         RowBox[{"CalibrationPlot", "::", "noisotope"}], "]"}]}], "]"}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"StandardsFound", "=", 
       RowBox[{"DeleteCases", "[", 
        RowBox[{
         RowBox[{"Map", "[", 
          RowBox[{
           RowBox[{
            RowBox[{"If", "[", 
             RowBox[{
              RowBox[{
               RowBox[{"Length", "[", 
                RowBox[{
                 RowBox[{"intData", "[", 
                  RowBox[{
                   RowBox[{"(", 
                    RowBox[{"___", "~~", "#", "~~", "___"}], ")"}], ",", 
                   "column"}], "]"}], "[", 
                 RowBox[{"KeyFind", "[", "stdPattern", "]"}], "]"}], "]"}], 
               "\[Equal]", "1"}], ",", "#", ",", "Null"}], "]"}], "&"}], ",", 
           "Standards"}], "]"}], ",", "Null"}], "]"}]}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"IDat", "=", 
       RowBox[{"CalibrationInterpolations", "[", 
        RowBox[{
        "intData", ",", "StandardsFound", ",", "column", ",", "opts"}], 
        "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"FitDat", "=", 
       RowBox[{
        RowBox[{"{", "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"DVMeans", "@", 
           RowBox[{"Flatten", "@", 
            RowBox[{"Values", "@", 
             RowBox[{"Values", "@", 
              RowBox[{"Normal", "@", 
               RowBox[{"StandardConc", "[", "StandardsFound", "]"}]}]}]}]}]}],
           ",", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"(", 
            RowBox[{"StandardInt", "//", "DVMeans"}], ")"}], "-", 
           RowBox[{"(", 
            RowBox[{
             RowBox[{
              RowBox[{
               RowBox[{"IDat", "[", 
                RowBox[{"[", "3", "]"}], "]"}], "[", "#", "]"}], "&"}], "/@", 
             RowBox[{"(", 
              RowBox[{"Values", "@", 
               RowBox[{"Normal", "@", 
                RowBox[{"intData", "[", 
                 RowBox[{"stdPattern", ",", 
                  RowBox[{
                   RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], "&"}]}], 
                 "]"}]}]}], ")"}]}], ")"}]}]}], "\[IndentingNewLine]", "}"}], 
        "//", "Transpose"}]}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"FitDatE", "=", 
       RowBox[{
        RowBox[{"{", "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"DVSEs", "@", 
           RowBox[{"Flatten", "@", 
            RowBox[{"Values", "@", 
             RowBox[{"Values", "@", 
              RowBox[{"Normal", "@", 
               RowBox[{"StandardConc", "[", "StandardsFound", "]"}]}]}]}]}]}],
           ",", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"(", 
            RowBox[{"StandardInt", "//", "DVSEs"}], ")"}], "+", 
           RowBox[{"(", 
            RowBox[{
             RowBox[{
              RowBox[{
               RowBox[{"IDat", "[", 
                RowBox[{"[", "4", "]"}], "]"}], "[", "#", "]"}], "&"}], "/@", 
             RowBox[{"(", 
              RowBox[{"Values", "@", 
               RowBox[{"Normal", "@", 
                RowBox[{"intData", "[", 
                 RowBox[{"stdPattern", ",", 
                  RowBox[{
                   RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], "&"}]}], 
                 "]"}]}]}], ")"}]}], ")"}]}]}], "\[IndentingNewLine]", "}"}], 
        "//", "Transpose"}]}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"FD", "=", 
       RowBox[{"MapThread", "[", 
        RowBox[{
         RowBox[{
          RowBox[{"{", 
           RowBox[{"#1", ",", "#2"}], "}"}], "&"}], ",", 
         RowBox[{"{", 
          RowBox[{"FitDat", ",", "FitDatE"}], "}"}]}], "]"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"FD", "=", 
       RowBox[{"DeleteMissing", "[", 
        RowBox[{"FD", ",", "1", ",", "Infinity"}], "]"}]}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"FitF", "=", 
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"TrueQ", "[", 
          RowBox[{"OptionValue", "[", "IncludeQuadraticTerm", "]"}], "]"}], 
         ",", "\[IndentingNewLine]", 
         RowBox[{"If", "[", 
          RowBox[{
           RowBox[{"TrueQ", "[", 
            RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}], "]"}], 
           ",", "\[IndentingNewLine]", 
           RowBox[{"EffectiveVarianceFit", "[", 
            RowBox[{
             RowBox[{"FD", "[", 
              RowBox[{"[", 
               RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
             RowBox[{"FD", "[", 
              RowBox[{"[", 
               RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", 
             RowBox[{
              RowBox[{"a", "*", 
               RowBox[{"x", "^", "2"}]}], "+", 
              RowBox[{"b", "*", "x"}], "+", "c"}], ",", 
             RowBox[{"{", 
              RowBox[{"a", ",", "b", ",", "c"}], "}"}], ",", 
             RowBox[{"{", "x", "}"}], ",", "5"}], "]"}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{"EffectiveVarianceFit", "[", 
            RowBox[{
             RowBox[{"FD", "[", 
              RowBox[{"[", 
               RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
             RowBox[{"FD", "[", 
              RowBox[{"[", 
               RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", 
             RowBox[{
              RowBox[{"a", "*", 
               RowBox[{"x", "^", "2"}]}], "+", 
              RowBox[{"b", "*", "x"}]}], ",", 
             RowBox[{"{", 
              RowBox[{"a", ",", "b"}], "}"}], ",", 
             RowBox[{"{", "x", "}"}], ",", "5"}], "]"}]}], 
          "\[IndentingNewLine]", "]"}], ",", "\[IndentingNewLine]", 
         RowBox[{"If", "[", 
          RowBox[{
           RowBox[{"TrueQ", "[", 
            RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}], "]"}], 
           ",", "\[IndentingNewLine]", 
           RowBox[{"EffectiveVarianceFit", "[", 
            RowBox[{
             RowBox[{"FD", "[", 
              RowBox[{"[", 
               RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
             RowBox[{"FD", "[", 
              RowBox[{"[", 
               RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", 
             RowBox[{
              RowBox[{"b", "*", "x"}], "+", "c"}], ",", 
             RowBox[{"{", 
              RowBox[{"b", ",", "c"}], "}"}], ",", 
             RowBox[{"{", "x", "}"}], ",", "5"}], "]"}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{"EffectiveVarianceFit", "[", 
            RowBox[{
             RowBox[{"FD", "[", 
              RowBox[{"[", 
               RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
             RowBox[{"FD", "[", 
              RowBox[{"[", 
               RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", 
             RowBox[{"b", "*", "x"}], ",", 
             RowBox[{"{", "b", "}"}], ",", 
             RowBox[{"{", "x", "}"}], ",", "5"}], "]"}]}], 
          "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}]}], ";",
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"OutDat", "=", 
       RowBox[{"DataTable", "[", 
        RowBox[{"Prepend", "[", 
         RowBox[{
          RowBox[{"MapThread", "[", 
           RowBox[{
            RowBox[{
             RowBox[{"{", 
              RowBox[{"#3", ",", "#1", ",", 
               RowBox[{"DVE", "@", 
                RowBox[{"(", 
                 RowBox[{"#2", "-", 
                  RowBox[{"NDV", "[", 
                   RowBox[{
                    RowBox[{
                    RowBox[{"IDat", "[", 
                    RowBox[{"[", "3", "]"}], "]"}], "[", "#4", "]"}], ",", 
                    RowBox[{
                    RowBox[{"IDat", "[", 
                    RowBox[{"[", "4", "]"}], "]"}], "[", "#4", "]"}]}], 
                   "]"}]}], ")"}]}]}], "}"}], "&"}], ",", 
            RowBox[{"{", 
             RowBox[{
              RowBox[{"Flatten", "@", 
               RowBox[{"Values", "@", 
                RowBox[{"Values", "@", 
                 RowBox[{"Normal", "@", 
                  RowBox[{
                  "StandardConc", "[", "StandardsFound", "]"}]}]}]}]}], ",", 
              "StandardInt", ",", "StandardsFound", ",", 
              RowBox[{"(", 
               RowBox[{"Values", "@", 
                RowBox[{"Normal", "@", 
                 RowBox[{"intData", "[", 
                  RowBox[{"stdPattern", ",", 
                   RowBox[{
                    RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], "&"}]}], 
                  "]"}]}]}], ")"}]}], "}"}]}], "]"}], ",", 
          RowBox[{"{", 
           RowBox[{
           "\"\<ID\>\"", ",", "\"\<Concentration\>\"", ",", 
            "\"\<Measurement\>\""}], "}"}]}], "]"}], "]"}]}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"On", "[", 
       StyleBox[
        RowBox[{"InterpolatingFunction", "::", "dmval"}], "MessageName"], 
       "]"}], ";", "\[IndentingNewLine]", 
      RowBox[{"On", "[", 
       RowBox[{"FittedModel", "::", "varzero"}], "]"}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"On", "[", 
       StyleBox[
        RowBox[{"Interpolation", "::", "inhr"}], "MessageName"], "]"}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"{", 
       RowBox[{"OutDat", ",", "FitF"}], "}"}]}], "\[IndentingNewLine]", 
     ")"}]}], "\[IndentingNewLine]", "]"}]}]}], "Input",
 InitializationCell->True],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "CalibrateCompositionalData", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{
     RowBox[{"IncludeConstantBasis", "\[Rule]", "False"}], ",", 
     RowBox[{"IncludeQuadraticTerm", "\[Rule]", "False"}], ",", 
     RowBox[{"BlankPattern", "\[Rule]", "Null"}], ",", 
     RowBox[{"DriftStandard", "\[Rule]", "Null"}]}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"DriftStandard", "::", "usage"}], "=", 
    "\"\<DriftStandard is an option to specify a string pattern that matches \
the row keys (IDs) of drift standard measurements.\>\""}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CalibrateCompositionalData", "::", "nostandards"}], "=", 
   "\"\<No standards were found for this element in the provided standard \
data.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"CalibrateCompositionalData", "::", "noisotope"}], "=", 
    "\"\<No element/isotope with the provided ID was measured.\>\""}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CalibrateCompositionalData", "::", "usage"}], "=", 
   "\"\<CalibrateCompositionalData[datatable,standardData,{\!\(\*SubscriptBox[\
\(column\), \(i\)]\),\!\(\*SubscriptBox[\(column\), \(2\)]\),...}] calibrates \
the measurements for the compounds in a set of columns in a DataTable using \
the standardData.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CalibrateCompositionalData", "[", 
    RowBox[{
    "intData_DataTable", ",", "standardConc_DataTable", ",", "columns_List", 
     ",", 
     RowBox[{"opts", ":", 
      RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
   RowBox[{"Module", "[", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{
      "IntDt", ",", "Element", ",", "StandardConc", ",", "Standards", ",", 
       "StandardInt", ",", "StandardsFound", ",", "OutDt", ",", "IDat", ",", 
       RowBox[{"BInterpols", "=", 
        RowBox[{"{", "}"}]}], ",", 
       RowBox[{"BInterpolsE", "=", 
        RowBox[{"{", "}"}]}], ",", 
       RowBox[{"Interpols", "=", 
        RowBox[{"{", "}"}]}], ",", 
       RowBox[{"InterpolsE", "=", 
        RowBox[{"{", "}"}]}], ",", 
       RowBox[{"DriftInterpols", "=", 
        RowBox[{"{", "}"}]}], ",", 
       RowBox[{"DriftInterpolsE", "=", 
        RowBox[{"{", "}"}]}], ",", 
       RowBox[{"StandardConcs", "=", 
        RowBox[{"{", "}"}]}], ",", "ColName", ",", "ColNum", ",", 
       RowBox[{"FullColNames", "=", 
        RowBox[{"{", "}"}]}]}], "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"(", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"IntDt", "=", "intData"}], ";", "\[IndentingNewLine]", 
       "\[IndentingNewLine]", 
       RowBox[{"Off", "[", 
        StyleBox[
         RowBox[{"InterpolatingFunction", "::", "dmval"}], "MessageName"], 
        "]"}], ";", "\[IndentingNewLine]", 
       RowBox[{"Off", "[", 
        RowBox[{"FittedModel", "::", "varzero"}], "]"}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"Off", "[", 
        StyleBox[
         RowBox[{"Interpolation", "::", "inhr"}], "MessageName"], "]"}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"Scan", "[", 
        RowBox[{
         RowBox[{
          RowBox[{"(", "\[IndentingNewLine]", 
           RowBox[{
            RowBox[{"Element", "=", 
             RowBox[{
              RowBox[{"StringCases", "[", 
               RowBox[{"#", ",", 
                RowBox[{"LetterCharacter", ".."}]}], "]"}], "//", "First"}]}],
             ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
            RowBox[{"StandardConc", "=", 
             RowBox[{"standardConc", "[", 
              RowBox[{"All", ",", 
               RowBox[{"Element", "~~", "___"}]}], "]"}]}], ";", 
            "\[IndentingNewLine]", 
            RowBox[{"If", "[", 
             RowBox[{
              RowBox[{
               RowBox[{"FailureQ", "[", "StandardConc", "]"}], "||", 
               RowBox[{"(", 
                RowBox[{
                 RowBox[{
                  RowBox[{"StandardConc", "[", 
                   RowBox[{"All", ",", "Length"}], "]"}], "[", "Max", "]"}], 
                 "\[Equal]", "0"}], ")"}]}], ",", 
              RowBox[{"ErrorMessage", "[", 
               RowBox[{"CalibrateCompositionalData", "::", "nostandards"}], 
               "]"}]}], "]"}], ";", "\[IndentingNewLine]", 
            RowBox[{"Standards", "=", 
             RowBox[{"Normal", "@", 
              RowBox[{"Keys", "@", "StandardConc"}]}]}], ";", 
            "\[IndentingNewLine]", "\[IndentingNewLine]", 
            RowBox[{"StandardInt", "=", 
             RowBox[{"Flatten", "@", 
              RowBox[{"Map", "[", 
               RowBox[{
                RowBox[{
                 RowBox[{"Normal", "@", 
                  RowBox[{"Values", "@", 
                   RowBox[{"intData", "[", 
                    RowBox[{
                    RowBox[{"(", 
                    RowBox[{"___", "~~", "#", "~~", "___"}], ")"}], ",", 
                    "column"}], "]"}]}]}], "&"}], ",", "Standards"}], 
               "]"}]}]}], ";", "\[IndentingNewLine]", 
            RowBox[{"If", "[", 
             RowBox[{
              RowBox[{"MatchQ", "[", 
               RowBox[{"StandardInt", ",", 
                RowBox[{"{", 
                 RowBox[{"Null", ".."}], "}"}]}], "]"}], ",", 
              RowBox[{"ErrorMessage", "[", 
               RowBox[{"CalibrateCompositionalData", "::", "noisotope"}], 
               "]"}]}], "]"}], ";", "\[IndentingNewLine]", 
            "\[IndentingNewLine]", 
            RowBox[{"StandardsFound", "=", 
             RowBox[{"DeleteCases", "[", 
              RowBox[{
               RowBox[{"Map", "[", 
                RowBox[{
                 RowBox[{
                  RowBox[{"If", "[", 
                   RowBox[{
                    RowBox[{
                    RowBox[{"Length", "[", 
                    RowBox[{"intData", "[", 
                    RowBox[{
                    RowBox[{"(", 
                    RowBox[{"___", "~~", "#", "~~", "___"}], ")"}], ",", 
                    "column"}], "]"}], "]"}], "\[GreaterEqual]", "1"}], ",", 
                    "#", ",", "Null"}], "]"}], "&"}], ",", "Standards"}], 
                "]"}], ",", "Null"}], "]"}]}], ";", "\[IndentingNewLine]", 
            "\[IndentingNewLine]", 
            RowBox[{"IDat", "=", 
             RowBox[{"CalibrationInterpolations", "[", 
              RowBox[{
              "intData", ",", "StandardsFound", ",", "#", ",", "opts"}], 
              "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
            RowBox[{"AppendTo", "[", 
             RowBox[{"Interpols", ",", 
              RowBox[{"IDat", "[", 
               RowBox[{"[", "1", "]"}], "]"}]}], "]"}], ";", 
            "\[IndentingNewLine]", 
            RowBox[{"AppendTo", "[", 
             RowBox[{"InterpolsE", ",", 
              RowBox[{"IDat", "[", 
               RowBox[{"[", "2", "]"}], "]"}]}], "]"}], ";", 
            "\[IndentingNewLine]", 
            RowBox[{"AppendTo", "[", 
             RowBox[{"BInterpols", ",", 
              RowBox[{"IDat", "[", 
               RowBox[{"[", "3", "]"}], "]"}]}], "]"}], ";", 
            "\[IndentingNewLine]", 
            RowBox[{"AppendTo", "[", 
             RowBox[{"BInterpolsE", ",", 
              RowBox[{"IDat", "[", 
               RowBox[{"[", "4", "]"}], "]"}]}], "]"}], ";", 
            "\[IndentingNewLine]", 
            RowBox[{"AppendTo", "[", 
             RowBox[{"DriftInterpols", ",", 
              RowBox[{"IDat", "[", 
               RowBox[{"[", "5", "]"}], "]"}]}], "]"}], ";", 
            "\[IndentingNewLine]", 
            RowBox[{"AppendTo", "[", 
             RowBox[{"DriftInterpolsE", ",", 
              RowBox[{"IDat", "[", 
               RowBox[{"[", "6", "]"}], "]"}]}], "]"}], ";", 
            "\[IndentingNewLine]", 
            RowBox[{"AppendTo", "[", 
             RowBox[{"StandardConcs", ",", 
              RowBox[{"StandardConc", "[", "StandardsFound", "]"}]}], "]"}], 
            ";", "\[IndentingNewLine]", 
            RowBox[{"AppendTo", "[", 
             RowBox[{"FullColNames", ",", 
              RowBox[{"If", "[", 
               RowBox[{
                RowBox[{
                 RowBox[{"Length", "[", 
                  RowBox[{"StringCases", "[", 
                   RowBox[{
                    RowBox[{"First", "@", 
                    RowBox[{"ColNames", "@", 
                    RowBox[{"StandardConc", "[", "StandardsFound", "]"}]}]}], 
                    ",", 
                    RowBox[{"\"\< / \>\"", "~~", "__"}]}], "]"}], "]"}], ">", 
                 "0"}], ",", 
                RowBox[{"#", "<>", 
                 RowBox[{"(", 
                  RowBox[{
                   RowBox[{"StringCases", "[", 
                    RowBox[{
                    RowBox[{"First", "@", 
                    RowBox[{"ColNames", "@", 
                    RowBox[{"StandardConc", "[", "StandardsFound", "]"}]}]}], 
                    ",", 
                    RowBox[{"\"\< / \>\"", "~~", "__"}]}], "]"}], "//", 
                   "First"}], ")"}]}], ",", "#"}], "]"}]}], "]"}]}], 
           "\[IndentingNewLine]", ")"}], "&"}], ",", "columns"}], "]"}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"OutDt", "=", "intData"}], ";", "\[IndentingNewLine]", 
       RowBox[{"MapIndexed", "[", "\[IndentingNewLine]", 
        RowBox[{
         RowBox[{
          RowBox[{"(", "\[IndentingNewLine]", 
           RowBox[{
            RowBox[{"ColName", "=", "#1"}], ";", "\[IndentingNewLine]", 
            RowBox[{"ColNum", "=", 
             RowBox[{"#2", "[", 
              RowBox[{"[", "1", "]"}], "]"}]}], ";", "\[IndentingNewLine]", 
            "\[IndentingNewLine]", 
            RowBox[{"QueryTo", "[", 
             RowBox[{"OutDt", ",", "\[IndentingNewLine]", "All", ",", 
              RowBox[{
               RowBox[{"(", 
                RowBox[{"#", "~", "Join", "~", 
                 RowBox[{"Association", "[", "\[IndentingNewLine]", 
                  RowBox[{"{", "\[IndentingNewLine]", 
                   RowBox[{
                    RowBox[{"(", 
                    RowBox[{"FullColNames", "[", 
                    RowBox[{"[", "ColNum", "]"}], "]"}], ")"}], "\[Rule]", 
                    RowBox[{"CalibrateDatum", "[", 
                    RowBox[{
                    RowBox[{"#", "[", "ColName", "]"}], ",", "ColName", ",", 
                    RowBox[{"StandardConcs", "[", 
                    RowBox[{"[", "ColNum", "]"}], "]"}], ",", "#\"Time\"", 
                    ",", 
                    RowBox[{"Interpols", "[", 
                    RowBox[{"[", "ColNum", "]"}], "]"}], ",", 
                    RowBox[{"InterpolsE", "[", 
                    RowBox[{"[", "ColNum", "]"}], "]"}], ",", 
                    RowBox[{"BInterpols", "[", 
                    RowBox[{"[", "ColNum", "]"}], "]"}], ",", 
                    RowBox[{"BInterpolsE", "[", 
                    RowBox[{"[", "ColNum", "]"}], "]"}], ",", 
                    RowBox[{"DriftInterpols", "[", 
                    RowBox[{"[", "ColNum", "]"}], "]"}], ",", 
                    RowBox[{"DriftInterpolsE", "[", 
                    RowBox[{"[", "ColNum", "]"}], "]"}], ",", "opts"}], 
                    "]"}]}], "\[IndentingNewLine]", "}"}], 
                  "\[IndentingNewLine]", "]"}]}], ")"}], "&"}], ",", 
              "\[IndentingNewLine]", 
              RowBox[{"FailureAction", "\[Rule]", "None"}]}], 
             "\[IndentingNewLine]", "]"}], ";"}], "\[IndentingNewLine]", 
           ")"}], "&"}], ",", "columns"}], "\[IndentingNewLine]", "]"}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"OutDt", "=", 
        RowBox[{"OutDt", "/.", 
         RowBox[{"{", 
          RowBox[{"x_Complex", "\[Rule]", "\[Placeholder]"}], "}"}]}]}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"On", "[", 
        StyleBox[
         RowBox[{"Interpolation", "::", "inhr"}], "MessageName"], "]"}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"On", "[", 
        StyleBox[
         RowBox[{"InterpolatingFunction", "::", "dmval"}], "MessageName"], 
        "]"}], ";", "\[IndentingNewLine]", 
       RowBox[{"On", "[", 
        RowBox[{"FittedModel", "::", "varzero"}], "]"}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"OutDt", "[", 
        RowBox[{"All", ",", "FullColNames"}], "]"}]}], "\[IndentingNewLine]", 
      ")"}]}], "\[IndentingNewLine]", "]"}]}], ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"a864546c-ae96-408e-a905-3556a0f423f7"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "CalibrationInterpolations", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{
     RowBox[{"IncludeConstantBasis", "\[Rule]", "False"}], ",", 
     RowBox[{"IncludeQuadraticTerm", "\[Rule]", "False"}], ",", 
     RowBox[{"BlankPattern", "\[Rule]", "Null"}], ",", 
     RowBox[{"DriftStandard", "\[Rule]", "Null"}]}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CalibrationInterpolations", "[", 
    RowBox[{"intData_", ",", "standardsFound_", ",", "column_", ",", 
     RowBox[{"opts", ":", 
      RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
   RowBox[{"Module", "[", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{"(*", 
       RowBox[{"BInterpols", ",", "BInterpolsE", ","}], "*)"}], 
      RowBox[{"Interpols", ",", "InterpolsE", ",", "DriftReference", ",", 
       RowBox[{"DriftInterpols", "=", 
        RowBox[{"1", "&"}]}], ",", 
       RowBox[{"DriftInterpolsE", "=", 
        RowBox[{"0", "&"}]}], ",", "V"}], "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"(", "\[IndentingNewLine]", 
      RowBox[{"(*", " ", 
       RowBox[{
       "Interpolations", " ", "for", " ", "standard", " ", "intensities"}], 
       " ", "*)"}], "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"Interpols", "=", 
        RowBox[{"Map", "[", 
         RowBox[{
          RowBox[{
           RowBox[{"(", "\[IndentingNewLine]", 
            RowBox[{
             RowBox[{"V", "=", 
              RowBox[{"Values", "@", 
               RowBox[{"Normal", "@", 
                RowBox[{
                 RowBox[{"intData", "[", 
                  RowBox[{"Select", "[", 
                   RowBox[{
                    RowBox[{"NumericDVQ", "[", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                    "]"}], "&"}], "]"}], "]"}], "[", 
                 RowBox[{
                  RowBox[{"___", "~~", "#", "~~", "___"}], ",", 
                  RowBox[{
                   RowBox[{"{", 
                    RowBox[{
                    RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                    RowBox[{"DVMeans", "@", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}]}],
                     "}"}], "&"}]}], "]"}]}]}]}], ";", "\[IndentingNewLine]", 
             RowBox[{"If", "[", 
              RowBox[{
               RowBox[{
                RowBox[{"Length", "[", "V", "]"}], ">", "0"}], ",", 
               RowBox[{"Interpolation", "[", 
                RowBox[{"V", ",", 
                 RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}], ",", 
               RowBox[{
                RowBox[{"Missing", "[", "]"}], "&"}]}], "]"}]}], 
            "\[IndentingNewLine]", ")"}], "&"}], ",", "standardsFound"}], 
         "]"}]}], ";", "\[IndentingNewLine]", 
       RowBox[{"InterpolsE", "=", 
        RowBox[{"Map", "[", "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{
           RowBox[{"(", "\[IndentingNewLine]", 
            RowBox[{
             RowBox[{"V", "=", 
              RowBox[{"Values", "@", 
               RowBox[{"Normal", "@", 
                RowBox[{
                 RowBox[{"intData", "[", 
                  RowBox[{"Select", "[", 
                   RowBox[{
                    RowBox[{"NumericDVQ", "[", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                    "]"}], "&"}], "]"}], "]"}], "[", 
                 RowBox[{
                  RowBox[{"___", "~~", "#", "~~", "___"}], ",", 
                  RowBox[{
                   RowBox[{"{", 
                    RowBox[{
                    RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                    RowBox[{"DVSEs", "@", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}]}],
                     "}"}], "&"}]}], "]"}]}]}]}], ";", "\[IndentingNewLine]", 
             RowBox[{"If", "[", 
              RowBox[{
               RowBox[{
                RowBox[{"Length", "[", "V", "]"}], ">", "0"}], ",", 
               RowBox[{"Interpolation", "[", 
                RowBox[{"V", ",", 
                 RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}], ",", 
               RowBox[{
                RowBox[{"Missing", "[", "]"}], "&"}]}], "]"}]}], 
            "\[IndentingNewLine]", ")"}], "&"}], ",", "standardsFound"}], 
         "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"(*", " ", 
        RowBox[{
        "Interpolations", " ", "for", " ", "blank", " ", "intensities"}], " ",
         "*)"}], "\[IndentingNewLine]", 
       RowBox[{"(*", 
        RowBox[{
         RowBox[{"If", "[", 
          RowBox[{
           RowBox[{"TrueQ", "[", 
            RowBox[{
             RowBox[{"OptionValue", "[", "BlankPattern", "]"}], "\[Equal]", 
             "Null"}], "]"}], ",", "\[IndentingNewLine]", 
           RowBox[{
            RowBox[{"BInterpols", "=", 
             RowBox[{"(", 
              RowBox[{"0", "&"}], ")"}]}], ";", "\[IndentingNewLine]", 
            RowBox[{"BInterpolsE", "=", 
             RowBox[{"(", 
              RowBox[{"0", "&"}], ")"}]}], ";"}], "\[IndentingNewLine]", ",", 
           "\[IndentingNewLine]", 
           RowBox[{
            RowBox[{"BInterpols", "=", 
             RowBox[{"Interpolation", "[", 
              RowBox[{
               RowBox[{"Values", "@", 
                RowBox[{"Normal", "@", 
                 RowBox[{
                  RowBox[{"intData", "[", 
                   RowBox[{"Select", "[", 
                    RowBox[{
                    RowBox[{"NumericDVQ", "[", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                    "]"}], "&"}], "]"}], "]"}], "[", 
                  RowBox[{
                   RowBox[{"OptionValue", "[", "BlankPattern", "]"}], ",", 
                   RowBox[{
                    RowBox[{"{", 
                    RowBox[{
                    RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                    RowBox[{"DVMeans", "@", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}]}],
                     "}"}], "&"}]}], "]"}]}]}], ",", 
               RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}]}], ";",
             "\[IndentingNewLine]", 
            RowBox[{"BInterpolsE", "=", 
             RowBox[{"Interpolation", "[", 
              RowBox[{
               RowBox[{"Values", "@", 
                RowBox[{"Normal", "@", 
                 RowBox[{
                  RowBox[{"intData", "[", 
                   RowBox[{"Select", "[", 
                    RowBox[{
                    RowBox[{"NumericDVQ", "[", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                    "]"}], "&"}], "]"}], "]"}], "[", 
                  RowBox[{
                   RowBox[{"OptionValue", "[", "BlankPattern", "]"}], ",", 
                   RowBox[{
                    RowBox[{"{", 
                    RowBox[{
                    RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                    RowBox[{"DVSEs", "@", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}]}],
                     "}"}], "&"}]}], "]"}]}]}], ",", 
               RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}]}], 
            ";"}]}], "\[IndentingNewLine]", "]"}], ";"}], "*)"}], 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"StringQ", "[", 
          RowBox[{"OptionValue", "[", "DriftStandard", "]"}], "]"}], ",", 
         "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"V", "=", 
           RowBox[{"Values", "@", 
            RowBox[{"Normal", "@", 
             RowBox[{
              RowBox[{"intData", "[", 
               RowBox[{"Select", "[", 
                RowBox[{
                 RowBox[{"NumericDVQ", "[", 
                  RowBox[{
                   RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                  "]"}], "&"}], "]"}], "]"}], "[", 
              RowBox[{
               RowBox[{"___", "~~", 
                RowBox[{"(", 
                 RowBox[{"OptionValue", "[", "DriftStandard", "]"}], ")"}], "~~",
                 "___"}], ",", 
               RowBox[{
                RowBox[{"{", 
                 RowBox[{
                  RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                  RowBox[{"DVMeans", "@", 
                   RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}]}],
                  "}"}], "&"}]}], "]"}]}]}]}], ";", "\[IndentingNewLine]", 
          RowBox[{"DriftReference", "=", 
           RowBox[{"If", "[", 
            RowBox[{
             RowBox[{
              RowBox[{"Length", "[", "V", "]"}], ">", "0"}], ",", 
             RowBox[{"Interpolation", "[", 
              RowBox[{"V", ",", 
               RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}], ",", 
             RowBox[{
              RowBox[{"Missing", "[", "]"}], "&"}]}], "]"}]}], ";", 
          "\[IndentingNewLine]", "\[IndentingNewLine]", 
          RowBox[{"V", "=", 
           RowBox[{"Values", "@", 
            RowBox[{"Normal", "@", 
             RowBox[{
              RowBox[{"intData", "[", 
               RowBox[{"Select", "[", 
                RowBox[{
                 RowBox[{"NumericDVQ", "[", 
                  RowBox[{
                   RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                  "]"}], "&"}], "]"}], "]"}], "[", 
              RowBox[{
               RowBox[{"___", "~~", 
                RowBox[{"(", 
                 RowBox[{"\"\<DRIFT\>\"", "|", 
                  RowBox[{"OptionValue", "[", "DriftStandard", "]"}]}], ")"}],
                 "~~", "___"}], ",", 
               RowBox[{
                RowBox[{"{", 
                 RowBox[{
                  RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                  RowBox[{
                   RowBox[{"(", 
                    RowBox[{"DVMeans", "@", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}], 
                    ")"}], "/", 
                   RowBox[{"DriftReference", "[", 
                    RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], 
                    "]"}]}]}], "}"}], "&"}], ",", 
               RowBox[{"FailureAction", "\[Rule]", "None"}]}], "]"}]}]}]}], 
          ";", "\[IndentingNewLine]", 
          RowBox[{"DriftInterpols", "=", 
           RowBox[{"If", "[", 
            RowBox[{
             RowBox[{
              RowBox[{"Length", "[", "V", "]"}], ">", "0"}], ",", 
             RowBox[{"Interpolation", "[", 
              RowBox[{"V", ",", 
               RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}], ",", 
             RowBox[{
              RowBox[{"Missing", "[", "]"}], "&"}]}], "]"}]}], ";", 
          "\[IndentingNewLine]", "\[IndentingNewLine]", 
          RowBox[{"V", "=", 
           RowBox[{"Values", "@", 
            RowBox[{"Normal", "@", 
             RowBox[{
              RowBox[{"intData", "[", 
               RowBox[{"Select", "[", 
                RowBox[{
                 RowBox[{"NumericDVQ", "[", 
                  RowBox[{
                   RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                  "]"}], "&"}], "]"}], "]"}], "[", 
              RowBox[{
               RowBox[{"___", "~~", 
                RowBox[{"(", 
                 RowBox[{"\"\<DRIFT\>\"", "|", 
                  RowBox[{"OptionValue", "[", "DriftStandard", "]"}]}], ")"}],
                 "~~", "___"}], ",", 
               RowBox[{
                RowBox[{"{", 
                 RowBox[{
                  RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                  RowBox[{
                   RowBox[{"(", 
                    RowBox[{"DVSEs", "@", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}], 
                    ")"}], "/", 
                   RowBox[{"DriftReference", "[", 
                    RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], 
                    "]"}]}]}], "}"}], "&"}], ",", 
               RowBox[{"FailureAction", "\[Rule]", "None"}]}], "]"}]}]}]}], 
          ";", "\[IndentingNewLine]", 
          RowBox[{"DriftInterpolsE", "=", 
           RowBox[{"If", "[", 
            RowBox[{
             RowBox[{
              RowBox[{"Length", "[", "V", "]"}], ">", "0"}], ",", 
             RowBox[{"Interpolation", "[", 
              RowBox[{"V", ",", 
               RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}], ",", 
             RowBox[{
              RowBox[{"Missing", "[", "]"}], "&"}]}], "]"}]}], ";"}]}], 
        "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", 
       "\[IndentingNewLine]", 
       RowBox[{"Join", "[", 
        RowBox[{
         RowBox[{"{", 
          RowBox[{"Interpols", ",", "InterpolsE"}], "}"}], ",", 
         RowBox[{"BlankInterpolations", "[", 
          RowBox[{"intData", ",", "column", ",", 
           RowBox[{"FilterRules", "[", 
            RowBox[{
             RowBox[{"{", "opts", "}"}], ",", " ", 
             RowBox[{"Options", "[", "BlankInterpolations", "]"}]}], "]"}]}], 
          "]"}], ",", 
         RowBox[{"{", 
          RowBox[{"DriftInterpols", ",", "DriftInterpolsE"}], "}"}]}], 
        "]"}]}], "\[IndentingNewLine]", 
      RowBox[{"(*", 
       RowBox[{"{", 
        RowBox[{
        "Interpols", ",", "InterpolsE", ",", "BInterpols", ",", "BInterpolsE",
          ",", "DriftInterpols", ",", "DriftInterpolsE"}], "}"}], "*)"}], 
      "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}]}], 
  ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"2cadb111-9064-4b6e-93b4-353cf456679e"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "BlankInterpolations", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{"BlankPattern", "\[Rule]", "Null"}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"BlankInterpolations", "[", 
   RowBox[{"intData_", ",", "column_", ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"BInterpols", ",", "BInterpolsE"}], "}"}], ",", 
    "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{"TrueQ", "[", 
         RowBox[{
          RowBox[{"OptionValue", "[", "BlankPattern", "]"}], "\[Equal]", 
          "Null"}], "]"}], ",", "\[IndentingNewLine]", 
        RowBox[{
         RowBox[{"BInterpols", "=", 
          RowBox[{"(", 
           RowBox[{"0", "&"}], ")"}]}], ";", "\[IndentingNewLine]", 
         RowBox[{"BInterpolsE", "=", 
          RowBox[{"(", 
           RowBox[{"0", "&"}], ")"}]}], ";"}], "\[IndentingNewLine]", ",", 
        "\[IndentingNewLine]", 
        RowBox[{
         RowBox[{"BInterpols", "=", 
          RowBox[{"Interpolation", "[", 
           RowBox[{
            RowBox[{"Values", "@", 
             RowBox[{"Normal", "@", 
              RowBox[{
               RowBox[{"intData", "[", 
                RowBox[{"Select", "[", 
                 RowBox[{
                  RowBox[{"NumericDVQ", "[", 
                   RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                   "]"}], "&"}], "]"}], "]"}], "[", 
               RowBox[{
                RowBox[{"OptionValue", "[", "BlankPattern", "]"}], ",", 
                RowBox[{
                 RowBox[{"{", 
                  RowBox[{
                   RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                   RowBox[{"DVMeans", "@", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}]}],
                   "}"}], "&"}]}], "]"}]}]}], ",", 
            RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}]}], ";", 
         "\[IndentingNewLine]", 
         RowBox[{"BInterpolsE", "=", 
          RowBox[{"Interpolation", "[", 
           RowBox[{
            RowBox[{"Values", "@", 
             RowBox[{"Normal", "@", 
              RowBox[{
               RowBox[{"intData", "[", 
                RowBox[{"Select", "[", 
                 RowBox[{
                  RowBox[{"NumericDVQ", "[", 
                   RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}], 
                   "]"}], "&"}], "]"}], "]"}], "[", 
               RowBox[{
                RowBox[{"OptionValue", "[", "BlankPattern", "]"}], ",", 
                RowBox[{
                 RowBox[{"{", 
                  RowBox[{
                   RowBox[{"AbsoluteTime", "[", "#\"Time\"", "]"}], ",", 
                   RowBox[{"DVSEs", "@", 
                    RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "column", "]"}]}]}],
                   "}"}], "&"}]}], "]"}]}]}], ",", 
            RowBox[{"InterpolationOrder", "\[Rule]", "1"}]}], "]"}]}], 
         ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"{", 
       RowBox[{"BInterpols", ",", "BInterpolsE"}], "}"}]}], 
     "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}]}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"c76fa7bf-a08a-4ffc-af7a-199d7d3302e4"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "RemoveNonDetectableValues", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{"BlankPattern", "\[Rule]", "Null"}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"RemoveNonDetectableValues", "::", "usage"}], "=", 
   "\"\<RemoveNonDetectableValues[datatable,{\!\(\*SubscriptBox[\(column\), \
\(i\)]\),\!\(\*SubscriptBox[\(column\), \(2\)]\),...}] removes values in \
intData that are below the detection limit based on blank measurements \
specified by the BlankPattern option.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"RemoveNonDetectableValues", "[", 
   RowBox[{"intData_DataTable", ",", "columns_List", ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{
     "IntDt", ",", "OutDt", ",", "BlankValues", ",", "ColName", ",", 
      "ColNum"}], "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"IntDt", "=", "intData"}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"Off", "[", 
       StyleBox[
        RowBox[{"InterpolatingFunction", "::", "dmval"}], "MessageName"], 
       "]"}], ";", "\[IndentingNewLine]", 
      RowBox[{"Off", "[", 
       StyleBox[
        RowBox[{"Interpolation", "::", "inhr"}], "MessageName"], "]"}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"OutDt", "=", "intData"}], ";", "\[IndentingNewLine]", 
      RowBox[{"MapIndexed", "[", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{
         RowBox[{"(", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"ColName", "=", "#1"}], ";", "\[IndentingNewLine]", 
           RowBox[{"ColNum", "=", 
            RowBox[{"#2", "[", 
             RowBox[{"[", "1", "]"}], "]"}]}], ";", "\[IndentingNewLine]", 
           "\[IndentingNewLine]", 
           RowBox[{"BlankValues", "=", 
            RowBox[{"Values", "@", 
             RowBox[{"Normal", "@", 
              RowBox[{
               RowBox[{"intData", "[", 
                RowBox[{"Select", "[", 
                 RowBox[{
                  RowBox[{"NumericDVQ", "[", 
                   RowBox[{
                    RowBox[{"Slot", "[", "1", "]"}], "[", "ColName", "]"}], 
                   "]"}], "&"}], "]"}], "]"}], "[", 
               RowBox[{
                RowBox[{"OptionValue", "[", "BlankPattern", "]"}], ",", 
                RowBox[{
                 RowBox[{"DVMeans", "@", 
                  RowBox[{
                   RowBox[{"Slot", "[", "1", "]"}], "[", "ColName", "]"}]}], 
                 "&"}]}], "]"}]}]}]}], ";", "\[IndentingNewLine]", 
           "\[IndentingNewLine]", 
           RowBox[{"QueryTo", "[", 
            RowBox[{"OutDt", ",", "\[IndentingNewLine]", 
             RowBox[{
              RowBox[{"KeyValueMap", "[", 
               RowBox[{
                RowBox[{
                 RowBox[{"<|", 
                  RowBox[{"#1", "\[Rule]", 
                   RowBox[{"If", "[", 
                    RowBox[{
                    RowBox[{"StringMatchQ", "[", 
                    RowBox[{"#1", ",", 
                    RowBox[{"OptionValue", "[", "BlankPattern", "]"}]}], 
                    "]"}], ",", "#2", ",", 
                    RowBox[{"(", 
                    RowBox[{"#2", "~", "Join", "~", 
                    RowBox[{"Association", "[", "\[IndentingNewLine]", 
                    RowBox[{"{", "\[IndentingNewLine]", 
                    RowBox[{"ColName", "\[Rule]", 
                    RowBox[{"TestDetectionLimit", "[", 
                    RowBox[{
                    RowBox[{"#2", "[", "ColName", "]"}], ",", 
                    RowBox[{"#2", "[", "\"\<Time\>\"", "]"}], ",", 
                    RowBox[{"Mean", "[", "BlankValues", "]"}], ",", 
                    RowBox[{"StandardDeviation", "[", "BlankValues", "]"}]}], 
                    "]"}]}], "\[IndentingNewLine]", "}"}], 
                    "\[IndentingNewLine]", "]"}]}], ")"}]}], "]"}]}], "|>"}], 
                 "&"}], ",", "#"}], "]"}], "&"}], ",", "\[IndentingNewLine]", 
             RowBox[{"FailureAction", "\[Rule]", "None"}]}], 
            "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", 
           "\[IndentingNewLine]", 
           RowBox[{"OutDt", "=", 
            RowBox[{"DataTable", "[", 
             RowBox[{"<|", 
              RowBox[{"OutDt", "//", "Normal"}], "|>"}], "]"}]}], ";"}], 
          "\[IndentingNewLine]", ")"}], "&"}], ",", "columns"}], 
       "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"On", "[", 
       StyleBox[
        RowBox[{"Interpolation", "::", "inhr"}], "MessageName"], "]"}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"On", "[", 
       StyleBox[
        RowBox[{"InterpolatingFunction", "::", "dmval"}], "MessageName"], 
       "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"DataTable", "[", 
       RowBox[{"<|", 
        RowBox[{"OutDt", "//", "Normal"}], "|>"}], "]"}]}], 
     "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}]}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"1aae6e4a-6a17-4e36-b76c-44545c172a72"],

Cell[BoxData[
 RowBox[{
  RowBox[{"TestDetectionLimit", "[", 
   RowBox[{"datum_", ",", "time_", ",", "bMean_", ",", "bSTD_"}], "]"}], ":=", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", "}"}], ",", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{"MissingQ", "[", "datum", "]"}], ",", 
        RowBox[{"Return", "[", "datum", "]"}]}], "]"}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{"!", 
         RowBox[{"NumericQ", "[", 
          RowBox[{"DVMeans", "[", "datum", "]"}], "]"}]}], ",", 
        RowBox[{"Return", "[", "datum", "]"}]}], "]"}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"DVMeans", "[", "datum", "]"}], "<", 
         RowBox[{"(", 
          RowBox[{"bMean", "+", 
           RowBox[{"3", "*", "bSTD"}]}], ")"}]}], ",", "\[IndentingNewLine]", 
        RowBox[{"Missing", "[", 
         RowBox[{"\"\<BelowDL\>\"", ",", 
          RowBox[{"(", 
           RowBox[{"bMean", "+", 
            RowBox[{"3", "*", "bSTD"}]}], ")"}]}], "]"}], ",", 
        "\[IndentingNewLine]", 
        RowBox[{"(*", 
         RowBox[{"If", "[", 
          RowBox[{
           RowBox[{
            RowBox[{"DVMeans", "[", "datum", "]"}], "<", 
            RowBox[{"(", 
             RowBox[{
              RowBox[{"bInterpol", "[", 
               RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], "+", 
              RowBox[{"10", "*", 
               RowBox[{"bInterpolE", "[", 
                RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}]}]}], 
             ")"}]}], ",", "\[IndentingNewLine]", 
           RowBox[{"Missing", "[", 
            RowBox[{"\"\<BelowQL\>\"", ",", 
             RowBox[{"(", 
              RowBox[{
               RowBox[{"bInterpol", "[", 
                RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], "+", 
               RowBox[{"10", "*", 
                RowBox[{"bInterpolE", "[", 
                 RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}]}]}], 
              ")"}]}], "]"}], ",", "\[IndentingNewLine]", "datum"}], 
          "\[IndentingNewLine]", "]"}], "*)"}], "\[IndentingNewLine]", 
        "datum"}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", 
     ")"}]}], "]"}]}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"6e5f865b-3735-40ca-bdb1-fc552d4c4049"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "CalibrateDatum", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{
     RowBox[{"IncludeConstantBasis", "\[Rule]", "False"}], ",", 
     RowBox[{"IncludeQuadraticTerm", "\[Rule]", "False"}], ",", 
     RowBox[{"BlankPattern", "\[Rule]", "Null"}], ",", 
     RowBox[{"DriftStandard", "\[Rule]", "Null"}]}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CalibrateDatum", "[", 
    RowBox[{
    "datum_", ",", "column_", ",", "standardConc_", ",", "time_", ",", 
     "interpol_", ",", "interpolE_", ",", "bInterpol_", ",", "bInterpolE_", 
     ",", "driftInterpol_", ",", "driftInterpolE_", ",", 
     RowBox[{"opts", ":", 
      RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
   RowBox[{"Module", "[", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{
      "FitF", ",", "FitDat", ",", "FitDatE", ",", "WeightDat", ",", "Params", 
       ",", "BlankEV", ",", "DriftEV", ",", "Output", ",", "FD", ",", "Val"}],
       "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"(", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"MatchQ", "[", 
          RowBox[{"datum", ",", 
           RowBox[{"Missing", "[", 
            RowBox[{
             RowBox[{"Except", "[", 
              RowBox[{"\"\<BelowDL\>\"", "|", "\"\<BelowQL\>\""}], "]"}], ",",
              "___"}], "]"}]}], "]"}], ",", 
         RowBox[{"Return", "[", "datum", "]"}]}], "]"}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"FitDat", "=", 
        RowBox[{
         RowBox[{"{", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"DVMeans", "@", 
            RowBox[{"Flatten", "@", 
             RowBox[{"Values", "@", 
              RowBox[{"Values", "@", 
               RowBox[{"Normal", "@", "standardConc"}]}]}]}]}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{
            RowBox[{
             RowBox[{
              RowBox[{"#", "[", 
               RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], "&"}], "/@",
              "interpol"}], "-", 
            RowBox[{"bInterpol", "[", 
             RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}]}]}], 
          "\[IndentingNewLine]", "}"}], "//", "Transpose"}]}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"FitDatE", "=", 
        RowBox[{
         RowBox[{"{", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"DVSEs", "@", 
            RowBox[{"Flatten", "@", 
             RowBox[{"Values", "@", 
              RowBox[{"Values", "@", 
               RowBox[{"Normal", "@", "standardConc"}]}]}]}]}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{
            RowBox[{
             RowBox[{
              RowBox[{"#", "[", 
               RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], "&"}], "/@",
              "interpolE"}], "+", 
            RowBox[{"bInterpolE", "[", 
             RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}]}]}], 
          "\[IndentingNewLine]", "}"}], "//", "Transpose"}]}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"FD", "=", 
        RowBox[{"MapThread", "[", 
         RowBox[{
          RowBox[{
           RowBox[{"{", 
            RowBox[{"#1", ",", "#2"}], "}"}], "&"}], ",", 
          RowBox[{"{", 
           RowBox[{"FitDat", ",", "FitDatE"}], "}"}]}], "]"}]}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"FD", "=", 
        RowBox[{"DeleteMissing", "[", 
         RowBox[{"FD", ",", "1", ",", "Infinity"}], "]"}]}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"FitF", "=", 
        RowBox[{"If", "[", 
         RowBox[{
          RowBox[{"TrueQ", "[", 
           RowBox[{"OptionValue", "[", "IncludeQuadraticTerm", "]"}], "]"}], 
          ",", "\[IndentingNewLine]", 
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{"TrueQ", "[", 
             RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}], "]"}],
             ",", "\[IndentingNewLine]", 
            RowBox[{"EffectiveVarianceFit", "[", 
             RowBox[{
              RowBox[{"FD", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
              RowBox[{"FD", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", 
              RowBox[{
               RowBox[{"a", "*", 
                RowBox[{"x", "^", "2"}]}], "+", 
               RowBox[{"b", "*", "x"}], "+", "c"}], ",", 
              RowBox[{"{", 
               RowBox[{"a", ",", "b", ",", "c"}], "}"}], ",", 
              RowBox[{"{", "x", "}"}], ",", "5"}], "]"}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{"EffectiveVarianceFit", "[", 
             RowBox[{
              RowBox[{"FD", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
              RowBox[{"FD", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", 
              RowBox[{
               RowBox[{"a", "*", 
                RowBox[{"x", "^", "2"}]}], "+", 
               RowBox[{"b", "*", "x"}]}], ",", 
              RowBox[{"{", 
               RowBox[{"a", ",", "b"}], "}"}], ",", 
              RowBox[{"{", "x", "}"}], ",", "5"}], "]"}]}], 
           "\[IndentingNewLine]", "]"}], ",", "\[IndentingNewLine]", 
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{"TrueQ", "[", 
             RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}], "]"}],
             ",", "\[IndentingNewLine]", 
            RowBox[{"EffectiveVarianceFit", "[", 
             RowBox[{
              RowBox[{"FD", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
              RowBox[{"FD", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", 
              RowBox[{
               RowBox[{"b", "*", "x"}], "+", "c"}], ",", 
              RowBox[{"{", 
               RowBox[{"b", ",", "c"}], "}"}], ",", 
              RowBox[{"{", "x", "}"}], ",", "5"}], "]"}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{"EffectiveVarianceFit", "[", 
             RowBox[{
              RowBox[{"FD", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
              RowBox[{"FD", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", 
              RowBox[{"b", "*", "x"}], ",", 
              RowBox[{"{", "b", "}"}], ",", 
              RowBox[{"{", "x", "}"}], ",", "5"}], "]"}]}], 
           "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}]}], 
       ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"Params", "=", 
        RowBox[{"If", "[", 
         RowBox[{
          RowBox[{"TrueQ", "[", 
           RowBox[{"OptionValue", "[", "IncludeQuadraticTerm", "]"}], "]"}], 
          ",", "\[IndentingNewLine]", 
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{"TrueQ", "[", 
             RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}], "]"}],
             ",", "\[IndentingNewLine]", 
            RowBox[{"If", "[", 
             RowBox[{
              RowBox[{
               RowBox[{"Length", "[", 
                RowBox[{"FD", "[", 
                 RowBox[{"[", 
                  RowBox[{"All", ",", "1"}], "]"}], "]"}], "]"}], "\[Equal]", 
               "3"}], ",", "\[IndentingNewLine]", 
              RowBox[{"{", "\[IndentingNewLine]", 
               RowBox[{
                RowBox[{"a", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"a", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", "0"}], "]"}]}], ",", "\[IndentingNewLine]", 
                RowBox[{"b", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"b", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", "0"}], "]"}]}], ",", "\[IndentingNewLine]", 
                RowBox[{"c", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"c", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", "0"}], "]"}]}]}], "\[IndentingNewLine]", "}"}], ",", 
              "\[IndentingNewLine]", 
              RowBox[{"{", "\[IndentingNewLine]", 
               RowBox[{
                RowBox[{"a", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"a", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", 
                   RowBox[{
                    RowBox[{"FitF", "[", "\"\<ParameterErrors\>\"", "]"}], 
                    "[", 
                    RowBox[{"[", "1", "]"}], "]"}]}], "]"}]}], ",", 
                "\[IndentingNewLine]", 
                RowBox[{"b", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"b", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", 
                   RowBox[{
                    RowBox[{"FitF", "[", "\"\<ParameterErrors\>\"", "]"}], 
                    "[", 
                    RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}], ",", 
                "\[IndentingNewLine]", 
                RowBox[{"c", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"c", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", 
                   RowBox[{
                    RowBox[{"FitF", "[", "\"\<ParameterErrors\>\"", "]"}], 
                    "[", 
                    RowBox[{"[", "3", "]"}], "]"}]}], "]"}]}]}], 
               "\[IndentingNewLine]", "}"}]}], "\[IndentingNewLine]", "]"}], 
            ",", "\[IndentingNewLine]", 
            RowBox[{"If", "[", 
             RowBox[{
              RowBox[{
               RowBox[{"Length", "[", 
                RowBox[{"FD", "[", 
                 RowBox[{"[", 
                  RowBox[{"All", ",", "1"}], "]"}], "]"}], "]"}], "\[Equal]", 
               "2"}], ",", "\[IndentingNewLine]", 
              RowBox[{"{", "\[IndentingNewLine]", 
               RowBox[{
                RowBox[{"a", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"a", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", "0"}], "]"}]}], ",", "\[IndentingNewLine]", 
                RowBox[{"b", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"b", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", "0"}], "]"}]}]}], "\[IndentingNewLine]", "}"}], ",", 
              "\[IndentingNewLine]", 
              RowBox[{"{", "\[IndentingNewLine]", 
               RowBox[{
                RowBox[{"a", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"a", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", 
                   RowBox[{
                    RowBox[{"FitF", "[", "\"\<ParameterErrors\>\"", "]"}], 
                    "[", 
                    RowBox[{"[", "1", "]"}], "]"}]}], "]"}]}], ",", 
                "\[IndentingNewLine]", 
                RowBox[{"b", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"b", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", 
                   RowBox[{
                    RowBox[{"FitF", "[", "\"\<ParameterErrors\>\"", "]"}], 
                    "[", 
                    RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}]}], 
               "\[IndentingNewLine]", "}"}]}], "\[IndentingNewLine]", "]"}]}],
            "\[IndentingNewLine]", "]"}], ",", "\[IndentingNewLine]", 
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{"TrueQ", "[", 
             RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}], "]"}],
             ",", "\[IndentingNewLine]", 
            RowBox[{"If", "[", 
             RowBox[{
              RowBox[{
               RowBox[{"Length", "[", 
                RowBox[{"FD", "[", 
                 RowBox[{"[", 
                  RowBox[{"All", ",", "1"}], "]"}], "]"}], "]"}], "\[Equal]", 
               "2"}], ",", "\[IndentingNewLine]", 
              RowBox[{"{", "\[IndentingNewLine]", 
               RowBox[{
                RowBox[{"b", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"b", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", "0"}], "]"}]}], ",", "\[IndentingNewLine]", 
                RowBox[{"c", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"c", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", "0"}], "]"}]}]}], "\[IndentingNewLine]", "}"}], ",", 
              "\[IndentingNewLine]", 
              RowBox[{"{", "\[IndentingNewLine]", 
               RowBox[{
                RowBox[{"b", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"b", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", 
                   RowBox[{
                    RowBox[{"FitF", "[", "\"\<ParameterErrors\>\"", "]"}], 
                    "[", 
                    RowBox[{"[", "1", "]"}], "]"}]}], "]"}]}], ",", 
                "\[IndentingNewLine]", 
                RowBox[{"c", "\[Rule]", 
                 RowBox[{"NDV", "[", 
                  RowBox[{
                   RowBox[{"c", "/.", 
                    RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}],
                    ",", 
                   RowBox[{
                    RowBox[{"FitF", "[", "\"\<ParameterErrors\>\"", "]"}], 
                    "[", 
                    RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}]}], 
               "\[IndentingNewLine]", "}"}]}], "\[IndentingNewLine]", "]"}], 
            ",", "\[IndentingNewLine]", 
            RowBox[{"If", "[", 
             RowBox[{
              RowBox[{
               RowBox[{"Length", "[", 
                RowBox[{"FD", "[", 
                 RowBox[{"[", 
                  RowBox[{"All", ",", "1"}], "]"}], "]"}], "]"}], "\[Equal]", 
               "1"}], ",", "\[IndentingNewLine]", 
              RowBox[{"{", "\[IndentingNewLine]", 
               RowBox[{"b", "\[Rule]", 
                RowBox[{"NDV", "[", 
                 RowBox[{
                  RowBox[{"b", "/.", 
                   RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}], 
                  ",", "0"}], "]"}]}], "\[IndentingNewLine]", "}"}], ",", 
              "\[IndentingNewLine]", 
              RowBox[{"{", "\[IndentingNewLine]", 
               RowBox[{"b", "\[Rule]", 
                RowBox[{"NDV", "[", 
                 RowBox[{
                  RowBox[{"b", "/.", 
                   RowBox[{"FitF", "[", "\"\<BestFitParameters\>\"", "]"}]}], 
                  ",", 
                  RowBox[{
                   RowBox[{"FitF", "[", "\"\<ParameterErrors\>\"", "]"}], "[", 
                   RowBox[{"[", "1", "]"}], "]"}]}], "]"}]}], 
               "\[IndentingNewLine]", "}"}]}], "\[IndentingNewLine]", "]"}]}],
            "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}]}], 
       ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"BlankEV", "=", 
        RowBox[{"If", "[", 
         RowBox[{
          RowBox[{
           RowBox[{"bInterpolE", "[", 
            RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], ">", "0"}], 
          ",", 
          RowBox[{"NDV", "[", 
           RowBox[{
            RowBox[{"bInterpol", "[", 
             RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], ",", 
            RowBox[{"bInterpolE", "[", 
             RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}]}], "]"}], ",", 
          RowBox[{"bInterpol", "[", 
           RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}]}], "]"}]}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"DriftEV", "=", 
        RowBox[{"If", "[", 
         RowBox[{
          RowBox[{"NumericQ", "[", 
           RowBox[{"driftInterpolE", "[", 
            RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], "]"}], ",", 
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{
             RowBox[{"driftInterpolE", "[", 
              RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], ">", "0"}], 
            ",", 
            RowBox[{"NDV", "[", 
             RowBox[{
              RowBox[{"driftInterpol", "[", 
               RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}], ",", 
              RowBox[{"driftInterpolE", "[", 
               RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}]}], "]"}], 
            ",", 
            RowBox[{"driftInterpol", "[", 
             RowBox[{"AbsoluteTime", "[", "time", "]"}], "]"}]}], "]"}], ",", 
          "1"}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"Val", "=", "datum"}], ";", "\[IndentingNewLine]", 
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"MissingQ", "[", "datum", "]"}], ",", 
         RowBox[{"(", 
          RowBox[{"Val", "=", 
           RowBox[{"datum", "[", 
            RowBox[{"[", "2", "]"}], "]"}]}], ")"}]}], "]"}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"Output", "=", 
        RowBox[{
         RowBox[{"If", "[", 
          RowBox[{
           RowBox[{"TrueQ", "[", 
            RowBox[{"OptionValue", "[", "IncludeQuadraticTerm", "]"}], "]"}], 
           ",", "\[IndentingNewLine]", 
           RowBox[{"If", "[", 
            RowBox[{
             RowBox[{"TrueQ", "[", 
              RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}], 
              "]"}], ",", "\[IndentingNewLine]", 
             RowBox[{"(", "\[IndentingNewLine]", 
              RowBox[{
               RowBox[{"(", 
                RowBox[{
                 RowBox[{"-", "b"}], "+", 
                 RowBox[{"Sqrt", "[", 
                  RowBox[{
                   RowBox[{"b", "^", "2"}], "+", 
                   RowBox[{"4", "*", "a", "*", 
                    RowBox[{"(", 
                    RowBox[{"(", 
                    RowBox[{
                    RowBox[{"Val", "/", "DriftEV"}], "-", "BlankEV"}], ")"}], 
                    ")"}]}], "-", 
                   RowBox[{"4", "*", "a", "*", "c"}]}], "]"}]}], ")"}], "/", 
               RowBox[{"(", 
                RowBox[{"2", "*", "a"}], ")"}]}], "\[IndentingNewLine]", 
              ")"}], ",", "\[IndentingNewLine]", 
             RowBox[{
              RowBox[{"(", 
               RowBox[{
                RowBox[{"-", "b"}], "+", 
                RowBox[{"Sqrt", "[", 
                 RowBox[{
                  RowBox[{"b", "^", "2"}], "+", 
                  RowBox[{"4", "*", "a", "*", 
                   RowBox[{"(", 
                    RowBox[{"(", 
                    RowBox[{
                    RowBox[{"Val", "/", "DriftEV"}], "-", "BlankEV"}], ")"}], 
                    ")"}]}]}], "]"}]}], ")"}], "/", 
              RowBox[{"(", 
               RowBox[{"2", "*", "a"}], ")"}]}]}], "\[IndentingNewLine]", 
            "]"}], ",", "\[IndentingNewLine]", 
           RowBox[{"If", "[", 
            RowBox[{
             RowBox[{"TrueQ", "[", 
              RowBox[{"OptionValue", "[", "IncludeConstantBasis", "]"}], 
              "]"}], ",", "\[IndentingNewLine]", 
             RowBox[{
              RowBox[{"(", 
               RowBox[{
                RowBox[{"-", "c"}], "+", 
                RowBox[{"(", 
                 RowBox[{"(", 
                  RowBox[{
                   RowBox[{"Val", "/", "DriftEV"}], "-", "BlankEV"}], ")"}], 
                 ")"}]}], ")"}], "/", "b"}], ",", "\[IndentingNewLine]", 
             RowBox[{
              RowBox[{"(", 
               RowBox[{"(", 
                RowBox[{
                 RowBox[{"Val", "/", "DriftEV"}], "-", "BlankEV"}], ")"}], 
               ")"}], "/", "b"}]}], "\[IndentingNewLine]", "]"}]}], 
          "\[IndentingNewLine]", "]"}], "/.", "Params"}]}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"Output", "=", 
        RowBox[{"DVEvaluate", "[", "Output", "]"}]}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"MissingQ", "[", "datum", "]"}], ",", 
         RowBox[{"Missing", "[", 
          RowBox[{
           RowBox[{"datum", "[", 
            RowBox[{"[", "1", "]"}], "]"}], ",", "Output"}], "]"}], ",", 
         "Output"}], "]"}]}], "\[IndentingNewLine]", ")"}]}], 
    "\[IndentingNewLine]", "]"}]}], ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"15e58c39-01d1-465c-b604-7c3f5d07d9c5"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Sample-Standard Bracketing calibration", "Section"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"FilterOutliers", "::", "usage"}], "=", 
   "\"\<FilterOutliers[data,{\!\(\*SubscriptBox[\(column\), \
\(1\)]\),\!\(\*SubscriptBox[\(column\), \(2\)]\),...},i] filters outliers in \
the specified columns (or keys) in data (DataTable or Association) based on a \
difference between the values and their mean which is larger than i standard \
deviations.\nFilterOutliers[data,{\!\(\*SubscriptBox[\(column\), \
\(1\)]\),\!\(\*SubscriptBox[\(column\), \(2\)]\),...}] filters outliers based \
on differences larger than 2 standard devations.\nFilterOutliers[data] \
filters outliers in all columns based on differences larger than 2 standard \
devations.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"FilterOutliers", "[", 
     RowBox[{"data_DataTable", ",", 
      RowBox[{"columns_:", "All"}], ",", 
      RowBox[{"SDMultiple_:", "2"}]}], "]"}], ":=", 
    RowBox[{"Module", "[", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"{", 
       RowBox[{"EVs", ",", "Cols"}], "}"}], ",", "\[IndentingNewLine]", 
      RowBox[{"(", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{"Cols", "=", 
         RowBox[{"If", "[", 
          RowBox[{
           RowBox[{"columns", "===", "All"}], ",", 
           RowBox[{"ColNames", "@", "data"}], ",", 
           RowBox[{"If", "[", 
            RowBox[{
             RowBox[{"ListQ", "[", "columns", "]"}], ",", "columns", ",", 
             RowBox[{"List", "[", "columns", "]"}]}], "]"}]}], "]"}]}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"EVs", "=", 
         RowBox[{
          RowBox[{"data", "[", 
           RowBox[{"All", ",", "Cols"}], "]"}], "[", 
          "MeanAndStandardDeviation", "]"}]}], ";", "\[IndentingNewLine]", 
        "\[IndentingNewLine]", 
        RowBox[{"data", "[", 
         RowBox[{"Select", "[", 
          RowBox[{
           RowBox[{"And", "@@", 
            RowBox[{"(", 
             RowBox[{"Module", "[", 
              RowBox[{
               RowBox[{"{", 
                RowBox[{"RD", "=", "#"}], "}"}], ",", 
               RowBox[{"(", "\[IndentingNewLine]", 
                RowBox[{
                 RowBox[{
                  RowBox[{"(", 
                   RowBox[{
                    RowBox[{"Abs", "[", 
                    RowBox[{
                    RowBox[{"RD", "[", "#", "]"}], "-", 
                    RowBox[{
                    RowBox[{"EVs", "[", "#", "]"}], "[", 
                    RowBox[{"[", "1", "]"}], "]"}]}], "]"}], "\[LessEqual]", 
                    RowBox[{"SDMultiple", "*", 
                    RowBox[{
                    RowBox[{"EVs", "[", "#", "]"}], "[", 
                    RowBox[{"[", "2", "]"}], "]"}]}]}], ")"}], "&"}], "/@", 
                 "Cols"}], "\[IndentingNewLine]", ")"}]}], "]"}], ")"}]}], 
           "&"}], "]"}], "]"}]}], "\[IndentingNewLine]", ")"}]}], 
     "\[IndentingNewLine]", "]"}]}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"FilterOutliers", "[", 
    RowBox[{"data_Association", ",", 
     RowBox[{"columns_:", "All"}], ",", 
     RowBox[{"SDMultiple_:", "2"}]}], "]"}], ":=", 
   RowBox[{"Normal", "@", 
    RowBox[{"FilterOutliers", "[", 
     RowBox[{
      RowBox[{"DataTable", "[", "data", "]"}], ",", "columns"}], "]"}]}]}], 
  ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"d8a44262-6fe2-487d-8695-a37942eec035"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"SSBSubstractBlanks", "::", "usage"}], "=", 
   "\"\<SSBSubstractBlanks[datatable] substracts the blank measurements from \
the standard and sample measurements in data which was obtained using the \
standard-sample-bracketing technique.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"SSBSubstractBlanks", "[", "data_DataTable", "]"}], ":=", 
   RowBox[{
    RowBox[{"data", "[", 
     RowBox[{
      RowBox[{"Partition", "[", 
       RowBox[{
        RowBox[{"Normal", "@", "#"}], ",", "3", ",", "2"}], "]"}], "&"}], 
     "]"}], "[", 
    RowBox[{
     RowBox[{
      RowBox[{"Association", "@", "#"}], "&"}], ",", 
     RowBox[{
      RowBox[{"Module", "[", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{"{", "Blanc", "}"}], ",", "\[IndentingNewLine]", 
        RowBox[{"(", "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"Blanc", "=", 
           RowBox[{"Merge", "[", 
            RowBox[{
             RowBox[{"{", 
              RowBox[{
               RowBox[{"Merge", "[", 
                RowBox[{
                 RowBox[{"Values", "@", 
                  RowBox[{"Values", " ", "@", 
                   RowBox[{"#", "[", 
                    RowBox[{"[", "1", "]"}], "]"}]}]}], ",", "Mean"}], "]"}], 
               ",", 
               RowBox[{"Merge", "[", 
                RowBox[{
                 RowBox[{"Values", "@", 
                  RowBox[{"Values", " ", "@", 
                   RowBox[{"#", "[", 
                    RowBox[{"[", "3", "]"}], "]"}]}]}], ",", "Mean"}], 
                "]"}]}], "}"}], ",", "Mean"}], "]"}]}], ";", 
          "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"Keys", "[", 
            RowBox[{"#", "[", 
             RowBox[{"[", "2", "]"}], "]"}], "]"}], "\[Rule]", 
           RowBox[{"(", 
            RowBox[{
             RowBox[{
              RowBox[{"#", "-", 
               RowBox[{"(", "Blanc", ")"}]}], "&"}], "/@", 
             RowBox[{"Values", "@", 
              RowBox[{"#", "[", 
               RowBox[{"[", "2", "]"}], "]"}]}]}], ")"}]}]}], 
         "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}], 
      "&"}]}], "]"}]}], ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"1fcb4427-e2c2-4191-b9bd-951aecfb123e"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"RoughnessFun", "[", 
    RowBox[{"deg_", ",", "knots_"}], "]"}], ":=", 
   RowBox[{
    RowBox[{"RoughnessFun", "[", 
     RowBox[{"deg", ",", "knots"}], "]"}], "=", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{"Integrate", "[", 
      RowBox[{
       RowBox[{
        RowBox[{"D", "[", 
         RowBox[{
          RowBox[{"Total", "@", 
           RowBox[{"MapIndexed", "[", 
            RowBox[{
             RowBox[{
              RowBox[{
               RowBox[{"ParamVar", "[", 
                RowBox[{"#2", "[", 
                 RowBox[{"[", "1", "]"}], "]"}], "]"}], "*", "#1"}], "&"}], 
             ",", 
             RowBox[{
              RowBox[{"Array", "[", 
               RowBox[{
                RowBox[{
                 RowBox[{"XVar", "^", "#"}], "&"}], ",", 
                RowBox[{"deg", "+", "1"}], ",", "0"}], "]"}], "~", "Join", 
              "~", 
              RowBox[{"Table", "[", 
               RowBox[{
                RowBox[{"BSplineBasis", "[", 
                 RowBox[{
                  RowBox[{"{", 
                   RowBox[{"deg", ",", "knots"}], "}"}], ",", "i", ",", 
                  "XVar"}], "]"}], ",", 
                RowBox[{"{", 
                 RowBox[{"i", ",", "0", ",", 
                  RowBox[{
                   RowBox[{"Length", "[", "knots", "]"}], "-", "deg", "-", 
                   "2"}]}], "}"}]}], "]"}]}]}], "]"}]}], ",", 
          RowBox[{"{", 
           RowBox[{"XVar", ",", "2"}], "}"}]}], "]"}], "^", "2"}], ",", 
       RowBox[{"{", 
        RowBox[{"XVar", ",", 
         RowBox[{"Min", "[", "knots", "]"}], ",", 
         RowBox[{"Max", "[", "knots", "]"}]}], "}"}]}], "]"}], 
     "\[IndentingNewLine]", ")"}]}]}], ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"76e16fda-5648-4364-a51e-75be3ca53747"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"RoughnessFun", "[", 
    RowBox[{"deg_", ",", "knots_", ",", 
     RowBox[{"params", ":", 
      RowBox[{"{", 
       RowBox[{
        RowBox[{"_", "?", "NumericQ"}], "..."}], "}"}]}]}], "]"}], ":=", 
   RowBox[{"(", "\[IndentingNewLine]", 
    RowBox[{"NIntegrate", "[", 
     RowBox[{
      RowBox[{
       RowBox[{"D", "[", 
        RowBox[{
         RowBox[{"Total", "@", 
          RowBox[{"MapIndexed", "[", 
           RowBox[{
            RowBox[{
             RowBox[{
              RowBox[{"params", "[", 
               RowBox[{"[", 
                RowBox[{"#2", "[", 
                 RowBox[{"[", "1", "]"}], "]"}], "]"}], "]"}], "*", "#1"}], 
             "&"}], ",", 
            RowBox[{
             RowBox[{"Array", "[", 
              RowBox[{
               RowBox[{
                RowBox[{"XVar", "^", "#"}], "&"}], ",", 
               RowBox[{"deg", "+", "1"}], ",", "0"}], "]"}], "~", "Join", "~", 
             RowBox[{"Table", "[", 
              RowBox[{
               RowBox[{"BSplineBasis", "[", 
                RowBox[{
                 RowBox[{"{", 
                  RowBox[{"deg", ",", "knots"}], "}"}], ",", "i", ",", 
                 "XVar"}], "]"}], ",", 
               RowBox[{"{", 
                RowBox[{"i", ",", "0", ",", 
                 RowBox[{
                  RowBox[{"Length", "[", "knots", "]"}], "-", "deg", "-", 
                  "2"}]}], "}"}]}], "]"}]}]}], "]"}]}], ",", 
         RowBox[{"{", 
          RowBox[{"XVar", ",", "2"}], "}"}]}], "]"}], "^", "2"}], ",", 
      RowBox[{"{", 
       RowBox[{"XVar", ",", 
        RowBox[{"Min", "[", "knots", "]"}], ",", 
        RowBox[{"Max", "[", "knots", "]"}]}], "}"}]}], "]"}], 
    "\[IndentingNewLine]", ")"}]}], ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"dc130a7a-0484-4928-a683-4d1c6a5e9281"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"CubicSplSmooth", "[", 
    RowBox[{"data_", ",", "lambda_"}], "]"}], ":=", 
   RowBox[{"Module", "[", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{"M", ",", "Knots", ",", "X", ",", "Dsq", ",", "a"}], "}"}], ",",
      "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"M", "=", 
       RowBox[{"Length", "@", "data"}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"Knots", "=", 
       RowBox[{"Flatten", "@", 
        RowBox[{"{", 
         RowBox[{"1", ",", "1", ",", "1", ",", 
          RowBox[{"Range", "@", "M"}], ",", "M", ",", "M", ",", "M"}], 
         "}"}]}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"X", "=", 
       RowBox[{"Table", "[", 
        RowBox[{
         RowBox[{"Evaluate", "@", 
          RowBox[{"N", "@", 
           RowBox[{"BSplineBasis", "[", 
            RowBox[{
             RowBox[{"{", 
              RowBox[{"3", ",", "Knots"}], "}"}], ",", "n", ",", "t"}], 
            "]"}]}]}], ",", 
         RowBox[{"{", 
          RowBox[{"t", ",", "1", ",", "M"}], "}"}], ",", 
         RowBox[{"{", 
          RowBox[{"n", ",", "0", ",", 
           RowBox[{"M", "+", "1"}]}], "}"}]}], "]"}]}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"Dsq", "=", 
       RowBox[{"Differences", "[", 
        RowBox[{"X", ",", "2"}], "]"}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"a", "=", 
       RowBox[{"LinearSolve", "[", 
        RowBox[{
         RowBox[{
          RowBox[{
           RowBox[{"Transpose", "[", "X", "]"}], ".", "X"}], "+", 
          RowBox[{"lambda", "*", 
           RowBox[{
            RowBox[{"Transpose", "[", "Dsq", "]"}], ".", "Dsq"}]}]}], ",", 
         RowBox[{
          RowBox[{"Transpose", "[", "X", "]"}], ".", "data"}], ",", 
         RowBox[{"Method", "\[Rule]", "\"\<Multifrontal\>\""}]}], "]"}]}], 
      ";", "\[IndentingNewLine]", 
      RowBox[{"Return", "[", 
       RowBox[{"X", ".", "a"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}], 
  ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"c8671769-2ab8-40e8-b615-7a9fdd82f2c5"],

Cell[BoxData[
 RowBox[{
  RowBox[{"SmoothSplineFit", "[", 
   RowBox[{"data_", ",", "errors_", ",", "deg_", ",", "lambda_"}], "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{
     "Basis", ",", "BasisFun", ",", "SquaredErrors", ",", "ParamList", ",", 
      "FitRes"}], "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"Basis", "=", 
       RowBox[{
        RowBox[{"Array", "[", 
         RowBox[{
          RowBox[{
           RowBox[{"XVar", "^", "#"}], "&"}], ",", 
          RowBox[{"deg", "+", "1"}], ",", "0"}], "]"}], "~", "Join", "~", 
        RowBox[{"Table", "[", 
         RowBox[{
          RowBox[{"BSplineBasis", "[", 
           RowBox[{
            RowBox[{"{", 
             RowBox[{"deg", ",", 
              RowBox[{"data", "[", 
               RowBox[{"[", 
                RowBox[{"All", ",", "1"}], "]"}], "]"}]}], "}"}], ",", "i", 
            ",", "XVar"}], "]"}], ",", 
          RowBox[{"{", 
           RowBox[{"i", ",", "0", ",", 
            RowBox[{
             RowBox[{"Length", "[", "data", "]"}], "-", "deg", "-", "2"}]}], 
           "}"}]}], "]"}]}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"BasisFun", "=", 
       RowBox[{"Total", "@", 
        RowBox[{"MapIndexed", "[", 
         RowBox[{
          RowBox[{
           RowBox[{
            RowBox[{"ParamVar", "[", 
             RowBox[{"#2", "[", 
              RowBox[{"[", "1", "]"}], "]"}], "]"}], "*", "#1"}], "&"}], ",", 
          "Basis"}], "]"}]}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"ParamList", "=", 
       RowBox[{"Array", "[", 
        RowBox[{"ParamVar", ",", 
         RowBox[{"Length", "[", "Basis", "]"}]}], "]"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"(*", 
       RowBox[{
        RowBox[{"SquaredErrors", "=", 
         RowBox[{"Simplify", "[", 
          RowBox[{"Total", "[", 
           RowBox[{
            RowBox[{
             RowBox[{
              RowBox[{"(", 
               RowBox[{"1", "/", 
                RowBox[{
                 RowBox[{"#", "[", 
                  RowBox[{"[", "2", "]"}], "]"}], "^", "2"}]}], ")"}], "*", 
              RowBox[{
               RowBox[{"(", 
                RowBox[{
                 RowBox[{"#", "[", 
                  RowBox[{"[", 
                   RowBox[{"1", ",", "2"}], "]"}], "]"}], "-", 
                 RowBox[{"(", 
                  RowBox[{"BasisFun", "/.", 
                   RowBox[{"XVar", "\[Rule]", 
                    RowBox[{"#", "[", 
                    RowBox[{"[", 
                    RowBox[{"1", ",", "1"}], "]"}], "]"}]}]}], ")"}]}], ")"}],
                "^", "2"}]}], "&"}], "/@", 
            RowBox[{"MapThread", "[", 
             RowBox[{
              RowBox[{
               RowBox[{"{", 
                RowBox[{"#1", ",", "#2"}], "}"}], "&"}], ",", 
              RowBox[{"{", 
               RowBox[{"data", ",", "errors"}], "}"}]}], "]"}]}], "]"}], 
          "]"}]}], ";", "\[IndentingNewLine]", 
        RowBox[{"ParamList", "=", 
         RowBox[{"Array", "[", 
          RowBox[{"ParamVar", ",", 
           RowBox[{"Length", "[", "Basis", "]"}]}], "]"}]}], ";", 
        "\[IndentingNewLine]", "\[IndentingNewLine]", 
        RowBox[{"FitRes", "=", 
         RowBox[{"NMinimize", "[", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{
            RowBox[{
             RowBox[{"(", "p", ")"}], "*", "SquaredErrors"}], "+", 
            RowBox[{
             RowBox[{"(", 
              RowBox[{"1", "-", "p"}], ")"}], "*", 
             RowBox[{"RoughnessFun", "[", 
              RowBox[{"deg", ",", 
               RowBox[{"data", "[", 
                RowBox[{"[", 
                 RowBox[{"All", ",", "1"}], "]"}], "]"}]}], "]"}]}]}], ",", 
           "\[IndentingNewLine]", "ParamList", ",", "\[IndentingNewLine]", 
           RowBox[{"PrecisionGoal", "\[Rule]", "1"}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{"AccuracyGoal", "\[Rule]", "1"}]}], "\[IndentingNewLine]", 
          "]"}]}], ";"}], "*)"}], "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"FitRes", "=", 
       RowBox[{"LinearModelFit", "[", 
        RowBox[{
         RowBox[{"CubicSplSmooth", "[", 
          RowBox[{"data", ",", "lambda"}], "]"}], ",", "Basis", ",", "XVar"}],
         "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"BasisFun", "/.", 
       RowBox[{"MapThread", "[", 
        RowBox[{
         RowBox[{
          RowBox[{"#1", "\[Rule]", "#2"}], "&"}], ",", 
         RowBox[{"{", 
          RowBox[{"ParamList", ",", 
           RowBox[{"FitRes", "[", "\"\<BestFitParameters\>\"", "]"}]}], 
          "}"}]}], "]"}]}]}], "\[IndentingNewLine]", ")"}]}], 
   "\[IndentingNewLine]", "]"}]}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"9b46345c-1dca-4167-b375-1983ff91cc7e"],

Cell[BoxData[
 RowBox[{
  RowBox[{"SmoothSplines", "[", 
   RowBox[{"data_", ",", "errors_", ",", "deg_", ",", 
    RowBox[{"knots", ":", 
     RowBox[{"{", 
      RowBox[{
       RowBox[{"_", "?", "NumericQ"}], ".."}], "}"}]}], ",", "rescaling_"}], 
   "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{
     "Basis", ",", "ExactFit", ",", "ParamList", ",", "BasisFun", ",", 
      "SquaredErrors", ",", "StartParameters", ",", "SmoothFit", ",", 
      "SquaredErrorFun", ",", "JacknifeScatter", ",", "ODFit"}], "}"}], ",", 
    "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{"(*", 
      RowBox[{
       RowBox[{"Basis", "=", 
        RowBox[{
         RowBox[{"Array", "[", 
          RowBox[{
           RowBox[{
            RowBox[{"XVar", "^", "#"}], "&"}], ",", 
           RowBox[{"deg", "+", "1"}], ",", "0"}], "]"}], "~", "Join", "~", 
         RowBox[{"Table", "[", 
          RowBox[{
           RowBox[{"BSplineBasis", "[", 
            RowBox[{
             RowBox[{"{", 
              RowBox[{"deg", ",", "knots"}], "}"}], ",", "i", ",", "XVar"}], 
            "]"}], ",", 
           RowBox[{"{", 
            RowBox[{"i", ",", "0", ",", 
             RowBox[{
              RowBox[{"Length", "[", "knots", "]"}], "-", "deg", "-", "2"}]}],
             "}"}]}], "]"}]}]}], ";", "\[IndentingNewLine]", 
       RowBox[{"ExactFit", "=", 
        RowBox[{"LinearModelFit", "[", 
         RowBox[{"data", ",", "Basis", ",", "XVar"}], "]"}]}], ";", 
       "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"ParamList", "=", 
        RowBox[{"Array", "[", 
         RowBox[{"ParamVar", ",", 
          RowBox[{"Length", "[", "Basis", "]"}]}], "]"}]}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"BasisFun", "=", 
        RowBox[{"Total", "@", 
         RowBox[{"MapIndexed", "[", 
          RowBox[{
           RowBox[{
            RowBox[{
             RowBox[{"ParamVar", "[", 
              RowBox[{"#2", "[", 
               RowBox[{"[", "1", "]"}], "]"}], "]"}], "*", "#1"}], "&"}], ",",
            "Basis"}], "]"}]}]}], ";"}], "*)"}], "\[IndentingNewLine]", 
     "\[IndentingNewLine]", 
     RowBox[{"(*", 
      RowBox[{
       RowBox[{"SquaredErrors", "=", 
        RowBox[{"Simplify", "[", 
         RowBox[{"Total", "[", 
          RowBox[{
           RowBox[{
            RowBox[{
             RowBox[{"(", 
              RowBox[{"1", "/", 
               RowBox[{
                RowBox[{"#", "[", 
                 RowBox[{"[", "2", "]"}], "]"}], "^", "2"}]}], ")"}], "*", 
             RowBox[{
              RowBox[{"(", 
               RowBox[{
                RowBox[{"#", "[", 
                 RowBox[{"[", 
                  RowBox[{"1", ",", "2"}], "]"}], "]"}], "-", 
                RowBox[{"(", 
                 RowBox[{"BasisFun", "/.", 
                  RowBox[{"XVar", "\[Rule]", 
                   RowBox[{"#", "[", 
                    RowBox[{"[", 
                    RowBox[{"1", ",", "1"}], "]"}], "]"}]}]}], ")"}]}], ")"}],
               "^", "2"}]}], "&"}], "/@", 
           RowBox[{"MapThread", "[", 
            RowBox[{
             RowBox[{
              RowBox[{"{", 
               RowBox[{"#1", ",", "#2"}], "}"}], "&"}], ",", 
             RowBox[{"{", 
              RowBox[{"data", ",", "errors"}], "}"}]}], "]"}]}], "]"}], 
         "]"}]}], ";"}], "\[IndentingNewLine]", "*)"}], "\[IndentingNewLine]", 
     RowBox[{"(*", 
      RowBox[{
       RowBox[{"StartParameters", "=", 
        RowBox[{"MapThread", "[", 
         RowBox[{
          RowBox[{
           RowBox[{"{", 
            RowBox[{"#1", ",", "#2"}], "}"}], "&"}], ",", 
          RowBox[{"{", 
           RowBox[{"ParamList", ",", 
            RowBox[{"ExactFit", "[", "\"\<BestFitParameters\>\"", "]"}]}], 
           "}"}]}], "]"}]}], ";"}], "*)"}], "\[IndentingNewLine]", 
     "\[IndentingNewLine]", 
     RowBox[{"(*", 
      RowBox[{
       RowBox[{
        RowBox[{"SquaredErrorFun", "[", 
         RowBox[{"p_", "?", "NumericQ"}], "]"}], ":=", 
        RowBox[{
         RowBox[{"SquaredErrorFun", "[", "p", "]"}], "=", 
         RowBox[{"Module", "[", 
          RowBox[{
           RowBox[{"{", "FitRes", "}"}], ",", "\[IndentingNewLine]", 
           RowBox[{"(", "\[IndentingNewLine]", 
            RowBox[{
             RowBox[{"FitRes", "=", 
              RowBox[{"NMinimize", "[", "\[IndentingNewLine]", 
               RowBox[{
                RowBox[{
                 RowBox[{
                  RowBox[{"(", "p", ")"}], "*", "SquaredErrors"}], "+", 
                 RowBox[{
                  RowBox[{"(", 
                   RowBox[{"1", "-", "p"}], ")"}], "*", 
                  RowBox[{"RoughnessFun", "[", 
                   RowBox[{"deg", ",", "knots"}], "]"}]}]}], ",", 
                "\[IndentingNewLine]", "ParamList", ",", 
                "\[IndentingNewLine]", 
                RowBox[{"PrecisionGoal", "\[Rule]", "2"}], ",", 
                "\[IndentingNewLine]", 
                RowBox[{"AccuracyGoal", "\[Rule]", "2"}]}], 
               "\[IndentingNewLine]", "]"}]}], ";", "\[IndentingNewLine]", 
             "\[IndentingNewLine]", 
             RowBox[{"(", 
              RowBox[{"SquaredErrors", "/.", 
               RowBox[{"FitRes", "[", 
                RowBox[{"[", "2", "]"}], "]"}]}], ")"}]}], 
            "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}]}]}], 
       ";"}], "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{
       RowBox[{"JacknifeScatter", "[", 
        RowBox[{"lambda_", "?", "NumericQ"}], "]"}], ":=", 
       RowBox[{
        RowBox[{"JacknifeScatter", "[", "lambda", "]"}], "=", 
        RowBox[{"Total", "[", "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{
           RowBox[{"(", "\[IndentingNewLine]", 
            RowBox[{
             RowBox[{"(", 
              RowBox[{
               RowBox[{"(", 
                RowBox[{
                 RowBox[{"SmoothSplineFit", "[", 
                  RowBox[{
                   RowBox[{"Delete", "[", 
                    RowBox[{"data", ",", "#"}], "]"}], ",", 
                   RowBox[{"Delete", "[", 
                    RowBox[{"errors", ",", "#"}], "]"}], ",", "deg", ",", 
                   "lambda"}], "]"}], "/.", 
                 RowBox[{"XVar", "\[Rule]", 
                  RowBox[{"data", "[", 
                   RowBox[{"[", 
                    RowBox[{"#", ",", "1"}], "]"}], "]"}]}]}], ")"}], "-", 
               RowBox[{"data", "[", 
                RowBox[{"[", 
                 RowBox[{"#", ",", "2"}], "]"}], "]"}]}], ")"}], "^", "2"}], 
            "\[IndentingNewLine]", ")"}], "&"}], "/@", 
          RowBox[{"Range", "[", 
           RowBox[{"1", ",", 
            RowBox[{"Length", "[", "data", "]"}]}], "]"}]}], 
         "\[IndentingNewLine]", "]"}]}]}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"SmoothFit", "=", 
       RowBox[{"NMinimize", "[", "\[IndentingNewLine]", 
        RowBox[{
         RowBox[{"{", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"JacknifeScatter", "[", "lambda", "]"}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{"lambda", ">", "0"}]}], "\[IndentingNewLine]", "}"}], ",", 
         "\[IndentingNewLine]", 
         RowBox[{"{", "lambda", "}"}], ",", "\[IndentingNewLine]", 
         RowBox[{"Method", "\[Rule]", 
          RowBox[{"{", 
           RowBox[{"\"\<NelderMead\>\"", ",", 
            RowBox[{"\"\<PostProcess\>\"", "\[Rule]", "False"}]}], "}"}]}], 
         ",", "\[IndentingNewLine]", 
         RowBox[{"AccuracyGoal", "\[Rule]", "3"}], ",", "\[IndentingNewLine]", 
         RowBox[{"PrecisionGoal", "\[Rule]", "3"}]}], 
        RowBox[{"(*", 
         RowBox[{",", "\[IndentingNewLine]", 
          RowBox[{"EvaluationMonitor", "\[RuleDelayed]", 
           RowBox[{"Print", "[", 
            RowBox[{"{", 
             RowBox[{"lambda", ",", 
              RowBox[{"JacknifeScatter", "[", "lambda", "]"}]}], "}"}], 
            "]"}]}]}], "*)"}], "\[IndentingNewLine]", "]"}]}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"SmoothFit", "=", 
       RowBox[{"SmoothSplineFit", "[", 
        RowBox[{"data", ",", "errors", ",", "3", ",", 
         RowBox[{"lambda", "/.", 
          RowBox[{"SmoothFit", "[", 
           RowBox[{"[", "2", "]"}], "]"}]}]}], "]"}]}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"ODFit", "=", 
       RowBox[{"NMinimize", "[", "\[IndentingNewLine]", 
        RowBox[{
         RowBox[{"{", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{
            RowBox[{"(", 
             RowBox[{
              RowBox[{"Mean", "[", 
               RowBox[{
                RowBox[{
                 RowBox[{
                  RowBox[{"(", 
                   RowBox[{"1", "/", 
                    RowBox[{"(", 
                    RowBox[{
                    RowBox[{
                    RowBox[{"#", "[", 
                    RowBox[{"[", "2", "]"}], "]"}], "^", "2"}], "+", 
                    RowBox[{"od", "^", "2"}]}], ")"}]}], ")"}], "*", 
                  RowBox[{
                   RowBox[{"(", 
                    RowBox[{
                    RowBox[{"#", "[", 
                    RowBox[{"[", 
                    RowBox[{"1", ",", "2"}], "]"}], "]"}], "-", 
                    RowBox[{"(", 
                    RowBox[{"SmoothFit", "/.", 
                    RowBox[{"XVar", "\[Rule]", 
                    RowBox[{"#", "[", 
                    RowBox[{"[", 
                    RowBox[{"1", ",", "1"}], "]"}], "]"}]}]}], ")"}]}], ")"}],
                    "^", "2"}]}], "&"}], "/@", 
                RowBox[{"MapThread", "[", 
                 RowBox[{
                  RowBox[{
                   RowBox[{"{", 
                    RowBox[{"#1", ",", "#2"}], "}"}], "&"}], ",", 
                  RowBox[{"{", 
                   RowBox[{"data", ",", "errors"}], "}"}]}], "]"}]}], "]"}], 
              "-", "1"}], ")"}], "^", "2"}], ",", "\[IndentingNewLine]", 
           RowBox[{
            RowBox[{"od", "\[GreaterEqual]", "0"}], "&&", 
            RowBox[{"od", "\[LessEqual]", 
             RowBox[{"10", "*", 
              RowBox[{"Mean", "[", "errors", "]"}]}]}]}]}], 
          "\[IndentingNewLine]", "}"}], ",", "\[IndentingNewLine]", 
         RowBox[{"{", "od", "}"}], ",", "\[IndentingNewLine]", 
         RowBox[{"Method", "\[Rule]", 
          RowBox[{"{", 
           RowBox[{"\"\<NelderMead\>\"", ",", 
            RowBox[{"\"\<PostProcess\>\"", "\[Rule]", "False"}]}], "}"}]}], 
         ",", "\[IndentingNewLine]", 
         RowBox[{"AccuracyGoal", "\[Rule]", "2"}], ",", "\[IndentingNewLine]", 
         RowBox[{"PrecisionGoal", "\[Rule]", "2"}]}], 
        RowBox[{"(*", 
         RowBox[{",", "\[IndentingNewLine]", 
          RowBox[{"EvaluationMonitor", "\[RuleDelayed]", 
           RowBox[{"Print", "[", "od", "]"}]}]}], "*)"}], 
        "\[IndentingNewLine]", "]"}]}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"(*", 
       RowBox[{
        RowBox[{"SmoothFit", "=", 
         RowBox[{
          RowBox[{"NMinimize", "[", "\[IndentingNewLine]", 
           RowBox[{
            RowBox[{"{", "\[IndentingNewLine]", 
             RowBox[{
              RowBox[{
               RowBox[{"(", 
                RowBox[{
                 RowBox[{"SquaredErrorFun", "[", "p", "]"}], "-", 
                 RowBox[{"Length", "[", "data", "]"}]}], ")"}], "^", "2"}], 
              ",", "\[IndentingNewLine]", 
              RowBox[{
               RowBox[{"p", "\[GreaterEqual]", "0"}], "&&", 
               RowBox[{"p", "\[LessEqual]", "1"}]}]}], "\[IndentingNewLine]", 
             "}"}], ",", "\[IndentingNewLine]", 
            RowBox[{"{", 
             RowBox[{"p", ",", "0", ",", "1"}], 
             RowBox[{"(*", 
              RowBox[{"1", "/", 
               RowBox[{"Mean", "[", 
                RowBox[{
                 RowBox[{
                  RowBox[{"(", 
                   RowBox[{"1", "/", 
                    RowBox[{"#", "^", "2"}]}], ")"}], "&"}], "/@", "errors"}],
                 "]"}]}], "*)"}], "}"}], ",", "\[IndentingNewLine]", 
            RowBox[{"AccuracyGoal", "\[Rule]", "1"}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{"PrecisionGoal", "\[Rule]", 
             RowBox[{"N", "[", 
              RowBox[{"-", 
               RowBox[{"Log10", "[", 
                RowBox[{
                 RowBox[{"Sqrt", "[", "2", "]"}], "/", 
                 RowBox[{"Sqrt", "[", 
                  RowBox[{"Length", "[", "data", "]"}], "]"}]}], "]"}]}], 
              "]"}]}], ",", "\[IndentingNewLine]", 
            RowBox[{"EvaluationMonitor", "\[RuleDelayed]", 
             RowBox[{"Print", "[", 
              RowBox[{"FullForm", "[", "p", "]"}], "]"}]}], ",", 
            "\[IndentingNewLine]", 
            RowBox[{"Method", "\[Rule]", 
             RowBox[{"{", 
              RowBox[{"\"\<NelderMead\>\"", ",", 
               RowBox[{"\"\<PostProcess\>\"", "\[Rule]", "False"}]}], 
              "}"}]}]}], "\[IndentingNewLine]", "]"}], "[", 
          RowBox[{"[", "2", "]"}], "]"}]}], ";"}], "*)"}], 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"(*", 
       RowBox[{
        RowBox[{"SmoothFit", "=", 
         RowBox[{"FindRoot", "[", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{
            RowBox[{"SquaredErrorFun", "[", "p", "]"}], "\[Equal]", 
            RowBox[{"Length", "[", "data", "]"}]}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{"{", 
            RowBox[{"p", ",", "0", ",", "0", ",", "1"}], "}"}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{"AccuracyGoal", "\[Rule]", "1"}], ",", 
           "\[IndentingNewLine]", 
           RowBox[{"PrecisionGoal", "\[Rule]", 
            RowBox[{"-", 
             RowBox[{"Log10", "[", 
              RowBox[{
               RowBox[{"Sqrt", "[", "2", "]"}], "/", 
               RowBox[{"Sqrt", "[", 
                RowBox[{"Length", "[", "data", "]"}], "]"}]}], "]"}]}]}]}], 
          RowBox[{"(*", 
           RowBox[{",", "\[IndentingNewLine]", 
            RowBox[{"EvaluationMonitor", "\[RuleDelayed]", 
             RowBox[{"Print", "[", 
              RowBox[{"FullForm", "[", "p", "]"}], "]"}]}]}], "*)"}], 
          "\[IndentingNewLine]", "]"}]}], ";"}], "*)"}], 
      "\[IndentingNewLine]", 
      RowBox[{"(*", 
       RowBox[{
        RowBox[{"SmoothFit", "=", 
         RowBox[{"{", 
          RowBox[{"p", "\[Rule]", "1"}], "}"}]}], ";"}], "*)"}], 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"(*", 
       RowBox[{
        RowBox[{
         RowBox[{
          RowBox[{"SquaredErrorFun", "[", "p", "]"}], "/.", "SmoothFit"}], "//",
          "Print"}], ";"}], "*)"}], "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"(*", 
       RowBox[{
        RowBox[{"SmoothFit", "=", 
         RowBox[{"NMinimize", "[", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{
            RowBox[{
             RowBox[{"(", 
              RowBox[{"p", "/.", "SmoothFit"}], ")"}], "*", "SquaredErrors"}],
             "+", 
            RowBox[{
             RowBox[{"(", 
              RowBox[{"1", "-", 
               RowBox[{"(", 
                RowBox[{"p", "/.", "SmoothFit"}], ")"}]}], ")"}], "*", 
             RowBox[{"RoughnessFun", "[", 
              RowBox[{"deg", ",", "knots"}], "]"}]}]}], ",", 
           "\[IndentingNewLine]", "ParamList"}], "\[IndentingNewLine]", 
          "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
        RowBox[{"SmoothFit", "=", 
         RowBox[{"BasisFun", "/.", 
          RowBox[{"SmoothFit", "[", 
           RowBox[{"[", "2", "]"}], "]"}]}]}], ";"}], "*)"}], 
      "\[IndentingNewLine]", 
      RowBox[{"{", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{
         RowBox[{"Evaluate", "[", 
          RowBox[{"SmoothFit", "/.", 
           RowBox[{"XVar", "\[Rule]", 
            RowBox[{
             RowBox[{"(", 
              RowBox[{"#", "-", 
               RowBox[{"rescaling", "[", 
                RowBox[{"[", "1", "]"}], "]"}]}], ")"}], "/", 
             RowBox[{"(", 
              RowBox[{
               RowBox[{"rescaling", "[", 
                RowBox[{"[", "2", "]"}], "]"}], "-", 
               RowBox[{"rescaling", "[", 
                RowBox[{"[", "1", "]"}], "]"}]}], ")"}]}]}]}], "]"}], "&"}], 
        ",", "\[IndentingNewLine]", 
        RowBox[{"od", "/.", 
         RowBox[{"ODFit", "[", 
          RowBox[{"[", "2", "]"}], "]"}]}]}], "\[IndentingNewLine]", "}"}]}], 
     "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}]}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"dae147cb-cf44-48e8-9d1a-1f530abf92e3"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"SSBCalculateCalibrationFit", "::", "usage"}], "=", 
   "\"\<SSBCalculateCalibrationFit[datatable,n] calculates the calibration \
fit of the standards in data which was obtained using the \
standard-sample-bracketing technique. The integer n specifies the number of \
iterations for the MonteCarloSimulation used to estimate \
uncertainties.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"SSBCalculateCalibrationFit", "[", 
   RowBox[{"data_DataTable", ",", "n_"}], "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"STDs", ",", "Splines", ",", "ScaleFactors"}], "}"}], ",", 
    "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"STDs", "=", 
       RowBox[{
        RowBox[{"data", "[", 
         RowBox[{"[", 
          RowBox[{"1", ";;", 
           RowBox[{"-", "1"}], ";;", "2"}], "]"}], "]"}], "[", 
        RowBox[{"All", ",", 
         RowBox[{
          RowBox[{"<|", 
           RowBox[{"#", ",", 
            RowBox[{"\"\<Time\>\"", "\[Rule]", 
             RowBox[{"AbsoluteTime", "[", 
              RowBox[{"#", "[", "\"\<Time\>\"", "]"}], "]"}]}]}], "|>"}], 
          "&"}]}], "]"}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"ScaleFactors", "=", 
       RowBox[{"{", 
        RowBox[{
         RowBox[{"Min", "[", 
          RowBox[{"STDs", "[", 
           RowBox[{"[", 
            RowBox[{"All", ",", "1"}], "]"}], "]"}], "]"}], ",", 
         RowBox[{"Max", "[", 
          RowBox[{"STDs", "[", 
           RowBox[{"[", 
            RowBox[{"All", ",", "1"}], "]"}], "]"}], "]"}]}], "}"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"QueryTo", "[", 
       RowBox[{"STDs", ",", "All", ",", 
        RowBox[{
         RowBox[{"<|", 
          RowBox[{"#", ",", 
           RowBox[{"\"\<Time\>\"", "\[Rule]", 
            RowBox[{
             RowBox[{"(", 
              RowBox[{
               RowBox[{"#", "[", "\"\<Time\>\"", "]"}], "-", 
               RowBox[{"ScaleFactors", "[", 
                RowBox[{"[", "1", "]"}], "]"}]}], ")"}], "/", 
             RowBox[{"(", 
              RowBox[{
               RowBox[{"ScaleFactors", "[", 
                RowBox[{"[", "2", "]"}], "]"}], "-", 
               RowBox[{"ScaleFactors", "[", 
                RowBox[{"[", "1", "]"}], "]"}]}], ")"}]}]}]}], "|>"}], 
         "&"}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"$DistributedContexts", "=", "Automatic"}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"<|", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{
         RowBox[{"#", "\[Rule]", 
          RowBox[{"Module", "[", 
           RowBox[{
            RowBox[{"{", 
             RowBox[{"ColDat", "=", 
              RowBox[{"NV", "@", 
               RowBox[{"STDs", "[", 
                RowBox[{"All", ",", 
                 RowBox[{"{", 
                  RowBox[{"\"\<Time\>\"", ",", "#"}], "}"}]}], "]"}]}]}], 
             "}"}], ",", "\[IndentingNewLine]", 
            RowBox[{"(", "\[IndentingNewLine]", 
             RowBox[{
              RowBox[{"Splines", "=", 
               RowBox[{"MonteCarloSimulation", "[", "\[IndentingNewLine]", 
                RowBox[{
                 RowBox[{
                  RowBox[{"Hold", "[", 
                   RowBox[{"SmoothSplines", "[", 
                    RowBox[{
                    "d", ",", "errs", ",", "3", ",", "knots", ",", "sf"}], 
                    "]"}], "]"}], "/.", 
                  RowBox[{"{", "\[IndentingNewLine]", 
                   RowBox[{
                    RowBox[{"errs", "\[RuleDelayed]", 
                    RowBox[{"With", "[", 
                    RowBox[{
                    RowBox[{"{", 
                    RowBox[{"eval", "=", 
                    RowBox[{
                    RowBox[{"(", 
                    RowBox[{"DVSEs", "@", "ColDat"}], ")"}], "[", 
                    RowBox[{"[", 
                    RowBox[{"All", ",", "2"}], "]"}], "]"}]}], "}"}], ",", 
                    RowBox[{"eval", "/;", "True"}]}], "]"}]}], ",", 
                    "\[IndentingNewLine]", 
                    RowBox[{"knots", "\[RuleDelayed]", 
                    RowBox[{"With", "[", 
                    RowBox[{
                    RowBox[{"{", 
                    RowBox[{"eval", "=", 
                    RowBox[{"ColDat", "[", 
                    RowBox[{"[", 
                    RowBox[{"All", ",", "1"}], "]"}], "]"}]}], "}"}], ",", 
                    RowBox[{"eval", "/;", "True"}]}], "]"}]}], ",", 
                    "\[IndentingNewLine]", 
                    RowBox[{"sf", "\[RuleDelayed]", 
                    RowBox[{"With", "[", 
                    RowBox[{
                    RowBox[{"{", 
                    RowBox[{"eval", "=", "ScaleFactors"}], "}"}], ",", 
                    RowBox[{"eval", "/;", "True"}]}], "]"}]}]}], 
                   "\[IndentingNewLine]", "}"}]}], ",", "\[IndentingNewLine]",
                  "n", ",", "\[IndentingNewLine]", 
                 RowBox[{"{", 
                  RowBox[{"d", "\[Rule]", "ColDat"}], "}"}], ",", 
                 "\[IndentingNewLine]", 
                 RowBox[{"UseParallelKernels", "\[Rule]", "True"}]}], 
                "\[IndentingNewLine]", "]"}]}], ";", "\[IndentingNewLine]", 
              RowBox[{"(*", 
               RowBox[{
                RowBox[{"Splines", "=", 
                 RowBox[{"SmoothSplines", "[", 
                  RowBox[{
                   RowBox[{"DVMeans", "@", "ColDat"}], ",", 
                   RowBox[{
                    RowBox[{"(", 
                    RowBox[{"DVSEs", "@", "ColDat"}], ")"}], "[", 
                    RowBox[{"[", 
                    RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", "3", ",", 
                   RowBox[{"ColDat", "[", 
                    RowBox[{"[", 
                    RowBox[{"All", ",", "1"}], "]"}], "]"}], ",", 
                   "ScaleFactors"}], "]"}]}], ";"}], "*)"}], 
              "\[IndentingNewLine]", "\[IndentingNewLine]", "Splines"}], 
             "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}]}], 
         "&"}], "/@", 
        RowBox[{"DeleteCases", "[", 
         RowBox[{
          RowBox[{"ColNames", "[", "STDs", "]"}], ",", "\"\<Time\>\""}], 
         "]"}]}], "\[IndentingNewLine]", "|>"}]}], "\[IndentingNewLine]", 
     ")"}]}], "\[IndentingNewLine]", "]"}]}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"125f0900-66a8-4a8e-8fee-6b07b8f9a190"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"SSBCalculateDeltas", "::", "usage"}], "=", 
   "\"\<SSBCalculateDeltas[datatable,standardFit] calculates the \[Delta] \
values for samples in data which was obtained using the \
standard-sample-bracketing technique, where the standardFit was previously \
calculated using SSBCalculateCalibrationFit.\>\""}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"SSBCalculateDeltas", "[", 
   RowBox[{"data_DataTable", ",", "standardFit_"}], "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", "Samples", "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"Samples", "=", 
       RowBox[{
        RowBox[{"data", "[", 
         RowBox[{"[", 
          RowBox[{"2", ";;", 
           RowBox[{"-", "2"}], ";;", "2"}], "]"}], "]"}], "[", 
        RowBox[{"All", ",", 
         RowBox[{
          RowBox[{"<|", 
           RowBox[{"#", ",", 
            RowBox[{"\"\<Time\>\"", "\[Rule]", 
             RowBox[{"AbsoluteTime", "[", 
              RowBox[{"#", "[", "\"\<Time\>\"", "]"}], "]"}]}]}], "|>"}], 
          "&"}]}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"Samples", "[", 
        RowBox[{"All", ",", 
         RowBox[{
          RowBox[{"Module", "[", 
           RowBox[{
            RowBox[{"{", 
             RowBox[{"RowV", "=", "#"}], "}"}], ",", 
            RowBox[{"(", "\[IndentingNewLine]", 
             RowBox[{"<|", 
              RowBox[{
               RowBox[{
                RowBox[{"(", "\[IndentingNewLine]", 
                 RowBox[{"#", "\[Rule]", 
                  RowBox[{
                   RowBox[{"(", 
                    RowBox[{
                    RowBox[{
                    RowBox[{"(", 
                    RowBox[{"NDV", "[", 
                    RowBox[{
                    RowBox[{"RowV", "[", 
                    RowBox[{"[", 
                    RowBox[{"#", ",", "1"}], "]"}], "]"}], ",", 
                    RowBox[{
                    RowBox[{"RowV", "[", 
                    RowBox[{"[", 
                    RowBox[{"#", ",", "2"}], "]"}], "]"}], "+", 
                    RowBox[{
                    RowBox[{
                    RowBox[{"standardFit", "[", 
                    RowBox[{"[", "#", "]"}], "]"}], "[", "\"\<SingleMean\>\"",
                     "]"}], "[", 
                    RowBox[{"[", "2", "]"}], "]"}]}]}], "]"}], ")"}], "/", 
                    RowBox[{"NDV", "[", 
                    RowBox[{
                    RowBox[{
                    RowBox[{
                    RowBox[{
                    RowBox[{"standardFit", "[", 
                    RowBox[{"[", "#", "]"}], "]"}], "[", "\"\<SingleMean\>\"",
                     "]"}], "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "[", 
                    RowBox[{"RowV", "[", "\"\<Time\>\"", "]"}], "]"}], ",", 
                    RowBox[{"StandardDeviation", "@", 
                    RowBox[{"With", "[", 
                    RowBox[{
                    RowBox[{"{", 
                    RowBox[{"x", "=", 
                    RowBox[{"RowV", "[", "\"\<Time\>\"", "]"}]}], "}"}], ",", 
                    RowBox[{
                    RowBox[{
                    RowBox[{
                    RowBox[{"#", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "[", "x", "]"}], "&"}], "/@", 
                    RowBox[{"(", 
                    RowBox[{
                    RowBox[{
                    RowBox[{"standardFit", "[", 
                    RowBox[{"[", "#", "]"}], "]"}], "[", 
                    "\"\<SimulationData\>\"", "]"}], "[", 
                    RowBox[{"[", 
                    RowBox[{"All", ",", "1"}], "]"}], "]"}], ")"}]}]}], 
                    "]"}]}]}], "]"}]}], "-", "1"}], ")"}], "*", 
                   RowBox[{"10", "^", "3"}]}]}], "\[IndentingNewLine]", ")"}],
                 "&"}], "/@", 
               RowBox[{"DeleteCases", "[", 
                RowBox[{
                 RowBox[{"ColNames", "[", "Samples", "]"}], ",", 
                 "\"\<Time\>\""}], "]"}]}], "\[IndentingNewLine]", "|>"}], 
             "\[IndentingNewLine]", ")"}]}], "]"}], "&"}]}], "]"}], "[", 
       RowBox[{"All", ",", 
        RowBox[{
         RowBox[{"KeyMap", "[", 
          RowBox[{
           RowBox[{
            RowBox[{
            "\"\<\[Delta]\>\"", "<>", "#", "<>", "\"\< / permil\>\""}], "&"}],
            ",", "#"}], "]"}], "&"}]}], "]"}]}], "\[IndentingNewLine]", 
     "\[IndentingNewLine]", 
     RowBox[{"(*", 
      RowBox[{"Samples", "[", 
       RowBox[{"All", ",", 
        RowBox[{
         RowBox[{
          RowBox[{"(", 
           RowBox[{
            RowBox[{
             RowBox[{"(", 
              RowBox[{"NDV", "[", 
               RowBox[{
                RowBox[{"#", "[", 
                 RowBox[{"[", 
                  RowBox[{"2", ",", "1"}], "]"}], "]"}], ",", 
                RowBox[{
                 RowBox[{"#", "[", 
                  RowBox[{"[", 
                   RowBox[{"2", ",", "2"}], "]"}], "]"}], "+", 
                 RowBox[{
                  RowBox[{"standardFit", "[", "\"\<SingleMean\>\"", "]"}], 
                  "[", 
                  RowBox[{"[", "2", "]"}], "]"}]}]}], "]"}], ")"}], "/", 
             RowBox[{"NDV", "[", 
              RowBox[{
               RowBox[{
                RowBox[{
                 RowBox[{"standardFit", "[", "\"\<SingleMean\>\"", "]"}], "[", 
                 RowBox[{"[", "1", "]"}], "]"}], "[", 
                RowBox[{"#", "[", 
                 RowBox[{"[", "1", "]"}], "]"}], "]"}], ",", 
               RowBox[{"StandardDeviation", "@", 
                RowBox[{"With", "[", 
                 RowBox[{
                  RowBox[{"{", 
                   RowBox[{"x", "=", 
                    RowBox[{"#", "[", 
                    RowBox[{"[", "1", "]"}], "]"}]}], "}"}], ",", 
                  RowBox[{
                   RowBox[{
                    RowBox[{
                    RowBox[{"#", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "[", "x", "]"}], "&"}], "/@", 
                   RowBox[{"(", 
                    RowBox[{
                    RowBox[{
                    "standardFit", "[", "\"\<SimulationData\>\"", "]"}], "[", 
                    RowBox[{"[", 
                    RowBox[{"All", ",", "1"}], "]"}], "]"}], ")"}]}]}], 
                 "]"}]}]}], "]"}]}], "-", "1"}], ")"}], "*", 
          RowBox[{"10", "^", "3"}]}], "&"}]}], "]"}], "*)"}], 
     "\[IndentingNewLine]", "\[IndentingNewLine]", 
     RowBox[{"(*", 
      RowBox[{"DTPlot", "[", 
       RowBox[{
        RowBox[{"STDs", "[", 
         RowBox[{"[", 
          RowBox[{"All", ",", 
           RowBox[{"{", 
            RowBox[{"1", ",", "3"}], "}"}]}], "]"}], "]"}], ",", 
        RowBox[{"PlotRange", "\[Rule]", "All"}]}], "]"}], "*)"}], 
     "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", "]"}]}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"e85ebe00-3451-461a-a03a-7049b2f2b58c"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"SSBCalculateDeltas", "::", "usage"}], "=", 
   "\"\<SSBCalculateDeltas[datatable] calculates the \[Delta] values for \
samples in data which was obtained using the standard-sample-bracketing \
technique.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"SSBCalculateDeltas", "[", "data_DataTable", "]"}], ":=", 
  RowBox[{
   RowBox[{
    RowBox[{"data", "[", 
     RowBox[{
      RowBox[{"Partition", "[", 
       RowBox[{
        RowBox[{"Normal", "@", "#"}], ",", "3", ",", "2"}], "]"}], "&"}], 
     "]"}], "[", 
    RowBox[{
     RowBox[{
      RowBox[{"Association", "@", "#"}], "&"}], ",", 
     RowBox[{
      RowBox[{"Module", "[", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{"{", "Standard", "}"}], ",", "\[IndentingNewLine]", 
        RowBox[{"(", "\[IndentingNewLine]", 
         RowBox[{"(*", 
          RowBox[{
           RowBox[{"Standard", "=", 
            RowBox[{"Merge", "[", 
             RowBox[{
              RowBox[{"{", 
               RowBox[{
                RowBox[{"Values", "@", 
                 RowBox[{"#", "[", 
                  RowBox[{"[", "1", "]"}], "]"}]}], ",", 
                RowBox[{"Values", "@", 
                 RowBox[{"#", "[", 
                  RowBox[{"[", "3", "]"}], "]"}]}]}], "}"}], ",", 
              RowBox[{
               RowBox[{"DVE", "[", 
                RowBox[{"Mean", "[", "#", "]"}], "]"}], "&"}]}], "]"}]}], 
           ";"}], "*)"}], "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"Standard", "=", 
           RowBox[{"Merge", "[", 
            RowBox[{
             RowBox[{"{", 
              RowBox[{
               RowBox[{"Values", "@", 
                RowBox[{"#", "[", 
                 RowBox[{"[", "1", "]"}], "]"}]}], ",", 
               RowBox[{"Values", "@", 
                RowBox[{"#", "[", 
                 RowBox[{"[", "3", "]"}], "]"}]}]}], "}"}], ",", 
             RowBox[{
              RowBox[{"NDV", "[", 
               RowBox[{
                RowBox[{"Mean", "[", 
                 RowBox[{"DVMeans", "@", "#"}], "]"}], ",", 
                RowBox[{
                 RowBox[{"StandardDeviation", "[", 
                  RowBox[{"DVMeans", "@", "#"}], "]"}], "/", 
                 RowBox[{"Sqrt", "[", "2", "]"}]}]}], "]"}], "&"}]}], "]"}]}],
           ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
          RowBox[{
           RowBox[{"Keys", "[", 
            RowBox[{"#", "[", 
             RowBox[{"[", "2", "]"}], "]"}], "]"}], "\[Rule]", 
           RowBox[{"(", 
            RowBox[{
             RowBox[{
              RowBox[{"(", 
               RowBox[{
                RowBox[{"Values", "@", 
                 RowBox[{"#", "[", 
                  RowBox[{"[", "2", "]"}], "]"}]}], "-", 
                TagBox["Standard",
                 Short[#, 5]& ]}], ")"}], "/", "Standard"}], "*", 
             RowBox[{"10", "^", "3"}]}], ")"}]}]}], "\[IndentingNewLine]", 
         ")"}]}], "\[IndentingNewLine]", "]"}], "&"}]}], "]"}], "[", 
   RowBox[{"All", ",", 
    RowBox[{
     RowBox[{"KeyMap", "[", 
      RowBox[{
       RowBox[{
        RowBox[{"\"\<\[Delta]\>\"", "<>", "#", "<>", "\"\< / permil\>\""}], 
        "&"}], ",", "#"}], "]"}], "&"}]}], "]"}]}]}], "Input",ExpressionUUID->\
"a340b9d8-86a9-4446-8c05-ba07c7019666"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"SSBCalculateMeanRatios", "::", "usage"}], "=", 
   "\"\<SSBCalculateMeanRatios[datatable,{\!\(\*SubscriptBox[\(column\), \
\(1\)]\)/\!\(\*SubscriptBox[\(column\), \(2\)]\),,\!\(\*SubscriptBox[\(column\
\), \(3\)]\)/\!\(\*SubscriptBox[\(column\), \(4\)]\),...}] calculates the \
mean isotope ratios for the standards and samples from the specified columns \
in data which was obtained using the standard-sample-bracketing technique, by \
first substracting blank measurements from the standards and samples and \
filtering outliers.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"SSBCalculateMeanRatios", "[", 
   RowBox[{"data_DataTable", ",", "ratios_List"}], "]"}], ":=", 
  RowBox[{"Module", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"Isotopes", ",", "ListRatios", ",", "ReplRules", ",", "OutDat"}],
      "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{"(", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"Isotopes", "=", 
       RowBox[{"DeleteDuplicates", "@", 
        RowBox[{"Cases", "[", 
         RowBox[{
          RowBox[{
           RowBox[{"(", 
            RowBox[{
             RowBox[{
              RowBox[{"List", "@@", "#"}], "&"}], "/@", "ratios"}], ")"}], "//",
            "Flatten"}], ",", "_String", ",", "2"}], "]"}]}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"ListRatios", "=", 
       RowBox[{
        RowBox[{
         RowBox[{"(", 
          RowBox[{
           RowBox[{"Cases", "[", 
            RowBox[{
             RowBox[{"List", "@@", 
              RowBox[{"(", "#", ")"}]}], ",", "_String", ",", "2"}], "]"}], "//",
            "Reverse"}], ")"}], "&"}], "/@", "ratios"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"ReplRules", "=", 
       RowBox[{"Association", "[", 
        RowBox[{"(", 
         RowBox[{
          RowBox[{
           RowBox[{
            RowBox[{"(", 
             RowBox[{
              RowBox[{"#", "[", 
               RowBox[{"[", "1", "]"}], "]"}], "<>", "\"\</\>\"", "<>", 
              RowBox[{"#", "[", 
               RowBox[{"[", "2", "]"}], "]"}]}], ")"}], "\[Rule]", 
            RowBox[{
             RowBox[{"#", "[", 
              RowBox[{"[", "1", "]"}], "]"}], "/", 
             RowBox[{"#", "[", 
              RowBox[{"[", "2", "]"}], "]"}]}]}], "&"}], "/@", "ListRatios"}],
          ")"}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"OutDat", "=", 
       RowBox[{"data", "[", 
        RowBox[{"All", ",", "All", ",", "Isotopes"}], "]"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"OutDat", "=", 
       RowBox[{"SSBSubstractBlanks", "[", "OutDat", "]"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"QueryTo", "[", 
       RowBox[{"OutDat", ",", "All", ",", "All", ",", 
        RowBox[{
         RowBox[{"ReplRules", "/.", "#"}], "&"}]}], "]"}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"QueryTo", "[", 
       RowBox[{"OutDat", ",", "All", ",", "FilterOutliers"}], "]"}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"QueryTo", "[", 
       RowBox[{"OutDat", ",", "All", ",", "MeanAndStandardError"}], "]"}], 
      ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"JoinT", "[", 
       RowBox[{
        RowBox[{
         RowBox[{
          RowBox[{
           RowBox[{"data", "[", 
            RowBox[{"[", 
             RowBox[{"2", ";;", 
              RowBox[{"-", "2"}], ";;", "2"}], "]"}], "]"}], "[", 
           RowBox[{"All", ",", "All", ",", 
            RowBox[{
             RowBox[{"AbsoluteTime", "[", 
              RowBox[{"#", "[", "\"\<Time\>\"", "]"}], "]"}], "&"}]}], "]"}], 
          "[", 
          RowBox[{"All", ",", "Mean"}], "]"}], "[", 
         RowBox[{"All", ",", 
          RowBox[{
           RowBox[{"<|", 
            RowBox[{"\"\<Time\>\"", "\[Rule]", 
             RowBox[{"DateObject", "[", "#", "]"}]}], "|>"}], "&"}]}], "]"}], 
        ",", "OutDat"}], "]"}]}], "\[IndentingNewLine]", ")"}]}], 
   "\[IndentingNewLine]", "]"}]}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"858ee8fa-51b8-4999-8028-a240b089b816"]
}, Open  ]],

Cell[CellGroupData[{

Cell["End private context", "Section"],

Cell[BoxData[
 RowBox[{
  RowBox[{"End", "[", "]"}], ";"}]], "Input",
 InitializationCell->True]
}, Closed]],

Cell[CellGroupData[{

Cell["End package", "Section"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"Protect", "[", 
   RowBox[{"Evaluate", "[", 
    RowBox[{"$Context", "<>", "\"\<*\>\""}], "]"}], "]"}], ";"}], "\n", 
 RowBox[{
  RowBox[{"EndPackage", "[", "]"}], ";"}]}], "Input",
 InitializationCell->True]
}, Open  ]]
}, Open  ]]
},
AutoGeneratedPackage->Automatic,
WindowSize->{1904, 1007},
WindowMargins->{{-8, Automatic}, {Automatic, 0}},
DockedCells->FEPrivate`FrontEndResource["FEExpressions", "PackageToolbar"],
Magnification:>1.1 Inherited,
FrontEndVersion->"11.2 for Microsoft Windows (64-bit) (September 10, 2017)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[580, 22, 38, 0, 107, "Title",ExpressionUUID->"33ce0891-e8e8-4ebb-b044-5e9c4997aa1c"],
Cell[CellGroupData[{
Cell[643, 26, 32, 0, 73, "Section",ExpressionUUID->"3bdffe53-503e-438d-b368-6dcabd3d2a3a"],
Cell[678, 28, 405, 10, 117, "Input",ExpressionUUID->"bf162c25-922f-412c-ba6b-98deeb45f15e",
 InitializationCell->True],
Cell[1086, 40, 184, 5, 50, "Input",ExpressionUUID->"520d09b8-e743-4a17-9289-308788865ffd",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[1307, 50, 27, 0, 73, "Section",ExpressionUUID->"b8b25aa4-2812-4686-80aa-91088fe87a53"],
Cell[CellGroupData[{
Cell[1359, 54, 31, 0, 59, "Subsection",ExpressionUUID->"197aea84-b377-4cf7-912d-a35543b2f904"],
Cell[1393, 56, 680, 14, 249, "Input",ExpressionUUID->"54dddeb2-fca2-4fb5-8f12-060647fd05eb",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[2110, 75, 29, 0, 59, "Subsection",ExpressionUUID->"38774563-428a-4738-bc44-2f472f6986a0"],
Cell[2142, 77, 263, 5, 95, "Input",ExpressionUUID->"398ecc10-42f3-4937-9d24-52234dc16099",
 InitializationCell->True]
}, Open  ]]
}, Open  ]],
Cell[CellGroupData[{
Cell[2454, 88, 40, 0, 73, "Section",ExpressionUUID->"3fb9cf7e-57f8-4225-b828-eab0a5ff23e0"],
Cell[2497, 90, 119, 3, 500, "Input",ExpressionUUID->"a80f1952-e8e6-428a-a8e6-66f91e02e120",
 InitializationCell->True]
}, Closed]],
Cell[CellGroupData[{
Cell[2653, 98, 33, 0, 57, "Section",ExpressionUUID->"0b6136ad-7382-4b6a-89b1-26eaa8453669"],
Cell[2689, 100, 552, 15, 728, "Input",ExpressionUUID->"d3ed0f0e-4479-480f-a1c7-e66e73935a92",
 InitializationCell->True],
Cell[3244, 117, 367, 12, 322, "Input",ExpressionUUID->"20e17bf5-1e3f-4d78-8e6c-016ac16b9ba4",
 InitializationCell->True]
}, Closed]],
Cell[CellGroupData[{
Cell[3648, 134, 37, 0, 57, "Section",ExpressionUUID->"c87fdc16-04cd-47fe-9819-f8e9cf145a73"],
Cell[3688, 136, 11487, 267, 1569, "Input",ExpressionUUID->"26fe29de-4be2-43e1-9819-c1b6864d0d99",
 InitializationCell->True],
Cell[15178, 405, 13906, 346, 1305, "Input",ExpressionUUID->"5dd53bca-1439-4d1c-aeac-3652bc40f6c5",
 InitializationCell->True],
Cell[29087, 753, 12832, 288, 1503, "Input",ExpressionUUID->"a864546c-ae96-408e-a905-3556a0f423f7",
 InitializationCell->True],
Cell[41922, 1043, 14152, 331, 933, "Input",ExpressionUUID->"2cadb111-9064-4b6e-93b4-353cf456679e",
 InitializationCell->True],
Cell[56077, 1376, 3623, 89, 359, "Input",ExpressionUUID->"c76fa7bf-a08a-4ffc-af7a-199d7d3302e4",
 InitializationCell->True],
Cell[59703, 1467, 5333, 118, 865, "Input",ExpressionUUID->"1aae6e4a-6a17-4e36-b76c-44545c172a72",
 InitializationCell->True],
Cell[65039, 1587, 2453, 61, 346, "Input",ExpressionUUID->"6e5f865b-3735-40ca-bdb1-fc552d4c4049",
 InitializationCell->True],
Cell[67495, 1650, 21569, 502, 2453, "Input",ExpressionUUID->"15e58c39-01d1-465c-b604-7c3f5d07d9c5",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[89101, 2157, 57, 0, 90, "Section",ExpressionUUID->"98ffbb77-dbf6-44d4-8fc8-5a88bb8f80ad"],
Cell[89161, 2159, 3450, 82, 566, "Input",ExpressionUUID->"d8a44262-6fe2-487d-8695-a37942eec035",
 InitializationCell->True],
Cell[92614, 2243, 2325, 61, 303, "Input",ExpressionUUID->"1fcb4427-e2c2-4191-b9bd-951aecfb123e",
 InitializationCell->True],
Cell[94942, 2306, 1860, 50, 157, "Input",ExpressionUUID->"76e16fda-5648-4364-a51e-75be3ca53747",
 InitializationCell->True],
Cell[96805, 2358, 1875, 51, 157, "Input",ExpressionUUID->"dc130a7a-0484-4928-a683-4d1c6a5e9281",
 InitializationCell->True],
Cell[98683, 2411, 2091, 55, 303, "Input",ExpressionUUID->"c8671769-2ab8-40e8-b615-7a9fdd82f2c5",
 InitializationCell->True],
Cell[100777, 2468, 4915, 125, 655, "Input",ExpressionUUID->"9b46345c-1dca-4167-b375-1983ff91cc7e",
 InitializationCell->True],
Cell[105695, 2595, 16916, 410, 2763, "Input",ExpressionUUID->"dae147cb-cf44-48e8-9d1a-1f530abf92e3",
 InitializationCell->True],
Cell[122614, 3007, 6561, 152, 976, "Input",ExpressionUUID->"125f0900-66a8-4a8e-8fee-6b07b8f9a190",
 InitializationCell->True],
Cell[129178, 3161, 7098, 173, 713, "Input",ExpressionUUID->"e85ebe00-3451-461a-a03a-7049b2f2b58c",
 InitializationCell->True],
Cell[136279, 3336, 3336, 87, 309, "Input",ExpressionUUID->"a340b9d8-86a9-4446-8c05-ba07c7019666"],
Cell[139618, 3425, 4152, 102, 596, "Input",ExpressionUUID->"858ee8fa-51b8-4999-8028-a240b089b816",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[143807, 3532, 38, 0, 99, "Section",ExpressionUUID->"1522c2f8-6dd0-495d-bee8-5fe46e3fe3cc"],
Cell[143848, 3534, 96, 3, 427, "Input",ExpressionUUID->"aa004ed2-f0dd-4d36-a303-70797ff707fa",
 InitializationCell->True]
}, Closed]],
Cell[CellGroupData[{
Cell[143981, 3542, 30, 0, 75, "Section",ExpressionUUID->"375dcbb8-a029-4ed1-a8a8-1bcbed2ca4b8"],
Cell[144014, 3544, 244, 7, 108, "Input",ExpressionUUID->"30186511-c3dd-422a-b5e1-2d1dcc4b34ce",
 InitializationCell->True]
}, Open  ]]
}, Open  ]]
}
]
*)

