|
| 1 | +package com.bc.fiduceo.reader.insitu.generic; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.databind.PropertyNamingStrategies; |
| 5 | +import org.esa.snap.core.datamodel.ProductData; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class CsvFormatConfig { |
| 13 | + |
| 14 | + private String name; |
| 15 | + private String delimiter; |
| 16 | + private char commentChar; |
| 17 | + private String regex; |
| 18 | + private String longitudeName; |
| 19 | + private String latitudeName; |
| 20 | + private String timeName = "time"; |
| 21 | + private String timeFormat; |
| 22 | + private List<String> timeVars; |
| 23 | + private boolean locationFromStationDatabase; |
| 24 | + private List<GenericVariable> variables; |
| 25 | + private StationDatabase stationDatabase; |
| 26 | + |
| 27 | + public static CsvFormatConfig loadConfig(String resourceKey) { |
| 28 | + ObjectMapper mapper = new ObjectMapper(); |
| 29 | + mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); |
| 30 | + String path = resourceKey + "_config.json"; |
| 31 | + |
| 32 | + try (InputStream in = CsvFormatConfig.class.getResourceAsStream(path)) { |
| 33 | + if (in == null) { |
| 34 | + throw new RuntimeException("Resource not found: " + path); |
| 35 | + } |
| 36 | + |
| 37 | + return mapper.readValue(in, CsvFormatConfig.class); |
| 38 | + } catch (IOException e) { |
| 39 | + throw new RuntimeException("Failed to load config from: " + path, e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public List<GenericVariable> getAllVariables() { |
| 44 | + List<GenericVariable> vars = new ArrayList<>(); |
| 45 | + |
| 46 | + if (stationDatabase != null) { |
| 47 | + vars.addAll(stationDatabase.getVariables()); |
| 48 | + } |
| 49 | + |
| 50 | + if (hasTimeVars()) { |
| 51 | + GenericVariable time = new GenericVariable(timeName, 'v',"int", ProductData.TYPE_INT64, -1.0, "seconds since 1970-01-01", null, "time", null); |
| 52 | + vars.add(time); |
| 53 | + |
| 54 | + for (GenericVariable var : variables) { |
| 55 | + if (!timeVars.contains(var.getName())) { |
| 56 | + vars.add(var); |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + vars.addAll(variables); |
| 61 | + } |
| 62 | + |
| 63 | + return vars; |
| 64 | + } |
| 65 | + |
| 66 | + private boolean hasTimeVars() { |
| 67 | + return timeVars != null && !timeVars.isEmpty(); |
| 68 | + } |
| 69 | + |
| 70 | + public String getName() { |
| 71 | + return name; |
| 72 | + } |
| 73 | + |
| 74 | + public void setName(String name) { |
| 75 | + this.name = name; |
| 76 | + } |
| 77 | + |
| 78 | + public String getDelimiter() { |
| 79 | + return delimiter; |
| 80 | + } |
| 81 | + |
| 82 | + public void setDelimiter(String delimiter) { |
| 83 | + this.delimiter = delimiter; |
| 84 | + } |
| 85 | + |
| 86 | + public char getCommentChar() { |
| 87 | + return commentChar; |
| 88 | + } |
| 89 | + |
| 90 | + public void setCommentChar(char commentChar) { |
| 91 | + this.commentChar = commentChar; |
| 92 | + } |
| 93 | + |
| 94 | + public String getRegex() { |
| 95 | + return regex; |
| 96 | + } |
| 97 | + |
| 98 | + public void setRegex(String regex) { |
| 99 | + this.regex = regex; |
| 100 | + } |
| 101 | + |
| 102 | + public String getLongitudeName() { |
| 103 | + return longitudeName; |
| 104 | + } |
| 105 | + |
| 106 | + public void setLongitudeName(String longitudeName) { |
| 107 | + this.longitudeName = longitudeName; |
| 108 | + } |
| 109 | + |
| 110 | + public String getLatitudeName() { |
| 111 | + return latitudeName; |
| 112 | + } |
| 113 | + |
| 114 | + public void setLatitudeName(String latitudeName) { |
| 115 | + this.latitudeName = latitudeName; |
| 116 | + } |
| 117 | + |
| 118 | + public String getTimeName() { |
| 119 | + return timeName; |
| 120 | + } |
| 121 | + |
| 122 | + public void setTimeName(String timeName) { |
| 123 | + this.timeName = timeName; |
| 124 | + } |
| 125 | + |
| 126 | + public String getTimeFormat() { |
| 127 | + return timeFormat; |
| 128 | + } |
| 129 | + |
| 130 | + public void setTimeFormat(String timeFormat) { |
| 131 | + this.timeFormat = timeFormat; |
| 132 | + } |
| 133 | + |
| 134 | + public List<String> getTimeVars() { |
| 135 | + return timeVars; |
| 136 | + } |
| 137 | + |
| 138 | + public void setTimeVars(List<String> timeVars) { |
| 139 | + this.timeVars = timeVars; |
| 140 | + } |
| 141 | + |
| 142 | + public boolean isLocationFromStationDatabase() { |
| 143 | + return locationFromStationDatabase; |
| 144 | + } |
| 145 | + |
| 146 | + public void setLocationFromStationDatabase(boolean locationFromStationDatabase) { |
| 147 | + this.locationFromStationDatabase = locationFromStationDatabase; |
| 148 | + } |
| 149 | + |
| 150 | + public List<GenericVariable> getVariables() { |
| 151 | + return variables; |
| 152 | + } |
| 153 | + |
| 154 | + public void setVariables(List<GenericVariable> variables) { |
| 155 | + this.variables = variables; |
| 156 | + } |
| 157 | + |
| 158 | + public StationDatabase getStationDatabase() { |
| 159 | + return stationDatabase; |
| 160 | + } |
| 161 | + |
| 162 | + public void setStationDatabase(StationDatabase stationDatabase) { |
| 163 | + this.stationDatabase = stationDatabase; |
| 164 | + } |
| 165 | +} |
0 commit comments