;---------------------------------------------------------------------- ; ESMF_template.ncl ; ; This is a template file for use with ESMF regridding. This is the ; template that uses four individual steps for regridding. There's ; a similar script that condenses all of the steps by calling ; "ESMF_regrid". This template is "ESMF_regrid_template.ncl" ; ; There are four main steps to regrid data from one grid to another: ; ; 1. Generate a description file (SCRIP or ESMF) for the source grid. ; ; 2. Generate a description file (SCRIP or ESMF) for the destination ; grid. ; ; 3. Generate the weights file, using the source and destination ; files created in #1 and #2. ; ; 4. Apply the weights to the data you want to regrid. ; ;---------------------------------------------------------------------- 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/esmf/ESMF_regridding.ncl" begin ;---Input file that contains lat/lon grid of source grid srcFileName = "sst.nc" ;---Input file that contains lat/lon grid of destination grid, if applicable. dstFileName = "output_grid.nc" ; ; Output (and input) files for grid descriptions and weight file ; These can be whatever names you want. ; srcGridName = "src_SCRIP.nc" ; "src_ESMF.nc" dstGridName = "dst_SCRIP.nc" ; "dst_ESMF.nc" wgtFileName = "NCEP_2_Rect.nc" ; this is just an example ;---Set to True if you want to skip any of these steps SKIP_SRC_GEN = False SKIP_DST_GEN = False SKIP_WGT_GEN = False ;---------------------------------------------------------------------- ; 1. Generate a description file (SCRIP or ESMF) for the source grid. ;---------------------------------------------------------------------- sfile = addfile(srcFileName,"r") x = sfile->X ; You may need to subscript this. if(.not.SKIP_SRC_GEN) then Opt = True Opt@ForceOverwrite = True Opt@PrintTimings = True Opt@Title = "NCEP Grid" ; An example Opt@Mask2D = where(.not.ismissing(x),1,0) ; Necessary if x has ; missing values. ;---If we have 1D lat/lon coordinate arrays. rectilinear_to_SCRIP(srcGridName,x&lat,x&lon,Opt) ;---If we have 2D lat/lon arrays. ; curvilinear_to_SCRIP(srcGridName,sfile->lat,sfile->lon,Opt) ;---If we have unstructured data (for example, X, Y, Z, data) ; unstructured_to_ESMF(srcGridName,sfile->lat,sfile->lon,Opt) ;---Clean up delete(Opt) end if ;---------------------------------------------------------------------- ; 2. Generate a description file (SCRIP or ESMF) for the destination ; grid. ;---------------------------------------------------------------------- ;---If applicable, open file that contains destination grid. dfile = addfile(dstFileName,"r") if(.not.SKIP_DST_GEN) then Opt = True Opt@LLCorner = (/ -60.d, 0.d/) ; default is (/-90,-180/) Opt@URCorner = (/ 60.d, 355.d/) ; default is (/ 90, 180/) Opt@ForceOverWrite = True Opt@PrintTimings = True ;---If we want to generate the destination grid from scratch latlon_to_SCRIP(dstGridName,"5x5",Opt) ;---If we have 1D lat/lon coordinate arrays. ; Mask2D is necessary if dst lat/lon has missing values. ; Opt@Mask2D = where(.not.ismissing(dfile->lat).and.\ ; .not.ismissing(dfile->lon),1,0) ; rectilinear_to_SCRIP(dstGridName,dfile->lat,dfile->lon,Opt) ;---If we have 2D lat/lon arrays. ; Mask2D is necessary if dst lat/lon has missing values. ; Opt@Mask2D = where(.not.ismissing(dfile->lat).and.\ ; .not.ismissing(dfile->lon),1,0) ; curvilinear_to_SCRIP(dstGridName,dfile->lat,dfile->lon,Opt) ;---If we have unstructured data (for example, X, Y, Z, data) ; Mask2D is necessary if dst lat/lon has missing values. ; Opt@Mask2D = where(.not.ismissing(dfile->lat).and.\ ; .not.ismissing(dfile->lon),1,0) ; unstructured_to_ESMF(dstGridName,dfile->lat,dfile->lon,Opt) ;---Clean up delete(Opt) end if ;---------------------------------------------------------------------- ; 3. Generate the weights file, using the source and destination ; files created in #1 and #2. ;---------------------------------------------------------------------- if(.not.SKIP_WGT_GEN) then Opt = True Opt@InterpMethod = "bilinear" ; "patch", "conserve" Opt@ForceOverWrite = True Opt@PrintTimings = True ESMF_regrid_gen_weights(srcGridName,dstGridName,wgtFileName,Opt) ;---Clean up delete(Opt) end if ;---------------------------------------------------------------------- ; 4. Apply the weights to the data you want to regrid. ;---------------------------------------------------------------------- Opt = True ; Opt@Debug = True Opt@PrintTimings = True x_regrid = ESMF_regrid_with_weights(x,wgtFileName,Opt) ;---------------------------------------------------------------------- ; Step 4 ; Plot the original and regridded data. ;---------------------------------------------------------------------- wks = gsn_open_wks("ps","ESMF") ; ESMF.ps res = True ; Plot mods desired. res@gsnDraw = False ; We will panel later. res@gsnFrame = False res@gsnMaximize = True ; Maximize plot res@mpMaxLatF = 90 ; Set accordingly res@mpMinLatF = -90 res@mpMinLonF = -180 res@mpMaxLonF = 180 res@cnFillOn = True ; color plot desired res@cnFillPalette = "gui_default" ; choose colormap for contours res@cnLinesOn = False ; turn off contour lines res@cnLineLabelsOn = False ; turn off contour lines ;;--Change (maybe) mnmxint = nice_mnmxintvl( min(var), max(var), 18, False) res@cnLevelSelectionMode = "ManualLevels" res@cnMinLevelValF = mnmxint(0) res@cnMaxLevelValF = mnmxint(1) res@cnLevelSpacingF = mnmxint(2) res@lbLabelBarOn = False ; Labelbar will be in panel ;---Plot data on original grid res@gsnAddCyclic = False dims = tostring(dimsizes(x)) res@tiMainString = "Original data (" + str_join(dims," x ") + ")" plot_orig = gsn_csm_contour_map(wks,x,res) delete(dims) ;---Plot data on new grid res@gsnAddCyclic = True dims = tostring(dimsizes(x_regrid)) res@tiMainString = "Regridded data (" + str_join(dims," x ") + ")" plot_regrid = gsn_csm_contour_map(wks,x_regrid,res) ;---Resources for paneling pres = True pres@gsnMaximize = True pres@gsnPanelLabelBar = True gsn_panel(wks,(/plot_orig,plot_regrid/),(/2,1/),pres) end