Viewing contents of file '../idllib/iuedac/iuelib/pro/dtrans.pro'
;******************************************************************************
;+
;*NAME:
;
;      DTRANS
;
;*PURPOSE:  
;
;      To convert homogeneous data files to a format compatible with the
;      current operating system. DTRANS supports data file conversions between
;      SPARC (Sun-4s and SPARCStations), MIPSEL (DECstation 3100 and IBM
;      386-class PCs) and VAX (VAXstation 3100 and MicroVAX).
;
;*CALLING SEQUENCE: 
;
;      DTRANS,IMAGET,vartyp,cpupar,recl
;
;*PARAMETERS:
;
;      IMAGET     (R) (I) (S) (0)
;          String name for file copied to host machine.
;      
;      VARTYP     (O) (I) (I) (0)
;          Type of data, where:
;
;                 byte              1
;                 integer           2
;                 longword integer  3 
;                 floating point    4
;                 double precision  5
;
;          If vartyp is not included in the calling statement, 
;          DTRANS will prompt the user.
;          
;      CPUPAR   (OPT) (I)  (I)     (0)
;          Parameter to identify data translation mode. 
;
;              no conversion        0
;
;              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##
;
;          Note: No Conversion is required between like data types,
;                such as DECstations and IBM 386s.
;
;          If CPUPAR is not given in the call, the user will be prompted
;          for it.
;
;          Note that only two modes are allowed for a given machine.
;          The allowed modes are those in which the final data format
;          is compatible with the host operating system. 
;          
;      RECL (O) (I) (IL) (0)
;          Logical record length in bytes. Required for all transfers
;          to the VMS system, so that the output record length may be
;          defined. If not included in the call in this situation,
;          DTRANS will prompt the user. This parameter is not required
;          for conversion to SunOs, ULTRIX, and DOS formats.
;
;
;*FILES USED:
;
;         Output file of converted data is stored as:
;             imaget;highest-version-number (VMS) or,
;             imaget.new (all others)
;
;*PROCEDURE:
;
;       Before converting files, DTRANS uses the procedure CONMENU to determine
;       the allowed conversion modes. The input file is opened, and the total 
;       number of input bytes is determined via the FSTAT command.  The input
;       parameters to the conversion process (vartyp, cpupar, and recl (vax
;       only)) are obtained from user input if these parameters were not
;       included in the call to DTRANS.  When selecting the vartyp, the value
;       of tbyt mod vbyt is used to detect errors due to non-binary data file
;       transfer, or errors in the selection of vartyp. On vms systems, tbyt
;       mod recl, and tbyt mod vbyt are used to detect errors.  If an error is 
;       detected, the user may choose to respecify the vartyp or recl, or abort
;       the procedure. Once valid conversion parameters are defined, DTRANS
;       proceeds to convert the data file to the new machine format. 
;
;       VMS systems:
;
;          The record length used for input and output associated
;          variables is given by the user. Single records are read
;          into a holding variable, translated to the proper format
;          by the routine TRANS_BYTES, and written out to a higher
;          version number. 
;
;       UNIX systems:
;
;          The value of tbyt determined from the FSTAT command is 
;          used to generate a list of factors of the total input
;          file size in bytes. The factor nearest to 2048 bytes
;          is used as a holding array size. Data is then read into 
;          the holding array from the input file, converted to the
;          new machine format, and written to disk. The output file
;          is named imaget + '.new' 
;
;*RESTRICTIONS:
;
;       - Files must be transferred between hosts in binary (image) mode.
;
;       - Files must have fixed length records 
;
;       - All values in data files must have the same data type.
;
;       - TRANS_BYTES does not currently convert complex data types.
;
;*SUBROUTINES CALLED:
;
;	PARCHECK
;       TRANS_BYTES 
;       DECOMPOSE
;       CONMENU
;       PLATFORM
;
;*SYSTEM VARIABLES USED:
;
;	none
;
;*NOTES:
;
;       - IUE GO files should be converted using con_rdaf or convert.
;
;       - IUE SAV files should be converted using con_sav.
;
;       - To convert files between computer systems which do not store
;         data as on a SUN, DECstation, DOS, or VAX, the subroutine
;         TRANS_BYTES should be modified.
;
;       - DTRANS checks for incompatibility between the total number 
;         of input bytes and requested data type and record length.
;         Such incompatibility results from data file transfer in
;         ASCII or other non-binary modes, or from errors in data
;         type or record length selection. If an error is detected,
;         the user has the option of reselecting the appropriate
;         parameter, or aborting the procedure. Not all errors
;         resulting from non-binary mode data file transfer will
;         be detected. To avoid possible undetected errors, always
;         transfer data files in binary mode.
;
;         Additionally, if a file is originally on a VMS system and has the
;         record attribute of "Carriage return carriage control" the following
;         steps BEFORE transferring the file are necessary:
;
;               $ anal/rms/fdl file.dat
;               $ edit/fdl file.fdl
;                   This puts you into an editor with a menu interface.  Type:
;                       mod
;                       rec
;                       carr
;                       none
;                       <ctrl-Z>
;                       exit
;               $ convert file.dat newfile.dat /fdl=file.fdl
;
;         where file.dat is the orginial file and newfile.dat is the corrected
;         file.
;
;	tested with IDL Version 2.1.2 (sunos sparc)     14 Oct 91
;	tested with IDL Version 2.1.2 (ultrix mispel)   14 Oct 91
;       tested with IDL Version 2.2.0 (ultrix vax)      14 Oct 91
;	tested with IDL Version 2.1.2 (vms vax)         14 Oct 91
;
;*EXAMPLES:
;
;*MODIFICATION HISTORY:
;
;     6/28/89  RWT  version 1  
;     7/6/89 RWT modified to handle files with and without
;       end-of-record bytes.
;     2/28/90 RWT correct typo in longword conversion assoc command
;       and expand prolog.
;     5/30/90 RWT replace vtos with vtou so program runs on both
;       SUNs and DECstations.
;     2/18/91 GRA include further modifications to enable file format
;       conversion between VMS, SunOs, and ULTRIX formats.
;     3/18/91 GRA renamed vtou to trans_bytes
;     4/30/91 RWT add support for DOS 
;     6/13/91 GRA changed "dos" to "DOS"
;     6/26/91 PJL cleaned up; added PARCHECK; tested SUN/VAX combinations;
;		  updated prolog
;     10/3/91 GRA Numerous modifications; removed sections of code which
;                 compensated for ASCII mode transfer; condensed and
;                 streamlined error checking and data translation 
;                 sections; tested on SUN, DEC, and VAX systems;
;                 updated prolog.
;     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
;    29 Apr 94  PJL  added notes about "Carriage return carriage control"
;    10 Jun 94  PJL  replaceed !version with CONMNEU and PLATFORM
;
;-
;******************************************************************************
 pro dtrans,imaget,vartyp,cpupar,recl
