PROGRAM toasciiprec
!========================================================================================================================================
!Programa para preparar os dados de entrada para o programa FCLIMDEX
!Modificado em: Marco de 2020
!    --> Foi retirada parte de processamento paralelo
!    --> Para compilar usar pgf90
!    --> Para o Eta-MC em 20km - Mais que 30 anos de dados compilar usando: -Mlarge_arrays -mcmodel=medium
!         ex.: toascii_eta-besm_hist_fewer_files_semmpi.f90 -Mlarge_arrays -mcmodel=medium -o toascii_eta-besm_hist_fewer_files_semmpi.x
!========================================================================================================================================
 IMPLICIT NONE

! INCLUDE "mpif.h" 

 INTEGER, PARAMETER :: nXPoint=355 
 INTEGER, PARAMETER :: nYPoint=390
 INTEGER, PARAMETER :: nMonths=12 !Número de meses
 INTEGER, PARAMETER :: YearInit=1976
 INTEGER, PARAMETER :: YearEND=2005
 INTEGER, PARAMETER :: nDaysTot=366*(YearEND-YearInit+1)
!GSM REAL               :: Var_prec(1,nYPoint,nDaysTot),Var_tmax(1,nYPoint,nDaysTot),Var_tmin(1,nYPoint,nDaysTot)	
 REAL,ALLOCATABLE   :: Var_prec_mon(:,:,:),Var_tmax_mon(:,:,:),Var_tmin_mon(:,:,:),Var_prec(:,:,:)
 CHARACTER(LEN=255) :: DATEy
 CHARACTER(LEN=255) :: DATEm
 CHARACTER          :: PATH*300
 CHARACTER(LEN=255) :: FILENAME1,FILENAME2
 CHARACTER          :: PATH_out*300
 CHARACTER(LEN=255) :: FILENAME_out
 CHARACTER(LEN=3)   :: xpoint 
 CHARACTER(LEN=3)   :: ypoint 
 LOGICAL            :: existfile
 REAL               :: rindex
 INTEGER            :: year, month, nDays, day, irec
 INTEGER            :: dd_prec, dd_temp, dd
 INTEGER            :: j 
 INTEGER            :: i
 INTEGER            :: it,t
 INTEGER            :: lrec
 INTEGER(4)         :: MON(12),MONLEAP(12)

 INTEGER            :: ierr,num_procs,mype,js_x,je_x
 INTEGER, ALLOCATABLE :: myXs(:),myXe(:)

 DATA MON/31,28,31,30,31,30,31,31,30,31,30,31/
 DATA MONLEAP/31,29,31,30,31,30,31,31,30,31,30,31/

! Edit:  PATH and PATH_out directories
 PATH='/dados/grpeta/Team/DiegoChagas/IndicesExtremos/Eta_CanESM2_cor_mensal/Historical/'
 
 ALLOCATE(Var_prec(nXPoint,nYPoint,nDaysTot));Var_prec=0.0   
  
             dd_prec=1
             DO year=YearInit,YearEnd
                WRITE(DATEy,'(I4)') year
                DO month=1,nMonths
                   WRITE(DATEm,'(I2.2)') month
                   IF (leapyear(year).eq.1) THEN
             	      nDays=MONLEAP(month)
	                  ELSE
             	      nDays=MON(month)
                   ENDIF 
                   ALLOCATE(Var_prec_mon(nXPoint,nYPoint,nDays));Var_prec_mon=0.0
   
                   FILENAME1='Eta_CanESM2_prec_'//TRIM(ADJUSTL(DATEm))//TRIM(DATEy)//'.bin'
                   PRINT*, nDays,DATEm,DATEy 
       	           OPEN(32,FILE=TRIM(PATH)//TRIM(FILENAME1),STATUS='old',FORM='unformatted',ACCESS='direct',recl=nXPoint*nYPoint*nDays*4)
				             
                   READ(32,rec=1) Var_prec_mon  
		                 DO t=1,nDays
                      Var_prec(:,:,dd_prec)=Var_prec_mon(:,:,t)
           		         dd_prec=dd_prec+1
		                 ENDDO   

                   DEALLOCATE(Var_prec_mon(nXPoint,nYPoint,nDays))                             
                ENDDO!months
             ENDDO!year
 

             DO i=1,nXPoint
                WRITE(xpoint,'(I3.3)')i
                PATH_out='/dados/grpeta/Team/DiegoChagas/IndicesExtremos/ascii_6cols_histCAN/'//TRIM(xpoint)//'/'
                CALL SYSTEM ( "mkdir -p "//PATH_out )
                FILENAME_out=TRIM(xpoint)//'.txt'
                OPEN(10,FILE=TRIM(PATH_out)//TRIM(FILENAME_out),STATUS='unknown',FORM='formatted',POSITION='append')
			
                DO j=1,nYPoint
	               WRITE(ypoint,'(I3.3)')j            
	                  dd=1
                   DO year=YearInit,YearEnd
                   WRITE(DATEy,'(I4)') year 
		                    DO month=1,nMonths
                      WRITE(DATEm,'(I2.2)') month
	           		        IF (leapyear(year).eq.1) THEN
             	           nDays=MONLEAP(month)
	                     ELSE
             	           nDays=MON(month)
                      ENDIF   
		                       DO day=1,nDays
                         IF (Var_prec(i,j,dd).lt.0) Var_prec(i,j,dd)=-99.9
		                       write(10,'(I4,x,I2,x,I2,x,F9.3,x,F9.3,x,F9.3)') year,month,day,Var_prec(i,j,dd),Var_prec(i,j,dd),Var_prec(i,j,dd)  
		                       dd=dd+1
		                       ENDDO !day
		                    ENDDO !month
                   ENDDO !year
                ENDDO  !1,nYPoint
                CLOSE (10)
             ENDDO		!1,nXPoint	
 
 DEALLOCATE(Var_prec(nXPoint,nYPoint,nDaysTot))	
	
 CONTAINS 
 RECURSIVE integer function leapyear(iyear)
 
 integer iyear
   if(mod(iyear,4).eq.0) then
      leapyear=1
   else
            leapyear=0
   endif
 end function leapyear
 
END PROGRAM 


