program driver
!$$$  main program documentation block
!                .      .    .                                       .
! main program: sno2mdl
!   prgmmr: gayno            ORG: NP2                DATE: 2005-dec-16
!
! abstract: interpolates nesdis snow cover and afwa snow depth data
!   to a regional e-grid or global gaussian grid.  program outputs
!   a snow cover and snow depth record in grib format.
!
! program history log:
!   2005-dec-16  gayno     initial version
!
! usage: model specific files use the following naming convention:
!        PREFIX = global or hiresw (nmm regional)
!        GRID   = grid name (t254, centnmm, etc)
!
!   input files:
!     fort.81                       - configuration namelist
!     imsnow96.grb                  - nesdis 4km (96 mesh) ims snow cover data
!     imsnow.grb                    - nesdis 23km (16 mesh) ims snow cover data
!     nam_imsmask                   - nesdis 23km ims land mask
!     PRD.SPPROD.SNODEPH.NHMAMAP    - afwa nh snow depth data
!     PRD.SPPROD.SNODEPH.SHMAMAP    - afwa sh snow depth data
!     afwa_slmask_nh_8              - afwa nh land/sea mask
!     afwa_slmask_sh_8              - afwa sh land/sea mask
!     $PREFIX_latitudes.$GRID.grb   - latitudes on model grid 
!                                     (h point lats for nmm) 
!     $PREFIX_longitudes.$GRID.grb  - longitudes on model grid 
!                                     (h point lons for nmm)
!     $PREFIX_slmask.$GRID.grb      - model land/sea mask
!
!   output files:  (including scratch files)
!     $PREFIX_snow.YYYYMMDDHH.$GRID.grb
!
!   subprograms called:
!   
!     modules:
!       from program_setup:
!         read_config_nml
!
!       from snowdat: 
!         readnesdis, readafwa, snowdat_cleanup
!
!       from sno2mdl:
!         gribit, interp
!
!       from model_grid:
!         thin_mask, read_mdl_grid_info, model_grid_cleanup
!
!     libraries:
!       from sfcutils:
!         interp_bilinear_nesdis, interp_aavg_nesdis, ll2xy_nesdis,
!         full_to_thin
!
!       from bacio:
!         baopenr, baopenw, baclose
!
!       from w3:
!         errexit, putgb, w3tagb, w3tage, aea, getgb, getbgh
!
!   libraries (for NCEP ibm):
!     sfcutils  - land surface utility library
!     bacio     - NCEP byte addressable i/o libaray
!     w3        - NCEP W3 library
!     sp        - NCEP spectral-grid transform library
!     ip        - NCEP ipolates library
!     ESSL      - fast scientific calculation subroutines
!
!   exit states:
!     cond =   0 - successful run
!          =  55 - error in interpolation routine
!          =  57 - bad write of model snow depth data
!          =  58 - bad write of model snow cover data
!          =  59 - bad open of model output grib file
!          =  60 - bad read of sh afwa land mask
!          =  61 - bad open of sh afwa land mask
!          =  62 - bad read of nh afwa land mask
!          =  63 - bad open of nh afwa land mask
!          =  64 - bad read of nh afwa snow depth data
!          =  65 - bad read of sh afwa data header
!          =  66 - bad open of sh afwa snow depth data
!          =  67 - bad read of nh afwa snow depth data
!          =  68 - bad read of nh afwa data header
!          =  69 - bad open of nh afwa snow depth data
!          =  70 - bad degrib of nesdis ims snow cover data
!          =  71 - bad degrib of nesdis ims sea ice data
!          =  72 - bad degrib of nesdis ims data header
!          =  73 - bad open of nesdis ims data
!          =  76 - unknown global model grid
!          =  77 - bad open on configuration namelist
!          =  78 - bad read on configuration namelist
!          =  79 - unrecognized model grid type
!          =  80 - bad open on model latitude grib file
!          =  81 - bad read on model latitude grib header
!          =  82 - bad degrib of model latitude data
!          =  83 - bad open on model longitude file
!          =  84 - bad degrib of model longitude data
!          =  85 - bad open of model land mask file
!          =  86 - bad degrib of model land mask data
!          =  87 - bad open of nesdis ims land mask file
!          =  88 - bad read of nesdis ims land mask data
!
! remarks: the interpolation of nesdis and afwa snow data to the model 
!   grid is as follows:
!
!   in the northern hemisphere: snow cover is determined from the 
!   nesdis data.  if cover exceeds the user-defined % threshold at
!   a model point, then the the afwa depth or a user-defined min
!   depth is used, whichever is greater. 
!
!   in the southern hemisphere: no nesdis data exists.  interpolate
!   afwa depth data.  if model grid point has zero depth, set
!   cover to zero percent.  otherwise, set to 100%
!
!   see module sno2mdl for more details.
!
! attributes:
!   language: f90
!   machine:  ibm RS/6000 SP
!
!$$$

 use snowdat,       only           : readnesdis, &
                                     readafwa,   &
                                     snowdat_cleanup

 use model_grid,    only           : read_mdl_grid_info, &
                                     model_grid_cleanup

 use sno2mdl,       only           : interp

 use program_setup, only           : read_config_nml

 implicit none

 call w3tagb('SNO2MDL',2005,350,0000,'NP2')

 print*,''
 print*,"***********************"
 print*,"*** BEGIN EXECUTION ***"
 print*,"***********************"

!-----------------------------------------------------------------------
! get configuration stuff.
!-----------------------------------------------------------------------

 call read_config_nml

!-----------------------------------------------------------------------
! read nesdis and afwa snow data.
!-----------------------------------------------------------------------

 call readnesdis

 call readafwa

!-----------------------------------------------------------------------
! read information about the model grid to which the
! snow data will be interpolated.
!-----------------------------------------------------------------------

 call read_mdl_grid_info

!-----------------------------------------------------------------------
! interpolate the data to the model grid, then write
! it to a grib file.
!-----------------------------------------------------------------------
 
 call interp

!-----------------------------------------------------------------------
! free up memory
!-----------------------------------------------------------------------

 call model_grid_cleanup
 call snowdat_cleanup

 print*,''
 print*,'****************************'
 print*,'**** NORMAL TERMINATION ****'
 print*,'****************************'

 call w3tage('SNO2MDL')

 stop

 end program driver