      SUBROUTINE CONVERT
      use winteracter
      USE BLK1
      USE BLKMAT
      USE BLKRM10
      integer inrm1,k
      CHARACTER*8 LABELF(28),labelin,ID
      character*72 namin,DLIN
      CHARACTER(LEN=255)  :: filter
      

!                                       Process standard RM2 input file to TAB format
      INTEGER IMTS(12)
      DATA IMTS/0,31,59,90,120,151,181,212,243,273,304,334/
      DATA LABELF/ 'INBNGEO ','INBNRST ','INRM1   ','INELEV  ','INHYD   ','INELTFL ','OUTFIL  ','OUTBNRMA','OUTBNRST','TIMFIL  ','OUTSMS  ','OUTSPL  ','OUTCON  ','INCROS  ','INSRCORD','INSTRESS','INBNSTRS','INWGT   ','INBNWGT ','OUTWGT  ','OUTBNWGT','OUTBNSTR','WEIRDATA','AWINDIN ','BWINDIN ','OUTFLOW ','INHARM  ','OUTBN3GE'/
!                                       First get a file
      filter="r10 file *.r10|*.R10|"
      CALL WSelectFile(FILTER,PromptOn+DirChange,NAMEFL,'Load R10 File')

	  IF (WInfoDialog(ExitButtonCommon).EQ.CommonOpen) THEN
		  go to 100
	  else
        RETURN
      endif

100   CONTINUE
      DO J=1,28
        DO K=1,72
          FNAME(J)(K:k)=' '
        ENDDO
      ENDDO
      INRM2=7
      ORT=0.
      ITYP=0
      NMAT=0
      DELTM=0.
      DELT=0.
      DATENDST='                '
      JT=0
      KH=0
      KQ=0
      KE=0
      KFC=0
      IHLIN=0
      HLIN=0.
      HCKLIN=0
      HCKLIN1=0
      NHLIN=0
      IQLIN=0
      QLIN=0.
      QDLIN=0.
      QCKLIN=0.
      NQLIN=0
      IELIN=0
      ELIN=0.
      ECKLIN=0.
      ECKLIN1=0
      NELIN=0
      IENDGEO=0
      NFCLIN=0
      IFCLIN=0
      IFCLIN1=0
    
      FCLIN=0
      FCLIN1=0
      FCLIN2=0
      FCLIN3=0
      FCLIN4=0
      FCLIN5=0
      IPIV=0
      ICTOTC=0
      IETP=0
      JET=0
      MANSLOP=0.
      MANMIN=0.
      OPEN(INRM2,FILE=NAMEFL,FORM='FORMATTED')

!                                       Now read files
      DO J=1,28
        IF(LABELIN(1:6) .EQ. 'ENDFIL') THEN
          NFIL=J-1     
          EXIT
        ENDIF
        READ(INRM2,'(A8,A72)') LABELIN,NAMIN
        DO K=1,27
          IF(LABELIN .EQ. LABELF(K)) THEN
            FNAME(K)=NAMIN
            EXIT
          ENDIF
        ENDDO
      ENDDO
      
!                                       Next start on controls       

