Viewing contents of file '../idllib/iuedac/iuelib/pro/convert.pro'
;******************************************************************************
;+
;*NAME:
;
;      CONVERT
;
;*CLASS:
;
;*CATEGORY:
;
;      IUESIPS
;
;*PURPOSE:  
;
;      To convert IUE .dat and .lab files to a format compatible
;      with the host operating system. CONVERT supports IUE RDAF-format
;      file conversions between SunOs (Sun-3s, Sun-4s, and SPARCStations),
;      ULTRIX (DECStation 3100), DOS (386-class PC), and VMS (VAX).
;
;*CALLING SEQUENCE:
;
;      CONVERT,IMAGEN,cpupar
;
;*PARAMETERS:
;
;      IMAGEN	(REQ) (I) (S) (0)
;      		String name for IUE image file copied from VAX
;        	If no extension is specified, program assumes that both
;        	a .dat and a .lab file exists and both are to be converted.
;
;      CPUPAR  (OPT) (I) (I) (0)
;              Parameter to identify data translation mode.
;
;                 ULTRIX/VAX  to  VMS/VAX
;                               or
;                 ULTRIX/MIPSEL  to VMS/MIPSEL  0   (VMS only)
;
;                 VAX     to  MIPSEL            1
;                 MIPSEL  to  VAX               2
;
;                 VAX     to  SPARC             3
;                 SPARC   to  VAX               4
;
;                 MIPSEL  to  SPARC             5
;                 SPARC   to  MIPSEL            6
;
;              Supported Data Types:
;
;                 VAX    - VAXstations, MicroVAX
;                 MIPSEL - DECstations, IBM 386
;                 SPARC  - SparcStations, Sun 4##
;
;            No Conversion is required between like data types when
;            the destination machine uses stream files (SUNOS, ULTRIX
;            and DOS). Conversion is required for like data types only
;            when the destination machine is using the VMS operating 
;            system (see 0 option above).
;
;            If CPUPAR is not given in the call, the user will be prompted
;            for it.
;
;            Note that only modes in which the final data format is
;            compatible with the host operating system are allowed.
;
;*EXAMPLES:
;
;     convert,'swp32199s.lab'        ; convert .lab file
;     convert,'swp32199s'            ; converts both .dat and .lab files
;
;*SYSTEM VARIABLES USED:
;
;     !err_string
;
;*INTERACTIVE INPUT:
;
;     If CPUPAR is not given in the call, the user will be prompted for it.
;
;*SUBROUTINES CALLED:
;
;     PARCHECK
;     IUE_PARSE
;     CHKFITS
;     SWAP_BYTES
;     CONMENU
;
;*FILES USED:
;
;     imxgen given in calling sequence.
;
;*SIDE EFFECTS:
;
;*RESTRICTIONS:
;
;     - files must be transferred between hosts in binary (image) mode.
;     - CONVERT must be executed on the "destination" machine.
;     - imaget must use standard RDAF file naming conventions 
;       (e.g. swp 32199s) 
;     - Valid file types include RI, PI, MELO, ELBL, FES, and MEHI, although
;       FES images must have at least 18 byte records.
;     - Not for use on fits formatted files.
;
;*NOTES:
;
;     - All files are truncated to have record lengths which agree
;       with value listed in the header record.
;
;*PROCEDURE:
;
;     Before converting files, CONVERT uses the procedure CONMENU to define
;     the allowed conversion modes and next_flag.  Once an allowed conversion
;     mode has been selected, the value of cpupar is used to set the variable
;     swap_flag and last_flag.  The input file variable imagen is parsed and
;     the presence on a file extension (*.lab or *.dat) is used to determine
;     which file types to convert.  First the input file is checked to
;     determine if it is a fits formated file.  If the input file is in fits
;     format, no conversion is done and the procedure returns.  The input file
;     is then opened, and a temporary header vector is obtained, and converted
;     to the requested output format according to the value of swap_flag.  The
;     conversion flag (flag) is read from this header vector and used for error
;     checking. The variables flag, last_flag, and next_flag take on values
;     according which machine architecture the file was or will be converted
;     to. These variables take on the values:
;
;         !version.arch    flag value
;             VAX             1
;             SPARC           2
;             MIPSEL          3
;             386             3
;
;     The value of flag is compared to the value last_flag to insure that the
;     correct conversion option was selected.  Upon successful file conversion,
;     the conversion flag is set to the value of next_flag.  Once the
;     conversion options and input file are checked, conversion parameters are
;     obtained from the temporary header and the fstat function.  The file is
;     then associated with a 2-d array (bytes per record, number of records),
;     and the entire file is read into a single 2-d array.  Label files are
;     truncated to 74 byte records, and written to disk with a converted header
;     vector.  Data files are truncated to a record length in agreement with
;     the value given by h(0). An additional conversion flag swap_all is set
;     depending on the data file type. The values of swap_flag and swap_all are
;     used to determine how the data is converted.  On VMS systems, the output
;     file is written to a higher version number.  On Unix systems, the output
;     file is written over the input file.
;     
;     CONVERT is written to be called by CON_RDAF so that multiple files can be
;     converted in one execution of CON_RDAF.  For this reason, returns are
;     executed rather than retalls.
;
;*MODIFICATION HISTORY:
;
;     6/28/89  RWT  version 1  
;     8/09/89  RWT  modify for converting FES images 
;                   and remove superfluous bytes from .lab & .dat files
;     8/14/89  RWT  remove need for bflag parameter & compress code
;     8/17/89  RWT  truncate records to value listed in h(0)
;     6/07/90  RWT  modify for handling DECstation format
;     2/18/91  GRA  modified to allow file conversion between
;                   SunOs, DEC and VAX machines. Do all swap
;                   operations on byte variable BEFORE they are
;                   read as any other type.
;     3/28/91  PJL  added PARCHECK; modified to allow output 
;		    record size > 32768 bytes
;     4/30/91  RWT  add support for DOS
;     5/20/91  GRA  modified last_flag to reflect DOS support
;                   allow h(7) or h(587) flag to be zero for
;                   files that originate on any cpu type
;     6/12/91  GRA  merged vms and sun versions, corrected error
;                   checking sections and comments to reflect
;                   additional values of cpupar.
;     6/21/91  GRA  fixed syntax error in the section which defines
;                   cpustrarr, updated prolog.
;     7/25/91  PJL  cleaned up; tested VAX to SUN and SUN to VAX options; 
;		    updated prolog
;     10/4/91  GRA  cleaned up print statements in label conversion
;                   section.
;     10/9/91  GRA  globally changed all references to "syspar" to "cpupar",
;                   changed cpupar to reference data formats as specified by
;                   the !version.arch system variable; used /block keyword
;                   when opening input file on a vms machine so that the
;                   rms information can be bypassed; condensed vms and unix
;                   branches; updated prolog; tested on ultrix/vax,
;                   ultrix/mipsel, vms/vax, sunos/sparc.
;     10/31/91 GRA  cleaned up print statements.
;     11/01/91 PJL  changed CON_GO reference to CON_RDAF
;      8/16/93 RWT  add version number to .dat file name, fix error in
;                   listing .dat file name, remove begin & ends for single 
;                   commands, and reduce printout
;     11/15/93 PJL  added CHKFITS; update prolog
;      5/24/94 RWT  read minimum number of bytes in temporary header (to 
;                   allow conversion of fes images with < 600 byte records)
;    24 May 94 LLT  compare expected number of bytes per record to total bytes
;                   in file divided by expected number of records (image data
;                   only) to check for spurious records at end of file.
;    30 Jun 94  PJL  replaced !version with CONMENU
;
;-
;******************************************************************************
 pro convert,imagen,cpupar
