Skip to content

Commit

Permalink
Merge pull request #278 from Open-EO/netcdfcollection
Browse files Browse the repository at this point in the history
Netcdfcollection
  • Loading branch information
jdries authored Mar 29, 2024
2 parents d4dd9e9 + 18b8199 commit d302886
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ object NetCDFCollection {
val conventions: String = rs.dataset.getMetadataItem("NC_GLOBAL#Conventions",DefaultDomain,0)
val extraDim = rs.dataset.getMetadataItem("NETCDF_DIM_EXTRA",DefaultDomain,0)

val arr = Array.ofDim[String](1)
val returnValue = GDALWarp.get_metadata_item(rs.dataset.token, 1, 4, 0, "NETCDF_DIM_t_VALUES","",arr)
if(returnValue<=0) {
throw new IllegalArgumentException(s"GDAL Could not retrieve time values from netcdf ${gdalNetCDFLink} with extra dimensions ${extraDim} and units ${units}")
}
val time_values = arr(0)// new String(arr,"UTF-8").trim

if (!conventions.startsWith("CF-1")) {
throw new IllegalArgumentException(s"Only netCDF files with CF-1.x conventions are supported by this openEO backend, but found ${conventions}.")
}
Expand All @@ -79,7 +72,12 @@ object NetCDFCollection {
if( units != "days since 1990-01-01") {
throw new IllegalArgumentException("Only netCDF files with a time dimension in 'days since 1990-01-01' are supported by this openEO backend.")
}
val timestamps = time_values.substring(1,time_values.length-1).split(",").map(t=>{LocalDate.of(1990,1,1).atStartOfDay(ZoneId.of("UTC")).plusDays(t.toInt)})
val bandCount: Int = rs.dataset.bandCount

//there's also a metadata item containing all timesteps, but it doesn't work on cluster for unknown reason
val timeValues = (1 to bandCount).map(b=>{rs.dataset.getMetadataItem("NETCDF_DIM_t",DefaultDomain,b).toInt})

val timestamps = timeValues.map(t=>{LocalDate.of(1990,1,1).atStartOfDay(ZoneId.of("UTC")).plusDays(t)})
val raster: Raster[MultibandTile] = rs.read().get
val temporalRasters: immutable.Seq[(ZonedDateTime, (String, ProjectedExtent, Tile))] = raster.tile.bands.zip(timestamps).map(rasterBand_time=>{
(rasterBand_time._2,(b,ProjectedExtent(raster.extent,rs.crs),rasterBand_time._1))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package org.openeo.geotrellis.layers

import com.azavea.gdal.GDALWarp
import geotrellis.proj4.{CRS, LatLng}
import geotrellis.raster.{CellSize, RasterExtent, ShortConstantNoDataCellType, ShortUserDefinedNoDataCellType}
import geotrellis.spark.util.SparkUtils
import geotrellis.vector.Extent
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.{Disabled, Test}
import org.junit.jupiter.api.{AfterAll, Disabled, Test}
import org.openeo.geotrellis.file.FixedFeaturesOpenSearchClient
import org.openeo.opensearch.OpenSearchResponses.{Feature, Link}

import java.time.ZonedDateTime



object NetCDFCollectionTest {
@AfterAll
def tearDown():Unit = {
GDALWarp.deinit()
GDALWarp.init(20)
}
}

class NetCDFCollectionTest {

@Disabled

@Test
def testLoadNetCDFCollection():Unit = {
val osClient = new FixedFeaturesOpenSearchClient()
Expand Down Expand Up @@ -44,7 +52,7 @@ class NetCDFCollectionTest {
)

val cube = NetCDFCollection.loadCollection(osClient, sc)
assertEquals(333,cube.count())
assertEquals(2331,cube.count())
assertEquals(Extent(603901.4819578232, 5656508.552285681, 653638.1910088382, 5687527.3439567955),cube.metadata.extent)
assertEquals(crs,cube.metadata.crs)
assertEquals(ShortUserDefinedNoDataCellType(32767),cube.metadata.cellType)
Expand Down

0 comments on commit d302886

Please sign in to comment.