Re: [netcdfgroup] What is the "standard" way to use NetCDF from Fortran 90?

  • To: Ed Hartnett <ed@xxxxxxxxxxxxxxxx>
  • Subject: Re: [netcdfgroup] What is the "standard" way to use NetCDF from Fortran 90?
  • From: Paul van Delst <Paul.Vandelst@xxxxxxxx>
  • Date: Tue, 14 Jul 2009 17:23:26 -0400
Ed Hartnett wrote:
Magnus Hagdorn <Magnus.Hagdorn@xxxxxxxx> writes:

F90 source code is portable (as long as it is standard conforming and
you have a decent compiler...)

Howdy Magnus!

As noted, fortran compilers do not produce compatible .mod files. This
can be a pain.

In practice it means you must build netCDF for every fortran compiler
you want to use on the machine. That is, if you want to use ifort and
gfortran on the same machine, you need to build the library twice, and
install them in different places. Then you have to use the correct one
when compiling fortran.

FWIW, once I've compiled the netCDF f90 code with various compilers, I use the shell script below to link in stuff to a default netCDF install location (typically $HOME/local/netcdf but I can also use an envar, $NETCDF_ROOT) for all the compilers I use on my linux box.

All of my makefiles then simply point to
  $HOME/local/netcdf/include
and
  $HOME/local/netcdf/lib
for the generic files no matter what compiler du jour I'm using.

Anyway....

cheers,

paulv


<-----begin Link_netCDF----->
#!/bin/sh

# --------------
# Usage function
# --------------
usage()
{
  echo
  echo "Usage: Link_netCDF  CompilerId"
  echo
  echo "  CompilerId: The identifier for the Fortran90 compiler used"
  echo "              to build the netCDF library, and used to identify"
  echo "              which library and .mod files to link."
  echo "              Valid identifiers are:"
  echo "                 Intel   : The Intel f95 compiler"
  echo "                 PGI     : The Portland Group f95 compiler"
  echo "                 Lahey   : The Lahey f95 compiler"
  echo "                 gfortran: The GNU Fortran compiler"
  echo "                 g95     : The g95 compiler"
  echo "                 Clear   : Specifying this clears all links"
  echo
}

# ----------------
# Linking function
# ----------------
link_files()
{
  # Set up
  # ...Error codes
  SUCCESS=0
  FAILURE=1
  # ...Assign arguments
  PACKAGE=$1
  shift 1
  FILE_LIST=$*

  # Check package
  if [ "${PACKAGE}" = "Clear" ]; then
    rm ${FILE_LIST}
  else
    for FILE in ${FILE_LIST}; do
      if [ -f ${FILE}.${PACKAGE} ]; then
        ln -sf ${FILE}.${PACKAGE} ${FILE}
      else
        echo " File ${FILE}.${PACKAGE} not found!"
        return ${FAILURE}
      fi
    done
  fi
  return ${SUCCESS}
}

