(******************************************************************* This file was generated automatically by the Mathematica front end. It contains Initialization cells from a Notebook file, which typically will have the same name as this file except ending in ".nb" instead of ".m". This file is intended to be loaded into the Mathematica kernel using the package loading commands Get or Needs. Doing so is equivalent to using the Evaluate Initialization Cells menu command in the front end. DO NOT EDIT THIS FILE. This entire file is regenerated automatically each time the parent Notebook file is saved in the Mathematica front end. Any changes you make to this file will be overwritten. ***********************************************************************) BeginPackage["PMSI`FileInputDataDT`",{"PMSI`ReadDateTime`"}] FileInputDataDT::usage = "FileInputDataDT.m is a package of functions related to reading text and csv files, assigning pattern names and providing data consructs based on \"word\", \"number\", \"date\", \"serial\", and \"currency\" formats. In addition, a number of functions are available for date and data manipulation. Datatype date\[EmptyVerySmallSquare]time is used from package PMSI`ReadDateTime`." showCurrency::usage = "showCurrency[nr_?NumberQ] pads a number to at most 10 digits with a comma every three digits and inserts a dollar sign." showArow::usage = "showArow[row_,rowpatt_] provides formatting for dates and currency when a rowpatt is provided such as {word,date,currency}." displayDates::usage = "displayDates[datalist_List,rowForm_] formats a listing of data records using TableForm and showArow." showTHErow::usage = "showTHErow[row_,rowpatt_] makes a string from a formatted date for exporting as a true text value." exportDates::usage = "exportDates[datalist_List,rowForm_] formats a datalisting for export with dates as true text value not wrapped by showDates." nameContains::usage = "[sName_String,xString_String] provides true or false if string sName contains the target string xString." isEmpty::usage = "isEmpty[x_List] returns True if x={} otherwise False." isNotEmpty::usage = "isNotEmpty[x_List] returns False if x={} otherwise True." isBlank::usage = "isBlank[x_String] returns True if x=\"\" otherwise False." isSpace::usage = "isSpace[x_String] returns True if x=\" \" otherwise False." Date\[EmptyVerySmallSquare]TimeQ::usage= "Date\[EmptyVerySmallSquare]TimeQ[expr] gives True if the head of x is date\[EmptyVerySmallSquare]time and False otherwise." sortBy::usage = "sortBy[list_,f_] sorts the list according to the ordering function f. So that on using pure functions for f any slot construct will work, for instance, #\[LeftDoubleBracket]1\[RightDoubleBracket]& or Mean[#]&." unPadSerial::usage = "unPadSerial[x_String] deletes leading zeroes from the string. This is used to provide a base number for serial numbers used for comparison when the padded zeroes are not consistent." readDataFile::usage = "readDataFile[fileName,options] reads a file of data in textfile or CSV format; (tab or comma delimited formats). fileName is the full string specifying file location. readDataFile returns the following list {titleLine,pattern variables,dataFile,fileName abbreviation,column formats,variable names}. Example input cell is: {titleLine,pattVars,dataFile,fnAbbr,rowForm,vars} = readFile[ fileFolder<>filename.txt,line2format\[Rule]True,abbr\[Rule]test]." line2format::usage = "line2format is an option indicating that line 2 of the input data file contains data type specifications for the variables contained in the heading row (line 1). Useable formats are word, date, number, serial, and currency. The default option is True." abbr::usage = "abbr is an option specifying a shorthand name associated with the input data file.The default option is an Input dialog box requesting abbr." inputDate::usage = "inputDate is an option specifying the date (string) to be used for blank or non-valid entries in columns identified as having a date format. The default option is the FileDate of fileName" setVarsToRules::usage= "setVarsToRules[fnAbbr,variableNames] assigns Symbol[variableName] list to the pattern elements for use in replacements of data records by the element." Begin["`Private`"] {$Context,$ContextPath} numberformatpatterns={"N*","n*"}; dateformatpatterns={"D*","d*"}; serialformatpatterns={"S*","s*"}; currencyformatpatterns={"C*","c*"}; comma=","; tab="\t"; cr="\r"; newline="\n"; entryToNumber[v_]:= Module[{trueStr=StringQ[v],xStrNumber, replaceSet={"$"\[Rule]""," "\[Rule]"",","\[Rule]"","\""\[Rule]""}}, xStrNumber=If[trueStr,StringReplace[v,replaceSet],v]; Which[ SameQ[xStrNumber,Null]||Not[trueStr]|| isBlank[xStrNumber],\[ImaginaryI], trueStr&&DigitQ[ StringReplace[xStrNumber,{"."\[Rule]"","-"\[Rule]""}]], ToExpression[xStrNumber],True,\[ImaginaryI]]]; dateconvert[x_String,usefileDate_date\[EmptyVerySmallSquare]time]:= With[{strX=StringReplace[x,{"\""\[Rule]"","\\"\[Rule]""}]}, Which[isBlank[strX]||isSpace[strX]|| Not[valid\[EmptyVerySmallSquare]dateQ[strX]],usefileDate,True, make\[Ellipsis]date\[EmptyVerySmallSquare]time[ deconstructDate[strX]]]]; isNumberIndicator[x_]:= Block[{StringMatchQ,Or}, Thread[StringMatchQ[x,Or@@numberformatpatterns],Or]]; isDateIndicator[x_]:= Block[{StringMatchQ,Or}, Thread[StringMatchQ[x,Or@@dateformatpatterns],Or]]; isSerialIndicator[x_]:= Block[{StringMatchQ,Or}, Thread[StringMatchQ[x,Or@@serialformatpatterns],Or]]; isCurrencyIndicator[x_]:= Block[{StringMatchQ,Or}, Thread[StringMatchQ[x,Or@@currencyformatpatterns],Or]]; deleteLead0s[{first_,rest__}/;SameQ[first,"0"]]:=deleteLead0s[{rest}]; deleteLead0s[l_List]:=l; fixedLength[list_List,length_Integer,blankList_List]:= With[{lsize=Length[list]},Which[lsize\[Equal]length,list, lsize>length,Take[list,length], True,Join[list,Take[blankList,length-lsize]]]]; textInFormat[s_String]:= Which[isNumberIndicator[s],"number",isDateIndicator[s],"date", isSerialIndicator[s],"serial",isCurrencyIndicator[s],"currency",True, "word"]; textToFormat[x_List,toFormat_List, usefileDate_date\[EmptyVerySmallSquare]time]:= MapThread[{#1,#2}&,{toFormat,x}]/. {form_,value_}/; form\[Equal]"word"\[RuleDelayed] value/. {form_,value_}/; form\[Equal]"number"\[RuleDelayed] entryToNumber[value]/.{form_,value_}/; form\[Equal]"date"\[RuleDelayed] dateconvert[value,usefileDate]/.{form_,value_}/; form\[Equal]"serial"\[RuleDelayed] unPadSerial[value]/.{form_,value_}/; form\[Equal]"currency"\[RuleDelayed]entryToNumber[value]; assignTest[format_String]:= Switch[format, "word","_?StringQ", "number","_?NumberQ", "date","_?Date\[EmptyVerySmallSquare]TimeQ", "serial","_?StringQ", "currency","_?NumberQ", _,"_?StringQ"]; assignForm[fn_String,v_List,form_String]:= With[{symbolName=fn<>form},Clear[symbolName]; Switch[form, "rowForm",Evaluate[Symbol[symbolName]]=v, "vars",Evaluate[Symbol[symbolName]]=v, "patt",Evaluate[Symbol[symbolName]]= MapThread[ ToExpression["e"<>ToString[#1]<>assignTest[#2]]&,{Range[ Length[v]],v}]] ]; setRule[symbolName_,rule_]:= With[{},Clear[symbolName];Evaluate[Symbol[symbolName]]=rule]; eVars[n_Integer]:=Table[Symbol["e"<>ToString[i]],{i,1,n}]; showCurrency[nr_?NumberQ]:= PaddedForm[nr,{10,2},DigitBlock\[Rule]3,NumberSeparator\[Rule]",", NumberSigns\[Rule]{"-$","$"}, ExponentFunction\[Rule](If[-10<#<10,Null,#]&)]; showArow[row_,rowpatt_]:=MapThread[ Which[#2\[Equal]"date",ddMMMyy[#1], #2\[Equal]"currency",showCurrency[#1], True,#1]&,{row,rowpatt}]; displayDates[datalist_List,rowForm_]:= TableForm[Map[showArow[#,rowForm]&,datalist]]; showTHErow[row_,rowpatt_]:= MapThread[ If[#2\[Equal]"date",ToString[displayDTG[#1,"dd-MMM-yyyy"]],#1]&,{row, rowpatt}]; exportDates[datalist_List,rowForm_]:=Map[showTHErow[#,rowForm]&,datalist]; nameContains[sName_String,xString_String]:= StringPosition[sName,xString]\[NotEqual]{}; isEmpty[x_List]:=Flatten[x]\[Equal]{}; isNotEmpty[x_List]:=Flatten[x]\[NotEqual]{}; isBlank[x_String]:=x\[Equal]""; isSpace[x_String]:=x\[Equal]" "; sortBy[list_,f_]:=list[[Ordering[Map[f,list]]]]; unPadSerial[x_String]:=StringJoin[deleteLead0s[Characters[x]]]; Date\[EmptyVerySmallSquare]TimeQ[x_date\[EmptyVerySmallSquare]time]:=True; Date\[EmptyVerySmallSquare]TimeQ[x_]:=False; setVarsToRules[fnAbr_,varStr_]:= MapThread[ setRule[#1,Symbol[fnAbr<>"patt"]\[RuleDelayed]#2]&,{varStr, eVars[Length[varStr]]}]; Options[readDataFile]={line2format\[Rule]True,abbr\[Rule]"", inputDate\[Rule]""}; readDataFile[fileName_,opts___?OptionQ]:= Module[{fn,fnAbbr,returnedDate,result,nullresult,r,rHead,size,blankLine, readFormat,headingLine,header,formatLine,line2indication,inFormat,rList, separator,datestring,refFileDate}, nullresult={Null,Null,Null,Null,Null,Null}; {line2indication,fnAbbr, datestring}={line2format,abbr,inputDate}/.{opts}/.Options[ readDataFile]; returnedDate=FileDate[fileName]; result=Catch[ If[SameQ[returnedDate,$Failed],Throw[nullresult], (refFileDate= dateconvert[datestring, make\[Ellipsis]date\[EmptyVerySmallSquare]time[ returnedDate]]; fn=Switch[fnAbbr,"", ToString[Input[fileName<>" - \!\(\* StyleBox[\"abbreviation\",\nFontFamily->\"Century Schoolbook\",\nFontSlant->\"Italic\",\nFontColor->GrayLevel[0.100008],\nBackground->RGBColor[1, 1, 0]]\)?"]],_,fnAbbr]<>"\[EmptyVerySmallSquare]"; separator= Which[nameContains[fileName,".csv"],comma, nameContains[fileName,".mcsv"],comma, nameContains[fileName,".txt"],tab, True,Input["word separator is? comma=, or tab=\t? "]]; rseparator={cr,newline}; r=OpenRead[fileName]; rHead=Read[r,String]; rHead=StringReplace[ rHead,{" "\[Rule]"","_"\[Rule]"","["\[Rule]"","]"\[Rule]"", "("\[Rule]"",")"\[Rule]""}]; size=Length[Union[Flatten[StringPosition[rHead,separator]]]]+1; readFormat=Table[Word,{i,1,size}]; blankLine=Table["",{i,1,size}]; If[line2indication,(formatLine= Read[r,readFormat,WordSeparators\[Rule]separator, RecordSeparators\[Rule]rseparator,NullWords\[Rule]True]; inFormat=Map[textInFormat[#]&,formatLine]; Skip[r,Word,WordSeparators\[Rule]separator, RecordSeparators\[Rule]rseparator,NullWords\[Rule]True]), inFormat=Table["word",{i,1,size}]]; rList=ReadList[r,Word,WordSeparators\[Rule]separator, RecordSeparators\[Rule]rseparator,NullWords\[Rule]True, RecordLists\[Rule]True]; rList=Map[fixedLength[#,size,blankLine]&,rList]; rList=If[line2indication, Map[textToFormat[#,inFormat,refFileDate]&,rList],rList]; headingLine= Read[StringToStream[rHead],readFormat, WordSeparators\[Rule]separator,NullWords\[Rule]True]; header=Map[StringInsert[#,fn,1]&,headingLine]; rowFormat=assignForm[fn,inFormat,"rowForm"]; pattFormat=assignForm[fn,inFormat,"patt"]; varsFormat=assignForm[fn,header,"vars"]; Close[r]; Print[headingLine]; Print[fn<>"rowForm =",rowFormat]; Print[fn<>"patt =",pattFormat]; Print[fn<>"vars =",varsFormat]; {headingLine,pattFormat,rList,fn,rowFormat,varsFormat})]]; If[SameQ[result,nullresult], (Print[ StyleForm["No such file ",FontFamily\[Rule]"Century Schoolbook", FontWeight\[Rule]"Bold",FontColor\[Rule]Red], StyleForm[fileName,FontFamily\[Rule]"Courier", FontSlant\[Rule]"Italic"]]; nullresult), result] ]; End[ ] EndPackage[ ]