PageRenderTime 71ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Solweig_graphs/src/au/edu/monash/mes/envimet/ReadEDTFile.java

https://github.com/mothlight/Solweig_graphs
Java | 466 lines | 359 code | 66 blank | 41 comment | 54 complexity | ef6452b9e2e5528c36339d1564614ca5 MD5 | raw file
  1. package au.edu.monash.mes.envimet;
  2. /* ====================================================================
  3. Kerry Nice
  4. Monash University
  5. ==================================================================== */
  6. import java.io.BufferedInputStream;
  7. import java.io.DataInputStream;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.text.DecimalFormat;
  13. import java.util.ArrayList;
  14. import java.util.TreeMap;
  15. /*
  16. The data files (.EDT) are simple binary files of type "float" with a single precision.
  17. The file are written with the following loop logic
  18. For z:= 0 to ZZ
  19. For y:=1 to YY
  20. For x:=1 to XX
  21. {
  22. Write Value[x][y][z]
  23. }
  24. with XX,YY and ZZ being the dimensions of the model domain.
  25. */
  26. public class ReadEDTFile
  27. {
  28. public static final String X = "x";
  29. public static final String Y = "y";
  30. public static final String Z = "z";
  31. private TreeMap data;
  32. public TreeMap getData()
  33. {
  34. return data;
  35. }
  36. public void setData(TreeMap data)
  37. {
  38. this.data = data;
  39. }
  40. public ReadEDTFile()
  41. {
  42. super();
  43. }
  44. public ReadEDTFile(String filename, ReadEDIFile readEDIFile)
  45. {
  46. super();
  47. readEDTFile(filename, readEDIFile);
  48. }
  49. public void readEDTFileSpecificVariablesSpecificPoint(String filename, ReadEDIFile readEDIFile, ArrayList<String> variablesToObtain
  50. , String cut, int cutValue, float ignoreValue, double xGridPoint, double yGridPoint)
  51. {
  52. //MonashCampusValidationNW_AT_00.00.00 23.03.2011.EDI
  53. String date = filename.substring(filename.length() -14, filename.length() -4);
  54. String time = filename.substring(filename.length() -23, filename.length() - 15);
  55. System.out.println("");
  56. System.out.println("date=" + date + " time=" + time);
  57. double startingX = 2.5;
  58. double startingY = 2.5;
  59. double startingZ = 0.0;
  60. double currentGridX = startingX;
  61. double currentGridY = startingY;
  62. double currentGridZ = startingZ;
  63. File file = new File(filename);
  64. FileInputStream fis = null;
  65. BufferedInputStream bis = null;
  66. DataInputStream dis = null;
  67. ArrayList<String> gridSpacingX = readEDIFile.getGridSpacingX();
  68. ArrayList<String> gridSpacingY = readEDIFile.getGridSpacingY();
  69. ArrayList<String> gridSpacingZ = readEDIFile.getGridSpacingZ();
  70. data = new TreeMap<String, ArrayList>();
  71. ArrayList<String> fileVariables = readEDIFile.getFileVariables();
  72. try
  73. {
  74. DecimalFormat df4 = new DecimalFormat( "#.####" );
  75. fis = new FileInputStream(file);
  76. bis = new BufferedInputStream(fis);
  77. dis = new DataInputStream(bis);
  78. int numOfVariablesInFile = readEDIFile.getNumOfVariablesInFile();
  79. for (int i = 0; i < numOfVariablesInFile; i++)
  80. {
  81. ArrayList variableData = new ArrayList<Float>();
  82. String variableName = fileVariables.get(i);
  83. //if (variableName.equals(variableToObtain))
  84. if (variablesToObtain.contains(variableName))
  85. {
  86. //System.out.println(variableName + " found");
  87. for (int z = 0; z < readEDIFile.getNumOfZGrids(); z++)
  88. {
  89. for (int y = 0; y < readEDIFile.getNumOfYGrids(); y++)
  90. {
  91. for (int x = 0; x < readEDIFile.getNumOfXGrids(); x++)
  92. {
  93. Float floatData = dis.readFloat();
  94. float floatSwapped = ReadEDTFile.swap(floatData);
  95. String formatedFloat = df4.format(floatSwapped);
  96. ArrayList xyPointData = null;
  97. if (cut == null)
  98. {
  99. xyPointData = new ArrayList();
  100. }
  101. else
  102. {
  103. if (cut.equals(X))
  104. {
  105. if (cutValue == x)
  106. {
  107. xyPointData = new ArrayList();
  108. }
  109. }
  110. else if (cut.equals(Y))
  111. {
  112. if (cutValue == y)
  113. {
  114. xyPointData = new ArrayList();
  115. }
  116. }
  117. if (cut.equals(Z))
  118. {
  119. if (cutValue == z)
  120. {
  121. //System.out.println (variableName + " x="+ currentGridX + " y=" + currentGridY + " z=" + currentGridZ + " value=" + formatedFloat);
  122. if (x == xGridPoint && y == yGridPoint)
  123. {
  124. xyPointData = new ArrayList();
  125. }
  126. }
  127. }
  128. }
  129. if (xyPointData != null)
  130. {
  131. xyPointData.add(currentGridX);
  132. xyPointData.add(currentGridY);
  133. xyPointData.add(formatedFloat);
  134. xyPointData.add(currentGridZ);
  135. if (floatSwapped != ignoreValue)
  136. {
  137. variableData.add(xyPointData);
  138. }
  139. System.out.println (variableName + " x="+ currentGridX + " y=" + currentGridY + " z=" + currentGridZ + " value=" + formatedFloat);
  140. }
  141. String nextGridXStr = (String)gridSpacingX.get(x);
  142. Float nextGridX = new Float(nextGridXStr).floatValue();
  143. currentGridX = currentGridX + nextGridX;
  144. }
  145. String nextGridYStr = (String)gridSpacingY.get(y);
  146. Float nextGridY = new Float(nextGridYStr).floatValue();
  147. currentGridY = currentGridY + nextGridY;
  148. currentGridX = startingX;
  149. }
  150. String nextGridZStr = (String)gridSpacingZ.get(z);
  151. Float nextGridZ = new Float(nextGridZStr).floatValue();
  152. currentGridZ = currentGridZ + nextGridZ;
  153. currentGridY = startingY;
  154. }
  155. currentGridZ = startingZ;
  156. data.put(variableName, variableData);
  157. //System.out.println("End of " + variableName);
  158. }
  159. else
  160. {
  161. int numberInVariable = readEDIFile.getNumOfZGrids() * readEDIFile.getNumOfYGrids() * readEDIFile.getNumOfXGrids();
  162. for (int useUp=0;useUp<numberInVariable;useUp++)
  163. {
  164. dis.readFloat();
  165. }
  166. }
  167. }
  168. // dispose all the resources after using them.
  169. fis.close();
  170. bis.close();
  171. dis.close();
  172. } catch (FileNotFoundException e)
  173. {
  174. e.printStackTrace();
  175. } catch (IOException e)
  176. {
  177. e.printStackTrace();
  178. }
  179. }
  180. public void readEDTFileSingleVariable(String filename, ReadEDIFile readEDIFile, String variableToObtain, String cut, int cutValue, float ignoreValue)
  181. {
  182. double startingX = 2.5;
  183. double startingY = 2.5;
  184. double startingZ = 0.0;
  185. double currentGridX = startingX;
  186. double currentGridY = startingY;
  187. double currentGridZ = startingZ;
  188. File file = new File(filename);
  189. FileInputStream fis = null;
  190. BufferedInputStream bis = null;
  191. DataInputStream dis = null;
  192. ArrayList<String> gridSpacingX = readEDIFile.getGridSpacingX();
  193. ArrayList<String> gridSpacingY = readEDIFile.getGridSpacingY();
  194. ArrayList<String> gridSpacingZ = readEDIFile.getGridSpacingZ();
  195. data = new TreeMap<String, ArrayList>();
  196. ArrayList<String> fileVariables = readEDIFile.getFileVariables();
  197. try
  198. {
  199. DecimalFormat df4 = new DecimalFormat( "#.####" );
  200. fis = new FileInputStream(file);
  201. bis = new BufferedInputStream(fis);
  202. dis = new DataInputStream(bis);
  203. int numOfVariablesInFile = readEDIFile.getNumOfVariablesInFile();
  204. for (int i = 0; i < numOfVariablesInFile; i++)
  205. {
  206. ArrayList variableData = new ArrayList<Float>();
  207. String variableName = fileVariables.get(i);
  208. if (variableName.equals(variableToObtain))
  209. {
  210. System.out.println(variableToObtain + " found");
  211. for (int z = 0; z < readEDIFile.getNumOfZGrids(); z++)
  212. {
  213. for (int y = 0; y < readEDIFile.getNumOfYGrids(); y++)
  214. {
  215. for (int x = 0; x < readEDIFile.getNumOfXGrids(); x++)
  216. {
  217. Float floatData = dis.readFloat();
  218. float floatSwapped = ReadEDTFile.swap(floatData);
  219. String formatedFloat = df4.format(floatSwapped);
  220. ArrayList xyPointData = null;
  221. if (cut == null)
  222. {
  223. xyPointData = new ArrayList();
  224. }
  225. else
  226. {
  227. if (cut.equals(X))
  228. {
  229. if (cutValue == x)
  230. {
  231. xyPointData = new ArrayList();
  232. }
  233. }
  234. else if (cut.equals(Y))
  235. {
  236. if (cutValue == y)
  237. {
  238. xyPointData = new ArrayList();
  239. }
  240. }
  241. if (cut.equals(Z))
  242. {
  243. if (cutValue == z)
  244. {
  245. xyPointData = new ArrayList();
  246. }
  247. }
  248. }
  249. if (xyPointData != null)
  250. {
  251. xyPointData.add(currentGridX);
  252. xyPointData.add(currentGridY);
  253. xyPointData.add(formatedFloat);
  254. xyPointData.add(currentGridZ);
  255. if (floatSwapped != ignoreValue)
  256. {
  257. variableData.add(xyPointData);
  258. }
  259. System.out.println (variableName + " x="+ currentGridX + " y=" + currentGridY + " z=" + currentGridZ + " value=" + formatedFloat);
  260. }
  261. String nextGridXStr = (String)gridSpacingX.get(x);
  262. Float nextGridX = new Float(nextGridXStr).floatValue();
  263. currentGridX = currentGridX + nextGridX;
  264. }
  265. String nextGridYStr = (String)gridSpacingY.get(y);
  266. Float nextGridY = new Float(nextGridYStr).floatValue();
  267. currentGridY = currentGridY + nextGridY;
  268. currentGridX = startingX;
  269. }
  270. String nextGridZStr = (String)gridSpacingZ.get(z);
  271. Float nextGridZ = new Float(nextGridZStr).floatValue();
  272. currentGridZ = currentGridZ + nextGridZ;
  273. currentGridY = startingY;
  274. }
  275. data.put(variableName, variableData);
  276. //System.out.println("End of " + variableName);
  277. }
  278. else
  279. {
  280. int numberInVariable = readEDIFile.getNumOfZGrids() * readEDIFile.getNumOfYGrids() * readEDIFile.getNumOfXGrids();
  281. for (int useUp=0;useUp<numberInVariable;useUp++)
  282. {
  283. dis.readFloat();
  284. }
  285. }
  286. }
  287. // dispose all the resources after using them.
  288. fis.close();
  289. bis.close();
  290. dis.close();
  291. } catch (FileNotFoundException e)
  292. {
  293. e.printStackTrace();
  294. } catch (IOException e)
  295. {
  296. e.printStackTrace();
  297. }
  298. }
  299. public void readEDTFile(String filename, ReadEDIFile readEDIFile)
  300. {
  301. double startingX = 2.5;
  302. double startingY = 2.5;
  303. double startingZ = 0.0;
  304. double currentGridX = startingX;
  305. double currentGridY = startingY;
  306. double currentGridZ = startingZ;
  307. File file = new File(filename);
  308. FileInputStream fis = null;
  309. BufferedInputStream bis = null;
  310. DataInputStream dis = null;
  311. ArrayList<String> gridSpacingX = readEDIFile.getGridSpacingX();
  312. ArrayList<String> gridSpacingY = readEDIFile.getGridSpacingY();
  313. ArrayList<String> gridSpacingZ = readEDIFile.getGridSpacingZ();
  314. data = new TreeMap<String, ArrayList>();
  315. ArrayList<String> fileVariables = readEDIFile.getFileVariables();
  316. try
  317. {
  318. DecimalFormat df4 = new DecimalFormat( "#.####" );
  319. fis = new FileInputStream(file);
  320. bis = new BufferedInputStream(fis);
  321. dis = new DataInputStream(bis);
  322. for (int i = 0; i < readEDIFile.getNumOfVariablesInFile(); i++)
  323. {
  324. ArrayList variableData = new ArrayList<Float>();
  325. String variableName = fileVariables.get(i);
  326. for (int z = 0; z < readEDIFile.getNumOfZGrids(); z++)
  327. {
  328. for (int y = 0; y < readEDIFile.getNumOfYGrids(); y++)
  329. {
  330. for (int x = 0; x < readEDIFile.getNumOfXGrids(); x++)
  331. {
  332. Float floatData = dis.readFloat();
  333. float floatSwapped = ReadEDTFile.swap(floatData);
  334. String formatedFloat = df4.format(floatSwapped);
  335. ArrayList xyPointData = new ArrayList();
  336. xyPointData.add(currentGridX);
  337. xyPointData.add(currentGridY);
  338. xyPointData.add(formatedFloat);
  339. xyPointData.add(currentGridZ);
  340. variableData.add(xyPointData);
  341. //System.out.println ("x="+ currentGridX + " y=" + currentGridY + " value=" + formatedFloat);
  342. String nextGridXStr = (String)gridSpacingX.get(x);
  343. Float nextGridX = new Float(nextGridXStr).floatValue();
  344. currentGridX = currentGridX + nextGridX;
  345. }
  346. String nextGridYStr = (String)gridSpacingY.get(y);
  347. Float nextGridY = new Float(nextGridYStr).floatValue();
  348. currentGridY = currentGridY + nextGridY;
  349. currentGridX = startingX;
  350. }
  351. String nextGridZStr = (String)gridSpacingZ.get(z);
  352. Float nextGridZ = new Float(nextGridZStr).floatValue();
  353. currentGridZ = currentGridZ + nextGridZ;
  354. currentGridY = startingY;
  355. }
  356. data.put(variableName, variableData);
  357. //System.out.println("End of " + variableName);
  358. }
  359. // dispose all the resources after using them.
  360. fis.close();
  361. bis.close();
  362. dis.close();
  363. } catch (FileNotFoundException e)
  364. {
  365. e.printStackTrace();
  366. } catch (IOException e)
  367. {
  368. e.printStackTrace();
  369. }
  370. }
  371. /**
  372. * Byte swap a single float value.
  373. *
  374. * @param value Value to byte swap.
  375. * @return Byte swapped representation.
  376. */
  377. public static float swap (float value)
  378. {
  379. int intValue = Float.floatToIntBits (value);
  380. intValue = swap (intValue);
  381. return Float.intBitsToFloat (intValue);
  382. }
  383. /**
  384. * Byte swap a single int value.
  385. *
  386. * @param value Value to byte swap.
  387. * @return Byte swapped representation.
  388. */
  389. public static int swap (int value)
  390. {
  391. int b1 = (value >> 0) & 0xff;
  392. int b2 = (value >> 8) & 0xff;
  393. int b3 = (value >> 16) & 0xff;
  394. int b4 = (value >> 24) & 0xff;
  395. return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
  396. }
  397. }