Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28689 Discussions

Include netCDF in my Fortran projet

Lamine06F
Novice
14,098 Views

Hello,

Suppose this is the code

Subroutine netCDF

    USE netcdf

     implicit none

     print*,"netCDF is working"

end subroutine netCDF

(copied from this link https://community.intel.com/t5/Intel-Fortran-Compiler/Link-NetCDF-Fortran-library-to-Visual-Studio-2008-Intel-Composer/m-p/1036966)

My objective is to link netCDF to my project. I have installed netCDF-C executable because from what I have read, it is necessary (link https://downloads.unidata.ucar.edu/netcdf/) and download netCDF-FORTRAN 4.6.1 (same link as above) . So how should I do to link netCDF with my project. 

Thank you

0 Kudos
1 Solution
mfinnis
New Contributor II
13,950 Views

I posted a step by step procedure for building the netCDF fortran libraries a while ago. At the time I wasn't familiar with CMake so it wasn't as clear as it could have been. Since then I have used CMake quite a bit and in some cases I use it rather than VS solutions and projects directly and use it to generate a VS solution when required.

With the proviso that I haven't used netCDF for anything other than running some of the supplied examples, here is  a procedure for building the netCDF fortran library for use with ifort in Windows:

 

If CMake is not installed then download CMake Widows x64 Installer from https://cmake.org/download/ and install.

Download netCDF-3 64-bit Windows binary netCDF4.9.2-NC3-64.exe from https://downloads.unidata.ucar.edu/netcdf/

Download netcdf-fortran-main.zip from https://github.com/Unidata/netcdf-fortran (https://codeload.github.com/Unidata/netcdf-fortran/zip/refs/heads/main)

(netcdf-fortran-4.6.1.zip from https://downloads.unidata.ucar.edu/netcdf/ needs some files to be generated which aren't on a Windows system - or not on mine at least.)

Install netCDF-3 libraries
(If netCDF is not added to the system PATH it will be required to be accessible to any applications that use it.)
Destination folder G:\netCDF\netCDF 4.9.2 (for the sake of example netCDF is installed in G:\netCDF)
Install all components

Extract all files netcdf-fortran-main.zip to G:\netCDF\netcdf-fortran-main-4.6.1

Run CMake
Where is the source code: G:/netCDF/netcdf-fortran-main-4.6.1/netcdf-fortran-main
Where to build the binaries: G:/netCDF/netcdf-fortran-main-4.6.1/MyBuild

Press Configure
Specify the generator for this project: Visual Studio ?? ????
Optional platform for generator: x64
Use default native compilers

will fail: ensure BUILD_SHARED_LIBS is UNCHECKED
set NETCDF_C_LIBRARY to G:/netCDF/netCDF 4.9.2/lib/netcdf.lib

Press Configure
will fail: set NETCDF_C_INCLUDE_DIR to G:/netCDF/netCDF 4.9.2/include

Press Configure
(will produce a number of not found messages in red but will end with "Configuring done" in output window)

Press Generate
(short display of progress dialog followed by "Generating done" in output window)

Close CMake

Go to folder G:\netCDF\netcdf-fortran-main-4.6.1\MyBuild

Open NC4F.sln in Visual Studio

In project netcdff open file module_netcdf_nc_data.F90 and insert

#else
  Integer(C_INT), Parameter :: NC_ENOPAR = -114
  Integer(C_INT), Parameter :: NC_NOQUANTIZE        = 0
  Integer(C_INT), Parameter :: NC_QUANTIZE_BITGROOM = 1

above the #endif at the end of the module

Build netcdff (IFORT) (dependencies will be built first)

To check functionality:

Build and run F77_tests_pres_temp_4D_wr followed by F77_tests_pres_temp_4D_rd
Build and run F90_tests_pres_temp_4D_wr followed by F90_tests_pres_temp_4D_rd

(If netcdf.dll is not in the path, add path=G:\netCDF\netCDF 4.9.2\bin to the Environment item of the Debugging tab of the projects' properties - the example projects can all be selected in Solution Explorer and the Environment item set in all of them at once.)

View solution in original post

30 Replies
RahulC_Intel
Moderator
12,020 Views

Hi Diallo, we are transferring this thread to the respective team for further help. Please refer to the link: https://community.intel.com/t5/Intel-Fortran-Compiler/bd-p/fortran-compiler and find your post under the Intel® Fortran Compiler community.


0 Kudos
Steve_Lionel
Honored Contributor III
11,972 Views

As netCDF does not appear to provide Visual Studio projects, just libraries and include/module files, the way to do this is to:

1. Right click on your Fortran project, select Properties.

2. Select Fortran > General and add the path to the netCDF Include folder in the property "Additional include directories".

3. Locate the netCDF import library in the netCDF lib folder and add it to your project as if it were a source file.

4. When running the program, the netCDF bin folder must be included in the PATH environment variable.

Lamine06F
Novice
11,925 Views

You are specifying which netCDF I have to link. From webpage, there are to netCDF : netCDF-C and netCDF-FORTRAN. So you are talking about netCDF-C or netCDF-FORTRAN

0 Kudos
Steve_Lionel
Honored Contributor III
11,914 Views

netCDF-Fortran is provided as sources only, with Linux-style makefiles. Given that your example has a USE of a netcdf module, that strongly suggests that you need the Fortran version of netCDF. You'll then need to figure out how to create a Visual Studio project to build it.

0 Kudos
mfinnis
New Contributor II
13,951 Views

I posted a step by step procedure for building the netCDF fortran libraries a while ago. At the time I wasn't familiar with CMake so it wasn't as clear as it could have been. Since then I have used CMake quite a bit and in some cases I use it rather than VS solutions and projects directly and use it to generate a VS solution when required.

With the proviso that I haven't used netCDF for anything other than running some of the supplied examples, here is  a procedure for building the netCDF fortran library for use with ifort in Windows:

 

If CMake is not installed then download CMake Widows x64 Installer from https://cmake.org/download/ and install.

Download netCDF-3 64-bit Windows binary netCDF4.9.2-NC3-64.exe from https://downloads.unidata.ucar.edu/netcdf/

Download netcdf-fortran-main.zip from https://github.com/Unidata/netcdf-fortran (https://codeload.github.com/Unidata/netcdf-fortran/zip/refs/heads/main)

(netcdf-fortran-4.6.1.zip from https://downloads.unidata.ucar.edu/netcdf/ needs some files to be generated which aren't on a Windows system - or not on mine at least.)

Install netCDF-3 libraries
(If netCDF is not added to the system PATH it will be required to be accessible to any applications that use it.)
Destination folder G:\netCDF\netCDF 4.9.2 (for the sake of example netCDF is installed in G:\netCDF)
Install all components

Extract all files netcdf-fortran-main.zip to G:\netCDF\netcdf-fortran-main-4.6.1

Run CMake
Where is the source code: G:/netCDF/netcdf-fortran-main-4.6.1/netcdf-fortran-main
Where to build the binaries: G:/netCDF/netcdf-fortran-main-4.6.1/MyBuild

Press Configure
Specify the generator for this project: Visual Studio ?? ????
Optional platform for generator: x64
Use default native compilers

will fail: ensure BUILD_SHARED_LIBS is UNCHECKED
set NETCDF_C_LIBRARY to G:/netCDF/netCDF 4.9.2/lib/netcdf.lib

Press Configure
will fail: set NETCDF_C_INCLUDE_DIR to G:/netCDF/netCDF 4.9.2/include

Press Configure
(will produce a number of not found messages in red but will end with "Configuring done" in output window)

Press Generate
(short display of progress dialog followed by "Generating done" in output window)

Close CMake

Go to folder G:\netCDF\netcdf-fortran-main-4.6.1\MyBuild

Open NC4F.sln in Visual Studio

In project netcdff open file module_netcdf_nc_data.F90 and insert

#else
  Integer(C_INT), Parameter :: NC_ENOPAR = -114
  Integer(C_INT), Parameter :: NC_NOQUANTIZE        = 0
  Integer(C_INT), Parameter :: NC_QUANTIZE_BITGROOM = 1

above the #endif at the end of the module

Build netcdff (IFORT) (dependencies will be built first)

To check functionality:

Build and run F77_tests_pres_temp_4D_wr followed by F77_tests_pres_temp_4D_rd
Build and run F90_tests_pres_temp_4D_wr followed by F90_tests_pres_temp_4D_rd

(If netcdf.dll is not in the path, add path=G:\netCDF\netCDF 4.9.2\bin to the Environment item of the Debugging tab of the projects' properties - the example projects can all be selected in Solution Explorer and the Environment item set in all of them at once.)

Lamine06F
Novice
11,853 Views

I would like to thank both of your for your answers. They have helped me a lot.

@mfinnis Your answer was well detailed and I have followed your instructions, but I failed to run Cmake. I download netCDF4.9.2-NC3-64.exe from https://downloads.unidata.ucar.edu/netcdf/  and added it the PATH (pic1.png). Next, I download https://codeload.github.com/Unidata/netcdf-fortran/zip/refs/heads/main   and unzipped it at C:\Program Files\netCDF 4.9.2 (pic2.png). Then I downloaded Cmake from  https://cmake.org/download/  .

Next I tried to run Cmake provinding :

Where is the source code : C:\Program Files\netCDF 4.9.2\netcdf-fortran-main

Where to build the binaries: C:\Program Files\netCDF 4.9.2\netcdf-fortran-main\Mybuild

Then I pressed configure but I got these errors (check pic4.png).

Please what have I missed.

Thanks!

 

 

0 Kudos
mfinnis
New Contributor II
11,842 Views

You can't write to Program Files or any of its subfolders so you need MyBuild to be somewhere else. I think there is config file written to the source directory as well so you can't have the source in Program Files either.

0 Kudos
Lamine06F
Novice
11,814 Views

Hello @mfinnis 

I put MyBuild folder in somewhere (C:/Users/lamin/source/repos/netCDF/MyBuilder) then I run Cmake. I have attached 3 pictures so that you can see the progression. pic5.png shows that some files are not found. pic6.nog shows that how it failed. pic6.png ( I have unchecked BUILD_SHARED_LIBS ) but NETCDF_C_LIBRARY is missing. 

Thank you for your help! 

0 Kudos
mfinnis
New Contributor II
11,791 Views

Try starting again. Delete your build directory and ensure there is nothing in your system path that relates to netCDF. Run CMake again. Attached are the screen shots on my machine of: Configure1.png, post first Configure after which BUILD_SHARED_LIBS is unchecked and NETCDF_C_LIBRARY is set to G:/netCDF/netCDF 4.9.2/lib/netcdf.lib; Configure2.png, post second Configure after which NETCDF_C_INCLUDE_DIR is set to G:/netCDF/netCDF 4.9.2/include; ConfiguringDone.png, post third Configure after which Generate generates the VS solution. ConfigureingDone.txt contains the contents of the status window after the third Configure.

0 Kudos
ChrisVH
Beginner
9,001 Views

Hi! I'm also trying to get netcdf/fortran/VS workflow. I've followed your recommendations but I am now stuck in the cmake build of netcdf fortran librairies. I do have the first expected error mentioning that netcdf-c is required. But I do not have an option available to set up NETCDF_C_LIBRARY. Would you have any idea about why this doesn't show up? Thank you for the help!cmake_screenshot.png  

0 Kudos
mfinnis
New Contributor II
8,944 Views

I would guess that there is a problem with the installation of netCDF. Maybe ensure all versions are fully uninstalled, check the characteristics of the version you want installed, install it and try again. Looking at the CMake script it looks as if NETCDF_C_LIBRARY isn't displayed if the script variables indicate that its location is known. The message about version 4.7.4 or greater being required is written if the function nc_def_var_szip is not found in the indicated library.

0 Kudos
mfinnis
New Contributor II
8,819 Views

The value of NETCDF_C_LIBRARY in CMakeCache.txt in the build directory might give a clue.

0 Kudos
ChrisVH
Beginner
8,788 Views

thank you for the response! I have uninstall the only version of netcdf_c that I could find and reinstalled it. The problem remained the same. 

NETCDF_C_LIBRARY is not mentioned in CMakeCache.txt (attached). That might indicate what the problem is. Only line mentioning netcdf_c is the following: 

//The directory containing a CMake configuration file for netCDF.
netCDF_DIR:PATH=C:/netcdf_C/netCDF 4.9.2/lib/cmake/netCDF

 

which is correct. Any idea of what's going on? Thank you! 

0 Kudos
mfinnis
New Contributor II
8,743 Views

Delete the cache (File|Delete Cache), Configure - follow the instructions above. (Do NOT set netCDF_DIR. For a successful configuration that should remain as netCDF_DIR-NOTFOUND.)

0 Kudos
ChrisVH
Beginner
8,546 Views

Hi - I've deleted the cache and I still get the same error. There are still no way for me to set NETCDF_C_LIBRARY. 

I do not set netCDF_DIR. It's actually not in the CMakeLists file. I guess it is set in a way automatically since the path is mentioned in the cache file. 

I've been digging more, but I can't find a solution online. 

Do you have another idea of what I'm doing wrong here? Maybe there is a way to set NETCDF_C_LIBRARY beforehand in the CMakeLists file? 

0 Kudos
mfinnis
New Contributor II
8,541 Views

Did you delete the build directory as well? If I start with a clean build directory and set the value of netCDF_DIR after the first configure I have the same output as you. (As far as I can tell.) If your display results from the first configure then, as you say, netCDF_DIR is getting set somewhere else - environment maybe. You could try running from the command line and using --trace which may give you a clue.

0 Kudos
ChrisVH
Beginner
8,502 Views

It seems that my mistake was to set the environment path for NETCDF_C (lib,bin,include) in the system environment variables. Deleting those made it work. I've build netcdf-fortran following your instructions and ran the 2 tests successfully. thanks for the help!

0 Kudos
Lamine06F
Novice
11,735 Views

Hello @mfinnis,

Your solution works out well.

My last question is what do mean by this sentence : 'Build netcdff (IFORT) (dependencies will be built first)'. Which dependencies should I build first.

Best regard! 

0 Kudos
mfinnis
New Contributor II
11,680 Views

It was just to point out that building netcdff would also build its dependancies, which can be seen from the Project Dependancies menu item are ZERO_CHECK (a CMake thing I think) and netcdff_c (a small C library required for netcdff). These are built automatically when building netcdff and are why you wil see something like "Build: 3 succeeded, 0 failed, 0 up-to-date, 0 skipped" when building netcdff.

0 Kudos
Lamine06F
Novice
11,612 Views

Hello @mfinnis ,

Sorry to disturb you again. When I build netcdff(ifort), I have a lot error. I have attached a picture. 

0 Kudos
Reply