********************************************************
      SUBROUTINE DPLOT
     &       (ICH1,ICH2,D1,ED1,D2,ED2,R1,ER1,DAY,ELV,NDATA,CMNT,IHR)
*-------------------------------------------------------
*      DATA WO PLOTTO SURU
*-------------------------------------------------------
      DIMENSION D1(500),D2(500),R1(500),DAY(500),ELV(500)
      DIMENSION ED1(500),ED2(500),ER1(500)
      CHARACTER CMNT*128
      CHARACTER TEXT1*7,TEXT2*7
c var
      parameter (MXROW=500)
      integer iery(4)
      real y(MXROW,7)
      character cmd(23)*72
*------- MAX DATA ----------------------
      XMAX1=DAY(1)
      YMAX11=D1(1)
      YMAX21=D2(1)
 2001 DO 2000 I2=2,NDATA
         IF(XMAX1.LT.DAY(I2))XMAX1=DAY(I2)
         IF(YMAX11.LT.D1(I2))YMAX11=D1(I2)
         IF(YMAX21.LT.D2(I2))YMAX21=D2(I2)
 2000 CONTINUE
      YMAX1=YMAX11
      IF(YMAX1.LT.YMAX21)YMAX1=YMAX21
*-------- KIRINOYOI SUUJI -------------
      CALL SUTBLE(XMAX1,XMAX2)
      CALL SUTBLE(YMAX1,YMAX2)
      YMIN2=-YMAX2/4.
*-------- MAKE DATA ARRAY --------------
      iery(1)=0
      iery(2)=1
      iery(3)=1
 2101 DO 2100 I21=1,NDATA
         y(i21,1)=day(i21)
         y(i21,2)=d1(i21)
         y(i21,3)=ed1(i21)
         IF(IHR.EQ.1)THEN
            y(i21,4)=d2(i21)
            y(i21,5)=ed2(i21)
         ENDIF
 2100 CONTINUE
*----------- REMARK -------------------
      WRITE(TEXT1,'(''CH:'',I4)')ICH1
      WRITE(TEXT2,'(''CH:'',I4)')ICH2
      IF(IHR.EQ.0)THEN
*------ CALL PLT  ---------------------------
         write(cmd(1),'(''R X 0 '',f7.1)') xmax2
         write(cmd(2),'(''R Y '',2f7.1)') ymin2,ymax2
         cmd(3)='LAB T '//cmnt(1:60)
         cmd(4)='LAB X DAY'
         cmd(5)='LAB Y FLUX'
ce       cmd(6)='MARK 23 ON 2'
ce       cmd(7)='LAB 2 MAR 23 JUS Lef'
         cmd(6)='MARK 2 ON 2'
         cmd(7)='LAB 2 MAR 2 JUS Lef'
         write(cmd(8),'(''LAB 2 VPOS 0.75 0.75 "'',a7,'' "'')')
     &        text1
         ncmd=8
         nvec=2
         call plt(y,iery,MXROW,ndata,nvec,cmd,ncmd,ier)
      ELSE
*------- MAX DATA ----------------------
         YMAX1=R1(1)
 3001    DO 3000 I3=3,NDATA
            IF(YMAX1.LT.R1(I3))YMAX1=R1(I3)
 3000    CONTINUE
*-------- KIRINOYOI SUUJI -------------
         CALL SUTBLE(YMAX1,YMAX3)
*-------- MAKE DATA     --------------
         iery(4)=1
 3101    DO 3100 I31=1,NDATA
            IF(R1(I31).LT.0)GO TO 3100
            IF(ER1(I31).LT.0)GO TO 3100
            y(i31,6)=r1(i31)
            y(i31,7)=er1(i31)
 3100    CONTINUE
*------ CALL PLT  ---------------------------
         cmd(1)='WI 2'
         cmd(2)='YAX 2 3'
         cmd(3)='LOC 0.0 0.2 1.0 1.0'
         write(cmd(4),'(''R X 0 '',f7.1)') xmax2
         write(cmd(5),'(''R Y2 '',2f7.1)') ymin2,ymax2
         cmd(6)='LAB T '//cmnt(1:60)
         cmd(7)='LAB NX OFF'
         cmd(8)='LAB Y2 FLUX'
