#include <misc.h>
#include <params.h>

subroutine bldfld

!----------------------------------------------------------------------- 
! 
! Purpose: 
!
! Build Master Field List of all possible fields in a history file.  Each field has 
! associated with it a "long_name" netcdf attribute that describes what the field is, 
! and a "units" attribute.
! 
! Method: Call a subroutine to add each field
! 
! Author: Jim Rosinski
! 
!-----------------------------------------------------------------------
   use history
   use pmgrid
!-----------------------------------------------------------------------
   implicit none
!------------------------------Commons----------------------------------
#include <comctl.h>
!-----------------------------------------------------------------------
#include <comtrcnm.h>
!-----------------------------------------------------------------------
!
! Local variables
!
   integer m,j        ! Indices
!
!
!-----------------------------------------------------------------------
!
! Call addfld to add each field to the Master Field List.
!
   call addfld ('PHIS    ','M2/S2   ',1,    'I','surface geopotential')
   call addfld ('PS      ','Pa      ',1,    'A','surface pressure')
   call addfld ('T       ','K       ',plev, 'A','temperature')
   call addfld ('U       ','M/S     ',plev, 'A','zonal wind')
   call addfld ('V       ','M/S     ',plev, 'A','meridional wind')
   call addfld ('ETADOT  ','1/S     ',plevp,'A','vertical (eta) velocity')
   call addfld ('SGH     ','M       ',1,    'I','Standard deviation of orography')
