[ncl-talk] loop error

Dennis Shea shea at ucar.edu
Sat Mar 24 08:36:00 MDT 2018


re:  "you better not use loops to perform simple operations as they are
slower than assignments."

Generally, it is more efficient to use array syntax. Your example has
'small' arrays so the speed difference is likely not noticeable.  Still,
thinking in array terms is a good exercise,


I suggest that you learn how to use conform:
   https://www.ncl.ucar.edu/Document/Functions/Built-in/conform.shtml
   https://www.ncl.ucar.edu/Document/Functions/Built-in/conform_dims.shtml

=====================

; for clarity, explicitly separate into different variables

  a  = temp(:,:,0,0)        ; a(time,level);  dimension numbers 0, 1
  la = conform(a, h, 1)  ; la(:,:)
  theta = a*(10000/la)^0.286
  copy_VarCoords(a,theta)
  theta at long_name = "potential temperature"
  theta at units = "K"

or, less clear, but technically more efficient

  theta = temp(:,:,0,0)*(10000/conform(temp(:,:,0,0), h, 1))^0.286

My recommendation is to write clear code unless necessary

---
Of course, there is a function. Use of the function allows for cleaner code.

   http://www.ncl.ucar.edu/Document/Functions/Contributed/pot_temp.shtml

  theta = *pot_temp*(h ,temp(:,:,0,0) , 1, False) ; will return all meta
data also
  printVarSummary(theta)
  printMinMax(theta,0)


On Sat, Mar 24, 2018 at 3:35 AM, Guido Cioni <guidocioni at gmail.com> wrote:

> The error message is exactly telling you what the problem is.
>
> fatal:Loop end must be scalar
>
>
> You have
>
> do i = 0,time-1
>
>
> i is a scalar integer, time is an array. You need to use dimsizes(time)-1.
>
> Also you better not use loops to perform simple operations as they are
> slower tha assignments.
>
> Il giorno 24 mar 2018, alle ore 07:10, Arijeet Dutta <
> arijeet.uoh at gmail.com> ha scritto:
>
> Hi all, I am trying to calculate potential temperature as follows
>
> h = addfile("air.mon.mean.nc","r")
> lat = h->lat
> lon = h->lon
> time = h->time
> temp = h->air
> l = h->level*10
>
>
> a = temp(:,:,0,0) ;tim,lv,lat,lon,
> do i = 0,time-1
>         do j = 0,l-1
>                 theta(i,j) = a*(10000/l(j))^0.286
>         end do
> end do
>
> fatal:Loop end must be scalar, can't execute loop
>
> Any help?
>
> Regards
> Arijeet
> _______________________________________________
> 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/20180324/891f208d/attachment.html>


More information about the ncl-talk mailing list