Commit 22832f74 authored by Matija Obreza's avatar Matija Obreza

Updated geo tile index calculation and linking with worldclim.org data

parent ea6d6353
......@@ -31,7 +31,6 @@ import javax.persistence.PersistenceContext;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.lang3.ObjectUtils;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
......@@ -49,7 +48,6 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.QAccessionId;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionGeoRepository;
......
......@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.genesys.worldclim.WorldClimUtil;
import org.genesys.worldclim.grid.generic.Header;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.impl.TileClimate;
......@@ -73,24 +74,36 @@ public class ClimateDataServiceImpl implements ClimateDataService {
Map<Long, TileClimate> rowMap = findOrMakeRows(tileIndexes);
for (final Long tileIndex : tileIndexes) {
TileClimate tileClimate = rowMap.get(tileIndex);
for (final Long genesysTileIndex : tileIndexes) {
if (genesysTileIndex == null) {
continue;
}
TileClimate tileClimate = rowMap.get(genesysTileIndex);
if (tileClimate == null) {
LOG.warn("No TileClimate for {}", genesysTileIndex);
continue;
}
Long bilTileIndex = WorldClimUtil.tileIndexToDataIndex(genesysTileIndex);
if (tileIndex * 2 > buffer.capacity() - 2) {
LOG.info("OUT OF FILE tile={}", tileIndex);
rowMap.remove(tileIndex);
if (bilTileIndex == null) {
LOG.debug("Antarctica not covered");
rowMap.remove(genesysTileIndex);
} else if (bilTileIndex * 2 > buffer.capacity() - 2) {
LOG.info("OUT OF FILE tile={}", bilTileIndex);
rowMap.remove(genesysTileIndex);
} else {
try {
short val = buffer.getShort((int) (tileIndex * 2));
short val = buffer.getShort((int) (bilTileIndex * 2));
if (val != nullValue) {
LOG.trace("tile={} val={}", tileIndex, val);
LOG.trace("tile={} val={}", bilTileIndex, val);
Object value = null;
if (factor < 1.0) {
// FIXME Find a better way!
Double value = null;
if (factor == 1.0) {
value = new Double((long) (factor * val));
} else {
value = new Double(factor * val);
} else if (factor == 1.0) {
value = new Long((long) (factor * val));
}
try {
......@@ -98,10 +111,12 @@ public class ClimateDataServiceImpl implements ClimateDataService {
} catch (IllegalAccessException | InvocationTargetException e) {
LOG.error("Cannot set TileClimate#{} to {}. {}", variableName, value, e.getMessage());
}
} else {
// don't save in case of NODATA
rowMap.remove(genesysTileIndex);
}
} catch (IndexOutOfBoundsException e) {
LOG.error("OUT OF BOUND tile={} capacity={} limit={}", tileIndex, buffer.capacity(), buffer.limit());
LOG.error("OUT OF BOUND genesysTile={} dataTile={} capacity={} limit={}", genesysTileIndex, bilTileIndex, buffer.capacity(), buffer.limit());
throw e;
}
}
......@@ -125,7 +140,7 @@ public class ClimateDataServiceImpl implements ClimateDataService {
map.put(tile.getTileIndex(), tile);
});
tileIndexes.forEach(tileIndex -> {
if (!map.containsKey(tileIndex)) {
if (tileIndex != null && !map.containsKey(tileIndex)) {
map.put(tileIndex, new TileClimate(tileIndex));
}
});
......
......@@ -239,12 +239,11 @@ public class WorldClimUpdater implements InitializingBean {
if (tileIndex != null) {
geo.setTileIndex(tileIndex);
toSave.add(geo);
tileIndexSet.add(tileIndex);
} else if (geo.getTileIndex() != null) {
geo.setTileIndex(null);
toSave.add(geo);
}
toSave.add(geo);
}
if (toSave.size() > 0) {
LOG.info("Updating {} accesssions' tileIndex at position {} of {}", toSave.size(), startIndex, accessionIds.size());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment