diff --git a/SDSLiteTests/NetCDFTests.cs b/SDSLiteTests/NetCDFTests.cs index 5937215..7156de6 100644 --- a/SDSLiteTests/NetCDFTests.cs +++ b/SDSLiteTests/NetCDFTests.cs @@ -38,5 +38,44 @@ public void Test_inq_libvers() Assert.IsNotNull(ver); Assert.IsTrue(ver.StartsWith("4.")); } + + [Test] + public void TestCreateFixedDimensionNC() + { + // create temporary file. + string file = System.IO.Path.GetRandomFileName() + ".nc"; + var dataset = (NetCDFDataSet)DataSet.Open(file); + + dataset.CreateDimension("lat", 3); + dataset.CreateDimension("lon", 2); + + + var lat = dataset.AddVariable("lat", new double[] { 40, 45, 50 }, "lat"); + lat.Metadata["long_name"] = "Latitude"; + lat.Metadata["actual_range"] = "40.0f, 50.0f"; + lat.Metadata["standard_name"] = "latitude"; + lat.Metadata["units"] = "degrees_north"; + lat.Metadata["axis"] = "Y"; + + + var lon = dataset.AddVariable("lon", new double[] { 130, 140 }, "lon"); + lon.Metadata["long_name"] = "Longitude"; + lon.Metadata["actual_range"] = "130.0f, 140.0f"; + lon.Metadata["units"] = "degrees_east"; + lon.Metadata["standard_name"] = "longitude"; + lon.Metadata["axis"] = "X"; + + dataset.Commit(); + + var value = dataset.AddVariable("value", "lat", "lon"); + value.Metadata["units"] = "m s-1"; + value.Metadata["original_units"] = "m s-1"; + value.Metadata["standard_name"] = "val"; + value.MissingValue = -1.0; + + dataset.Variables["value"].PutData(new double[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } }); + dataset.Dispose(); + System.IO.File.Delete(file); + } } } diff --git a/ScientificDataSet/Providers/NetCDF/NetCDFDataSet.cs b/ScientificDataSet/Providers/NetCDF/NetCDFDataSet.cs index 2938207..26e2588 100644 --- a/ScientificDataSet/Providers/NetCDF/NetCDFDataSet.cs +++ b/ScientificDataSet/Providers/NetCDF/NetCDFDataSet.cs @@ -1008,9 +1008,30 @@ public void SetChunkSizes(int[] sizes) chunkSizes = sizes; } - internal int[] ChunkSizes { get { return chunkSizes; } } - + internal int[] ChunkSizes { get { return chunkSizes; } } + #endregion + + /// + /// Create a new Dimension of length N (N = 0 unlimited) + /// + public void CreateDimension(string dim, int len) + { + int res; + int id; + res = NetCDF.nc_inq_dimid(this.NcId, dim, out id); + if (res == (int)ResultCode.NC_EBADDIM) + { + StartChanges(); + res = NetCDF.nc_def_dim(this.NcId, dim, new IntPtr(len), out id); + Commit(); + HandleResult(res); + } + else + { + HandleResult(res); + } + } } internal class AttributeTypeMap diff --git a/ScientificDataSet/Providers/NetCDF/NetCdfVariable.cs b/ScientificDataSet/Providers/NetCDF/NetCdfVariable.cs index 15d88fc..c9167af 100644 --- a/ScientificDataSet/Providers/NetCDF/NetCdfVariable.cs +++ b/ScientificDataSet/Providers/NetCDF/NetCdfVariable.cs @@ -124,13 +124,18 @@ internal static NetCdfVariable Create(NetCDFDataSet dataSet, string na /* Getting exsting dims and creating new */ int[] dimids = new int[dims.Length]; + int[] dimlen = new int[dims.Length]; for (int i = 0; i < dims.Length; i++) { int id; res = NetCDF.nc_inq_dimid(dataSet.NcId, dims[i], out id); if (res == (int)ResultCode.NC_NOERR) - { + { + IntPtr lenp; + res = NetCDF.nc_inq_dimlen(dataSet.NcId, id, out lenp); + NetCDFDataSet.HandleResult(res); dimids[i] = id; + dimlen[i] = lenp.ToInt32(); } else if (res == (int)ResultCode.NC_EBADDIM) { @@ -138,6 +143,7 @@ internal static NetCdfVariable Create(NetCDFDataSet dataSet, string na res = NetCDF.nc_def_dim(dataSet.NcId, dims[i], new IntPtr(NcConst.NC_UNLIMITED), out id); NetCDFDataSet.HandleResult(res); dimids[i] = id; + dimlen[i] = NcConst.NC_UNLIMITED; } else { @@ -197,7 +203,14 @@ internal static NetCdfVariable Create(NetCDFDataSet dataSet, string na for (int i = 0; i < dims.Length; i++) { - chunksizes[i] = new IntPtr(chunk); + if (dimlen[i] == NcConst.NC_UNLIMITED) + { + chunksizes[i] = new IntPtr(chunk); + } + else + { + chunksizes[i] = new IntPtr(dimlen[i]); + } } }