package cquest.rwfile; //------------------------------------------------------------------------ //|Date |Author |Method/Description //------------------------------------------------------------------------ //------------------------------------------------------------------------ /** this class is used to save a FID in a *.mrui format which is the clean version of *.dat, i.e : it's platform-independent. @author an @version 1*/ import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.DecimalFormat; import java.util.Vector; import cquest.Data; import cquest.FID; import cquest.rwfile.filerror; public class FileManagementWrite { //------------------------------------------------------------------------ // Variables //------------------------------------------------------------------------ static int REAL = 0; static int IMAG = 1; final int ABS = 2; final int OK = 0; final int CANCEL = 1; static int SIGNAL = 0; private File dataFile; private FileOutputStream dataStream; private DataOutputStream dataFilter; static byte[] val4B81; static byte[] val8180; static byte[] val8081; static byte[] val8181; static { val4B81 = new byte[2]; val4B81[0] = (byte)0x4b; val4B81[1] = (byte)0x81; val8180 = new byte[2]; val8180[0] = (byte)0x81; val8180[1] = (byte)0x80; val8081 = new byte[2]; val8081[0] = (byte)0x80; val8081[1] = (byte)0x81; val8181 = new byte[2]; val8181[0] = (byte)0x81; val8181[1] = (byte)0x81; } //------------------------------------------------------------------------ //constructor String fileToOpen //------------------------------------------------------------------------ public FileManagementWrite(String fileToSave) throws IOException { this.dataFile = new File(fileToSave); // creation of the stream dataStream = new FileOutputStream(dataFile); // creation of the filter dataFilter = new DataOutputStream(dataStream); } //------------------------------------------------------------------------ //methods //------------------------------------------------------------------------ //------------------------------------------------------------------------ // SaveMru(Data DataToSave,String BaseName) //------------------------------------------------------------------------ //this method save all the FID in the data object as a binary compliant with the mrui data format /* static public synchronized void SaveMru(Mrui mrui, File BaseName) throws IOException { Data DataToSave = mrui.data; // we retrieve the information in the header Vector header = DataToSave.getHeader(); double stepTime = ((Double)header.elementAt(0)).doubleValue(); double B0 = ((Double)header.elementAt(1)).doubleValue(); double transmiterFreq = ((Double)header.elementAt(2)).doubleValue(); double nucleus = ((Double)header.elementAt(3)).doubleValue(); double hzref = ((Double)header.elementAt(5)).doubleValue(); double ppmref = ((Double)header.elementAt(6)).doubleValue(); double ph0 = DataToSave.zeroOrderPhase; double beginTime = DataToSave.beginTime; double nop = (double)DataToSave.getOriginalLength(); DecimalFormat extensionNumberFormat = new DecimalFormat("0000"); String s = BaseName.getName(); int i = s.lastIndexOf('.'); String withoutext; if ((i < s.length()) && (i > 0)) { withoutext = s.substring(0, i); } else { withoutext = s; } mrui.data.setSaveNameOfFile(withoutext); String tempName; if (mrui.preferences.saveDataAsSeparateFiles) { for (int count = 0; count < DataToSave.getSignalsNb(); count++) { if (DataToSave.getSignalsNb() > 1) tempName = withoutext + extensionNumberFormat.format(count) + ".mrui"; else tempName = withoutext + ".mrui"; FileManagementWrite tempFile = new FileManagementWrite((new File(BaseName.getParent(), tempName)).getAbsolutePath()); System.out.println( "Saving frame " + (count + 1) + " of " + DataToSave.getSignalsNb() + " to file " + tempName + "."); //the first double specifies the type of data : // 0 for a fid // 1 for a simulated Spectrum // 2 for a simulated Fid // 3 for a peak table // here we write FID so tempFile.WriteADouble(SIGNAL); // 0 tempFile.WriteADouble(nop); // 1 tempFile.WriteADouble(stepTime); // 2 tempFile.WriteADouble(beginTime); // 3 tempFile.WriteADouble(ph0); // 4 tempFile.WriteADouble(transmiterFreq); // 5 tempFile.WriteADouble(B0); // 6 tempFile.WriteADouble(nucleus); // 7 tempFile.WriteADouble(hzref); // 8 tempFile.WriteADouble(ppmref); // 9 double echoes = 0; if (DataToSave.areEchoes) echoes = 1; tempFile.WriteADouble(echoes); // 10 System.out.println("echoes=" + echoes); /*double echoes= 0; if(DataToSave.areEchoes) tempFile.WriteADouble(DataToSave.leftPointsEcho);// 10 else tempFile.WriteADouble(100); // 10 */ /* tempFile.WriteADouble(DataToSave.apodizeView); // 11 tempFile.WriteADouble(DataToSave.zeroFillingView); // 12 tempFile.WriteADouble(DataToSave.getSignalsNb()); // 13 System.out.println("DataToSave.leftPointsEcho " + DataToSave.leftPointsEcho); if (DataToSave.areEchoes) tempFile.WriteADouble(DataToSave.leftPointsEcho); // 14 else tempFile.WriteADouble(0); //14 for (int c = 0; c < (64 - 15); c++) { //this is to create empty space in the header so we have 64 double total tempFile.WriteADouble(0.0); // ->64 } // The signal is written double signal[][] = DataToSave.getSignal(count); for (int j = 0; j < nop; j++) { tempFile.WriteADouble(signal[REAL][j]); tempFile.WriteADouble(signal[IMAG][j]); } try { // Additional information tempFile.WriteLine(DataToSave.nameOfPatient); tempFile.WriteLine(DataToSave.dateOfExperiment); tempFile.WriteLine(DataToSave.spectrometer); tempFile.WriteLine(DataToSave.additionalInformation); //Some Strings for future use tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); } catch (Exception e) { System.out.println(e); } } System.out.println("Mrui type saved in multiple files."); } else { // Data to be saved in one file tempName = withoutext + ".mrui"; FileManagementWrite tempFile = new FileManagementWrite((new File(BaseName.getParent(), tempName)).getAbsolutePath()); //the first double specifies the type of data : // 0 for a fid // 1 for a simulated Spectrum // 2 for a simulated Fid // 3 for a peak table // here we write FID so tempFile.WriteADouble(SIGNAL); // 0 //we write these values to the stream in the order specified //by the rdb bynary specification, but we write double instead of float tempFile.WriteADouble(nop); // 1 tempFile.WriteADouble(stepTime); // 2 tempFile.WriteADouble(beginTime); // 3 tempFile.WriteADouble(ph0); // 4 tempFile.WriteADouble(transmiterFreq); // 5 tempFile.WriteADouble(B0); // 6 tempFile.WriteADouble(nucleus); // 7 tempFile.WriteADouble(hzref); // 8 tempFile.WriteADouble(ppmref); // 9 double echoes = 0; if (DataToSave.areEchoes) echoes = 1; tempFile.WriteADouble(echoes); // 10 System.out.println("echoes=" + echoes); /*double echoes= 0; if(DataToSave.areEchoes) tempFile.WriteADouble(DataToSave.leftPointsEcho);// 10 else tempFile.WriteADouble(100); // 10 */ /* tempFile.WriteADouble(DataToSave.apodizeView); // 11 tempFile.WriteADouble(DataToSave.zeroFillingView); // 12 tempFile.WriteADouble(DataToSave.getSignalsNb()); // 13 System.out.println("DataToSave.leftPointsEcho " + DataToSave.leftPointsEcho); if (DataToSave.areEchoes) tempFile.WriteADouble(DataToSave.leftPointsEcho); // 14 else tempFile.WriteADouble(0); //14 for (int c = 0; c < (64 - 15); c++) { //this is to create empty space in the header so we have 64 double total tempFile.WriteADouble(0.0); // ->64 } // The signals are written for (int count = 0; count < DataToSave.getSignalsNb(); count++) { System.out.println( "Saving frame " + (count + 1) + " of " + DataToSave.getSignalsNb() + " to file " + tempName + " .\r"); double signal[][] = DataToSave.getSignal(count); for (int j = 0; j < nop; j++) { tempFile.WriteADouble(signal[REAL][j]); tempFile.WriteADouble(signal[IMAG][j]); } } try { // Additional information tempFile.WriteLine(DataToSave.nameOfPatient); tempFile.WriteLine(DataToSave.dateOfExperiment); tempFile.WriteLine(DataToSave.spectrometer); tempFile.WriteLine(DataToSave.additionalInformation); //Some Strings for future use tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); } catch (Exception e) { System.out.println(e); } System.out.println("Mrui type saved to " + tempName + "."); } }*/ //------------------------------------------------------------------------ // SaveMrsiResultsAsText(Data DataToSave, String BaseName) //------------------------------------------------------------------------ //this method save all the FID in the data object as a text file /*static public synchronized void SaveMrsiResultsAsText(Mrui mrui, File BaseName) throws IOException { SaveMrsiResultsAsText(mrui.data, BaseName); }*/ static public synchronized void SaveMrsiResultsAsText2(Data data, File BaseName,boolean[] forWarning,boolean echo) throws IOException { DecimalFormat formDouble = new DecimalFormat("0.0000E0"); DecimalFormat formInteger = new DecimalFormat("0000"); System.out.println("Save File: " + BaseName); FileManagementWrite tempFile = new FileManagementWrite(BaseName.getAbsolutePath()); String line = new String(); String name; Vector feat; Vector featerr; int numberOfPeaks = 0; for (int i = 0; i < data.getSignalsNb(); i++) { if (data.result.getNBPeakOfFIDAt(i) > numberOfPeaks) { numberOfPeaks = data.result.getNBPeakOfFIDAt(i); } } data.result.setNoiseLevel(1,(int) (data.getOriginalLength()*20/100)); for (int peak = 0; peak < numberOfPeaks; peak++) { try { tempFile.write("Metabolite:"); tempFile.writeTab(); name = data.result.getNameAt(peak); tempFile.write(name); tempFile.writeLineFeed(); tempFile.write("Pixel Position"); tempFile.writeTab(); tempFile.write("Amplitude"); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("SD"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("Phase"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("Damping"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); if(echo) { tempFile.write("DampingR"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); } tempFile.write("DFreq"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("Noise from LastPts"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("Estimated CRB"); tempFile.writeLineFeed(); } catch (Exception e) { } for (int j = 0; j < data.dataHeightForMrsi; j++) { for (int i = 0; i < data.dataWidthForMrsi; i++) { try { feat = data.result.getFeaturesAt(j * data.dataWidthForMrsi + i, peak); featerr = data.result.getFeaturesErrorsAt(j * data.dataWidthForMrsi + i, peak); tempFile.write(formInteger.format((double)i)); tempFile.writeTab(); tempFile.write(formInteger.format((double)j)); tempFile.writeTab(); tempFile.write( formDouble.format( ((Double) (data.result.getFeaturesAt(j * data.dataWidthForMrsi + i, peak).elementAt(0))) .doubleValue())); tempFile.writeTab(); tempFile.writeTab(); tempFile.write( formDouble.format( ((Double) ( data .result .getFeaturesErrorsAt(j * data.dataWidthForMrsi + i, peak) .elementAt(0))) .doubleValue())); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); double p= ((Double)feat.elementAt(4)).doubleValue()*180/Math.PI; tempFile.write(formDouble.format(p)); tempFile.writeTab(); tempFile.writeTab(); //damping right double t= ((Double)feat.elementAt(1)).doubleValue(); t=t*1000/data.stepTime; tempFile.write(formDouble.format(t)); tempFile.writeTab(); tempFile.writeTab(); //damping left if(echo){ t= ((Double)feat.elementAt(2)).doubleValue(); t=t*1000/data.stepTime; tempFile.write(formDouble.format(t)); tempFile.writeTab(); tempFile.writeTab(); } double f= ((Double)feat.elementAt(3)).doubleValue()*1000/data.stepTime ; tempFile.write(formDouble.format(f)); tempFile.writeTab(); tempFile.writeTab(); double noise =data.result.getNoiseLevel(j*data.dataWidthForMrsi+i); //double calcTime=data.result.getCalculationTime(j*data.dataWidthForMrsi+i); tempFile.write(formDouble.format(noise)); //tempFile.writeTab(); //tempFile.writeTab(); //data.result.setNoiseLevel(0,data.getSignalsNb()*20/100); //noise =data.result.getNoiseLevel(j*data.dataWidthForMrsi+i); //tempFile.write(formDouble.format(noise)); // retriev the number of points to calculate the noise residue: int NForNoise=data.result.getNPoints(); data.result.setNoiseLevel(j*data.dataWidthForMrsi+i,0,NForNoise); noise =data.result.getNoiseLevel(j*data.dataWidthForMrsi+i); tempFile.writeTab(); tempFile.writeTab(); double CRB=noise*((Double) ( data .result .getFeaturesErrorsAt(j * data.dataWidthForMrsi + i, peak) .elementAt(0))) .doubleValue(); //data.result.setNoiseLevel(0,data.getSignalsNb()*20/100); //noise =data.result.getNoiseLevel(j*data.dataWidthForMrsi+i); //tempFile.write(formDouble.format(noise)); tempFile.write(formDouble.format(CRB)); tempFile.writeLineFeed(); } catch (Exception e){ System.out.println ("Error"); System.out.println (e.getMessage()); } } } try { tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.writeLineFeed(); } catch (Exception e) { }; } try { for (int k=0;k numberOfPeaks) { numberOfPeaks = data.result.getNBPeakOfFIDAt(i); } } // retriev the number of points to calculate the noise residue: int NForNoise=data.result.getNPoints(); data.result.setNoiseLevel(0,NForNoise); for (int peak = 0; peak < numberOfPeaks; peak++) { try { tempFile.write("Metabolite:"); tempFile.writeTab(); name = data.result.getNameAt(peak); tempFile.write(name); tempFile.writeLineFeed(); tempFile.write("Pixel Position"); tempFile.writeTab(); tempFile.write("Amplitude"); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("SD"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("Phase"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("Damping"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("Noise from Residue"); tempFile.writeTab(); tempFile.writeTab(); tempFile.writeTab(); tempFile.write("Estimated CRB"); tempFile.writeLineFeed(); } catch (Exception e) { } for (int j = 0; j < data.dataHeightForMrsi; j++) { for (int i = 0; i < data.dataWidthForMrsi; i++) { try { feat = data.result.getFeaturesAt(j * data.dataWidthForMrsi + i, peak); featerr = data.result.getFeaturesErrorsAt(j * data.dataWidthForMrsi + i, peak); tempFile.write(formInteger.format((double)i)); tempFile.writeTab(); tempFile.write(formInteger.format((double)j)); tempFile.writeTab(); tempFile.write( formDouble.format( ((Double) (data.result.getFeaturesAt(j * data.dataWidthForMrsi + i, peak).elementAt(0))) .doubleValue())); tempFile.writeTab(); tempFile.writeTab(); tempFile.write( formDouble.format( ((Double) ( data .result .getFeaturesErrorsAt(j * data.dataWidthForMrsi + i, peak) .elementAt(0))) .doubleValue())); tempFile.writeTab(); //double t= ((Double)feat.elementAt(1)).doubleValue(); //double terr=((Double)featerr.elementAt(1)).doubleValue(); // t=t*1000/data.stepTime; /* terr=terr*1000/data.stepTime;*/ // tempFile.write(formDouble.format(t)); tempFile.writeTab(); // tempFile.write(formDouble.format(terr)); //tempFile.writeTab(); //double f= ((Double)feat.elementAt(3)).doubleValue()*1000/data.stepTime ; //double ferr= ((Double)featerr.elementAt(3)).doubleValue()*1000/data.stepTime ; /*if(f==0){ tempFile.write(formDouble.format(f)); tempFile.writeTab(); //tempFile.write(formDouble.format(ferr)); //tempFile.writeTab(); } else{ f=(f- data.result.referenceFrequency)*1e6/data.transmitterFreq; //ferr=(ferr- data.result.referenceFrequency)*1e6/data.transmitterFreq; tempFile.write(formDouble.format(f)); tempFile.writeTab(); //tempFile.write(formDouble.format(ferr)); //tempFile.writeTab(); }*/ tempFile.writeTab(); double p= ((Double)feat.elementAt(4)).doubleValue()*180/Math.PI; tempFile.write(formDouble.format(p)); tempFile.writeTab(); tempFile.writeTab(); double t= ((Double)feat.elementAt(1)).doubleValue(); t=t*1000/data.stepTime; tempFile.write(formDouble.format(t)); tempFile.writeTab(); tempFile.writeTab(); double noise =data.result.getNoiseLevel(j*data.dataWidthForMrsi+i); //double calcTime=data.result.getCalculationTime(j*data.dataWidthForMrsi+i); tempFile.write(formDouble.format(noise)); tempFile.writeTab(); tempFile.writeTab(); double CRB=noise*((Double) ( data .result .getFeaturesErrorsAt(j * data.dataWidthForMrsi + i, peak) .elementAt(0))) .doubleValue(); //data.result.setNoiseLevel(0,data.getSignalsNb()*20/100); //noise =data.result.getNoiseLevel(j*data.dataWidthForMrsi+i); //tempFile.write(formDouble.format(noise)); tempFile.write(formDouble.format(CRB)); tempFile.writeLineFeed(); } catch (Exception e){ System.out.println ("Error"); System.out.println (e.getMessage()); } } } try { tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.writeLineFeed(); } catch (Exception e) { }; } tempFile.dataStream.close(); } //------------------------------------------------------------------------ // SaveMruiAsText(Data DataToSave,String BaseName) //------------------------------------------------------------------------ //this method save all the FID in the data object as a text file /*static public synchronized void SaveMruiAsText(Mrui mrui, File BaseName) throws IOException { Data data = mrui.data; DecimalFormat form = new DecimalFormat("0.####E0"); DecimalFormat extensionNumberFormat = new DecimalFormat("0000"); form.setGroupingUsed(false); extensionNumberFormat.setGroupingUsed(false); Vector header = data.getHeader(); double stepTime = ((Double)header.elementAt(0)).doubleValue(); double B0 = ((Double)header.elementAt(1)).doubleValue(); double transmiterFreq = ((Double)header.elementAt(2)).doubleValue(); double nucleus = ((Double)header.elementAt(3)).doubleValue(); double hzref = ((Double)header.elementAt(5)).doubleValue(); double ppmref = ((Double)header.elementAt(6)).doubleValue(); double ph0 = data.zeroOrderPhase; double beginTime = data.beginTime; int nop = data.getOriginalLength(); String s = BaseName.getName(); int i = s.lastIndexOf('.'); String withoutext; if ((i < s.length()) && (i > 0)) { withoutext = s.substring(0, i); } else { withoutext = s; } mrui.data.setSaveNameOfFile(withoutext); String tempName; try { if (mrui.preferences.saveDataAsSeparateFiles) { for (int count = 0; count < data.getSignalsNb(); count++) { if (data.getSignalsNb() > 1) tempName = withoutext + extensionNumberFormat.format(count) + ".txt"; else tempName = withoutext + ".txt"; System.out.println("Saving frame " + (count + 1) + " of " + data.getSignalsNb() + " to " + tempName + ".\r"); FileManagementWrite tempFile = new FileManagementWrite((new File(BaseName.getParent(), tempName)).getAbsolutePath()); tempFile.write("jMRUI Data Textfile"); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("Filename: " + tempName); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("PointsInDataset: " + trim("" + data.getOriginalLength())); tempFile.writeLineFeed(); tempFile.write("DatasetsInFile: 1"); tempFile.writeLineFeed(); tempFile.write("SamplingInterval: " + trim(form.format(data.stepTime))); tempFile.writeLineFeed(); tempFile.write("ZeroOrderPhase: " + trim(form.format(data.zeroOrderPhase))); tempFile.writeLineFeed(); tempFile.write("BeginTime: " + trim(form.format(data.beginTime))); tempFile.writeLineFeed(); tempFile.write("TransmitterFrequency: " + trim(form.format(data.transmitterFreq))); tempFile.writeLineFeed(); tempFile.write("MagneticField: " + trim(form.format(data.B0))); tempFile.writeLineFeed(); tempFile.write("TypeOfNucleus: " + trim(form.format(data.nucleus))); tempFile.writeLineFeed(); tempFile.write("NameOfPatient: " + data.nameOfPatient); tempFile.writeLineFeed(); tempFile.write("DateOfExperiment: " + data.dateOfExperiment); tempFile.writeLineFeed(); tempFile.write("Spectrometer: " + data.spectrometer); tempFile.writeLineFeed(); tempFile.write("AdditionalInfo: " + data.additionalInformation); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("Signal and FFT"); tempFile.writeLineFeed(); tempFile.write("sig(real)"); tempFile.writeTab(); tempFile.write("sig(imag)"); tempFile.writeTab(); tempFile.write("fft(real)"); tempFile.writeTab(); tempFile.write("fft(imag)"); tempFile.writeLineFeed(); tempFile.write("Signal 1 out of 1 in file"); tempFile.writeLineFeed(); double[][] sig = data.getSignal(count); double[][] fft = data.getFFT(count); for (int j = 0; j < data.getOriginalLength(); j++) { tempFile.write(form.format(sig[REAL][j])); tempFile.writeTab(); tempFile.write(form.format(sig[IMAG][j])); tempFile.writeTab(); tempFile.write(form.format(fft[REAL][j])); tempFile.writeTab(); tempFile.write(form.format(fft[IMAG][j])); tempFile.writeLineFeed(); } tempFile.dataStream.close(); } System.out.println("Data saved in multiple textfiles."); } else { tempName = withoutext + ".txt"; FileManagementWrite tempFile = new FileManagementWrite((new File(BaseName.getParent(), tempName)).getAbsolutePath()); tempFile.write("jMRUI Data Textfile"); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("Filename: " + tempName); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("PointsInDataset: " + trim("" + data.getOriginalLength())); tempFile.writeLineFeed(); tempFile.write("DatasetsInFile: " + trim("" + data.getSignalsNb())); tempFile.writeLineFeed(); tempFile.write("SamplingInterval: " + trim(form.format(data.stepTime))); tempFile.writeLineFeed(); tempFile.write("ZeroOrderPhase: " + trim(form.format(data.zeroOrderPhase))); tempFile.writeLineFeed(); tempFile.write("BeginTime: " + trim(form.format(data.beginTime))); tempFile.writeLineFeed(); tempFile.write("TransmitterFrequency: " + trim(form.format(data.transmitterFreq))); tempFile.writeLineFeed(); tempFile.write("MagneticField: " + trim(form.format(data.B0))); tempFile.writeLineFeed(); tempFile.write("TypeOfNucleus: " + trim(form.format(data.nucleus))); tempFile.writeLineFeed(); tempFile.write("NameOfPatient: " + data.nameOfPatient); tempFile.writeLineFeed(); tempFile.write("DateOfExperiment: " + data.dateOfExperiment); tempFile.writeLineFeed(); tempFile.write("Spectrometer: " + data.spectrometer); tempFile.writeLineFeed(); tempFile.write("AdditionalInfo: " + data.additionalInformation); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("Signal and FFT"); tempFile.writeLineFeed(); tempFile.write("sig(real)"); tempFile.writeTab(); tempFile.write("sig(imag)"); tempFile.writeTab(); tempFile.write("fft(real)"); tempFile.writeTab(); tempFile.write("fft(imag)"); tempFile.writeLineFeed(); for (int count = 0; count < data.getSignalsNb(); count++) { System.out.println("Saving frame " + (count + 1) + " of " + data.getSignalsNb() + " to " + tempName + ".\r"); double[][] sig = data.getSignal(count); double[][] fft = data.getFFT(count); tempFile.write("Signal " + (count + 1) + " out of " + data.getSignalsNb() + " in file"); tempFile.writeLineFeed(); for (int j = 0; j < data.getOriginalLength(); j++) { tempFile.write(form.format(sig[REAL][j])); tempFile.writeTab(); tempFile.write(form.format(sig[IMAG][j])); tempFile.writeTab(); tempFile.write(form.format(fft[REAL][j])); tempFile.writeTab(); tempFile.write(form.format(fft[IMAG][j])); tempFile.writeLineFeed(); } } tempFile.dataStream.close(); System.out.println("Data saved as textfile in " + tempName + "."); } } catch (Exception e) { System.out.println(e); } }*/ //------------------------------------------------------------------------ // SaveMru(Simulation DataToSave,String BaseName) //------------------------------------------------------------------------ //this method save all the simulations Spectrum in the mrui file /*static public synchronized void SaveMru(Simulation DataToSave, File BaseName, boolean signal, boolean peak) throws IOException { if (!peak) { // The signal is to be saved as .mrui SaveMru(DataToSave.mrui, BaseName); } else { // The peaks are saved double[][] peakTable = DataToSave.getPeaks(); // we get the number of compound : double nop = (double)DataToSave.getNumberOfPoints(); // in this part we save the peak instead of the signal // so we write almost the same header but after we write the peaks instead // of the signal double stepTime = DataToSave.mrui.data.stepTime; double B0 = 0.0; double transmiterFreq = 63861140; double nucleus = SIMULATION; double ph0 = DataToSave.mrui.data.zeroOrderPhase; double beginTime = DataToSave.mrui.data.beginTime; double noise = DataToSave.getSignalToNoiseRatio(); double numberOfCompounds = peakTable.length; String s = BaseName.getName(); int i = s.lastIndexOf('.'); String withoutext; if ((i < s.length()) && (i > 0)) withoutext = s.substring(0, i) + ".peaks"; else withoutext = s + ".peaks"; FileManagementWrite tempFile = new FileManagementWrite((new File(BaseName.getParent(), withoutext)).getAbsolutePath()); //the first double specifies the type of data : // 0 for a fid // 1 for a simulated Spectrum // 2 for a simulated Fid // 3 for a peak table // here we write peak so tempFile.WriteADouble(PEAKTABLE); tempFile.WriteADouble(nop); tempFile.WriteADouble(stepTime); tempFile.WriteADouble(beginTime); tempFile.WriteADouble(ph0); tempFile.WriteADouble(transmiterFreq); tempFile.WriteADouble(B0); tempFile.WriteADouble(nucleus); tempFile.WriteADouble(noise * 100); tempFile.WriteADouble(numberOfCompounds); for (int c = 0; c < 64 - 10; c++) { //this is to create empty space in the header so we have 64 double total //in the header tempFile.WriteADouble(0.0); } // so now we just write the peak table for (int counter = 0; counter < numberOfCompounds; counter++) for (int counte = 0; counte < 8; counte++) tempFile.WriteADouble(peakTable[counter][counte]); } }*/ //------------------------------------------------------------------------ // SaveJcamp() //------------------------------------------------------------------------ //this method save all the FID in the data object as a binary compliant with //the mrui data format /*static public synchronized void SaveJcamp(Data DataToSave, String BaseName, String title) throws IOException { try { // we retrieve the informations of the header Vector header = DataToSave.getHeader(); //the steptime has to converted in second double stepTimeD = (((Double)header.elementAt(0)).doubleValue()) / 1000; String B0 = ((Double)header.elementAt(1)).toString(); String transmiterFreq = ((Double)header.elementAt(2)).toString(); String nucleus = (String)header.elementAt(3); for (int count = 0; count < DataToSave.getSignalsNb(); count++) { String tempName = BaseName + count + ".jdx"; FID tempFid = DataToSave.getFIDAt(count); FileManagementWrite tempFile = new FileManagementWrite(tempName); String ph0 = new Double(DataToSave.zeroOrderPhase).toString(); //the beginTime has to converted in second double beginTimeD = (DataToSave.beginTime) / 1000; int nopI = DataToSave.getOriginalLength(); String nop = new Integer(nopI).toString(); double signal[][] = DataToSave.getSignal(count); //we start to write the labels String label = "##TITLE= "; String entry = label + title; tempFile.WriteLine(entry); label = "##JCAMP-DX= "; entry = label + "5.0 $$ produced by Mrui 2001"; tempFile.WriteLine(entry); label = "##DATATYPE= "; entry = label + "NMR FID"; tempFile.WriteLine(entry); label = "##DATA CLASS= "; entry = label + "NTUPLES"; tempFile.WriteLine(entry); label = "##.OBSERVE FREQUENCY= "; entry = label + transmiterFreq; tempFile.WriteLine(entry); label = "##.OBSERVE NUCLEUS= "; entry = label + nucleus; tempFile.WriteLine(entry); label = "##.DELAY= "; entry = label + beginTimeD; tempFile.WriteLine(entry); label = "##NTUPLES= "; entry = label + "NMR FID"; tempFile.WriteLine(entry); label = "##VAR_NAME= "; entry = label + " TIME , FID/REAL , FID/IMAG"; tempFile.WriteLine(entry); label = "##SYMBOL= "; entry = label + " X , R , I , N"; tempFile.WriteLine(entry); label = "##VAR_TYPE= "; entry = label + "= INDEPENDENT , DEPENDENT , DEPENDENT , PAGE"; tempFile.WriteLine(entry); label = "##VAR_FORM= "; entry = label + " AFFN , AFFN , AFFN , AFFN"; tempFile.WriteLine(entry); label = "##VAR_DIM= "; entry = label + nop + " , " + nop + " , " + nop + ", 2"; tempFile.WriteLine(entry); label = "##UNITS= "; entry = label + " SECONDS , ARBITRARY UNITS , ARBITRARY UNITS "; tempFile.WriteLine(entry); label = "##FIRST= "; String FirstX = (new Double(beginTimeD)).toString(); String FirstR = (new Double(signal[0][0])).toString(); String FirstI = (new Double(signal[1][0])).toString(); String FisrtN = "1"; entry = label + FirstX + " , " + FirstR + " , " + FirstI + " , " + FisrtN; tempFile.WriteLine(entry); label = "##LAST= "; double LastXD = beginTimeD + (nopI - 1) * stepTimeD; String LastX = (new Double(LastXD)).toString(); String LastR = (new Double(signal[0][nopI - 1])).toString(); String LastI = (new Double(signal[1][nopI - 1])).toString(); String LastN = "2"; entry = label + LastX + " , " + LastR + " , " + LastI + " , " + LastN; tempFile.WriteLine(entry); label = "##MIN= "; String MinX = "" + beginTimeD; // String MinR=((new Double(tempFid.GetMinRealS())).toString()); String MinR = "" + tempFid.GetMinRealS(); String MinI = "" + tempFid.GetMinImagS(); String MinN = "1"; entry = label + MinX + " , " + MinR + " , " + MinI + " , " + MinN; tempFile.WriteLine(entry); label = "##MAX= "; double MaxXD = beginTimeD + stepTimeD * (nopI - 1); String MaxX = (new Double(MaxXD)).toString(); String MaxR = "" + tempFid.GetMaxRealS(); String MaxI = "" + tempFid.GetMaxImagS(); String MaxN = "1"; entry = label + MaxX + " , " + MaxR + " , " + MaxI + " , " + MaxN; tempFile.WriteLine(entry); label = "##FACTOR= "; entry = label + " 1.000000 , 1.000000 , 1.000000 , 1"; tempFile.WriteLine(entry); label = "##PAGE= "; entry = label + "N=1"; tempFile.WriteLine(entry); label = "##DATA TABLE= "; entry = label + " (X++(R..R)), XYDATA"; tempFile.WriteLine(entry); double Xvalue = beginTimeD; entry = ""; // we then start to write the data first the real part for (int counter = 0; counter < nopI; counter++) { if (counter % 3 == 0) { entry += new Double(Xvalue).toString(); entry += " "; } Xvalue += stepTimeD; entry += new Double(signal[0][counter]).toString(); entry += " "; if (counter % 3 == 2) { tempFile.WriteLine(entry); entry = ""; } } // the last line tempFile.WriteLine(entry); entry = ""; label = "##PAGE= "; entry = label + "N=2"; tempFile.WriteLine(entry); label = "##DATA TABLE= "; entry = label + " (X++(I..I)), XYDATA"; tempFile.WriteLine(entry); Xvalue = beginTimeD; entry = ""; // then we write the complex part for (int counter = 0; counter < nopI; counter++) { if (counter % 3 == 0) { entry += new Double(Xvalue).toString(); entry += " "; } Xvalue += stepTimeD; entry += new Double(signal[1][counter]).toString(); entry += " "; if (counter % 3 == 2) { tempFile.WriteLine(entry); entry = ""; } } // the last line tempFile.WriteLine(entry); entry = ""; label = "##ENDNTUPLES= "; entry = label + "##NMR FID"; tempFile.WriteLine(entry); label = "##END= "; entry = label + ""; tempFile.WriteLine(entry); } } catch (filerror ex) { } }*/ //------------------------------------------------------------------------ // saveDatNT() //------------------------------------------------------------------------ //this method save all the FID in the data object as a binary file compliant with //the .dat format readable by a NT mrui97 version /*static public synchronized void saveDatNT(Data DataToSave, String BaseName, String title) throws IOException { // we retrieve the informations of the header Vector header = DataToSave.getHeader(); double stepTime = ((Double)header.elementAt(0)).doubleValue(); double B0 = ((Double)header.elementAt(1)).doubleValue(); double transmiterFreq = ((Double)header.elementAt(2)).doubleValue(); String nucleus = (String)header.elementAt(3); double nucleusSize = (double)nucleus.length(); for (int count = 0; count < DataToSave.getSignalsNb(); count++) { FID tempFid = DataToSave.getFIDAt(count); double MaxSignalVal; //we get the max value of the signal if (tempFid.GetMaxRealS() > tempFid.GetMaxImagS()) MaxSignalVal = tempFid.GetMaxRealS(); else MaxSignalVal = tempFid.GetMaxImagS(); // we create a .scl file try { String headerName = BaseName + count + ".scl"; FileManagementWrite tempFileHeader = new FileManagementWrite(headerName); tempFileHeader.WriteLine("sfrq : " + transmiterFreq); tempFileHeader.WriteLine("number of blocks : " + 1); tempFileHeader.WriteLine("true scaling fid : " + MaxSignalVal); tempFileHeader.WriteLine("norm : " + MaxSignalVal); } catch (filerror ex) { } String tempName = BaseName + count + ".dat"; FileManagementWrite tempFile = new FileManagementWrite(tempName); double ph0 = DataToSave.zeroOrderPhase; double beginTime = DataToSave.beginTime; double nop = DataToSave.getOriginalLength(); double signal[][] = DataToSave.getSignal(count); // We write these values to the stream in the order specified // by the rdb bynary specification, but we have to introduce on offset each // 128 bit tempFile.WriteByte(val4B81); int ByteFromOffset = 0; tempFile.WriteARFloat((float)nop * 2); ByteFromOffset += 4; tempFile.WriteARFloat((float)stepTime); ByteFromOffset += 4; tempFile.WriteARFloat((float)beginTime); ByteFromOffset += 4; tempFile.WriteARFloat((float)ph0); ByteFromOffset += 4; int counter = 0; for (int c = 0; c < (64 - 4); c++) { //this is to create empty space in the header so we have 64 double total //in the header tempFile.WriteARFloat((float)0.0); ByteFromOffset += 4; if ((ByteFromOffset == 128) && (c != 59)) { tempFile.WriteByte(val8180); ByteFromOffset = 0; } } tempFile.WriteByte(val8081); ByteFromOffset = 0; // we write the real part of the signal for (counter = 0; counter < java.lang.reflect.Array.getLength(signal[0]); counter++) { tempFile.WriteARFloat((float)signal[0][counter]); ByteFromOffset += 4; if (ByteFromOffset == 128) { tempFile.WriteByte(val8181); ByteFromOffset = 0; } } // we write the complex part of the signal for (counter = 0; counter < java.lang.reflect.Array.getLength(signal[1]); counter++) { tempFile.WriteARFloat((float)signal[1][counter]); ByteFromOffset += 4; if (ByteFromOffset == 128) { tempFile.WriteByte(val8181); ByteFromOffset = 0; } } // that's finished for this FID } }*/ //------------------------------------------------------------------------ // SaveMruFromRawSignal() //------------------------------------------------------------------------ /** This method saves raw information for ONE signal (FID) into a MRU file. It is used to save to a file an extracted signal or a generated signal @author AN */ static public synchronized void SaveMruFromRawSignal( String BaseName, double stepTime, double B0, double transmiterFreq, double nucleus, double hzref, double ppmref, double ph0, double beginTime, double nop, double signal[][]) throws IOException { String tempName = BaseName + ".mrui"; FileManagementWrite tempFile = new FileManagementWrite(tempName); // The first double specifies the type of data : // 0 for a fid // 1 for a simulated Spectrum // 2 for a simulated Fid // 3 for a peak table tempFile.WriteADouble(SIGNAL); // we write these values to the stream in the order specified // by the rdb bynary specification, but we write double instead of // float tempFile.WriteADouble(nop); tempFile.WriteADouble(stepTime); tempFile.WriteADouble(beginTime); tempFile.WriteADouble(ph0); tempFile.WriteADouble(transmiterFreq); tempFile.WriteADouble(B0); // now we use a double to write each characters of the nucleus // but first we write the number of characters in the nucleus tempFile.WriteADouble((double)nucleus); tempFile.WriteADouble(hzref); tempFile.WriteADouble(ppmref); tempFile.WriteADouble(0); //areEchoes tempFile.WriteADouble(0); //apodizeView tempFile.WriteADouble(0); //zeroFilling for (int c = 0; c < (64 - (13)); c++) { //this is to create empty space in the header so we have 64 double total in the header tempFile.WriteADouble(0.0); } // We write the signal for (int counter = 0; counter < java.lang.reflect.Array.getLength(signal[0]); counter++) { tempFile.WriteADouble(signal[REAL][counter]); tempFile.WriteADouble(signal[IMAG][counter]); } } //------------------------------------------------------------------------ // SaveMruFromRawSignalS() //------------------------------------------------------------------------ /** This method saves raw information for more signals (FID) into a MRU file. It is used to save to a file extracted signals or a generated signals @author AN */ static public synchronized void SaveMruFromRawSignalS( String BaseName, double stepTime, double B0, double transmiterFreq, double nucleus, double hzref, double ppmref, double ph0, double beginTime, double nop, double signals[][][]) throws IOException { String tempName = BaseName; if (!BaseName.endsWith(".mrui")) tempName = BaseName + ".mrui"; FileManagementWrite tempFile = new FileManagementWrite(tempName); // The first double specifies the type of data : // 0 for a fid // 1 for a simulated Spectrum // 2 for a simulated Fid // 3 for a peak table tempFile.WriteADouble(SIGNAL); // we write these values to the stream in the order specified // by the rdb bynary specification, but we write double instead of // float tempFile.WriteADouble(nop); tempFile.WriteADouble(stepTime); tempFile.WriteADouble(beginTime); tempFile.WriteADouble(ph0); tempFile.WriteADouble(transmiterFreq); tempFile.WriteADouble(B0); // now we use a double to write each characters of the nucleus // but first we write the number of characters in the nucleus tempFile.WriteADouble((double)nucleus); tempFile.WriteADouble(hzref); tempFile.WriteADouble(ppmref); tempFile.WriteADouble(0); //areEchoes tempFile.WriteADouble(0); //apodizeView tempFile.WriteADouble(0); //zeroFilling tempFile.WriteADouble(signals.length); //number of signals for (int c = 0; c < (64 - (14)); c++) { //this is to create empty space in the header so we have 64 double total in the header tempFile.WriteADouble(0.0); } // We write the signal for (int sig = 0; sig < signals.length; sig++){ for (int counter = 0; counter < java.lang.reflect.Array.getLength(signals[0][0]); counter++) { tempFile.WriteADouble(signals[sig][REAL][counter]); tempFile.WriteADouble(signals[sig][IMAG][counter]); } } try { // Additional information tempFile.WriteLine("nameOfPatient"); tempFile.WriteLine("dateOfExperiment"); tempFile.WriteLine("spectrometer"); tempFile.WriteLine("additionalInformation"); //Some Strings for future use tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); tempFile.WriteLine(""); } catch (Exception e) { System.out.println(e); } } //------------------------------------------------------------------------ // saveResultsAsText(mrui) //------------------------------------------------------------------------ /* static public synchronized void saveResultsAsText(Mrui mrui, String name, int index) { DecimalFormat form; if (mrui.preferences.saveResultTextDoublePrecision) { form = new DecimalFormat("0.################E0"); } else { form = new DecimalFormat("0.####E0"); } form.setGroupingUsed(false); double alfaInHz = 0; Vector feat; Vector errors; int numberOfSignals = mrui.data.getSignalsNb(); int numberOfPeaks = 0; for (int i = 0; i < numberOfSignals; i++) if (mrui.data.result.getNBPeakOfFIDAt(i) > numberOfPeaks) numberOfPeaks = mrui.data.result.getNBPeakOfFIDAt(i); double[][] frequency = new double[numberOfSignals][numberOfPeaks]; double[][] frequencySd = new double[numberOfSignals][numberOfPeaks]; double[][] amplitude = new double[numberOfSignals][numberOfPeaks]; double[][] amplitudeSd = new double[numberOfSignals][numberOfPeaks]; double[][] damping = new double[numberOfSignals][numberOfPeaks]; double[][] dampingSd = new double[numberOfSignals][numberOfPeaks]; double[][] dampingEcho = new double[numberOfSignals][numberOfPeaks]; double[][] dampingEchoSd = new double[numberOfSignals][numberOfPeaks]; double[][] phase = new double[numberOfSignals][numberOfPeaks]; double[][] phaseSd = new double[numberOfSignals][numberOfPeaks]; int from = 0; int to = numberOfSignals; if (index != -1) { from = index; to = index + 1; } double[] noise = new double[(to - from)]; for (int i = from; i < to; i++) { noise[(i - from)] = mrui.data.result.getNoiseLevel(i); for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { // vector: (amplitude, damping, damping echo, frequency, phase) or vector null if failure feat = mrui.data.result.getFeaturesAt(i, j); if (feat != (Vector) null) { // Bug fix by Yoeri, 4-7-2001 frequency[i][j] = ((Double)feat.elementAt(3)).doubleValue() * 1000 / mrui.data.stepTime - mrui.data.result.referenceFrequency; if (mrui.preferences.xAxisUnits == KHZUNIT) frequency[i][j] = frequency[i][j] / 1000; if (mrui.preferences.xAxisUnits == PPMUNIT) frequency[i][j] = frequency[i][j] * 1e6 / mrui.data.transmitterFreq; amplitude[i][j] = ((Double)feat.elementAt(0)).doubleValue(); double t = - ((Double)feat.elementAt(1)).doubleValue(); if (mrui.preferences.dampingUnits == ALFAHZUNIT) damping[i][j] = t * 1000 / mrui.data.stepTime; if (mrui.preferences.dampingUnits == ALFAHZPIUNIT) damping[i][j] = t * 1000 / (mrui.data.stepTime * Math.PI); if (mrui.preferences.dampingUnits == ALFAMSUNIT) { alfaInHz = t * 1000 / mrui.data.stepTime; damping[i][j] = mrui.data.stepTime / t; } if (mrui.data.areEchoes) { t = - ((Double)feat.elementAt(2)).doubleValue(); if (mrui.preferences.dampingUnits == ALFAHZUNIT) dampingEcho[i][j] = t * 1000 / mrui.data.stepTime; if (mrui.preferences.dampingUnits == ALFAHZPIUNIT) dampingEcho[i][j] = t * 1000 / (mrui.data.stepTime * Math.PI); if (mrui.preferences.dampingUnits == ALFAMSUNIT) { alfaInHz = t * 1000 / mrui.data.stepTime; dampingEcho[i][j] = mrui.data.stepTime / t; } } phase[i][j] = ((Double)feat.elementAt(4)).doubleValue() * 180 / Math.PI; } errors = mrui.data.result.getFeaturesErrorsAt(i, j); if (errors != (Vector) null) { frequencySd[i][j] = ((Double)errors.elementAt(3)).doubleValue() * 1000 / Mrui.mrui.data.stepTime * noise[(i - from)]; if (mrui.preferences.xAxisUnits == KHZUNIT) frequencySd[i][j] = frequencySd[i][j] / 1000 * noise[(i - from)]; if (mrui.preferences.xAxisUnits == PPMUNIT) frequencySd[i][j] = frequencySd[i][j] * 1e6 / Mrui.mrui.data.transmitterFreq * noise[(i - from)]; amplitudeSd[i][j] = ((Double)errors.elementAt(0)).doubleValue() * noise[(i - from)]; double t = - ((Double)errors.elementAt(1)).doubleValue(); if (mrui.preferences.dampingUnits == ALFAHZUNIT) dampingSd[i][j] = t * 1000 / mrui.data.stepTime * noise[(i - from)]; if (mrui.preferences.dampingUnits == ALFAHZPIUNIT) dampingSd[i][j] = t * 1000 / (mrui.data.stepTime * Math.PI) * noise[(i - from)]; if (mrui.preferences.dampingUnits == ALFAMSUNIT) { double alfaErrorInHz = t * 1000 / mrui.data.stepTime; dampingSd[i][j] = 1000 * alfaErrorInHz / (alfaInHz * (alfaInHz + alfaErrorInHz)) * noise[(i - from)]; } if (mrui.data.areEchoes) { t = - ((Double)errors.elementAt(1)).doubleValue(); if (mrui.preferences.dampingUnits == ALFAHZUNIT) dampingEchoSd[i][j] = t * 1000 / mrui.data.stepTime * noise[(i - from)]; if (mrui.preferences.dampingUnits == ALFAHZPIUNIT) dampingEchoSd[i][j] = t * 1000 / (mrui.data.stepTime * Math.PI) * noise[(i - from)]; if (mrui.preferences.dampingUnits == ALFAMSUNIT) { double alfaErrorInHz = t * 1000 / mrui.data.stepTime; dampingEchoSd[i][j] = 1000 * alfaErrorInHz / (alfaInHz * (alfaInHz + alfaErrorInHz)) * noise[(i - from)]; } } phaseSd[i][j] = ((Double)errors.elementAt(4)).doubleValue() * 180 / Math.PI * noise[(i - from)]; } } } try { FileManagementWrite tempFile = new FileManagementWrite(name); tempFile.write("jMRUI Results Textfile"); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("Filename: " + mrui.data.getFIDAt(0).name); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("Name of Patient: " + mrui.data.nameOfPatient); tempFile.writeLineFeed(); tempFile.write("Date of Experiment: " + mrui.data.dateOfExperiment); tempFile.writeLineFeed(); tempFile.write("Spectrometer: " + mrui.data.spectrometer); tempFile.writeLineFeed(); tempFile.write("Additional Information: " + mrui.data.additionalInformation); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write("Points"); tempFile.writeTab(); tempFile.write("Samp.Int."); tempFile.writeTab(); tempFile.write("ZeroOrder"); tempFile.writeTab(); tempFile.write("BeginTime"); tempFile.writeTab(); tempFile.write("Tra.Freq."); tempFile.writeTab(); tempFile.write("Magn.F."); tempFile.writeTab(); tempFile.write("Nucleus"); tempFile.writeLineFeed(); tempFile.write(trim("" + mrui.data.getOriginalLength())); tempFile.writeTab(); tempFile.write(trim(form.format(mrui.data.stepTime))); tempFile.writeTab(); tempFile.write(trim(form.format(mrui.data.zeroOrderPhase))); tempFile.writeTab(); tempFile.write(trim(form.format(mrui.data.beginTime))); tempFile.writeTab(); tempFile.write(trim(form.format(mrui.data.transmitterFreq))); tempFile.writeTab(); tempFile.write(trim(form.format(mrui.data.B0))); tempFile.writeTab(); tempFile.write(trim(form.format(mrui.data.nucleus))); tempFile.writeLineFeed(); tempFile.writeLineFeed(); tempFile.write(trim("Name of Algorithm: " + mrui.data.result.nameOfAlgorithm)); tempFile.writeLineFeed(); tempFile.writeLineFeed(); // FREQUENCIES if (mrui.preferences.xAxisUnits != HZUNIT) { if (mrui.preferences.xAxisUnits == KHZUNIT) tempFile.write("Frequencies (kHz)"); if (mrui.preferences.xAxisUnits == PPMUNIT) tempFile.write("Frequencies (ppm)"); } else tempFile.write("Frequencies (Hz)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(frequency[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } if (mrui.preferences.xAxisUnits != HZUNIT) { if (mrui.preferences.xAxisUnits == KHZUNIT) tempFile.write("Standard deviation of Frequencies (kHz)"); if (mrui.preferences.xAxisUnits == PPMUNIT) tempFile.write("Standard deviation of Frequencies (ppm)"); } else tempFile.write("Standard deviation of Frequencies (Hz)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(frequencySd[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } tempFile.writeLineFeed(); // AMPLITUDES tempFile.write("Amplitudes (-)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(amplitude[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } tempFile.write("Standard deviation of Amplitudes (-)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(amplitudeSd[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } tempFile.writeLineFeed(); // DAMPINGS if (mrui.preferences.dampingUnits == ALFAHZUNIT) tempFile.write("Dampings (Hz)"); if (mrui.preferences.dampingUnits == ALFAHZPIUNIT) tempFile.write("Linewidths (Hz)"); if (mrui.preferences.dampingUnits == ALFAMSUNIT) tempFile.write("T2 (ms)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(damping[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } if (mrui.preferences.dampingUnits == ALFAHZUNIT) tempFile.write("Standard deviation of Dampings (Hz)"); if (mrui.preferences.dampingUnits == ALFAHZPIUNIT) tempFile.write("Standard deviation of Linewidths (Hz)"); if (mrui.preferences.dampingUnits == ALFAMSUNIT) tempFile.write("Standard deviation of T2 (ms)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(dampingSd[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } tempFile.writeLineFeed(); if (mrui.data.areEchoes) { if (mrui.preferences.dampingUnits == ALFAHZUNIT) tempFile.write("Echo Dampings (Hz)"); if (mrui.preferences.dampingUnits == ALFAHZPIUNIT) tempFile.write("Echo Linewidths (Hz)"); if (mrui.preferences.dampingUnits == ALFAMSUNIT) tempFile.write("Echo T2 (ms)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(dampingEcho[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } if (mrui.preferences.dampingUnits == ALFAHZUNIT) tempFile.write("Standard deviation of Echo Dampings (Hz)"); if (mrui.preferences.dampingUnits == ALFAHZPIUNIT) tempFile.write("Standard deviation of Echo Linewidths (Hz)"); if (mrui.preferences.dampingUnits == ALFAMSUNIT) tempFile.write("Standard deviation of Echo T2 (ms)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(dampingEchoSd[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } tempFile.writeLineFeed(); } // AMPLITUDES tempFile.write("Phases (degrees)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(phase[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } tempFile.write("Standard deviation of Phases (degrees)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { for (int j = 0; j < mrui.data.result.getNBPeakOfFIDAt(i); j++) { tempFile.write(form.format(phaseSd[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } tempFile.writeLineFeed(); //NOISE tempFile.write("Noise : "); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { tempFile.write(form.format(noise[(i - from)])); tempFile.writeTab(); tempFile.writeLineFeed(); } // Writes pH to Text file. Done by Yoeri, 4-7-2001. // Now also writes [Mg2+] to Text file. Done by Yoeri, 31-7-2001. if (mrui.preferences.saveResultTextWithpHMg) { tempFile.write("pH"); tempFile.writeTab(); tempFile.write("Standard deviation of pH"); tempFile.writeTab(); tempFile.write("[Mg2+]"); tempFile.writeTab(); tempFile.write("Standard deviation of [Mg2+]"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { double pH = mrui.data.result.getPHAt(i); double pHError = mrui.data.result.getPHErrorAt(i); double Mg = mrui.data.result.getMgAt(i); double MgError = mrui.data.result.getMgErrorAt(i); if (Double.isNaN(pH)) tempFile.write("Not known"); else tempFile.write(form.format(pH)); tempFile.writeTab(); if (Double.isNaN(pHError)) tempFile.write("Not known"); else tempFile.write(form.format(pHError)); tempFile.writeTab(); if (Double.isNaN(Mg)) tempFile.write("Not known"); else tempFile.write(form.format(Mg)); tempFile.writeTab(); if (Double.isNaN(MgError)) tempFile.write("Not known"); else tempFile.write(form.format(MgError)); tempFile.writeLineFeed(); } tempFile.writeLineFeed(); } // Writes Singular Values to Text file. Done by Yoeri, 28-1-2002. if (mrui.data.result.checkForSingularValues() && mrui.preferences.saveResultTextWithSinvals) { tempFile.write("Singular Values"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { double[] daSinvals = mrui.data.result.getSingularValuesAt(i); if ((daSinvals != null) && (daSinvals.length > 0)) { for (int j = 0; j < daSinvals.length; j++) { if (Double.isNaN(daSinvals[j])) tempFile.write("Not known"); else tempFile.write(form.format(daSinvals[j])); tempFile.writeTab(); } } else tempFile.write("Not known"); tempFile.writeLineFeed(); } tempFile.writeLineFeed(); } // Original and estimated Datapoints (fid & fft) if (mrui.preferences.saveResultTextWithData) { tempFile.write("Original and estimated signal and FFT"); tempFile.writeLineFeed(); tempFile.write("sig(real)"); tempFile.writeTab(); tempFile.write("sig(imag)"); tempFile.writeTab(); tempFile.write("fft(real)"); tempFile.writeTab(); tempFile.write("fft(imag)"); tempFile.writeTab(); tempFile.write("sig(real)"); tempFile.writeTab(); tempFile.write("sig(imag)"); tempFile.writeTab(); tempFile.write("fft(real)"); tempFile.writeTab(); tempFile.write("fft(imag)"); tempFile.writeLineFeed(); for (int i = from; i < to; i++) { tempFile.write("Signal number: " + (i - from + 1)); tempFile.writeLineFeed(); double[][] sig = mrui.data.getSignal(i); double[][] fft = mrui.data.getFFT(i); double[][] sigEst = mrui.data.result.getSignal(i); double[][] fftEst = mrui.data.result.getFFT(i); for (int j = 0; j < mrui.data.getOriginalLength(); j++) { tempFile.write(form.format(sig[REAL][j])); tempFile.writeTab(); tempFile.write(form.format(sig[IMAG][j])); tempFile.writeTab(); tempFile.write(form.format(fft[REAL][j])); tempFile.writeTab(); tempFile.write(form.format(fft[IMAG][j])); tempFile.writeTab(); tempFile.write(form.format(sigEst[REAL][j])); tempFile.writeTab(); tempFile.write(form.format(sigEst[IMAG][j])); tempFile.writeTab(); tempFile.write(form.format(fftEst[REAL][j])); tempFile.writeTab(); tempFile.write(form.format(fftEst[IMAG][j])); tempFile.writeLineFeed(); } } } tempFile.dataStream.close(); } catch (Exception e) { System.out.println(e); } }*/ //-----------------------------------------------------savePlane static public synchronized void savePlane(String name, double[][] plane) { DecimalFormat form = new DecimalFormat("0.####E0"); form.setGroupingUsed(false); try { FileManagementWrite tempFile = new FileManagementWrite(name); for (int i = 0; i < plane.length; i++) { for (int j = 0; j < plane[0].length; j++) { tempFile.write(form.format(plane[i][j])); tempFile.writeTab(); } tempFile.writeLineFeed(); } tempFile.dataStream.close(); } catch (Exception e) { System.out.println(e); } } //-----------------------------------------------------Trim static private String trim(String str) { while (str.length() < 10) str += " "; return str; } //------------------------------------------------------------------------ //------------------------------------------------------------------------ //------------------------------------WriteLine() //this method takes a String argument and write it to the file, byte by byte terminating the line with \n private synchronized void WriteLine(String stringToWrite) throws IOException, filerror { if (stringToWrite.length() > 80) { throw new filerror("Line to long"); } // We write all the chars to the file for (int counter = 0; counter < stringToWrite.length(); counter++) { int tempInt = stringToWrite.charAt(counter); this.dataFilter.writeByte(tempInt); } // The lf character at the end of the line this.dataFilter.writeByte(0x0A); } //-------------------------------------write private synchronized void write(String stringToWrite) throws IOException, filerror { for (int counter = 0; counter < stringToWrite.length(); counter++) { int tempInt = stringToWrite.charAt(counter); this.dataFilter.writeByte(tempInt); } } //-------------------------------------writeLineFeed private synchronized void writeLineFeed() throws IOException, filerror { this.dataFilter.writeByte(0x0A); } //-------------------------------------writeTab private synchronized void writeTab() throws IOException, filerror { this.dataFilter.writeByte(0x09); } //-------------------------------------WriteByte(byte[]) /** This method write an array of bytes to the stream @author an @version 1*/ private synchronized void WriteByte(byte byteArray[]) throws IOException { this.dataFilter.write(byteArray); } //--------------------------------------WriteAFloat() private synchronized void WriteAFloat(float f) throws IOException { this.dataFilter.writeFloat(f); } //--------------------------------------WriteARFloat() private synchronized void WriteARFloat(float f) throws IOException { int tempInt = java.lang.Float.floatToIntBits(f); byte[] tempArray = new byte[4]; tempArray[0] = (byte) ((tempInt & 0xff000000) >> 24); tempArray[1] = (byte) ((tempInt & 0x00ff0000) >> 16); tempArray[2] = (byte) ((tempInt & 0x0000ff00) >> 8); tempArray[3] = (byte) (tempInt & 0x000000ff); for (int counter = 0; counter < 2; counter++) { byte temp = tempArray[counter]; tempArray[counter] = tempArray[3 - counter]; tempArray[3 - counter] = temp; } this.WriteByte(tempArray); ; } //---------------------------------------WriteAInt() /** this method Write a Int ( 32-bit) on the stream this is not used in theorie @author an @version 1*/ private synchronized void WriteAInt(int i) throws IOException { this.dataFilter.writeInt(i); } //---------------------------------------WriteADouble() private synchronized void WriteADouble(double d) throws IOException { this.dataFilter.writeDouble(d); } }