package cquest.rwfile; //------------------------------------------------------------------------ // fileTXT.java //------------------------------------------------------------------------ // //------------------------------------------------------------------------ //|Date |Author |Method/Description | //------------------------------------------------------------------------ /* To read .txt files. It is a tricky class because not only files with just two columns of textnumbers can be read, but also some information in a header and above all, data from multiple frames. The file may begin or end with rubbish, but somewhere the conversion will look for the first sequence of at least 4 lines with 2 numbers seperated by either spaces or tabs. behind these numbers, more rubbish is allowed to appear. .txt Files saved with jMRUI can also be loaded. Even the parameters are retrieved from such a file. Home made files can include the parameters as well if the syntax is: SamplingInterval: [double(ms)] ZeroOrderPhase: [double(deg)] BeginTime: [double(ms)] TransmitterFrequency: [double(Hz)] MagneticField: [double(T)] TypeOfNucleus: [string] NameOfPatient: [string] DateOfExperiment: [string] Spectrometer: [string] AdditionalInfo: [string] More than 1 signal can be loaded if the datapoints are preceded by: Signal [number] out of [number] in file example: xxxxxxx xx 897 654 xxx xxx x 64 xxx 654 xx 654 654e4 654E-0 25 654 -65454 654654 xxxxx 654e4 654e4 -654e87 +31E-65 6545 xxxxx4465 x654 +6546 ytgf 654 54 After conversion this will result in: Real: Imaginary: 654e4 654e0 25 654 -65454 65454 654e4 654e4 -654e87 31e-65 */ import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StreamTokenizer; import cquest.Data; import cquest.rwfile.filerror; public class fileTXT extends FileManagementRead { Data tempData; String fileName; // Name of the ASCII file to open static int MAXLENGTH = 65536; private int numberOfFrames = 1; private boolean endOfFileEncountered; //------------------------------------------------------------------------ //constructor //------------------------------------------------------------------------ public fileTXT(String fileToOpen) throws java.io.IOException { super(fileToOpen, false); fileName = fileToOpen; } //------------------------------------------------------------------------ // methods //------------------------------------------------------------------------ protected Data loadTXT() throws java.io.IOException, filerror { System.out.println("Loading txt file"); tempData = new Data(); // data structure to be returned endOfFileEncountered = false; // The following is arbitrary. tempData.stepTime = 1; tempData.nucleus = -1; tempData.B0 = 0; tempData.transmitterFreq = 63860000; tempData.areEchoes = false; // I don't know how big the file is going to be, so I reserve some space. double[][] signal = new double[2][MAXLENGTH]; FileInputStream inputFile = null; try { inputFile = new FileInputStream(fileName); // open the file } catch (Exception e) { System.out.println("Error !" + e.getMessage()); //System.out.println(e); } // create a new tokenizer Reader r = new BufferedReader(new InputStreamReader(inputFile)); StreamTokenizer st = new StreamTokenizer(r); // define tokenizer's rules st.resetSyntax(); st.wordChars(0, 255); st.whitespaceChars(0, 32); st.eolIsSignificant(true); st.slashSlashComments(true); st.slashStarComments(true); double[] valuesA = readLine(st); double[] valuesB = readLine(st); double[] valuesC = readLine(st); double[] valuesD = readLine(st); // Read lines until 4 consequtive lines with 2 numbers while ((valuesA == (double[]) null) || (valuesB == (double[]) null) || (valuesC == (double[]) null) || (valuesD == (double[]) null)) { if (endOfFileEncountered) { throw new filerror("No data could be found in the file."); } valuesA = valuesB; valuesB = valuesC; valuesC = valuesD; valuesD = readLine(st); } // Begin reading the first frame. signal[REAL][0] = valuesA[0]; signal[IMAG][0] = valuesA[1]; signal[REAL][1] = valuesB[0]; signal[IMAG][1] = valuesB[1]; signal[REAL][2] = valuesC[0]; signal[IMAG][2] = valuesC[1]; signal[REAL][3] = valuesD[0]; signal[IMAG][3] = valuesD[1]; int numberOfPoints = 4; while (((valuesA = readLine(st)) != (double[]) null) && (numberOfPoints != MAXLENGTH)) { signal[REAL][numberOfPoints] = valuesA[0]; signal[IMAG][numberOfPoints] = valuesA[1]; numberOfPoints++; } // until the first line not starting with 2 values // Put the points that have just been read in Data if (numberOfPoints == MAXLENGTH) System.out.println( "More points in file than the " + MAXLENGTH + " allowed. Will continue with " + MAXLENGTH + " points."); double[][] signalB = new double[2][numberOfPoints]; for (int i = 0; i < numberOfPoints; i++) { signalB[REAL][i] = signal[REAL][i]; signalB[IMAG][i] = signal[IMAG][i]; } tempData.addSignal(signalB); // If there is more than 1 frame, read these too. // The number of frames has to be defined in the header. for (int count = 1; count < numberOfFrames; count++) { System.out.println("Frame " + (count + 1) + " of " + numberOfFrames + " is being loaded.\r"); signalB = new double[2][numberOfPoints]; int i = 0; while ((valuesA = readLine(st)) != (double[]) null) { signalB[REAL][i] = valuesA[0]; signalB[IMAG][i] = valuesA[1]; i++; } tempData.addSignal(signalB); } System.out.println( "Algorithm was able to identify " + numberOfPoints + " points for " + numberOfFrames + " frame(s) in file."); // Both 0 if they haven't been found in the text files header //tempData.setPhases(); return tempData; } //------------------------------------------------------ private double[] readLine(StreamTokenizer st) { int tokenType; // Type of Token just read; double[] value = new double[2]; boolean convertible = true; String str1 = ""; String str2 = ""; // First word on the line try { for (tokenType = st.nextToken(); (tokenType != StreamTokenizer.TT_WORD) && (tokenType != StreamTokenizer.TT_EOF); tokenType = st.nextToken()) { }; // seek next Word token. endOfFileEncountered = (tokenType == StreamTokenizer.TT_EOF); str1 = (st.sval).replace(',', '.'); if (tokenType == StreamTokenizer.TT_WORD) value[0] = (Double.valueOf(str1)).doubleValue(); else convertible = false; } catch (Exception e) { convertible = false; } // Second word on the line try { for (tokenType = st.nextToken(); (tokenType != StreamTokenizer.TT_WORD) && (tokenType != StreamTokenizer.TT_EOF); tokenType = st.nextToken()) { }; // seek next Word token. endOfFileEncountered = (tokenType == StreamTokenizer.TT_EOF); str2 = (st.sval).replace(',', '.'); if (tokenType == StreamTokenizer.TT_WORD) value[1] = (Double.valueOf(str2)).doubleValue(); else convertible = false; } catch (Exception e) { convertible = false; } // Continue reading until a linefeed try { tokenType = StreamTokenizer.TT_WORD; while ((tokenType != StreamTokenizer.TT_EOF) && (tokenType != StreamTokenizer.TT_EOL)) { tokenType = st.nextToken(); } endOfFileEncountered = (tokenType == StreamTokenizer.TT_EOF); } catch (Exception e) { convertible = false; } if (!convertible) checkIfIsPartOfHeader(str1, str2); if (convertible) return value; else return (double[]) null; } //------------------------------------------------------ private void checkIfIsPartOfHeader(String str1, String str2) { if (str1.equalsIgnoreCase("DatasetsInFile:")) { numberOfFrames = (int)getValue(str2); } if (str1.equalsIgnoreCase("SamplingInterval:")) { tempData.stepTime = getValue(str2); } if (str1.equalsIgnoreCase("ZeroOrderPhase:")) { tempData.zeroOrderPhase = getValue(str2); } if (str1.equalsIgnoreCase("BeginTime:")) { tempData.beginTime = getValue(str2); } if (str1.equalsIgnoreCase("TransmitterFrequency:")) { tempData.transmitterFreq = getValue(str2); } if (str1.equalsIgnoreCase("MagneticField:")) { tempData.B0 = getValue(str2); } if (str1.equalsIgnoreCase("TypeOfNucleus:")) { // tempData.nucleus= getValue(str2); } if (str1.equalsIgnoreCase("NameOfPatient:")) { tempData.nameOfPatient = str2; } if (str1.equalsIgnoreCase("DateOfExperiment:")) { tempData.dateOfExperiment = str2; } if (str1.equalsIgnoreCase("Spectrometer:")) { tempData.spectrometer = str2; } if (str1.equalsIgnoreCase("AdditionalInfo:")) { tempData.additionalInformation = str2; } } //------------------------------------------------------ private double getValue(String str) { try { str = str.replace(',', '.'); return (Double.valueOf(str)).doubleValue(); } catch (Exception e) { return 0; } } }