Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

TransformDataFile.java

00001 /* 00002 * Created on Aug 2, 2004 00003 * 00004 * To change the template for this generated file go to 00005 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments 00006 */ 00007 package org.nees.rbnb; 00008 00009 import java.io.*; 00010 import java.util.StringTokenizer; 00011 import java.util.Vector; 00012 import java.util.Enumeration; 00013 00014 import org.nees.daq.ISOtoRbnbTime; 00015 00022 public class TransformDataFile { 00023 00024 private void computTransform() { 00025 00026 double d = 0.12192; 00027 double dx1=0.0, dx2=0.0, lx1=0.0, lx2=0.0; 00028 00029 for (int i = 0; i < values.length; i++) 00030 { 00031 dx1 = values[i][indexForDx1]; 00032 dx2 = values[i][indexForDx2]; 00033 lx1 = values[i][indexForLCx1]; 00034 lx2 = values[i][indexForLCx2]; 00035 values[i][indexForDisp] = - (dx1+dx2)/2.0; 00036 values[i][indexForRot] = (dx1/d - dx2/d)/2.0; 00037 values[i][indexForForce] = (lx1 + lx2)/2.0; 00038 values[i][indexForMoment] = lx1*d - lx2*d; 00039 } 00040 } 00041 00042 String inPath,outPath; 00043 00044 BufferedReader rd; 00045 00046 String[] names; 00047 double [][] values; 00048 String[] timeStamp; 00049 String sampleRateLine; 00050 00051 int indexForDx1 = -1; 00052 int indexForDx2 = -1; 00053 int indexForLCx1 = -1; 00054 int indexForLCx2 = -1; 00055 int indexForDisp = -1; 00056 int indexForRot = -1; 00057 int indexForForce = -1; 00058 int indexForMoment = -1; 00059 00060 public static void main(String[] args) { 00061 TransformDataFile t = new TransformDataFile(); 00062 t.doit(args[0], args[1]); 00063 } 00064 00065 public void doit(String inFilePath, String outFilePath) 00066 { 00067 inPath = inFilePath; 00068 outPath = outFilePath; 00069 00070 int count = probeFile(); 00071 00072 if (count > 0) 00073 { 00074 if (setIndexes()) 00075 { 00076 if (readFileToArrays(count)) 00077 { 00078 00079 System.out.println("size of values: " + values.length + "," + values[0].length); 00080 00081 computTransform(); 00082 00083 writeFile(); 00084 } 00085 } 00086 } 00087 } 00088 00092 private int probeFile() { 00093 00094 File probe = new File(inPath); 00095 String in = null; 00096 int lineCount = 0; 00097 00098 // does it exist and is it readable 00099 if (probe != null && probe.exists() && probe.canRead()) 00100 { 00101 try 00102 { 00103 rd = new BufferedReader(new FileReader(probe)); 00104 System.out.println("Sucessfully connected to " + inPath); 00105 } 00106 catch (IOException e) 00107 { 00108 e.printStackTrace(); 00109 return 0; 00110 } 00111 if (rd == null) 00112 { 00113 System.out.println("Failed to open file stream " + inPath); 00114 return 0; 00115 } 00116 } 00117 else // data not available 00118 { 00119 System.out.println("Data unavailable: path..."); 00120 if (probe == null) 00121 System.out.println("Could not open file."); 00122 else if (!probe.exists()) 00123 System.out.println("File does not exist"); 00124 else if (!probe.canRead()) 00125 System.out.println("File is unreadable"); 00126 return 0; 00127 } 00128 00129 boolean header = true; 00130 00131 try { 00132 // read one line ahead 00133 00134 while(header && ((in = rd.readLine()) != null)) 00135 { 00136 if (!in.startsWith("Time")) 00137 System.out.println("Skipping header line: " + in); 00138 else 00139 header = false; 00140 } 00141 00142 if (in == null) 00143 { 00144 System.out.println("Unexpected end of file."); 00145 } 00146 00147 // the first non-header line should be a list of channel names 00148 System.out.println("Channel list line: " + in); 00149 00150 // parse out the channel names and discard any path information 00151 // e.q. FromDaq/LoadCell_z will be sent as LoadCell_z 00152 StringTokenizer st = new StringTokenizer(in," "); // seperated by blanks 00153 Vector channelNames = new Vector(); 00154 00155 while (st.hasMoreTokens()) 00156 channelNames.add(st.nextToken()); 00157 00158 names = new String[channelNames.size() + 4]; 00159 00160 Enumeration en = channelNames.elements(); 00161 int pos,i = 0; 00162 String name; 00163 while (en.hasMoreElements()) 00164 { 00165 name = (String)en.nextElement(); 00166 pos = name.lastIndexOf('/'); 00167 if (pos > -1) name = name.substring(pos+1); 00168 System.out.println("name " + i + " = " + name); 00169 names[i] = name; 00170 i++; 00171 } 00172 names[i] = "Displasment_x"; i++; 00173 names[i] = "Rotation_x";i++; 00174 names[i] = "Force_x";i++; 00175 names[i] = "Moment_x"; 00176 00177 while((in = rd.readLine()) != null) 00178 { 00179 // just read and dump data to get line count; 00180 lineCount++; 00181 } 00182 00183 rd.close(); 00184 00185 } 00186 catch (Throwable t) { 00187 t.printStackTrace(); 00188 return 0; 00189 } 00190 00191 return lineCount; 00192 00193 } // probeFile 00194 00198 private boolean setIndexes() { 00199 // LVDT_x1 LVDT_x2 LoadCell_x1 LoadCell_x2 00200 // int indexForDx1 = -1; 00201 // int indexForDx2 = -1; 00202 // int indexForLCx1 = -1; 00203 // int indexForLCx2 = -1; 00204 // int indexForDisp = -1; 00205 // int indexForRot = -1; 00206 // int indexForForce = -1; 00207 // int indexForMoment = -1; 00208 00209 // NOTE: indexes offset by one because time is in names arrays 00210 // but not in valeus array 00211 for (int i = 0; i < names.length; i ++) 00212 { 00213 if (names[i].equals("LVDT_x1")) indexForDx1 = i-1; 00214 if (names[i].equals("LVDT_x2")) indexForDx2 = i-1; 00215 if (names[i].equals("LoadCell_x1")) indexForLCx1 = i-1; 00216 if (names[i].equals("LoadCell_x2")) indexForLCx2 = i-1; 00217 } 00218 00219 if (indexForDx1 < 0) return false; 00220 if (indexForDx2 < 0) return false; 00221 if (indexForLCx1 < 0) return false; 00222 if (indexForLCx2 < 0) return false; 00223 00224 // NOTE: indexes offset by one because time is in names arrays 00225 // but not in valeus array 00226 indexForDisp = names.length - 5; 00227 indexForRot = names.length - 4; 00228 indexForForce = names.length - 3; 00229 indexForMoment = names.length - 2; 00230 00231 System.out.println("indexForDx1 = " + indexForDx1); 00232 System.out.println("indexForDx2 = " + indexForDx2); 00233 System.out.println("indexForLCx1 = " + indexForLCx1); 00234 System.out.println("indexForLCx2 = " + indexForLCx2); 00235 System.out.println("indexForDisp = " + indexForDisp); 00236 System.out.println("indexForRot = " + indexForRot); 00237 System.out.println("indexForForce = " + indexForForce); 00238 System.out.println("indexForMoment = " + indexForMoment); 00239 00240 return true; 00241 } 00242 00246 private boolean readFileToArrays(int size) { 00247 int lineCount = 0; 00248 File probe = new File(inPath); 00249 String in = null; 00250 00251 // NOTE: indexes offset by one because time is not in valeus array 00252 values = new double[size][names.length-1]; 00253 timeStamp = new String[size]; 00254 00255 try 00256 { 00257 rd = new BufferedReader(new FileReader(probe)); 00258 System.out.println("Sucessfully opened " + inPath); 00259 } 00260 catch (IOException e) 00261 { 00262 e.printStackTrace(); 00263 return false; 00264 } 00265 if (rd == null) 00266 { 00267 System.out.println("Failed to open file stream " + inPath); 00268 return false; 00269 } 00270 00271 boolean header = true; 00272 00273 try { 00274 // read one line ahead 00275 00276 while(header && ((in = rd.readLine()) != null)) 00277 { 00278 if (!in.startsWith("Time")) 00279 { 00280 System.out.println("Skipping header line: " + in); 00281 if (in.startsWith("Sample")) sampleRateLine = in; 00282 continue; 00283 } 00284 else 00285 header = false; 00286 } 00287 00288 if (in == null) 00289 { 00290 System.out.println("Unexpected end of file."); 00291 } 00292 00293 // skipping channel list line 00294 // the first non-header line should be a list of channel names 00295 System.out.println("Channel list line: " + in); 00296 00297 lineCount = -1; 00298 00299 while((in = rd.readLine()) != null) 00300 { 00301 lineCount++; 00302 StringTokenizer st = new StringTokenizer(in," "); 00303 00304 // the first token is the time stamp 00305 String tsString = st.nextToken(); 00306 timeStamp[lineCount] = tsString; 00307 00308 // The rest of line is floting point data values 00309 int i = 0; 00310 while (st.hasMoreTokens()) 00311 { 00312 // post data to channel i 00313 values[lineCount][i] = Double.parseDouble(st.nextToken()); 00314 i++; 00315 } 00316 } 00317 } 00318 catch (Throwable t) { 00319 t.getStackTrace(); 00320 return false; 00321 } 00322 return true; 00323 } 00324 00328 private void writeFile() { 00329 try 00330 { 00331 PrintWriter out = new PrintWriter(new FileWriter(outPath)); 00332 // write header 00333 /* 00334 * Active channels: ATL1,ATT1,ATL3,ATT3 00335 * Sample rate: 10.000000 00336 * Channel units: g,g,in,kip (unknown at this time!) 00337 * Time ATL1 ATT1 ATL3 ATT3 00338 */ 00339 out.print("Active channels: "); 00340 out.print(names[0]); // label for Time 00341 for (int i = 1; i < names.length; i++) 00342 { 00343 out.print("," + names[i]); 00344 } 00345 out.println(); 00346 00347 if (sampleRateLine != null) out.println(sampleRateLine); 00348 00349 // Channel units are unknown at this time! 00350 00351 // write channel names 00352 out.print(names[0]); // label for Time 00353 for (int i = 1; i < names.length; i++) 00354 { 00355 out.print(" " + names[i]); 00356 } 00357 out.println(); 00358 00359 // write data 00360 00361 for (int i = 0; i < timeStamp.length; i++) 00362 { 00363 out.print(timeStamp[i]); 00364 for (int j = 0; j < values[0].length; j++) 00365 { 00366 out.print(" " + values[i][j]); 00367 } 00368 out.println(); 00369 out.flush(); 00370 } 00371 out.close(); 00372 00373 } 00374 catch (Throwable t) 00375 { 00376 t.printStackTrace(); 00377 } 00378 } 00379 }

Generated on Tue Aug 24 11:12:27 2004 for Data turbine for NEESGrid by doxygen 1.3.7