Wei-keng and Jim,
This is not a bug, IMO. Compare the documentation for these two functions:
nf90_inquire_attribute(ncid, varid, name ...
varid
Variable ID of the attribute's variable, or NF90_GLOBAL for a global
attribute.
nf90_inquire_variable(ncid, varid, name ...
varid
Variable ID.
A variable ID of NF90_GLOBAL is explicitly allowed with
nf90_inquire_attribute, but not with nf90_inquire_variable. The intended
functions for discovering global attributes are nf90_inquire and
nf90_inquire_attribute. With a bit of study, the F90 documentation on this
topic seems rather clear to me.
Attempting to use nf90_inquire_variable for global attributes is a
mis-application of that function. Furthermore, that function is probably
returning the appropriate error code in this case, as documented: "The
variable ID is invalid for the specified netCDF dataset."
Nick Paplor's suggested fix looks good to me.
--Dave
On Tue, Jul 14, 2015 at 12:16 PM, Wei-keng Liao <
wkliao@xxxxxxxxxxxxxxxxxxxxx> wrote:
> > These latter two functions accept NF90_GLOBAL as "varid" but an error is
> thrown when this is used for varid in nf90_inquire_variable().
>
> This may be a bug.
> The netCDF Fortran 90 document did not say using NF90_GLOBAL in the varid
> argument is illegal.
> As nf90_inquire_variable() calls nc_inq_var() internally, the netCDF-C
> 4.3.3.1 document for nc_inq_var() also did not say using NC_GLOBAL is
> illegal.
>
> Although nf90_inquire() can get you the desired result as pointed out by
> Nick Papior, but you may still want to report this possible bug to the
> Unidata netCDF Support <support-netcdf@xxxxxxxxxxxxxxxx>
>
> Wei-keng
>
> On Jul 14, 2015, at 11:13 AM, <Jim.Enright@xxxxxxxxxxxxxx> <
> Jim.Enright@xxxxxxxxxxxxxx> wrote:
>
> > Hi,
> >
> > I'm wondering if it is possible to use the nf90_inquire_variable()
> function to find information about global attributes in a similar way to
> nf90_inquire_attribute()?
> > For example, should it be possible to set varid to be NF90_GLOBAL to
> find the number of global attributes? i.e.:
> > call check(nf90_inquire_variable(ncid, NF90_GLOBAL, nAtts = numAttr))
> >
> > The reason for this query is because we have a code block (snippet shown
> below) which prints some header information by looping over an array of
> variables.
> > It first calls nf90_inquire_variable to get the number of attributes and
> then uses this to get more details about the attributes by calling
> nf90_inq_attname() and nf90_inquire_attribute().
> > These latter two functions accept NF90_GLOBAL as "varid" but an error is
> thrown when this is used for varid in nf90_inquire_variable().
> > Is this the expected behaviour of nf90_inquire_variable? If so, can you
> recommend the best alternative way to get the number of global attributes -
> nf90_inquire() perhaps?
> >
> > do idxVar = varBegin, numVars
> >
> > varName = varInfoArray%varInfoArr(idxVar)%varName
> >
> > if( varName .eq. 'UNDEFINED' ) then
> > varid = nf90_global
> > else
> > varid = varInfoArray%varInfoArr(idxVar)%varId
> > end if
> >
> > call check( nf90_inquire_variable( ncid, varid, nAtts = numAttr )
> > ...
> > ...
> > do idxAttr = 1, numAttr
> >
> > ! Get the names & details of the attributes
> > call check( nf90_inq_attname( ncid, varid, idxAttr, attrName ))
> >
> > call check( nf90_inquire_attribute( ncid, varid, attrName, netCDFType,
> numAttrValues ))
> > end do
> > ...
> > end do
> >
> > Best regards,
> > Jim
>