Ticket #459: edit_netcdf.f90

File edit_netcdf.f90, 1.6 KB (added by mmcgrath, 3 years ago)

Code to modify a PFTmap by adding an attribute and changing NaN values

Line 
1program simple_xy_rd
2  use netcdf
3  implicit none
4
5  ! This is the name of the data file we will read.
6  character (len = *), parameter :: FILE_NAME = "PFTmap.nc"
7
8  ! This will be the netCDF ID for the file and data variable.
9  integer :: ncid, varid
10
11  ! Loop indexes, and error handling.
12  integer :: x, y, status,i
13 
14  REAL :: fillvalue
15  REAL,DIMENSION(1:426496) :: var
16
17
18  ! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
19  ! the file.
20  call check( nf90_open(FILE_NAME, NF90_WRITE, ncid) )
21
22  ! Get the varid of the data variable, based on its name.
23  call check( nf90_inq_varid(ncid, "maxvegetfrac", varid) )
24
25  call check( nf90_get_att(ncid, varid, "missing_value", fillvalue) )
26  var(:)=fillvalue
27  WRITE(6,*) var(1:100)
28  call check( NF90_GET_VAR (ncid, varid, var, &
29       start=(/1,1,1,1/), count=(/128,119,28,1/)))
30
31  DO i=1,426496
32     if(ISNAN(var(i))) var(i)=fillvalue
33  ENDDO
34  status = NF90_PUT_VAR (ncid, varid, var, &
35       start=(/1,1,1,1/), count=(/128,119,28,1/))
36
37  WRITE(6,*) var(1:100)
38 
39  ! Add a new attribute
40  call check( nf90_get_att(ncid, varid, "_FillValue", fillvalue) )
41  status = nf90_redef(ncid)
42  call check( NF90_put_att(ncid,varid, "missing_value", fillvalue) )
43
44  ! Close the file, freeing all resources.
45  call check( nf90_close(ncid) )
46
47  print *,"*** SUCCESS reading example file ", FILE_NAME, "! "
48
49contains
50  subroutine check(status)
51    integer, intent ( in) :: status
52   
53    if(status /= nf90_noerr) then
54      print *, trim(nf90_strerror(status))
55      stop "Stopped"
56    end if
57  end subroutine check 
58end program simple_xy_rd