;
 npar = n_params(0)
 on_ioerror,nofile
;
 if (npar eq 0) then begin
    print,'CONVERT,IMAGET,cpupar'
    retall
 endif  ; npar eq 0
 parcheck,npar,[1,2],'CONVERT'
;
 lastrarr = ['UNDEFINED','VAX','SPARC','MIPSEL']
;
 conmenu,cpupar,cpustr,next_flag=next_flag
;
; set swap_flag based on cpupar
; set last_flag, allowed previous conversions
; last_flag = 1 for VAX, 2 for SPARC, 3 for MIPSEL - input data format
;
 case cpupar of 
     0: begin
           swap_flag = 0
           if (cpu eq 'vax') then last_flag = 1 else last_flag = 3
        end  ; 0
     1: begin
           swap_flag = 0
           last_flag = 1
        end  ; 1
     2: begin
           swap_flag = 0
           last_flag = 3
        end  ; 2
     3: begin
           swap_flag = 1
           last_flag = 1
        end  ; 3
     4: begin
           swap_flag = 1
           last_flag = 2
        end  ; 4
     5: begin
           swap_flag = 1
           last_flag = 3
        end  ; 5
     6: begin
           swap_flag = 1
           last_flag = 2
        end  ; 6
 endcase  ; cpupar
