Print[StyleForm[StringJoin[
        "Updated: Sept 25, 2002. ",
        "Loaded DatIO.m"
        ]
  ,FontSize->9,FontFamily->"Times", FontColor->
  RGBColor[0., 0.25,0.25]
   ]];

(* Author: A.I. McLeod *)
(* Copyright (c), A.I. McLeod, 2001 *)
(* DatIO *)
(* Summary:
Data input/output.
*)


BeginPackage["DatIO`"]


Options[ReadData] = {Silent -> False};
 
ReadData /: ReadData::NotFound = "``";

ReadData /: ReadData::usage = "ReadData[file] file is a character string \
which specifies a file. ReadData[file] inputs a  data file. Initial lines in \
the file which begin with the character # are assumed to contain comments and \
documentation. The remainder of the file contains the data values \
\!\(z\_1\),\!\(z\_2\), ... which are read by rows.  The output is a vector \
containing all the data. There is one option which has default value, \
Silent->False.  When Silent->False, the title is displayed as well as the \
number of data values input. When Silent->True, the title is not displayed \
nor the number of data values input.  If the file contains p columns and n \
rows, you can obtain the n-by-p matrix by using Partition.  For example, \
Partition[ReadData[file],p] returns an n-by-p matrix assuming that there are \
n*p data values present in the file.";

Options[WriteData] = {Title -> False};
 
WriteData /: WriteData::usage = "WriteData[filename, x] outputs the numeric 
list x to filename. If x is a vector, it is output as a single column. If x 
is a matrix, it is output as a tab-delimited table.  Otherwise x is flattened 
and output as one column. WriteData takes an option argument, Title. If Title 
is set a character string then this string is first on  the output file. The 
default setting for Title is False. Number less than 0.00001 are chopped to 0."; 


(********************************************************************) 

Begin["`Private`"]

(********************************************************************) 

ReadData[(datafile_)?StringQ, (opts___)?OptionQ] := 
 Module[
  {sQ, continuetitle = True, z = {}, i, c, title, zt, NoTitle = True}, 
     If[ !File === FileType[datafile], Message[ReadData::NotFound, datafile]; 
        Return[{}]]; sQ = Silent /. {opts} /. Options[ReadData]; 
      OpenRead[datafile]; title = ""; While[continuetitle, 
       i = StreamPosition[datafile]; c = ToString[Read[datafile, Character]]; 
        c = Which[LetterQ[c], c, c == "#", "", True, 0]; 
        If[c != 0, NoTitle = False]; If[c == 0, continuetitle = False, 
         title = StringJoin[StringJoin[title, "\n"], Read[datafile, String, 
            RecordSeparators -> "\n"]]]]; SetStreamPosition[datafile, i]; 
      While[ToString[zt = Read[datafile, Real]] != "EndOfFile", 
       AppendTo[z, zt]]; Close[datafile]; 
      If[ !sQ, If[ !NoTitle, Print[StringDrop[title, 1]]]; 
        Print["Number of Values Input = ", Length[z]]]; z];
  
WriteData[(filename_)?StringQ, (x_)?NumberListQ, (opts___)?OptionQ] := 
 Module[{x2 = N[x], ti, ismatrix = MatrixQ[x], pr, istitle}, 
 ti = Title /. {opts} /. Options[WriteData]; 
 istitle = StringQ[ti]; 
 ti = If[istitle, StringReplace[StringJoin["#", ti], "\n" -> "\n#"], ti]; 
 OpenWrite[filename]; 
 x2 = Chop[x,0.00001];
 x2 = If[ismatrix, x2, Flatten[x2]]; 
 x2 = StringReplace[StringReplace[StringReplace[StringDrop[ToString[x2], 
           -2], "}," -> "\n"], "{" -> ""], " " -> ""]; 
 x2 = If[ismatrix, StringReplace[x2, "," -> "\t"], 
        StringReplace[x2, "," -> "\n"]]; 
 x2 = If[istitle, StringJoin[ti, "\n", x2], x2]; 
 WriteString[filename, x2]; 
 Close[filename]
];



End[];
EndPackage[];
