NcFileStitcher { static void setGlobalAttributes(SlstrNFileWritable nFileWriteable, List<Attribute>[] globalAttributeLists, String parentDirectoryName, Date creationDate) throws IOException { final DateFormatConverter globalAttributesDateFormatConverter = new DateFormatConverter(new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'")); List<String> namesOfAddedAttributes = new ArrayList<>(); for (int i = 0; i < globalAttributeLists.length; i++) { List<Attribute> globalAttributes = globalAttributeLists[i]; for (Attribute globalAttribute : globalAttributes) { final String globalAttributeName = globalAttribute.getFullName(); if (!namesOfAddedAttributes.contains(globalAttributeName)) { if (globalAttributeName.equals(PRODUCT_NAME)) { nFileWriteable.addGlobalAttribute(PRODUCT_NAME, parentDirectoryName); } else if (globalAttributeName.equals(CREATION_TIME)) { nFileWriteable.addGlobalAttribute(CREATION_TIME, globalAttributesDateFormatConverter.format(creationDate)); } else if (globalAttribute.isArray()) { final Array values = globalAttribute.getValues(); for (int j = i; j < globalAttributeLists.length; j++) { final Attribute otherGlobalAttribute = getAttributeFromList(globalAttributeName, globalAttributeLists[j]); if (otherGlobalAttribute != null && values != otherGlobalAttribute.getValues() && !otherGlobalAttribute.getFullName().equals(globalAttribute.getFullName())) { nFileWriteable.addGlobalAttribute(globalAttributeName + "_" + j, otherGlobalAttribute.getValues().toString()); break; } } nFileWriteable.addGlobalAttribute(globalAttributeName, values.toString()); } else if (globalAttribute.getDataType().isNumeric()) { final Number value = globalAttribute.getNumericValue(); for (int j = i; j < globalAttributeLists.length; j++) { final Attribute otherGlobalAttribute = getAttributeFromList(globalAttributeName, globalAttributeLists[j]); if (otherGlobalAttribute != null && !value.equals(otherGlobalAttribute.getNumericValue())) { nFileWriteable.addGlobalAttribute(globalAttributeName + "_" + j, otherGlobalAttribute.getNumericValue()); break; } } nFileWriteable.addGlobalAttribute(globalAttributeName, value.toString()); } else { final String value = globalAttribute.getStringValue(); for (int j = i; j < globalAttributeLists.length; j++) { final Attribute otherGlobalAttribute = getAttributeFromList(globalAttributeName, globalAttributeLists[j]); if ((otherGlobalAttribute != null && otherGlobalAttribute.getStringValue() != null)) { if (!value.equals(otherGlobalAttribute.getStringValue())) { nFileWriteable.addGlobalAttribute(globalAttributeName + "_" + j, otherGlobalAttribute.getStringValue()); break; } } } if (value != null) { nFileWriteable.addGlobalAttribute(globalAttributeName, value); } } } namesOfAddedAttributes.add(globalAttributeName); } } }    }
@Test public void testSetGlobalAttributes() throws IOException { final File file = new File(targetDirectory, "something.nc"); SlstrNFileWritable netcdfWriteable = new SlstrNFileWritable(file.getAbsolutePath()); List<Attribute>[] attributeLists = new List[2]; attributeLists[0] = new ArrayList<>(); attributeLists[0].add(new Attribute("xyz", "yz")); attributeLists[0].add(new Attribute("abc", "23")); final ArrayByte someArray = new ArrayByte(new int[]{2}, true); someArray.setByte(0, (byte) 5); someArray.setByte(0, (byte) 5); attributeLists[0].add(new Attribute("def", someArray)); attributeLists[1] = new ArrayList<>(); attributeLists[1].add(new Attribute("xyz", "yz")); attributeLists[1].add(new Attribute("abc", "44")); attributeLists[1].add(new Attribute("defg", someArray)); final DateFormatConverter globalAttributesDateFormatConverter = new DateFormatConverter(new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'")); final String someTimeAsString = globalAttributesDateFormatConverter.format(new GregorianCalendar(2013, 6, 7, 15, 32, 52).getTime()); attributeLists[1].add(new Attribute("creation_time", someTimeAsString)); final Date now = Calendar.getInstance().getTime(); final String nowAsString = globalAttributesDateFormatConverter.format(now); NcFileStitcher.setGlobalAttributes(netcdfWriteable, attributeLists, targetDirectory.getName(), now); netcdfWriteable.create(); netcdfWriteable.close(); netcdfFile = NetcdfFileOpener.open(file); assertNotNull(netcdfFile); final List<Attribute> globalAttributes = netcdfFile.getGlobalAttributes(); assertEquals(7, globalAttributes.size()); assertEquals("xyz", globalAttributes.get(0).getFullName()); assertEquals("yz", globalAttributes.get(0).getStringValue()); assertEquals("abc_1", globalAttributes.get(1).getFullName()); assertEquals("44", globalAttributes.get(1).getStringValue()); assertEquals("abc", globalAttributes.get(2).getFullName()); assertEquals("23", globalAttributes.get(2).getStringValue()); assertEquals("def", globalAttributes.get(3).getFullName()); assertEquals(someArray.toString(), globalAttributes.get(3).getStringValue()); assertEquals("defg", globalAttributes.get(4).getFullName()); assertEquals(someArray.toString(), globalAttributes.get(4).getStringValue()); assertEquals("creation_time", globalAttributes.get(5).getFullName()); assertEquals(nowAsString, globalAttributes.get(5).getStringValue()); }