;
 iue_parse,imagen,disk,path,cam,isn,type,aper,ext,ver
;
; if ext = '', set flag for converting both a .lab & a .dat file
;
 if (ext eq '') then mod2 = 1 else mod2 = 0
;
; if .lab file, correct record 0 entries and convert
;
 if ( (ext eq '.lab') or (mod2) ) then begin
   ;
   ; form *.lab name
   ;
    imaget=disk+path+cam+isn+type+aper+'.lab'+ver
;
; make sure not a fits file
;
    chkfits,imaget,newsips,/silent
    if (newsips) then begin
       print,imaget + ' is a fits file.  File will not be converted.'
       return
    endif  ; newsips
   ;
    print,' '
    print,' '
    print,'Converting ' + imaget + ' from ' + cpustr + ' format.'
   ;
   ; open file, get temporary header
   ;
    openr,/get_lun,unit,imaget,/block
    rec = assoc(unit,bytarr(40))
    status = fstat(unit)
   ;
    bh = rec(0)
    if (swap_flag) then swap_bytes,bh,2
   ;
    h = fix(bh,0,20)
   ;
   ; check input file conversion flag 
   ;
    lab_flag = 7
    flag = h(lab_flag)
   ;  
    if (flag ne 0) then begin             
       ;
       ; non-zero value indicates previous conversion
       ; value lt 0 or gt 3 indicates improper conversion option
       ; i.e. running program on CPU other than destination machine
       ; or input file is not of specified type
       ; try swapping bytes to read flag
       ;
       if ( (flag lt 0) or (flag gt 3) ) then begin
          swap_bytes,bh,2
          h = fix(bh,0,20)
          flag = h(lab_flag)
          if ( (flag ge 0) or (flag le 3) ) then errstr = lastrarr(flag) $
             else errstr = 'UNDEFINED'
          print,' '
          print,' Difficulty reading input file.'
          print,' '
          print,'  File ' + imaget + ' previously converted'
          print,'     to ' + errstr + ' format.'
          print,' '
          print,'  Input file format is incompatible'
          print,'     with requested conversion option.'
          print,' '
          print,' Returning from CONVERT'
          close,unit
          free_lun,unit
          return
       endif  ; flag < 0 or flag > 3 
       print,' '
       print,' File ' + imaget + ' previously converted'
       print,'   to ' + lastrarr(flag) + ' format.'
       if (last_flag ne flag) then begin
          print,' '
          print,'   Input File format is incompatible'
          print,'      with requested conversion option.'
          print,' '
          print,' Returning from CONVERT'
          close,unit
          free_lun,unit
          return
       endif  ; flag ne 
    endif 
   ;
   ; define conversion parameters
   ;
    tbytes = status.size           ; total bytes in file
    recl = 74L                     ; assume 74 bytes / record
    nrec = h(2) + 1                ; # of records
    bpr = status.size / nrec       ; bytes per record
   ;
    print,' '
    print,'   File type = label'
    print,'   Total # of bytes = ' + strtrim(tbytes,2)
    print,'   Expected record length = ' + strtrim(recl,2)
    print,'   Actual label record length (in bytes) = ' + strtrim(bpr,2)
    print,'   Number of records = ' + strtrim(nrec,2)
   ;
    if (bpr eq 0) then begin
       print,' '
       print,' ERROR reading from '+imaget
       print,' Returning from CONVERT'
       close,unit
       free_lun,unit
       return
    endif  ; bpr eq 0
   ;
   ; read entire file into 2-d array
   ;
    rec = assoc(unit,bytarr(bpr,nrec))
    tmp = rec(0)
    close,unit
   ;
   ; remove any superfluous bytes
   ; (i.e. VAX filler bytes or end-of-record bytes)
   ;
    if (bpr ne recl) then begin
       print,'   Truncating label to 74-byte records'
       tmp = tmp(0:recl-1,*)
    endif  ; bpr ne recl
   ;
   ; get new header
   ;
    bh = tmp(0:recl-1,0)
   ;
   ; swap bytes in header depending on cpupar
   ;
    if (swap_flag) then swap_bytes,bh,2
   ;
    h = fix(bh,0,recl/2)
    h([0,1]) = recl              ; 74 bytes per record 
    h(lab_flag) = next_flag      ; set file conversion flag
    tmp(0) = byte(h,0,recl)      ; byte header
   ;
   ; write out file
   ;
