/
Example netcdf program

Example netcdf program

program test_netcdf
! -----------------------------------------------------------------------
! The drunken divers testcase
! some lines are stolen from unidata - netcdf testprograms
! -----------------------------------------------------------------------

  use netcdf
  
  implicit none
  
  character*256     :: outputfile='divers_birth_day_drink.nc'
 
  integer :: stdout=6

!  include 'netcdf.inc'
  integer           :: ncid, k
  integer           :: latdim, londim, depthdim, timedim
  integer           :: vardims(4)
  integer           :: latid, lonid, depthid, timeid, ndepth=5
  integer           :: varid
  real              :: depth(5), drinks(1,1,5,1), degeast, degnorth
  real*8            :: rdays
  character (len = *), parameter :: varunit = "glasses"
  character (len = *), parameter :: varname = "number of drinks"
  character (len = *), parameter :: varshort = "drinks" 
  character (len = *), parameter :: units = "units"
  character (len = *), parameter :: long_name = "long_name"
  character (len = *), parameter :: lat_name = "latitude"
  character (len = *), parameter :: lon_name = "longitude"
  character (len = *), parameter :: lat_units = "degrees_north"
  character (len = *), parameter :: lon_units = "degrees_east"
  character (len = *), parameter :: depth_units = "m"
  character (len = *), parameter :: time_units = "days since 2000-01-01 00:00:00"
  character (len = *), parameter :: origin = "time_origin"
  character (len = *), parameter :: origin_val = "1-jan-2000 00:00:00" 
! -----------------------------------------------------------------------
!   define where and when the diver dives and 
!   in which depth he has how much birthday drinks
! -----------------------------------------------------------------------

    degnorth = 57.02
    degeast  = 20.3
    rdays    = 10.0
    do k=1, 5
      depth(k)  = float(k)*float(k)
      drinks(1,1,k,1) = depth(k)
    enddo
 
! -----------------------------------------------------------------------
!   create the file
! -----------------------------------------------------------------------
    
    call check( nf90_create(outputfile, nf90_clobber, ncid))
    write(stdout,*) 'file ',trim(outputfile),' has been created '
! -----------------------------------------------------------------------
!   define axis
! -----------------------------------------------------------------------

    call check( nf90_def_dim(ncid, 'longitude', 1, londim))
    call check( nf90_def_dim(ncid, 'latitude' , 1, latdim))
    call check( nf90_def_dim(ncid, 'depth' ,    ndepth, depthdim))
    call check( nf90_def_dim(ncid, 'time'     , nf90_unlimited, timedim))
    call check( nf90_def_var(ncid, lon_name, nf90_real, londim, lonid))
    call check( nf90_def_var(ncid, lat_name, nf90_real, latdim, latid))
    call check( nf90_def_var(ncid, 'depth',     nf90_real, depthdim, depthid))
    call check( nf90_def_var(ncid, 'time',      nf90_real, timedim, timeid))

    call check( nf90_put_att(ncid, latid, units, lat_units) )
    call check( nf90_put_att(ncid, lonid, units, lon_units) )
    call check( nf90_put_att(ncid, depthid, units, depth_units))
    call check( nf90_put_att(ncid, timeid,  units, time_units))
    call check( nf90_put_att(ncid, timeid,  origin, origin_val))
 
    vardims(1) = londim
    vardims(2) = latdim
    vardims(3) = depthdim
    vardims(4) = timedim
    
! -----------------------------------------------------------------------
!   define variables
! -----------------------------------------------------------------------
    call check( nf90_def_var(ncid, trim(varshort), nf90_real, vardims, varid))
    call check( nf90_put_att(ncid, varid, units ,trim(varunit)))
    call check( nf90_put_att(ncid, varid, long_name, trim(varname)))

    call check( nf90_enddef(ncid))
! -----------------------------------------------------------------------
!   now write something
! -----------------------------------------------------------------------

    call check( nf90_put_var(ncid, latid, degnorth))
    call check( nf90_put_var(ncid, lonid, degeast))
    call check( nf90_put_var(ncid, depthid, depth))
    
    call check( nf90_put_var(ncid, timeid, rdays))
    
    call check( nf90_put_var(ncid, varid, drinks))
!-----------------------------------------------------------------------
!   ready
!-----------------------------------------------------------------------
    call check( nf90_close(ncid))

contains
  subroutine check(status)
    integer, intent ( in) :: status
    
    if(status /= nf90_noerr) then 
      print *, trim(nf90_strerror(status))
      stop "stopped"
    end if
  end subroutine check  
end program test_netcdf

Related content

netCDF
More like this
NCO
More like this
Install netCDF
Install netCDF
More like this
Slow execution of nccopy from netcdf 4.6.3
Slow execution of nccopy from netcdf 4.6.3
More like this
Linking with the netcdf library
Linking with the netcdf library
More like this
NcView
More like this