[ncl-talk] Subscript error help
Adam Hoefs
ajhoefs at wisc.edu
Wed Nov 29 17:13:01 MST 2017
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20171130/23a361ae/attachment.html>
More information about the ncl-talk
mailing list