# -----------
# Main script
# -----------
# Set up
# ...Error codes
SUCCESS=0
FAILURE=1
# ...Test for package argument and assign
if [ $# -ne 1 ]; then
  usage; exit ${FAILURE}
fi
PACKAGE=$1
# ...Location of netCDF installs
if [ -z "${NETCDF_ROOT}" ]; then
  NETCDF_ROOT="${HOME}/local/netcdf"
fi
# ...Generic link filenames
INCLUDE_FILE="netcdf.mod typesizes.mod"
LIB_FILE="libnetcdf.a"
# ...Save current location
CURRENT_DIR=${PWD}

# Link in the include files
cd ${NETCDF_ROOT}/include
link_files ${PACKAGE} ${INCLUDE_FILE}
if [ $? -ne ${SUCCESS} ]; then
  exit ${FAILURE}
fi

# Link in the library files
cd ${NETCDF_ROOT}/lib
link_files ${PACKAGE} ${LIB_FILE}
if [ $? -ne ${SUCCESS} ]; then
  exit ${FAILURE}
fi

# Back where you came from
cd ${CURRENT_DIR}
<-----end Link_netCDF----->


So if I do:

$ : Link_netCDF Lahey

I get the following:

$ : dir ~/local/netcdf/include/ ~/local/netcdf/lib/
~/local/netcdf/include/:
total 1.5M
-rw-r--r-- 1 wd20pd wd4 9.6K Nov  7  2008 ncvalues.h
-rw-r--r-- 1 wd20pd wd4  31K Nov  7  2008 netcdf.h
-rw-r--r-- 1 wd20pd wd4   23 Nov  7  2008 netcdf.hh
-rw-r--r-- 1 wd20pd wd4  49K Nov  7  2008 netcdf.inc
lrwxrwxrwx 1 wd20pd wd4   16 Jul 14 17:15 netcdf.mod -> netcdf.mod.Lahey
-rw-r--r-- 1 wd20pd wd4  51K Oct 27  2006 netcdf.mod.Lahey
-rw-r--r-- 1 wd20pd wd4 790K Apr 13  2006 netcdf.mod.PGI
-rw-r--r-- 1 wd20pd wd4 160K Sep 11  2007 netcdf.mod.g95
-rw-r--r-- 1 wd20pd wd4 233K Nov  7  2008 netcdf.mod.gfortran
-rw-r--r-- 1 wd20pd wd4  19K Nov  7  2008 netcdfcpp.h
lrwxrwxrwx 1 wd20pd wd4   19 Jul 14 17:15 typesizes.mod -> typesizes.mod.Lahey
-rw-r--r-- 1 wd20pd wd4  456 Oct 27  2006 typesizes.mod.Lahey
-rw-r--r-- 1 wd20pd wd4 1.4K Apr 13  2006 typesizes.mod.PGI
-rw-r--r-- 1 wd20pd wd4 1.7K Sep 11  2007 typesizes.mod.g95
-rw-r--r-- 1 wd20pd wd4 2.4K Nov  7  2008 typesizes.mod.gfortran

~/local/netcdf/lib/:
total 3.2M
lrwxrwxrwx 1 wd20pd wd4   17 Jul 14 17:15 libnetcdf.a -> libnetcdf.a.Lahey
-rw-r--r-- 1 wd20pd wd4 902K Oct 27  2006 libnetcdf.a.Lahey
-rw-r--r-- 1 wd20pd wd4 523K Apr 13  2006 libnetcdf.a.PGI
-rw-r--r-- 1 wd20pd wd4 625K Sep 11  2007 libnetcdf.a.g95
-rw-r--r-- 1 wd20pd wd4 609K Nov  7  2008 libnetcdf.a.gfortran
-rw-r--r-- 1 wd20pd wd4 470K Nov  7  2008 libnetcdf_c++.a


Similarly,

$ : Link_netCDF PGI

produces:

$ : dir ~/local/netcdf/include/ ~/local/netcdf/lib/
~/local/netcdf/include/:
total 1.5M
-rw-r--r-- 1 wd20pd wd4 9.6K Nov  7  2008 ncvalues.h
-rw-r--r-- 1 wd20pd wd4  31K Nov  7  2008 netcdf.h
-rw-r--r-- 1 wd20pd wd4   23 Nov  7  2008 netcdf.hh
-rw-r--r-- 1 wd20pd wd4  49K Nov  7  2008 netcdf.inc
lrwxrwxrwx 1 wd20pd wd4   14 Jul 14 17:16 netcdf.mod -> netcdf.mod.PGI
-rw-r--r-- 1 wd20pd wd4  51K Oct 27  2006 netcdf.mod.Lahey
-rw-r--r-- 1 wd20pd wd4 790K Apr 13  2006 netcdf.mod.PGI
-rw-r--r-- 1 wd20pd wd4 160K Sep 11  2007 netcdf.mod.g95
-rw-r--r-- 1 wd20pd wd4 233K Nov  7  2008 netcdf.mod.gfortran
-rw-r--r-- 1 wd20pd wd4  19K Nov  7  2008 netcdfcpp.h
lrwxrwxrwx 1 wd20pd wd4   17 Jul 14 17:16 typesizes.mod -> typesizes.mod.PGI
-rw-r--r-- 1 wd20pd wd4  456 Oct 27  2006 typesizes.mod.Lahey
-rw-r--r-- 1 wd20pd wd4 1.4K Apr 13  2006 typesizes.mod.PGI
-rw-r--r-- 1 wd20pd wd4 1.7K Sep 11  2007 typesizes.mod.g95
-rw-r--r-- 1 wd20pd wd4 2.4K Nov  7  2008 typesizes.mod.gfortran

~/local/netcdf/lib/:
total 3.2M
lrwxrwxrwx 1 wd20pd wd4   15 Jul 14 17:16 libnetcdf.a -> libnetcdf.a.PGI
-rw-r--r-- 1 wd20pd wd4 902K Oct 27  2006 libnetcdf.a.Lahey
-rw-r--r-- 1 wd20pd wd4 523K Apr 13  2006 libnetcdf.a.PGI
-rw-r--r-- 1 wd20pd wd4 625K Sep 11  2007 libnetcdf.a.g95
-rw-r--r-- 1 wd20pd wd4 609K Nov  7  2008 libnetcdf.a.gfortran
-rw-r--r-- 1 wd20pd wd4 470K Nov  7  2008 libnetcdf_c++.a



  • 2009 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: