Topic: netCDF4

I'm new to SimplyFortran. I have code, which requires access to netCDF4. My program has a 'use netcdf' statement. Where should I put the netCDF package and associated netcdf.mod file?

Re: netCDF4

You can place the NetCDF files anywhere you wish, but you'll need to configure your Simply Fortran project to look for them in the appropriate location.  You can configure the directories that are searched for libraries and modules in the Project Options window under the File Locations category

One word of caution: the netcdf.mod file will have to be compatible with our compiler.  If it was created with Simply Fortran or any GNU Fortran release from version 10 and up, it should work fine.  If it was created with an older version of GNU Fortran or a different compiler, you're going to see an error message.

Jeff Armstrong
Approximatrix, LLC

Re: netCDF4

Thank you very much for the quick reply Jeff.

I'm struggling a bit with this however. Specifically, I get a make error but I'm not sure if it is because I have not pointed to the netcdf.mod file properly or if it has been compiled with an incorrect version of Fortran. Here's the error I get:
_____________________________________

==============================================================================
Generating Makefile... Okay
==============================================================================
Generating target
Undefined symbols for architecture x86_64:
  "___netcdf_MOD_nf90_close", referenced from:
      _quickwrite_ in GeoSobel_Main-3.20.o
      _readlatlon_ in GeoSobel_Main-3.20.o
      _MAIN__ in GeoSobel_Main-3.20.o
      _checkdimensions_ in GeoSobel_Main-3.20.o
      _readpixelspacing_ in GeoSobel_Main-3.20.o
      _getattributes_ in GeoSobel_Main-3.20.o
      _getlatlonattributes_ in GeoSobel_Main-3.20.o
      ...
  "___netcdf_MOD_nf90_copy_att", referenced from:
      _generateglobals_ in GeoSobel_Main-3.20.o
...
  "___netcdf_MOD_nf90_strerror", referenced from:
      _handle_err_ in GeoSobel_Main-3.20.o
ld: symbol(s) not found for architecture x86_64
Error: Last command making (target) returned a bad status
Error: Make execution terminated
_____________________________________

I used brew to upgrade my version of netcdf but I'm not sure which version of fortran that was used to build netcdf. I'm guessing that it is gFortran and probably a pretty recent vintage. The version of gFortran on my machine appears to be 11.2.

petercornillon@satdat1 ~ % which gfortran
/usr/local/bin/gfortran
petercornillon@satdat1 bin % cd /usr/local/bin         
petercornillon@satdat1 bin % ls -l gfortran*
lrwxr-xr-x  1 petercornillon  admin  35 Jan  6 12:59 gfortran -> ../Cellar/gcc/11.2.0_3/bin/gfortran
lrwxr-xr-x  1 petercornillon  admin  38 Jan  6 12:59 gfortran-11 -> ../Cellar/gcc/11.2.0_3/bin/gfortran-11
petercornillon@satdat1 bin %

Re: netCDF4

You would have seen a different message if the module was incompatible.  You're fine with the module you have, but it looks like the linker isn't linking with netcdf.  You'll need to add the proper library (or libraries) to Project Options under the Compiler Flags section.

In the Compiler Flags section, you might be able to get away with adding just the following to the box labelled "Linker:"

-lnetcdf

On Intel Macs, there's a good chance it should work.  If not, though, you can return to the terminal and enter the command:

pkg-config --libs netcdf

Whatever the string returned by the above command turns out to be, copy it in its entirety and paste it into that "Linker" box we discussed above.  On my Apple Silicon Mac, for example, I did actually have to include a path to the library as well as the library flag itself.

Let us know if the above works!

Jeff Armstrong
Approximatrix, LLC

Re: netCDF4

Bummer - it didn't work or I did it incorrectly. Here's what I did:

petercornillon@satdat1 ~ % pkg-config --libs netcdf
-L/usr/local/Cellar/netcdf/4.8.1/lib -lnetcdf

