[ncl-talk] Array reordering in OISST

Dennis Shea shea at ucar.edu
Tue May 21 12:25:28 MDT 2019


Keep in  mind:

NCL arrays are *row major* and use *zero-based* indexing similar to that
used by the C, C++ and Python languages and the ordering of arrays within
netCDF files. By comparison, other common processing languages (eg.,
Fortran, MATLAB, R) are *column major* and use *one-based *indexing.
[Fortran can change to a user defined index range: x(-12:37) ]

===> From the Mini-Language Manual

Section 7.7: NCL/Fortran array mapping

In Fortran, the leftmost array dimension varies fastest while in NCL the
rightmost array dimension varies fastest. Sometimes this causes confusion.
Rarely is reordering an array required when invoking a Fortran subroutine
from an NCL script. Thus, even though the array dimension names appear in
reverse order the individual array elements directly map.  *The rule
“fastest varying dimension in one language map to fastest varying dimension
in another language” applies here*.


          NCL                 Fortran
x(time,lev,lat,lon) <=map=> x(lon,lat,lev,time)

Consider the following two arrays where N=2 and M=3:
 ncl:   x(N,M)   <==>       x(M,N)   :Fortran
         x(0,0)     <==>       x(1,1)
         x(0,1)     <==>       x(2,1)
         x(0,2)     <==>       x(3,1)
         x(1,0)     <==>       x(1,2)
         x(1,1)     <==>       x(2,2)
         x(1,2)     <==>       x(3,2)

On Tue, May 21, 2019 at 4:58 AM Atsuyoshi MANDA <am at bio.mie-u.ac.jp> wrote:

> Hi Dennis and Rick,
>
> Thank you for your suggestions. I have learned how to use short2flt
> and coordinate subscripting and understand I did not use
> gsn_csm_contour_map() properly. These help me a lot.
>
> The script I sent yesterday is a part of my script that preprocesses
> the data for my Fortran program (I have deleted some redundant part of
> the script). The program assumes (lon, lat, time) dimensions and
> south-to-north direction in lat dimension. I want to analyze several
> SST datasets, so I want to reorder the dimensions in the OISST dataset
> to be consistent with other datasets.
>
> Thank you,
> Atsuyoshi
>
> 2019年5月21日(火) 2:17 Dennis Shea <shea at ucar.edu>:
>
> >
> > I think I understand what you want to do. You use:
> >
> > [A]  This following uses standard subscripting (indexing):
> >
> > print("sst(n,90-30,130)="+sst(n,90-30,130)+" "+lon(130)+"E
> "+lat(90-30)+"N")
> > print("sstre(n,130,90+30-1)="+sstre(n,130,90+30-1)+" "+lon(130)+"E
> > "+sstre&lat(90+30-1)+"N")
> >
> >
> > [B] I think you intended to use coordinate subscripting which uses the
> following syntax: {...}
> > A 'best practices' programming style is to use variables rather than
> hard-wiring numbers. Generally, this makes ithe intended use more clear.
> >
> >    LATS = 30
> >    LATN = 90
> >    LON = 130
> >
> >     SST = sst(n,{LATS:LATN},{LON})
> >     printVarSummary(SST)
> >
> > or, say
> >    LATS = 30
> >    LATN = 90
> >    LONW = 110
> >    LONE  = 150
> >
> >     SST = sst(n,{LATS:LATN},{LONW:LONE})
> >     printVarSummary(SST)
> >
> > =================================
> > [1] Your file variable is:
> >      short sst(time, lat, lon)
> >
> >      The coordinates are:
> >
> >        Coordinates:
> >             time: [66443..80078]
> >             lat: [89.5..-89.5]               ; <=== North-to-South
> ordering
> >             lon: [0.5..359.5]
> >
> > [2] NCL has the short2flt function that unpacks an maintains meta data.
> >
> >     sst = short2flt( f->sst )   ; import and unpack the variable
> maintaining meta data
> >     printVarSummary(sst)
> >     printMinMax(sst,0)
> >
> > [3]  It is not necessary to explicitly read the following coordinate
> variables from the file. NCL imports the variable as a data structure
> ['object'] that contains the array values and meta data including
> coordinate information associated with the variable.
> >
> > time = sst&time
> > lat    = sst&lat
> > lon   = sst&lon
> >
> > [4] There are different methods for accessing subsets of a variable:
> classic indexing and coordinate variable indexing. See above.
> >
> > [5] Generally, NCL graphics are aware of coordinate order. Hence, no
> reordering is necessary.
> >
> > ---
> > [6]
> >
> >
> >
> >
> > On Sun, May 19, 2019 at 9:46 PM Atsuyoshi MANDA <am at bio.mie-u.ac.jp>
> wrote:
> >>
> >> Hi,
> >>
> >> I am trying to reorder the array dimensions in the monthly mean OISST
> >> version 2 dataset, which was downloaded from
> >> ftp://ftp.cdc.noaa.gov/Datasets/noaa.oisst.v2/sst.mnmean.nc.
> >>
> >> However, reordering using the named subscripting in my script does not
> work.
> >>
> >> Please see the attached files:
> >> OIV2LR.sst.1982JUL.png (original data)
> >> OIV2LR.sstre.1982JUL.png (reordered data)
> >>
> >> Any suggestions would be greatly appreciated.
> >>
> >> Here is my script:
> >> ;=========================
> >> indir="./"
> >> infle="sst.mnmean.nc"
> >> in=indir+"/"+infle
> >>
> >> script_name  = get_script_name()
> >> outdir="BIN_"+systemfunc("basename "+script_name+" .ncl")
> >>
> >> dset="OIV2LR"
> >> MM=7
> >>
> >> month_abbr =
> (/"","JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP", \
> >>                     "OCT","NOV","DEC"/)
> >> MMM=month_abbr(MM)
> >>
> >> system("mkdir -vp "+outdir)
> >>
> >>
> >> f=addfile(in, "r")
> >>
> >> ;printVarSummary(f)
> >> ;print(f)
> >>
> >> lon=f->lon
> >> lat=f->lat
> >> sst=f->sst
> >> time=f->time
> >>
> >> utc_date = cd_calendar(time, 0)
> >>
> >> year   = tointeger(utc_date(:,0))    ; Convert to integer for
> >> month  = tointeger(utc_date(:,1))    ; use sprinti
> >> day    = tointeger(utc_date(:,2))
> >> hour   = tointeger(utc_date(:,3))
> >> minute = tointeger(utc_date(:,4))
> >> second = utc_date(:,5)
> >> date_str = sprinti("%0.2iZ ", hour) + sprinti("%0.2i ", day) + \
> >>            month_abbr(month) + " "  + sprinti("%0.4i", year)
> >>
> >> yyyys=1982
> >> yyyye=2017
> >>
> >> sst_scaled=sst*sst at scale_factor + sst at add_offset
> >> copy_VarAtts(sst,sst_scaled)
> >> copy_VarCoords(sst,sst_scaled)
> >> printVarSummary(sst_scaled)
> >>
> >> sstre=sst_scaled(time|:,lon|:,lat|:)
> >> copy_VarAtts(sst_scaled,sst)
> >> copy_VarCoords(sst_scaled,sstre)
> >> printVarSummary(sstre)
> >>
> >>
> >>
> >> dim=dimsizes(year)
> >> nt=dim(0)
> >>
> >> do n=7,7 ;0,nt-1
> >>
> >> if(year(n) .ge. yyyys .and. year(n) .le. yyyye .and. month(n) .eq.
> MM)then
> >> print(n+" "+date_str(n))
> >>
> >>
> >> figdir="FIG_"+systemfunc("basename "+script_name+" .ncl")+"_"+dset
> >> system("mkdir -vp "+figdir)
> >> type="png"
> >>
> >>
> >>
> >> figfle=dset+"."+"sstre."+year(n)+MMM+""
> >> fig=figdir+"/"+figfle
> >>
> >> print("figfle="+figfle+"."+type)
> >>
> >> wks = gsn_open_wks(type, fig)
> >>
> >> res=True
> >>   res at cnFillOn              = True               ; turn on color fill
> >>   res at cnLinesOn             = False              ; turn off contour
> lines
> >> plot1=gsn_csm_contour_map(wks,sstre(n,:,:),res)
> >>
> >>
> >>
> >> figfle=dset+"."+"sst."+year(n)+MMM+""
> >> fig=figdir+"/"+figfle
> >>
> >> print("figfle="+figfle+"."+type)
> >>
> >> wks = gsn_open_wks(type, fig)
> >>
> >> res=True
> >>   res at cnFillOn              = True               ; turn on color fill
> >>   res at cnLinesOn             = False              ; turn off contour
> lines
> >> plot1=gsn_csm_contour_map(wks,sst(n,:,:),res)
> >>
> >>
> >> print("sst(n,90-30,130)="+sst(n,90-30,130)+" "+lon(130)+"E
> "+lat(90-30)+"N")
> >> print("sstre(n,130,90+30-1)="+sstre(n,130,90+30-1)+" "+lon(130)+"E
> >> "+sstre&lat(90+30-1)+"N")
> >>
> >> end if
> >>
> >> end do
> >> ;=========================
> >>
> >> Thank you,
> >> Atsuyoshi
> >>
> >> -
> >> Atsuyoshi Manda, Ph.D
> >> Associate Professor,
> >> Mie University
> >> 1577 Kurimamachiya-cho Tsu city, Mie 514-8507 JAPAN
> >> _______________________________________________
> >> 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/20190521/acaeccf1/attachment.html>


More information about the ncl-talk mailing list