pro detren,data,ddata,dmean,trend,badval ; Program removes a linear trend from a data array ; Input variables: ; DATA - the array to be detrended - IT IS ASSUMED THAT THE ELEMENTS OF ; THIS ARRAY PROGRESS WITH A CONSTANT TIME STEPS!! If you would like to ; detrend with a variable which has breaks in time, but no bad values, use ; detrend.pro. You must then have a time array. ; BADVAL - if any elements in DATA are to be ignored, they must ; be given the value BADVAL. ; Output variables ; DDATA - the linearly detrended array where datafit=a+b*i, i=1,n and ; DDATA=DATA-datafit ; DMEAN - the mean of the array DATA ; TREND - the linear trend in units of DATA/array element ; Load all values first - remove trend from good data points next. ddata=data ; Ignore bad data values. dtim=findgen(n_elements(data)) g=where(data ne badval,num) if (num gt 2) then begin fdata=data(g) fdtim=dtim(g) ; Perform a linear regression on DATA, TIME num=n_elements(fdata) tsum=total(fdtim) dsum=total(fdata) t2sum=total(fdtim^2.) dtsum=total(fdata*fdtim) del=num*t2sum-tsum^2. a=(t2sum*dsum-tsum*dtsum)/del b=(num*dtsum-tsum*dsum)/del ; Calculate the output variables dmean=dsum/num trend=b ddata(g)=data(g)-(a+b*fdtim) endif else begin dmean=badval trend=badval ddata(*)=badval endelse return stop end