Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Dimension with fixed length (revised version of #34) #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions SDSLiteTests/NetCDFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>("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<double>("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<double>("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);
}
}
}
25 changes: 23 additions & 2 deletions ScientificDataSet/Providers/NetCDF/NetCDFDataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,30 @@ public void SetChunkSizes(int[] sizes)
chunkSizes = sizes;
}

internal int[] ChunkSizes { get { return chunkSizes; } }

internal int[] ChunkSizes { get { return chunkSizes; } }
#endregion

/// <summary>
/// Create a new Dimension of length N (N = 0 unlimited)
/// </summary>
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
Expand Down
17 changes: 15 additions & 2 deletions ScientificDataSet/Providers/NetCDF/NetCdfVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,26 @@ internal static NetCdfVariable<DataType> 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)
{
// Creating new dimension
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
{
Expand Down Expand Up @@ -197,7 +203,14 @@ internal static NetCdfVariable<DataType> 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]);
}
}
}

Expand Down