# [ncl-talk] Dealing with 2D lat and lon outputs of WRF

Dennis Shea shea at ucar.edu
Fri Oct 4 16:26:04 MDT 2019

```As noted by Rick, the *grad_latlon_cfd*
requires a rectilinear grid.
One-dimensional lat/lon arrays. This is clear in both the text and
the function prototyping

(
z          : numeric,  ; float, double, integer only
*lat    [*]* : numeric,
*lon    [*]* : numeric,
cyclic [1] : logical,
opt    [1] : logical
)

===============================================================
(""wrfout_d04_2005-07-29_12:06:00","r")
ctt    = *wrf_user_getvar*
<http://www.ncl.ucar.edu/Document/Functions/WRF_arw/wrf_user_getvar.shtml>(a,"ctt",-1)

*printVarSummary*
<http://www.ncl.ucar.edu/Document/Functions/Built-in/printVarSummary.shtml>(ctt)
; Examine variable
opt_sd = True
opt_sd at PrintStat = True
stat_ctt = *stat_dispersion*
<http://www.ncl.ucar.edu/Document/Functions/Contributed/stat_dispersion.shtml>(ctt,
opt_sd )  ; look at distribution

lat2d  = *wrf_user_getvar*(a,"XLAT",0)
lon2d  = *wrf_user_getvar*(a,"XLONG",0)

dim2d  = *dimsizes*(lat2d)
nlat   = dim2d(0)     ; arbitrary; here (roughly same resolution as WRF)
mlon   = dim2d(1)
; generate rectilinear coordinate variables
lat    = *fspan*( *min*(lat2d), *max*(lat2d) ,nlat)
lon    = *fspan*( *min*(lon2d), *max*(lon2d) ,mlon)
lat!0 = "lat"
lon!0 = "lon"
lat at units = "degrees_north"
lon at units = "degrees_east"

; Bilinear interpolation to rectilinear ['regular'] grid

cttgrd = *rcm2rgrid_Wrap*(lat2d,lon2d,ctt,lat,lon,0)
*printVarSummary*(cttgrd)
print("-----")

; Zonal and Meridional gradients

(cttgrd, lat, lon, False, False)

; Extract variables from the list variable
[meridional] direction

*printVarSummary*
; Examine variable
stat_ctt_lat = *stat_dispersion*
opt_sd )  ; look at distribution
print("-----")

*printVarSummary*
stat_ctt_lon = *stat_dispersion*
opt_sd )  ; look at distribution
print("-----")

; Interpolate gradients back to WRF grid

<http://www.ncl.ucar.edu/Document/Functions/Contributed/rgrid2rcm_Wrap.shtml>
(lat, lon, ctt_grad_lat, lat2d, lon2d, 0)
<http://www.ncl.ucar.edu/Document/Functions/Contributed/rgrid2rcm_Wrap.shtml>
(lat, lon, ctt_grad_lon, lat2d, lon2d, 0)
*printVarSummary*
*printVarSummary*

===

There are multiple example of this methodology using ESMF_regrid
*http://www.ncl.ucar.edu/Applications/ESMF.shtml *
<http://www.ncl.ucar.edu/Applications/ESMF.shtml>

Examples: 29, 30, 37 [others there also]

On Fri, Oct 4, 2019 at 9:59 PM Rick Brownrigg via ncl-talk <
ncl-talk at ucar.edu> wrote:

> Hi,
>
> I am not expert at this, but based upon the docs for the
> function_latlon_cfd, it looks like it requires a rectilinear grid, which
> would imply you'll need to regrid your curvilinear WRF grid. NCL has
> numerous functions for doing this (I can't advise which one you should use):
>
>    http://ncl.ucar.edu/Applications/regrid.shtml
>
> I also note in the second warning you provided, its complaining about 3
> dimensions in parameter 1 (i.e., the lat variable) -- Is there a time
> dimension on that variable as well as X/Y?
>
> Hope that helps...
> Rick
>
>
> On Fri, Oct 4, 2019 at 1:25 PM Prashanth Bhalachandran via ncl-talk <
> ncl-talk at ucar.edu> wrote:
>
>> Dear NCL'ers,
>> I have a question on dealing with the 2D lat and long variables from WRF
>> output. For example, in my code below, I am having trouble using the lat
>> long information to either use them as coordinate variables or to be used
>> as inputs to the horizontal gradient function. What is the best way to
>> create or extract 1D lat and long information so that I can use them here?
>>
>> Thank you,
>> Prashanth
>>
>> begin
>>
>>
>> a = addfile("wrfout_d04_2005-07-29_12:06:00","r")
>>
>>
>> ctt = wrf_user_getvar(a,"ctt",-1) ; Cloud top temperatures in degree
>> Celsius
>>
>> lat = wrf_user_getvar(a,"XLAT",-1)
>>
>> lon = wrf_user_getvar(a,"XLONG",-1)
>>
>>
>> ;Original Dimensions and sizes: [Time | 10] x [south_north | 480] x
>> [west_east | 480]
>>
>>
>> ; Take the horizontal gradient
>>
>>   cttLatLon  = grad_latlon_cfd (ctt, lat, lon, False, False)
>>
>>
>> latitudinal direction
>>
>>
>>
>>   delete(cttLatLon)
>>
>>
>>
>>     print("ctt_grad_lat contains all missing values, cannot continue.")
>>
>>     return
>>
>>   end if
>>
>>
>>
>>     print("ctt_grad_lon contains all missing values, cannot continue.")
>>
>>     return
>>
>>   end if
>> end
>>
>>
>> Output:
>>
>> I can't prescribe lat and lon as coordinate variables since they are 2D.
>>
>> \$ ncl -n -Q cloud_top.ncl
>>
>> fatal:Coordinate variables must have one dimension only
>>
>> fatal:No coordinate variable exists for dimension (lat) in variable (ctt)
>>
>> fatal:["Execute.c":8637]:Execute: Error occurred at or near line 12 in
>> file cloud_top.ncl
>>
>>
>> or use them as inputs to the gradient function:
>>
>> \$ ncl -n -Q cloud_top.ncl
>>
>> fatal:Number of dimensions in parameter (1) of (grad_latlon_cfd) is (3),
>> (1) dimensions were expected
>>
>> fatal:["Execute.c":8637]:Execute: Error occurred at or near line 12 in
>> file cloud_top.ncl
>>
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20191004/a15683f5/attachment.html>
```