Skip to main content

Example 06 — Filter with Dealloc Loop

Reads soybean land-use data for multiple years, filters values below 10 ha, and writes the result. Demonstrates dealloc inside a loop.

Source Code

program main
use FPL
implicit none

integer(kind=4) :: k
character(len=4) :: year
type(nc2d_double_lld) :: soybean

do k = 2000, 2014
write(year, '(i4)') k
write(*, '(1a1,a25,x,i4,$)') char(13), "Filtering :::::::::::::: ", k

soybean%varname = "landuse"
soybean%lonname = "lon"
soybean%latname = "lat"

call readgrid("database/LUCULTSOJA"//year//".nc", soybean)

! Remove data below 10 ha
where (soybean%ncdata .lt. 10 .and. soybean%ncdata .ne. soybean%FillValue)
soybean%ncdata = 0.0
elsewhere (soybean%ncdata .eq. 0)
soybean%ncdata = 0.0
end where

call writegrid("database/filteredSoybean"//year//".nc", soybean)

! Deallocate before next iteration
call dealloc(soybean)
end do

write(*,*) "Finish filtering"
end program main

Compile & Run

gfortran -o ex6.out ex6_dealloc.f90 -I/usr/lib64/gfortran/modules/ -lFPL
./ex6.out

Output

Example 06 result