Viewing contents of file '../idllib/iuedac/iuelib/pro/datarec.pro'
;******************************************************************************
;+
;*NAME:
;
;	DATAREC
;
;*CLASS:
;
;	File Transfer
;  
;*CATEGORY:
;
;	IUESIPS
;  
;*PURPOSE:
;
;	Procedure to convert IUESIPS GO-format tape or disk files to
;	RDAF-format disk files - deals with data records.
;  
;*CALLING SEQUENCE:
;
;	DATAREC,UNIT,IMAGET,NCAM,NIMAGE,NL,NB,LUNI,ITFN,NBLOCKS
;  
;*PARAMETERS:
;
;	UNIT    (REQ) (S) (0) (I)
;		Input disk or device file name (e.g. on UNIX systems 'nrmt0',
;		on VMS systems an integer)
;
;	IMAGET  (REQ) (I) (0) (S)
;		IUE file name using standard RDAF notation (e.g. 'LWP12345l')
;
;	NCAM    (REQ) (I) (0) (I)
;		IUE camera number
;
;	NIMAGE  (REQ) (I) (0) (I L)
;		IUE image sequence number
;
;	NL      (REQ) (I/O) (0) (I L)
;		Number of lines (records) to be read in IUE file
;
;	NB      (REQ) (I) (0) (I L)
;		Number of bytes per record
;
;	LUNI    (REQ) (I) (0) (I)
;		Logical unit number of input device file (from LLDISK.PRO)
;  
;	ITFN    (REQ) (I) (0) (I)
;		ITF number to be inserted into scale factor record
;
;	NBLOCKS  (REQ) (I) (0) (B I)
;		 Number of 360-byte blocks found in label
;
;*EXAMPLES:
;  
;*SYSTEM VARIABLES USED:
;
;	!err_string
;
;*INTERACTIVE INPUT:
;
;	none
;  
;*SUBROUTINES CALLED:
;
;	PARCHECK
;       PLATFORM
;  
;*FILES USED:
;
;	GO format file - input.  Already opened by VICAR.PRO
;
;	IMAGET + '.dat' - output disk file
;
;*SIDE EFFECTS:
;  
;*RESTRICTIONS:
;
;	- GO file must be open and positioned at the beginning of the scale
;	  factor record (e.g., VICAR has been run)
;
;	- VICAR returns the logical unit of the device file being read 
;	  (sequentially) and this is needed by this procedure.
;
;	- Only for use with IUESIPS files.
;  
;*NOTES:
;
;	File format not checked.
;
;*PROCEDURE:
;
;	First the procedure modifies NIMAGE to handle numbers > 32767.  The
;	next step is to determine the dimensions of the output file.  For
;	extracted data files, each record is preceded with an extra 2 bytes
;	which represent (1) record number, and (2) number bytes in record.
;	These are removed.  Next, the output file is opened and the spectral
;	file data extracted.  A header record like that on extracted spectra
;	files is constructed for images.  The header is updated and saved and
;	the results of the procedure are printed to the screen.
;  
;*I_HELP:
;  
;*MODIFICATION HISTORY:
;
;    Written by LLT based on DDISK.
;   30 Apr 92 rwt modified to handle go disk files with truncated data
;                 records
;   24 Feb 93 LLT Fix problem with converting raw images read from tape.
;   22 Nov 93 PJL update documentation
;   10 Jun 94 PJL replaced !version with PLATFORM
;   20 Oct 94 PJL added code for tapectrl.device equal 'NA'
;   27 Apr 95 RWT add offset definition when tapectrl.device='NA'
;-
;******************************************************************************
 pro datarec,unit,imaget,ncam,nimage,nl,nb,luni,itfn,nblocks
;
 npar = n_params(0)
 if (npar eq 0) then begin
    print,'DATAREC,UNIT,IMAGET,NCAM,NIMAGE,NL,NB,LUNI,ITFN'
    retall
 endif  ; npar eq 0
;
 idfmt = ' '
 platform,dummy,tapectrl=tapectrl,sysos=sys,idfmt=idfmt
;;; sys = !version.os
;;; cpu = !version.arch
;
; modify nimage if > 32767 for compatibility with i*2 format
;
 nimage = fix (nimage - 65536*(nimage ge 32768))
;
; determine dimensions of output file
;
 nbytes = nb
;
; for extracted data files, each record is preceded with an extra 2 bytes
;   which represent (1) record number, and (2) number bytes in record.
;
 if ( (nb eq 1204) or (nb eq 2048) ) then begin
    nbytes = nb - 4                           ; subtract off 1st 2 bytes.
    nl     = nl - 1                           ; file already has a header record
 endif  ; nb
;
; raw images = byte data, all other types are integer*2.
;
 if ( (nb eq 768) and (nl eq 768) ) then begin
    ns   = nbytes 
    data = bytarr(nb,nl) 
 endif else begin
    ns = nbytes/2
    data = intarr(ns,nl) ; image data - will be redefined for extracted data
 endelse  ; type
;
; open output file
;
 file = imaget + '.dat'
 openw,/get_lun,lun,file,nbytes      
;
; assume input disk or device file already open 
;
 dev = unit
