PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/esa/beam/meris/icol/KernelOffNadir.java

https://github.com/bcdev/beam-meris-icol
Java | 217 lines | 175 code | 25 blank | 17 comment | 46 complexity | b71446c40a61cb87a6b02d8ad160bd00 MD5 | raw file
  1. package org.esa.beam.meris.icol;
  2. import org.esa.beam.framework.gpf.OperatorException;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.StringTokenizer;
  10. /**
  11. * Class for reading off-nadir kernels provided by RS.
  12. * (This is more or less a test class at this point. Check if it will be needed finally.)
  13. *
  14. * @author Olaf Danne
  15. * @version $Revision: $ $Date: $
  16. */
  17. public class KernelOffNadir {
  18. public static final int KERNEL_MATRIX_WIDTH_RR = 51;
  19. private String fileName;
  20. private List<Float> kernelList;
  21. public KernelOffNadir(String fileName) {
  22. this.fileName = fileName;
  23. try {
  24. if (fileName.startsWith("W_ray")) {
  25. readKernelMatrixRayleigh();
  26. } else if (fileName.startsWith("W_aer")) {
  27. readKernelMatrixAerosol();
  28. }
  29. } catch (IOException e) {
  30. // todo: handle or propagate
  31. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  32. }
  33. }
  34. public float[] getKernelAsArray() {
  35. float[] result = new float[kernelList.size()];
  36. for (int i = 0; i < kernelList.size(); i++) {
  37. result[i] = kernelList.get(i);
  38. }
  39. return result;
  40. }
  41. private void readKernelMatrixRayleighOld() throws IOException {
  42. BufferedReader bufferedReader;
  43. InputStream inputStream;
  44. inputStream = CoeffW.class.getResourceAsStream(fileName);
  45. bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  46. StringTokenizer st;
  47. try {
  48. kernelList = new ArrayList<Float>();
  49. int i = 0;
  50. String line;
  51. while ((line = bufferedReader.readLine()) != null && i < KERNEL_MATRIX_WIDTH_RR) {
  52. line = line.trim();
  53. st = new StringTokenizer(line, ", ", false);
  54. // reduced resulution, take every 3rd value catching the center...
  55. if ((i - 1) % 3 == 0) {
  56. int j = 0;
  57. while (st.hasMoreTokens() && j < KERNEL_MATRIX_WIDTH_RR) {
  58. // W matrix element
  59. if ((j - 1) % 3 == 0) {
  60. kernelList.add(Float.parseFloat(st.nextToken()));
  61. } else {
  62. st.nextToken();
  63. }
  64. j++;
  65. }
  66. }
  67. i++;
  68. }
  69. } catch (IOException e) {
  70. throw new OperatorException("Failed to load W matrix: \n" + e.getMessage(), e);
  71. } catch (NumberFormatException e) {
  72. throw new OperatorException("Failed to load W matrix: \n" + e.getMessage(), e);
  73. } finally {
  74. if (inputStream != null) {
  75. inputStream.close();
  76. }
  77. }
  78. }
  79. private void readKernelMatrixRayleigh() throws IOException {
  80. BufferedReader bufferedReader;
  81. InputStream inputStream;
  82. inputStream = CoeffW.class.getResourceAsStream(fileName);
  83. bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  84. StringTokenizer st;
  85. try {
  86. // read and save all lines in file (half matrix)
  87. String[] line = new String[KERNEL_MATRIX_WIDTH_RR];
  88. int lineIndex = 25;
  89. while (lineIndex < KERNEL_MATRIX_WIDTH_RR && (line[lineIndex] = bufferedReader.readLine()) != null) {
  90. line[lineIndex] = line[lineIndex].trim();
  91. lineIndex++;
  92. }
  93. // W(i,j) --> W (i,-j) (mirror)
  94. for (int j = 0; j < 25; j++) {
  95. line[24 - j] = new String(line[26 + j]);
  96. }
  97. kernelList = new ArrayList<Float>();
  98. for (int i = 0; i < KERNEL_MATRIX_WIDTH_RR; i++) {
  99. st = new StringTokenizer(line[i], ", ", false);
  100. // reduced resolution, take every 3rd value catching the center...
  101. if ((i - 1) % 3 == 0) {
  102. int k = 0;
  103. while (st.hasMoreTokens() && k < KERNEL_MATRIX_WIDTH_RR) {
  104. // W matrix element
  105. if ((k - 1) % 3 == 0) {
  106. kernelList.add(Float.parseFloat(st.nextToken()));
  107. } else {
  108. st.nextToken();
  109. }
  110. k++;
  111. }
  112. }
  113. }
  114. } catch (IOException e) {
  115. throw new OperatorException("Failed to load W matrix: \n" + e.getMessage(), e);
  116. } catch (NumberFormatException e) {
  117. throw new OperatorException("Failed to load W matrix: \n" + e.getMessage(), e);
  118. } finally {
  119. if (inputStream != null) {
  120. inputStream.close();
  121. }
  122. }
  123. }
  124. private void readKernelMatrixAerosolOld() throws IOException {
  125. BufferedReader bufferedReader;
  126. InputStream inputStream;
  127. inputStream = CoeffW.class.getResourceAsStream(fileName);
  128. bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  129. StringTokenizer st;
  130. try {
  131. kernelList = new ArrayList<Float>();
  132. int i = 0;
  133. String line;
  134. while ((line = bufferedReader.readLine()) != null && i < KERNEL_MATRIX_WIDTH_RR) {
  135. line = line.trim();
  136. st = new StringTokenizer(line, ", ", false);
  137. int j = 0;
  138. while (st.hasMoreTokens() && j < KERNEL_MATRIX_WIDTH_RR) {
  139. kernelList.add(Float.parseFloat(st.nextToken()));
  140. j++;
  141. }
  142. i++;
  143. }
  144. } catch (IOException e) {
  145. throw new OperatorException("Failed to load W matrix: \n" + e.getMessage(), e);
  146. } catch (NumberFormatException e) {
  147. throw new OperatorException("Failed to load W matrix: \n" + e.getMessage(), e);
  148. } finally {
  149. if (inputStream != null) {
  150. inputStream.close();
  151. }
  152. }
  153. }
  154. private void readKernelMatrixAerosol() throws IOException {
  155. BufferedReader bufferedReader;
  156. InputStream inputStream;
  157. inputStream = CoeffW.class.getResourceAsStream(fileName);
  158. bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  159. StringTokenizer st;
  160. try {
  161. // read and save all lines in file (half matrix)
  162. String[] line = new String[KERNEL_MATRIX_WIDTH_RR];
  163. int lineIndex = 25;
  164. while (lineIndex < KERNEL_MATRIX_WIDTH_RR && (line[lineIndex] = bufferedReader.readLine()) != null) {
  165. line[lineIndex] = line[lineIndex].trim();
  166. lineIndex++;
  167. }
  168. // W(i,j) --> W (i,-j) (mirror)
  169. for (int j = 0; j < 25; j++) {
  170. line[24 - j] = new String(line[26 + j]);
  171. }
  172. kernelList = new ArrayList<Float>();
  173. for (int i = 0; i < KERNEL_MATRIX_WIDTH_RR; i++) {
  174. st = new StringTokenizer(line[i], ", ", false);
  175. int k = 0;
  176. while (st.hasMoreTokens() && k < KERNEL_MATRIX_WIDTH_RR) {
  177. // W matrix element
  178. kernelList.add(Float.parseFloat(st.nextToken()));
  179. k++;
  180. }
  181. }
  182. } catch (IOException e) {
  183. throw new OperatorException("Failed to load W matrix: \n" + e.getMessage(), e);
  184. } catch (NumberFormatException e) {
  185. throw new OperatorException("Failed to load W matrix: \n" + e.getMessage(), e);
  186. } finally {
  187. if (inputStream != null) {
  188. inputStream.close();
  189. }
  190. }
  191. }
  192. }