# [ncl-talk] Binning multilevel MODIS data using bin_sum

Dennis Shea shea at ucar.edu
Tue May 3 13:41:05 MDT 2016

```[1] You should use print statements to discover the error. Then ...
actually look at what is being printed out. Then see if your code is
consistent with what is being printed; then carefully look at the function
documentation. Look carefully at the content{ sizes, dimension ordering
....

[2] Look at
x          = short2flt_hdf( f->\$vNam\$ )       <=== 3D  (20,406,270)
printVarSummary(x)

[snip]

printVarSummary(GBIN(lev,:,:))           <=== 2D (406,270))

work = ndtooned(lon2d)                         <=== 1D  (406*270)
printVarSummary(work)

WORK = ndtooned(x)                             <=== 1D (20*406*270)
printVarSummary(WORK)

bin_sum(GBIN(lev,:,:),GKNT(lev,:,:),lon,lat \
,ndtooned(lon2d),ndtooned(lat2d),ndtooned(x) )

It should be

bin_sum(GBIN(lev,:,:),GKNT(lev,:,:),lon,lat \
,ndtooned(lon2d),ndtooned(lat2d),ndtooned(x(lev,:,:) )

The function has gbin and gknt as 2-D    ===> gbin [*][*]

procedure bin_sum (
gbin [*][*] : numeric ,
gknt [*][*] : integer ,
glon    [*] : numeric ,
glat    [*] : numeric ,
zlon    [*] : numeric ,
zlat    [*] : numeric ,
z       [*] : numeric
)

zlon, zlat and z MUST be the same length.

Also,  ..... the original code could be substantially improved. NOTE that
the code has
comments inserted next to each line. This helps debug!!!

do nf=0,nfil-1
print(nf+"   "+fili(nf))
attribute
x       = short2flt_hdf( f->\$vNam\$ )           ; (level=20,
latitude=270, longitude=406)
latx    = ndtooned(f->Latitude_mod07)     ; latx(406*270)  <==
(latitude=270, longitude=406)
lonx    = ndtooned(f->Longitude_mod07)         ; lonx
"               "            "

; lonx(109620)
if (nf.eq.0) then
printVarSummary(x)
printMinMax(x, 1)
print("nlatx="+dimsizes(latx)+"  mlonx="+dimsizes(lonx))
end if

do lev=0,nlev-1
bin_sum(GBIN(lev,:,:),GKNT(lev,:,:),lon,lat,
lonx,latx,ndtooned(x(lev,:,:)) )
end do
end do

I attached a code .. I suggest you look at it ... *carefully***

On Tue, May 3, 2016 at 7:15 AM, Alan Brammer <abrammer at albany.edu> wrote:

> I’ve had weird issues with bin_sum() and looping over arrays in the past.
> My solution has usually been to pass a temporary variable to bin_sum()
> then set your GBIN array to that temp variable.
>
> e.g.  unchanged lines omitted for brevity.
>
>  do nf=0,nfil-1
>    do lev=0,nlev-1
>
> ….
> tmp_var = GBIN(lev,:,:) ;; This could be defined outside of loop and just
> reset to zero for each loop.
> tmp_var = 0
> tmp_ct = toint(tmp_var) ;; as above doesn’t need to be redefined each
> loop just set to 0 each time.
>
> bin_sum(tmp_var, tmp_ct, ,lon,lat ,
> ndtooned(lon2d),ndtooned(lat2d),ndtooned(x) )
>
> GBIN(lev,:,:) = tmp_var
> GKNT(lev,:,:) = tmp_ct
> …
> end
>
>
>
>
>
> Alan
>
>
>
> ##############################
> Alan Brammer,
> Post-Doc Researcher
>
> Department of Atmospheric and Environmental Sciences,
> University at Albany, State University of New York, Albany, NY, 12222
> abrammer at albany.edu
> ##############################
>
> On 3 May 2016, at 04:43, AJILESH PP <ajileshparolla at gmail.com> wrote:
>
> Hi all, I modified the loop, but still I'm getting same data values on all
> 20 levels. Can anybody please tell me where I went wrong?
>
> On Sat, 23 Apr 2016 at 02:41 AJILESH PP <ajileshparolla at gmail.com> wrote:
>
>> Hi,
>> Thank You for the prompt reply. Sorry for the mistake, Now loop is
>> working output has 20 levels, but all the levels have the same data values
>> (all levels are same).
>>
>> On Thu, 21 Apr 2016 at 20:56 Rick Brownrigg <brownrig at ucar.edu> wrote:
>>
>>> Hi,
>>>
>>> I'm not totally sure, but I wonder if "level" in this line should be
>>> "lev"?
>>>
>>>
>>> bin_sum(GBIN(level,:,:),GKNT(level,:,:),lon,lat \
>>>
>>> In any case, a few strategic print() statements on your indexing
>>> variables will show where/when thing go arwy.
>>>
>>> HTH...
>>> Rick
>>>
>>>
>>> On Thu, Apr 21, 2016 at 7:10 AM, AJILESH PP <ajileshparolla at gmail.com>
>>> wrote:
>>>
>>>> Dear all
>>>> I want to bin MODIS level 2 products for a period of time. I modified
>>>> an example script given in ncl HDF examples and it is giving me only output
>>>> of a single level, where the desired output is for 20 levels. I tried to
>>>> add a level loop and level dimension also as shown in the script below.
>>>> --------------------------------------
>>>>   GBIN  = new ( (/nlev,nlat,mlon/), float )
>>>>   GKNT  = new ( (/nlev,nlat,mlon/), integer )
>>>>
>>>> snip
>>>>
>>>>  do nf=0,nfil-1
>>>>    do lev=0,nlev-1
>>>>      print(nf+"   "+fili(nf))
>>>>      x          = short2flt_hdf( f->\$vNam\$ )
>>>>      lat2d1     = f->Latitude
>>>>      lon2d1     = f->Longitude
>>>>      lat2d      = conform(x,lat2d1,(/1,2/))
>>>>      lon2d      = conform(x,lon2d1,(/1,2/))
>>>>      level        =
>>>> (/1000,950,920,850,700,620,500,400,300,250,200,100,150,100,70,50,30,20,10,5/)
>>>>      x!0          = "level"
>>>>      x&level      = level
>>>>      nx           = product(dimsizes(x))
>>>>     bin_sum(GBIN(level,:,:),GKNT(level,:,:),lon,lat \
>>>>            ,ndtooned(lon2d),ndtooned(lat2d),ndtooned(x) )
>>>> printVarSummary(x)
>>>> ---------------------------------------
>>>> but it is giving this error:
>>>> fatal:Subscript out of range, error in subscript #0
>>>> fatal:An error occurred reading GBIN
>>>>
>>>> Can anybody help me to resolve this issue?
>>>> Please find my script and data as follows
>>>> Data:   MOD07_L2.A2012166.0*.hdf
>>>> Script:  bin.ncl
>>>>
>>>> Any help would be appreciated
>>>> Thank You!
>>>>
>>>> _______________________________________________
>>>> 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
>
>
>
> _______________________________________________
> 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/20160503/5e086ceb/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MOD01_L2.ncl_talk.ncl
Type: application/octet-stream
Size: 3024 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20160503/5e086ceb/attachment.obj
```