!write a program by fortran language to calculate Standardized Precipitation Evapotranspiration Index (SPEI) using GEV fitting program spei_calculation implicit none !Declare variables integer :: n, i, j, k, index real, dimension(:), allocatable :: precipitation, evapotranspiration, spei real :: mu, sigma, shape !Read number of data points read *, n !Allocate memory for arrays allocate(precipitation(n), evapotranspiration(n), spei(n)) !Read precipitation and evapotranspiration data do i=1,n read *, precipitation(i), evapotranspiration(i) end do !Calculate SPEI values using GEV fitting do i=1,n index = i k = 0 do j=1,n if (precipitation(j) - evapotranspiration(j) < precipitation(index) - evapotranspiration(index)) then index = j k = k + 1 end if end do !GEV parameter estimation mu = sum(log(precipitation(index:index+k) - evapotranspiration(index:index+k))) / k sigma = sqrt(sum((log(precipitation(index:index+k) - evapotranspiration(index:index+k)) - mu)**2) / k) shape = ((3 * sum((log(precipitation(index:index+k) - evapotranspiration(index:index+k)) - mu) / sigma)) / k) / ((sum(((log(precipitation(index:index+k) - evapotranspiration(index:index+k)) - mu) / sigma)**3)) / k) !SPEI calculation spei(i) = (log(precipitation(i) - evapotranspiration(i)) - mu) / sigma + shape * ((log(precipitation(i) - evapotranspiration(i)) - mu) / sigma) end do !Print SPEI values do i=1,n write(*,*) spei(i) end do !Deallocate memory deallocate(precipitation, evapotranspiration, spei) end program spei_calculation