<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 &amp; lon were type double, the caculated quantity was double. NCL will not automatically &#39;downscale&#39; 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 = &quot;Pa&quot; ;<br>        PS:long_name = &quot;Surface pressure&quot; ;<br>        PS:cell_methods = &quot;time: mean&quot; ;<br>    double lat(lat) ;<br>        lat:long_name = &quot;latitude&quot; ;<br>        lat:units = &quot;degrees_north&quot; ;<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">&lt;<a href="mailto:wangh1@uchicago.edu" target="_blank">wangh1@uchicago.edu</a>&gt;</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&#39;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 &quot;$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl&quot;<br>
load &quot;$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl&quot;<br>
load &quot;$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl&quot;<br>
load &quot;$NCARG_ROOT/lib/ncarg/nclscripts/csm/skewt_func.ncl&quot;<br>
load &quot;$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl&quot;<br>
load &quot;/home/wangh1/contributed.ncl&quot;<br>
;************************************************<br>
<br>
begin<br>
<br>
;************************************************<br>
; file handling<br>
;************************************************<br>
fn  =systemfunc(&quot;ls /*/con.cam.h0.*.nc&quot;); define filename, 20 years summer files<br>
in  = addfiles(fn,&quot;r&quot;)                         ; open netcdf file<br>
ListSetType(in, &quot;join&quot;)<br>
<br>
;************************************************<br>
; read needed variables from file<br>
;************************************************<br>
PS   = in[:]-&gt;PS                                ; get pressure<br>
lat= in[1]-&gt;lat<br>
lon=in[1]-&gt;lon<br>
gradLatLon= grad_latlon_cfd(PS,PS&amp;lat,PS&amp;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&#39;t be coerced to type of left hand side<br>
fatal:[&quot;Execute.c&quot;:8578]:Execute: Error occurred at or near line 58 in file /home/wangh1/contributed.ncl<br>
<br>
fatal:[&quot;Execute.c&quot;:8578]:Execute: Error occurred at or near line 32 in file bou2.ncl<br>
<br>
Contributed.ncl file I used is:<br>
<br>
undef(&quot;grad_latlon_cfd&quot;)<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(&quot;grad_latlon_cfd: illegal rank: rankz=&quot;+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 &#39;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 = &quot;cfd: meridional gradient&quot;<br>
  cgy@units     = &quot;?/m&quot;<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=&gt;X<br>
;;       These don&#39;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 = &quot;gradx: ZONAL GRADIENT&quot;<br>
;;CGX@units     = &quot;?/m&quot;<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 = &quot;cfd: zonal gradient&quot;<br>
  cgx@units     = &quot;?/m&quot;<br>
<br>
  gradLatLon = [/ cgy, cgx /]   ; return two variables as a type &#39;list&#39;<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>