ce       cmd(9)='MARK 23 ON 2'
ce       cmd(10)='LAB 2 MAR 23 JUS Lef'
         cmd(9)='MARK 2 ON 2'
         cmd(10)='LAB 2 MAR 2 JUS Lef'
         write(cmd(11),'(''LAB 2 VPOS 0.75 0.8 "'',a7,'' "'')')
     &        text1
         cmd(12)='MARK 7 ON 3'
         cmd(13)='LAB 3 MAR 7 JUS Lef'
         write(cmd(14),'(''LAB 3 VPOS 0.75 0.75 "'',a7,'' "'')')
     &        text2
         cmd(15)='WI 3'
         cmd(16)='YAX 4'
         cmd(17)='LOC 0.0 0.1 1.0 0.3'
         write(cmd(18),'(''R X 0 '',f7.1)') xmax2
         write(cmd(19),'(''R Y3 0 '',f7.1)') ymax3
         cmd(20)='LAB X DAY'
         cmd(21)='LAB Y4 RATIO'
         cmd(22)='MARK 22 ON 4'
         cmd(23)='WI ALL'
         ncmd=23
         nvec=4
         call plt(y,iery,MXROW,ndata,nvec,cmd,ncmd,ier)
*---------- END---------------------
      ENDIF
      RETURN
      END
***************************************************
      SUBROUTINE DPLOT2
     & (D1,ED1,DAY,NDATA,PER0,STP,DTP,FCURVE,FSIGM,NBIN,CHI,NTRY,ICH1,
     &  CMNT,CMNT0,NCMNT)
      DIMENSION D1(NDATA),ED1(NDATA),DAY(NDATA)
      DIMENSION CHI(NTRY)
      DIMENSION FCURVE(NBIN),FSIGM(NBIN)
      CHARACTER CMNT0(NCMNT)*128
      REAL*8 STPD,STP,DTP
*--------------------------------------------------
*     FOLDED LIGHT CURVE WO KAKU
*--------------------------------------------------
      CHARACTER CMNT*128,CMNT2*60
      CHARACTER TEXT1*7
      parameter (MXROW=2000)
      integer iery(2)
      real y(MXROW,4)
      character cmd(50)*80
*------ FOLDED LIGHT CURVE---------------
*------- MAX DATA ----------------------
      YMAX1=AMIN1(D1(1)*2,D1(1)+ED1(1))
      XMAX=1.0
 2001 DO 2000 I2=2,NDATA
         CHECK=AMIN1(D1(I2)*2,D1(I2)+ED1(I2))
         IF(YMAX1.LT.CHECK)YMAX1=CHECK
 2000 CONTINUE
*-------- KIRINOYOI SUUJI -------------
      CALL SUTBLE(YMAX1,YMAX)
      YMIN=-YMAX/4.
*-------- MAKE DATA ARRAY -------------
      iery(1)=1
      iery(2)=1
 2101 DO 2100 I21=1,NDATA
         X0=DAY(I21)/PER0
         X0=X0-INT(X0)
         y(i21,1)=x0
         y(i21,2)=0
         y(i21,3)=d1(i21)
         y(i21,4)=ed1(i21)
 2100 CONTINUE
      ndata2=ndata+1
      y(ndata2,1)=-1.2e-34
      y(ndata2,2)=-1.2e-34
*----------- TITL -------------------
      WRITE(CMNT2,'('' Folded Light Curve: Period '',F8.3,'' Day'')')
     &       PER0
      WRITE(TEXT1,'(''CH:'',I4)')ICH1
      cmd(1)='SK S'
      cmd(2)='P V'
      cmd(3)='WI 1'
      cmd(4)='LOC 0.05 0.0 0.5 0.7'
      cmd(5)='R X 0 1'
      write(cmd(6),'(''R Y '',2f7.1)') ymin,ymax
      cmd(7)='LAB T '//cmnt2
      cmd(8)='LAB 4 VPOS 0.275 0.015 "Phase"'
      cmd(9)='LAB NX ON'
      cmd(10)='LAB Y Flux (counts/sec/cm\\u2\\d)'
      cmd(11)='COL 2 ON 1'
      cmd(12)='MARK 2 ON 1'
      cmd(13)='LAB 1 MAR 2 JUS Lef'
      write(cmd(14),'(''LAB 1 VPOS 0.35 0.52 "'',a7,'' "'')')
     &     text1
*------- CHI SQUARED SEARCH------------
*------- X-Y MAX ----------------------
      XMAX=NTRY*dtp
      YMAX1=CHI(1)
 4001 DO 4000 I4=2,NTRY
         IF(YMAX1.LT.CHI(I4))YMAX1=CHI(I4)
 4000 CONTINUE
      CALL SUTBLE(YMAX1,YMAX)