Captured "-L/usr/local/Cellar/netcdf/4.8.1/lib -lnetcdf" and pasted it into Compiler Flags, saved the project and clicked on Build. I got the same error.

So then I pasted "/usr/local/Cellar/netcdf/4.8.1/lib" in the File Locations window. Specifically, I pasted it into the skinny box with the Browse, Add buttons to the right of it under the big box labeled "Modules/Include Files", clicked Add and then Save Options As Defaults, saved the project and clicked on Browse again. I got the same error.

I'm feeling a bit stupid at this point and apologize for imposing on you this way.

Re: netCDF4

The problem is that it can't find the library; it does seem to be able to find the modules and include files.  You need to paste that string in the box marked Linker in Compiler Flags.  Is that what you did?  If so, you should see some different errors if it still isn't working.

You could also use the File Locations, but let's focus on the more direct route by using Compiler Flags for now.

Jeff Armstrong
Approximatrix, LLC

Re: netCDF4

Yes, I had pasted the string in the Linker box in Compiler Flags.

Here's a link to a screenshot <<It didn't allow me to include the link to the screenshot. It says that I can use Images but when I try it doesn't seem to like it. Specifically, I can have left square bracket url or image right square bracket, the url and then then left square bracket \img or \url \right square bracket. Is there a way that I can send you a screenshot? Following is the rest of the message I wrote before discovering that I couldn't attach the screenshot.>>. On the right hand side of the screenshot is the SimplyFortran App with the Project Options open, showing what I pasted into the Linker box. On the left hand side of the screenshot is another screenshot showing the Modules/Include Files locations I've pasted into it, including the one pointing to the location for the netCDF lib files - I think - and a terminal window showing how I obtained these locations. I tried it all again and it still doesn't seem to be working.

Re: netCDF4

First, image posting on the forum is maybe not what one expects.  The image must be hosted elsewhere, and the img tag requires a url to the hosted image.  We don't have any image upload capability.  Hopefully, it didn't just reject a url to a hosted image... If you want to send one to me, though, you can email it to support@approximatrix.com.

Second, in the File Locations category, if you want to use it, you need to add /usr/local/Cellar/netcdf/4.8.1/lib under the Libraries tab.  The linker is having trouble finding the library, most likely a file named /usr/local/Cellar/netcdf/4.8.1/lib/libnetcdf.a.  In the Include/Module tab, you need to add  /usr/local/Cellar/netcdf/4.8.1/include, which should be where the module files are.  The linker has no knowledge of "modules" in reality; they are only useful to the Fortran compiler.

If you take the step above of adding the library directory properly, you can change your Linker entry to just be:

-lnetcdf

since the File Locations entry, under the Libraries tab contains the directory to search for said library.

I'm assuming in this process that you're trying to use netCDF as installed by homebrew, correct?

Please do send that screenshot, though.  We'll get this sorted out.  Something minor is just slightly amiss.

Jeff Armstrong
Approximatrix, LLC

Re: netCDF4

I just received your screenshot, and I think that additional libraries need to be linked.  Try changing your Linker flags to be:

-L/usr/local/Cellar/netcdf/4.8.1/lib -lnetcdff -lnetcdf

Note that the first library, -lnetcdff ends with two fs.  I think that the main netcdf library doesn't contain the necessary Fortran functions.

I'll send this same message via email.  Let us know if it works!

Jeff Armstrong
Approximatrix, LLC

Re: netCDF4

That works. I did get a warning:

ld: warning: dylib (/usr/local/Cellar/netcdf/4.8.1/lib/libnetcdff.dylib) was built for newer macOS version (11.5) than being linked (11.3)

I assume that I don’t have to worry about this?

Thanks a million!

Re: netCDF4

I think you should be fine with that warning.  I'm glad to hear things are working!

Jeff Armstrong
Approximatrix, LLC

Re: netCDF4

Thanks again Jeff.