SDSlite icon indicating copy to clipboard operation
SDSlite copied to clipboard

Failed to create netcdf classic dataset

Open AIMakinde opened this issue 10 months ago • 0 comments

Hi, First, I wish to use this moment to say a big thank you to the team that has put together this sort-after scientific library useful for interacting with netCDF in C#.

I wrote the code below to create a new netcdf data using the classic netcdf library, that is, netcdf without hdf5. However, I got this error "NetCDF4 error" on the first line of using (DataSet ds = DataSet.Open($"msds:nc?file={outputnc}&openMode=createNew")). After tracing the error, I realised it was a netcdf error number -128 which means Attempt to use a feature that was not turned on when netCDF was built.

So I have two questions:

  1. Why is the SDSlite not working with my netCDF whereas I have other programs, utilities and packages that use the same netCDF on my machine without any issue?
  2. Let us assume I install a dedicated netCDF library for use with SDSLite only, how could I configure the Dataset for a specific netCDF.dll path?

`public static void TestNetCDF() { // Output filename var outputnc = "my_3d_netcdf.nc";

// Define dimensions
int latSize = 10; // Number of latitude points
int lonSize = 20; // Number of longitude points
int timeSize = 5; // Number of time steps



// Create a new NetCDF file
using (DataSet ds = DataSet.Open($"msds:nc?file={outputnc}&openMode=createNew"))
{
    // Generate dimension values
    float[] lats = Enumerable.Range(0, latSize).Select(i => 0.0f + (i * 0.25f)).ToArray();
    float[] lons = Enumerable.Range(0, lonSize).Select(i => 0.0f + (i * 0.25f)).ToArray();
    DateTime[] times = Enumerable.Range(0, timeSize).Select(d => DateTime.Now.AddDays(d)).ToArray();


    // Define dimensions
    var lat_dim = ds.AddAxis<float[]>("lat", "degrees north", lats);
    var lon_dim = ds.AddAxis<float[]>("lon", "degrees east", lons);
    var time_dim = ds.AddAxis<DateTime[]>("time", "days since 01-01-2024", times, true);
    time_dim.Metadata["name"] = "Time";
    time_dim.Metadata["long_name"] = "Time";

    // Create a variable (e.g., temperature)
    var temperature = ds.Add<double[,,]>("temperature", ["time", "lat", "lon"]);
    temperature.MissingValue = -1.0e-32d;


    // Generate some sample data (you can replace this with actual data)
    double[,,] data2d;
    for (int t = 0; t < timeSize; t++)
    {
        data2d = new double[1,latSize, lonSize];
        for (int ilat = 0; ilat < latSize; ilat++)
        {
            for (int ilon = 0; ilon < lonSize; ilon++)
            {
                // Assign temperature values (e.g., random values for demonstration)
                data2d[0,ilat, ilon] = (20.0 + 2.0 * (ilat + ilon) + t);
            }
        }

        // Assign temperature values (e.g., random values for demonstration)
        temperature.PutData([t, 0, 0], data2d);
    }

    // Add metadata (optional)
    ds.Metadata["title"] = "My 3D NetCDF Dataset";
    ds.Metadata["institution"] = "My Institution";
    ds.Metadata["comment"] = "Sample 3D dataset with temperature data";

    // Save the dataset
    ds.Commit();
}

Console.WriteLine("NetCDF file created successfully!");

}`

AIMakinde avatar May 02 '24 14:05 AIMakinde