netcdf-fortran icon indicating copy to clipboard operation
netcdf-fortran copied to clipboard

How to resolve ''Fatal Error: Can't open module file ‘netcdf.mod’ for reading....."

Open anserwei opened this issue 6 years ago • 8 comments
trafficstars

Hi, I am a beginner who is learning how to read and write a netcdf file with Fortran 90. I got some problems. Could someone help me?

  • I have installed ''netcdf-fortran'' through conda. (https://anaconda.org/conda-forge/netcdf-fortran)

    conda install -c conda-forge netcdf-fortran

  • When I run a program named simple_xy_wr.f90 on Mac terminal. I got an error:

    Fatal Error: Can't open module file ‘netcdf.mod’ for reading at (1): No such file or directory compilation terminated.

  • I am confused. I don't know how to fix this.

  • I check the directory of ‘netcdf.mod’, which locates in ''/Users/Jim/anaconda3/include". I also find: netcdf.h; netcdf.inc .......; netcdf_nf_interfaces.mod, etc.

  • But, in the directory "/usr/local/include", nothing is related to netcdf.

  • Actually, I use gfortran to run the program.

So, could someone help me? I appreciate it. Thank you so much.

anserwei avatar Oct 27 '19 02:10 anserwei

Try using -I/Users/Jim/anaconda3/include as an argument in gfortran.

edhartnett avatar Oct 27 '19 19:10 edhartnett

Try using -I/Users/Jim/anaconda3/include as an argument in gfortran.

Thank you. Could you tell me more details about how to use -I/Users/Jim/anaconda3/include as an argument in gfortran.

anserwei avatar Oct 28 '19 14:10 anserwei

When you run make, you'll specify FFLAGS="-I/Users/Jim/anaconda3/include on the command line, or alternatively you can set the FFLAGS environmental variable.

WardF avatar Oct 28 '19 20:10 WardF

When you run make, you'll specify FFLAGS="-I/Users/Jim/anaconda3/include on the command line, or alternatively you can set the FFLAGS environmental variable.

Hi, thanks for your response. But, the error is still there.

  • MacOS: Catalina 10.15 Gfortran: GNU Fortran (GCC). 6.3.0 Xcode 11.1

  • I have installed ''netcdf-fortran'' through conda. conda install -c conda-forge netcdf-fortran

  • In my Mac, I find 'netcdf.mod, ... , netcdf_nf_interfaces.mod, etc.' in ''/Users/Jim/anaconda3/include".

image image

And, nothing is related to netcdf in ''/usr/local/include".

image image

  • The directory of gfortran:

image image

  • Then, I directly run export F90FLAGS="-I/Users/jim/anaconda3/include" on the command line; I also run export FFLAGS="-I/Users/jim/anaconda3/include".

image image

  • The directory of gfortran is the same as the previous point.

image image

  • Now, run simple_xy_nc4_wr.f90, I got the same error........

image image

Thus, so frustrated...... Could you help me more? Thank you so much.

anserwei avatar Oct 29 '19 03:10 anserwei

Try:

gfortran -I/Users/Jim/anaconda3/include -L/Users/Jim/anaconda3/lib simple_xy_nc4_wr.f90

But I believe you will also need some additional arguments to make that work. Run nf_config to get more information about what arguments you need to set.

Frustration will continue until you learn about the -I and the -L options of your compiler. There is ample documentation online. For example, from https://linux.die.net/man/1/gfortran.

-Idir
    These affect interpretation of the "INCLUDE" directive (as well as of the "#include" directive of the cpp preprocessor).

    Also note that the general behavior of -I and "INCLUDE" is pretty much the same as of -I with "#include" in the cpp preprocessor, with regard to looking for header.gcc files and other such things.

    This path is also used to search for .mod files when previously compiled modules are required by a "USE" statement. 

Most users do not compile using gfortran from the command line, but using make, or (if they're extra-clever) automake/autoconf/libtool. Within such systems you will find respect for environmental flags like FCFLAGS, FFLAGS, CPPFLAGS, etc. More info is available on-line.

edhartnett avatar Oct 29 '19 08:10 edhartnett

Thank you! I read Unidata NetCDF Fortran Library on website:

https://www.unidata.ucar.edu/software/netcdf/docs-fortran/

And, I also read some information about how to set env variables. After trying many times, I can successfully read/write netCDF files with fortran90.

anserwei avatar Nov 01 '19 17:11 anserwei

Seems like this issue can be closed now.

edwardhartnett avatar Dec 20 '19 21:12 edwardhartnett

My problem was solved with the following command, I hope it paves the way for you too. sudo apt-get install netcdf-bin

moslem1994 avatar May 01 '21 10:05 moslem1994