*/
final GridSampleDimension[] result = new GridSampleDimension[numBands];
final Category[] categoryXS = new Category[bandLists.length];
final Unit<?>[] unitXS = new Unit[bandLists.length];
while (--numBands >= 0) {
GridSampleDimension sampleDim = null;
Category[] categoryArray = null;
int indexOfQuantitative = 0;
assert PRIMARY_SOURCE_INDEX == 0; // See comment below.
for (int i=bandLists.length; --i>=0;) {
if (bandLists[i] == null) {
continue;
}
/*
* Iterates among all sources (i) for the current band. We iterate
* sources in reverse order because the primary source MUST be the
* last one iterated, in order to have proper values for variables
* 'sampleDim', 'categoryArray' and 'indexOfQuantitative' after the
* loop.
*/
final GridSampleDimension[] allBands = bandLists[i];
sampleDim = allBands[allBands.length == 1 ? 0 : numBands];
final List<Category> categories = sampleDim.getCategories();
// GridSampleDimension may contain no categories
if (categories == null || categories.isEmpty()) {
result[numBands] = sampleDim;
continue;
}
categoryArray = (Category[]) categories.toArray();
indexOfQuantitative = getQuantitative(categoryArray);
if (indexOfQuantitative < 0) {
return null;
}
unitXS [i] = sampleDim.getUnits();
categoryXS[i] = categoryArray[indexOfQuantitative];
}
if (categoryArray == null) {
continue;
}
final Category oldCategory = categoryArray[indexOfQuantitative];
final Unit<?> oldUnit = sampleDim.getUnits();
final Category newCategory = deriveCategory(categoryXS, parameters);
final Unit<?> newUnit = deriveUnit(unitXS, parameters);
if (newCategory == null) {
return null;
}
if (!oldCategory.equals(newCategory) || !Utilities.equals(oldUnit, newUnit)) {
/*
* Create a new sample dimension. Note that we use a null title, not the same
* title than the original sample dimension, because the new sample dimension
* may be quite different. For example the original sample dimension may be
* about "Temperature" in °C units, and the new one about "Gradiant magnitude
* of Temperature" in °C/km units. The GridSampleDimension constructor will
* infers the title from what looks like the "main" category.
*/
final CharSequence title = null;
categoryArray[indexOfQuantitative] = newCategory;
result[numBands] = new GridSampleDimension(title, categoryArray, newUnit);
} else {
// Reuse the category list from the primary source.
result[numBands] = sampleDim;
}
}