NCL Home > Documentation > Functions > General applied math

dtrend_n

Estimates and removes the least squares linear trend of the given dimension from all grid points.

Available in version 5.2.0 or later.

Prototype

	function dtrend_n (
		y               : numeric,  
		return_info [1] : logical,  
		dim         [1] : integer   
	)

	return_val [dimsizes(y)] :  numeric

Arguments

y

A multi-dimensional array or scalar value equal to the data to be detrended. The dimension from which the trend is calculated is indicated by the dim argument.

return_info

A logical scalar controlling whether attributes corresponding to the y-intercept and slope are attached to return_val. True = attributes returned. False = no attributes returned.

dim

A scalar integer indicating which dimension of y to do the calculation on. Dimension numbering starts at 0.

Return value

An array of the same size as y. Double if y is double, float otherwise.

Two attributes (slope and y_intercept) may be attached to return_val if return_info = True. These attributes will be one-dimensional arrays if y is one-dimensional. If y is multi-dimensional, the attributes will be the same size as y minus the dim-th dimension but in the form of a one-dimensional array. e.g. if y is 45 x 34, then the attributes will be a one-dimensional array of size 45*34. This occurs because attributes can not be multi-dimensional. Double if return_val is double, float otherwise.

You access the attributes through the @ operator:

print(return_val@slope)
print(return_val@y_intercept)

Description

Estimates and removes the least squares linear trend of the given dimension from all grid points. Missing values are not allowed, use dtrend_msg_n if missing values exist. The mean is also removed. Optionally returns the slope (eg, linear trend per unit time interval) and y-intercept for graphical purposes.

Assumes y is equally spaced. If this is not the case, then use dtrend_msg_n even if the data do not contain missing values.

See Also

dtrend_msg, dtrend_msg_n, dtrend, dtrend_quadratic

Examples

Example 1

y is three-dimensional with dimensions lat, lon, and time. The returned array will have the same size. Remember that the mean is also removed.

    yDtrend = dtrend_n(y,False,2)
Example 2

Same as example 1 but with the optional attributes. Let y be temperatures in units of K and the time dimension have units of months.

  yDtrend = dtrend_n(y,True,2)
; yDtrend@slope = a one-dimensional array of nlat * nlon elements.
; the units are K/month

Since attributes can not be returned as two-dimensional arrays, the user should use onedtond to create a two-dimensional array for plotting purposes:

 
   slope2D = onedtond(yDtrend@slope,(/nlat,nlon/))
   delete (YDtrend@slope)
   slope2D = slope2D*120        ; would give [K/decade]

   yInt2D  = onedtond(yDtrend@y_intercept,(/nlat,nlon/))
Example 3

Let y be three-dimensional array with dimensions time, lat, lon. Do the calculation across the time dimension.

  yDtrend = dtrend_n(y,False,0)
; yDtrend will be three-dimensional with dimensions time, lat, lon