This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
> From: Jessica Burkhart <address@hidden> > To: address@hidden > Subject: Problem with ncopen > Organization: Oklahoma Climate Survey Jessica, > > > > > I am having a problem with ncopen repeatedly returning 0 as a valid > > > > > file id. The other netCDF functions, ncclose and ncdimid, return > > > > > bad id error codes with a netCDF file id of 0. ... > > > We are using netCDF version 3.3.1 on a DEC Alpha machine with the LINUX > > > Red Hat 4.0 OS. > > > > > > I was not specific enough about the problem. ncopen with NC_WRITE is > > > returning a 0 which is causing ncclose and ncdimid to return error > > > codes. However, when I force ncopen to return a positive value, then the > > > file can be accessed. ... > To force my program to open with a positive value, I wrote a while loop to > reopen the file with a NetCDF file id greater than zero. For example, > > ncid = ncopen("test.nc",NC_WRITE); > while (ncid == 0) > { > cerr<<"NCID equals zero .. reopen\n" > ncid = ncopen("test.nc",NC_WRITE); > } All this does is open the file twice, once with an ID of 0 and once with an ID of 1. You should be able to call ncclose on both handles, just as with the Unix stdio library, you can call fopen multiple times on the same file and call fclose once for each file descriptor returned by fopen. > Your example worked on our platform, but I didn't get it to return a > netCDF file id equal to zero. ??? > The following example doesn't work on our platform: > #include <netcdf.h> > #include <stdio.h> > > int > main() > { > int ncid; > ncopts = NC_VERBOSE; > ncclose(0); // Force to close with ncid = 0 > printf("ncerr = %d\n",ncerr); > return 0; > } > > It has the following error message and error code: > ncclose: ncid 0: Not a netCDF id > ncerr = -33 The above error message is expected and is the correct behavior, because ncclose is only supposed to work on IDs of *open* netCDF files. The property of a file being open is a characteristic of each process, and each process has its own set of netCDF file IDs for open netCDF files. That is, if one program opens a two netCDF files, they will have IDs 0 and 1 (under the netCDF-2 interface), and if another process simultaneously opens three other netcdf files, they should return netCDF IDs 0, 1, and 2 to that process, even though they may refer to completely different files. NetCDF IDs do not persist once a process exits. Thus the above program is attempting to close a netCDF file it has never opened, so this cannot be expected to work. > As long as ncopen doesn't return zero for the netCDF id, my code > works fine. > > If it works on your system, we might need to reinstall the netCDF > program. Do you have a small example that fails on your system in which you open a netCDF file with ncopen(), it returns 0 for a netCDF ID, you attempt to close it using that netCDF ID, and it fails? That would be a bug, but so far I can't tell whether there is really a problem. Maybe the symptoms you are seeing are just a result of a misunderstanding about the persistence of netCDF IDs, e.g. an attempt to call ncclose() before ncopen() ... --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu