1 | subroutine def_var(nid,name,title,units,nbdim,dimids,nvarid,ierr) |
---|
2 | |
---|
3 | ! This subroutine defines variable 'name' in a (pre-existing and opened) |
---|
4 | ! NetCDF file (known from its NetCDF ID 'nid'). |
---|
5 | ! The number of dimensions 'nbdim' of the variable, as well as the IDs of |
---|
6 | ! corresponding dimensions must be set (in array 'dimids'). |
---|
7 | ! Upon successfull definition of the variable, 'nvarid' contains the |
---|
8 | ! NetCDF ID of the variable. |
---|
9 | ! The variables' attributes 'title' (Note that 'long_name' would be more |
---|
10 | ! appropriate) and 'units' are also set. |
---|
11 | ! Modifs: Aug2010 Ehouarn: enforce outputs to be real*4 |
---|
12 | |
---|
13 | implicit none |
---|
14 | |
---|
15 | include "netcdf.inc" |
---|
16 | |
---|
17 | integer,intent(in) :: nid ! NetCDF file ID |
---|
18 | character(len=*),intent(in) :: name ! the variable's name |
---|
19 | character(len=*),intent(in) :: title ! 'title' attribute of variable |
---|
20 | character(len=*),intent(in) :: units ! 'units' attribute of variable |
---|
21 | integer,intent(in) :: nbdim ! number of dimensions of the variable |
---|
22 | integer,dimension(nbdim),intent(in) :: dimids ! NetCDF IDs of the dimensions |
---|
23 | ! the variable is defined along |
---|
24 | integer,intent(out) :: nvarid ! NetCDF ID of the variable |
---|
25 | integer,intent(out) :: ierr ! returned NetCDF staus code |
---|
26 | |
---|
27 | ! 1. Switch to NetCDF define mode |
---|
28 | ierr=NF_REDEF(nid) |
---|
29 | |
---|
30 | ! 2. Define the variable |
---|
31 | !#ifdef NC_DOUBLE |
---|
32 | !ierr = NF_DEF_VAR (nid,adjustl(name),NF_DOUBLE,nbdim,dimids,nvarid) |
---|
33 | !#else |
---|
34 | ierr = NF_DEF_VAR (nid,adjustl(name),NF_FLOAT,nbdim,dimids,nvarid) |
---|
35 | !#endif |
---|
36 | if(ierr/=NF_NOERR) then |
---|
37 | write(*,*) "def_var: Failed defining variable "//trim(name) |
---|
38 | write(*,*) NF_STRERROR(ierr) |
---|
39 | call abort_physic("def_var",'netcdf definition problem',1) |
---|
40 | endif |
---|
41 | |
---|
42 | ! 3. Write attributes |
---|
43 | ierr=NF_PUT_ATT_TEXT(nid,nvarid,"title",& |
---|
44 | len_trim(adjustl(title)),adjustl(title)) |
---|
45 | if(ierr/=NF_NOERR) then |
---|
46 | write(*,*) "def_var: Failed writing title attribute for "//trim(name) |
---|
47 | write(*,*) NF_STRERROR(ierr) |
---|
48 | call abort_physic("def_var",'netcdf title attribute problem',1) |
---|
49 | endif |
---|
50 | |
---|
51 | ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",& |
---|
52 | len_trim(adjustl(units)),adjustl(units)) |
---|
53 | if(ierr/=NF_NOERR) then |
---|
54 | write(*,*) "def_var: Failed writing units attribute for "//trim(name) |
---|
55 | write(*,*) NF_STRERROR(ierr) |
---|
56 | call abort_physic("def_var",'netcdf units attribute problem',1) |
---|
57 | endif |
---|
58 | |
---|
59 | ! 4. Switch out of NetCDF define mode |
---|
60 | ierr = NF_ENDDEF(nid) |
---|
61 | |
---|
62 | end |
---|