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.
>To: address@hidden >cc: address@hidden >From: Paul van Delst <address@hidden> >Subject: NetCDF file status >Organization: CIMSS @ NOAA/NCEP >Keywords: 200203281510.g2SFAha03731 Hi Paul, > I hope this is the right place to ask this - I didn't want to > broadcast it to netcdfgroup. Yes, sorry it took me so long to reply ... > How does one determine if a netCDF file is already open? E.g. If I > have a f90 routine: > > function read_netcdf_data( ncfile_id, ...other arguments, filename = > filename ) > > How can I use the ncfile_id to determine if the file is open. Or for > that matter, what mode the file is in; data or define? e.g. I'd like > to be able to do something like > > if ( .not. ncfile_is_open( ncfile_id ) ) then ... > open it using optional filename argument > > and > > if ( ncfile_mode( ncfile_id ) /= 'DATA' ) then ... > or > if ( .NOT. ncfile_data_mode( ncfile_id ) ) then ... > put it in data mode > > any ideas? I think the open test can be handled with the > nf90_inquire() function - the only *listed* cause of error is if the > specified netCDF id does not refer to an open netCDF dataset. But the > second one seems a bit trickier. And the documentation always states > "possible causes of errors include:" suggesting something other than > an unopened dataset may trigger an error in the inquire function. Your idea of using nf90_inquire() will work in that it will tell you whether the ncfile_id is associated with an open file (but not which file). The only possible cause for nf90_inquire() to return an error is if the specified id is not in the list of open IDs (as can be seen in the C source in libsrc/nc.c for the NC_check_id function, which is ultimately called by nf90_inquire()). The documentation should be more definitive about this; I think it states "possible causes of errors include:" just to make it a little more maintainable in case there are additional errors in the future (e.g. a network error for remote access). Once you have determined that the file is open, you can determine which mode it is in by calling a function that returns an error if the dataset was in define mode, for example nf90_redef(). This will return NF90_NOERR if in data mode (in which case you can call nf90_enddef() to return to data mode). Otherwise you were already in define mode and there was no change of state. A function that would safely return the state would be a good addition to the interface. Please let us know if this doesn't work or you have other questions about this. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu