NCL Website header
NCL Home > Documentation > Language

Common error messages in NCL


Subscript out of range, error in subscript #1

Sample code that causes the error:

    x = random_uniform(-100,100,(/10,20,30/))
    print(x(5,20,5))                           ; index '20' is invalid

Cause: Subscripting an array using an index that is out-of-bounds. Subscript numbers start at 0 and go from left to right, so subscript "#1" refers to the second dimension from the left.

Fix: Check your subscript indexes to make sure they are in the range of your array size. Use print and printVarSummary.


Number of subscripts on right-hand-side do not match number of dimensions of variable: (4), Subscripts used: (3)

Sample code that causes the error:

    x = random_uniform(-50,50,(/5,32,64/))   ; x is 3D (5 x 32 x 64)
    y = x(0,:,:,:)                           ; x is subscripted as a 4D array

Cause: Subscripting an array using the wrong dimensionality.

Fix: Check and fix your subscript syntax. Use print and printVarSummary.


Assignment type mismatch, right hand side can't be coerced to type of left hand side

Sample code that causes the error:

    x = 5
    x = "Now I'm a string"

Cause: Reassigning a variable using a different type or dimensionality.

Fix: Use the reassignment operator, or delete the variable first.

    x = 5
    x := "Now I'm a string"

or

    x = 5
    delete(x)
    x = "Now I'm a string"


syntax error: possibly an undefined procedure

Sample code that causes the error:

    i = 5
    prnt(i)

Cause: Referencing a function or procedure that doesn't exist.

Fix: Check the spelling of function/procedure and whether you need to load another NCL script that defines it.


syntax error: function fspan expects 3 arguments, got 2

Sample code that causes the error:

    x = fspan(0,10)     ; fspan requires 3 arguments

Cause: Calling a function or procedure with the wrong number of arguments.

Fix: Check and correct your function or procedure arguments. Read the documentation for that particular function for help.


syntax error: line -1

Sample code that causes the error:

    if (x.lt.0) then
       x = 5

Cause: You have an unclosed code block, like a "begin" without an "end", an "if" without an "end if", or a "do" without an "end do".

Fix: Check for unclosed code blocks and close them.


Dimension sizes of left hand side and right hand side of assignment do not match

Sample code that causes the error:

    x = (/2,5,9,3/)      ; x has 4 elements
    y = (/8,7,0,2,3/)    ; y has 5 elements
    x = y                ; error

Cause: Assigning a 1D array to another 1D array with a different number of elements.

Fix: Check and fix the array sizes on the left and/or right side of the "=", or use the reassignment operator.

    x = (/2,5,9,3/)      ; x has 4 elements
    y = (/8,7,0,2,3/)    ; y has 5 elements
    x := y               ; x now has 5 elements


Number of dimensions on right hand side do not match number of dimension in left hand side

Sample code that causes the error:

    x = random_uniform(-10,10,(/3,2,2/))   ; x is 3 x 2 x 2
    y = random_uniform(-20,20,(/4,5/))     ; y is 4 x 5
    x = y                                  ; error

Cause: Assigning one array to another when they don't have the same number of dimensions.

Fix: Use dimsizes and printVarSummary to check your array sizes, and then correct as necessary. You can also use the reassignment operator.

    x = random_uniform(-10,10,(/3,2,2/))   ; x is 3 x 2 x 2
    y = random_uniform(-20,20,(/4,5/))     ; y is 4 x 5
    x := y                                 ; x is now 4 x 5


Dimension size mismatch, dimension (0) of left hand side reference does not have the same size as the right hand side reference after subscripting.

Sample code that causes the error:

    x = (/2,5,9,3/)      ; x has 4 elements
    y = (/8,7,0,2,3/)    ; y has 5 elements
    x(0:2) = y(3:4)       ; trying to assign 2 values in y to 3 values in x

Cause: Subsetting an array and trying to assign it a variable or another array subset that has a different number of elements.

Fix: Check and correct your array subscripts.


The result of the conditional expression yields a missing value. NCL can not determine branch, see the ismissing function.

Sample code that causes the error:

    x = new(1,float)   ; x is assigned a missing value
    if(x.gt.5) then
      print("x > 5")
    end if

Cause: Using a missing value in an "if" statement or some other conditional statement.

Fix: If there's a chance your variable could be missing, then use ismissing to test for missing values.

    x = new(1,float)
    if(.not.ismissing(x).and.x.gt.5) then
      print("x > 5")
    end if


Variable (x1) is undefined

Sample code that causes the error:

    x = 5
    print(x1)

Cause: Referencing a variable that doesn't exist.

Fix: Check the spelling.


Attempt to reference attribute (FillValue) which is undefined

Sample code that causes the error:

    x            = (/2,5,9,3,0/)
    x@_FillValue = -999

    print(x@FillValue)    ; attribute spelled incorrectly

Cause: Referencing an attribute that doesn't exist.

Fix: Check and correct the spelling.


There are 1 floats larger than INT_MAX, which has been flagged missing

Sample code that causes the error:

    x = 2^31        ; 2.147484e+09
    i = toint(x)     ; triggers the "larger than INT_MAX" error

Cause: Using "toint" to convert a float or double value that is larger than (2^31-1) to an integer.

Fix: Try using tolong, or use where to do something with these large values before converting them:


  x = (/2^19,2^20,2^30,2^31/)
  i = toint(x)    ; triggers the warning

;---Solution 1: live with it; the last value will be set to the default int missing value
  print(i)      ; last value is -2147483647 (integer missing)

;---Solution 2: use "tolong"
  l = tolong(x)
  print(l)        ; last value is 2147483648, the correct value

;---Solution 3: convert large values to a smaller value, and perhaps set to missing
  x@_FillValue = -999
  x = where(x.ge.(2^31-1),x@_FillValue,x)
  j = toint(x)
  print(j)  ; This is effectively the same as solution #1, but
            ; no warnings are produced. Last value is -999 and
            ; is a missing value.


Argument 0 of the current function or procedure was coerced to the appropriate type and thus will not change if the function or procedure modifies its value

Sample code that causes the error:

    x   = (/8,1,5/)
    str = str_join(x,",")    ; str_join expects string input

Cause: Calling a function or procedure with the wrong argument type. This generally happens if the function is expecting a string and you give it a numerical value.

Fix: Convert the argument to a string using the tostring function or concatenating it with an empty string ("") using the (+) operator.

    x   = (/8,1,5/)
    str = str_join("" + x,",")

Note: one way you can find out what line an NCL script is failing on is to comment out the "begin" and "end" statements (if any) of the main code, and then run the script with the -x option:

   ncl -x 11.ncl
This will cause every line to be echoed to the screen as it is executed.


tofloat: A bad value was passed to (string) tofloat, input strings must contain numeric digits, replacing with missing value

    str = "ab54cd"
    x   = tofloat(str)

Cause: Trying to convert a string that contains non-numeric characters to a numerical value. This is common when reading in ASCII files that contain non-numeric fields.

Fix: Print out the strings you are trying to convert, to verify that they actually have numbers in them. If they don't, then you either have an error in your code, or you'll need to fix these strings to contain only numerical values before converting them.


Minus: Dimension size, for dimension number 0, of operands does not match, can't continue

    x    = (/2,9,3/)      ; x has 3 elements
    y    = (/8,7,0,3/)    ; y has 4 elements
    diff = x-y            ; error

Cause: Trying to subtract two arrays of different lengths.

Fix: Check your arrays to make sure they are the same size. Use printVarSummary to help examine the arrays.


fatal:Minus: Number of dimensions do not match, can't continue

  x    = (/(/1,2,3,4/),(/5,6,7,8/),(/9,10,11,12/)/)  ; 3 x 4
  y    = (/1,0,2,5/)                                 ; 4 elements
  diff = x - y

Cause: Trying to subtract two arrays of different dimensionality.

Fix: If one array is a subset in size of another, then you can use the conform function to "conform" the smaller array to the size of the larger one.

  x    = (/(/1,2,3,4/),(/5,6,7,8/),(/9,10,11,12/)/)    ; 3 x 4
  y    = (/1,0,2,5/)                                 ; 4 elements
  diff = x - conform(x,y,1)    ; y will be propagated to a 3 x 4 array


fatal:_NclBuildArray: each element of a literal array must have the same dimension sizes, at least one item doesn't

This message comes from trying to use a "nested" array of dimension sizes inside something like a new statement.

  xdims = (/10,20,30/)
  x     = random_uniform(-10,10,xdims)
  data  = new((/2,xdims/),float)          ; this will cause the error

The third line of the above code expands to:

  data = new((/2,(/10,20,30/)/),float)    ; this will cause the error
You cannot use "(/2,(/10,20,30/)/)" syntax in a new statement. It must be changed to "(/2,10,20,30/)":

  xdims = (/10,20,30/)
  xrank = dimsizes(xdims)
  x     = random_uniform(-10,10,xdims)

  data_dims     = new(xrank+1,integer)
  data_dims(0)  = 2
  data_dims(1:) = xdims 
  data = new(data_dims,float)

scalar field is constant; no contour lines will appear

Sample code that causes the error:

  x  = new((/50,50/),float)
  x  = 2.    ; Setting the whole array to a single value, 2.0

  wks = gsn_open_wks("x11","contour")

  res = True
  res@cnFillOn = True
  plot = gsn_csm_contour(wks,x,res)

Cause: You are trying to contour a data variable where every element is the same value. A plot will be created, but it will be a blank box with the text "CONSTANT FIELD - VALUE IS 2".

Fix: If you believe that your data variable should not be constant, then you need to double-check either the data that's being read in, or check all of your calculations. The printMinMax procedure is very useful in "debugging" your data to make sure it has the range of values that you expect.

Sometimes having a constant field is correct. We've seen this occasionally when users try to plot the first time step of simulated data. The first time step might be some an "initial condition" where the whole array is initialized to the same value.

If you simply want to skip the drawing of a plot that has a constant field, then use min and max:

  x  = new((/50,50/),float)
  x  = 2.    ; Setting the whole array to a single value, 2.0

  wks = gsn_open_wks("x11","contour")

  res = True
  res@cnFillOn = True

  if(min(x).eq.max(x)) then
   print("Error: you have a constant field. Will not create a contour plot.")
  else
    plot = gsn_csm_contour(wks,x,res)
  end if

If you want the plot to be drawn, but with a color, then use the cnConstFEnableFill resource as suggested:

  x  = new((/50,50/),float)
  x  = 2.    ; Setting the whole array to a single value, 2.0

  wks = gsn_open_wks("x11","contour")

  res = True
  res@cnConstFEnableFill = True   ; enable the whole field to be drawn in a single color
  res@cnFillOn = True
  plot = gsn_csm_contour(wks,x,res)

ContourPlotDraw: Workspace reallocation would exceed maximum size

Cause: This is caused when trying to contour a very large data array.

Fix: There are two possible fixes.

A quick-and-easy one to try is to switch to raster contouring:

  res@cnFillMode = "RasterFill"
  res@cnRasterSmoothingOn = True   ; optional, but may produce better results

A second fix is more involved, and requires that you "bump up" your workspace size. Add the following code right after your "gsn_open_wks" call:

  setvalues NhlGetWorkspaceObjectId() 
    "wsMaximumSize" : 300000000
  end setvalues 

You will likely need to adjust the "300000000" number. Start with something small and slowly increase it until the error message goes away.

You can permanently bump up the workspace size by setting this resource in your ~/.hluresfile:

  *wsMaximumSize : 300000000


warning:_NhlCreateSplineCoordApprox: Attempt to create spline approximation for X axis failed: consider adjusting trXTensionF value

This message can come from trying to plot data that has highly irregularly-spaced X and/or Y axis coordinate values. See the script below for an example.

To fix the issue, subscript your data over the area that is more regularly spaced.

;
; Generate some dummy data with dummy 1D lat/lon coordinate arrays,
; with highly irregular spacing for the latitudes.
;
  lat       = (/-88,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,88/)
  lon       = ispan(-179,179,60)
  nlat      = dimsizes(lat)
  nlon      = dimsizes(lon)
  lat@units = "degrees_north"
  lon@units = "degrees_east"
  x         = generate_2d_array(10, 10, -19.69, 15.82, 0, (/nlat,nlon/))
  x!0       = "lat"
  x!1       = "lon"
  x&lat     = lat
  x&lon     = lon

  wks = gsn_open_wks("x11","spline_error")

  res              = True
  res@cnFillOn     = True
  res@tiMainString = "Contours are in wrong latitude location"  

  plot = gsn_csm_contour_map(wks,x,res)              ; Warning from "_NhlCreateSplineCoordApprox"

  res@tiMainString = "Contours are in correct latitude location"   

  plot = gsn_csm_contour_map(wks,x({-10:10},:),res)  ; Using {-10:10} subscripting for the
                                                        ; latitudes removes the warning.

Argument list too long

Sample code that causes the error:

  diri  = "/some/directory/path/"
  files = systemfunc("ls -1 " + diri + "sgp15swfcldgrid*")

Cause: You are trying to list the contents of a directory that has hundreds of files, and reaching a UNIX limitation on the "ls" command.

Fix: Use one of these two methods:

  files = systemfunc("cd "+diri+" ; echo sgp15swfcldgrid* | xargs ls")

  files = systemfunc("find " + diri + " -name 'sgp15swfcldgrid*' -print | xargs basename")


Unable to load System Resource File

Sample code that causes the error:

This is an error message that can occur when you run any NCL script.

Cause: You either don't have your NCARG_ROOT environment variable set correctly or set at all. NCARG_ROOT needs to be set to the root location of where the NCL software is installed.

Fix: Check the setting of your NCARG_ROOT environment variable and other NCARG_XXXX variables.

On the UNIX command line, type:

    which ncl

If this returns (for example), "/usr/local/ncl/bin", then NCARG_ROOT should be set to /usr/local/ncl. See the section "Set the NCARG_ROOT environment variable" section on the NCL download page for more information.