|
33 | 33 | package ucar.nc2;
|
34 | 34 |
|
35 | 35 | import java.io.*;
|
36 |
| - |
| 36 | +import java.util.HashMap; |
37 | 37 | import ucar.ma2.Array;
|
| 38 | +import ucar.ma2.InvalidRangeException; |
38 | 39 | import ucar.nc2.constants.CDM;
|
39 | 40 |
|
40 | 41 | /**
|
@@ -106,4 +107,158 @@ static public boolean close( float d1, float d2) {
|
106 | 107 | else
|
107 | 108 | return Math.abs(d1-d2) < 1.0e-5;
|
108 | 109 | }
|
| 110 | + |
| 111 | + /** |
| 112 | + * Clone metadata from the given netcdf to the given netcdf file writer. |
| 113 | + * |
| 114 | + * @param fromNetcdf the cloned netcdf |
| 115 | + * @param toWriter the netcdf file writer |
| 116 | + */ |
| 117 | + public static void cloneMetadata(NetcdfFile fromNetcdf, NetcdfFileWriter toWriter) { |
| 118 | + |
| 119 | + final Group rootFrom = fromNetcdf.getRootGroup(); |
| 120 | + final Group rootTo = toWriter.addGroup(null, null); |
| 121 | + |
| 122 | + cloneMetadata(toWriter, rootFrom, rootTo); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Clone data from the given netcdf to the given netcdf file writer. |
| 127 | + * |
| 128 | + * @param fromNetcdf the cloned netcdf |
| 129 | + * @param toWriter the netcdf file writer |
| 130 | + * @throws IOException |
| 131 | + * @throws InvalidRangeException |
| 132 | + */ |
| 133 | + public static void cloneData(NetcdfFile fromNetcdf, NetcdfFileWriter toWriter) |
| 134 | + throws IOException, InvalidRangeException { |
| 135 | + |
| 136 | + final Group rootFrom = fromNetcdf.getRootGroup(); |
| 137 | + final Group rootTo = toWriter.getNetcdfFile().getRootGroup(); |
| 138 | + |
| 139 | + cloneData(toWriter, rootFrom, rootTo); |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Clone metadata from the given group to the given group of the netcdf file writer. |
| 145 | + * |
| 146 | + * @param to the netcdf file writer |
| 147 | + * @param groupFrom the cloned metadata group |
| 148 | + * @param groupTo the written group |
| 149 | + */ |
| 150 | + private static void cloneMetadata(final NetcdfFileWriter to, final Group groupFrom, final Group groupTo) { |
| 151 | + |
| 152 | + // dimension |
| 153 | + cloneMetadataDimension(to, groupFrom, groupTo); |
| 154 | + |
| 155 | + // attribute |
| 156 | + cloneMetadataAttribute(to, groupFrom, groupTo); |
| 157 | + |
| 158 | + // variable |
| 159 | + cloneMetadataVariable(to, groupFrom, groupTo); |
| 160 | + |
| 161 | + // enum |
| 162 | + cloneMetadataEnum(to, groupFrom, groupTo); |
| 163 | + |
| 164 | + for (final Group gFrom : groupFrom.getGroups()) { |
| 165 | + // create new Group |
| 166 | + final Group g = to.addGroup(groupTo, gFrom.getShortName()); |
| 167 | + cloneMetadata(to, gFrom, g); |
| 168 | + |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Clone metadata dimension. |
| 174 | + * |
| 175 | + * @param to the netcdf file writer |
| 176 | + * @param groupFrom the cloned metadata dimension group |
| 177 | + * @param groupTo the written group |
| 178 | + */ |
| 179 | + private static void cloneMetadataDimension(final NetcdfFileWriter to, final Group groupFrom, final Group groupTo) { |
| 180 | + for (final Dimension dimension : groupFrom.getDimensions()) { |
| 181 | + if (!dimension.isVariableLength()) { |
| 182 | + to.addDimension(groupTo, dimension.getShortName(), dimension.getLength(), dimension.isShared(), |
| 183 | + dimension.isUnlimited(), dimension.isVariableLength()); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Clone metadata attribute. |
| 190 | + * |
| 191 | + * @param to the netcdf file writer |
| 192 | + * @param groupFrom the cloned metadata attribute group |
| 193 | + * @param groupTo the written group |
| 194 | + */ |
| 195 | + private static void cloneMetadataAttribute(final NetcdfFileWriter to, final Group groupFrom, final Group groupTo) { |
| 196 | + for (final Attribute attribute : groupFrom.getAttributes()) { |
| 197 | + final Attribute att = new Attribute(attribute.getShortName(), attribute); |
| 198 | + to.addGroupAttribute(groupTo, att); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Clone metadata variable. |
| 204 | + * |
| 205 | + * @param to the netcdf file writer |
| 206 | + * @param groupFrom the cloned metadata variable group |
| 207 | + * @param groupTo the written group |
| 208 | + */ |
| 209 | + private static void cloneMetadataVariable(final NetcdfFileWriter to, final Group groupFrom, final Group groupTo) { |
| 210 | + for (final Variable variable : groupFrom.getVariables()) { |
| 211 | + |
| 212 | + final String parsedDim = variable.getDimensionsString(); |
| 213 | + final Variable v = to.addVariable(groupTo, variable.getShortName(), variable.getDataType(), parsedDim); |
| 214 | + |
| 215 | + for (final Attribute attribute : variable.getAttributes()) { |
| 216 | + final Attribute att = new Attribute(attribute.getShortName(), attribute); |
| 217 | + v.addAttribute(att); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Clone metadata enum. |
| 225 | + * |
| 226 | + * @param to the netcdf file writer |
| 227 | + * @param groupFrom the cloned metadata enum group |
| 228 | + * @param groupTo the written group |
| 229 | + */ |
| 230 | + private static void cloneMetadataEnum(final NetcdfFileWriter to, final Group groupFrom, final Group groupTo) { |
| 231 | + for (final EnumTypedef enumTypedef : groupFrom.getEnumTypedefs()) { |
| 232 | + final EnumTypedef e = |
| 233 | + new EnumTypedef(enumTypedef.getShortName(), new HashMap<>(enumTypedef.getMap()), enumTypedef.getBaseType()); |
| 234 | + to.addTypedef(groupTo, e); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Clone data. |
| 240 | + * |
| 241 | + * @param to the netcdf file writer |
| 242 | + * @param groupFrom the cloned metadata enum group |
| 243 | + * @param groupTo the written group |
| 244 | + * @throws IOException |
| 245 | + * @throws InvalidRangeException |
| 246 | + */ |
| 247 | + private static void cloneData(final NetcdfFileWriter to, final Group groupFrom, final Group groupTo) |
| 248 | + throws IOException, InvalidRangeException { |
| 249 | + if (null != groupTo) { |
| 250 | + for (final Variable variableFrom : groupFrom.getVariables()) { |
| 251 | + |
| 252 | + final Variable variableTo = groupTo.findVariable(variableFrom.getShortName()); |
| 253 | + if (null != variableTo) { |
| 254 | + to.write(variableTo, variableFrom.read().copy()); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + for (final Group gFrom : groupFrom.getGroups()) { |
| 259 | + final Group gTo = groupTo.findGroup(gFrom.getShortName()); |
| 260 | + cloneData(to, gFrom, gTo); |
| 261 | + } |
| 262 | + } |
| 263 | + } |
109 | 264 | }
|
0 commit comments