我在写一个使用Microsoft Scientific Data Set读取NetCDF文件的C#程序。如何处理NULLS:C#,Microsoft SDS 1.3和NetCDF文件
using System;
using System.IO;
using sds = Microsoft.Research.Science.Data;
using Microsoft.Research.Science.Data.Imperative;
namespace NetCDFConsoleApp
{
class Program
{
static void Main(string[] args)
{
// Gets dataset from file.
var dataset = sds.DataSet.Open("E:\\Temp\\test.nc?openMode=readOnly");
// Get the starting DateTime from the meta data.
string dt = (string)dataset.Metadata["START_DATE"];
//load dataset into array
Single[,,] dataValues = dataset.GetData<float[,,]>("ACPR");
//Get DateTime from Metadata fields.
DateTime dt2 = DateTime.ParseExact(dt, "yyyy-MM-dd_HH:mm:ss", null);
// Latitude grid ranges from = 0 to 215; East Cape is ~ 125-144
for (int iLatitude = 137; iLatitude < 138; iLatitude++)
{
//Longitude ranges from 0 to 165; East Cape is ~ 125-150
for (int iLongitude = 133; iLongitude < 134; iLongitude++)
{
//There is normally 85 hours worth of data in a file. But not always...
for (int iTime = 0; iTime < 65; iTime++)
{
// Get each data point
float? thisValue = dataValues[iTime,iLatitude,iLongitude];
//Burp it out to the Console. Increment the datetime while im at it.
Console.WriteLine(dt.ToString() + ',' + dt2.ToString() + ',' + iTime.ToString() + ',' + dt2.AddHours(iTime));
}
}
}
Console.ReadLine();
}
}
}
这些文件包含地图网格(X,Y)上的预测降雨量数据。每个网格参考应该有85小时的数据。
E:\temp>sds list test.nc
[2] ACPR of type Single (Time:85) (south_north:213) (west_east:165)
[1] Times of type SByte (Time:85) (DateStrLen:19)
但是偶尔他们可能会少一些(说60-70小时)。当这种情况发生时,我的C#程序在导入数据时失败。
var dataset = sds.DataSet.Open("test.nc?openMode=readOnly");
Single[,,] dataValues = dataset.GetData<Single[,,]>("ACPR");
我可以用命令行重现错误。
在这里,我可以成功地提取网格XY:125,130小时60-65。我在这个文件中的最后一个值是Time = 69。
E:\temp>sds data test.nc ACPR[60:65,125:125,130:130]
[2] ACPR of type Single (Time:85) (south_north:213) (west_east:165)
Name = ACPR
description = ACCUMULATED TOTAL GRID SCALE PRECIPITATION
MemoryOrder = XY
coordinates = XLONG XLAT XTIME
stagger =
FieldType = 104
units = mm
[60,125,130] 13.4926
[61,125,130] 15.24556
[62,125,130] 16.3638
[63,125,130] 17.39618
[64,125,130] 20.00507
[65,125,130] 23.57192
如果我尝试阅读过去的小时数69,我得到以下错误。
E:\temp>sds data test.nc ACPR[60:70,125:125,130:130]
[2] ACPR of type Single (Time:85) (south_north:213) (west_east:165)
Name = ACPR
description = ACCUMULATED TOTAL GRID SCALE PRECIPITATION
MemoryOrder = XY
coordinates = XLONG XLAT XTIME
stagger =
FieldType = 104
units = mm
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at nc_get_vara_float(Int32 , Int32 , UInt64* , UInt64* , Single*)
at NetCDFInterop.NetCDF.nc_get_vara_float(Int32 ncid, Int32 varid, IntPtr[] start, IntPtr[] count, Single[] data)
at Microsoft.Research.Science.Data.NetCDF4.NetCdfVariable`1.ReadData(Int32[] origin, Int32[] shape)
at sdsutil.Program.PrintData(Variable v, String range, String format)
at sdsutil.Program.DoData(String uri, String[] args)
at sdsutil.Program.Main(String[] args)
E:\temp>
如果该文件包含了完整的85小时内,我可以请求时间0-100,它仍然给了我85个值没有错误。
我相信那个问题是NULL /丢失的数据。有什么方法可以在导入变量不为空的数据时指定?或者使用一些try/catch?
Single[,,] dataValues = dataset.GetData<Single[,,]>("ACPR")>> where it's not blank thanks. ;
编辑:我开始怀疑文件形成不正确。使用SDS浏览器一个好文件的元数据与这样的糟糕的外观;
然而,命令行显示元数据作为两个相同的。
E:\temp>sds good.nc
[2] ACPR of type Single (Time:85) (south_north:213) (west_east:165)
[1] Times of type SByte (Time:85) (DateStrLen:19)
E:\temp>sds bad.nc
[2] ACPR of type Single (Time:85) (south_north:213) (west_east:165)
[1] Times of type SByte (Time:85) (DateStrLen:19)
E:\temp>
我不知道我是否可以使用通用C#函数来处理NULLS,或者如果我需要使用SDS中的某些内容。 – Peter
来自doc:“当您使用C#等强类型语言调用Scientific DataSet方法时,科学数据集库不会强制数据类型。数据集中的数据类型和您指定的数据类型为 类型参数到GetData方法必须完全匹配。“。 'Single [,,] dataValues = dataset.GetData(“ACPR”); '这是单身,浮动,双等?什么'var dataValues = dataset.GetData (“ACPR”);'当你尝试不同类型?你是否已经安装了数据集查看器,如果可以的话,你可以添加'dataset.View();' –
请注意我的“从文档”评论对这里使用的库做了一个假设,这个链接可能是有用的。 –