|
| 1 | +package com.bc.fiduceo.reader.slstr_subset; |
| 2 | + |
| 3 | +import com.bc.fiduceo.store.Store; |
| 4 | +import org.esa.snap.core.util.StringUtils; |
| 5 | +import org.esa.snap.core.util.io.CsvReader; |
| 6 | +import ucar.nc2.NetcdfFile; |
| 7 | +import ucar.nc2.Variable; |
| 8 | + |
| 9 | +import java.awt.image.Raster; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.InputStreamReader; |
| 13 | +import java.nio.charset.Charset; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.TreeSet; |
| 19 | + |
| 20 | +import static ucar.nc2.NetcdfFiles.openInMemory; |
| 21 | + |
| 22 | +class NcCache { |
| 23 | + |
| 24 | + private final Charset charset = StandardCharsets.UTF_8; |
| 25 | + private final char[] separators = new char[]{'|'}; |
| 26 | + |
| 27 | + private Store store; |
| 28 | + private RasterInfo rasterInfo; |
| 29 | + private HashMap<String, String> dddBMap; |
| 30 | + private HashMap<String, NetcdfFile> ncFilesMap; |
| 31 | + private HashMap<String, Variable> variableMap; |
| 32 | + |
| 33 | + void open(Store store, RasterInfo rasterInfo) throws IOException { |
| 34 | + this.store = store; |
| 35 | + this.rasterInfo = rasterInfo; |
| 36 | + |
| 37 | + parseDDDB(); |
| 38 | + |
| 39 | + ncFilesMap = new HashMap<>(); |
| 40 | + variableMap = new HashMap<>(); |
| 41 | + } |
| 42 | + |
| 43 | + void close() throws IOException { |
| 44 | + dddBMap.clear(); |
| 45 | + dddBMap = null; |
| 46 | + |
| 47 | + variableMap.clear(); |
| 48 | + variableMap = null; |
| 49 | + |
| 50 | + for (final NetcdfFile ncFile : ncFilesMap.values()) { |
| 51 | + ncFile.close(); |
| 52 | + } |
| 53 | + ncFilesMap.clear(); |
| 54 | + ncFilesMap = null; |
| 55 | + |
| 56 | + if (store != null) { |
| 57 | + store.close(); |
| 58 | + store = null; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void parseDDDB() throws IOException { |
| 63 | + final InputStream inputStream = getClass().getResourceAsStream("slstr_subset_dddb_100.txt"); |
| 64 | + if (inputStream == null) { |
| 65 | + throw new IllegalStateException("The internal resource file could not be read."); |
| 66 | + } |
| 67 | + |
| 68 | + final CsvReader reader = new CsvReader(new InputStreamReader(inputStream, charset), separators, true, "#"); |
| 69 | + final List<String[]> recordList = reader.readStringRecords(); |
| 70 | + dddBMap = new HashMap<>(recordList.size()); |
| 71 | + for (String[] record : recordList) { |
| 72 | + dddBMap.put(record[0], record[1]); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + List<String> getVariableNames() { |
| 77 | + return new ArrayList<>(dddBMap.keySet()); |
| 78 | + } |
| 79 | + |
| 80 | + Variable getVariable(String variableName) throws IOException { |
| 81 | + Variable variable = variableMap.get(variableName); |
| 82 | + if (variable != null) { |
| 83 | + return variable; |
| 84 | + } else { |
| 85 | + final String fileName = dddBMap.get(variableName); |
| 86 | + if (StringUtils.isNullOrEmpty(fileName)) { |
| 87 | + throw new IOException("Invalid variable name requested: " + variableName); |
| 88 | + } |
| 89 | + |
| 90 | + final NetcdfFile ncFile = getNetcdfFile(fileName); |
| 91 | + variable = ncFile.findVariable(variableName); |
| 92 | + if (variable == null) { |
| 93 | + throw new IOException("Invalid variable name requested: " + variableName); |
| 94 | + } |
| 95 | + |
| 96 | + if (isTiePointVariable(variableName)) { |
| 97 | + final double subSampling = (double) rasterInfo.tiePointResolution / (double) rasterInfo.rasterResolution; |
| 98 | + final double offset = rasterInfo.rasterTrackOffset - rasterInfo.tiePointTrackOffset * subSampling; |
| 99 | + variable = new SlstrSubsetTiePointVariable(variable, rasterInfo.rasterWidth, rasterInfo.rasterHeight, offset, subSampling); |
| 100 | + } |
| 101 | + variableMap.put(variableName, variable); |
| 102 | + return variable; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // package access for testing only tb 2022-08-18 |
| 107 | + static boolean isTiePointVariable(String variableName) { |
| 108 | + return variableName.endsWith("_tn") || variableName.endsWith("_to"); |
| 109 | + } |
| 110 | + |
| 111 | + private NetcdfFile getNetcdfFile(String fileName) throws IOException { |
| 112 | + NetcdfFile ncFile = ncFilesMap.get(fileName); |
| 113 | + if (ncFile == null) { |
| 114 | + final TreeSet<String> keys = store.getKeysEndingWith(fileName); |
| 115 | + final byte[] bytes = store.getBytes(keys.first()); |
| 116 | + ncFile = openInMemory(fileName, bytes); |
| 117 | + ncFilesMap.put(fileName, ncFile); |
| 118 | + } |
| 119 | + return ncFile; |
| 120 | + } |
| 121 | +} |
0 commit comments