      program sunp2sp_m            
c converts sun percent monthly time series to cloud oktas
      parameter (xmiss=-999.9,imiss=-9999)
      character*80 mhfmt,mdfmt,mhfmt2,mdfmt2
      character*80 infl,outfl,bkfl
      character*80 ihd,jhd
      integer sunp(12)
      character nm*20,cn*13,id*9
      integer dys(12)
      real day(12),dec(12),cld(12)
c     
      DATA DYS/31,28.25,31,30,31,30,31,31,30,31,30,31/
      DATA DEC/-21.07,-13.4,-2.42,9.38,18.77,22.95,21.1,13.53,
     &      2.62,-9.08,-18.6,-23.35/                 
      pi=3.14159265
      rad=180.0/PI


      mhfmt='(a80)'
c      mhfmt2='(i7,i5,i6,i5,a20,a13,2i4,i7,a9)'
      mhfmt2='(i7,i6,i7,i5,x,a20,x,a13,x,i4,x,i4,i7,a9)'
      mdfmt='(i4,12i5)'
      mdfmt2='(i4,13i5)'
c
      write(*,*)'Enter sunshine percent file name'
      read(*,'(a\)')infl
      open(1,file=infl,status='old')
      write(*,*)'Enter cloud oktas file name to write to'
      read(*,'(a\)')infl
      open(2,file=infl,status='unknown')
      
1     read(1,mhfmt2,end=99)iwmo,lat,lon,ielv,nm,cn,iy1,iy2,ibox,id
      write(2,mhfmt2)iwmo,lat,lon,ielv,nm,cn,iy1,iy2,ibox,id
      xlat=real(lat)/100
      do iy=iy1,iy2
       read(1,mdfmt)iyear,(sunp(im),im=1,12)
       DO im=1,12
        ratio=(REAL(sunp(im))/1000)
        IF(RATIO.GE.0.95) CLD(im)=0
	IF(RATIO.LT.0.95.AND.RATIO.GE.0.35) CLD(im)=(0.95-RATIO)*100
	IF(RATIO.LT.0.35.AND.RATIO.GE.0.15)
     &     CLD(im)=((0.35-RATIO)*50)+60
        IF(RATIO.LT.0.15) CLD(im)=((0.15-RATIO)*100)+70
	IF(CLD(im).GT.80.0) CLD(im)=80.0
	if(ratio.lt.0)cld(im)=-9999
       enddo
       write(2,mdfmt)iyear,(nint(cld(im)),im=1,12)
      enddo
      goto 1
99    end        


