|
| 1 | +package com.bc.fiduceo.reader.insitu.tao.preproc; |
| 2 | + |
| 3 | +import com.bc.fiduceo.util.TimeUtils; |
| 4 | +import org.esa.snap.core.util.StringUtils; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.text.DecimalFormat; |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +public class TaoPreProcessor { |
| 11 | + |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + final Configuration configuration = new Configuration(); |
| 14 | + configuration.sourceDir = "C:\\Satellite\\CIMR\\TAO_buoy"; |
| 15 | + configuration.targetDir = "C:\\Satellite\\CIMR\\TAO_merged"; |
| 16 | + configuration.filePrefix = "TAO_T0N110W"; |
| 17 | + configuration.sssFileName = "TAO_T0N110W_D_SALT_hourly.ascii"; |
| 18 | + |
| 19 | + // --- read all we need --- |
| 20 | + final File sourceDir = new File(configuration.sourceDir); |
| 21 | + final File sssFile = new File(sourceDir, configuration.sssFileName); |
| 22 | + |
| 23 | + final HashMap<String, List<SSSRecord>> sssMap = parseSSSFile(sssFile); |
| 24 | + final HashMap<String, List<TAORecord>> taoMap = new HashMap<>(); |
| 25 | + |
| 26 | + // --- assemble final records --- |
| 27 | + Set<String> deployments = sssMap.keySet(); |
| 28 | + for (final String deployment : deployments) { |
| 29 | + final List<SSSRecord> sssRecords = sssMap.get(deployment); |
| 30 | + final ArrayList<TAORecord> taoRecords = new ArrayList<>(); |
| 31 | + |
| 32 | + for (final SSSRecord sssRecord : sssRecords) { |
| 33 | + final TAORecord taoRecord = new TAORecord(); |
| 34 | + String M = ""; |
| 35 | + String Q = ""; |
| 36 | + |
| 37 | + taoRecord.date = sssRecord.date; |
| 38 | + taoRecord.SSS = sssRecord.SSS; |
| 39 | + M = M.concat(sssRecord.M); |
| 40 | + Q = Q.concat(sssRecord.Q); |
| 41 | + |
| 42 | + taoRecord.M = M; |
| 43 | + taoRecord.Q = Q; |
| 44 | + |
| 45 | + taoRecords.add(taoRecord); |
| 46 | + } |
| 47 | + |
| 48 | + taoMap.put(deployment, taoRecords); |
| 49 | + } |
| 50 | + |
| 51 | + // --- write files per month and deployment ---- |
| 52 | + final DecimalFormat format = new DecimalFormat("00"); |
| 53 | + deployments = taoMap.keySet(); |
| 54 | + PrintWriter writer = null; |
| 55 | + for (final String deployment : deployments) { |
| 56 | + final Calendar utcCalendar = TimeUtils.getUTCCalendar(); |
| 57 | + final String filePrefix = configuration.filePrefix + "_" + deployment + "_"; |
| 58 | + |
| 59 | + int currentYear = -9; |
| 60 | + int currentMonth = -9; |
| 61 | + |
| 62 | + final List<TAORecord> taoRecords = taoMap.get(deployment); |
| 63 | + for (final TAORecord taoRecord : taoRecords) { |
| 64 | + utcCalendar.setTimeInMillis(taoRecord.date * 1000L); |
| 65 | + int year = utcCalendar.get(Calendar.YEAR); |
| 66 | + int month = utcCalendar.get(Calendar.MONTH); |
| 67 | + if (year != currentYear || month != currentMonth) { |
| 68 | + currentYear = year; |
| 69 | + currentMonth = month; |
| 70 | + writer = switchWriter(configuration, format, writer, filePrefix, currentYear, currentMonth); |
| 71 | + } |
| 72 | + |
| 73 | + writer.println(taoRecord.toLine()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (writer != null) { |
| 78 | + writer.flush(); |
| 79 | + writer.close(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static PrintWriter switchWriter(Configuration configuration, DecimalFormat format, PrintWriter writer, String filePrefix, int currentYear, int currentMonth) throws IOException { |
| 84 | + if (writer != null) { |
| 85 | + writer.flush(); |
| 86 | + writer.close(); |
| 87 | + } |
| 88 | + // create new writer |
| 89 | + final File targetDir = new File(configuration.targetDir, currentYear + File.separator + format.format(currentMonth)); |
| 90 | + if (!targetDir.exists()) { |
| 91 | + if (!targetDir.mkdirs()) { |
| 92 | + throw new IOException("unable to create directory: " + targetDir.getAbsolutePath()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + final File targetFile = new File(targetDir, filePrefix + currentYear + "-" + format.format(currentMonth) + ".txt"); |
| 97 | + if (!targetFile.createNewFile()) { |
| 98 | + throw new IOException("unable to create file: " + targetFile.getAbsolutePath()); |
| 99 | + } |
| 100 | + |
| 101 | + return new PrintWriter(targetFile); |
| 102 | + } |
| 103 | + |
| 104 | + private static HashMap<String, List<SSSRecord>> parseSSSFile(File sssFile) { |
| 105 | + final HashMap<String, List<SSSRecord>> sssMap = new HashMap<>(); |
| 106 | + try (final FileReader fileReader = new FileReader(sssFile)) { |
| 107 | + final BufferedReader bufferedReader = new BufferedReader(fileReader); |
| 108 | + String line; |
| 109 | + ArrayList<SSSRecord> records = null; |
| 110 | + while ((line = bufferedReader.readLine()) != null) { |
| 111 | + if (line.startsWith("Platform") || line.startsWith("Parameter") || line.startsWith("YYYY")) { |
| 112 | + continue; // skip these tb 2023-03-30 |
| 113 | + } |
| 114 | + |
| 115 | + if (line.startsWith("Deployment")) { |
| 116 | + // parse deployment and start new record-container |
| 117 | + final String[] tokens = StringUtils.split(line, new char[]{' '}, true); |
| 118 | + records = new ArrayList<>(); |
| 119 | + sssMap.put(tokens[1], records); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + if (line.startsWith("Depth") || line.startsWith("Height")) { |
| 124 | + // TODO! parse depth or height and store |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + final String[] tokens = StringUtils.split(line, new char[]{' '}, true); |
| 129 | + final SSSRecord sssRecord = new SSSRecord(); |
| 130 | + sssRecord.date = toUnixEpoch(tokens[0], tokens[1]); |
| 131 | + sssRecord.SSS = tokens[2]; |
| 132 | + if (tokens.length == 12) { |
| 133 | + sssRecord.Q = tokens[10].substring(0, 1); |
| 134 | + sssRecord.M = tokens[11].substring(0, 1); |
| 135 | + } else if (tokens.length == 5) { |
| 136 | + sssRecord.Q = tokens[3]; |
| 137 | + sssRecord.M = tokens[4]; |
| 138 | + } else { |
| 139 | + throw new IOException("unknown line format:" + line); |
| 140 | + } |
| 141 | + |
| 142 | + records.add(sssRecord); |
| 143 | + } |
| 144 | + } catch (IOException e) { |
| 145 | + throw new RuntimeException(e); |
| 146 | + } |
| 147 | + |
| 148 | + return sssMap; |
| 149 | + } |
| 150 | + |
| 151 | + // Triton |
| 152 | + // ------ |
| 153 | + // salt YYYYMMDD HHMMSS SSS Q M depth |
| 154 | + // airt AIRT Q M height |
| 155 | + // baro SLP Q M height |
| 156 | + // rain RAIN Q M height |
| 157 | + // rh RH Q M height |
| 158 | + // sst SST Q M depth |
| 159 | + // wind WSPD WDIR Q M height |
| 160 | + // lon |
| 161 | + // lat |
| 162 | + // 8 parameter |
| 163 | + |
| 164 | + // TAO |
| 165 | + // salt YYYYMMDD HHMMSS SSS Q M depth |
| 166 | + // airt AIRT Q M height |
| 167 | + // baro SLP Q M height |
| 168 | + // rain RAIN Q M height |
| 169 | + // rh RH Q M height |
| 170 | + // sst SST Q M depth |
| 171 | + // wind WSPD WDIR Q M height |
| 172 | + |
| 173 | + static class Configuration { |
| 174 | + String sourceDir; |
| 175 | + String targetDir; |
| 176 | + String filePrefix; |
| 177 | + String sssFileName; |
| 178 | + } |
| 179 | + |
| 180 | + static class SSSRecord { |
| 181 | + int date; |
| 182 | + String SSS; |
| 183 | + String Q; |
| 184 | + String M; |
| 185 | + } |
| 186 | + |
| 187 | + static class TAORecord { |
| 188 | + int date; |
| 189 | + String SSS; |
| 190 | + String Q; |
| 191 | + String M; |
| 192 | + |
| 193 | + String toLine() { |
| 194 | + return date + " " + SSS + " " + Q + " " + M; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + static int toUnixEpoch(String yyyymmdd, String hhmmss) { |
| 199 | + int year = Integer.parseInt(yyyymmdd.substring(0, 4)); |
| 200 | + int month = Integer.parseInt(yyyymmdd.substring(4, 6)); |
| 201 | + int day = Integer.parseInt(yyyymmdd.substring(6, 8)); |
| 202 | + |
| 203 | + int hour = Integer.parseInt(hhmmss.substring(0, 2)); |
| 204 | + int minute = Integer.parseInt(hhmmss.substring(2, 4)); |
| 205 | + int second = Integer.parseInt(hhmmss.substring(4, 6)); |
| 206 | + |
| 207 | + final Calendar calendar = TimeUtils.getUTCCalendar(); |
| 208 | + calendar.set(Calendar.YEAR, year); |
| 209 | + calendar.set(Calendar.MONTH, month - 1); |
| 210 | + calendar.set(Calendar.DAY_OF_MONTH, day); |
| 211 | + calendar.set(Calendar.HOUR_OF_DAY, hour); |
| 212 | + calendar.set(Calendar.MINUTE, minute); |
| 213 | + calendar.set(Calendar.SECOND, second); |
| 214 | + |
| 215 | + long timeInMillis = calendar.getTime().getTime(); |
| 216 | + return (int) (timeInMillis / 1000); |
| 217 | + } |
| 218 | +} |
0 commit comments