Hello Matthew,
thanks for your detailed explanations. I was now able to read array
variables out of the netcdf file. Great!
I must still do something wrong with string variables. Even if the string is
properly dimensioned, as you advised, before passing it to the netcdf
function, the contents of the string remains unchanged, i.e. is not filled
with the data from the file.
I used this declaration:
Declare Function nc_get_att_text Lib "netcdf.dll" (ByVal ncid As Long, ByVal
varid As Long, ByVal name As String, ByVal op As String) As Long
varid was -1 (NC_GLOBAL)and the string op was filled with as many NULL
characters equal to the length of the attribute. I checked this from a
Ncdump of the netcdf file.
After calling the nc_get_att_text function the string op contains still only
NULL characters.
What could be wrong?
Thanks and best regards
H.J. Rieger
> Hello!
>
> Referring to:
> "If the error is suppressed (with "OnError Resume Next") then I can read
> some simple data from the *.cdf file. But if string variables or array
> variables are involved, the program fails or even crashes."
>
> for this case, I want to right away say that you need to keep an eye on
> how you
> pass your arrays. You have to pass them by referencing the first element
> of
> the array. For example, I want to load a longitude variable into an
> array:
>
> 'ncFile is the handle to a NetCDF file from nc_open
> 'lon_id is the longitude variable id from nc_inq_varid
> 'lon_length is the size of the longitude dimension
>
> Redim lon_array!(1 To lon_length&)
> NcErr& = nc_get_var_float(ncFile&, lon_id&, lon_array!(1))
>
> Same thing for a 2D array:
>
> Redim lon_array2!(1 To cellx_length&, 1 To celly_length&)
> NcErr& = nc_get_var_float(ncFile&, lon_id&, lon_array2!(1, 1))
>
> For strings, you must make sure the string variable you are passing in has
> space already allocated for it by using Space or String. VB takes care of
> passing it your function properly as long as your string parameter is
> declared to be ByVal in the nc_get_att_text declaration. You can
> determine
> the size of the string by looking at the dimensions of the variable. For
> attributes you would use nc_inq_attlen or nc_inq_att. Once you have the
> string length you must create a string of the right size to contain the
> string data. For example, I want to get the units for the longitude
> variable above:
>
> NcErr& = nc_inq_attlen(ncFile&, lon_id&, "units", units_len&)
> units$ = String(units_len& + 1, Chr$(0))
> NcErr& = nc_get_att_text(ncFile&, lon_id&, "units", units$)
> 'Can precede units$ with ByVal
>
> I got the string length for the attribute, created a string of Null
> characters
> of the appropriate length (I like to include an extra character just in
> case),
> and then I retrieved the string from the attribute.
>
> In summary, make sure you have memory room in your arrays and strings
> since
> the NetCDF API doesn't allocate those for you. Make sure you specify the
> first
> indices of the array when you are retrieving a variable into an array.
> And,
> finally, make sure your declarations are correct. Where the C API says
> byte
> use integer, short use integer, int use long, float use single, and double
> use
> double. Be sure your declaration uses ByVal when passing strings.
>
> Good Luck!
> Matthew Hanna
>
> ncdigest wrote:
>
> >ncdigest Thursday, December 9 2004 Volume 01 : Number
> 828
> >
> >
> >
> >Today's Topics:
> >netcdf.dll and Visual Basic 6
> >
> >----------------------------------------------------------------------
> >
> >Date: Thu, 9 Dec 2004 19:27:08 +0100 (MET)
> >From: =?ISO-8859-1?Q?=22Hans-J=FCrgen_Rieger=22?= <H.Rieger@xxxxxx>
> >Subject: netcdf.dll and Visual Basic 6
> >
> >Hi,
> >
> >I´ve got some problems using the netcdf.dll (prebuilt dll from the
> >netcdf-3.5.1 download) with Visual Basic 6.
> >In a new module I wrote the declaration:
> >
> >Declare Function nc_open Lib "netcdf.dll" (ByVal path As String, ByVal
> >cmode As Long, ByRef ncidp As Long) As Long
> >
> >Then I used it in a procedure:
> >Sub main
> >Dim path As String, ncidp As Long, res As Long
> >path = "c:\test.cdf"
> >res = nc_open(path, 0, ncidp)
> >End Sub
> >
> >Running the program, I get the error message "Bad dll calling convention"
> >
> >If the error is suppressed (with "OnError Resume Next") then I can read
> >some simple data from the *.cdf file. But if string variables or array
> >variables are involved, the program fails or even crashes.
> >
> >I´ve got the impression from other netcdfgroup members that it is
> >possible to use the
> >netcdf.dll in the way outlined above.
> >
> >Does anybody know what I am doing wrong? What calling convention does
> >the netcdf.dll uses?
> >
> >With best regards
> >
> >H.J. Rieger
> >
> >- --
> >GMX ProMail mit bestem Virenschutz http://www.gmx.net/de/go/mail
> >+++ Empfehlung der Redaktion +++ Internet Professionell 10/04 +++
> >
> >------------------------------
> >
> >End of ncdigest V1 #828
> >***********************
> >
> >
--
NEU +++ DSL Komplett von GMX +++ http://www.gmx.net/de/go/dsl
GMX DSL-Netzanschluss + Tarif zum supergünstigen Komplett-Preis!