Chinaunix首页 | 论坛 | 博客
  • 博客访问: 256004
  • 博文数量: 45
  • 博客积分: 170
  • 博客等级: 入伍新兵
  • 技术积分: 488
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-13 14:43
文章分类

全部博文(45)

文章存档

2014年(2)

2013年(35)

2012年(8)

我的朋友

分类: C#/.net

2013-05-29 18:15:33


点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Reflection;
  7. using System.Reflection.Emit;

  8. namespace NC
  9. {
  10.      ////下面是导入netcdf.dll 需要哪些函数可导入哪些,但是要注意函数的参数
  11.     public static class NetCDFInterop
  12.     {
  13.      
  14.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  15.         public static extern int nc_open(string path, CreateMode omode, out int ncidp);
  16.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  17.         public static extern int nc_close(int ncidp);
  18.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  19.         public static extern int nc_inq_varid(int ncid, string nvars, out int varid);
  20.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  21.         public static extern int nc_get_var_float(int ncid, int varid,float [,] ip);
  22.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  23.         public static extern int nc_inq(int ncid, out int ndims, out int nvars, out int ngatts,out int unlimdimid);
  24.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  25.         public static extern int nc_inq_dim(int ncid, int dimid, string name, out int length);
  26.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  27.         public static extern int nc_inq_dimname(int ncid, int dimid, StringBuilder name);
  28.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  29.         public static extern int nc_inq_dimid(int ncid, string name, out int dimid);
  30.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  31.         public static extern int nc_inq_dimlen(int ncid, int dimid, out int length);
  32.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  33.         public static extern int nc_inq_ndims(int ncid, out int ndims);
  34.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  35.         public static extern int nc_inq_nvars(int ncid, out int nvars);
  36.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  37.         public static extern int nc_inq_varname(int ncid, int varid, StringBuilder name);
  38.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  39.         public static extern int nc_inq_varndims(int ncid, int varid, out int ndims);
  40.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  41.         public static extern int nc_inq_vardimid(int ncid, int varid, int[] dimids);
  42.         [DllImport("netcdf.dll", CallingConvention = CallingConvention.Cdecl)]
  43.         public static extern int nc_inq_var_fill(int ncid, int varid, out int no_fill, out object fill_value);
  44.       
  45.         public enum CreateMode : int
  46.         {
  47.             NC_NOWRITE = 0,
  48.             /// read & write
  49.             NC_WRITE = 0x0001,
  50.             NC_CLOBBER = 0,
  51.             /// Don't destroy existing file on create
  52.             NC_NOCLOBBER = 0x0004,
  53.             /// argument to ncsetfill to clear NC_NOFILL
  54.             NC_FILL = 0,
  55.             /// Don't fill data section an records
  56.             NC_NOFILL = 0x0100,
  57.             /// Use locking if available
  58.             NC_LOCK = 0x0400,
  59.             /// Share updates, limit cacheing
  60.             NC_SHARE = 0x0800,
  61.             NC_64BIT_OFFSET = 0x0200,
  62.             /// Enforce strict netcdf-3 rules
  63.             NC_CLASSIC = 0x0100,
  64.             /// causes netCDF to create a HDF5/NetCDF-4 file
  65.             NC_NETCDF4 = 0x1000
  66.         }
  67.     }
  68.     class Program
  69.     {
  70.         ///
  71.         /// author:hsh
  72.         /// time:2013-05-29
  73.         ///
  74.         ///
  75.         static void Main(string[] args)
  76.         {
  77.             int i;
  78.             string path = @"K:vs2010codeNCNCecns.20130326.sst.nc";
  79.             NetCDFInterop.nc_open(path, NetCDFInterop.CreateMode.NC_NOWRITE, out i);
  80.             int ndims_in, nvars_in, ngatts_in, unlimdimid_in;
  81.             NetCDFInterop.nc_inq(i, out ndims_in, out nvars_in, out ngatts_in, out unlimdimid_in);
  82.             int lat_varid,lon_varid;
  83.             NetCDFInterop.nc_inq_varid(i, "LAT1001_1051", out lat_varid);
  84.             NetCDFInterop.nc_inq_varid(i, "LON1201_1251", out lon_varid);
  85.             float[,] lats_in = new float[51,1];
  86.             float[,] lons_in = new float[51,1];
  87.             float[,] data = new float[51,51];
  88.             NetCDFInterop.nc_get_var_float(i,lat_varid,lats_in);
  89.             NetCDFInterop.nc_get_var_float(i, lon_varid, lons_in);
  90.             Console.WriteLine("经度:");
  91.             for (int j = 0; j < 51; j++)
  92.             {
  93.                 Console.Write(lons_in[j, 0] + " ");
  94.             }
  95.             Console.WriteLine();
  96.             Console.WriteLine("纬度:");
  97.             for (int j = 0; j < 51; j++)
  98.             {
  99.                 Console.Write(lats_in[j,0]+" ");
  100.             }
  101.             int temp_varid;
  102.             NetCDFInterop.nc_inq_varid(i, "SST", out temp_varid);
  103.             NetCDFInterop.nc_get_var_float(i, temp_varid, data);
  104.             Console.WriteLine();
  105.             Console.WriteLine("SST:");
  106.             for (int j = 0; j < 51; j++)
  107.             {
  108.                 for (int k = 0; k < 51; k++)
  109.                 {
  110.                     Console.Write(data[j, k]+", ");
  111.                 }
  112.             }
  113.                 Console.ReadKey();
  114.             
  115.         }
  116.     }
  117. }


阅读(7420) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

li4107262017-04-08 21:06:16

你好。
请问使用C#如何写入nc文件?看了一天还是不会好蛋疼啊...如果可以的话能发我一份示例程序吗。
谢了。
qq:1427669717@qq.com

xhecity2015-09-18 16:22:19

您好,我看了你文章,但是程序还是运行出错,可以将工程文件发我一份学习下吗,谢谢86543653@qq.com