;+
; NAME:
;       MKUWORDS
;
; PURPOSE:
;      
;       MKUWORDS is a collection of functions to make unsigned 16, 24
;       and 32 bit words (both little and big endian) from an array of
;       bytes. 
;                                                                  
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;       each of the functions are called in the same way, as an
;       example we use mkuintle:
;       uintleword = mkuintle(buf, is)
;
; INPUTS:
;       buf: The array of bytes from which to construct the words.
;       is: The position in buf from where to start.
;
; KEYWORD PARAMETERS:
;
;
; OUTPUTS:
;       unsigned 16, 24 or 32 bit words (littl or big endian)
;       depending on which functions is used.
;
; CALLS:
;       
;
; COMMON BLOCKS:
;       
;
; PROCEDURE:
;
;
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       XX-Apr-2001: Viggo Hansteen,Oivind Wikstol
;       03-Oct-2001: Oivind Wikstol. Added documentation.
;-
function mkuint,buf,is
  if n_elements(is) eq 0 then is = 0
  b=ulong(2^8)
  return,uint(b*buf(is+1)+buf(is))
end

function mkulong,buf,is
  if n_elements(is) eq 0 then is = 0
  b=ulong(2^8)
  return,ulong(buf(is)+b*(buf(is+1)+b*(buf(is+2)+b*buf(is+3))))
end

function mku24,buf,is
  if n_elements(is) eq 0 then is = 0
  b=ulong(2^8)
  return,ulong(buf(is)+b*(buf(is+1)+b*buf(is+2)))
end

function mkuintle,buf,is
  if n_elements(is) eq 0 then is = 0
  b=ulong(2^8)
  return,uint(b*buf(is)+buf(is+1))
end

function mkulongle,buf,is
  if n_elements(is) eq 0 then is = 0
  b=ulong(2^8)
  return,b*b*ulong(mkuintle(buf,is))+ulong(mkuintle(buf,is+2))
end

function mku24le,buf,is
  if n_elements(is) eq 0 then is = 0
  b=ulong(2^8)
  return,ulong(buf(is+2)+b*(buf(is+1)+b*buf(is)))
end

pro ccsds_hdr::read, lu, swap = swap
;+
; NAME:
;       CCSDS_HDR__READ
;
; PURPOSE:
;       CCDSD_HDR__READ reads ccsds headers from SOLAR-B packet data
;       and stores header info in the instance
;       data of objects of class 'ccsds_hdr'
;
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;       An object of class CCSDS_HDR is generated with this statement: 
;                   ccsds_hdr = obj_new('ccsds_hdr')
;
;       CCSDS_HDR__READ reads the CCSDS header, which is a part of all
;       SOLAR B data packets. The ccsds header is read and stored in
;       the object's instance data with this statement: 
;                     ccsds_hdr-> read, lu  (logical unit of data file)
;
; INPUTS:
;       lu:  Logical unit number of data file
;
; KEYWORD PARAMETERS:
;
; OUTPUTS:
;       Reads CCSDS header data into the parameters of the CCSDS_HDR
;       object.
;
; CALLS:
;       MKUINTLE
;       MKU24LE
;       MKULONGLE
;
; COMMON BLOCKS:
;       
;
; PROCEDURE:
;       The procedure reads CCSDS header data into the instance data
;       of the CCSDS header object.
;       The CCSDS header is 10 bytes long. It consists of the
;       following parameters:
;         Version number: (3 bit). Stored as IDL byte.
;         Type: (1 bit). Stored as IDL byte
;         Sequence header flag: (1 bit). Stored as IDL byte
;         Apid: (11 bit). Stored as IDL unsigned integer.
;         Sequence flag. (2 bit). Stored as IDL byte
;         Sequence count: (14 bit). Stored as IDL unsigned integer
;         Packet length: (16 bit). Stored as IDL unsigned integer
;         Time: (32 bit). Stored as IDL unsigned long.
;                
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       XX-Mar-2001: Oivind Wikstol.
;       01-Oct-2001: Oivind Wikstol. Added documentation.
;       03-Nov-2005: Oivind Wikstol - Added Swap keyword.
;-
  ccsds_length = 10
  header=bytarr(ccsds_length)
  readu,lu,header
  if n_elements(swap) eq 0 then swap=0
;   swap bytes in header in order to convert to big endian
  if swap ne 0 then begin
    for i=0,ccsds_length-2,2 do begin
      buf=header[i]
      header[i]=header[i+1]
      header[i+1]=buf
    endfor
  endif  

  self.raw = header
  self.ver_no = ishft(header(0),-5)
  self.type = ishft(ishft(header(0),3),-7)
  self.seq_hdr_flag = ishft(ishft(header(0),4),-7)
  dum=mkuintle(header,0)
  self.apid = ishft(ishft(dum,5),-5)

  self.seq_flag=ishft(header(2),-6)
  dum=mkuintle(header,2)
  self.seq_count=ishft(ishft(dum,2),-2)

  self.pkt_len = mkuintle(header,4)
  self.time = mkulongle(header,6)

end