;
 npar = n_params()
 if npar eq 0 then begin
    print,'DTRANS,IMAGET,vartyp,cpupar,recl'
    retall
 endif  ; npar
 parcheck,npar,[1,2,3,4],'DTRANS'
;
 platform,dummy,copymes=copymes,sysos=sys
;
; test input file 
;
 decompose,imaget,disk,uic,name,ext,vers
;
 infile = imaget
 if (strtrim(copymes.present,2) eq 'supersede') then   $
    outfile = disk + uic + name + ext + ';0' else   $
    outfile = disk + uic + name + ext + '.new'
;
 openr,/get_lun,unit1,infile,/block
 stat = fstat(unit1)
 tbyt = stat.size
;
; acquire input conversion parameters
;
; get vartyp
;
 select:
;
 if npar lt 2 then begin
   ;
    select_type:
   ;
    print,' '
    print,'Select Output Data Type'
    print,' '
    print,'  Byte                 1'
    print,'  Integer              2'
    print,'  Long Integer         3'
    print,'  Floating Point       4'
    print,'  Double Precision     5'
    print,' '
    vartyp = ''
    read,'  Data Type? ',vartyp
 endif  ; npar lt 2
;
 case vartyp of
    '1': begin
         vbyt = 1L
         vstr = 'Byte - (1 byte/element)'
       endcase  ; '1'
    '2': begin
         vbyt = 2L
         vstr = 'Integer - (2 bytes/element)'
       endcase  ; '2'
    '3': begin
         vbyt = 4L
         vstr = 'Long Integer - (4 bytes/element)'
       endcase  ; '3'
    '4': begin
         vbyt = 4L
         vstr = 'Floating Point - (4 bytes/element)'
       endcase  ; '4'
    '5': begin
         vbyt = 8L
         vstr = 'Double Precision - (8 bytes/element)'
       endcase  ; '5'
    else: goto,select_type
 endcase  ; vartyp
;
 vartyp = fix(vartyp)
;
; check tbyt mod vbyt to trap errors
;
 if vartyp ne 1 then begin
    tvflg = ((tbyt mod vbyt) gt 0)
    if tvflg then begin
       print,' '
       print,'WARNING: Total number of input bytes = ' + strtrim(tbyt,2)
       print,'  Not compatible with selected data type'
       print,'  ' + vstr
       print,' '
       print,'  Files must be transferred via ftp - binary mode'
       print,'  ASCII mode transfers add extra bytes'
      ;
       get_opt1:
      ;
       print,' '
       print,'Options available: '
       print,' '
       print,'  Select New Data Type     1'
       print,'  Abort Conversion         2'
       print,' '
       opt = ''
       read,'Option? ',opt
       case opt of
          '1': goto,select_type
          '2': begin
                  free_lun,unit1
                  retall
               endcase  ; '2'
          else: goto,get_opt1
      endcase  ; opt
    endif  ; tvflg
 endif  ; vartyp ne 1
