;---------------------------------------------------------------------------
; Document name: DECOMP5.PRO
; Created by:    Dietmar Germerott, MPAE Lindau, August 12, 1998
;
; Last Modified: Mon Aug 24 16:47:15 1998 (GERMEROTT@linsu1.mpae.gwdg.de)
;---------------------------------------------------------------------------
;

PRO decomp5,cim,header,dim,helpp=helpp,errmsg=errmsg

;+
; PROJECT:
;       SOHO - SUMER
;
; NAME:
;       DECOMP5
;
; PURPOSE: 
;       Decompress SUMER Images, compressed with method 5
;
; CATEGORY:
;       Analysis
; 
; EXPLANATION:
;       
; SYNTAX: 
;       decomp5
;
; EXAMPLES:
;       
; INPUTS:
;       cim - compressed image
;       header - SUMER binary header
;
; OPTIONAL INPUTS: 
;       None.
;
; OUTPUTS:
;       dim - decompressed Image
;
; OPTIONAL OUTPUTS:
;       None.
;
; KEYWORDS: 
;       help - print help message
;
; COMMON:
;       None.
;
; RESTRICTIONS: 
;       None.
;
; SIDE EFFECTS:
;       None.
;
; HISTORY:
;       Version 1, August 14, 1998, Dietmar Germerott, MPAE Lindau. Written
;          based on decompression METHOD5 by several People
;          in this routine alog10 is used as in the dpu source code
;   
; CONTACT:
;       Udo Schuehle, MPAE Lindau (Schuehle@linmpi.mpg.de)
;-
;
   ON_ERROR, 2
   
   errstr = ''
   
;---------------------------------------------------------------------------
;  check some conditions
;---------------------------------------------------------------------------

   IF KEYWORD_SET(helpp) THEN GOTO,help_label 
   
   IF N_PARAMS() LT 3 THEN BEGIN
      
      errstr = ' Parameter Error - need 3 Parameter'
      GOTO,help_label
   ENDIF

   cmpsheme = comprm(header)
   IF cmpsheme(0) NE 5 THEN BEGIN
      errstr = ' Not the right Method for decompressing'
      GOTO,handle_error
   ENDIF
   
;---------------------------------------------------------------------------
;  get compression parameters and size of image
;---------------------------------------------------------------------------

   p2 = cmp2(header) & p2=p2(0)
   p3 = cmp3(header) & p3=p3(0)
   
   sizeofimage = SIZE(cim)
   spec = sizeofimage(1)
   spat = sizeofimage(2)
   
;---------------------------------------------------------------------------
;  create new uncompressed image
;---------------------------------------------------------------------------

   dim = LONARR(spec, spat)
   
;---------------------------------------------------------------------------
;  compute decompression levels
;---------------------------------------------------------------------------

   scal = 255./ALOG10((p2-p3) > 255)
   FOR k=-255, -1 DO IF (-k EQ ROUND(ALOG10(-k)*scal)) THEN lev = -k
                                ;PRINT, ' Level =', lev, ' Scale =', scal
   
;---------------------------------------------------------------------------
;  decompress
;---------------------------------------------------------------------------

   glev = WHERE(cim GT lev, gcnt)
   llev = WHERE(cim LE lev, lcnt)
   
   IF gcnt GT 0 THEN dim(glev) = ROUND(10^(cim(glev)/scal)+p3)
   IF lcnt GT 0 THEN dim(llev) = cim(llev)+p3
   
   RETURN
   
help_label:
   
   PRINT,' '
   PRINT,' Usage of DECOMP5 :
   PRINT,' DECOMP5,compressed_image,binary_header,decompressed_image
   PRINT,' '
   PRINT,' Input - compressedimage,binary_header'
   PRINT,' Output - decompressed_image'
   PRINT,' '
   
handle_error:
   
   IF errstr NE '' THEN MESSAGE,errstr,/cont
   errmsg = errstr
END


;---------------------------------------------------------------------------
; End of 'DECOMP5.PRO'.
;---------------------------------------------------------------------------