200   CONTINUE
      CALL GINPT(INRM2,ID,DLIN)
      IF(ID(1:8) .EQ. 'ENDLIMIT') THEN
        GO TO 250
      ELSEIF(ID(1:2) .EQ. 'TI') THEN
        TITLEHEAD=DLIN
        GO TO 250
      ELSEIF(ID(1:8) .EQ. 'MAXFRONT') THEN
        READ(DLIN,'(I8)') LIMITVAL(1)
      ELSEIF(ID(1:8) .EQ. 'MAXCROSS') THEN
        READ(DLIN,'(I8)') LIMITVAL(5)
      ELSEIF(ID(1:8) .EQ. 'MAXCPTS ') THEN
        READ(DLIN,'(I8)') LIMITVAL(6)
      ELSEIF(ID(1:8) .EQ. 'BUFFSIZ ') THEN
        READ(DLIN,'(I8)') LIMITVAL(2)
      ELSEIF(ID(1:8) .EQ. 'BUFFSIZL') THEN
        READ(DLIN,'(I16)') LIMITVAL(2)

      ELSEIF(ID(1:8) .EQ. 'MAXQPTS ') THEN
        READ(DLIN,'(I8)') LIMITVAL(10)
      ELSEIF(ID(1:8) .EQ. 'MAXQINPT') THEN
        READ(DLIN,'(I8)') LIMITVAL(7)
      ELSEIF(ID(1:8) .EQ. 'MAXHPTS ') THEN
        READ(DLIN,'(I8)') LIMITVAL(11)
      ELSEIF(ID(1:8) .EQ. 'MAXHINPT') THEN
        READ(DLIN,'(I8)') LIMITVAL(8)
      ELSEIF(ID(1:8) .EQ. 'MAXELPTS') THEN
        READ(DLIN,'(I8)') LIMITVAL(12)
      ELSEIF(ID(1:8) .EQ. 'MAXEINPT') THEN
        READ(DLIN,'(I8)') LIMITVAL(9)
      ELSEIF(ID(1:8) .EQ. 'MAXPBUFR') THEN
        READ(DLIN,'(I8)') LIMITVAL(4)
      ELSEIF(ID(1:8) .EQ. 'MAXSTEPS') THEN
        READ(DLIN,'(I8)') LIMITVAL(3)
      ENDIF
      GO TO 200
250   CONTINUE  
      CALL GINPT(INRM2,ID,DLIN)
      IF(ID(1:2) .EQ. 'TI') THEN
        TT=0
        TITLEHEAD=DLIN

      ELSEIF(ID(1:2) .EQ. 'C0') THEN
        DO J=25,32
          IF(DLIN(J:J) .EQ. '/') THEN
            DLIN(J:J)=','
            READ(DLIN(1:24),'(3I8)') IOPTZD,IDNOPT,IYRR
            READ(DLIN(25:32),*) ITMD,ITMM
            DAYOFY=IMTS(ITMM)+ITMD
            IF(MOD(IYRR,4) .EQ. 0   .AND.  ITMM .GT. 2) DAYOFY=DAYOFY+1
            READ(DLIN(33:72),'(F8.0,I8,2F8.0,I8)')TET,IEDSW,TBFACT,TBMIN
     +     ,IPROJ
            GO TO 60
          ENDIF
        ENDDO
      
        READ(DLIN,5061)IOPTZD,IDNOPT,IYRR,DAYOFY,TET,IEDSW,TBFACT,TBMIN
     +  ,IPROJ

        write(*,*) 'read c0'
CIPK FEB04 SAVE TIMES IN CASE IOV ACTIVE
   60   CONTINUE 
	  IYKK=IYRR
        IDTM=DAYOFY
        TTEM=TET
        GO TO 250
cipk end changes apr 96
      ELSEIF(ID(1:2) .EQ. 'C1') THEN
CIPK SEP05
        ISAVITR=0
cipk FEB15  allow zero bed option to be based on a level      
        READ(DLIN,5021) 
     +  NDP,IGRV,IZB,IPASS1,IPASS2,IPASS3,IZERS,ISAVITR,zerbed
 5021   FORMAT(8I8,F8.0)      
cipk mar15 add switch for density distribution
        read(dlinextra,'(i8)') ivertden
	
        GO TO 250        
        
      ELSEIF(ID(1:2) .EQ. 'C2') THEN
CIPK DEC07 REPLACE OMEGA BY OMEGA1      
        READ(DLIN,5020) OMEGA1,ELEV,XSCALE,YSCALE,ZSCALE
        write(*,*) 'read c2'
        GO TO 250

      ELSEIF(ID(1:2) .EQ. 'C3') THEN

        READ(DLIN,5020) CMIN,CPR,UNOM,UDIR,HMNN,DSET,DSETD,IFXBC,IEQSWT
CIPK NOV97      READ(LIN,7000) ID,DLIN
        GO TO 250

      ELSEIF(ID(1:2) .EQ. 'C4') THEN

cipk sep96 add to 3 lines below for ocean exchange percentantage and mixing
        READ(DLIN,5020) SALI,TEMPI,SEDI,UINP,VINP,prcnt,DMIX
        write(*,*) 'read c4'
        GO TO 250
      
      ELSEIF(ID(1:2) .EQ. 'C5') THEN

        READ(DLIN,5011) NITI,NITN,TSTART,NCYC,IPRT,NPRTI
     +  ,NPRTF,IRSAV,IDSWT
        write(*,*) 'read c5'