!
! Constituent tracers
!
   do m=1,pcnst
      call addfld (tracnam(m),'KG/KG   ',plev, 'A',trim(tracnam(m))//' constituent tracer')
      call addfld (hadvnam(m),'KG/KGS  ',plev, 'A',trim(tracnam(m))//' horizontal advection')
      call addfld (vadvnam(m),'KG/KGS  ',plev, 'A',trim(tracnam(m))//' vertical advection')
      call addfld (fixcnam(m),'KG/KGS  ',plev, 'A',trim(tracnam(m))//' conservation fixer')
      call addfld (tendnam(m),'KG/KGS  ',plev, 'A',trim(tracnam(m))//' total constituent tendency')
      call addfld (tottnam(m),'KG/KGS  ',plev, 'A',trim(tracnam(m))//' SLT constituent tendency (advection + fixer)')
      call addfld (vdiffnam(m),'KG/KGS ',plev, 'A','Vertical diffusion of '//tracnam(m))
      call addfld (dcconnam(m),'KG/KGS ',plev, 'A','Convective adjustment of '//tracnam(m))
   end do
   do m=2,pcnst
      call addfld (sflxnam(m),'KG/M2/S ',1,    'A',trim(tracnam(m))//' surface flux')
      call addfld (srcnam(m), 'KG/KGS  ',plev, 'A',trim(tracnam(m))//' constituent sources/sinks')
   end do
!
! Non-advected species
!
   do m=pcnst+1,pcnst+pnats
      call addfld(tracnam(m),'UNKNOWN ',plev,'A',trim(tracnam(m))//' constituent tracer')
   end do

   call addfld ('DUH     ','K/S     ',plev, 'A','U horizontal diffusive heating')
   call addfld ('DVH     ','K/S     ',plev, 'A','V horizontal diffusive heating')
   call addfld ('DTH     ','K/S     ',plev, 'A','T horizontal diffusive heating')
   call addfld ('ORO     ','FLAG    ',1,    'I','ocean (0), land (1), sea ice (2) flag')
   call addfld ('SNOWH   ','M       ',1,    'A','Water equivalent snow depth')
   call addfld ('SICTHK  ','M       ',1,    'A','sea ice thickness')
   call addfld ('PRECL   ','M/S     ',1,    'A','Large-scale (stable) precipitation rate')
   call addfld ('PRECC   ','M/S     ',1,    'A','Convective precipitation rate')
   call addfld ('PRECSL  ','M/S     ',1,    'A','Large-scale (stable) snow rate (water equivalent)')
   call addfld ('PRECSC  ','M/S     ',1,    'A','Convective snow rate (water equivalent)')
   call addfld ('SHFLX   ','W/M2    ',1,    'A','Surface sensible heat flux')
   call addfld ('LHFLX   ','W/M2    ',1,    'A','Surface latent heat flux')
   call addfld ('QFLX    ','KG/M2/S ',1,    'A','Surface water flux')
   call addfld ('PBLH    ','M       ',1,    'A','PBL height')
   call addfld ('USTAR   ','M/S     ',1,    'A','Surface friction velocity')
   call addfld ('TREFHT  ','K       ',1,    'A','Reference height temperature')
   call addfld ('CGH     ','K/M     ',plevp,'A','Counter-gradient term for heat in PBL')
   call addfld ('CGQ     ','1/M     ',plevp,'A','Counter-gradient term for moisture in PBL')
   call addfld ('CGS     ','S/M2    ',plevp,'A','Counter-gradient coeff on surface kinematic fluxes')
   call addfld ('TPERT   ','K       ',1,    'A','Perturbation temperature (eddies in PBL)')
   call addfld ('QPERT   ','KG/KG   ',1,    'A','Perturbation specific humidity (eddies in PBL)')
   call addfld ('KVH     ','M2/S    ',plevp,'A','Vertical diffusion diffusivities (heat/moisture)')
   call addfld ('KVM     ','M2/S    ',plevp,'A','Vertical diffusion diffusivities (momentum)')
   call addfld ('DUV     ','M/S2    ',plev, 'A','U vertical diffusion')
   call addfld ('DVV     ','M/S2    ',plev, 'A','V vertical diffusion')
   call addfld ('DTV     ','K/S     ',plev, 'A','T vertical diffusion')
   call addfld ('FSNS    ','W/M2    ',1,    'A','Net solar flux at surface')
   call addfld ('FLNS    ','W/M2    ',1,    'A','Net longwave flux at surface')
   call addfld ('FLNT    ','W/M2    ',1,    'A','Net longwave flux at top')
   call addfld ('FSNT    ','W/M2    ',1,    'A','Net solar flux at top')
   call addfld ('CLOUD   ','FRACTION',plev, 'A','Cloud fraction')
   call addfld ('SETLWP  ','G/M2    ',plev, 'A','Specified liquid water path lengths')
   call addfld ('CLDLWP  ','G/M2    ',plev, 'A','Cloud liq water path length (accounting for cloud fraction)')
   call addfld ('EFFCLD  ','FRACTION',plev, 'A','Effective cloud fraction')
   call addfld ('FLNTC   ','W/M2    ',1,    'A','Clearsky net longwave flux at top')
   call addfld ('FSNTC   ','W/M2    ',1,    'A','Clearsky net solar flux at top')
   call addfld ('FLNSC   ','W/M2    ',1,    'A','Clearsky net longwave flux at surface')
   call addfld ('FSNSC   ','W/M2    ',1,    'A','Clearsky net solar flux at surface')
   call addfld ('OMEGA   ','PA/S    ',plev, 'A','Vertical velocity (pressure)')
   call addfld ('DQP     ','KG/KGS  ',plev, 'A','Specific humidity tendency due to precipitation')
   call addfld ('TAUX    ','N/M2    ',1,    'A','zonal surface stress')
   call addfld ('TAUY    ','N/M2    ',1,    'A','meridional surface stress')
   call addfld ('SRFRAD  ','W/M2    ',1,    'A','Net radiative flux at surface')
   call addfld ('QRS     ','K/S     ',plev, 'A','Solar heating rate')
   call addfld ('QRL     ','K/S     ',plev, 'A','Longwave heating rate')
   call addfld ('CLDTOT  ','FRACTION',1,    'A','Vertically-integrated total cloud')
   call addfld ('CLDLOW  ','FRACTION',1,    'A','Vertically-integrated low cloud')
   call addfld ('CLDMED  ','FRACTION',1,    'A','Vertically-integrated mid-level cloud')
   call addfld ('CLDHGH  ','FRACTION',1,    'A','Vertically-integrated high cloud')
   call addfld ('TOTLWP  ','FRACTION',1,    'A','Vertically-integrated actual liquid water path length')
   call addfld ('LWSH    ','M       ',1,    'A','Liquid water scale height')
!ku --------------------------------------------------------------------
   call addfld ('ALDIF   ','FRACTION',1,    'A','Albedo - longwve - diffuse')
   call addfld ('ASDIF   ','FRACTION',1,    'A','albedo - solar - diffuse')
   call addfld ('ALDIR   ','FRACTION',1,    'A','Albedo - longwave - direct')
   call addfld ('ASDIR   ','FRACTION',1,    'A','albedo - solar - direct')
!ku --------------------------------------------------------------------
!
! Put TS1 on history file since it will be different than TS in a coupled
! run
!
   do m=1,plevmx
      call addfld (tsnam(m),'K       ',1,    'A',tsnam(m)//' subsoil temperature')
   end do

   call addfld ('TS      ','K       ',1,    'A','Surface temperature')
   call addfld ('SOLIN   ','W/M2    ',1,    'A','Solar insolation')
   call addfld ('UTEND   ','M/S2    ',plev, 'A','U tendency')
   call addfld ('VTEND   ','M/S2    ',plev, 'A','V tendency')
   call addfld ('TTEND   ','K/S     ',plev, 'A','T tendency')
   call addfld ('LPSTEN  ','PA/S    ',1,    'A','Surface pressure tendency')
   call addfld ('UTGW    ','M/S2    ',plev, 'A','U tendency - gravity wave drag')
   call addfld ('VTGW    ','M/S2    ',plev, 'A','V tendency - gravity wave drag')
   call addfld ('TAUGWX  ','N/M2    ',1,    'A','Zonal gravity wave drag surface stress')
   call addfld ('TAUGWY  ','N/M2    ',1,    'A','Meridional gravity wave drag surface stress')
   call addfld ('DTCOND  ','K/S     ',plev, 'A','T tendency - convective adjustment')
   call addfld ('CMFDT   ','K/S     ',plev, 'A','T tendency - moist convetion')
   call addfld ('CMFDQ   ','KG/KGS  ',plev, 'A','Q tendency - moist convection')
   call addfld ('ZMDT    ','K/S     ',plev, 'A','T tendency - zhang moist convection')
   call addfld ('ZMDQ    ','KG/KGS  ',plev, 'A','Q tendency - zhang moist convection')
   call addfld ('CMFDQR  ','KG/KGS  ',plev, 'A','Q tendency - moist convective rainout')
   call addfld ('CMFMC   ','KG/M2S  ',plev, 'A','Moist convection mass flux')
   call addfld ('CMFSL   ','W/M2    ',plev, 'A','Moist convection liquid water static energy flux')
   call addfld ('CMFLQ   ','W/M2    ',plev, 'A','Moist convection total water flux')
   call addfld ('CNVCLD  ','FRACTION',1,    'A','Total convective cloud amount')
   call addfld ('FSDS    ','W/M2    ',1,    'A','Flux Shortwave Downwelling Surface')
   call addfld ('FSNIRT  ','W/M2    ',1,    'A','Flux Shortwave Near-IR Absorbed at TOA')
   call addfld ('FSNRTC  ','W/M2    ',1,    'A','Clear-sky Flux Shortwave Near-IR Absorbed at toa')
   call addfld ('FSNIRTSQ','W/M2    ',1,    'A','Flux Shortwave Near-IR Absorbed at toa >= 0.7 microns')
   call addfld ('O3VMR   ','FRACTION',plev, 'A','Ozone volume mixing ratio')
!
! Fields needed such that all variables passed to flux coupler
! also appear on history tape
!
   call addfld ('SOLL    ','W/M2    ',1,    'A','Solar downward near infrared direct  to surface')
   call addfld ('SOLS    ','W/M2    ',1,    'A','Solar downward visible direct  to surface')
   call addfld ('SOLLD   ','W/M2    ',1,    'A','Solar downward near infrared diffuse to surface')
   call addfld ('SOLSD   ','W/M2    ',1,    'A','Solar downward visible diffuse to surface')
!
! Heating rate needed for d(theta)/dt computation
!
   call addfld ('HR      ','K/S     ',plev, 'A','Heating rate needed for d(theta)/dt computation')
!
! Fields which go out active monthly average history files
!
   call addfld ('VT      ','KM/S    ',plev, 'A','Meridional heat transport')
   call addfld ('VZ      ','M3/S3   ',plev, 'A','Meridional transport')
   call addfld ('VQ      ','M/S     ',plev, 'A','Meridional water transport')
   call addfld ('VVPUU   ','M2/S2   ',plev, 'A','Kinetic Energy per Air Mass')
   call addfld ('RELHUM  ','FRACTION',plev, 'A','Relative humidity')
   call addfld ('Z3      ','M       ',plev, 'A','Geopotential Height (above sea level)')
   call addfld ('MQ      ','KG/M2   ',plev, 'A','Water vapor mass in layer')
   call addfld ('PSL     ','PASCALS ',1,    'A','Sea level pressure')
   call addfld ('OMEGAUP ','PA/S    ',plev, 'A','PA/S')
   call addfld ('NUMOMGUP','FRACTION',plev, 'A','FRACTION')
   call addfld ('CLOUDUP ','FRACTION',plev, 'A','FRACTION')
   call addfld ('DPSLON  ','M/S2    ',plev, 'A','M/S2')
   call addfld ('DPSLAT  ','M/S2    ',plev, 'A','M/S2')
!
! Print master field list to stdout
!
   write(6,*)'BLDFLD:nfmaster=',nfmaster
   write(6,*)' ******* MASTER FIELD LIST *******'

   do j=1,nfmaster
      write(6,9000)j,masterlist(j)%name,masterlist(j)%units
9000  format (i5,1x,a8,1x,a8)
   end do

   return
end subroutine bldfld
