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