C
      

        GO TO 250

      ELSEIF(ID(1:2) .EQ. 'C6') THEN
      
CIPK APR11
        READ(DLIN,'(8I8)') IOUTFREQ,IOUTRST,IREWOUT,ID1DND,ICPU,IOOC
     +   ,IREWMES,IDEBUG
CIPK JAN09
 
        GO TO 250
        
CIPK AUG11  add logic to allow exclusion whenID1DND = 3 or 4
   16 ELSEIF(ID(1:3) .EQ. 'EXM') THEN

	  READ(DLIN,'(9I8)') (MDLIST(I),I=1,9)
        DO I=1,9
	    IF(MDLIST(I) .GT. 0) THEN
	      IDEXCL(MDLIST(I))=1
	    ENDIF
        ENDDO
        GO TO 250
      
      ELSEIF(ID(1:3) .EQ. 'INI') THEN
      
        READ(DLIN,'(F8.0)') ELEV1
 
        GO TO 250

      ELSEIF(ID(1:3) .EQ. 'VLA') THEN
      
        READ(DLIN,'(F8.0)') YLATREF
        IVL=1
 
        GO TO 250

      ELSEIF(ID(1:4) .EQ. 'RMA2') THEN
        IRMA2=1
        GO TO 250

      ELSEIF(ID(1:2) .EQ. 'CV') THEN
        READ(DLIN,'(6F8.0,i8,f8.0)') (CONV(J),J=1,6),idrpt,drfact
        GO TO 250

      ELSEIF(ID(1:3) .EQ. 'IOV') THEN
        IOV=1


cipk feb97 add line to select optimisation
        GO TO 250

      ELSEIF(ID(1:3) .EQ. 'IOP') THEN
        READ(DLIN,'(F8.2)') W2FACT
        IOPTIM=0

        GO TO 250

      ELSEIF(ID(1:5) .EQ. 'DFACT') THEN
        READ(DLIN,'(3F8.2,I8)') DFACTIN1,DFACTIN2,DFACTIN3,INEWDEN

        
        GO TO 250

      ELSEIF(ID(1:4) .EQ. 'ICE1') THEN
        READ(DLIN,'(8F8.0,i8)')ROW,CHEAT,TMED,HTR,XLAT,ROSN,ROIC,TICE,
     +	  ICESW
        GO TO 250

      ELSEIF(ID(1:4) .EQ. 'ICE2') THEN
        READ(DLIN,'(5F8.0)') CAL1,CAL2,CAL3,CAL4,VTR
        call ginpt(lin,id,dlin)
        GO TO 250
      
      ELSEIF(ID(1:3) .EQ. 'COL') THEN
        READ(DLIN,'(F8.2)') TRANSIT
        ITRANSIT=1
cipk revised unit
        GO TO 250
      
      ELSEIF(ID(1:3) .EQ. 'TST') THEN
        READ(DLIN,'(I8,2F8.0)') NODETR,TRELEV,TRFACT


        GO TO 250

      ELSEIF(ID(1:3) .EQ. 'PWR') THEN
        READ(DLIN,'(2I8,2F8.0,I8,3F8.0,I8)') NINCC,NOUTCC(NINCC),
     +      ADDSAL(NINCC),ADDTMP(NINCC,1),IPWRSW(NINCC),ADDTMP(NINCC,3)
     +     ,ADDSED(NINCC),ADDMAX(NINCC),NADTYP(NINCC)
        GO TO 250

      ELSEIF(ID(1:3) .EQ. 'EQT') THEN
        READ(DLIN,'(3F8.0)') EQTEMP,XKRAT,EXTING
        GO TO 250

      ELSEIF(ID(1:6) .EQ. 'BEDMIN') THEN
        READ(DLIN,'(F8.0)') BEDMIN
        GO TO 250

      ELSEIF(ID(1:6) .EQ. 'BEDMAX') THEN
        READ(DLIN,'(F8.0)') BEDMAX
        GO TO 250

      ELSEIF(ID(1:8) .EQ. 'BEDSHIFT') THEN
        READ(DLIN,'(F8.0)') BEDSHIFT
        GO TO 250

      ELSEIF(ID(1:8) .EQ. 'FLOWSCAL') THEN        
        READ(DLIN,'(F8.0)') FLOWSCAL
        GO TO 250

CIPK AUG10   ADD SETTLING FOR SEDIMENT 
      ELSEIF(ID(1:4) .EQ. 'VSET') THEN
        READ(DLIN,'(3F8.0)') VSET,VDECAY,PRECIP      
        GO TO 250

CIPK AUG10   ADD BED HEAT TRANSFER 
      ELSEIF(ID(1:4) .EQ. 'BDHT') THEN
        IBEDHT=1
        READ(DLIN,'(5F8.0)')TGL,DLINER,XKL,DDRY,XKG
        GO TO 250

      ELSEIF(ID(1:3) .EQ. 'CHI') THEN
        READ(DLIN,'(F8.0)') CHI1
        GO TO 250

      ELSEIF(ID(1:3) .EQ. 'SPC') THEN
        READ(DLIN,'(9A8)') (ASPLPT(J),J= 1,8)
        ITOTC=0
        DO J=1,8
          IF(ASPLPT(J) .EQ. 'VELOCITY') THEN
            ICOMP(ITOTC+1)=-1
            ITOTC=ITOTC+1
          ELSEIF(ASPLPT(J) .EQ. 'DEPTH   ') THEN
            ICOMP(ITOTC+1)=3
            ITOTC=ITOTC+1
          ELSEIF(ASPLPT(J) .EQ. 'WS-ELEV ') THEN
            ICOMP(ITOTC+1)=7
            ITOTC=ITOTC+1
          ELSEIF(ASPLPT(J) .EQ. 'SALINITY') THEN
            ICOMP(ITOTC+1)=4
            ITOTC=ITOTC+1
          ELSEIF(ASPLPT(J) .EQ. 'TEMPERAT') THEN
            ICOMP(ITOTC+1)=5
            ITOTC=ITOTC+1
          ELSEIF(ASPLPT(J) .EQ. 'SEDIMENT') THEN
            ICOMP(ITOTC+1)=6
            ITOTC=ITOTC+1
          ENDIF
        ENDDO
        GO TO 250
        
      ELSEIF(ID(1:3) .EQ. 'SP ') THEN
        READ(DLIN,5010) (NSPLPT(J),J= 1,9)
        READ(DLINEXTRA,'(I8)') NSPLPT(10)
        DO J=10,1,-1
          IF(NSPLPT(J) .GT. 0) THEN
            JSPLPT=J
            GO TO 22
          ENDIF
        ENDDO
   22   CONTINUE
        GO TO 250

      ELSEIF(ID(1:4) .EQ. 'CSPC') THEN
        READ(DLIN,'(9A8)') (ACSPLPT(J),J= 1,8)
        ICTOTC=0
        DO J=1,8
          IF(ACSPLPT(J) .EQ. 'FLOW    ') THEN
            ICCOMP(ICTOTC+1)=1
            ICTOTC=ICTOTC+1
          ELSEIF(ACSPLPT(J) .EQ. 'DEPTH   ') THEN
            ICCOMP(ICTOTC+1)=2
            ICTOTC=ICTOTC+1
          ELSEIF(ACSPLPT(J) .EQ. 'WS-ELEV ') THEN
            ICCOMP(ICTOTC+1)=3
            ICTOTC=ICTOTC+1
          ELSEIF(ACSPLPT(J) .EQ. 'SALINITY') THEN
            ICCOMP(ICTOTC+1)=4
            ICTOTC=ICTOTC+1
          ELSEIF(ACSPLPT(J) .EQ. 'TEMPERAT') THEN
            ICCOMP(ICTOTC+1)=5
            ICTOTC=ICTOTC+1
          ELSEIF(ACSPLPT(J) .EQ. 'SEDIMENT') THEN
            ICCOMP(ICTOTC+1)=6
            ICTOTC=ICTOTC+1
          ENDIF
        ENDDO
        GO TO 250

