Viewing contents of file '../idllib/iuedac/iuelib/pro/copuf.pro'
;******************************************************************************
;+
;*NAME:
;
;    	COPUF
;  
;*CLASS:
;
;	Disk File Utility 
;  
;*CATEGORY:
;
;	IUESIPS  NEWSIPS
;  
;*PURPOSE:
;  
;    	Copies unformatted or formatted data from the input file to the 
;       specified output file using IDL read and write commands (i.e., 
;       without spawning to systems commands). If /nocopy is specified, 
;       copuf only returns the full output file name. 
;
;*CALLING SEQUENCE:
;
;    	COPUF,IMAGI,IMAGO,nocopy=nocopy,form=form
;  
;*PARAMETERS:
;  
;    	IMAGI  (REQ)  (I)  (0) (S)
;             	Name of input file (string). If no extension is given, 
;               copuf assumes both an iuesips .dat and a .lab file are to
;               copied.
;
;    	IMAGO   (REQ) (I/O) (0) (S)
;             	Name of output file (string).
;               - For systems with version numbers (e.g., VMS), if 
;               IMAGO is blank (i.e. ' '), or any number, then the output 
;               file will have the same name as the input file with the version 
;               number incremented. 
;               - For systems without version numbers (e.g., unix),
;               and IMAGO is blank or a number, the file is not copied, 
;               but IMAGO is set to IMAGI. 
;
;       NOCOPY  (OPT) (KEY) (0) (S)
;               keyword specifying no file copy should be performed.
;               COPUF will only return the full output file name.
;
;       FORM    (OPT) (KEY) (0) (S)
;               keyword specifying formatted file to be copied (i.e. 
;               an ASCII text file). If not specified, unformatted files 
;               are assumed.
; 
;*EXAMPLES:
;
;    	copuf,'swp25303s.dat','modt.dat'   ; copy SWP 25303 ELBL file into
;    	                            ; MODT.DAT file (note for running
;    	                            ; these renamed output files with other
;    	                            ; RDAF procedures, it may be necessary
;    	                            ; to use the standard naming conventions
;    	                            ; (e.g., LWPnnnnS.DAT or SWPnnnS.DAT).
; 
;    	copuf,'modt.dat',' '       ; (VMS) copy MODT.DAT file
;                                  ; into new file with same name
;                                  ; (unix) nothing is done
;
;       imago = 0
;       copuf,'swp12345.mxlo',imago  ; (unix) no copy is performed, but imago is
;                                    ; set to imagi
;                                    ; (vms) new version is created
;
;       copuf,imagi,imago,/nocopy    ; no copy is performed (on any system)
;                                    ; but imago is defined or expanded to 
;                                    ; full path name
;
;*SYSTEM VARIABLES USED:
;
;       none
;  
;*INTERACTIVE INPUT:
;
;	If IMAGO already exists on a system not supporting version numbers, 
;       then the user is prompted before continuing.
;  
;*SUBROUTINES CALLED:
;  
;    	PARCHECK
;    	DECOMPOSE
;       PLATFORM
;       YESNO
; 
;*FILES USED:
;
;   	IMAGET & OUTPUT FILE (IMAGO)
;  
;*SIDE EFFECTS:
;  
;*RESTRICTIONS:
;
;        - For VMS systems, unformatted files must have fixed-length records.
;        - Formatted files must have less than 5000 lines.
;  
;*NOTES:
;  
;    	 - If no extension is specified with imagi, COPUF assumes both 
;          the .dat and .lab iuesips files are to be copied.
;        - If no extension is specified with imago, the extension(s) for imagi
;          are assumed.
;        - If IMAGO already exists (on systems without version numbers), the
;          user is prompted to continue before the file is overwritten.
;        - If imago is blank or a number, and the system does not allow version
;          numbers, then copuf only creates the output file name. 
;        - Version numbers are left off IMAGO name so on vms systems output 
;          file is always assigned the highest version number.
;        - In programs like BSPOT that are modifying either IUESIPS or
;          NEWSIPS files, the NEWSIPS files may need the /nocopy option 
;          because FITS files can not be updated like iuesips files.
;        - COPUF is compatible with all IUESIPS and NEWSIPS data sets.
; 
;*PROCEDURE:
;  
;       File is read and then written out to disk with new name.
; 	
;*I_HELP nn:
;  
;  
;*MODIFICATION HISTORY:
;
;       Written by R. Thompson 2/95 (based on modlbl and bs_inout)
;       3 Mar 95 RWT add /nocopy option (for defining NEWSIPS file names
;                    without copying file)
;       3 Mar 95 RWT remove version numbers from output file
;       7 Mar 95 RWT add support for formatted files
;       7 Mar 95 RWT increase formatted file limit from 900 to 5,000 lines
;-
;******************************************************************************
 pro copuf,imagi,imago,nocopy=nocopy,form=form
; 
 npar = n_params(0)
 if (npar eq 0) then begin
   print,'COPUF,IMAGI,IMAGO,nocopy=nocopy,form=form'
   retall
 endif  ; npar eq 0
 parcheck,npar,2,'COPUF'
 decompose,imagi,disk,uic,image,ext,vers
 if keyword_set(nocopy) then fcop = 0 else fcop = 1
 if keyword_set(form) then forf = 1 else forf = 0
;
; find input file(s) & determine full path name
;
 if ext eq '' then begin                 ; assume 2 iuesips files
    ext = '.dat'
    ofil = 2
    im  = disk + uic + image + '.lab' + vers     ; find .lab file
    b = findfile(im,count=ct)
    if (ct eq 0) then begin
      print,' file ' + im + ' not found; action returning'
      retall
    endif 
 endif else ofil = 1                     ; iuesips files
;
; look for .dat (or other) file
;
 im  = disk + uic + image + ext + vers
 a = findfile(im,count=ct)
 if (ct ne 0) then im = a(0) else begin
    print,' file ' + im + ' not found; action returning'
    retall
 endelse
;
 decompose,im,disk,uic,image,ext,vers
 platform,dummy,copymes=copymes,sysos=sys
 s = size(imago)
 if (s(1) ne 7) then imago = string(32b)
 imi = imago
;
;  read input file (if a valid output file name is given)
;
 if (imi ne string(32b)) or ( copymes.past eq ' superseded ') then $
 for i=1,ofil do begin
    if (i eq 2) then begin
       im = b(0)
       decompose,im,disk,uic,image,ext,vers
    endif
    if (fcop) then begin
       openr,unr,im,/get_lun                 ; open input file
       if (forf) then begin                  ; read a formatted file 
          a = ''
          nl = 0
          hd = strarr(5000)                     
          while not eof(unr) do begin
               readf,unr,a
               hd(nl) = a
               nl = nl + 1
          endwhile  ; not eof
          hd = hd(0:nl-1)
       endif else begin                      ; read a unformatted file
          fs = fstat(unr)
          tot = fs.size                               ; total bytes in file
          reclen = fs.rec_len                         ; vms record length
          recl = assoc(unr,bytarr(tot))
          lab1 = recl(0)
       endelse
       free_lun,unr
    endif
    ;   
    ; increment version number if imago = ' '
    ;
    if (imi eq string(32b)) then begin
       ;delim = strmid(vers,0,1)
       ;versn = strmid(vers,1,3)                  ; allow numbers to 999
       ;if (fix(versn) ne 0) then vers = delim + strtrim(string(fix(versn)+1),2)
       ;imago = disk + uic + image + ext + vers
       imago = disk + uic + image + ext
    endif                               ; imago is not a blank or unix
    ;
    ; check output file name, add ext if not included
    ; prompt user to continue if file will be overwritten
    ;
    decompose,imago,odisk,ouic,oimage,oext,overs
    if (oext eq '') or (i eq 2) then oext = ext
    ;imago = odisk + ouic + oimage + oext + overs        
    imago = odisk + ouic + oimage + oext 
    if (fcop) then begin
       a = findfile(imago,count=ct)
       if (i eq 1) and (ct gt 0) and (copymes.past ne ' superseded ') then begin
          print,' File '+imago+' will be overwritten'
          ans = ''       
          read,prompt=' Do you wish to continue? (y/n default = y) ',ans
          yesno,ans
          if (not(ans)) then retall
       endif
       if (forf) then begin             ; formatted
          openw,unr,imago,/get_lun
          for n = 0,nl-1 do begin
             a = hd(n)
             printf,unr,a 
          endfor
       endif else begin                 ; unformatted
          if (sys eq 'vms') then openw,unr,imago,reclen,/none,/get_lun $
                            else openw,unr,imago,/get_lun
          recl=assoc(unr,bytarr(tot))
          recl(0) = lab1
       endelse
       free_lun,unr       
    endif
    if (i eq 2) then imago = odisk + ouic + oimage + '.dat'
 ;   print,' Output file name = ',imago
 endfor 
;
; create name if imago still blank
; (& remove extension for iuesips images if imagi had no extension)
;
 if (imago eq string(32b)) then begin      ; just set output file name
    if (ofil eq 2) then imago = disk + uic + image + '.dat' $
    else imago = disk + uic + image + ext 
 endif                                     ; just set output name (no file copy)
;
 return
 end  ; copuf