*------- MAKE DATA ARRAY ---------------------
 4101 DO 4100 I41=1,NTRY
         y(ndata2+i41,1)=stp+float(i41)*dtp
         y(ndata2+i41,2)=0
         y(ndata2+i41,3)=chi(i41)
         y(ndata2+i41,4)=0
 4100 CONTINUE
      ndata2=ndata2+ntry+1
      y(ndata2,1)=-1.2e-34
      y(ndata2,2)=-1.2e-34
*----------- TITL -------------------
      cmd(15)='WI 2'
      cmd(16)='LOC 0.55 0.7 1.0 1.0'
      write(cmd(17),'(''R X '',2f7.1)') stp,stp+xmax
      write(cmd(18),'(''R Y 0 '',f7.1)') ymax
      cmd(19)='LAB 5 VPOS 0.775 0.68 "Period (Day)"'
      cmd(20)='LAB NX ON'
ce    cmd(21)='LAB Y \\gx\\u2'
      cmd(21)='LAB Y Reduced \\gx\\u2'
      cmd(22)='COL 7 ON 2'
      cmd(23)='MARK 16 ON 2'
*------- FOLDED LIGHT CURVE------------
*------- MAX DATA ARRAY ----------------------
      YMAX1=AMIN1(FCURVE(1)*2,FCURVE(1)+FSIGM(1))
      XMAX=1.0
 5001 DO 5000 I5=2,NBIN
         CHECK=AMIN1(FCURVE(I5)*2,FCURVE(I5)+FSIGM(I5))
         IF(YMAX1.LT.CHECK)YMAX1=CHECK
 5000 CONTINUE
*-------- KIRINOYOI SUUJI -------------
      CALL SUTBLE(YMAX1,YMAX)
      YMIN=-YMAX/4.
*-------- MAKE DATA ARRAY --------------
 5101 DO 5100 I51=1,NBIN
         X1=(I51-1.0)/FLOAT(NBIN)
         X2=FLOAT(I51)/FLOAT(NBIN)
         X0=(X1+X2)/2.0
         y(ndata2+i51,1)=x0
         y(ndata2+i51,2)=(x2-x1)/2.0
         y(ndata2+i51,3)=fcurve(i51)
         y(ndata2+i51,4)=fsigm(i51)
 5100 CONTINUE
      ndata2=ndata2+nbin
*----------- TITL -------------------
      cmd(24)='WI 3'
      cmd(25)='LOC 0.55 0.0 1.0 0.7'
      cmd(26)='R X 0 1'
ce    write(cmd(27),'(''R Y '',2f7.1)') ymin,ymax
      cmd(27)=cmd(6)
      cmd(28)='LAB X Phase'
      cmd(29)='LAB Y Flux (counts/sec/cm\\u2\\d)'
      cmd(30)='COL 5 ON 3'
      cmd(31)='MARK 23 ON 3'
      cmd(32)='LAB 3 MAR 23 JUS Lef'
      write(cmd(33),'(''LAB 3 VPOS 0.85 0.52 "'',a7,'' "'')')
     &     text1
      cmd(34)='WI 1'
*------ PLOT COMMENT------------------
      write(cmd(35),'(''LAB 7 VPOS 0.1 0.96 J L CS 0.7 "'',
     &     a25,'' "'')') cmnt(1:25)
      write(cmd(36),'(''LAB 8 VPOS 0.1 0.94 J L CS 0.7 "'',
     &     a34,'' "'')') cmnt(27:60)
 6001 DO 6000 I6=1,NCMNT
        write(cmd(36+i6),'(''LAB '',i2,'' VPOS 0.1 '',f5.2,
     &        '' CS 0.7 J L "'',a40,'' "'')')
     &        8+i6,0.94-0.02*i6,cmnt0(i6)
 6000 CONTINUE
      ncmd=46
      nvec=2
      call plt(y,iery,MXROW,ndata2,nvec,cmd,ncmd,ier)
      RETURN
      END
C***********************************************************************
      SUBROUTINE SUTBLE(A,B)
C***********************************************************************
C      SUBROUTINE SUTBLE(A,B)
C      A YORI OKIKUTE KIRINO YOI NUMBER B O SAGASU     A > 1
C***********************************************************************
      B=0
      IF(A.LE.0.0) RETURN
      X=ALOG10(A)
      I=X
      IF(A.LT.1.)I=I-1
      X=X-I
      X=10.**X
      B=10
      IF(X.LE.8.0) B=8
      IF(X.LE.5.0) B=5
      IF(X.LE.4.0) B=4
      IF(X.LE.2.0) B=2
      B=B*10.**FLOAT(I)
      RETURN
      END
