<div dir="ltr"><div><div>The problem has been fixed.<br><br></div>The issue was that the return variable was created to match the type of the PS variable (float). However, because the lat & lon were type double, the caculated quantity was double. NCL will not automatically 'downscale' a double to float. The user must explicitly do this via a function.<br><br></div><div>The updated contributed.ncl_640 is at: <br><br><a href="http://www.cgd.ucar.edu/~shea/contributed.ncl_640">http://www.cgd.ucar.edu/~shea/contributed.ncl_640</a><br></div><div><br></div>[snip<br><div> float PS(time, lat, lon) ;<br> PS:units = "Pa" ;<br> PS:long_name = "Surface pressure" ;<br> PS:cell_methods = "time: mean" ;<br> double lat(lat) ;<br> lat:long_name = "latitude" ;<br> lat:units = "degrees_north" ;<br></div><div>[snip]<br><br></div><div>Running the attached test script resulted in<br><br>Variable: glon<br>Type: double<br>Total Size: 110592 bytes<br> 13824 values<br>Number of Dimensions: 3<br>Dimensions and sizes: [time | 1] x [lat | 96] x [lon | 144]<br>Coordinates: <br> time: [58216..58216]<br> lat: [ -90.. 90]<br> lon: [ 0..357.5]<br>Number Of Attributes: 2<br> long_name : cfd: meridional gradient<br> units : ?/m<br><br>Variable: glat<br>Type: float<br>Total Size: 55296 bytes<br> 13824 values<br>Number of Dimensions: 3<br>Dimensions and sizes: [time | 1] x [lat | 96] x [lon | 144]<br>Coordinates: <br> time: [58216..58216]<br> lat: [ -90.. 90]<br> lon: [ 0..357.5]<br>Number Of Attributes: 2<br> long_name : cfd: zonal gradient<br> units : ?/m<br><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 24, 2016 at 8:44 AM, Huan Wang <span dir="ltr"><<a href="mailto:wangh1@uchicago.edu" target="_blank">wangh1@uchicago.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
In regard to this question,<br>
<br>
I'm trying to use grad_latlon_cfd function by loading contributed.ncl for version 6.<br>
My codes are here,<br>
<br>
;NCL tutorial script: vert_1.ncl<br>
;************************************************<br>
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"<br>
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"<br>
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"<br>
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/skewt_func.ncl"<br>
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"<br>
load "/home/wangh1/contributed.ncl"<br>
;************************************************<br>
<br>
begin<br>
<br>
;************************************************<br>
; file handling<br>
;************************************************<br>
fn =systemfunc("ls /*/con.cam.h0.*.nc"); define filename, 20 years summer files<br>
in = addfiles(fn,"r") ; open netcdf file<br>
ListSetType(in, "join")<br>
<br>
;************************************************<br>
; read needed variables from file<br>
;************************************************<br>
PS = in[:]->PS ; get pressure<br>
lat= in[1]->lat<br>
lon=in[1]->lon<br>
gradLatLon= grad_latlon_cfd(PS,PS&lat,PS&lon,True,False)<br>
glon= gradLatLon[0]<br>
glat= gradLatLon[1]<br>
end<br>
<br>
<br>
And the result is:<br>
fatal:Assignment type mismatch, right hand side can't be coerced to type of left hand side<br>
fatal:["Execute.c":8578]:Execute: Error occurred at or near line 58 in file /home/wangh1/contributed.ncl<br>
<br>
fatal:["Execute.c":8578]:Execute: Error occurred at or near line 32 in file bou2.ncl<br>
<br>
Contributed.ncl file I used is:<br>
<br>
undef("grad_latlon_cfd")<br>
function grad_latlon_cfd(z:numeric, lat[*]:numeric, lon[*]:numeric, rCyclic[1]:logical, opt[1]:logical)<br>
local dimz, rankz, nlat, mlon, rad, re, con, dlon, dx, cgx, cgy, gradLatLon<br>
begin<br>
dimz = dimsizes(z)<br>
rankz = dimsizes(dimz)<br>
<br>
if (rankz.lt.2 .or. rankz.gt.4) then<br>
print("grad_latlon_cfd: illegal rank: rankz="+rankz)<br>
exit<br>
end if<br>
<br>
mlon = dimz(rankz-1)<br>
nlat = dimz(rankz-2)<br>
;************************************************<br>
; Miscellaneous<br>
;************************************************<br>
rad = 4.0*atan(1.0)/180.0<br>
re = 6.37122e6<br>
con = re*rad ; one deg lat = 111198.8 meters<br>
<br>
;************************************************<br>
; Use 'center_finite_diff_n for meridional (Y) gradient<br>
;************************************************<br>
cgy = center_finite_diff_n( z, lat, False ,0,rankz-2)<br>
cgy = cgy/con<br>
copy_VarCoords( z, cgy) ; add mets<br>
cgy@long_name = "cfd: meridional gradient"<br>
cgy@units = "?/m"<br>
<br>
;************************************************<br>
; Use cfd for zonal (X) gradients<br>
; Generally, these are much smaller than the meridional (Y) gradients<br>
; This assumes that the longitudes are equally spaced<br>
; Pre-allocate space for gradients<br>
;************************************************<br>
<br>
dlon = (lon(2)-lon(1))*0.0174533 ; convert to radians<br>
dx = re*cos(0.0174533*lat)*dlon ; different at each latitude; dx(nlat)<br>
cgx = new( dimz, typeof(z), getFillValue(z) ) ; lon=>X<br>
;; These don't work<br>
;;DX = conform_dims( dimz, dx, rankz-2)<br>
;;CGX = center_finite_diff_n (z, DX, rCyclic,0,0)<br>
;;copy_VarCoords( z, CGX)<br>
;;CGX@long_name = "gradx: ZONAL GRADIENT"<br>
;;CGX@units = "?/m"<br>
<br>
if (rankz.eq.2) then<br>
do nl=0,nlat-1 ; loop over each latitude<br>
cgx(nl,:) = center_finite_diff_n (z(nl,:), dx(nl), rCyclic,0,0)<br>
end do<br>
else if (rankz.eq.3) then<br>
do nl=0,nlat-1 ; loop over each latitude<br>
cgx(:,nl,:) = center_finite_diff_n (z(:,nl,:), dx(nl), rCyclic,0,0)<br>
end do<br>
else if (rankz.eq.4) then<br>
do nl=0,nlat-1 ; loop over each latitude<br>
cgx(:,:,nl,:) = center_finite_diff_n (z(:,:,nl,:), dx(nl), rCyclic,0,0)<br>
end do<br>
end if ; rankz=4<br>
end if ; rankz=3<br>
end if ; rankz=2<br>
<br>
copy_VarCoords( z, cgx)<br>
cgx@long_name = "cfd: zonal gradient"<br>
cgx@units = "?/m"<br>
<br>
gradLatLon = [/ cgy, cgx /] ; return two variables as a type 'list'<br>
return( gradLatLon )<br>
end<br>
<br>
<br>
<br>
Thank you!<br>
<br>
<br>
Huan<br>
_______________________________________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a><br>
</blockquote></div><br></div>