;---------------------------------------------------------------------- ; ESMF_regrid_template.ncl ; ; This is a template file for use with ESMF regridding. This is the ; template that uses the single "ESMF_regrid" function to do the ; regridding. There's another template, "ESMF_template.ncl", that ; does the regridding in several individual steps. This can be ; useful if you need to skip any of the steps. ; ; The ESMF_regrid function does the following: ; ; 1. Generates a description file (SCRIP or ESMF) for the source grid. ; ; 2. Generates a description file (SCRIP or ESMF) for the destination ; grid. ; ; 3. Generates the weights file, using the source and destination ; files. ; ; 4. Applies the weights to the data you want to regrid. ; ; 5. Attaches metadata to regridded variable, if appropriate. ; ;---------------------------------------------------------------------- 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, and some data to regrid srcFileName = "test_in.nc" ;;;Details from ncl_filedump -c of test_in.nc: ;;; Copyright (C) 1995-2014 - All Rights Reserved ;;; University Corporation for Atmospheric Research ;;; NCAR Command Language Version 6.2.0 ;;; The use of this software is governed by a License Agreement. ;;; See http://www.ncl.ucar.edu/ for more details. ;;; ;;;Variable: f ;;;Type: file ;;;filename: test_in ;;;path: test_in.nc ;;; file global attributes: ;;; creation_date : Thu Aug 14 12:12:20 GMT 2014 ;;; NCL_Version : 6.2.0 ;;; system : Linux metappdev.afccc.af.mil 2.6.32-431.20.3.el6.x86_64 #1 SMP Fri Jun 6 18:30:54 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux ;;; Conventions : None ;;; grib_source : prate.2014070800.ENSMEAN.ensmean.anom.1x1.grb ;;; title : NCL: convert-GRIB-to-netCDF ;;; dimensions: ;;; forecast_time0 = 7 ;;; lat_3 = 181 ;;; lon_3 = 360 ;;; variables: ;;; float PRATE_3_SFC_ave1m ( forecast_time0, lat_3, lon_3 ) ;;; initial_time : 07/08/2014 (00:00) ;;; model : Climate Data Assimilation System 2 (CDAS2) ;;; parameter_number : 59 ;;; parameter_table_version : 2 ;;; grid_number : 3 ;;; level_indicator : 1 ;;; _FillValue : 1e+20 ;;; units : kg/m^2/s ;;; long_name : Precipitation rate ;;; center : US National Weather Service - NCEP (WMC) ;;; ;;; float lat_3 ( lat_3 ) ;;; units : degrees_north ;;; grid_description : 65160-point (181x360) global longitude-latitude grid. (0,0) at 0E, 90N, matrix layout. N .B.: prime meridian not duplicated. ;;; long_name : latitude ;;; ;;; float lon_3 ( lon_3 ) ;;; units : degrees_east ;;; grid_description : 65160-point (181x360) global longitude-latitude grid. (0,0) at 0E, 90N, matrix layout. N .B.: prime meridian not duplicated. ;;; long_name : longitude ;;; ;;; integer forecast_time0 ( forecast_time0 ) ;;; units : months ;;; long_name : Forecast offset from initial time ;;; ;;;Coordinates: ;;; ;;; forecast_time0: ;;; ;;; 2 3 4 5 6 7 8 ;;; ;;; lat_3: ;;; ;;; 90.00 89.00 88.00 87.00 86.00 85.00 ;;; ... ;;; -84.00 -85.00 -86.00 -87.00 -88.00 -89.00 ;;; -90.00 ;;; ;;; lon_3: ;;; ;;; 0.000 1.000 2.000 3.000 4.000 5.000 ;;; ... ;;; 354.0 355.0 356.0 357.0 358.0 359.0 ;---Input file that contains lat/lon grid of destination grid, if applicable. dstFileName = "test_out.nc" ;;; NOTE: test_out.nc is empty netcdf created from ncgen ;;;Details from ncl_filedump -c of test_out.nc: ;;; Copyright (C) 1995-2014 - All Rights Reserved ;;; University Corporation for Atmospheric Research ;;; NCAR Command Language Version 6.2.0 ;;; The use of this software is governed by a License Agreement. ;;; See http://www.ncl.ucar.edu/ for more details. ;;; ;;;Variable: f ;;;Type: file ;;;filename: test_out ;;;path: test_out.nc ;;; file global attributes: ;;; dimensions: ;;; fcst_time = 7 ;;; lat = 180 ;;; lon = 360 ;;; variables: ;;; float lat ( lat ) ;;; units : degrees_north ;;; actual_range : ( -89.5, 89.5 ) ;;; long_name : Latitude ;;; standard_name : latitude ;;; axis : Y ;;; coordinate_defines : point ;;; ;;; float lon ( lon ) ;;; long_name : Longitude ;;; units : degrees_east ;;; standard_name : longitude ;;; actual_range : ( 0.5, 359.5 ) ;;; axis : X ;;; coordinate_defines : point ;;; ;;; float prate ( fcst_time, lat, lon ) ;;; initial_time : 07/08/2014 (00:00) ;;; model : Climate Data Assimilation System 2 (CDAS2) ;;; parameter_number : 59 ;;; parameter_table_version : 2 ;;; grid_number : 3 ;;; level_indicator : 1 ;;; _FillValue : 1e+20 ;;; units : kg/m^2/s ;;; long_name : 1 month Avg Precipitation rate ;;; center : US National Weather Service - NCEP (WMC) ;;; ;;; integer fcst_time ( fcst_time ) ;;; long_name : MonthsForecast offset from initial time ;;; units : months ;;; ;;;Coordinates: ;;; ;;; fcst_time: ;;; ;;; 2 3 4 5 6 7 8 ;;; ;;; lat: ;;; ;;; 89.50 88.50 87.50 86.50 85.50 84.50 ;;; ... ;;; -84.50 -85.50 -86.50 -87.50 -88.50 -89.50 ;;; ;;; lon: ;;; ;;; 0.5000 1.500 2.500 3.500 4.500 5.500 ;;; ... ;;; 354.5 355.5 356.5 357.5 358.5 359.5 ;---Open the source data file and get some data to regrid sfile = addfile(srcFileName,"r") varname = "PRATE_3_SFC_ave1m" var = sfile->$varname$ ; You may need to subscript this. ;---Open the destination data file dfile = addfile(dstFileName,"r") ;---Don't need to plot, need to write file, so open the output file outFileName = "regrid.nc" ofile = addfile(outFileName, "w") ;---Set up options for regridding Opt = True Opt@SrcGridLat = sfile->lat_3 ; Be sure to use appropriate names Opt@SrcGridLon = sfile->lon_3 ;here Opt@SrcRegional = False ; Necessary if source grid not global Opt@SrcTitle = "NCEP Grid" ; Optional ; Opt@SrcMask2D = where(.not.ismissing(var),1,0) ; Necessary if has ; missing values. ; ; If your destination grid is contained in a file, then use this code. ; Otherwise, if you just want to regrid to a generic grid, like a ; "1x1 degree grid, then skip to the next set of resources. ; dstlat = dfile->lat ; Be sure to use appropriate names dstlon = dfile->lon ; here. Opt@DstGridLon = dstlon Opt@DstGridLat = dstlat ; Opt@DstMask2D = where(.not.ismissing(dstlat).and.\ ; .not.ismissing(dstlon),1,0) ; Necessary if lat/lon ; has missing values. ; ; Use this if you just want to regrid to some generic grid, like a "1x ; "1x1 degree grid. ; ;; Opt@DstGridType = "1x1" ; Opt@DstRegional = False ; Necessary if destination grid not global Opt@ForceOverwrite = True ; Optional, but recommended Opt@PrintTimings = True ; Optional Opt@InterpMethod = "bilinear" ; "patch", "conserve" var_regrid = ESMF_regrid(var,Opt) ofile->var_regrid = varname ;; my attempt to write to output file ; not sure if syntax is right ;;; gets: ;;;file_ext_q: ;;;fatal:["FileSupport.c":4017]:_NclCreateFile: Can not open file: properly. printVarSummary(var_regrid) ; Make sure this looks okay! end ;;;This result from printVarSummary seems right: ;;;Variable: var_regrid ;;;Type: float ;;;Total Size: 1814400 bytes ;;; 453600 values ;;;Number of Dimensions: 3 ;;;Dimensions and sizes: [forecast_time0 | 7] x [lat | 180] x [lon | 360] ;;;Coordinates: ;;; forecast_time0: [2..8] ;;; lat: [89.5..-89.5] ;;; lon: [ 0.5..359.5] ;;;Number Of Attributes: 12 ;;; missing_value : 1e+20 ;;; remap : remapped via ESMF_regrid_with_weights: Bilinear remapping ;;; initial_time : 07/08/2014 (00:00) ;;; model : Climate Data Assimilation System 2 (CDAS2) ;;; parameter_number : 59 ;;; parameter_table_version : 2 ;;; grid_number : 3 ;;; level_indicator : 1 ;;; _FillValue : 1e+20 ;;; units : kg/m^2/s ;;; long_name : Precipitation rate ;;; center : US National Weather Service - NCEP (WMC)