csv - How to extract data from NetCDF file using C# and Microsoft Scientific Data Set? -
i trying extract data netcdf file using microsoft's scientific data set libraries (sds). i'm restricted using ms sds , c#. i'm not programmer trade i'm struggling basics working. begin i'd write simple script extract data , write .csv file. using introduction doc , codeplex tutorials. i've tried write simple c# console app reads file , writes out console or ideally .csv file.
using sds 1.3 command line can view contents of test file:
d:\netcdf>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) d:\netcdf>
my script looks this:
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 path netcdf file used data source. var dataset = sds.dataset.open("d:\\netcdf\\test.nc?openmode=readonly"); sbyte[,] times = dataset.getdata<sbyte[,]>("times"); //int32[,] times = dataset.getdata<int32[,]>("times"); //single[] longitudes = dataset.getdata<single[]>("west_east"); //var latitudes = dataset.getdata<single[]>("south_north"); single[,,] datavalues = dataset.getdata<single[,,]>("acpr"); (int itime = 50; itime < 60; itime++) { (int ilongitude = 130; ilongitude < 150; ilongitude++) { (int ilatitude = 130; ilatitude < 140; ilatitude++) { // write output data float thisvalue = datavalues[itime,ilatitude,ilongitude]; console.writeline(itime); console.writeline(ilatitude); console.writeline(ilongitude); console.writeline(thisvalue); } } } } } }
if comment out var times... line runs. i'm struggling sds read time dimension. if use sbyte complains variable doesn't exist. if use int32 complains converting string.
system.invalidoperationexception unhandled hresult=-2146233079 message=requested variable not exist in data set source=scientificdataset stacktrace: @ microsoft.research.science.data.imperative.datasetextensions.findvariable(dataset dataset, func`2 predicate) @ microsoft.research.science.data.imperative.datasetextensions.getdata[d](dataset dataset, string variablename) @ netcdfconsoleapp.program.main(string[] args) in \\gdc-fs01\user$\prm\visual studio 2015\projects\netcdfconsoleapp\program.cs:line 16 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception:
what missing?
i think have solved puzzle. problem 2nd variable empty. i'm not sure if accident or design.
i have found date of when data supposed start in meta data field. i've modified code retrieves this, , writes console.
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 path netcdf file used data source. var dataset = sds.dataset.open("d:\\netcdf\\test.nc?openmode=readonly"); string dt = (string)dataset.metadata["start_date"]; single[,,] datavalues = dataset.getdata<single[,,]>("acpr"); (int itime = 50; itime < 60; itime++) { (int ilongitude = 130; ilongitude < 150; ilongitude++) { (int ilatitude = 130; ilatitude < 140; ilatitude++) { // write output data float thisvalue = datavalues[itime,ilatitude,ilongitude]; console.writeline(dt.tostring() + ',' + itime.tostring() + ',' + ilatitude.tostring() + ',' + ilongitude.tostring() + ',' + thisvalue.tostring()); } } } console.readline(); } } }
i've struggled i'm sharing in hope of use else.
one thing found useful discussion tab on codeplex has lots of useful code snippets.
Comments
Post a Comment