[ncl-talk] Subscript error help

Kevin Hallock hallock at ucar.edu
Thu Nov 30 12:28:25 MST 2017


Hi Adam,

Before addressing the error, I wanted to verify that your version of NCL is actually 5.2. NCL 6.4.0 was released earlier this year, while NCL 5.2 was released in early 2010 and may not match behavior described in our up-to-date documentation. You may want to consider upgrading: http://ncl.ucar.edu/Download/ <http://ncl.ucar.edu/Download/>

Regarding the error you observed, error messages generated by NCL can often contain useful information that is specific to your script. In this case, it looks like the script is trying to access a two dimensional variable as if it only had one dimension:
val = new((/1,355/),float)
[…]
plot = gsn_csm_xy(wks,x(r),val(r),res)

There is a list of common error messages <http://www.ncl.ucar.edu/Document/Language/error_messages.shtml> with generic examples on the NCL website; the second error message in that list seems relevant: Number of subscripts on right-hand-side do not match number of dimensions of variable: (4), Subscripts used: (3) <http://www.ncl.ucar.edu/Document/Language/error_messages.shtml#NumSubRHS>

I suspect that there may have been a second “fatal: …” error message indicating which line this error occurred on. In general, including as much error/debug output as possible in ncl-talk emails helps us to identify issues and respond more quickly.

Also, you can also try to identify what is causing an error by adding appropriate “print()” or “printVarSummary()” calls to your script immediately before where the error is occurring. For example, adding “printVarSummary(val)” in this case would show that val is a 1x355 array — technically two dimensional, although one dimension is of length 1 — while “printVarSummary(x)” would show a one-dimensional array of length 355.

Finally, I’m not 100% certain what the goal of your script is, but it seems as though the x and y values for your plot are single scalar values “x(r)” and “val(r)”. Without knowing exactly what the script is trying to accomplish, I can’t be much more specific than that, but based on those x and y values I think that your script might not generate the plot that you’re expecting.

I hope this helps,
Kevin

> On Nov 29, 2017, at 5:13 PM, Adam Hoefs <ajhoefs at wisc.edu> wrote:
> 
> Hello,
> I am trying to make a basic xy plot in NCL 5.2, and am getting the following error:
> fatal:Number of subscripts do not match number of dimensions of variable,(1) Subscripts used, (2) Subscripts expected
> 
> I have looked at past questions that have the same error but cannot figure out how to apply their solutions to my script.
> Any help would be much appreciated!
> -Adam
> 
> ;*************************************************
> ; plot_csv_example.ncl
> ;
> ; this script will plot data from csv/txt output
> ; for the columns you specify.
> ; please read through the code before you use it. It is currently set up to read in the csv file
> ; but not make the plot--you'll have to add code to make a plot.
> ;
> ;*************************************************
> ; first need to tell NCL where the function libraries are. These next 3 lines have to be at the top 
> ; of EVERY NCL script you write:
> 
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> 
> ; after specifiying the paths you have to have a "begin script" statement:
> begin
> 
> ;************************************************************
> ; start user input
> ;************************************************************
> ; first designate the directory the directory where your csv file resides
> filedir = "/users/hoefs/"
> 
> delim = ","
> 
> ; specify filename
> filename = "Houston2016.csv"
> 
> ; specify column (number) of data to use on the x-axis of the plot
> ; note: though NCL usually starts counting at zero, when reading ascii/text/csv files
> ; the first column is numbered 1.
> xcol = 1
> 
> ; specify column (number) of data to use on the y-axis of the plot
> ycol = 2
> 
> ; specify the name of the output filename of the plot
> ; the script is currently set up to create a PDF plot. ".pdf" will be appended to the filename automatically
> outfilename = "HoustonOzone2016"
> ; note that the output file is currently set up to be written to the same
> ; directory as the csv file
> 
> ;************************************************************
> ; end user input
> ;************************************************************
> 
> file1 = ("/users/hoefs/Houston2016/")
> ; read in contents of file with "asciiread" function. Contents go into an array defined here as "values1d1"
> values1d1 = asciiread("Houston2016.csv",-1,"string")
> ; use the str_split and dimsizes functions to get the number of columns
> ncols = dimsizes(str_split(values1d1(1),","))
> ; use the dimsizes function to get the number of rows (includes header row(s) if there are any)
> nrows = dimsizes(values1d1) ; this shouldn't change
> 
> ; declare arrays to put data into. Adjust array size if there's a header row.
> ; for example, if there's a header row, xdata = new(nrows-1,float)
> xdata = new(355,float)
> ydata = new(355,float)
> 
> ; note: if there is a header row (or rows) you'll need to change this loop
> ; for example, for a single header row change the length of the do loop to : do r = 1,nrows-2
> ; this loop reads in each row of values1d1 with the function "str_get_field" and then converts 
> ; the string into a floating number with the "tofloat" function and stores the values in the xdata and ydata arrays.
> do r = 0,nrows-1
>    xdata(r) = tofloat(str_get_field(values1d1(r),xcol,","))
>    ydata(r) = tofloat(str_get_field(values1d1(r),ycol,","))
> end do
> 
> ; here's an example of how to check that the data were read in correctly:
> printVarSummary(ydata)
> printMinMax(ydata,True)
> print("the average is "+avg(ydata))
> 
> 
> ;**********************************************************
> ; now make the plot!
> ; a few basic things are included below but the bulk of the plotting commands you'll have to add.
> ; check the NCL example pages for how to make the kind of plot you want (xy scatter for example)
> 
> res = True
> res at gsnFrame = False
> res at xyLineThicknessF = 3
> res at trYMinF = 0.0
> res at trYMaxF = 0.1
> res at trXMinF = 2016000
> res at trXMaxF = 2016355
> res at xyDashPattern = 0
> res at xyLineColor = (/"NavyBlue"/)
> res at tiXAxisString = "Date"
> res at tiYAxisString = "Ozone Concentration (ppb)"
> 
> res at tiMainString = "8-Hr Max Daily Avg. Ozone Concentration in Houston 2016" ; title
> 
> outname = ("HoustonOzone") ; output filename
> 
> wks   = gsn_open_wks("pdf","HoustonOzone")
> 
> res at gsnMaximize          = True
> res at gsnPaperOrientation  = "Landscape"
> val = new((/1,355/),float)
> x = 2016000 + ispan(0,355,1)
> 
> plot = gsn_csm_xy(wks,x(r),val(r),res)
> draw(plot)
> frame(wks)
> 
> 
> end  ; obligatory "end of script" statement
> 
> 
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20171130/74c6df4b/attachment.html>


More information about the ncl-talk mailing list