;;;    if (sys eq 'vms') then outfile = disk+path+cam+isn+type+aper+'.lab'  $
;;;      else outfile = disk+path+cam+isn+type+aper+'.lab'
    outfile = disk + path + cam + isn + type + aper + '.lab'
   ;
    openw,unit,outfile,recl
    rec = assoc(unit,bytarr(recl,nrec))
    rec(0) = tmp
    close,unit
    free_lun,unit
   ;
 endif  ; lab file
;
; if .dat file, determine file type and convert accordingly
;
 if ( (ext eq '.dat') or (mod2) ) then begin
   ;
   ; set some additional parameters
   ;
    uptype = strupcase(type)
   ;
    case uptype of
       'R': begin               ; IUE raw image file
               dat_flag=7
               addr = 3
               swap_all = 0     ; no byte swapping needed (for data records)
            endcase  ; 'R'
       'F': begin               ; IUE FES image
               dat_flag=7
               addr = 3
               swap_all = 1     ; byte swapping necessary
            endcase  ; 'F'
       'P': begin               ; IUE PI image file
               dat_flag = 7
               addr = 3
               swap_all = 1     ; byte swapping necessary
            endcase  ; 'P'
      else: begin               ; IUE MELO, MEHI & LBLS files
               dat_flag = 587   ; use h(587) for correction flag
               addr = 1         ; add 1 record
               swap_all = 1     ; swap bytes
            endcase  ; else
    endcase  ; uptype
   ;
   ; form *.dat name
   ;
    imaget=disk+path+cam+isn+type+aper+'.dat'+ver
   ;
   ; make sure not a fits file
   ;
    chkfits,imaget,newsips,/silent
    if (newsips) then begin
       print,imaget + ' is a fits file.  File will not be converted.'
       return
    endif  ; newsips
   ;
    print,' '
    print,'Converting ' + imaget + ' from ' + cpustr + ' format.'
   ;
   ; open file, get temporary header
   ;
    hrl = dat_flag * 2 + 2           ; fewest bytes read from temporary header
    openr,/get_lun,unit,imaget,/block
;    rec = assoc(unit,bytarr(1200))
    rec = assoc(unit,bytarr(hrl))    
    status = fstat(unit)
   ;
    bh = rec(0)
    if (swap_flag) then swap_bytes,bh,2
   ;
;    h = fix(bh,0,600)
    h = fix(bh,0,hrl/2)
   ;
   ; check input file conversion flag
   ;
    flag = h(dat_flag)
   ; 
    if (flag ne 0) then begin             
      ;
      ; non-zero value indicates previous conversion
      ; value lt 0 or gt 3 indicates improper conversion option
      ; i.e. running program on CPU other than destination machine
      ; or input file is not of specified type.
      ; try swapping bytes to read flag
      ;
       if ( (flag lt 0) or (flag gt 3) ) then begin
          swap_bytes,bh,2