;
 case (tapectrl.device) of
   'NA':  begin
              if (strpos(unit,'.') lt 0) then begin
                 print,'ERROR in input device'
                 retall
              endif else begin
                 tflag = 0
                 offset = nbytes
              endelse
          end  ; tapectrl.device eq 'NA'
     '':  begin
            offset = 1
            s = size(unit)
            if (s(1) ne 7) then tflag = 1 else begin
               if (strpos(unit,'.') lt 0) then begin
                  print,'ERROR in input device'
                  retall
               endif else tflag = 0
            endelse  ; s(1)
          end  ; tapectrl.device eq ''
   else:  begin
              offset = nbytes
              if (strpos(unit,'.') lt 0) then begin         ; device file
                 tflag = 1
                 dev = tapectrl.device + unit
              endif else tflag = 0                          ; disk file
          end  ; else
 endcase  ; tapectrl.device
;
 on_ioerror,tape_err
;
; extracted spectral file data (type l,s, or h)
;
 if ( (nb eq 1204) or (nb eq 2048) ) then begin
    temp = nb/2
    if ( (nb eq 2048) and (tflag eq 0) ) then begin
       a = fstat(luni)
       actleft = a.size - (nblocks*360L)
       expleft = nb * (nl+1L)
       if (sys eq 'vms') then vaxbytes = 2 * (nblocks+nl+1) else vaxbytes = 0
       diff = expleft + vaxbytes - actleft
       if (diff gt 48) then begin
          expdiff = (nl+1L) * 48L
          if ((diff-expdiff) lt 48) then $
          print,'    truncated data records found; program continuing' else $
          print,'    possibly truncated data records found; program continuing'
          temp = 1000
       endif  ; diff gt 48
    endif  ; (nb eq 2048) and (tflag eq 0)
;
    h = intarr(temp)
    record = assoc(lun,h)
    if (tapectrl.device eq '') and tflag then begin    ;Read scale factor record
       taprd,h,luni,1 
    endif else begin
       readu,luni,h
       if (idfmt ne 'ieeebig') then byteorder,h       ; swap bytes
    endelse  ; read in data
    h = h(2:*)                                    ; strip off leading bytes.
    h([0,1]) = [nbytes,ns]
    if (h(4) eq 0) then h(4) = nimage             ; correct for early sips error
    if (h(4) gt 300) and (h(4) ne nimage) then begin ;compare label & record 0
       print,'WARNING: Image number in label disagrees with' $
          ,' number in record 0'
       print,' nimage =',nimage,' h(4) =',h(4)
       print,'ACTION: Continuing'
    endif  ; end check of label & record 0
    h([70,71]) = [1,h(4)] 
    h([580]) = itfn
    record(0) = h
   ;
   ; read data records for type= s,l or h.
   ;
    tdata = intarr(temp,nl)
    record = assoc(lun,data,offset)
     if (tapectrl.device eq '') and tflag then begin         ;Read data records
          datar = intarr(nb/2)
          for ii=0,nl-1 do begin            ;TAPRD can only read one record
             taprd,datar,luni,1            ;at a time.
             tdata(0,ii)=datar
          endfor  ; ii
     endif else begin
          readu,luni,tdata
          if (idfmt ne 'ieeebig') then byteorder,tdata     ; swap bytes
     endelse  ; read in data
     data(0,0) = tdata(2:*,*)             ; strip off leading bytes 
     record(0) = data        
     if (tapectrl.device ne '') then close,luni
 endif else begin
   ;
   ; image file data (type r, f, or p)
   ; construct header record like that on extracted spectra files
   ;
    h     = intarr(nbytes/2)
    rec0  = assoc(lun,h)
    h([0,1,2,3,4,5,70,71]) = [nbytes,ns,nl,ncam,nimage,1,1,nimage] 
    record = assoc(lun,data,offset)
    if (tapectrl.device eq '') and tflag then begin
       if (nb eq 768) then begin
          datar = bytarr(nb)
          for ii=0,nl-1 do begin            ;TAPRD can only read one record
             taprd,datar,luni            ;at a time.
             data(0,ii) = datar
          endfor  ; ii
       endif else begin
          datar = intarr(nb/2)
          for ii=0,nl-1 do begin            ;TAPRD can only read one record
             taprd,datar,luni,1            ;at a time.
             data(0,ii)=datar
          endfor  ; ii
       endelse  ; nb eq 768
    endif else begin
       readu,luni,data
       if ( (idfmt ne 'ieeebig') and (nb ne 768) ) then byteorder,data 
    endelse  ; read in data
    if (tapectrl.device ne '') then close,luni
   ;
   ; update and store header 
   ;
    h([18,19]) = [min(data),max(data)]
    rec0(0) = h
    ;
;    record(nl+1) = maxs(0:nbytes/2-1)
;    record(nl+2) = mins(0:nbytes/2-1)
    record(0) = data
    print,'min. and max. values:', min(data), max(data)
 endelse  ; image transfer
;
 on_ioerror,null
;
; close luns & end
;
 free_lun,lun
 print,'   '+strtrim(string(nl+1b),2)+' records written to '+file
 if not (tflag and (tapectrl.device eq '')) then free_lun,luni   ;CLOSE GO file.
 return
;
; tape or other errors 
;
 tape_err:
    print,'Unexpected error encountered in DATAREC'
    print,!err_string
    if not (tflag and (tapectrl.device eq '')) then free_lun,luni
    on_ioerror,null
    retall 
;
 end  ; datarec