-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmasklandmask.f90
136 lines (91 loc) · 3.2 KB
/
masklandmask.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
program masklandmask
! gfortran -o masklandmask masklandmask.f90 -I/usr/local/include -L/usr/local/lib -lnetcdff
use iso_fortran_env
use netcdf
implicit none
integer, parameter :: sp = real32
integer, parameter :: i2 = int16
character(200) :: classfile ! the file containing the land cover classes
character(200) :: targetfile ! the file to be modified
real(sp), allocatable, dimension(:,:,:) :: classfrac
integer(i2), allocatable, dimension(:,:) :: var
integer :: status
integer :: ncid
integer :: dimid
integer :: varid
integer :: xlen
integer :: ylen
integer :: nclass
integer :: x
integer :: y
integer(i2) :: imissing
real(sp) :: landfrac
! ---------
! G3WBM (surface cover classes)
! 0: Land
! 1: Land (No Landsat observation)
! 10: Snow
! 20: Wet Soil / Wet Vegetation / Lava
! 30: Salt Marsh
! 40: Temporal Flooded Area
! 50: Permanent Water
! 51: Permanent Water (Added by SWBD)
! 99: Ocean (Given by external land/sea mask)
! ---------
call getarg(1,classfile)
status = nf90_open(classfile,nf90_nowrite,ncid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'lon',dimid)
if (status == nf90_ebaddim) status = nf90_inq_dimid(ncid,'x',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=xlen)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'lat',dimid)
if (status == nf90_ebaddim) status = nf90_inq_dimid(ncid,'y',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=ylen)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'class',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=nclass)
if (status /= nf90_noerr) call handle_err(status)
allocate(classfrac(xlen,ylen,nclass))
status = nf90_inq_varid(ncid,'classfrac',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,classfrac)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_close(ncid)
if (status /= nf90_noerr) call handle_err(status)
! ---------
call getarg(2,targetfile)
allocate(var(xlen,ylen))
status = nf90_open(targetfile,nf90_write,ncid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_varid(ncid,'Band1',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,var)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'missing_value',imissing)
if (status /= nf90_noerr) call handle_err(status)
do y = 1,ylen
do x = 1,xlen
landfrac = sum(classfrac(x,y,1:2))
if (landfrac <= 0.) var(x,y) = imissing
end do
end do
status = nf90_put_var(ncid,varid,var)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_close(ncid)
if (status /= nf90_noerr) call handle_err(status)
! ---------
contains
subroutine handle_err(status)
! Internal subroutine - checks error status after each netcdf call,
! prints out text message each time an error code is returned.
integer, intent (in) :: status
if(status /= nf90_noerr) then
write(0,*)'NetCDF error: ',trim(nf90_strerror(status))
stop
end if
end subroutine handle_err
end program masklandmask