C#读取nc数据集
时间: 2023-07-02 13:09:29 浏览: 406
要读取nc数据集,可以使用NetCDF库。以下是一个简单的示例代码:
```csharp
using System;
using System.IO;
using System.Linq;
using System.Diagnostics;
using NetCDFInterop;
class Program
{
static void Main(string[] args)
{
string filePath = "example.nc";
// Open netcdf file
var ncFile = NetCDF.Open(filePath);
// Get dimensions
int xDim = ncFile.GetDim("x");
int yDim = ncFile.GetDim("y");
int zDim = ncFile.GetDim("z");
// Get variables
var var1 = ncFile.GetVar("var1");
var var2 = ncFile.GetVar("var2");
// Read data
double[] data1 = new double[xDim * yDim * zDim];
double[] data2 = new double[xDim * yDim * zDim];
var1.Read(data1);
var2.Read(data2);
// Print data
for (int i = 0; i < xDim; i++)
{
for (int j = 0; j < yDim; j++)
{
for (int k = 0; k < zDim; k++)
{
int index = i * yDim * zDim + j * zDim + k;
Console.WriteLine($"({i}, {j}, {k}): {data1[index]}, {data2[index]}");
}
}
}
// Close netcdf file
ncFile.Close();
}
}
```
注意:需要安装NetCDF库才能使用`NetCDFInterop`命名空间。可以通过NuGet安装`NetCDFInterop`包。
阅读全文