BeginPackage["netUtil`"]kbin3::"usage" = "Keyboard input of numeric values."kbinstrg::"usage" = "Keyboard input of string response."inpvec::"usage" = 
      "Keyboard input of vector of elements and check range."pmat2::"usage" = "display matrix to screen."genrch::"usage" = "generate row/col headings for pmat2."rndoff::"usage" = "round off function."plotnLists::"usage" = "plot columns of matrix."plotLegend::"usage" = "plot color line legend."getFdata::"usage" = "get data from a file."timeDate::"usage" = "prints date and time."openFna::"usage" = "opens a file."rcvrExpr::"usage" = "recovers data from a file."rprtFdata::"usage" = "reports contents of data in a file."saveExpr::"usage" = "saves an expression to a file."Begin["`Private`"]kbin3[q_, p_] := 
      Module[{c, ans, vrp, vrp2}, c = 0; 
        vrp = " Input Error. Type a vector with # elements="; 
        vrp2 = " Input Error. Type a matrix of order "; 
        While[c == 0, ans = Input[q]; 
          Which[Head[ans] === Symbol, 
            Print[ans, " Input Error; Characters not allowed"]; , 
            Head[p] === Integer || Head[p] === Real, 
            If[Head[p] =!= Head[ans], 
              Print[ans, " Input Error. Input must be ", Head[p]]; , 
              c = 1]; , Head[p] === List && Length[p] == 1, 
            If[VectorQ[ans] == False, Print[ans, vrp<>ToString[p]]; , 
              If[Length[ans] ≠ First[p], 
                Print[ans, " Error: Vector requires # of elements=", 
                  First[p]]; , c = 1]]; , 
            Head[p] === List && Length[p] == 2, 
            If[MatrixQ[ans] == False, Print[ans, vrp2<>ToString[p]]; , 
              If[Dimensions[ans] ≠ p, 
                Print[ans, " Error; Matrix dimensions must be ", p]; 
                , c = 1]]; ]; ]; Return[ans]; ]kbinstrg[q_, p_] := 
      Module[{c, ans}, c = 0; 
        While[c == 0, ans = InputString[q]; 
          Do[If[{ans} == p[[j]], 
              c = 1; Break[]]; , {j, 1, Length[p]}]; 
          If[c ≠ 1, Print[ans, " Input Error. Type Again"]]; ]; 
        Return[ans]; ]inpvec[r_, n_, ty_] := 
      Module[{q, selv, rng}, q = "Type  element vector of  indices."; 
        q = StringInsert[StringInsert[q, ToString[n], 6], ty, -10]; 
        rng = False; 
        While[rng == False, selv = kbin3[q, {n}]; 
          If[Max[selv] > Max[r] || Min[selv] < Min[r], 
            Print[selv, ":Vector elements out of range"], rng = True]; ]; 
        Return[selv]; ]pmat2[m_, pc_, rch___] := 
      Module[{m1, a, c, indx, col1, row}, m1 = Transpose[m]; 
        c = Dimensions[m1][[1]]; 
        a = Floor[(c/pc)]; indx = Join[Table[pc, {a}], {Mod[c, pc]}]; 
        If[0 == indx[[-1]], 
          indx = Drop[indx, -1]]; 
        If[Length[{rch}] == 0, 
          row = (ToString[#1]&)/@
              Range[Dimensions[m][[1
                  ]]]; 
          col1 = 
            (ToString[#1]&)/@
              Range[Dimensions[m][[2
                  ]]]; , 
          row = rch[[1]]; 
          col1 = rch[[2]]; ]; 
        Do[Print[""]; 
          Print[TableForm[
              Transpose[
                Take[m1, indx[[j]]]], 
              TableHeadings -> 
                Append[{row}, 
                  Take[col1, 
                    indx[[j]]]], 
              TableSpacing -> {0, 4}]]; 
          m1 = Drop[m1, indx[[j]]]; 
          col1 = 
            Drop[col1, indx[[j]]]; 
          , {j, 1, Length[indx]}]; ]genrch[rh_, ch_, rc_] := 
      Module[{a, b, ab}, fn[a_, b_] := StringInsert[a, ToString[b], -1]; 
        a = Table[rh, {rc[[1]]}]; 
        a = 
          (fn[a[[#1]], #1]&)/@
            Range[rc[[1]]]; 
        b = Table[ch, {rc[[2]]}]; 
        b = (fn[b[[#1]], #1]&)/@
            Range[rc[[2]]]; 
        ab = Join[{a}, {b}]; Return[ab]; ]rndoff[n_, d_] := N[(10^(-d)) Round[n (10^d)]]plotnLists[m_, tl_, xu_, ps_, pm_, pn_:  0] := 
      Module[{pobj, colr, ipt, cntr = 0}, 
        If[ps == 0 && pm == 0, Print["Parameter error;no plot"]; 
          Return[]]; pobj = {}; 
        colr = { .5,  .0,  .5,  .0,  .0,  .0,  .0,  .0, 1. ,  .0, 
            1. ,  .0,  .0, 1. , 1. , 1. ,  .0,  .0, 1. ,  .0, 1. , 1. , 
            1. ,  .0,  .0,  .0,  .5,  .0,  .5,  .0,  .0,  .5,  .5,  .5,  .0, 
             .0}; If[ps == 1, ipt = $DisplayFunction, ipt = Identity]; 
        pobj = MapThread[
            ListPlot[#1, AxesLabel -> {xu, "y"}, 
                PlotJoined -> True, PlotRange -> All, 
                PlotLabel -> 
                  StringInsert[tl, ToString[cntr = cntr + 1], 1], 
                PlotStyle 
                  -> {
                    RGBColor[
                      (colr = RotateLeft[colr, 3])[[1
                        ]], 
                      colr[[2]], 
                      colr[[3]]]}, 
                DisplayFunction -> ipt]&, {Transpose[m]}]; 
        If[pn == 1, 
          MapThread[
            AppendTo[pobj[[#1, 1]], 
                Text[#1, 
                  Last[pobj[[#1, 1, 2, 1
                      ]]]]]&, {
              Range[Length[Transpose[m]]]}]; ]; 
        If[pm == 1, 
          Show[pobj, PlotLabel -> tl, 
            DisplayFunction -> $DisplayFunction]]; ]plotLegend[nl_] := 
      Module[{pobj, m, colr, nlm}, pobj = {}; 
        colr = { .5,  .0,  .5,  .0,  .0,  .0,  .0,  .0,  .0,  .0,  .0, 
            1. ,  .0, 1. ,  .0,  .0, 1. , 1. , 1. ,  .0,  .0, 1. ,  .0, 1. , 
            1. , 1. ,  .0,  .0,  .0,  .5,  .0,  .5,  .0,  .0,  .5,  .5,  .5, 
             .0,  .0}; If[nl > 12, nlm = 12, nlm = nl]; 
        If[nl ≤ 1, Return[]]; 
        m = Transpose[Table[Range[0, nlm], {2}]]; 
        pobj = MapThread[
            ListPlot[#1, AxesLabel -> {None, "Line"}, 
                Ticks -> {None, Range[-1 + (nlm + 1)]}, 
                PlotJoined -> True, PlotRange -> All, 
                PlotStyle 
                  -> {
                    RGBColor[
                      (colr = RotateLeft[colr, 3])[[1
                        ]], 
                      colr[[2]], 
                      colr[[3]]]}, 
                DisplayFunction -> Identity]&, {m}]; 
        Show[pobj, PlotLabel -> "Legend", 
          DisplayFunction -> $DisplayFunction]; ]; getFdata := 
      Module[{fname, ok, data, extnt, strt, end}, ok = 0; 
        While[ok == 0, fname = InputString["Type name of file"]; 
          If[fname == "quit", Return[{0, 0}]]; 
          If[1 == Length[FileNames[fname]], Print[fname, " was found"]; 
            ok = 1; , Print[fname, " was not found"]]; ]; 
        Print["File created:"]; timeDate[FileDate[fname]]; 
        extnt = kbinstrg[
            "Use all or part of data file? (all/part)", {{"all"}, {
                "part"}}]; Print["Data read from file=", extnt]; 
        If[extnt == "all", data = ReadList[fname]; 
          extnt = {1, 
              Dimensions[data][[1
                ]]}; , 
          strt = kbin3["Type starting position", 0]; 
          end = kbin3["Type ending position", 0]; OpenRead[fname]; 
          Skip[fname, Expression, strt]; 
          data = ReadList[fname, Expression, 1 + end - strt]; Close[fname]; 
          Print["Data requested from file=", strt, " to ", end]; 
          extnt = ToString[Join[{strt}, {end}]]; ]; 
        Print["Returned data dimension=", Dimensions[data]]; 
        Return[{data, extnt}]; ]\(timeDate[ts_] := 
      Module[{month}, 
        month = {{"Jan "}, {"Feb "}, {"Mar "}, {"Apr "}, {
              "May "}, {"Jun "}, {"Jul "}, {"Aug "}, {
              "Sep "}, {"Oct "}, {"Nov "}, {"Dec "}}; 
        Print[\(month
              [[ts[[2
                ]]]]\)[[1]], 
          ts[[3]], ",", 
          ts[[1]], " at ", 
          ts[[4]], ":", 
          ts[[5]]]; ]\)openFna[fc_, p_] := 
      Module[{ques, ans, ans1, fn, name, indx}, fn = {}; 
        ques = "Setup file for  ? y/n"; 
        Do[ans = 
            kbinstrg[
              StringInsert[ques, 
                fc[[j
                      ]][[2
                    ]][[1
                  ]], -6], {{"y"}, {"n"}}]; 
          If[ans == "y", ans1 = "n"; 
            While[ans1 == "n", 
              name = InputString[
                  StringInsert["Give a file name for ", 
                    fc[[j
                          ]][[2
                        ]][[1
                      ]], -1]]; 
              If[1 == Length[FileNames[name]], 
                Print[name, " already exists"]; 
                ans1 = kbinstrg[
                    "Continue with this choice? y/n", {{"y"}, {
                        "n"}}], ans1 = "y"]; ]; 
            indx = fc[[j
                  ]][[1
                ]]; fn = Append[fn, Append[{indx}, name]]; 
            If[p == 0, OpenRead[name]]; If[p == 1, OpenWrite[name]]; 
            If[p == 2, OpenAppend[name]]; If[p == 3, OpenTemporary[name]]; ]; 
          If[ans == "y", 
              Print["file for", 
                fc[[j]], " is ", 
                name]];, {j, 1, Length[fc]}]; Return[fn]; ]rcvrExpr[rid___, rds_:  0] := 
      Module[{ans, fname, ok = 0, expr, d, sw = 0}, 
        If[Length[{rid}] ≠ 0, 
          If[False === StringQ[rid], 
            Print["arg error:first arg must be string"]; Return[]]; ]; 
        If[False === IntegerQ[rds], 
          Print["arg error:2nd arg must be integer"]; Return[]]; 
        While[ok == 0, fname = InputString["Type name of file"]; 
          If[fname == "quit", Return[]]; 
          If[1 == Length[FileNames[fname]], 
            Print[fname, " file was found"]; ok = 1; , 
            Print[fname, " file was not found"]]; ]; 
        Print["File created on:"]; timeDate[FileDate[fname]]; 
        If[rds == 0, ans = "last"; , ans = "search"]; 
        Print["Expression recovery=", ans]; 
        If[ans == "last", d = Get[fname]; 
          expr = d[[3]]; 
          Print["Recovered Expr id=", 
            d[[1]], ",", 
            d[[2]]]; Return[expr]; ]; 
        Print["Searching for ", rid, ",", rds]; OpenRead[fname]; 
        While[sw == 0, d = Read[fname, Expression]; 
          If[d === EndOfFile, Break[]]; 
          If[d[[1]] == rid && 
              d[[2]] == rds, 
            expr = d[[3]]; sw = 1; 
            Close[fname]; Print["Expression Found"]; ]; ]; 
        If[sw == 0, Print["Search Failed"]; Return[]]; Return[expr]; ]
        rprtFdata := 
      Module[{fname, ok, eof, data, cnt}, cnt = 1; ok = 0; 
        While[ok == 0, fname = InputString["Type name of file"]; 
          If[fname == "quit", Return[]]; 
          If[1 == Length[FileNames[fname]], Print[fname, " was found"]; 
            ok = 1; , Print[fname, " was not found"]]; ]; 
        Print["File created:"]; timeDate[FileDate[fname]]; 
        OpenRead[fname]; eof = 0; 
        While[eof == 0, data = Read[fname, Expression]; 
          If[data === EndOfFile, Break[]; , 
            Print[cnt, " ", 
              data[[1]], ",", 
              data[[2]], " Length=", 
              Length[data[[3]]], 
              " Dimensions=", Dimensions[data], " Head=", 
              Head[data]]; cnt += 1]; ]; Close[fname]; ]\(saveExpr[x_] := 
      Module[{xid, xdt, fx, fi, xsin, y}, 
        If[Length[x] == 0, Print["Expression has not been evaluated"]; 
          Return[]]; Print["expression ok"]; 
        xid = InputString["Type an ID for the expression"]; 
        Print["Expression ID=", xid]; 
        xdt = Plus@@\((Date[]\ Reverse[Table[10\^\(y\ 2\), {y, 0, 5}]])\); 
        Print["Expression date-time stamp=", xdt]; fx = {{1, {xid}}}; 
        fi = openFna[fx, 2]; 
        If[Length[fi] ≠ 0, 
          Print["Opened ", \(fi[\([1]\)]\)[\([2]\)]]; 
          xsin = Join[{xid}, {xdt}]; 
          Write[\(fi[[1
                ]]\)[[2
              ]], Append[xsin, x]]; 
          Close[\(fi[[1
                ]]\)[[2
              ]]]]; 
	
        Print["File written and closed"]]\)End[]EndPackage[]