quantil2.pro 1.85 KB
FUNCTION quantil2,input,npercent,noplot=noplot,above=above

;+
; NAME:
;       quantil2
; PURPOSE:
;	Given an input array, computes the value val so that npercent
;	of the input values are below val.
;	if above is set, npercent of the input values are above val
; CATEGORY:
;	General
; CALLING SEQUENCE:
;       val=quantil2(input,npercent)
; INPUTS:
;       input = array (any dim) for which mean profile is to be calculated
;       npercent = percentage value
; OPTIONAL INPUT PARAMETERS:
; OUTPUTS:
;       val = value so that npercent % of the values are below val
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
;
; RESTRICTIONS:
; PROCEDURE:
;	Uses sort instead of histogram (more efficient than quantil)
;	interpol
; MODIFICATION HISTORY:
;      18-Oct-1993, Written by Jean-Philippe Bernard, IPAC.
;-


IF N_PARAMS(0) LT 2 THEN BEGIN
  PRINT,'Calling sequence: val=quantil(input,npercent)'
  PRINT,'accepted Key-words: /noplot,/above'
  GOTO, sortie
ENDIF

si=size(npercent)

IF si(0) NE 0 and si(0) NE 1 THEN BEGIN
  print,'nperc should be a single value or 1D array'
  goto,sortie
ENDIF

single=0
IF si(0) EQ 0 THEN single=1

IF single EQ 1 THEN BEGIN
  IF npercent LT 0 OR npercent GT 100. THEN BEGIN
    print,'we should have 0<npercent<100 !'
    goto,sortie
  ENDIF
ENDIF ELSE BEGIN
  ind=where(npercent LT 0. OR npercent GT 100.,count)
  IF count NE 0 THEN BEGIN
    print,'we should have 0<npercent<100 !'
    goto,sortie
  ENDIF
ENDELSE

indef=-32768.

ind=where(input ne indef,count)
inp=input(ind)

ma=max(inp) & mi=min(inp)
IF ma NE mi THEN BEGIN
  si=size(inp) & Ninp=si(1)
  nv=(1.*lindgen(Ninp))/(1.*Ninp)*100.
  ord=sort(inp)
  inpord=inp(ord)
  value=interpol(inpord,nv,npercent)
  IF not keyword_set(noplot) THEN BEGIN
    plot,nv,inpord
  ENDIF
  IF single EQ 1 THEN value=value(0)
ENDIF ELSE BEGIN
  value=indef
ENDELSE

RETURN,value
sortie:
END