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

Include netCDF in my Fortran projet

Lamine06F
Novice
14,104 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,956 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
mfinnis
New Contributor II
4,959 Views

Did you make the edit to module_netcdf_nc_data.F90?

0 Kudos
Lamine06F
Novice
4,872 Views

Yes I did it.

Lamine06F_0-1696624059660.png

 

0 Kudos
mfinnis
New Contributor II
4,416 Views

I can't see all the errors so it's difficult to say. I will add that if I comment out the edit to module_netcdf_nc_data.F90 and rebuild I get the same errors as you appear to have - what I can see is the same and there are 46 errors. Maybe double check that you have done what you think you have done, clean the solution and rebuild.

0 Kudos
Lamine06F
Novice
4,159 Views

I have gone through, but I am still getting the same errors. Here is errors file.

0 Kudos
mfinnis
New Contributor II
4,096 Views

As you can see, module_netcdf_nc_data.F90 compiles ok. When module_netcdf_nf_data.F90 is compiled it fails because NC_NOQUANTIZE and NC_QUANTIZE_BITGROOM are not defined.  The file module_netcdf_nf_data.F90 should use netcdf_nc_data. The edit above ensures that those two parameters are defined. So you need to find out why they're not. Failing that you can set  NF_NOQUANTIZE = 0 and NF_QUANTIZE_BITGROOM = 1 in module_netcdf_nf_data.F90 as they are the only places NC_NOQUANTIZE and NC_QUANTIZE_BITGROOM are used.

What is strange is that module_netcdf_nc_interfaces.F90, which also uses netcdf_nc_data and accesses NC_ENOPAR, the other parameter in the edit, compiles ok.

0 Kudos
andrew_4619
Honored Contributor III
4,698 Views

The first errors are the interesting ones, we don't see that. An error compiling a module then cascades a whole list of consequential errors that are of little interest.

0 Kudos
Lamine06F
Novice
3,846 Views

@mfinnis 

 

After setting  NF_NOQUANTIZE = 0 and NF_QUANTIZE_BITGROOM = 1 in module_netcdf_nf_data.F90, It compile well. Few errors appeared nf_nc_noparallel.F90, and I solve them by setting status=0.

To come to my first question, how to link my netCDF (or MyBuilder) to my Fortran program (attached). 

Thank for your incredible help.

 

 

0 Kudos
mfinnis
New Contributor II
3,742 Views

Using Visual Studio:

Either create a new solution or use an existing solution.

Add New Project|Intel(R) Fortran|Console Application|Empty Project. (Name it and make sure you know where it is.)

Copy netCDF.f90 to the project folder.

Source Files|Add|Existing Item. Select netCDF.90

In project properties:
Add the Configuration Properties|Fortran|General|Additional Include Directories and Configuration Properties|Fortran|General|Preprocessor Definitions copied from one of the example projects in the NC4F solution. (In my case they are G:\netCDF\netcdf-fortran-main-4.6.1\netcdf-fortran-main\fortran;G:\netCDF\netcdf-fortran-main-4.6.1\netcdf-fortran-main\fortran/$(ConfigurationName);G:\netCDF\netcdf-fortran-main-4.6.1\MyBuild\fortran;G:\netCDF\netcdf-fortran-main-4.6.1\MyBuild\fortran/$(ConfigurationName);G:\netCDF\netCDF 4.9.2\include;G:\netCDF\netCDF 4.9.2\include/$(ConfigurationName) and ;_CRT_SECURE_NO_WARNINGS;NCSHORT_T=integer*2;NCBYTE_T=byte;NF_RELAX_COORD_BOUND;NF_HAS_PARALLEL4;NF_HAS_PNETCDF;NF_INT1_IS_C_SIGNED_CHAR;NF_INT2_IS_C_SHORT;NF_INT_IS_C_INT;NF_INT8_IS_C_LONG_LONG;NF_REAL_IS_C_FLOAT;NF_DOUBLEPRECISION_IS_C_DOUBLE;HAVE_TS29113_SUPPORT;CMAKE_INTDIR=\"Debug\" .)
Ensure that the properties are set for the correct Configuration and Platform - in my case Debug and x64.
Add the Configuration Properties|Linker|Input|Additional Dependencies from the NC4F project. (In my case they are $(NOINHERIT) user32.lib ..\..\fortran\Debug\netcdff.lib "G:\netCDF\netCDF 4.9.2\lib\netcdf.lib" "G:\netCDF\netCDF 4.9.2\lib\netcdf.lib" . Though NOTE that the relative path to netcdff.lib will probably have to be replaced by its absolute path. In my case "G:\netCDF\netcdf-fortran-main-4.6.1\MyBuild\fortran\Debug\netcdff.lib" .

With a bit of luck that should build ok.

When you run it, ensure netcdf.dll is in the path. If it isn't and you are running from within Visual Studio (which I think you have to for Debug configurations) add its path to Configuration Properties|Debugging|Environment. (In my case path=G:\netCDF\netCDF 4.9.2\bin )

Running your source, the output was:

Data to be written to NetCDF file:
----------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 10 11
6 7 8 9 10 11 12 13 14 15 16 17
12 13 14 15 16 17 18 19 20 21 22 23
18 19 20 21 22 23 24 25 26 27 28 29
24 25 26 27 28 29 30 31 32 33 34 35
30 31 32 33 34 35 36 37 38 39 40 41
----------------------------------------------------------------

Data read from NetCDF file:
----------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 10 11
6 7 8 9 10 11 12 13 14 15 16 17
12 13 14 15 16 17 18 19 20 21 22 23
18 19 20 21 22 23 24 25 26 27 28 29
24 25 26 27 28 29 30 31 32 33 34 35
30 31 32 33 34 35 36 37 38 39 40 41
----------------------------------------------------------------

bharper
Beginner
136 Views

Hi,

I have tried several times previously to build these Intel Fortran Windows libs without success (!) but your instructions do appear comprehensive. I have previously survived on publicly posted binary updates and wonder why that could not be continued in a collegiate manner? I can't see that the IP licencing would prohibit that? If it is possible I and many others would certainly appreciate it.

Best.

0 Kudos
Lamine06F
Novice
2,055 Views

Dear @mfinnis 

I would like to tell you I can never thank you too much for your help. I would have not been able to arrive at any result without you. I have never liked online discussion group, but you change my mind. From today onward, it will be my main source of solving my problems. 

Best regard!

Reply