      program cmpx_2_form
c
c This version: contolled by command line arguments only
      implicit logical (q)
      complex z(3000,1600)
      character type*1,tide*4,fni*64,fno*64,str*64,cyl*8,fix*16
     &,         endian*2

      data qrevo/.false./, qcyl/.false./, ncyl/0/, fno/' '/
     &,    nover/-999/, qff,quu,qfu/3*.false./, endian/'^ '/
     &,    ltide/2/

      call start_time_message ('mzuf')
      i=0

 1    i=i+1
      call getarg (i,str)
      if (str(1:1).eq.' ') goto 11

      if (str(1:1).ne.'-') then
         fni=str
      elseif (str(1:2).eq.'-h') then
         print 1001
         stop
 1001 format (/' USAGE:   mzuf.out [options] [-o outputfile ] inputfile'
     &//       ' PURPOSE: OTEQ type complex array copy '
     &/        '          formatted to unformatted'
     &/        '       or unformatted to formatted'
     &//' OPTIONS:'
     &/' -l#s     - character length of tide symbols [default s=2]'
     &/' -r       - reverse latitude order north-south'
     &/' -ch      - rotate cylindrically one half turn'
     &/' -c#n     - rotate cylindrically n towards west'
     &/' -v#n     - output array version number'
     &/' -o file  - output file name'
     &/' -f2u[s]  - formatted to unformatted, [default s=''t'']'
     &/' -u2f[s]  - unformatted to formatted, [default s=''f'']'
     &/'            s - (string) for output, use input file name and'
     &/'                replace tail with s.'
     &/' -BE      - read unformatted data with CONVERT=BIG ENDIAN'
     &//' OBS! No blanks in options except after -o'
     &/)
      elseif (str(1:2).eq.'-l') then
         read (str(3:),*) ltide
      elseif (str(1:2).eq.'-r') then
         qrevo=.true.
      elseif (str(1:2).eq.'-c') then
         qcyl=.true.
         cyl=str(3:)
      elseif (str(1:3).eq.'-BE') then
         endian='% '
      elseif (str(1:2).eq.'-o') then
         if (str(3:3).eq.' ') then
            i=i+1
            call getarg (i,str)
            fno=str
         else
            fno=str(3:)
         endif
      elseif (str(1:2).eq.'-v') then
         read (str(3:),*,end=99) nover
      elseif (str(1:4).eq.'-f2u') then
         qfu=.true.
         quu=.false.
         fix='t'
         lf=1
         if (str(5:5).ne.' ') then
            fix=str(5:)
            lf=index_l_nonblank(fix)
         endif
      elseif (str(1:4).eq.'-u2f') then
         qfu=.false.
         quu=.false.
         fix='f'
         lf=1
         if (str(5:5).ne.' ') then
            fix=str(5:)
            lf=index_l_nonblank(fix)
         endif
      elseif (str(1:4).eq.'-u2u') then
         qfu=.false.
         quu=.true.
         fix='u'
         lf=1
         if (str(5:5).ne.' ') then
            fix=str(5:)
            lf=index_l_nonblank(fix)
         endif
      elseif (str(1:4).eq.'-f2f') then
         qfu=.false.
         qff=.true.
         fix='a'
         lf=1
         if (str(5:5).ne.' ') then
            fix=str(5:)
            lf=index_l_nonblank(fix)
         endif
      endif
         
      goto 1

 11   if (fno(1:1).eq.' ') then
         fno=fni
         i=index(fno,' ')-lf
         fno(i:)=fix(:lf)
      endif
      
      call set_length_tag_tide (ltide)
      
      if (qfu) then
         call openfs ('21 R '//fni)
         call openfs ('31 < '//fno)
         call getsiz_f (21,m,n)
      elseif (quu) then
         call openfs ('21 '//endian//fni)
         call openfs ('31 < '//fno)
         call getsiz (21,m,n)
      elseif (qff) then
         call openfs ('21 R '//fni)
         call openfs ('31 B '//fno)
         call getsiz_f (21,m,n)
      else
         call openfs ('21 '//endian//fni)
         call openfs ('31 B '//fno)
         call getsiz (21,m,n)
      endif

      type='.'
      tide='..'
      if (qfu.or.qff) then
         call getzm_f    (21,z,m,n,type,tide)
         call getver_f   (nver)
         if (nover.ge.0) nver=nover
         call setver     (nver)
      else
         call getzm      (21,z,m,n,type,tide)
         call getver     (nver)
         if (nover.ge.0) nver=nover
         call setver     (nver)
      endif

      if (index('EF ET EN EV EC ET ',tide(1:2)).gt.0) goto 98
      if (nver.eq.-1) goto 98

      if (qrevo) call zrevo (z,m,n,type)
      if (qcyl) then
         if (index('hH',cyl(1:1)).gt.0) then
            ncyl=m/2
         else
            read (cyl,*,end=99) ncyl
         endif
         call zrcyl (z,m,n,ncyl,z(1,800))
      endif
      if (qfu.or.quu) then
         call outzm   (31,z,m,n,type,tide)
      else
         call outzm_f (31,z,m,n,type,tide)
      endif
      stop
 99   print '(a)',' <Main-->T> Numerical argument required'
      stop
 98   print '(2a,i5)',' <Main-->T> Bad input; tide$, ver.# ',tide,nver
      stop
      end

