SURFEX  V8_0
Surfex V8_0 release
 All Classes Files Functions Variables
def_var_netcdf.F90
Go to the documentation of this file.
1 !SFX_LIC Copyright 1994-2014 CNRS, Meteo-France and Universite Paul Sabatier
2 !SFX_LIC This is part of the SURFEX software governed by the CeCILL-C licence
3 !SFX_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
4 !SFX_LIC for details. version 1.
5 ! #########
6  SUBROUTINE def_var_netcdf (DGU, &
7  kfile_id,hname,hlong_name,kdim_id,hatt_title,hatt_text,kvar_id,ktype,klen)
8 !
9 !!
10 !! MODIFICATIONS
11 !! -------------
12 !! B. Decharme 07/2013 special case for time in netcdf output files
13 !-------------------------------------------------------------------------------
14 !
15 !
17 !
18 USE modd_io_surf_nc, ONLY : cfileout_nc, nid_nc
19 USE modd_ol_fileid, ONLY : xvar_to_fileout, xout
20 USE modd_surf_par, ONLY : xundef, nundef
21 !
22 !
23 USE yomhook ,ONLY : lhook, dr_hook
24 USE parkind1 ,ONLY : jprb
25 !
26 IMPLICIT NONE
27 include "netcdf.inc"
28 
29 !
30 TYPE(diag_surf_atm_t), INTENT(INOUT) :: dgu
31 !
32 INTEGER, INTENT(IN) :: kfile_id
33  CHARACTER(LEN=*), INTENT(IN) :: hname
34  CHARACTER(LEN=*), INTENT(IN) :: hlong_name
35 INTEGER, DIMENSION(:), INTENT(IN) :: kdim_id
36  CHARACTER(LEN=*),DIMENSION(:),OPTIONAL, INTENT(IN) :: hatt_title,hatt_text
37 INTEGER, OPTIONAL, INTENT(OUT) :: kvar_id
38 INTEGER, OPTIONAL, INTENT(IN) :: ktype
39 INTEGER, OPTIONAL, INTENT(IN) :: klen
40 !
41 ! ** local variables
42 !
43  CHARACTER(LEN=20), DIMENSION(:), ALLOCATABLE :: ytemp
44  CHARACTER(LEN=100) :: yname
45  CHARACTER(LEN=50) :: ypas
46 INTEGER, DIMENSION(4) :: iret
47 INTEGER :: jret,ivar_id,iatt,jatt,ilen,indim
48 INTEGER :: ifield,jfield,isize
49 LOGICAL :: nowrite, gopened, gexist
50 REAL(KIND=JPRB) :: zhook_handle
51 !--------------------------------------
52 !
53 IF (lhook) CALL dr_hook('DEF_VAR_NETCDF',0,zhook_handle)
54 !
55 IF (.NOT.ALLOCATED(xvar_to_fileout)) ALLOCATE(xvar_to_fileout(100))
56 !
57 xout=xout+1
58 ALLOCATE(ytemp(xout))
59 ytemp(xout) = hname
60 IF (xout.GT.1) ytemp(1:xout-1) = xvar_to_fileout
61 !
62 IF(ALLOCATED(xvar_to_fileout)) DEALLOCATE(xvar_to_fileout)
63 ALLOCATE(xvar_to_fileout(xout))
64 xvar_to_fileout = ytemp
65 !
66 DEALLOCATE(ytemp)
67 !
68 ! if output fields selection is active, test if this field is to be written
69 IF ( hname/='xx' .AND. hname/='yy' .AND. hname/='lon' .AND. &
70  hname/='lat' .AND. hname/='time' .AND. dgu%LSELECT ) THEN
71  ifield=count(dgu%CSELECT /= ' ')
72  nowrite=.true.
73  DO jfield=1,ifield
74  IF ( trim(dgu%CSELECT(jfield))==trim(hname) ) THEN
75  nowrite=.false.
76  ENDIF
77  ENDDO
78  IF ( nowrite .AND. lhook) CALL dr_hook('DEF_VAR_NETCDF',1,zhook_handle)
79  IF ( nowrite ) RETURN
80 ENDIF
81 !
82 !define variables in the netcdf file
83 indim=SIZE(kdim_id)
84 !
85 IF (present(ktype)) THEN
86  iret(1) = nf_def_var(kfile_id,hname,ktype,indim,kdim_id,ivar_id)
87 ELSE
88  iret(1) = nf_def_var(kfile_id,hname,nf_double,indim,kdim_id,ivar_id)
89 ENDIF
90 !
91 IF (indim/=0) THEN
92  IF (present(ktype)) THEN
93  IF (ktype==nf_double .OR. ktype==nf_float) THEN
94  iret(2) = nf_put_att_double(kfile_id,ivar_id,'_FillValue',nf_double,1,xundef)
95  ELSEIF (ktype==nf_int .OR. ktype==nf_short) THEN
96  iret(2) = nf_put_att_int(kfile_id,ivar_id,'_FillValue',nf_int,1,nundef)
97  ELSEIF (ktype==nf_char) THEN
98  IF (indim>1) iret(2) = nf_put_att_text(kfile_id,ivar_id,'_FillValue',nf_char,1,"")
99  IF (present(klen)) iret(3) = nf_put_att_int(kfile_id,ivar_id,'len',nf_int,1,klen)
100  ENDIF
101  ELSE
102  iret(2) = nf_put_att_double(kfile_id,ivar_id,'_FillValue',nf_double,1,xundef)
103  ENDIF
104 ENDIF
105 !
106 IF (present(ktype)) THEN
107  IF (ktype==nf_char) THEN
108  IF (present(klen)) iret(3) = nf_put_att_int(kfile_id,ivar_id,'len',nf_int,1,klen)
109  ENDIF
110 ENDIF
111 !
112 IF (hlong_name.NE.'') iret(2) = nf_put_att_text(kfile_id,ivar_id,'long_name',len_trim(hlong_name),hlong_name)
113 !
114 !Write optional attribute
115 IF (present(hatt_title).AND.present(hatt_text)) THEN
116  iatt=SIZE(hatt_title)
117  IF (iatt .EQ. SIZE(hatt_text)) THEN
118  DO jatt=1,iatt
119  ilen=len_trim(hatt_text(jatt))
120  ypas=hatt_text(jatt)
121  jret = nf_put_att_text(kfile_id,ivar_id,hatt_title(jatt),ilen,hatt_text(jatt))
122  ENDDO
123  ENDIF
124 ENDIF
125 !
126 IF (present(kvar_id)) kvar_id = ivar_id
127 !
128 IF (lhook) CALL dr_hook('DEF_VAR_NETCDF',1,zhook_handle)
129 !
130 END SUBROUTINE def_var_netcdf
subroutine def_var_netcdf(DGU, KFILE_ID, HNAME, HLONG_NAME, KDIM_ID, HATT_TITLE, HATT_TEXT, KVAR_ID, KTYPE, KLEN)