;
; get cpupar
;
 conmenu,cpupar,cpustr
;
; get record length, user defined for vms systems, 
; selected by program on other operating systems 
;
 if (sys eq 'vms') then begin
   ;
   ; VMS section
   ;
    if (npar lt 4) then begin
      ;
       select_recl:
      ;
       recl = ''
       print,' '
       print,'Total number of input bytes = ' + strtrim(tbyt,2)
       print,' '
       print,'Select Desired Output Record Length in Bytes'
       print,' '
       read,'  Record Length? ',recl
       recl = long(recl)
    endif  ; npar lt 4
   ;
   ; check tbyt mod recl to trap errors
   ;
    trflg = ((tbyt mod recl) gt 0)
    if trflg then begin
       print,' '
       print,'WARNING: Total number of input bytes = ' + strtrim(tbyt,2)
       print,'  Not compatible with selected record length'
       print,'  Record Length ' + strtrim(recl,2) + ' bytes'
       print,' '
       print,'  Files must be transferred via ftp - binary mode'
       print,'  ASCII mode transfers add extra bytes'
      ;
       get_opt2:
      ;
       print,' '
       print,'Options Available:'
       print,' '
       print,'  Select New Record Length     1'
       print,'  Abort Conversion             2'
       print,' '
       opt = ''
       read,'Option? ',opt
       case opt of
         '1': goto,select_recl
         '2': begin
                 free_lun,unit1
                 retall
              endcase  ; '2'
         else: goto,get_opt2
       endcase  ; opt
    endif  ; trflg
   ;
   ; check recl mod vbyt to trap errors
   ;
    rvflg = ((recl mod vbyt) gt 0)
    if rvflg then begin
       print,' '
       print,'WARNING: Selected data type - ' + vstr
       print,'  Not compatible with selected record length'
       print,'  Record Length ' + strtrim(recl,2) + ' bytes'
      ;
       get_opt3:
      ;
       print,' '
       print,'Options Available:'
       print,' '
       print,'  Select New Parameters        1'
       print,'  Abort Conversion             2'
       print,' '
       opt = ''
       read,'Option? ',opt
       case opt of
         '1': goto,select
         '2': begin
                 free_lun,unit1
                 retall
              endcase  ; '2'
         else: goto,get_opt3
       endcase  ; opt
    endif  ; rvflg
   ;
 endif else begin       ; sys ne 'vms' (not transfer to VAX)
   ;
   ; UNIX section
   ; 
   ; obtain a list of factors of the number of data elements
   ; for use in calculating an appropriate holding array size
   ;
    nele = tbyt/vbyt
   ;
    if (nele gt 32767L) then factors = lindgen(long(sqrt(nele))-1L)+2L else $
       factors = lindgen(long(nele)-1L)+2L
   ;
    indf = where(((nele mod factors) eq 0L),nfact)
   ;
    if (nfact eq 0) then begin
       if (nele gt 32767L) then begin
          print,'WARNING: Total number of data elements is a prime number.'
          print,'May not have enough memory to make array.'
       endif  ; nele gt 32767L
       factors = [nele]
    endif else begin
       factors = factors(indf)
    endelse  ; nfact eq 0
   ;
   ; select the recl closest to 2048 bytes
   ;
    recl = $
      factors(where(abs(factors*vbyt - 2048) eq min(abs(factors*vbyt - 2048))))
    recl = recl(0)*vbyt
   ;
 endelse  ; sys
;
; finished acquiring parameters
;
 print,' '
 print,' '
 print,'Converting ' + infile + ' from ' + cpustr + ' format.'
 print,' '
 print,'Total number of input bytes is ' + strtrim(tbyt,2)
 print,' '
 print,'Data type selected is ' + vstr
 print,' '
 print,'Total number of data elements is ' + strtrim(tbyt/vbyt,2)
 print,' '
 if (sys eq 'vms') then begin
    print,'Record length selected is ' + strtrim(recl,2) + ' bytes'
    print,' '
 endif  ; sys eq 'vms'
 print,' '
 if (strtrim(copymes.present,2) eq 'supersede') then   $
    print,'Converted file will be named '+outfile+' (highest version number)' $
 else print,'Converted file will be named ' + outfile
 print,' '
;
; begin  conversion
;

 if (recl gt 32767) then openw,/get_lun,unit2,outfile,recl,/block else  $
    openw,/get_lun,unit2,outfile,recl
;
 nrec = tbyt/recl
;
; define input/output associated variables, and holding array
;
 irec = assoc(unit1,bytarr(recl))
 orec = assoc(unit2,bytarr(recl))
;
 var = bytarr(recl)
;
 print,' '
 print,'    Writing to disk...'
;
 for i =0L,nrec-1 do begin
    var = irec(i)
    trans_bytes,var,vartyp,cpupar
    orec(i) = var
 endfor  ; i
;
 print,'                       done.'
 print,' '
;
 free_lun,unit1
 free_lun,unit2
;
 return
 end  ; dtrans