      program main
c
*** readrsig: reading rsm-sigma files and write to GrADS format ***
*** developed by kei yoshimura
c
** need to edit by yourself (from here) **
      integer levs_
      parameter(levs_=28)
      integer igrd_
      parameter(igrd_=128)
      integer jgrd_
      parameter(jgrd_=85)
** need to edit by yourself (to here) **
      integer igrd1_
      parameter(igrd1_=igrd_+1)
      integer jgrd1_
      parameter(jgrd1_=jgrd_+1)
      integer iwav1_
      parameter(iwav1_=(igrd_-12)/3*2+1)
      integer jwav1_
      parameter(jwav1_=(jgrd_-12)/3*2+1)
      integer lnwav_
      parameter(lnwav_=((igrd_-12)/3*2+1)*((jgrd_-12)/3*2+1))
      integer lngrd_
      parameter(lngrd_=(igrd_+1)*(jgrd_+1))
      integer ntrac_
      parameter(ntrac_=1)
      integer ncldg_
      parameter(ncldg_=0)
      integer ntotal_
      parameter(ntotal_=ntrac_+ncldg_)
      parameter(n=100,km=levs_)
c
      common/regbiti/ lab(4), ifin
      common/regbit/ delx, dely, rnnp1max,
     1   snnp1(lnwav_),rnnp1(lnwav_),epsx(lnwav_),epsy(lnwav_)
      common/regbiti/ icen,igen,icen2,ienst,iensi
      common/regbit/ runid,usrid
      character*8 lab
c
      real rproj,rtruth,rorient,rdelx,rdely,rcenlat,rcenlon,
     1     rlftgrd,rbtmgrd,cproj,ctruth,corient,cdelx,cdely,
     2     ccenlat,ccenlon,clftgrd,cbtmgrd,rlat1,rlon1,rlat2,rlon2
      common /comrloc/ 
     1 rproj,rtruth,rorient,rdelx,rdely,rcenlat,rcenlon,rlftgrd,rbtmgrd
     2,cproj,ctruth,corient,cdelx,cdely,ccenlat,ccenlon,clftgrd,cbtmgrd
     3,rlat1,rlon1,rlat2,rlon2
      parameter(levmax=100)
      dimension idate(4),si(km+1),sl(km),
     1          flat(lngrd_), flon(lngrd_),
     1          fm2(lngrd_), fm2x(lngrd_), fm2y(lngrd_)
      dimension q(lngrd_),te(lngrd_,km),
     1          uu(lngrd_,km),vv(lngrd_,km),rq(lngrd_,km*ntotal_)
      dimension gz(lngrd_)
      dimension dummy(2*levmax),ext(512-(6+2*levmax))
      character infile*80, outfile*80
c
      real*4 gzo(lngrd_),qo(lngrd_)
      real*4 teo(lngrd_,km),uuo(lngrd_,km),vvo(lngrd_,km)
      real*4 rqo(lngrd_,km*ntotal_)
c
      if (iargc().ne.2) then
        print*,'Usage: readrsig infile outfile'
        stop
      endif
c
      call getarg(1,infile)
      call getarg(2,outfile)
c
      open(n,file=infile,form='unformatted')
      open(1,file=outfile,form='unformatted',access='direct',
     $     status='new',recl=4*lngrd_)
      read(n) lab
      print*,lab
c
      read(n) fhour,idate,(si(k),k=1,km+1),(sl(k),k=1,km)
     1         ,(dummy(i),i=1,2*levmax+1-km-1-km),ext
      print*, fhour,idate,(si(k),k=1,km+1),(sl(k),k=1,km)
     1         ,(dummy(i),i=1,2*levmax+1-km-1-km),ext
c
c
      read(n) (gz(i),i=1,lngrd_)
      call maxmin(gz,lngrd_,1,1,1,'gz')
      do i=1,lngrd_
        gzo(i)=gz(i)
      enddo
      write(1,rec=1) gzo
c
      read(n) (q (i),i=1,lngrd_)
      call maxmin(q,lngrd_,1,1,1,'q')
      do i=1,lngrd_
        qo(i)=q(i)
      enddo
      write(1,rec=2) qo
c
      do 300 k=1,km
      read(n) (te(i,k),i=1,lngrd_)
      do i=1,lngrd_
        teo(i,k)=te(i,k)
      enddo
      write(1,rec=2+k) (teo(i,k),i=1,lngrd_)
300   continue
      call maxmin(te,lngrd_,km,1,km,'te')
c
      do 310 k=1,km
      read(n) (uu(i,k),i=1,lngrd_)
      read(n) (vv(i,k),i=1,lngrd_)
      do i=1,lngrd_
        uuo(i,k)=uu(i,k)
        vvo(i,k)=vv(i,k)
      enddo
310   continue
      do k=1,km
        write(1,rec=2+km+k) (uuo(i,k),i=1,lngrd_)
      enddo
      do k=1,km
        write(1,rec=2+2*km+k) (vvo(i,k),i=1,lngrd_)
      enddo
      call maxmin(uu,lngrd_,km,1,km,'uu')
      call maxmin(vv,lngrd_,km,1,km,'vv')
c
      do 320 k=1,km*ntotal_
      read(n) (rq(i,k),i=1,lngrd_)
      do i=1,lngrd_
        rqo(i,k)=rq(i,k)
      enddo
      write(1,rec=2+3*km+k) (rqo(i,k),i=1,lngrd_)
320   continue
      call maxmin(rq,lngrd_,km*ntotal_,1,km*ntotal_,'rq')
c
      read(n) (fm2 (i),i=1,lngrd_)
      read(n) (fm2x(i),i=1,lngrd_)
      read(n) (fm2y(i),i=1,lngrd_)
      read(n) (flat(i),i=1,lngrd_)
      read(n) (flon(i),i=1,lngrd_)
c
      close(n)
c
      return
      end
c
      subroutine maxmin(a,len,k,k1,k2,ch)
c
      dimension a(len,k)
      character ch*(*)
c
      do j=k1,k2
      aamax = a(1,j)
      aamin = a(1,j)
      do m=1,len
      aamax = max( aamax, a(m,j) )
      aamin = min( aamin, a(m,j) )
      enddo
      print   *,ch,' has max=',aamax,' min=',aamin,' at k=',j
      enddo
      return
      end
c