;         h = fix(bh,0,600)
          h = fix(bh,0,hrl/2)
          flag = h(dat_flag)
          if ( (flag ge 0) or (flag le 3) ) then errstr = lastrarr(flag) $
             else errstr = 'UNDEFINED'
          print,' '
          print,' Difficulty reading input file.'
          print,' '
          print,'  File ' + imaget + ' previously converted'
          print,'     to ' + errstr + ' format.'
          print,' '
          print,'  Input File format is incompatible'
          print,'     with requested conversion option.'
          print,' '
          print,' Returning from CONVERT'
          close,unit
          free_lun,unit
          return
       endif  ; flag < 0 or flag > 3 
       print,' '
       print,' File ' + imaget + ' previously converted'
       print,'   to ' + lastrarr(flag) + ' format.'
       if (last_flag ne flag) then begin
          print,' '
          print,'   Input File format is incompatible'
          print,'      with requested conversion option.'
          print,' '
          close,unit
          free_lun,unit
          print,'Returning from CONVERT'
          return
       endif  ; flag ne 
    endif 
   ;
   ; define conversion parameters
   ;
    tbytes = status.size            ; total bytes in file
    recl = long(h(0))               ; expected record size 
    nrec = h(2)*h(5) + addr         ; expected # of records
    bpr = status.size/nrec          ; bytes per record input
    if (dat_flag eq 7) and (bpr lt recl) then begin
       nrec=h(2)+1
       bpr=status.size/nrec
    endif ;image data, records too short
   ;
    print,' '
    print,'   File type = data'
    print,'   Total # of bytes = ' + strtrim(tbytes,2)
    print,'   Expected record length = ' + strtrim(recl,2)
    print,'   Actual data record length = ' + strtrim(bpr,2)
    print,'   Number of records = ' + strtrim(nrec,2)
   ;
    if (bpr eq 0) then begin
       print,' '
       print,' ERROR reading from '+imaget
       print,' Returning from CONVERT'
       close,unit      
       free_lun,unit
       return
    endif  ; bpr
   ;
   ; read entire file into 2-d array
   ;
    rec = assoc(unit,bytarr(bpr,nrec))
    tmp = rec(0)
    close,unit
   ;
   ; remove any superfluous bytes
   ; (i.e. VAX filler bytes or end-of-record bytes)
   ;
    if (bpr ne recl) then begin
       print,'   Truncating data file records to remove superfluous bytes'
       tmp = tmp(0:recl-1,*)       ; truncate records to agree with h(0)
    endif  ; bpr ne recl
   ;
   ; swap bytes depending on cpupar for all but RAW images
   ;
    if (swap_all and swap_flag) then swap_bytes,tmp,2
   ;
   ; get new header
   ;
    bh = tmp(0:recl-1,0)
   ;
   ; if RAW image and swap_flag, swap bytes in header
   ;
    if ((swap_all eq 0) and swap_flag) then swap_bytes,bh,2
   ;
   ; add conversion flag
   ;
    h = fix(bh,0,recl/2)
    h(dat_flag) = next_flag          ; set file conversion flag
    tmp(0) = byte(h,0,recl)          ; byte header
   ;
   ; write out file
   ;
;;;    if (sys eq 'vms') then outfile = disk+path+cam+isn+type+aper+'.dat' $
;;;      else outfile = disk+path+cam+isn+type+aper+'.dat'
    outfile = disk + path + cam + isn + type + aper + '.dat'
   ;
    openw,unit,outfile,recl
    rec = assoc(unit,bytarr(recl,nrec))
   ;
    rec(0) = tmp
   ;
    close,unit
    free_lun,unit
   ;
 endif  ; dat file
;
 return
;
 nofile:
    print,' '
    print,' IO ERROR detected converting file ' + imaget
    print,'  ' + !err_string
    print,' Returning from CONVERT'
    return
;
 end  ; convert