OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
landcover_nc4.f95
Go to the documentation of this file.
1 module landcover
2 
3  implicit none
4 
5 ! -- restrict access to only module methods listed as public.
6  private
8 
9 
10  integer, dimension(:,:), allocatable :: lcdata
11 
12  contains
13 
14  integer function load_landcover(lc_file) result(status)
15 
16 ! include 'hdf.f90'
17 ! include 'dffunc.f90'
18  use netcdf
19 
20  implicit none
21 
22  character(len=255), intent(in) :: lc_file
23 
24  character(len=255) :: sds_name
25  integer :: sd_id, sds_id, sds_index
26  integer, dimension(2) :: start2, stride2, dims2
27  integer, dimension(32):: dimids
28  integer :: rank, dtype, nattrs
29 
30  integer :: nc_id
31  integer :: dim_id
32  integer :: dset_id
33  integer :: grp_id
34 
35  character(len=255) :: dset_name
36  character(len=255) :: attr_name
37  character(len=255) :: group_name
38  character(len=255) :: err_msg
39 
40  status = nf90_open(lc_file, nf90_nowrite, nc_id)
41  if (status /= nf90_noerr) then
42  print *, "ERROR: Failed to open deepblue lut_nc4 file: ", status
43  return
44  end if
45 
46  group_name = 'LANDCOVER'
47  status = nf90_inq_ncid(nc_id, group_name, grp_id)
48  if (status /= nf90_noerr) then
49  print *, "ERROR: Failed to get ID of group "//trim(group_name)//": ", status
50  return
51  end if
52 
53  dset_name = 'VEGETATION'
54  status = nf90_inq_varid(grp_id, dset_name, dset_id)
55  if (status /= nf90_noerr) then
56  print *, "ERROR: Failed to get ID of dataset "//trim(dset_name)//": ", status
57  return
58  end if
59  status = nf90_inquire_variable(grp_id, dset_id, dimids=dimids)
60  status = nf90_inquire_dimension(grp_id, dimids(1), len = dims2(1))
61  status = nf90_inquire_dimension(grp_id, dimids(2), len = dims2(2))
62  allocate(lcdata(dims2(1), dims2(2)), stat=status)
63  if (status /= 0) then
64  print *, "ERROR: Unable to allocate lcdata array: ", status
65  return
66  end if
67 
68  start2=(/1,1/)
69  stride2=(/1,1/)
70  status = nf90_get_var(grp_id, dset_id, lcdata, start=start2, &
71  stride=stride2, count=dims2)
72  err_msg = nf90_strerror(status)
73  if (status /= nf90_noerr) then
74  print *, "ERROR: Failed to read dataset "//trim(dset_name)//": ", status
75  return
76  end if
77 
78  status = nf90_close(nc_id)
79  if (status /= nf90_noerr) then
80  print *, "ERROR: Failed to close lut_nc4 file: ", status
81  return
82  end if
83 
84  end function load_landcover
85 
86  subroutine unload_landcover(status)
87  implicit none
88 
89  integer, intent(inout) :: status
90 
91  deallocate(lcdata, stat=status)
92  if (status /= 0) then
93  print *, "ERROR: Unable to deallocate land cover array: ", status
94  return
95  end if
96 
97  end subroutine unload_landcover
98 
99  integer function get_landcover(lat,lon,status) result(lc)
100  implicit none
101 
102  real, intent(in) :: lat
103  real, intent(in) :: lon
104  integer, intent(inout) :: status
105 
106  integer :: ilat,ilon
107 
108  status = 0
109 
110  ilat = floor(lat * 20.0) + 1800 + 1
111  ilon = floor(lon * 20.0) + 3600 + 1
112  if (lon == 180.0 .AND. ilon == 3601) ilon = 1
113  if (ilat > size(lcdata,1) .OR. ilon > size(lcdata,2)) then
114  print *, "ERROR: Indices out of bounds for land cover array: ", ilat, ilon
115  status = -1
116  return
117  end if
118  lc = lcdata(ilat,ilon)
119 
120  return
121  end function get_landcover
122 
123 
124 end module landcover
integer function, public get_landcover(lat, lon, status)
integer function, public load_landcover(lc_file)
string & trim(string &s, const string &delimiters)
Definition: EnvsatUtil.cpp:29
subroutine, public unload_landcover(status)
dtype
Definition: DDataset.hpp:31