; --------------------------------------------------------------------------- ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;  Program Name: radar2Hydro_forcing_ESMFregrid_5min.ncl                      ;
;                                                                             ;
;  National Water Model (NWM) WRF-hydro forcing engine is developed by        ;
;  National Center for Atmospheric Research (NCAR), under the sponsorship     ;
;  of National Water Center (NWC).                                            ;
;                                                                             ;
;   Team Members:                                                             ;
;     NCAR Staff: Linlin Pan, Wei Yu, and David Gochis                        ;
;      NWC/OWP Staff: Brian Cosgrove, Zhengtao Cui, Cham Pham, and James Taft ;
;                                                                             ;
;  This is a ncl program to perform  regridding.                              ;
;                                                                             ;
;  Input: gfs file, weighting function, output file                           ;
;                                                                             ;
;  Output: regridded  file                                                    ;
;                                                                             ;
; For non-fatal errors output is witten to $DATA/logs                         ;
;                                                                             ;
; Author(s)/Contact(s): Linlin Pan, lpan@ucar.edu                             ;
; Origination                                                  Sept., 2015    ;
;                                                                             ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;----------------------------------------------------------------------
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/ut_string.ncl"
;load "./ESMF_regridding.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/esmf/ESMF_regridding.ncl"

begin

;----------------------------------------------------------------------
; Source, destination , and weight filenames for generating 
; ESMF_regridding weights
;----------------------------------------------------------------------
    wgtFileName_conserve =  wgtFileName_in

  if ( .not.isfilepresent( dstGridName ) ) then
     print( " ... source grid file not found : "+ dstGridName )
     exit
  end if

;----------------------------------------------------------------------
; read in source and destination grid netCDF files
;----------------------------------------------------------------------

;---  destination grid data
  dstfile     = addfile( dstGridName ,"r")
  dlon3d=dstfile->XLONG_M   ;(USER'S NEED TO CONFIRM THIS VARIABLE IS WHAT IS IN THEIR DATA FILE)
  dlat3d=dstfile->XLAT_M   ;(USER'S NEED TO CONFIRM THIS VARIABLE IS WHAT IS IN THEIR DATA FILE)

  dlon2d=dlon3d(0,:,:)
  dlat2d=dlat3d(0,:,:)

  dims=dimsizes(dlat2d)
  outFile = getenv("outFile")

;
;dg NEED TO EDIT INPUT DATA TIME INTERVAL HERE...
;
  dt=3600.0   ;forcing data timestep in seconds... (USER'S MUST ENTER/CONFIRM THIS IS SET TO DATA TIMESTEP (or INTERVAL))
  flag=0    ;WRF - flag for removing accum precip... (DO NOT CHANGE THIS)

;----------------------------------------------------------------------
; Open source data files to be regridded...
;----------------------------------------------------------------------
;
;dg NEED TO EDIT INPUT AND OUTPUT DIRECTORIES HERE...
;

  srcfilename = getenv ("srcFile")
  datfils = systemfunc ("/bin/ls -1 "+srcfilename)    ;list of file names
  num_datfils     = dimsizes(datfils)

   wgtFileName = wgtFileName_in

        opt                = True
        opt@WgtFileName    = wgtFileName
        opt@CopyVarAtts    = True
        opt@CopyVarCoords  = False

        opt@Debug          = True



  do ifil = 0,num_datfils-1,1   ; loop through datafiles one at a time

   
      datfile = addfile( datfils(ifil), "r")

      print( " ... Open input file : "+ datfils(ifil) )
  
;      if(.not. isfilevar(datfile,"VAR_209_6_9_P0_L102_GLL0")) then
;           exit()
;      end if 
     if(isfilevar(datfile,"VAR_209_6_8_P0_L102_GLL0")) then
	if ( ifil .eq. 0 ) then
	  precip_rate_ave =  datfile->VAR_209_6_8_P0_L102_GLL0/num_datfils
	else
	  precip_rate_ave =  precip_rate_ave + datfile->VAR_209_6_8_P0_L102_GLL0/num_datfils
	end if
     else
	if ( ifil .eq. 0 ) then
	  precip_rate_ave =  datfile->VAR_209_6_2_P0_L102_GLL0/num_datfils
	else
	  precip_rate_ave =  precip_rate_ave + datfile->VAR_209_6_2_P0_L102_GLL0/num_datfils
	end if
     endif
  
   end do   ; end do for file loop

     ;----------------------------------------------------------------------
     ; Temporary output
     ;----------------------------------------------------------------------
      if(isfilepresent(outFile) ) then
         system("rm -f "+outFile)
      end if

      ncdf= addfile(outFile,"c")
      ncdf->lat = dlat2d   ;output lat
      ncdf->lon = dlon2d   ;output lon

   
     ;----------------------------------------------------------------------
     ;  Processing...no further mods should be required...
     ;----------------------------------------------------------------------
     ;do v=6,6

     ;      precip_rate = ESMF_regrid_with_weights(datfile->VAR_209_6_2_P0_L102_GLL0 , wgtFileName, opt)
           precip_rate = ESMF_regrid_with_weights(precip_rate_ave , wgtFileName, opt)
           precip_rate = precip_rate / 3600.0
           precip_rate@description = "RAINRATE"
           precip_rate@units       = "mm s-1"
           ncdf->precip_rate = precip_rate

   


end