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