! <-~~~=| (+) Subroutine to read netCDF file with 3 options: 2d, 3d, 4d ~ lon, lat, time, lev  	=@
! <-~~~=| (-) Only avaiable to read 1 variables                                                	=@
! <-~~~=| Reference: http://spanlib.sourceforge.net/doc/doc_f90_exa.html                       	=@
! <-~~~=| ----------------                                                                     	=@
! <-~~~=| To compile:                                                                          	=@
! <-~~~=|            ifort -L ${netcdf}lib/ -lnetcdff -I ${netcdf}include/ main_program.F90    	=@
! <-~~~=| ----------:									       	=@
! <-~~~=|            where: netcdf="/opt/apps/intel_12.0/linux86_64/netcdf/4.1.3/"             	=@
! <-~~~=|==-------------------------------------------------------------------------------------=@

! <-~~~=| Read 2D netCDF ...
SUBROUTINE rd_ncdf_2d (filenm,lon,lat,nlon,nlat,var,var_nm)
   character (len = *), intent (in) :: filenm, var_nm
   character (len = 15) :: lon_nm, lat_nm
   integer, parameter :: NDIMS = 2
   integer :: ncid, var_id, dims_id(NDIMS)
   integer :: lon_id, lat_id
   integer, intent (in out) :: nlon, nlat
   real, allocatable, dimension (:,:), intent (in out) :: var
   real, allocatable, dimension (:), intent (in out) :: lon, lat

   print*,"   Reading netCDF file: ",filenm,"..."
   call thor ( nf90_open (filenm,nf90_Nowrite,ncid) )
        call thor ( nf90_inq_varid (ncid,var_nm,var_id) )
        call thor ( nf90_inquire_variable (ncid,var_id,dimids = dims_id(1:2)) )
        call thor ( nf90_inquire_dimension (ncid, dims_id(1),name = lon_nm, len = nlon) )
        call thor ( nf90_inquire_dimension (ncid, dims_id(2),name = lat_nm, len = nlat) )

        print*,"      - Longitude's name = ",lon_nm,"; length = ", nlon
        print*,"      - Latitude's name  = ",lat_nm,"; length = ", nlat

        ALLOCATE (lon(nlon),lat(nlat),var(nlon,nlat)) 

        call thor ( nf90_inq_varid (ncid,lon_nm,lon_id) )
        call thor ( nf90_get_var (ncid,lon_id,lon) )
        call thor ( nf90_inq_varid (ncid,lat_nm,lat_id) )
        call thor ( nf90_get_var (ncid,lat_id,lat) )

        call thor ( nf90_get_var(ncid,var_id,var) )
   call thor ( nf90_close(ncid) )
   print*,"   Completed reading file ",filenm

   return
END SUBROUTINE rd_ncdf_2d

! <-~~~=| Read 3D netCDF ...
SUBROUTINE rd_ncdf_3d (filenm,lon,lat,t,nlon,nlat,nt,var,var_nm)
   character (len = *), intent (in) :: filenm, var_nm
   character (len = 15) :: lon_nm, lat_nm, t_nm
   integer, parameter :: NDIMS = 3
   integer :: ncid, var_id, dims_id(NDIMS)
   integer :: lon_id, lat_id, t_id
   integer, intent (in out) :: nlon, nlat, nt
   real, allocatable, dimension (:,:,:), intent (in out) :: var
   real, allocatable, dimension (:), intent (in out) :: lon, lat, t

   print*,"   Reading netCDF file: ",filenm,"..."
   call thor ( nf90_open (filenm,nf90_Nowrite,ncid) )
   call thor ( nf90_inq_varid (ncid,var_nm,var_id) )
        call thor ( nf90_inquire_variable (ncid,var_id,dimids = dims_id(1:3)) )
        call thor ( nf90_inquire_dimension (ncid, dims_id(1),name = lon_nm, len = nlon) )
        call thor ( nf90_inquire_dimension (ncid, dims_id(2),name = lat_nm, len = nlat) )
        call thor ( nf90_inquire_dimension (ncid, dims_id(3),name = t_nm, len = nt) )

        print*,"      - Longitude's name = ",lon_nm,"; length = ", nlon
        print*,"      - Latitude's name  = ",lat_nm,"; length = ", nlat
        print*,"      - Time's name      = ",t_nm,"; length = ", nt

        ALLOCATE (lon(nlon),lat(nlat),t(nt),var(nlon,nlat,nt))
   
        call thor ( nf90_inq_varid (ncid,lon_nm,lon_id) )
        call thor ( nf90_get_var (ncid,lon_id,lon) )
        call thor ( nf90_inq_varid (ncid,lat_nm,lat_id) )
        call thor ( nf90_get_var (ncid,lat_id,lat) )
        call thor ( nf90_inq_varid (ncid,t_nm,t_id) )
        call thor ( nf90_get_var (ncid,t_id,t) )

        call thor ( nf90_get_var(ncid,var_id,var) )
   call thor ( nf90_close(ncid) )
   print*,"   Completed reading file ",filenm

   return
END SUBROUTINE rd_ncdf_3d

!SUBROUTINE rd_ncdf_4d
!END SUBROUTINE rd_ncdf_4d

! <-~~~=| Nf90 checking errors ...
SUBROUTINE thor (stat)
   use netcdf
   integer :: stat
   if ( stat .ne. nf90_noerr) then
      print*, trim(nf90_strerror(stat))
      stop
   endif
END SUBROUTINE
! <-~~~=|==-------------------------------------------------------------------------------------=@
