      SUBROUTINE INITSIZ(IIN1,N1,M1,K1)

      USE WINTERACTER
      USE BLK1MOD
      USE BLK2MOD
      USE BLKMAP
      
      INCLUDE 'D.INC'
      
      IF(K1 .EQ. 0) THEN
        MAXPL=200000
        MAXP=200000
        MAXE=120000
        MAXSTO=2
        MAXLIN=3000
        MAXECON=60
        MAXECON1=30
        MAXLN=20
        MAELN=300
        RETURN
      ENDIF
      IMIDS=0
      IF(IIN1 .EQ. 10.  .AND. IGFG .EQ. 0  .AND.  ITRIAN .EQ. 0) THEN
        CALL RDRM1(IIN1,N1,M1,IMIDS)
      ENDIF 
      IF(ITRIAN .EQ. 0) NMIDS=1
       iqsw(1)=1
      iqsw(2)=0
      CALL WMenuSetState(ID_ITYPN,ItemChecked,1)
      
      IF(N1 .GT. MAXP  .OR.  M1 .GT. MAXE  .AND. IMIDS .EQ. 0) then

        CALL WMessageBox(YesNo, QuestionIcon, 1,'Do you wish to add 20,000 nodes and elements to the limit (YES) or reset sizes (NO)','LIMITS EXCEEDED') 

        IF (WInfoDialog(4) .ne. 2) then     
! yes
          MAXP=N1+20000
          MAXE=M1+20000
        ELSE
          CALL RESETSIZ
        ENDIF
      ELSEIF((N1 .GT. MAXP/3  .OR.  M1 .GT. MAXE)  .AND. IMIDS .EQ. 1) then  
        CALL WMessageBox(YesNo, QuestionIcon, 1,'This is a large unfilled network, do you wish to reset sizes?','LIMITS EXCEEDED') 
        IF (WInfoDialog(4) .ne. 2) then     
! yes
          CALL RESETSIZ
        ENDIF
          
      endif
      
      ALLOCATE (CORD(MAXP,2),XUSR(MAXP),YUSR(MAXP),XC(MAXE),YC(MAXE)&
                  ,NOP(MAXE,8),IMAT(MAXE),THTA(MAXE),IMATL(MAXE),CORDSN(MAXP,2)&
                  ,WD(MAXP) ,WD1(MAXP),INSKP(MAXP), IESKP(MAXE),NCORN(MAXE)&
                  ,WIDTH(MAXP), SS1(MAXP), SS2(MAXP), WIDS(MAXP)&
                   ,IJUN(MAXP),INEW(MAXP),IEM(MAXE),LINTYP(MAXLIN),NEFLAG(MAXP),NEF(MAXP,3),LAY(0:MAXP+1),WTLAY(0:MAXP+1,9)&
                   ,WIDBS(MAXP),SSO(MAXP),NODDEL(MAXP),IELDEL(MAXE)&
                   ,NOPSV(MAXE,8),nefsv(MAXP,3),IMATSV(MAXE),LOCK(MAXP),BS1(MAXP),EDIF(0:MAXP),IGRPSER(MAXE),IOD(MAXP))
                   
      IJUN=0 
      lay=0            
      IGRPSER=1
                   
      ALLOCATE (NRIVCR1(MAXP),WTRIVCR1(MAXP),NRIVCR2(MAXP),WTRIVCR2(MAXP))
      
      
   	  ALLOCATE  (xusrsto(MAXP,MAXSTO),yusrsto(MAXP,MAXSTO),wdsto(MAXP,MAXSTO),&
        WIDTHsto(MAXP,MAXSTO), SS1sto(MAXP,MAXSTO), SS2sto(MAXP,MAXSTO), WIDSsto(MAXP,MAXSTO)&
       ,WIDBSsto(MAXP,MAXSTO),SSOsto(MAXP,MAXSTO),bs1sto(MAXP,MAXSTO)&
	   ,nopsto(MAXE,8,MAXSTO),imatsto(MAXE,MAXSTO),thtasto(MAXE,MAXSTO))
	   
      ALLOCATE	    (ICCLNSTO(50,350,MAXSTO)&
	   ,NPSTO(MAXSTO),NESTO(MAXSTO),NLSTSTO(MAXSTO),NCLMSTO(MAXSTO))
	   
      ALLOCATE (ILISTSTO(MAXLN,MAELN,MAXSTO),LLISTSTO(MAXLN,MAXSTO))

	   
	  ALLOCATE (MLIST(MAXE),ENXT(MAXE),NDELM(MAXP),LIST(MAXP)       &
                    ,NINC(MAXP),NELIM(MAXE))  
                    
      ALLOCATE (ICON(MAXE,MAXECON))
      
      ALLOCATE (NECON(MAXP,MAXECON)) 
      
      ALLOCATE (MSN(MAXP),ICN(MAXP)) 
      
      ALLOCATE (ILIST(MAXLN,MAXE),LLIST(MAXLN))

      RETURN
      END
      
      SUBROUTINE RESETSIZ
      
      USE WINTERACTER
      USE BLK1MOD
  	  USE BLKMAP
      include 'd.inc'
                                                 
!
! Declare window-type and message variables
!
      TYPE(WIN_STYLE)    :: WINDOW

      TYPE(WIN_MESSAGE) :: MESSAGE

      integer  :: NTYP,NLOCC


      call wdialogload(IDD_MLIMITS)
      ierr=infoerror(1)

      CALL WDialogSelect(IDD_MLIMITS)
      ierr=infoerror(1)

      CALL WDialogPutINTEGER(IDF_INTEGER1,MAXP)
      CALL WDialogPutINTEGER(IDF_INTEGER2,MAXE)
      CALL WDialogPutINTEGER(IDF_INTEGER3,MAXPL)
      
	  CALL WDialogShow(-1,-1,0,Modal)
      ierr=infoerror(1)

      do
!
          IF (WInfoDialog(ExitButton) .EQ. IDOK) THEN

           CALL WDialogGetINTEGER(IDF_INTEGER1,MAXP)
           CALL WDialogGetINTEGER(IDF_INTEGER2,MAXE)
           CALL WDialogGetINTEGER(IDF_INTEGER3,MAXPL)

            GO TO 100
		  ENDIF

      enddo

  100 CONTINUE
      return
      end
      
      SUBROUTINE SETGFGTRIAN(I1,I2,N2,M2)
      USE BLK1MOD
! Define a common block with file names etc

      INCLUDE 'BFILES.I90'
      CHARACTER (LEN=255)  :: FNAMTMP
      IGFG=I1
      ITRIAN=I2
      IF(ITRIAN .EQ. 1) THEN
        READ(10,*) M2
        REWIND (10)
        itunit=14
        FNAMTMP=FNAMKEP
  		DO L=255,1,-1
		  IF(FNAMTMP(L:L) .EQ. '.') THEN
		    FNAMTMP(L+1:L+4)='node'
		    OPEN(ITUNIT,FILE=FNAMTMP,STATUS='OLD',ACTION='READ')
            READ(ITUNIT,*) N2
            CLOSE(ITUNIT)
            RETURN
          ENDIF
        ENDDO
      ENDIF  
      RETURN
      END