;---This eliminate/remove duplicate library created before 6.3.0 ; 6.3.0 added: get_unique_values, count_unique_values, count_unique_values_n ;---Added functions with the suffix _unique ;=============================================================== undef("eliminate_dup_pairs_all") function eliminate_dup_pairs_all( x[*], y[*] ) ; ; Simple (possibly slow) function to eliminate duplicates ; Based on f77 fortran subroutine ; local nx, ny, nxy, xout, yout, no, i, j, iflg begin nx = dimsizes(x) ny = dimsizes(y) if (nx.ne.ny) then print("FATAL: eliminate_dup_pairs_all: x,y must be same size: nx="+nx+" ny="+ny) exit end if nxy = nx ; convenience xout = x yout = y no = 0 xout(0) = x(0) yout(0) = y(0) do i=1,nx-1 iflg = 0 do j=0,no if (xout(j).eq.x(i) .and. yout(j).eq.y(i)) then iflg = 1 break end if end do if (iflg.eq.0) then no = no+1 xout(no) = x(i) yout(no) = y(i) end if end do xout@NCL_tag = "NCL: eliminate_dup_pairs_all" yout@NCL_tag = "NCL: eliminate_dup_pairs_all" return([/xout(0:no), yout(0:no)/]) ; return unique values end ;------------ undef(" eliminate_dup_pairs_consecutive") function eliminate_dup_pairs_consecutive( x[*], y[*] ) ; ; Simple (possibly slow) function to eliminate duplicates ; local nx, ny, nxy, xout, yout, nn, flg begin nx = dimsizes(x) ny = dimsizes(y) if (nx.ne.ny) then print("FATAL: eliminate_dup_pairs_consecutine: x,y must be same size: nx="\ +nx+" ny="+ny) exit end if nxy = nx xout = x yout = y xout@NCL_tag = "NCL: eliminate_dup_pairs_consecutive" yout@NCL_tag = "NCL: eliminate_dup_pairs_consecutive" nn = ind(x(0:nxy-2).ne.x(1:) .and. y(0:nxy-2).ne.y(1:)) if (isconstant(x).eq.1 .and. isconstant(y).eq.1) then return( [/ x(0), y(0) /] ) ; return single value end if nnknt = dimsizes(nn) xout(0:nnknt-1) = x(nn) yout(0:nnknt-1) = y(nn) xout(nnknt) = x(nxy-1) yout(nnknt) = y(nxy-1) return([/xout(0:nnknt), yout(0:nnknt)/]) ; return unique values end ;------------ undef("eliminate_dup_consecutive") function eliminate_dup_consecutive(x[*]) ; ; Eliminate consecutive duplicates ; local nx, nn, nnknt, xout begin nx = dimsizes(x) xout = x xout@NCL_tag = "NCL: eliminate_dup_consecutive" nn = ind(x(1:).ne.x(0:nx-2)) if (isconstant(x).eq.1) then ; ?what to return? return( x(0) ) ; return single value ;;if (ismissing(nn(0))) then ; return _FillValue ;; if (isatt(x,"_FillValue")) then ;; return( x@_FillValue ) ;; else ;; return(default_fillvalue(typeof(x))) ;; end if ;;end if end if nnknt= dimsizes(nn) xout(0:nnknt-1) = x(nn) xout(nnknt) = x(nx-1) return(xout(0:nnknt)) end ;------------ undef("eliminate_dup_all") function eliminate_dup_all( x[*] ) ; ; Eliminate all duplicates ; This function obsolete with NCL 6.3.0: get_unique_values ; Original code is commneted for historical reasons ; local nx, xout, no, i, j, iflg begin ; ; Simple (possibly slow) loop to eliminate duplicates ; nx = dimsizes(x) xout = new(nx,typeof(x)) no = 0 xout(0) = x(0) do i=1,nx-1 iflg = 0 do j=0,no if (xout(j).eq.x(i)) then iflg = 1 break end if end do if (iflg.eq.0) then no = no+1 xout(no) = x(i) end if end do xout@NCL_tag = "NCL: eliminate_dup_all" return(xout(0:no)) end ;------------ undef("eliminate_dup_all_unique") function eliminate_dup_all_unique( x[*] ) ; ; Eliminate all duplicates ; NOTE: Return values are unique but are not necessarily 'left-to-right' ; local xout begin xout = get_unique_values(x) xout@NCL_tag = "NCL: eliminate_dup_all_unique" return( xout ) end