[netcdfgroup] Hi..Anybody uses NetCdf in C#?

Hi.

I am still investigating the problem that using either NetCDF 4.1.1 or
NetCDF 4.1.2 in Windows crashes.
Below write I these crashing code. Can anyone test to see if the exception
is thrown?
Appreciate any comments about mine abuse......

With the following code, it is able to create NetCDF file and add new
attribute 'History', while it throws exception when you open and close the
newly created file again.

namespace NetCDFCrashingCode
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            string path = @"C:\data_11.nc";
            //NetCDFInterop.nc_create(MarshalString(path),
(int)CreateMode.NC_NETCDF4, out i);
            NetCDFInterop.nc_create(path, CreateMode.NC_NETCDF4, out i);
            NetCDFInterop.nc_redef(i);
            NetCDFInterop.nc_put_att_string(i, -1, MarshalString("history"),
new IntPtr(1), MarshalStringArray(new String[] { "his" }));
            NetCDFInterop.nc_enddef(i);
            NetCDFInterop.nc_sync(i);
            NetCDFInterop.nc_close(i);
            NetCDFInterop.nc_open(@"C:\data_11.nc", CreateMode.NC_WRITE, out
i);
            NetCDFInterop.nc_close(i);
        }

        public static IntPtr MarshalString(string str)
        {
            byte[] array = Encoding.UTF8.GetBytes(str);
            int size = Marshal.SizeOf(typeof(byte)) * (array.Length + 1);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.Copy(array, 0, ptr, array.Length);
            Marshal.WriteByte(ptr, array.Length, 0);
            return ptr;
        }

        public static IntPtr MarshalStringArray(string[] strArr)
        {
            int n = strArr.Length;
            int sz = Marshal.SizeOf(typeof(byte*));
            IntPtr ptrA = Marshal.AllocHGlobal(sz * n);
            for (int i = 0; i < n; i++)
            {
                Marshal.WriteIntPtr(ptrA, sz * i, MarshalString(strArr[i]));
            }
            return ptrA;
        }
    }

    public enum CreateMode : int
    {
        NC_NOWRITE = 0,
        /// <summary>read &amp; write</summary>
        NC_WRITE = 0x0001,
        NC_CLOBBER = 0,
        /// <summary>Don't destroy existing file on create</summary>
        NC_NOCLOBBER = 0x0004,
        /// <summary>argument to ncsetfill to clear NC_NOFILL</summary>
        NC_FILL = 0,
        /// <summary>Don't fill data section an records</summary>
        NC_NOFILL = 0x0100,
        /// <summary>Use locking if available</summary>
        NC_LOCK = 0x0400,
        /// <summary>Share updates, limit cacheing</summary>
        NC_SHARE = 0x0800,
        NC_64BIT_OFFSET = 0x0200,
        /// <summary>Enforce strict netcdf-3 rules</summary>
        NC_CLASSIC = 0x0100,
        /// <summary>causes netCDF to create a HDF5/NetCDF-4 file</summary>
        NC_NETCDF4 = 0x1000
    }

    public static class NetCDFInterop
    {
        [DllImport("netcdf4.dll")]
        public static extern int nc_create(string path, CreateMode cmode,
out int ncidp);
        //public static extern int nc_create(IntPtr path, int cmode, out int
ncidp);

        [DllImport("netcdf4.dll")]
        public static extern int nc_open(string path, CreateMode omode, out
int ncidp);
        [DllImport("netcdf4.dll")]
        public static extern int nc_close(int ncidp);
        [DllImport("netcdf4.dll")]
        public static extern int nc_sync(int ncid);
        [DllImport("netcdf4.dll")]
        public static extern int nc_enddef(int ncid);
        [DllImport("netcdf4.dll")]
        public static extern int nc_redef(int ncid);

        [DllImport("netcdf4.dll")]
        public static extern int nc_put_att_string(int ncid, int varid,
            IntPtr name,
            IntPtr len,
            IntPtr op);
    }
}
  • 2011 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: