C==========================================================================
	SUBROUTINE quickPLT(ngcard,icardno,ibit,iword,filename,
	1	nopeak,nodata,everyf,xlow,xhigh,icol,datscl,jcscl,*)
C==========================================================================
C	Plot out NCPP jcpds cards (per page) as stick diagrams
C
C if NODATA is .false. plot a diffractogram
C if NOPEAK is .false. plot a set of observed peaks
C    If EVERYF is .true. superimpose obs and PDF 
C DATSCL is the raw data magnification factor (1=full scale=100%, 2=50%...)
C JCSCL  is the JCPDS magnification factor (1=full scale=100%, 2=50%...)
C==========================================================================
	include 'logic.common'
	CHARACTER*(*) FILENAME
	REAL datscl,jcscl
	INTEGER icol
	logical nopeak,nodata,everyf
	INTEGER*4 NAMREC,FULREC,DIREC,ISEQ,JCARD,ngcard
	BYTE CHFORM(200),CHNAME(205)
	DIMENSION dsp(200),angle(200),xt(2),yt(2)
	INTEGER*2 INT(200)
	INTEGER*2 LCHFORM,LCHNAME
	CHARACTER*1 QM,name*60
	
	CHARACTER str1*8,str2*8,lbl*32
	BYTE ians,hardf

	IF (EVERYF .or. (NODATA .and. NOPEAK)) THEN
	  ncpp = min(1+nphase-icardno,NGCARD)
	ELSE
	  ncpp = min(2+nphase-icardno,NGCARD)
	ENDIF

	HARDF = .FALSE.

	IF (.not. nodata) THEN 
	  Ymax = max(B(1,1),.00001)
	  do ii=2,nxarr
	    ymax = max(ymax,b(ii,1))
	  enddo
D	type *,nxarr
D	type *,xarray(1),xarray(nxarr)
D	type *,Ymax,' Ymax'
	ENDIF

21	IB = ibit 
	iw = iword
	nph = nphase
	icrd = icardno
	IF (HARDF) then
	  CALL OPNHD(lo,li)
	ELSE
	  CALL BGRAF(ICRT,lo,li)
	ENDIF

C set up plot size
	chars = 1.1
	charss = chars/40.
	call pgsch(chars)
C overall plot borders
	YTOP = 0.
	ybot = 3.5*charss
	Xleft = 2.5*charss
	Xright = 0.5*charss
C individual plot borders
	ymart = 0.0*charss
	ymarb = 0.0*charss
C size of plot
	ysize = (1. - ytop - ybot) / (1.*ncpp)
	CALL PGVPORT(Xleft,1.-Xright,yBOT,1.-YTOP)
	if (wave0 .LE. 0.0) then
	  lbl = 'd (\A)'
	else
	  lbl = '2\gH (degrees)'
	endif
	CALL PGLABEL(lbl,' ',' ')
	CALL PGBOX('bc',0.0,0,'bc',0.0,0)
C--------------------------------------------------------------------
C--------------------------------------------------------------------
C loop for each plot
	k = 1
	DO 200 I=1,ncpp
C set up plot limits 
	  ylow = ybot + ymarb + (ncpp-i)*ysize
	  yhgh = ybot - ymart + (ncpp-i+1.)*ysize
	  CALL PGVPORT(Xleft,1.-Xright,ylow,yhgh)
C Y axis is arbitrary here
	  CALL PGWINDOW(xlow,xhigh,0.0,1.)
	  call pgsch(1.0)
	  call pgsch(chars)
C label the x-axis
	  if (i .eq. ncpp) THEN
	    CALL PGBOX('bnist',0.0,0,' ',0.0,0)
	  ELSE
	    CALL PGBOX('bist',0.0,0,' ',0.0,0)
	  endif
	  IF ((EVERYF .or. I .eq. 1) .and. .not. nodata) THEN 
C Plot the observed data
	    IF (EVERYF) call pgsci(2)
	    CALL PGWINDOW(xlow,xhigh,0.0,ymax/DATSCL)
	    call pgline(nxarr,xarray,B(1,1))
	    IF (EVERYF) THEN
	      call pgmtext('T',-3.0,0.98,1.0,filename)
	      if (datscl .ne. jcscl) THEN
		write (name,'(A,f6.2)') 'Data \(2235)',datscl/jcscl
	        call pgmtext('T',-4.5,0.98,1.0,name)
	      ENDIF
	      call pgsci(1)
	    ELSE
	      call pgmtext('T',-1.5,0.98,1.0,filename)
	      call pgsci(1)
C the only plot in this box
C   label the y-axis
	      CALL PGWINDOW(xlow,xhigh,0.0,99.9/datscl)
	      if (datscl .gt. 1) THEN
		CALL PGBOX(' ',0.0,0,'bvnist',10.0,4)
	      ELSE
		CALL PGBOX(' ',0.0,0,'bvnist',25.0,3)
	      ENDIF
	      goto 200
	    ENDIF
	  ELSEIF ((EVERYF .or. I .eq. 1) .and. .not. nopeak) THEN 
C Plot the observed peaks 
	    CALL PGWINDOW(xlow,xhigh,0.0,99.9/datscl)
	    IF (EVERYF) call pgsci(2)
	    DO II=1,NPKS(1)
	      IF (wave0 .GT. 0) THEN 
		CALL DTOTT(dspace(ii,1),xpos,wave0)
	      ELSE
		xpos = dspace(ii,1)
	      ENDIF
	      if (xpos .ge. MIN(xlow,xhigh) .and. 
	1		xpos .le. MAX(xlow,xhigh)) THEN
		XT(1) = xpos
		XT(2) = xpos
		YT(1) = 0.0
		YT(2) = relint(ii,1)
		call PGLINE(2,XT,YT)
	      ENDIF
	    ENDDO
	    IF (EVERYF) THEN
	      call pgmtext('T',-3.0,0.98,1.0,filename)
	      call pgsci(1)
	      if (datscl .ne. jcscl) THEN
		write (name,'(A,f6.2)') 'Obs Peaks \(2235)',datscl/jcscl
	        call pgmtext('T',-4.5,0.98,1.0,name)
	      ENDIF
	    ELSE
	      call pgmtext('T',-1.5,0.98,1.0,filename)
	      call pgsci(1)
C the only plot in this box
C   label the y-axis
	      if (datscl .gt. 1) THEN
		CALL PGBOX(' ',0.0,0,'bvnist',10.0,4)
	      ELSE
		CALL PGBOX(' ',0.0,0,'bvnist',25.0,3)
	      endif
	      goto 200
	    ENDIF
	  ENDIF
	  if (k .eq. 1) goto 14
31	  ib = ib + 1
	  IF (ib .ge. 32) then
	    ib = 0
32	    iw = iw + 1
	    IF (iw .gt. N_Lwords) goto 127
	    IF (TABLE(iw) .EQ.0) GOTO 32
	  ENDIF
	  IF (IBITS(TABLE(iw),ib,1).EQ.1) THEN
C     found a card
	    icrd = icrd + 1
	  ELSE
	    goto 31
	  ENDIF
C   convert bit# and word# to seq#
14	  k = k + 1
	  ISEQ = 32*(iw-1) + ib  + 1
C 	get record numbers from seq#
	  READ (LUNM,rec=ISEQ,err=127) JCARD,NAMREC,FULREC,DIREC
C   get name info
	  CALL READNAM(NAMREC,LUNN,QM,FCOR,CHFORM,LCHFORM,CHNAME,LCHNAME)
C    get D/I info
	  CALL READDI(DIREC,dsp,INT,NDSINT,LPDF,0.0,200)
!	  CALL DSORT(dsp,INT,NDSINT)
	  IF (wave0 .gt. 0.0) then	
	    do ii=1,ndsint
	      CALL DTOTT(dsp(iI),angle(ii),wave0)
	    enddo
	  ENDIF
C label each plot in upper right corner
	  write(lbl,'(i6.6)') jcard
	  call pgmtext('T',-1.5,0.98,1.0,lbl)
	  IF (HARDF) then
	    IF (LCHFORM + LCHNAME .lt. 50) then 
		write(name,'(60a)') (CHFORM(j),j=1,LCHFORM),' / ',
	1		(CHNAME(j),j=1,min(55-lchform,LCHNAME))
     	    ELSE
		write(name,'(60a)') (CHFORM(j),j=1,min(60,LCHFORM))
	    ENDIF
	    call pgmtext('T',-1.5,0.02,0.0,name)
	  ELSE
	    WRITE(str1,'(i8)') icrd
	    call press(str1,l1)
	    WRITE(str2,'(i8)') nph
	    call press(str2,l2)
	    call pgmtext('T',-1.5,0.02,0.0,str1(1:l1)//'/'//str2(1:l2))
	  ENDIF
c Plot the JCPDS peaks 
	  CALL PGWINDOW(xlow,xhigh,0.0,99.9/jcscl)
	  DO II=1,NDSINT
	    IF (wave0 .GT. 0) THEN 
	      xpos = angle(II)
	    ELSE
	      xpos = dsp(II)
	    ENDIF
	    if (xpos .ge. MIN(xlow,xhigh) .and. 
	1		xpos .le. MAX(xlow,xhigh)) THEN
	      XT(1) = xpos
	      XT(2) = xpos
	      YT(1) = 0.0
	      YT(2) = int(ii)
	      call PGLINE(2,XT,YT)
	    ENDIF
	  ENDDO
	  if (jcscl .gt. 1) THEN
	    CALL PGBOX(' ',0.0,0,'bvnist',10.0,4)
	  ELSE
	    CALL PGBOX(' ',0.0,0,'bvnist',25.0,3)
	  ENDIF
200	CONTINUE

C close hardcopy graphics device and reopen terminal
	IF (HARDF) then
	  CALL CLHD(lo,li)
	ELSE
	  WRITE(LO,79)
79	  FORMAT ('$REPEAT PLOT ON HARDCOPY? (Default: N): ')
	  ians = 'N'
	  READ (LI,'(a1)',end=998) IANS
C close the graphics terminal 
998	  CALL EGRAF(ICRT,lo,li)
	  IF(IANS.EQ.'Y' .or. IANS.EQ.'y') THEN 
	    hardf = .true.
	    goto 21
	  ENDIF
	ENDIF

999	RETURN
127	return 1
	END