CIPK AUG11
      ELSEIF(ID(1:4) .EQ. 'CSP ') THEN
        READ(DLIN,5010) (NCSPLPT(J),J= 1,9)
        READ(DLINEXTRA,'(I8)') NCSPLPT(10)
        DO J=10,1,-1
          IF(NCSPLPT(J) .GT. 0) THEN
            JCSPLPT=J
            GO TO 23
          ENDIF
        ENDDO
   23   CONTINUE
        GO TO 250
      ELSEIF(ID(1:3) .EQ. 'ED1') THEN
        READ(DLIN,5030) J,(ORT(J,K),K=1,7)
        write(*,*) 'read ed1'

        IF(NMAT .LT. J) NMAT=J
        GO TO 250
CIPK NOV97      READ(LIN,7000) ID,DLIN
      ELSEIF(ID(1:3) .EQ. 'ED2') THEN
        READ(DLIN,5031) (ORT(J,K),K=8,13)
        write(*,*) 'read ed2'
cipk mar98 
        GO TO 250  
  	ELSEIF(ID(1:3) .EQ. 'ED3') THEN
        if(IEDSW .EQ. 5) THEN
          READ(DLIN,5031) ELDER(J,1),ELDER(J,2)
        ELSE    	    
          READ(DLIN,5032) IT1,TT1,TT2
	    IF(TT1 .GT. 0.) THEN
            TBFACT1(J)=TT1
	    ENDIF
	    IF(TT2 .GT. 0.) THEN
	      TBMIN1(J)=TT2
          ENDIF
        ENDIF
        GO TO 250
cipk mar07 add to test            
C      ELSE
C        GO TO 24
C      ENDIF
        
        
        ENDIF
      ELSEIF(ID(1:8) .EQ. 'ENDGEO  ') THEN
        GO TO 250  
      ELSEIF(ID(1:2) .EQ. 'DT') THEN
        JT=JT+1
        KQ=0
        KH=0
        KE=0
        KFC=0
        
        IF(ID(3:3) .EQ. 'M') THEN
          READ(DLIN(1:16),'(F8.0,I8)') DELTM(JT),IWIND(JT)                
        ELSE
          READ(DLIN(1:16),'(F8.0,I8)') DELT(JT),IWIND(JT)
        ENDIF
        IF(DELTM(JT) .GT. 0  .OR.  DELT(JT) .GT. 0) THEN
          READ(DLIN(17:40),'(2I8,F8.0)') IYRR,DAYOFY,TET
          CALL MAKEDATE(DATENDST(JT),IYRR,DAYOFY,TET)
        ENDIF
        READ(DLIN(41:56),'(F8.0,I8)') URLC(JT), NRITR(JT)
      ELSEIF(ID(1:2) .EQ. 'HC') THEN
        KH=KH+1
        READ(DLIN,'(I8,F8.0,I8)')  IHLIN(KH,JT),HLIN(KH,JT),HCKLIN(KH,JT)
        IF(HCKLIN(KH,JT) .EQ. 2) THEN
          HCKLIN(KH,JT)=0
          HCKLIN1(KH,JT)=1
        ENDIF
        HLIN1(KH,JT)=-9999.
        GO TO 250  
      ELSEIF(ID(1:2) .EQ. 'HI') THEN
        KH=KH+1
        READ(DLIN,'(I8,2F8.0,I8)')  IHLIN(KH,JT),HLIN(KH,JT),HLIN1(KH,JT),HCKLIN(KH,JT)
        GO TO 250  
      ELSEIF(ID(1:2) .EQ. 'QC') THEN
        KQ=KQ+1
        READ(DLIN,'(I8,2F8.0,I8)')  IQLIN(KQ,JT),QLIN(KQ,JT),QDLIN(KQ,JT),QCKLIN(KQ,JT)
        IQLIN1(KQ,JT)=1
        GO TO 250  
      ELSEIF(ID(1:2) .EQ. 'QF') THEN
        KQ=KQ+1
        READ(DLIN,'(I8,2F8.0,I8)')  IQLIN(KQ,JT),QLIN(KQ,JT),QDLIN(KQ,JT),QCKLIN(KQ,JT)
        IQLIN1(KQ,JT)=2
        GO TO 250  
      ELSEIF(ID(1:2) .EQ. 'QG') THEN
        KQ=KQ+1
        READ(DLIN,'(I8,2F8.0,I8)')  IQLIN(KQ,JT),QLIN(KQ,JT),QDLIN(KQ,JT),QCKLIN(KQ,JT)
        IQLIN1(KQ,JT)=1
        GO TO 250  
      ELSEIF(ID(1:2) .EQ. 'QH') THEN
        KQ=KQ+1
        READ(DLIN,'(I8,2F8.0,I8)')  IQLIN(KQ,JT),QLIN(KQ,JT),QDLIN(KQ,JT),QCKLIN(KQ,JT)
        IQLIN1(KQ,JT)=2
        GO TO 250  
      ELSEIF(ID(1:3) .EQ. 'EFE') THEN
        KE=KE+1
        IETP(KE,JT)=1
        READ(DLIN,'(I8,F8.0,2I8)')  IELIN(KE,JT),ELIN(KE,JT),ecklin1(KE,JT),ECKLIN(KE,JT)
        GO TO 250  
      ELSEIF(ID(1:3) .EQ. 'SQC') THEN
        KFC=KFC+1
        READ(DLIN,'(I8,5F8.0)')  ISFLIN(KFC,JT),SFLIN(KFC,JT),SFLIN1(KFC,JT),SFLIN2(KFC,JT),SFLIN3(KFC,JT),SFLIN4(KFC,JT)
        GO TO 250  
      ELSEIF(ID(1:2) .EQ. 'FC') THEN
        KFC=KFC+1
        READ(DLIN,'(2I8,5F8.0,8x,f8.0)')  IFCLIN(KFC,JT),IFCLIN1(KFC,JT),FCLIN(KFC,JT),FCLIN1(KFC,JT),FCLIN2(KFC,JT),FCLIN3(KFC,JT),FCLIN4(KFC,JT),FCLIN5(KFC,JT)
        GO TO 250  
      ELSEIF(ID(1:8) .EQ. 'ENDSTEP') THEN
        NQLIN(JT)=KQ
        NHLIN(JT)=KH
        NELIN(JT)=KE
        NFCLIN(JT)=KFC
          
        GO TO 250
      ELSEIF(ID(1:8) .EQ. 'ENDDATA') THEN
        go to 400
      ELSE
        WRITE(75,*) 'DATA LINE ABOVE NOT CONVERTED'
        IF(IENDGEO .EQ.0) THEN
          IF(ID(1:3) .EQ. 'ED2') THEN
            WRITE(DLIN(1:8),'(I8)'),ITEMP
          ENDIF
          NHOLDC=NHOLDC+1
          HOLDC(NHOLDC)=ID//DLIN
        ENDIF
        GO TO 250
      ENDIF
      GO TO 250  
400   CONTINUE      
      RETURN
    END
    
    SUBROUTINE MAKEDATE(STRING,IYRR,DAYOFY,HOUR)
    INTEGER JDAYC(0:12)
    CHARACTER*16 STRING
    INTEGER IYRR,DAYOFY,JLP,JLPM,MON,IDAY
    REAL HOUR
    DATA JDAYC/0,31,59,90,120,151,181,212,243,273,304,334,365/
    JLP=0
    JLPM=0
    DO J=1,12
      IF(J .EQ. 2 .AND.  MOD(IYRR,4) .EQ. 0) JLP=1
      IF(J .EQ. 3 .AND.  MOD(IYRR,4) .EQ. 0) JLPM=1
      IF(DAYOFY .LE. JDAYC(J)+JLP) THEN
        MON=J
        IDAY=DAYOFY-JDAYC(J-1)-JLPM
        EXIT
      ENDIF
    ENDDO
    WRITE(STRING(1:3),'(I2.2,''/'')') IDAY 
    WRITE(STRING(4:6),'(I2.2,''/'')') MON
    WRITE(STRING(7:11),'(I4,'' '')') IYRR
    IF(HOUR .LT. 10.) THEN
      WRITE(STRING(12:16),'(''0'',F4.2)') HOUR
    ELSE  
      WRITE(STRING(12:16),'(F5.2)') HOUR
    ENDIF
    RETURN
    END