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

Failed to create netcdf classic dataset #50

Open
AIMakinde opened this issue May 2, 2024 · 0 comments
Open

Failed to create netcdf classic dataset #50

AIMakinde opened this issue May 2, 2024 · 0 comments

Comments

@AIMakinde
Copy link

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!");

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant