Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compilation program to read NetCDF file with fortran90

714 views
Skip to first unread message

i_want_to_love_...@hotmail.com

unread,
Mar 19, 2009, 4:27:17 AM3/19/09
to
I am a student in Kyoto University in Kyoto, Japan.

I want to read and write files in NetCDF, so at first I am going to
read values of files written in NetCDF.
The file to read is below.

*********************************************
implicit none
include 'netcdf.inc'

integer :: ncid,status,varid
real,dimension(100,100,26) :: UDATA
!real,dimension(99,99,27) :: WDATA

status = NF_OPEN ('wrfout_d01_2006-01-01_00:00:00',0,ncid)
if (status .ne. NF_NOERR) call HANDLE_ERR(STATUS)

status = nf_inq_varid (ncid, "U", varid)
if (status .ne. NF_NOERR) then
write(6,*) "VAR ERROR"
stop
endif

status = nf_get_var_real (ncid,varid,UDATA)
if(status .ne. NF_NOERR) then
write(6,*) "READ ERROR"
stop
endif

status = nf_close(ncid)

write(6,*) ncid,varid

stop
end
*************************************************

And then, I typed

$ ifort -c -I/usr/local/netcdf-3.6.2/include 1st.f90
$ ifort -lnetcdf -L/usr/local/netcdf-3.6.2/lib 1st.o -o a.out

Then, I got these errors.

[C@localhosts netcdf]$ ifort -lnetcdf -L/usr/local/netcdf-3.6.2/lib
1st.o -o a.out
1st.o: In function `MAIN__':
1st.f90:(.text+0x46): undefined reference to `nf_open_'
1st.f90:(.text+0x76): undefined reference to `nf_inq_varid_'
1st.f90:(.text+0xa1): undefined reference to `nf_get_var_real_'
1st.f90:(.text+0x11a): undefined reference to `nf_close_'
1st.f90:(.text+0x30a): undefined reference to `handle_err_'

If you know something about these errors, please give me information
anything you have.

Hiroshi

Arjen Markus

unread,
Mar 19, 2009, 8:04:19 AM3/19/09
to

You should probably change the order of the arguments:

ifort -L/usr/local/netcdf-3.6.2/lib -lnetcdf 1st.o -o a.out

and you need additional libraries. Here is a compile/link command I
use
myself:

ifort -o program program.f90 -Inetcdf-4.0/f90 netcdf-4.0/f90/.libs/
libnetcdff90.a netcdf-4.0/fortran/.libs/libnetcdff.a netcdf-4.0/
libsrc/.libs/libnetcdf.a

Not the most efficient perhaps, but for my particular case it works.
As you can see you need a few more libraries.

Regards,

Arjen

Dave Allured

unread,
Mar 19, 2009, 1:16:24 PM3/19/09
to

You are trying to use the Netcdf F77 interface. You can make it work,
but I recommend the F90 interface instead, since your main program is
F90. See the Netcdf on-line documentation for the Fortran 90 API.

Also, check with your system administrator to make sure that the F90
interface is installed. It is an optional part of Netcdf. In
particular, make sure that the file netcdf.mod is available and
specified on your compile command line.

--Dave

Paul van Delst

unread,
Mar 20, 2009, 9:59:15 AM3/20/09
to

Not for v3.6.2 he doesn't. You're using netCDF v4.0.

To the OP: Once you solve your link problems, why not try using the Fortran90 API to netCDF?

As an aside, I've been thinking about upgrading to v4.0 but the reason I use netCDF (as
opposed to, say, HDF) is that netCDF is simple (both the library build and usage). If the
above explosion of required libraries to use netCDF v4.0 is true, I may stick with the old
version (netCDF v3.6.x).

cheers,

paulv

i_want_to_love_...@hotmail.com

unread,
Mar 22, 2009, 8:18:20 AM3/22/09
to

For everyone, thanks for your attention.

The file I want to read is made by WRF (Weather Research and
Forecasting) model.
I built up WRF with NetCDF, and both were compiled with ifort and icc.
Can I use only F77 interface with default setting?

Hiroshi

i_want_to_love_...@hotmail.com

unread,
Mar 22, 2009, 8:18:36 AM3/22/09
to
On 3月20日, 午後10:59, Paul van Delst <Paul.vanDe...@noaa.gov> wrote:

For everyone, thanks for your attention.

Dave Allured

unread,
Mar 22, 2009, 1:59:38 PM3/22/09
to

Yes, you should be able to use only F77 interface. Based on your
original post, I think you have at least three specific problems. Arjen
addressed two of them, I will try to explain:

1. -lnetcdf should be positioned to the right of the -L option.

2. You may need a second library libnetcdff. Try adding -lnetcdff to
your command.

3. handle_error is a user-supplied subroutine. Just insert this after
the end of your main program (untested):

subroutine handle_error (status)
implicit none
integer, intent (in) :: status
print *, 'Netcdf error number = ', status
stop
end subroutine handle_error

Here is a modification of your original compile commands. Only the
second line is changed:

ifort -c -I/usr/local/netcdf-3.6.2/include 1st.f90
ifort -L/usr/local/netcdf-3.6.2/lib -lnetcdf -lnetcdff 1st.o -o a.out

First try this without -lnetcdff . If you still get the missing symbol
errors for nf_open_ etc., then add -lnetcdff . You may need to search
all file names under /usr/local/netcdf-3.6.2 to find this library, and
add a second -L command to give its correct path if different than the
one in the first -L option.

I am unsure about libnetcdff because they changed the Fortran API
library configuration somewhere around version 3.6.2 or 3.6.3. The
Fortran library was split off into the separate library libnetcdff.
This is further complicated by a newly introduced configure option to
build the Fortran libraries either way. I do not have all the details
memorized. ;-)

You can examine the Netcdf library file(s) yourself to locate symbols
like nf_open_ which are part of the F77 interface. Something like this,
it may vary between platforms:

cd /usr/local/netcdf-3.6.2/lib
nm libnetcdf* | grep nf_open_

If you can't figure this out here in c.l.f., then try the Netcdf users
list. They are very helpful.

--Dave

TideMan

unread,
Mar 22, 2009, 3:20:10 PM3/22/09
to
On Mar 23, 6:59 am, Dave Allured <nos...@nospom.com> wrote:

I've just been through the process of setting up read/write of netCDF
files using gfortran on Ubuntu 8.04.
First, I downloaded netCDF 4.0 sources, then CDed to the directory
and:
./configure
make
sudo su
make install

This generated and installed a static library in /usr/local/lib

Now to compile and link Test.f90 I do this:
gfortran -o Test Test.f90 /usr/local/lib/libnetcdf.a

0 new messages