FUNCTION	BCS_DECOMP,inj,iwrap
;+
; Program:   BCS_DECOMP
; purpose:   Decompression program for Solar_A BCS.
;            Given a compressed value of counts in a bin, produce
;            a true value of input counts.
;            Uses a compression scheme of "constant error" designed by 
;            AF, produced by programm SCHEME_CONSTANT.PRO and 
;            programmed into EPROM on 5-Dec-1990 by Ian Belchamber. 
;            The maximum relative error (true-decompressed)/decompressed 
;            of this scheme is: 
;            0 for decompressed values less or equal to 35.
;            +- 0.0138 for decompressed values between 36.5 and 23724
;                          (true input counts between 36 and 24051)
;            +- 0.002  for decompressed values between 24100 and 65431
;                          (true input counts between 24052 and 65535)
;            The range of true values corresponding to the compressed 
;            value j is then (rounded to the nearest integer): 
;            from (1 - error)*value(j) to (1 + error)*value(j) 
;            where 'error' is one of the above values 0, 0.0138, 0.002
;
; input:     J = a scalar value or an array of integer compressed values
;            A real number will be truncated to an integer. 
;            IWRAP: a scalar value, indicates whether normal
;                   decompression or wrapped around
;                   set iwrap to 24052 when wrapped around (to anything else 
;                   when normal compression)
; output:    VALUE = an array of decompressed counts. 
;
; history:
;	
;	AF, MSSL	5-Dec-1990	written:   
;	RDB		Feb 1991	made into a function
;	RDB		18-Feb-91	renamed from DECOMPRESS to BCS_DECOMP
;-

;  Convert to integer array, and allow for scalar value
   nj = N_elements(inj)
   if nj eq 1 then begin
	j = intarr(nj)
	j(0) = inj
   endif else  j = fix(inj)

;;   savej = j
;;   j = intarr( N_elements(savej) )
;;   j(0) = savej


