Skip to content

Commit fc17bf2

Browse files
committed
split channel attributes for Modis
1 parent 4d4970c commit fc17bf2

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

core/src/main/java/com/bc/fiduceo/reader/modis/MxD021KM_Reader.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
import com.bc.fiduceo.reader.time.TimeLocator;
1717
import com.bc.fiduceo.reader.time.TimeLocator_TAI1993Scan;
1818
import com.bc.fiduceo.util.NetCDFUtils;
19-
import com.bc.fiduceo.util.TimeUtils;
20-
import ucar.ma2.Array;
21-
import ucar.ma2.ArrayInt;
22-
import ucar.ma2.InvalidRangeException;
23-
import ucar.ma2.MAMath;
19+
import org.esa.snap.core.util.StringUtils;
20+
import ucar.ma2.*;
21+
import ucar.ma2.DataType;
2422
import ucar.nc2.Attribute;
23+
import ucar.nc2.AttributeContainer;
2524
import ucar.nc2.Structure;
2625
import ucar.nc2.Variable;
2726

@@ -31,8 +30,6 @@
3130
import java.nio.file.Files;
3231
import java.nio.file.Path;
3332
import java.util.ArrayList;
34-
import java.util.Calendar;
35-
import java.util.Date;
3633
import java.util.List;
3734

3835
import static com.bc.fiduceo.reader.modis.ModisConstants.LATITUDE_VAR_NAME;
@@ -483,4 +480,44 @@ private void injectThermalNoiseVariables() throws IOException {
483480
arrayCache.inject(new ThermalNoiseVariable(noiseVariable, i, productSize.getNy()));
484481
}
485482
}
483+
484+
@SuppressWarnings("ConstantConditions")
485+
@Override
486+
protected void splitAttributes(Variable channelVariable, int index, int numChannels) {
487+
final ArrayList<Attribute> toRemove = new ArrayList<>();
488+
final ArrayList<Attribute> toAdd = new ArrayList<>();
489+
490+
final AttributeContainer attributes = channelVariable.attributes();
491+
for (Attribute attribute : attributes) {
492+
toRemove.add(attribute);
493+
494+
final DataType dataType = attribute.getDataType();
495+
if (dataType.isString()) {
496+
final String stringValue = attribute.getStringValue();
497+
final String[] tokens = StringUtils.split(stringValue, new char[]{','}, true);
498+
if (tokens.length == numChannels) {
499+
final Attribute newAttribute = new Attribute(attribute.getShortName(), tokens[index]);
500+
toAdd.add(newAttribute);
501+
} else {
502+
toAdd.add(attribute);
503+
}
504+
} else if (dataType.isNumeric()) {
505+
final int length = attribute.getLength();
506+
if (length == numChannels) {
507+
final Number numericValue = attribute.getNumericValue(index);
508+
final Attribute newAttribute = new Attribute(attribute.getShortName(), numericValue);
509+
toAdd.add(newAttribute);
510+
} else {
511+
toAdd.add(attribute);
512+
}
513+
}
514+
}
515+
516+
for (Attribute attribute : toRemove) {
517+
attributes.remove(attribute);
518+
}
519+
for (Attribute attribute : toAdd) {
520+
attributes.addAttribute(attribute);
521+
}
522+
}
486523
}

core/src/main/java/com/bc/fiduceo/reader/netcdf/NetCDFReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,16 @@ private void addChannelVariables(List<Variable> result, Variable variable, int n
150150
final Variable channelVariable = variable.section(section);
151151
final String channelVariableName = variableBaseName + layerExtension.getExtension(channel);
152152
channelVariable.setName(channelVariableName);
153+
splitAttributes(channelVariable, channel, numChannels);
153154
result.add(channelVariable);
154155
origin[channel_dimension_index]++;
155156
}
156157
}
157158

159+
protected void splitAttributes(Variable channelVariable, int index, int numChannels) {
160+
// nothing to do here. If required, readers may override this method tb 2020-11-25
161+
}
162+
158163
protected Array acquisitionTimeFromTimeLocator(int y, Interval interval) throws IOException {
159164
final int height = interval.getY();
160165
final int width = interval.getX();

core/src/test/java/com/bc/fiduceo/reader/modis/MxD021KM_Reader_IO_Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,22 @@ public void testGetVariables_Terra() throws IOException, InvalidRangeException {
204204
variable = variables.get(18);
205205
assertEquals("EV_1KM_RefSB_ch10", variable.getShortName());
206206
assertEquals(DataType.USHORT, variable.getDataType());
207+
NCTestUtils.assertAttribute(variable, "radiance_scales", "0.0036217605");
207208

208209
variable = variables.get(34);
209210
assertEquals("EV_1KM_RefSB_Uncert_Indexes_ch11", variable.getShortName());
210211
assertEquals(DataType.UBYTE, variable.getDataType());
212+
NCTestUtils.assertAttribute(variable, "specified_uncertainty", "1.5");
211213

212214
variable = variables.get(48);
213215
assertEquals("EV_1KM_Emissive_ch22", variable.getShortName());
214216
assertEquals(DataType.USHORT, variable.getDataType());
217+
NCTestUtils.assertAttribute(variable, "band_names", "22");
215218

216219
variable = variables.get(65);
217220
assertEquals("EV_1KM_Emissive_Uncert_Indexes_ch23", variable.getShortName());
218221
assertEquals(DataType.UBYTE, variable.getDataType());
222+
NCTestUtils.assertAttribute(variable, "scaling_factor", "4.0");
219223

220224
variable = variables.get(69);
221225
assertEquals("EV_1KM_Emissive_Uncert_Indexes_ch28", variable.getShortName());

0 commit comments

Comments
 (0)