"Fossies" - the Fresh Open Source Software Archive

Member "netcdf-fortran-4.4.5/examples/F90/sfc_pres_temp_wr.f90" (31 Jan 2019, 5499 Bytes) of package /linux/misc/netcdf-fortran-4.4.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Fortran 90 source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 ! This is part of the netCDF package.
    2 ! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
    3 ! See COPYRIGHT file for conditions of use.
    4 
    5 ! This example writes some surface pressure and temperatures. It is
    6 ! intended to illustrate the use of the netCDF fortran 90 API. The
    7 ! companion program sfc_pres_temp_rd.f90 shows how to read the netCDF
    8 ! data file created by this program.
    9 
   10 ! This program is part of the netCDF tutorial:
   11 ! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
   12 
   13 ! Full documentation of the netCDF Fortran 90 API can be found at:
   14 ! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
   15 
   16 ! $Id: sfc_pres_temp_wr.f90,v 1.12 2010/04/06 19:32:09 ed Exp $
   17 
   18 program sfc_pres_temp_wr
   19   use netcdf
   20   implicit none
   21 
   22   ! This is the name of the data file we will create.
   23   character (len = *), parameter :: FILE_NAME = "sfc_pres_temp.nc"
   24   integer :: ncid
   25 
   26   ! We are writing 2D data, a 12 x 6 lon-lat grid. We will need two
   27   ! netCDF dimensions.
   28   integer, parameter :: NDIMS = 2
   29   integer, parameter :: NLATS = 6, NLONS = 12
   30   character (len = *), parameter :: LAT_NAME = "latitude"
   31   character (len = *), parameter :: LON_NAME = "longitude"
   32   integer :: lat_dimid, lon_dimid
   33 
   34   ! In addition to the latitude and longitude dimensions, we will also
   35   ! create latitude and longitude netCDF variables which will hold the
   36   ! actual latitudes and longitudes. Since they hold data about the
   37   ! coordinate system, the netCDF term for these is: "coordinate
   38   ! variables."
   39   real :: lats(NLATS), lons(NLONS)
   40   integer :: lat_varid, lon_varid
   41   real, parameter :: START_LAT = 25.0, START_LON = -125.0
   42 
   43   ! We will write surface temperature and pressure fields. 
   44   character (len = *), parameter :: PRES_NAME="pressure"
   45   character (len = *), parameter :: TEMP_NAME="temperature"
   46   integer :: pres_varid, temp_varid
   47   integer :: dimids(NDIMS)
   48 
   49   ! It's good practice for each variable to carry a "units" attribute.
   50   character (len = *), parameter :: UNITS = "units"
   51   character (len = *), parameter :: PRES_UNITS = "hPa"
   52   character (len = *), parameter :: TEMP_UNITS = "celsius"
   53   character (len = *), parameter :: LAT_UNITS = "degrees_north"
   54   character (len = *), parameter :: LON_UNITS = "degrees_east"
   55 
   56   ! We will create some pressure and temperature data to write out.
   57   real, dimension(:,:), allocatable :: temp_out
   58   real, dimension(:,:), allocatable :: pres_out
   59   real, parameter :: SAMPLE_PRESSURE = 900.0
   60   real, parameter :: SAMPLE_TEMP = 9.0
   61 
   62   ! Loop indices
   63   integer :: lat, lon
   64 
   65   ! Allocate memory.
   66   allocate(pres_out(NLONS, NLATS))
   67   allocate(temp_out(NLONS, NLATS))
   68 
   69   ! Create pretend data. If this were not an example program, we would
   70   ! have some real data to write, for example, model output.
   71   do lat = 1, NLATS
   72      lats(lat) = START_LAT + (lat - 1) * 5.0
   73   end do
   74   do lon = 1, NLONS
   75      lons(lon) = START_LON + (lon - 1) * 5.0
   76   end do
   77   do lon = 1, NLONS
   78      do lat = 1, NLATS
   79         pres_out(lon, lat) = SAMPLE_PRESSURE + (lon - 1) * NLATS + (lat - 1)
   80         temp_out(lon, lat) = SAMPLE_TEMP + .25 * ((lon - 1) * NLATS + (lat - 1))
   81      end do
   82   end do
   83 
   84   ! Create the file. 
   85   call check( nf90_create(FILE_NAME, nf90_clobber, ncid) )
   86 
   87   ! Define the dimensions.
   88   call check( nf90_def_dim(ncid, LAT_NAME, NLATS, lat_dimid) )
   89   call check( nf90_def_dim(ncid, LON_NAME, NLONS, lon_dimid) )
   90 
   91   ! Define the coordinate variables. They will hold the coordinate
   92   ! information, that is, the latitudes and longitudes. A varid is
   93   ! returned for each.
   94   call check( nf90_def_var(ncid, LAT_NAME, NF90_REAL, lat_dimid, lat_varid) )
   95   call check( nf90_def_var(ncid, LON_NAME, NF90_REAL, lon_dimid, lon_varid) )
   96 
   97   ! Assign units attributes to coordinate var data. This attaches a
   98   ! text attribute to each of the coordinate variables, containing the
   99   ! units.
  100   call check( nf90_put_att(ncid, lat_varid, UNITS, LAT_UNITS) )
  101   call check( nf90_put_att(ncid, lon_varid, UNITS, LON_UNITS) )
  102 
  103   ! Define the netCDF variables. The dimids array is used to pass the
  104   ! dimids of the dimensions of the netCDF variables.
  105   dimids = (/ lon_dimid, lat_dimid /)
  106   call check( nf90_def_var(ncid, PRES_NAME, NF90_REAL, dimids, pres_varid) )
  107   call check( nf90_def_var(ncid, TEMP_NAME, NF90_REAL, dimids, temp_varid) )
  108 
  109   ! Assign units attributes to the pressure and temperature netCDF
  110   ! variables.
  111   call check( nf90_put_att(ncid, pres_varid, UNITS, PRES_UNITS) )
  112   call check( nf90_put_att(ncid, temp_varid, UNITS, TEMP_UNITS) )
  113 
  114   ! End define mode.
  115   call check( nf90_enddef(ncid) )
  116 
  117   ! Write the coordinate variable data. This will put the latitudes
  118   ! and longitudes of our data grid into the netCDF file.
  119   call check( nf90_put_var(ncid, lat_varid, lats) )
  120   call check( nf90_put_var(ncid, lon_varid, lons) )
  121 
  122   ! Write the pretend data. This will write our surface pressure and
  123   ! surface temperature data. The arrays of data are the same size as
  124   ! the netCDF variables we have defined.
  125   call check( nf90_put_var(ncid, pres_varid, pres_out) )
  126   call check( nf90_put_var(ncid, temp_varid, temp_out) )
  127 
  128   ! Close the file.
  129   call check( nf90_close(ncid) )
  130    
  131   ! If we got this far, everything worked as expected. Yipee! 
  132   print *,"*** SUCCESS writing example file sfc_pres_temp.nc!"
  133 
  134 contains
  135   subroutine check(status)
  136     integer, intent ( in) :: status
  137     
  138     if(status /= nf90_noerr) then 
  139       print *, trim(nf90_strerror(status))
  140       stop 2
  141     end if
  142   end subroutine check  
  143 end program sfc_pres_temp_wr
  144