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

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

(* CreatedBy='Mathematica 9.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       157,          7]
NotebookDataLength[     25666,        681]
NotebookOptionsPosition[     21887,        599]
NotebookOutlinePosition[     22345,        616]
CellTagsIndexPosition[     22302,        613]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["Phreeqc", "Title",ExpressionUUID->"b3757596-3fc5-47c3-a912-96ac76688ebf"],

Cell[CellGroupData[{

Cell["Begin package", "Section",ExpressionUUID->"03e8e64e-1a43-4904-91ff-d6bf7d62264b"],

Cell[BoxData[
 RowBox[{
  RowBox[{"BeginPackage", "[", 
   RowBox[{"\"\<DataAnalysis`Phreeqc`\>\"", ",", 
    RowBox[{"{", "\"\<DataAnalysis`\>\"", "}"}]}], "]"}], ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"a2d4416a-60ff-4c20-8e35-385309a395ce"],

Cell[BoxData[
 RowBox[{
  RowBox[{"Unprotect", "[", 
   RowBox[{"Evaluate", "[", 
    RowBox[{"$Context", "<>", "\"\<*\>\""}], "]"}], "]"}], ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"a27989ab-86d2-46ea-b79b-6cdd86195da1"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Messages", "Section",ExpressionUUID->"f49c46c5-f07e-4b49-ae71-60946ef6a7b2"],

Cell[CellGroupData[{

Cell["Functions", "Subsection",ExpressionUUID->"b9949ac9-a5c6-4bd7-b035-e3591da31926"],

Cell[BoxData[{
 RowBox[{"PhreeqcOpenLink", ";"}], "\[IndentingNewLine]", 
 RowBox[{"PhreeqcCloseLink", ";"}], "\[IndentingNewLine]", 
 RowBox[{"PhreeqcLoadDB", ";"}], "\[IndentingNewLine]", 
 RowBox[{"PhreeqcRunString", ";"}], "\[IndentingNewLine]", 
 RowBox[{"PhreeqcRunFile", ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"8c3ed1ea-031d-483d-882a-f276d3eb3fde"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Options", "Subsection",ExpressionUUID->"27043a36-bfbf-4496-8236-cd29de10248c"],

Cell[BoxData[
 RowBox[{"SelectedOutput", ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"9702fba5-ce2d-4177-97fe-d8d2b514425f"]
}, Open  ]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Begin private context", "Section",ExpressionUUID->"d848fb4d-7f6f-40e7-91e9-2797cf8b8e27"],

Cell[BoxData[
 RowBox[{
  RowBox[{"Begin", "[", "\"\<`Private`\>\"", "]"}], ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"fc85558f-9dca-408a-a518-36cdee216454"]
}, Closed]],

Cell[CellGroupData[{

Cell["Error handling", "Section",ExpressionUUID->"ba4a8ac4-7076-4c3d-8293-1477e383577c"],

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,ExpressionUUID->"a6f67f45-9053-4f68-933d-28739f73f2f5"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"SetAttributes", "[", 
   RowBox[{"CatchError", ",", "HoldAll"}], "]"}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"CatchError", "[", "Body_", "]"}], ":=", 
   RowBox[{"(", 
    RowBox[{
     RowBox[{"Catch", "[", "Body", "]"}], "===", "$ErrorFlag"}], ")"}]}], 
  ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"4b3f312e-2999-4ab1-9b88-2ea202edc072"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Directory", "Section",ExpressionUUID->"98c90b6c-08de-4e6f-8df1-d3622852088d"],

Cell[BoxData[
 RowBox[{
  RowBox[{"PackageFolder", "=", 
   RowBox[{"DirectoryName", "[", "$InputFileName", "]"}]}], ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"a9d7929b-7866-40ec-8a26-5652d21f7bd5"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Link handling", "Section",ExpressionUUID->"398ed82e-aae4-4824-9ff5-13ef9e30707b"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcOpenLink", "::", "usage"}], "=", 
   "\"\<PhreeqcOpenLink[] opens a new link to IPhreeqc and returns the link \
object.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcOpenLink", "[", "]"}], ":=", 
   RowBox[{"Module", "[", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", "L", "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"(", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"SetDirectory", "[", 
        RowBox[{"ToFileName", "[", 
         RowBox[{"{", 
          RowBox[{"PackageFolder", ",", "\"\<bin\>\"", ",", "$SystemID"}], 
          "}"}], "]"}], "]"}], ";", "\[IndentingNewLine]", 
       RowBox[{"L", "=", 
        RowBox[{"If", "[", 
         RowBox[{
          RowBox[{"$OperatingSystem", "\[Equal]", "\"\<Windows\>\""}], ",", 
          "\[IndentingNewLine]", 
          RowBox[{"Install", "[", "\"\<PhreeqMathematica`\>\"", "]"}], ",", 
          "\[IndentingNewLine]", 
          RowBox[{"Install", "[", "\"\<PhreeqMathematica\>\"", "]"}]}], 
         "\[IndentingNewLine]", "]"}]}], ";", "\[IndentingNewLine]", 
       RowBox[{"ResetDirectory", "[", "]"}], ";", "\[IndentingNewLine]", 
       "\[IndentingNewLine]", "L"}], "\[IndentingNewLine]", ")"}]}], 
    "\[IndentingNewLine]", "]"}]}], ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"ff795b99-2a4e-4d07-8a40-8d27ae393809"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcCloseLink", "::", "usage"}], "=", 
   "\"\<PhreeqcCloseLink[link] closes a link to certain IPhreeqc link object.\
\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"PhreeqcCloseLink", "[", "l_LinkObject", "]"}], ":=", 
    RowBox[{"Module", "[", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"{", "}"}], ",", "\[IndentingNewLine]", 
      RowBox[{"(", "\[IndentingNewLine]", 
       RowBox[{"Uninstall", "[", "l", "]"}], "\[IndentingNewLine]", ")"}]}], 
     "\[IndentingNewLine]", "]"}]}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcCloseLink", "[", "]"}], ":=", 
   RowBox[{"If", "[", 
    RowBox[{
     RowBox[{
      RowBox[{"Head", "[", "$DefaultPhreeqcLink", "]"}], "===", 
      "LinkObject"}], ",", 
     RowBox[{"Uninstall", "[", "$DefaultPhreeqcLink", "]"}]}], "]"}]}], 
  ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"3f73fef7-e3d2-4ca1-8f5b-1ec45d1990d4"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcLoadDB", "::", "usage"}], "=", 
   "\"\<PhreeqcLoadDB[link,file] sets a new database for a certain IPhreeqc \
link object.\nPhreeqcLoadDB[file] sets a new database for the default \
IPhreeqc link object.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcLoadDB", "[", 
    RowBox[{"l_LinkObject", ",", "file_"}], "]"}], ":=", 
   RowBox[{"Module", "[", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"(", "\[IndentingNewLine]", 
      RowBox[{"ExternalCall", "[", 
       RowBox[{"l", ",", 
        RowBox[{"CallPacket", "[", 
         RowBox[{"0", ",", 
          RowBox[{"{", "file", "}"}]}], "]"}]}], "]"}], "\[IndentingNewLine]",
       ")"}]}], "\[IndentingNewLine]", "]"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcLoadDB", "[", "file_", "]"}], ":=", 
   RowBox[{"(", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"CheckDefaultLink", "[", "]"}], ";", "\[IndentingNewLine]", 
     RowBox[{"PhreeqcLoadDB", "[", 
      RowBox[{"$DefaultPhreeqcLink", ",", "file"}], "]"}]}], 
    "\[IndentingNewLine]", ")"}]}], ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"10e233d1-d9a6-406b-9365-3cf4889deb03"],

Cell[BoxData[
 RowBox[{
  RowBox[{"CheckDefaultLink", "[", "]"}], ":=", 
  RowBox[{"(", "\[IndentingNewLine]", 
   RowBox[{"If", "[", 
    RowBox[{
     RowBox[{"!", 
      RowBox[{"(", 
       RowBox[{
        RowBox[{"Head", "[", "$DefaultPhreeqcLink", "]"}], "===", 
        "LinkObject"}], ")"}]}], ",", "\[IndentingNewLine]", 
     RowBox[{"(", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"Unprotect", "[", "$DefaultPhreeqcLink", "]"}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"$DefaultPhreeqcLink", "=", 
        RowBox[{"PhreeqcOpenLink", "[", "]"}]}], ";", "\[IndentingNewLine]", 
       RowBox[{"Protect", "[", "$DefaultPhreeqcLink", "]"}], ";"}], 
      "\[IndentingNewLine]", ")"}], ",", "\[IndentingNewLine]", 
     RowBox[{"If", "[", 
      RowBox[{
       RowBox[{"!", 
        RowBox[{"MemberQ", "[", 
         RowBox[{
          RowBox[{"Links", "[", "]"}], ",", "$DefaultPhreeqcLink"}], "]"}]}], 
       ",", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{"Unprotect", "[", "$DefaultPhreeqcLink", "]"}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"Quiet", "[", 
         RowBox[{"Uninstall", "[", "$DefaultPhreeqcLink", "]"}], "]"}], ";", 
        "\[IndentingNewLine]", 
        RowBox[{"$DefaultPhreeqcLink", "=", 
         RowBox[{"PhreeqcOpenLink", "[", "]"}]}], ";", "\[IndentingNewLine]", 
        
        RowBox[{"Protect", "[", "$DefaultPhreeqcLink", "]"}], ";"}]}], 
      "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}], 
   "\[IndentingNewLine]", ")"}]}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"00ae13c9-458a-4d03-b64f-04251fbe99ab"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Run scripts", "Section",ExpressionUUID->"b578d522-78b7-41fe-be31-5bf40a3c4d62"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "PhreeqcRunString", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{"SelectedOutput", "\[Rule]", "False"}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"SelectedOutput", "::", "usage"}], "=", 
    "\"\<SelectedOutput is an option to PhreeqcRunString[link,str] specifying \
whether a selected output table should be returned.\>\""}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"PhreeqcRunString", "::", "noresult"}], "=", 
    "\"\<PHREEQC did not return any selected output for the script \
'`1`'.\>\""}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcRunString", "::", "usage"}], "=", 
   "\"\<PhreeqcRunString[link,str] evaluates a PHREEQC script from a string \
using the specified link object.\nPhreeqcRunString[str] uses the default link \
object.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcRunString", "[", 
    RowBox[{"l_LinkObject", ",", "str_", ",", 
     RowBox[{"opts", ":", 
      RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
   RowBox[{"Module", "[", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{"Ret", ",", "ColRow"}], "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"(", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"Ret", "=", 
        RowBox[{"ExternalCall", "[", 
         RowBox[{"l", ",", 
          RowBox[{"CallPacket", "[", 
           RowBox[{"1", ",", 
            RowBox[{"{", 
             RowBox[{
              RowBox[{"StringReplace", "[", 
               RowBox[{"str", ",", 
                RowBox[{"\"\<\\n\>\"", "\[Rule]", "\"\<;\>\""}]}], "]"}], ",", 
              RowBox[{"If", "[", 
               RowBox[{
                RowBox[{"TrueQ", "[", 
                 RowBox[{"OptionValue", "[", "SelectedOutput", "]"}], "]"}], 
                ",", "1", ",", "0"}], "]"}]}], "}"}]}], "]"}]}], "]"}]}], ";",
        "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"TrueQ", "[", 
          RowBox[{"OptionValue", "[", "SelectedOutput", "]"}], "]"}], ",", 
         "\[IndentingNewLine]", 
         RowBox[{"(*", 
          RowBox[{"MapIndexed", "[", 
           RowBox[{
            RowBox[{
             RowBox[{"(", 
              RowBox[{"Prepend", "[", 
               RowBox[{"#1", ",", 
                RowBox[{"If", "[", 
                 RowBox[{
                  RowBox[{
                   RowBox[{"#2", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "\[Equal]", "1"}], ",", 
                  "\"\<ID\>\"", ",", 
                  RowBox[{
                   RowBox[{"#2", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "-", "1"}]}], "]"}]}], 
               "]"}], ")"}], "&"}], ",", 
            RowBox[{"ImportString", "[", 
             RowBox[{"Ret", ",", "\"\<Table\>\""}], "]"}]}], "]"}], "*)"}], 
         "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{"Ret", "===", "\"\<\>\""}], ",", 
            RowBox[{"ErrorMessage", "[", 
             RowBox[{
              RowBox[{"PhreeqcRunString", "::", "noresult"}], ",", "str"}], 
             "]"}]}], "]"}], ";", "\[IndentingNewLine]", 
          RowBox[{"Ret", "=", 
           RowBox[{"ImportString", "[", 
            RowBox[{"Ret", ",", "\"\<Table\>\""}], "]"}]}], ";", 
          "\[IndentingNewLine]", 
          RowBox[{"ColRow", "=", 
           RowBox[{
            RowBox[{"FirstPosition", "[", 
             RowBox[{"Ret", ",", 
              RowBox[{"{", 
               RowBox[{"_String", ".."}], "}"}]}], "]"}], "//", "First"}]}], 
          ";", "\[IndentingNewLine]", 
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{"ColRow", ">", "1"}], ",", "\[IndentingNewLine]", 
            RowBox[{
             RowBox[{"PrependTo", "[", 
              RowBox[{"Ret", ",", 
               RowBox[{"Ret", "[", 
                RowBox[{"[", "ColRow", "]"}], "]"}]}], "]"}], ";", 
             "\[IndentingNewLine]", 
             RowBox[{"Ret", "=", 
              RowBox[{"Delete", "[", 
               RowBox[{"Ret", ",", 
                RowBox[{"ColRow", "+", "1"}]}], "]"}]}], ";"}]}], 
           "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", 
          "\[IndentingNewLine]", 
          RowBox[{"MapIndexed", "[", 
           RowBox[{
            RowBox[{
             RowBox[{"(", 
              RowBox[{"Prepend", "[", 
               RowBox[{"#1", ",", 
                RowBox[{"If", "[", 
                 RowBox[{
                  RowBox[{
                   RowBox[{"#2", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "\[Equal]", "1"}], ",", 
                  "\"\<ID\>\"", ",", 
                  RowBox[{"\"\<Result-\>\"", "<>", 
                   RowBox[{"ToString", "[", 
                    RowBox[{
                    RowBox[{"#2", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "-", "1"}], "]"}]}]}], 
                 "]"}]}], "]"}], ")"}], "&"}], ",", "Ret"}], "]"}]}], 
         "\[IndentingNewLine]", ",", "\[IndentingNewLine]", "Ret"}], 
        "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", ")"}]}], 
    "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcRunString", "[", 
    RowBox[{"str_", ",", 
     RowBox[{"opts", ":", 
      RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
   RowBox[{"(", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"CheckDefaultLink", "[", "]"}], ";", "\[IndentingNewLine]", 
     RowBox[{"PhreeqcRunString", "[", 
      RowBox[{"$DefaultPhreeqcLink", ",", "str", ",", "opts"}], "]"}]}], 
    "\[IndentingNewLine]", ")"}]}], ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"2f4149e6-47d5-4e09-a326-f4381b472d47"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "PhreeqcRunFile", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{"SelectedOutput", "\[Rule]", "False"}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"PhreeqcRunFile", "::", "noresult"}], "=", 
    "\"\<PHREEQC did not return any selected output for the PHREEQC script in \
'`1`'.\>\""}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcRunFile", "::", "usage"}], "=", 
   "\"\<PhreeqcRunFile[link,file] evaluates a PHREEQC script from a file \
using the specified link object.\nPhreeqcRunFile[file] uses the default link \
object.\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcRunFile", "[", 
    RowBox[{"l_LinkObject", ",", "file_", ",", 
     RowBox[{"opts", ":", 
      RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
   RowBox[{"Module", "[", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{"Ret", ",", "ColRow"}], "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"(", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"Ret", "=", 
        RowBox[{"ExternalCall", "[", 
         RowBox[{"l", ",", 
          RowBox[{"CallPacket", "[", 
           RowBox[{"2", ",", 
            RowBox[{"{", 
             RowBox[{"file", ",", 
              RowBox[{"If", "[", 
               RowBox[{
                RowBox[{"TrueQ", "[", 
                 RowBox[{"OptionValue", "[", "SelectedOutput", "]"}], "]"}], 
                ",", "1", ",", "0"}], "]"}]}], "}"}]}], "]"}]}], "]"}]}], ";",
        "\[IndentingNewLine]", "\[IndentingNewLine]", 
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"TrueQ", "[", 
          RowBox[{"OptionValue", "[", "SelectedOutput", "]"}], "]"}], ",", 
         "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{"Ret", "===", "\"\<\>\""}], ",", 
            RowBox[{"ErrorMessage", "[", 
             RowBox[{
              RowBox[{"PhreeqcRunFile", "::", "noresult"}], ",", "file"}], 
             "]"}]}], "]"}], ";", "\[IndentingNewLine]", 
          RowBox[{"(*", 
           RowBox[{"MapIndexed", "[", 
            RowBox[{
             RowBox[{
              RowBox[{"(", 
               RowBox[{"Prepend", "[", 
                RowBox[{"#1", ",", 
                 RowBox[{"If", "[", 
                  RowBox[{
                   RowBox[{
                    RowBox[{"#2", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "\[Equal]", "1"}], ",", 
                   "\"\<ID\>\"", ",", 
                   RowBox[{
                    RowBox[{"#2", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "-", "1"}]}], "]"}]}], 
                "]"}], ")"}], "&"}], ",", 
             RowBox[{"ImportString", "[", 
              RowBox[{"Ret", ",", "\"\<Table\>\""}], "]"}]}], "]"}], "*)"}], 
          "\[IndentingNewLine]", 
          RowBox[{"Ret", "=", 
           RowBox[{"ImportString", "[", 
            RowBox[{"Ret", ",", "\"\<Table\>\""}], "]"}]}], ";", 
          "\[IndentingNewLine]", 
          RowBox[{"ColRow", "=", 
           RowBox[{
            RowBox[{"FirstPosition", "[", 
             RowBox[{"Ret", ",", 
              RowBox[{"{", 
               RowBox[{"_String", ".."}], "}"}]}], "]"}], "//", "First"}]}], 
          ";", "\[IndentingNewLine]", 
          RowBox[{"If", "[", 
           RowBox[{
            RowBox[{"ColRow", ">", "1"}], ",", "\[IndentingNewLine]", 
            RowBox[{
             RowBox[{"PrependTo", "[", 
              RowBox[{"Ret", ",", 
               RowBox[{"Ret", "[", 
                RowBox[{"[", "ColRow", "]"}], "]"}]}], "]"}], ";", 
             "\[IndentingNewLine]", 
             RowBox[{"Ret", "=", 
              RowBox[{"Delete", "[", 
               RowBox[{"Ret", ",", 
                RowBox[{"ColRow", "+", "1"}]}], "]"}]}], ";"}]}], 
           "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", 
          "\[IndentingNewLine]", 
          RowBox[{"MapIndexed", "[", 
           RowBox[{
            RowBox[{
             RowBox[{"(", 
              RowBox[{"Prepend", "[", 
               RowBox[{"#1", ",", 
                RowBox[{"If", "[", 
                 RowBox[{
                  RowBox[{
                   RowBox[{"#2", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "\[Equal]", "1"}], ",", 
                  "\"\<ID\>\"", ",", 
                  RowBox[{"\"\<Result-\>\"", "<>", 
                   RowBox[{"ToString", "[", 
                    RowBox[{
                    RowBox[{"#2", "[", 
                    RowBox[{"[", "1", "]"}], "]"}], "-", "1"}], "]"}]}]}], 
                 "]"}]}], "]"}], ")"}], "&"}], ",", "Ret"}], "]"}]}], 
         "\[IndentingNewLine]", ",", "\[IndentingNewLine]", "Ret"}], 
        "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", ")"}]}], 
    "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"PhreeqcRunFile", "[", 
    RowBox[{"file_", ",", 
     RowBox[{"opts", ":", 
      RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
   RowBox[{"(", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"CheckDefaultLink", "[", "]"}], ";", "\[IndentingNewLine]", 
     RowBox[{"PhreeqcRunFile", "[", 
      RowBox[{"$DefaultPhreeqcLink", ",", "file", ",", "opts"}], "]"}]}], 
    "\[IndentingNewLine]", ")"}]}], ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"5c62f437-3077-4b6f-b9c8-b07684258a91"]
}, Open  ]],

Cell[CellGroupData[{

Cell["End private context", "Section",ExpressionUUID->"7e15254e-c78e-41f8-a65d-e2292127ba99"],

Cell[BoxData[
 RowBox[{
  RowBox[{"End", "[", "]"}], ";"}]], "Input",
 InitializationCell->
  True,ExpressionUUID->"5c86e5bd-5a68-4122-8b0b-607e3feaa885"]
}, Closed]],

Cell[CellGroupData[{

Cell["End package", "Section",ExpressionUUID->"e0ca3e82-c38f-488d-a851-6354ceef0951"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"Protect", "[", 
   RowBox[{"Evaluate", "[", 
    RowBox[{"$Context", "<>", "\"\<*\>\""}], "]"}], "]"}], ";"}], "\n", 
 RowBox[{
  RowBox[{"EndPackage", "[", "]"}], ";"}]}], "Input",
 InitializationCell->
  True,ExpressionUUID->"71b9bd00-fb6a-4b92-aaf6-c50707e4f717"]
}, Closed]]
}, Open  ]]
},
AutoGeneratedPackage->Automatic,
WindowSize->{1920, 1007},
WindowMargins->{{-8, Automatic}, {Automatic, -8}},
DockedCells->FEPrivate`FrontEndResource["FEExpressions", "PackageToolbar"],
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[579, 22, 79, 0, 96, "Title",ExpressionUUID->"b3757596-3fc5-47c3-a912-96ac76688ebf"],
Cell[CellGroupData[{
Cell[683, 26, 87, 0, 67, "Section",ExpressionUUID->"03e8e64e-1a43-4904-91ff-d6bf7d62264b"],
Cell[773, 28, 265, 6, 44, "Input",ExpressionUUID->"a2d4416a-60ff-4c20-8e35-385309a395ce",
 InitializationCell->True],
Cell[1041, 36, 242, 6, 44, "Input",ExpressionUUID->"a27989ab-86d2-46ea-b79b-6cdd86195da1",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[1320, 47, 82, 0, 67, "Section",ExpressionUUID->"f49c46c5-f07e-4b49-ae71-60946ef6a7b2"],
Cell[CellGroupData[{
Cell[1427, 51, 86, 0, 53, "Subsection",ExpressionUUID->"b9949ac9-a5c6-4bd7-b035-e3591da31926"],
Cell[1516, 53, 380, 7, 121, "Input",ExpressionUUID->"8c3ed1ea-031d-483d-882a-f276d3eb3fde",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[1933, 65, 84, 0, 53, "Subsection",ExpressionUUID->"27043a36-bfbf-4496-8236-cd29de10248c"],
Cell[2020, 67, 142, 3, 44, "Input",ExpressionUUID->"9702fba5-ce2d-4177-97fe-d8d2b514425f",
 InitializationCell->True]
}, Open  ]]
}, Open  ]],
Cell[CellGroupData[{
Cell[2211, 76, 95, 0, 67, "Section",ExpressionUUID->"d848fb4d-7f6f-40e7-91e9-2797cf8b8e27"],
Cell[2309, 78, 177, 4, 31, "Input",ExpressionUUID->"fc85558f-9dca-408a-a518-36cdee216454",
 InitializationCell->True]
}, Closed]],
Cell[CellGroupData[{
Cell[2523, 87, 88, 0, 53, "Section",ExpressionUUID->"ba4a8ac4-7076-4c3d-8293-1477e383577c"],
Cell[2614, 89, 610, 16, 140, "Input",ExpressionUUID->"a6f67f45-9053-4f68-933d-28739f73f2f5",
 InitializationCell->True],
Cell[3227, 107, 425, 13, 64, "Input",ExpressionUUID->"4b3f312e-2999-4ab1-9b88-2ea202edc072",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[3689, 125, 83, 0, 67, "Section",ExpressionUUID->"98c90b6c-08de-4e6f-8df1-d3622852088d"],
Cell[3775, 127, 218, 5, 44, "Input",ExpressionUUID->"a9d7929b-7866-40ec-8a26-5652d21f7bd5",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[4030, 137, 87, 0, 67, "Section",ExpressionUUID->"398ed82e-aae4-4824-9ff5-13ef9e30707b"],
Cell[4120, 139, 1414, 32, 292, "Input",ExpressionUUID->"ff795b99-2a4e-4d07-8a40-8d27ae393809",
 InitializationCell->True],
Cell[5537, 173, 1037, 28, 197, "Input",ExpressionUUID->"3f73fef7-e3d2-4ca1-8f5b-1ec45d1990d4",
 InitializationCell->True],
Cell[6577, 203, 1297, 32, 254, "Input",ExpressionUUID->"10e233d1-d9a6-406b-9365-3cf4889deb03",
 InitializationCell->True],
Cell[7877, 237, 1634, 39, 311, "Input",ExpressionUUID->"00ae13c9-458a-4d03-b64f-04251fbe99ab",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[9548, 281, 85, 0, 67, "Section",ExpressionUUID->"b578d522-78b7-41fe-be31-5bf40a3c4d62"],
Cell[9636, 283, 5962, 147, 634, "Input",ExpressionUUID->"2f4149e6-47d5-4e09-a326-f4381b472d47",
 InitializationCell->True],
Cell[15601, 432, 5544, 137, 596, "Input",ExpressionUUID->"5c62f437-3077-4b6f-b9c8-b07684258a91",
 InitializationCell->True]
}, Open  ]],
Cell[CellGroupData[{
Cell[21182, 574, 93, 0, 67, "Section",ExpressionUUID->"7e15254e-c78e-41f8-a65d-e2292127ba99"],
Cell[21278, 576, 154, 4, 31, "Input",ExpressionUUID->"5c86e5bd-5a68-4122-8b0b-607e3feaa885",
 InitializationCell->True]
}, Closed]],
Cell[CellGroupData[{
Cell[21469, 585, 85, 0, 53, "Section",ExpressionUUID->"e0ca3e82-c38f-488d-a851-6354ceef0951"],
Cell[21557, 587, 302, 8, 52, "Input",ExpressionUUID->"71b9bd00-fb6a-4b92-aaf6-c50707e4f717",
 InitializationCell->True]
}, Closed]]
}, Open  ]]
}
]
*)