;   silly cases can only be possible if input array is not a byte array

   ind = where(j lt 0)
   if ( ind(0) ge 0 ) then begin
      print,'$(a)','  ****  Negative compressed value  ***** '
      stop
   endif

   ind = where(j ge 256)
   if ( ind(0) ge 0 ) then begin
      print,'$(a)', ' **** Compressed value has to be le 255  **** '
      stop
   endif

  if n_params(0) eq 1 then iwrap=0


 if (iwrap ne 24052 ) then begin  ; normal decompression


 true = $
 [ 0.0,    1.0,    2.0,    3.0,    4.0,    5.0,    6.0,    7.0,$
 8.0,    9.0,   10.0,   11.0,   12.0,   13.0,   14.0,   15.0,$
 16.0,   17.0,   18.0,   19.0,   20.0,   21.0,   22.0,   23.0,$
 24.0,   25.0,   26.0,   27.0,   28.0,   29.0,   30.0,   31.0,$
 32.0,   33.0,   34.0,   35.0,   36.5,   38.5,   40.5,   42.5,$
 44.5,   46.5,   48.5,   50.5,   52.5,   54.5,   56.5,   58.5,$
 60.5,   62.5,   64.5,   66.5,   68.5,   70.5,   73.0,   76.0,$
 79.0,   82.0,   85.0,   88.0,   91.0,   94.0,   97.0,  100.0,$
 103.0,  106.0,  109.5,  113.5,  117.5,  121.5,  125.5,  129.5,$
 133.5,  137.5,  141.5,  146.0,  151.0,  156.0,  161.0,  166.0,$
 171.0,  176.0,  181.5,  187.5,  193.5,  199.5,  205.5,  211.5]


 true = [true, $
 218.0,  225.0,  232.0,  239.0,  246.0,  253.0,  260.5,  268.5,$
 276.5,  284.5,  293.0,  302.0,  311.0,  320.0,  329.5,  339.5,$
 349.5,  359.5,  370.0,  381.0,  392.0,  403.5,  415.5,  427.5,$
 440.0,  453.0,  466.0,  479.5,  493.5,  508.0,  523.0,  538.0,$
 553.5,  569.5,  586.0,  603.0,  620.5,  638.5,  657.0,  676.0,$
 695.5,  715.5,  736.0,  757.0,  778.5,  801.0,  824.0,  847.5,$
 872.0,  897.0,  922.5,  949.0,  976.0, 1003.5, 1032.0, 1061.5,$
 1092.0, 1123.5, 1155.5, 1188.0, 1221.5, 1256.0, 1291.5, 1328.0,$
 1365.5, 1404.0, 1443.5, 1484.0, 1526.0, 1569.5, 1614.0, 1659.5,$
 1706.5, 1755.0, 1804.5, 1855.5, 1908.0, 1962.0, 2017.5, 2074.5]


 true = [true, $
 2133.0, 2193.0, 2255.0, 2319.0, 2384.5, 2451.5, 2520.5, 2591.5,$
 2664.5, 2739.5, 2816.5, 2895.5, 2977.0, 3061.0, 3147.0, 3235.5,$
 3326.5, 3420.0, 3516.5, 3615.5, 3717.0, 3821.5, 3929.0, 4039.5,$
 4153.0, 4269.5, 4389.5, 4513.0, 4640.0, 4770.5, 4904.5, 5042.5,$
 5184.5, 5330.5, 5480.5, 5634.5, 5792.5, 5955.0, 6122.0, 6293.5,$
 6470.0, 6651.5, 6838.0, 7030.0, 7227.5, 7430.5, 7639.0, 7853.0,$
 8073.0, 8299.5, 8532.5, 8772.0, 9018.0, 9270.5, 9530.5, 9798.0,$
 10073.0,10355.5,10645.5,10944.0,11251.0,11566.5,11891.0,12224.5,$
 12567.0,12919.0,13281.0,13653.0,14035.5,14429.0,14833.5,15249.0,$
 15676.0,16115.0,16566.5,17031.0,17508.5,17999.0,18503.0,19021.0,$
 19553.5,20101.0,20664.0,21243.0,21838.0,22449.5,23078.0,23724.0]
  

 endif else begin  ; wrapped around ( input was ge 24052):



 true = $
 [24100.0,24197.0,24294.5,24392.5,24490.5,24589.0,24688.0,24787.5,$
  24887.5,24987.5,25088.0,25189.0,25290.5,25392.5,25494.5,25597.0,$
  25700.0,25803.5,25907.5,26012.0,26117.0,26222.0,26327.5,26433.5,$
  26540.0,26647.0,26754.5,26862.5,26970.5,27079.0,27188.0,27297.5,$
  27407.5,27518.0,27629.0,27740.0,27851.5,27963.5,28076.0,28189.0,$
  28302.5,28416.5,28531.0,28646.0,28761.5,28877.5,28993.5,29110.0,$
  29227.0,29344.5,29462.5,29581.0,29700.0,29819.5,29939.5,30060.0,$
  30181.0,30302.5,30424.5,30547.0,30670.0,30793.5,30917.5,31042.0,$
  31167.0,31292.5,31418.5,31545.0,31672.0,31799.5,31927.5,32056.0,$
  32185.0,32314.5,32444.5,32575.0,32706.0,32837.5,32969.5,33102.0 ]


 true = [true, $
  33235.0,33368.5,33503.0,33638.0,33773.5,33909.5,34046.0,34183.0,$
  34320.5,34458.5,34597.0,34736.0,34875.5,35016.0,35157.0,35298.5,$
  35440.5,35583.0,35726.0,35869.5,36014.0,36159.0,36304.5,36450.5,$
  36597.0,36744.0,36891.5,37040.0,37189.0,37338.5,37488.5,37639.0,$
  37790.5,37942.5,38095.0,38248.0,38401.5,38556.0,38711.0,38866.5,$
  39023.0,39180.0,39337.5,39495.5,39654.0,39813.5,39973.5,40134.0,$
  40295.5,40457.5,40620.0,40783.5,40947.5,41112.0,41277.5,41443.5,$
  41610.0,41777.5,41945.5,42114.0,42283.5,42453.5,42624.0,42795.5,$
  42967.5,43140.0,43313.5,43487.5,43662.0,43837.5,44014.0,44191.0,$
  44368.5,44547.0,44726.0,44905.5,45086.0,45267.5,45449.5,45632.0 ]


 true = [true, $
  45815.5,46000.0,46185.0,46370.5,46557.0,46744.0,46931.5,47120.0,$
  47309.5,47500.0,47691.0,47882.5,48075.0,48268.5,48462.5,48657.0,$
  48852.5,49049.0,49246.0,49443.5,49642.0,49841.5,50042.0,50243.0,$
  50444.5,50647.0,50850.5,51055.0,51260.5,51466.5,51673.0,51880.5,$
  52089.0,52298.5,52509.0,52720.0,52931.5,53144.0,53357.5,53572.0,$
  53787.5,54004.0,54221.0,54438.5,54657.0,54876.5,55097.0,55318.5 ]


 true = [true, $
  55541.0,55764.5,55988.5,56213.0,56438.5,56665.0,56892.5,57121.0,$
  57350.5,57581.0,57812.5,58045.0,58278.5,58513.0,58748.0,58983.5,$
  59220.0,59457.5,59696.0,59935.5,60176.0,60417.5,60660.0,60903.5,$
  61148.0,61393.5,61640.0,61887.5,62136.0,62385.5,62636.0,62887.5,$
  63140.0,63393.5,63648.0,63903.5,64160.0,64417.5,64676.0,64935.5,$
  65196.0,65431.0 ]


  nt = N_elements(true)
  if ( max(j) ge nt ) then begin
     print,'$(a,i3)', ' ** WRAP AROUND - compressed value has to be le ',nt-1
     stop
  endif


 endelse


 value = true(j)

return,value
end
