[38] | 1 | subroutine writediagsoil(ngrid,name,title,units,dimpx,px) |
---|
| 2 | |
---|
| 3 | ! Write variable 'name' to NetCDF file 'diagsoil.nc'. |
---|
| 4 | ! The variable may be 3D (lon,lat,depth) subterranean field, |
---|
| 5 | ! a 2D (lon,lat) surface field, or a simple scalar (0D variable). |
---|
| 6 | ! |
---|
| 7 | ! Calls to 'writediagsoil' can originate from anywhere in the program; |
---|
| 8 | ! An initialisation of variable 'name' is done if it is the first time |
---|
| 9 | ! that this routine is called with given 'name'; otherwise data is appended |
---|
| 10 | ! (yielding the sought time series of the variable) |
---|
| 11 | |
---|
| 12 | ! Modifs: Aug.2010 Ehouarn: enforce outputs to be real*4 |
---|
| 13 | |
---|
[1047] | 14 | use comsoil_h, only: nsoilmx |
---|
[1130] | 15 | use control_mod, only: ecritphy, day_step, iphysiq |
---|
| 16 | use mod_phys_lmdz_para, only : is_mpi_root, is_master, gather |
---|
| 17 | use mod_grid_phy_lmdz, only : klon_glo, Grid1Dto2D_glo |
---|
[1047] | 18 | |
---|
[38] | 19 | implicit none |
---|
| 20 | |
---|
| 21 | #include"dimensions.h" |
---|
[1047] | 22 | !#include"dimphys.h" |
---|
[38] | 23 | #include"paramet.h" |
---|
[1130] | 24 | !#include"control.h" |
---|
[1047] | 25 | !#include"comsoil.h" |
---|
[38] | 26 | #include"netcdf.inc" |
---|
| 27 | |
---|
| 28 | ! Arguments: |
---|
| 29 | integer,intent(in) :: ngrid ! number of (horizontal) points of physics grid |
---|
| 30 | ! i.e. ngrid = 2+(jjm-1)*iim - 1/jjm |
---|
| 31 | character(len=*),intent(in) :: name ! 'name' of the variable |
---|
| 32 | character(len=*),intent(in) :: title ! 'long_name' attribute of the variable |
---|
| 33 | character(len=*),intent(in) :: units ! 'units' attribute of the variable |
---|
| 34 | integer,intent(in) :: dimpx ! dimension of the variable (3,2 or 0) |
---|
| 35 | real,dimension(ngrid,nsoilmx),intent(in) :: px ! variable |
---|
[1047] | 36 | ! Note: nsoilmx is a parameter set in 'comsoil_h' |
---|
[38] | 37 | |
---|
| 38 | ! Local variables: |
---|
| 39 | real*4,dimension(iip1,jjp1,nsoilmx) :: data3 ! to store 3D data |
---|
[1047] | 40 | ! Note iip1,jjp1 known from paramet.h; nsoilmx known from comsoil_h |
---|
[38] | 41 | real*4,dimension(iip1,jjp1) :: data2 ! to store 2D data |
---|
| 42 | real*4 :: data0 ! to store 0D data |
---|
| 43 | integer :: i,j,l ! for loops |
---|
| 44 | integer :: ig0 |
---|
| 45 | |
---|
| 46 | real*4,save :: date ! time counter (in elapsed days) |
---|
| 47 | integer,save :: isample ! sample rate at which data is to be written to output |
---|
| 48 | integer,save :: ntime=0 ! counter to internally store time steps |
---|
| 49 | character(len=20),save :: firstname="1234567890" |
---|
| 50 | integer,save :: zitau=0 |
---|
| 51 | |
---|
| 52 | character(len=30) :: filename="diagsoil.nc" |
---|
| 53 | |
---|
| 54 | ! NetCDF stuff: |
---|
| 55 | integer :: nid ! NetCDF output file ID |
---|
| 56 | integer :: varid ! NetCDF ID of a variable |
---|
| 57 | integer :: ierr ! NetCDF routines return code |
---|
| 58 | integer,dimension(4) :: id ! NetCDF IDs of the dimensions of the variable |
---|
| 59 | integer,dimension(4) :: edges,corners |
---|
| 60 | |
---|
[1130] | 61 | #ifdef CPP_PARA |
---|
| 62 | ! Added to work in parallel mode |
---|
| 63 | real dx3_glop(klon_glo,nsoilmx) |
---|
| 64 | real dx3_glo(iim,jjp1,nsoilmx) ! to store a global 3D data set |
---|
| 65 | real dx2_glop(klon_glo) |
---|
| 66 | real dx2_glo(iim,jjp1) ! to store a global 2D (surface) data set |
---|
| 67 | real px2(ngrid) |
---|
| 68 | #endif |
---|
| 69 | |
---|
[38] | 70 | ! 1. Initialization step |
---|
| 71 | if (firstname.eq."1234567890") then |
---|
| 72 | ! Store 'name' as 'firstname' |
---|
| 73 | firstname=name |
---|
| 74 | ! From now on, if 'name'.eq.'firstname', then it is a new time cycle |
---|
| 75 | |
---|
| 76 | ! just to be sure, check that firstnom is large enough to hold nom |
---|
| 77 | if (len_trim(firstname).lt.len_trim(name)) then |
---|
| 78 | write(*,*) "writediagsoil: Error !!!" |
---|
| 79 | write(*,*) " firstname string not long enough!!" |
---|
| 80 | write(*,*) " increase its size to at least ",len_trim(name) |
---|
| 81 | stop |
---|
| 82 | endif |
---|
| 83 | |
---|
| 84 | ! Set output sample rate |
---|
| 85 | isample=int(ecritphy) ! same as for diagfi outputs |
---|
| 86 | ! Note ecritphy is known from control.h |
---|
| 87 | |
---|
| 88 | ! Create output NetCDF file |
---|
[1130] | 89 | if (is_master) then |
---|
| 90 | ierr=NF_CREATE(filename,NF_CLOBBER,nid) |
---|
| 91 | if (ierr.ne.NF_NOERR) then |
---|
[38] | 92 | write(*,*)'writediagsoil: Error, failed creating file '//trim(filename) |
---|
| 93 | stop |
---|
[1130] | 94 | endif |
---|
| 95 | endif ! of if (is_master) |
---|
| 96 | |
---|
[38] | 97 | ! Define dimensions and axis attributes |
---|
[1047] | 98 | call iniwritesoil(nid,ngrid) |
---|
[38] | 99 | |
---|
| 100 | ! set zitau to -1 to be compatible with zitau incrementation step below |
---|
| 101 | zitau=-1 |
---|
| 102 | |
---|
| 103 | else |
---|
| 104 | ! If not an initialization call, simply open the NetCDF file |
---|
[1130] | 105 | if (is_master) then |
---|
| 106 | ierr=NF_OPEN(filename,NF_WRITE,nid) |
---|
| 107 | endif |
---|
[38] | 108 | endif ! of if (firstname.eq."1234567890") |
---|
| 109 | |
---|
| 110 | ! 2. Increment local time counter, if necessary |
---|
| 111 | if (name.eq.firstname) then |
---|
| 112 | ! if we run across 'firstname', then it is a new time step |
---|
| 113 | zitau=zitau+iphysiq |
---|
| 114 | ! Note iphysiq is known from control.h |
---|
| 115 | endif |
---|
| 116 | |
---|
| 117 | ! 3. Write data, if the time index matches the sample rate |
---|
| 118 | if (mod(zitau+1,isample).eq.0) then |
---|
| 119 | |
---|
| 120 | ! 3.1 If first call at this date, update 'time' variable |
---|
| 121 | if (name.eq.firstname) then |
---|
| 122 | ntime=ntime+1 |
---|
[1130] | 123 | date=float(zitau+1)/float(day_step) |
---|
[38] | 124 | ! Note: day_step is known from control.h |
---|
| 125 | |
---|
[1130] | 126 | if (is_master) then |
---|
| 127 | ! Get NetCDF ID for "time" |
---|
| 128 | ierr=NF_INQ_VARID(nid,"time",varid) |
---|
| 129 | ! Add the current value of date to the "time" array |
---|
[38] | 130 | !#ifdef NC_DOUBLE |
---|
[1130] | 131 | ! ierr=NF_PUT_VARA_DOUBLE(nid,varid,ntime,1,date) |
---|
[38] | 132 | !#else |
---|
[1130] | 133 | ierr=NF_PUT_VARA_REAL(nid,varid,ntime,1,date) |
---|
[38] | 134 | !#endif |
---|
[1130] | 135 | if (ierr.ne.NF_NOERR) then |
---|
[38] | 136 | write(*,*)"writediagsoil: Failed writing date to time variable" |
---|
| 137 | stop |
---|
[1130] | 138 | endif |
---|
| 139 | endif ! of if (is_master) |
---|
[38] | 140 | endif ! of if (name.eq.firstname) |
---|
| 141 | |
---|
| 142 | ! 3.2 Write the variable to the NetCDF file |
---|
| 143 | if (dimpx.eq.3) then ! Case of a 3D variable |
---|
| 144 | ! A. Recast data along 'dynamics' grid |
---|
[1130] | 145 | #ifdef CPP_PARA |
---|
| 146 | ! gather field on a "global" (without redundant longitude) array |
---|
| 147 | call Gather(px,dx3_glop) |
---|
| 148 | !$OMP MASTER |
---|
| 149 | if (is_mpi_root) then |
---|
| 150 | call Grid1Dto2D_glo(dx3_glop,dx3_glo) |
---|
| 151 | ! copy dx3_glo() to dx3(:) and add redundant longitude |
---|
| 152 | data3(1:iim,:,:)=dx3_glo(1:iim,:,:) |
---|
| 153 | data3(iip1,:,:)=data3(1,:,:) |
---|
| 154 | endif |
---|
| 155 | !$OMP END MASTER |
---|
| 156 | !$OMP BARRIER |
---|
| 157 | #else |
---|
[38] | 158 | do l=1,nsoilmx |
---|
| 159 | ! handle the poles |
---|
| 160 | do i=1,iip1 |
---|
| 161 | data3(i,1,l)=px(1,l) |
---|
| 162 | data3(i,jjp1,l)=px(ngrid,l) |
---|
| 163 | enddo |
---|
| 164 | ! rest of the grid |
---|
| 165 | do j=2,jjm |
---|
| 166 | ig0=1+(j-2)*iim |
---|
| 167 | do i=1,iim |
---|
| 168 | data3(i,j,l)=px(ig0+i,l) |
---|
| 169 | enddo |
---|
| 170 | data3(iip1,j,l)=data3(1,j,l) ! extra (modulo) longitude |
---|
| 171 | enddo |
---|
| 172 | enddo |
---|
[1130] | 173 | #endif |
---|
[38] | 174 | |
---|
| 175 | ! B. Write (append) the variable to the NetCDF file |
---|
[1130] | 176 | if (is_master) then |
---|
[38] | 177 | ! B.1. Get the ID of the variable |
---|
| 178 | ierr=NF_INQ_VARID(nid,name,varid) |
---|
| 179 | if (ierr.ne.NF_NOERR) then |
---|
| 180 | ! If we failed geting the variable's ID, we assume it is because |
---|
| 181 | ! the variable doesn't exist yet and must be created. |
---|
| 182 | ! Start by obtaining corresponding dimensions IDs |
---|
| 183 | ierr=NF_INQ_DIMID(nid,"longitude",id(1)) |
---|
| 184 | ierr=NF_INQ_DIMID(nid,"latitude",id(2)) |
---|
| 185 | ierr=NF_INQ_DIMID(nid,"depth",id(3)) |
---|
| 186 | ierr=NF_INQ_DIMID(nid,"time",id(4)) |
---|
| 187 | ! Tell the world about it |
---|
| 188 | write(*,*) "=====================" |
---|
| 189 | write(*,*) "writediagsoil: creating variable "//trim(name) |
---|
| 190 | call def_var(nid,name,title,units,4,id,varid,ierr) |
---|
| 191 | endif ! of if (ierr.ne.NF_NOERR) |
---|
| 192 | |
---|
| 193 | ! B.2. Prepare things to be able to write/append the variable |
---|
| 194 | corners(1)=1 |
---|
| 195 | corners(2)=1 |
---|
| 196 | corners(3)=1 |
---|
| 197 | corners(4)=ntime |
---|
| 198 | |
---|
| 199 | edges(1)=iip1 |
---|
| 200 | edges(2)=jjp1 |
---|
| 201 | edges(3)=nsoilmx |
---|
| 202 | edges(4)=1 |
---|
| 203 | |
---|
| 204 | ! B.3. Write the slab of data |
---|
| 205 | !#ifdef NC_DOUBLE |
---|
| 206 | ! ierr=NF_PUT_VARA_DOUBLE(nid,varid,corners,edges,data3) |
---|
| 207 | !#else |
---|
| 208 | ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data3) |
---|
| 209 | !#endif |
---|
| 210 | if (ierr.ne.NF_NOERR) then |
---|
| 211 | write(*,*) "writediagsoil: Error: Failed writing "//trim(name)//& |
---|
| 212 | " to file "//trim(filename)//" at time",date |
---|
| 213 | endif |
---|
[1130] | 214 | endif ! of if (is_master) |
---|
[38] | 215 | |
---|
| 216 | elseif (dimpx.eq.2) then ! Case of a 2D variable |
---|
[1130] | 217 | |
---|
[38] | 218 | ! A. Recast data along 'dynamics' grid |
---|
[1130] | 219 | #ifdef CPP_PARA |
---|
| 220 | ! gather field on a "global" (without redundant longitude) array |
---|
| 221 | px2(:)=px(:,1) |
---|
| 222 | call Gather(px2,dx2_glop) |
---|
| 223 | !$OMP MASTER |
---|
| 224 | if (is_mpi_root) then |
---|
| 225 | call Grid1Dto2D_glo(dx2_glop,dx2_glo) |
---|
| 226 | ! copy dx3_glo() to dx3(:) and add redundant longitude |
---|
| 227 | data2(1:iim,:)=dx2_glo(1:iim,:) |
---|
| 228 | data2(iip1,:)=data2(1,:) |
---|
| 229 | endif |
---|
| 230 | !$OMP END MASTER |
---|
| 231 | !$OMP BARRIER |
---|
| 232 | #else |
---|
[38] | 233 | ! handle the poles |
---|
| 234 | do i=1,iip1 |
---|
| 235 | data2(i,1)=px(1,1) |
---|
| 236 | data2(i,jjp1)=px(ngrid,1) |
---|
| 237 | enddo |
---|
| 238 | ! rest of the grid |
---|
| 239 | do j=2,jjm |
---|
| 240 | ig0=1+(j-2)*iim |
---|
| 241 | do i=1,iim |
---|
| 242 | data2(i,j)=px(ig0+i,1) |
---|
| 243 | enddo |
---|
| 244 | data2(iip1,j)=data2(1,j) ! extra (modulo) longitude |
---|
| 245 | enddo |
---|
[1130] | 246 | #endif |
---|
[38] | 247 | |
---|
| 248 | ! B. Write (append) the variable to the NetCDF file |
---|
[1130] | 249 | if (is_master) then |
---|
[38] | 250 | ! B.1. Get the ID of the variable |
---|
| 251 | ierr=NF_INQ_VARID(nid,name,varid) |
---|
| 252 | if (ierr.ne.NF_NOERR) then |
---|
| 253 | ! If we failed geting the variable's ID, we assume it is because |
---|
| 254 | ! the variable doesn't exist yet and must be created. |
---|
| 255 | ! Start by obtaining corresponding dimensions IDs |
---|
| 256 | ierr=NF_INQ_DIMID(nid,"longitude",id(1)) |
---|
| 257 | ierr=NF_INQ_DIMID(nid,"latitude",id(2)) |
---|
| 258 | ierr=NF_INQ_DIMID(nid,"time",id(3)) |
---|
| 259 | ! Tell the world about it |
---|
| 260 | write(*,*) "=====================" |
---|
| 261 | write(*,*) "writediagsoil: creating variable "//trim(name) |
---|
| 262 | call def_var(nid,name,title,units,3,id,varid,ierr) |
---|
| 263 | endif ! of if (ierr.ne.NF_NOERR) |
---|
| 264 | |
---|
| 265 | ! B.2. Prepare things to be able to write/append the variable |
---|
| 266 | corners(1)=1 |
---|
| 267 | corners(2)=1 |
---|
| 268 | corners(3)=ntime |
---|
| 269 | |
---|
| 270 | edges(1)=iip1 |
---|
| 271 | edges(2)=jjp1 |
---|
| 272 | edges(3)=1 |
---|
| 273 | |
---|
| 274 | ! B.3. Write the slab of data |
---|
| 275 | !#ifdef NC_DOUBLE |
---|
| 276 | ! ierr=NF_PUT_VARA_DOUBLE(nid,varid,corners,edges,data2) |
---|
| 277 | !#else |
---|
| 278 | ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data2) |
---|
| 279 | !#endif |
---|
| 280 | if (ierr.ne.NF_NOERR) then |
---|
| 281 | write(*,*) "writediagsoil: Error: Failed writing "//trim(name)//& |
---|
| 282 | " to file "//trim(filename)//" at time",date |
---|
| 283 | endif |
---|
[1130] | 284 | endif ! of if (is_master) |
---|
[38] | 285 | |
---|
| 286 | elseif (dimpx.eq.0) then ! Case of a 0D variable |
---|
[1130] | 287 | #ifdef CPP_PARA |
---|
| 288 | write(*,*) "writediagsoil: dimps==0 case not implemented in // mode!!" |
---|
| 289 | stop |
---|
| 290 | #endif |
---|
[38] | 291 | ! A. Copy data value |
---|
| 292 | data0=px(1,1) |
---|
| 293 | |
---|
| 294 | ! B. Write (append) the variable to the NetCDF file |
---|
| 295 | ! B.1. Get the ID of the variable |
---|
| 296 | ierr=NF_INQ_VARID(nid,name,varid) |
---|
| 297 | if (ierr.ne.NF_NOERR) then |
---|
| 298 | ! If we failed geting the variable's ID, we assume it is because |
---|
| 299 | ! the variable doesn't exist yet and must be created. |
---|
| 300 | ! Start by obtaining corresponding dimensions IDs |
---|
| 301 | ierr=NF_INQ_DIMID(nid,"time",id(1)) |
---|
| 302 | ! Tell the world about it |
---|
| 303 | write(*,*) "=====================" |
---|
| 304 | write(*,*) "writediagsoil: creating variable "//trim(name) |
---|
| 305 | call def_var(nid,name,title,units,1,id,varid,ierr) |
---|
| 306 | endif ! of if (ierr.ne.NF_NOERR) |
---|
| 307 | |
---|
| 308 | ! B.2. Prepare things to be able to write/append the variable |
---|
| 309 | corners(1)=ntime |
---|
| 310 | |
---|
| 311 | edges(1)=1 |
---|
| 312 | |
---|
| 313 | ! B.3. Write the data |
---|
| 314 | !#ifdef NC_DOUBLE |
---|
| 315 | ! ierr=NF_PUT_VARA_DOUBLE(nid,varid,corners,edges,data0) |
---|
| 316 | !#else |
---|
| 317 | ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data0) |
---|
| 318 | !#endif |
---|
| 319 | if (ierr.ne.NF_NOERR) then |
---|
| 320 | write(*,*) "writediagsoil: Error: Failed writing "//trim(name)//& |
---|
| 321 | " to file "//trim(filename)//" at time",date |
---|
| 322 | endif |
---|
| 323 | |
---|
| 324 | endif ! of if (dimpx.eq.3) elseif (dimpx.eq.2) ... |
---|
| 325 | endif ! of if (mod(zitau+1,isample).eq.0) |
---|
| 326 | |
---|
| 327 | ! 4. Close the NetCDF file |
---|
[1130] | 328 | if (is_master) then |
---|
| 329 | ierr=NF_CLOSE(nid) |
---|
| 330 | endif |
---|
[38] | 331 | |
---|
| 332 | end subroutine writediagsoil |
---|