|
| 1 | +package com.bc.fiduceo.qc; |
| 2 | + |
| 3 | +import com.bc.fiduceo.FiduceoConstants; |
| 4 | +import com.bc.fiduceo.log.FiduceoLogger; |
| 5 | +import com.bc.fiduceo.util.NetCDFUtils; |
| 6 | +import org.apache.commons.cli.CommandLine; |
| 7 | +import org.apache.commons.cli.HelpFormatter; |
| 8 | +import org.apache.commons.cli.Option; |
| 9 | +import org.apache.commons.cli.Options; |
| 10 | +import ucar.ma2.Array; |
| 11 | +import ucar.ma2.IndexIterator; |
| 12 | +import ucar.ma2.InvalidRangeException; |
| 13 | +import ucar.nc2.NetcdfFile; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.OutputStream; |
| 18 | +import java.io.PrintWriter; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.*; |
| 22 | +import java.util.logging.Logger; |
| 23 | +import java.util.regex.Pattern; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import static com.bc.fiduceo.FiduceoConstants.VERSION_NUMBER; |
| 28 | +import static com.bc.fiduceo.util.MMDUtil.getMMDFileNamePattern; |
| 29 | + |
| 30 | +class MmdQCTool { |
| 31 | + |
| 32 | + private final static Logger logger = FiduceoLogger.getLogger(); |
| 33 | + |
| 34 | + private FileMessages fileMessages; |
| 35 | + private MatchupAccumulator matchupAccumulator; |
| 36 | + |
| 37 | + // package access for testing only tb 2023-02-14 |
| 38 | + static Options getOptions() { |
| 39 | + final Options options = new Options(); |
| 40 | + |
| 41 | + final Option helpOption = new Option("h", "help", false, "Prints the tool usage."); |
| 42 | + options.addOption(helpOption); |
| 43 | + |
| 44 | + final Option inputOption = new Option("i", "input", true, "Defines the MMD input directory."); |
| 45 | + inputOption.setRequired(true); |
| 46 | + options.addOption(inputOption); |
| 47 | + |
| 48 | + final Option timeOption = new Option("t", "time", true, "Defines matchup time variable name."); |
| 49 | + timeOption.setRequired(true); |
| 50 | + options.addOption(timeOption); |
| 51 | + |
| 52 | + return options; |
| 53 | + } |
| 54 | + |
| 55 | + // package access for testing only tb 2023-02-14 |
| 56 | + static void writeReport(OutputStream outputStream, MatchupAccumulator accumulator, FileMessages fileMessages) { |
| 57 | + final PrintWriter writer = new PrintWriter(outputStream); |
| 58 | + |
| 59 | + final int fileCount = accumulator.getFileCount(); |
| 60 | + writer.println("Analysed " + fileCount + " file(s)"); |
| 61 | + if (fileCount == 0) { |
| 62 | + writer.flush(); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + writer.println(); |
| 67 | + final HashMap<String, List<String>> messageMap = fileMessages.getMessageMap(); |
| 68 | + final int size = messageMap.size(); |
| 69 | + writer.println(size + " file(s) with errors"); |
| 70 | + if (size > 0) { |
| 71 | + // @todo 1 tb/tb add error messages per file here |
| 72 | + } |
| 73 | + |
| 74 | + writer.println(); |
| 75 | + writer.println("Total number of matchups: " + accumulator.getSummaryCount()); |
| 76 | + writer.println("Daily distribution:"); |
| 77 | + |
| 78 | + final TreeMap<String, Integer> treeMap = new TreeMap<>(accumulator.getDaysMap()); |
| 79 | + final Set<Map.Entry<String, Integer>> entries = treeMap.entrySet(); |
| 80 | + for (final Map.Entry<String, Integer> entry : entries) { |
| 81 | + writer.println(entry.getKey() + ": " + entry.getValue()); |
| 82 | + } |
| 83 | + |
| 84 | + writer.flush(); |
| 85 | + } |
| 86 | + |
| 87 | + void run(CommandLine commandLine) throws IOException { |
| 88 | + final String inputDirOption = commandLine.getOptionValue("i"); |
| 89 | + final List<Path> mmdFiles = getInputFiles(inputDirOption); |
| 90 | + logger.info("Found " + mmdFiles.size() + " input file(s) to analyze."); |
| 91 | + |
| 92 | + fileMessages = new FileMessages(); |
| 93 | + matchupAccumulator = new MatchupAccumulator(); |
| 94 | + |
| 95 | + // loop over files |
| 96 | + for (final Path mmdFile : mmdFiles) { |
| 97 | + |
| 98 | + try (final NetcdfFile netcdfFile = NetCDFUtils.openReadOnly(mmdFile.toAbsolutePath().toString())) { |
| 99 | + |
| 100 | + // read time variable center pixel |
| 101 | + final String timeVariableName = commandLine.getOptionValue("t"); |
| 102 | + final Array timeArray = NetCDFUtils.getCenterPosArrayFromMMDFile(netcdfFile, timeVariableName, null, |
| 103 | + null, FiduceoConstants.MATCHUP_COUNT); |
| 104 | + final IndexIterator iterator = timeArray.getIndexIterator(); |
| 105 | + while (iterator.hasNext()) { |
| 106 | + final int time = iterator.getIntNext(); |
| 107 | + matchupAccumulator.add(time); |
| 108 | + } |
| 109 | + } catch (IOException | InvalidRangeException ioException) { |
| 110 | + fileMessages.add(mmdFile.getFileName().toString(), ioException.getMessage()); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // write report |
| 115 | + final TreeMap<String, Integer> treeMap = new TreeMap<>(matchupAccumulator.getDaysMap()); |
| 116 | + final Set<Map.Entry<String, Integer>> entries = treeMap.entrySet(); |
| 117 | + for (final Map.Entry<String, Integer> entry : entries) { |
| 118 | + System.out.println(entry.getKey() + ": " + entry.getValue()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private List<Path> getInputFiles(String inputDirOption) throws IOException { |
| 123 | + final File inputDir = new File(inputDirOption); |
| 124 | + if (!inputDir.isDirectory()) { |
| 125 | + throw new IOException("Not a valid directory: " + inputDir.getAbsolutePath()); |
| 126 | + } |
| 127 | + |
| 128 | + final Pattern pattern = getMMDFileNamePattern(); |
| 129 | + try (Stream<Path> pathStream = Files.walk(inputDir.toPath())) { |
| 130 | + final Stream<Path> regularFiles = pathStream.filter(Files::isRegularFile); |
| 131 | + final Stream<Path> mmdFileStream = regularFiles.filter(path -> pattern.matcher(path.getFileName().toString()).matches()); |
| 132 | + return mmdFileStream.collect(Collectors.toList()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + static void printUsageTo(OutputStream outputStream) { |
| 137 | + final String ls = System.lineSeparator(); |
| 138 | + final PrintWriter writer = new PrintWriter(outputStream); |
| 139 | + writer.write("mmd-qc-tool version " + VERSION_NUMBER); |
| 140 | + writer.write(ls + ls); |
| 141 | + |
| 142 | + final HelpFormatter helpFormatter = new HelpFormatter(); |
| 143 | + helpFormatter.printHelp(writer, 120, "matchup-tool <options>", "Valid options are:", |
| 144 | + getOptions(), 3, 3, ""); |
| 145 | + |
| 146 | + writer.flush(); |
| 147 | + } |
| 148 | +} |
0 commit comments