[3344] | 1 | module iniwritesoil_mod |
---|
[38] | 2 | |
---|
[3344] | 3 | implicit none |
---|
| 4 | |
---|
| 5 | contains |
---|
| 6 | |
---|
| 7 | subroutine iniwritesoil(nid,ngrid,inertia,area,nbplon,nbplat,nsoilmx,mlayer) |
---|
| 8 | |
---|
[38] | 9 | ! initialization routine for 'writediagoil'. Here we create/define |
---|
| 10 | ! dimensions (longitude, latitude, depth and time) and other fixed |
---|
| 11 | ! (time-independent) parameters. |
---|
| 12 | |
---|
[1528] | 13 | USE comcstfi_h, only: pi |
---|
| 14 | USE regular_lonlat_mod, ONLY: lon_reg, lat_reg |
---|
| 15 | use mod_grid_phy_lmdz, ONLY: nbp_lon, nbp_lat |
---|
[1047] | 16 | |
---|
[38] | 17 | implicit none |
---|
| 18 | |
---|
[1528] | 19 | include"netcdf.inc" |
---|
[38] | 20 | |
---|
| 21 | ! Arguments: |
---|
[3344] | 22 | integer, intent(in) :: ngrid |
---|
| 23 | integer, intent(in) :: nid ! NetCDF output file ID |
---|
| 24 | integer, intent(in) :: nbplon, nbplat ! sizes of area |
---|
| 25 | integer, intent(in) :: nsoilmx ! number of subterranean layers |
---|
| 26 | real, intent(in) :: inertia(nbplon,nbplat,nsoilmx) |
---|
| 27 | real, intent(in) :: area(nbplon,nbp_lat) ! mesh area (m2) |
---|
| 28 | real, intent(in) :: mlayer(nsoilmx) ! soil mid-layer depths |
---|
[38] | 29 | |
---|
| 30 | ! Local variables: |
---|
| 31 | |
---|
| 32 | ! NetCDF stuff: |
---|
| 33 | integer :: ierr ! NetCDF routines return code |
---|
| 34 | integer :: idim_rlatu ! ID of the 'latitude' dimension |
---|
| 35 | integer :: idim_rlonv ! ID of the 'longitude' dimension |
---|
| 36 | integer :: idim_depth ! ID of the 'depth' dimension |
---|
| 37 | integer :: idim_time ! ID of the 'time' dimension |
---|
| 38 | integer :: varid ! to store NetCDF ID of a variable |
---|
| 39 | integer,dimension(3) :: dimids ! to store IDs of dimensions of a variable |
---|
| 40 | character(len=60) :: text ! to store some text |
---|
[1528] | 41 | real,dimension(nbp_lon+1,nbp_lat,nsoilmx) :: data3 ! to store 3D data |
---|
[38] | 42 | integer :: i,j,l,ig0 |
---|
[1532] | 43 | real,allocatable :: lon_reg_ext(:) ! extended longitudes |
---|
[38] | 44 | |
---|
[1532] | 45 | |
---|
| 46 | if (nbp_lon*nbp_lat==1) then |
---|
| 47 | ! 1D model |
---|
| 48 | allocate(lon_reg_ext(1)) |
---|
| 49 | else |
---|
| 50 | ! 3D model |
---|
| 51 | allocate(lon_reg_ext(nbp_lon+1)) |
---|
| 52 | endif |
---|
| 53 | |
---|
[38] | 54 | ! 1. Define the dimensions |
---|
| 55 | ! Switch to NetCDF define mode |
---|
| 56 | ierr=NF_REDEF(nid) |
---|
| 57 | |
---|
| 58 | ! Define the dimensions |
---|
[1532] | 59 | if (nbp_lon*nbp_lat==1) then |
---|
| 60 | ierr=NF_DEF_DIM(nid,"longitude",1,idim_rlonv) |
---|
| 61 | else |
---|
| 62 | ierr=NF_DEF_DIM(nid,"longitude",nbp_lon+1,idim_rlonv) |
---|
| 63 | endif |
---|
[38] | 64 | if (ierr.ne.NF_NOERR) then |
---|
| 65 | write(*,*)"iniwritesoil: Error, could not define longitude dimension" |
---|
| 66 | endif |
---|
[1528] | 67 | ierr=NF_DEF_DIM(nid,"latitude",nbp_lat,idim_rlatu) |
---|
[38] | 68 | if (ierr.ne.NF_NOERR) then |
---|
| 69 | write(*,*)"iniwritesoil: Error, could not define latitude dimension" |
---|
| 70 | endif |
---|
| 71 | ierr=NF_DEF_DIM(nid,"depth",nsoilmx,idim_depth) |
---|
| 72 | if (ierr.ne.NF_NOERR) then |
---|
| 73 | write(*,*)"iniwritesoil: Error, could not define depth dimension" |
---|
| 74 | endif |
---|
| 75 | ierr=NF_DEF_DIM(nid,"time",NF_UNLIMITED,idim_time) |
---|
| 76 | if (ierr.ne.NF_NOERR) then |
---|
| 77 | write(*,*)"iniwritesoil: Error, could not define time dimension" |
---|
| 78 | endif |
---|
| 79 | |
---|
| 80 | ! Switch out of NetCDF define mode |
---|
| 81 | ierr=NF_ENDDEF(nid) |
---|
| 82 | |
---|
| 83 | ! 2. Define (as variables) and write dimensions, as well as their attributes |
---|
| 84 | ! 2.1. Longitude |
---|
| 85 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
| 86 | |
---|
| 87 | ! Define the variable |
---|
| 88 | #ifdef NC_DOUBLE |
---|
[2573] | 89 | ierr=NF_DEF_VAR(nid,"longitude",NF_DOUBLE,1,[idim_rlonv],varid) |
---|
[38] | 90 | #else |
---|
[2573] | 91 | ierr=NF_DEF_VAR(nid,"longitude",NF_FLOAT,1,[idim_rlonv],varid) |
---|
[38] | 92 | #endif |
---|
| 93 | if (ierr.ne.NF_NOERR) then |
---|
| 94 | write(*,*)"iniwritesoil: Error, could not define longitude variable" |
---|
| 95 | endif |
---|
| 96 | |
---|
| 97 | ! Longitude attributes |
---|
| 98 | text="East longitude" |
---|
| 99 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
| 100 | text="degrees_east" |
---|
| 101 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
| 102 | |
---|
[1528] | 103 | lon_reg_ext(1:nbp_lon)=lon_reg(1:nbp_lon) |
---|
[1532] | 104 | if (nbp_lon*nbp_lat/=1) then ! in 3D only: |
---|
| 105 | ! add extra redundant point (180 degrees, since lon_reg starts at -180 |
---|
| 106 | lon_reg_ext(nbp_lon+1)=-lon_reg_ext(1) |
---|
| 107 | endif |
---|
[1528] | 108 | |
---|
[38] | 109 | ! Write longitude to file |
---|
| 110 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
| 111 | ! Write |
---|
| 112 | #ifdef NC_DOUBLE |
---|
[1528] | 113 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,lon_reg_ext*(180./pi)) |
---|
[38] | 114 | #else |
---|
[1528] | 115 | ierr=NF_PUT_VAR_REAL(nid,varid,lon_reg_ext*(180./pi)) |
---|
[38] | 116 | #endif |
---|
| 117 | ! Note: rlonv is known from comgeom.h and pi from comcstfi.h |
---|
| 118 | if (ierr.ne.NF_NOERR) then |
---|
| 119 | write(*,*)"iniwritesoil: Error, could not write longitude variable" |
---|
| 120 | endif |
---|
| 121 | |
---|
| 122 | ! 2.2. Latitude |
---|
| 123 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
| 124 | |
---|
| 125 | ! Define the variable |
---|
| 126 | #ifdef NC_DOUBLE |
---|
[2573] | 127 | ierr=NF_DEF_VAR(nid,"latitude",NF_DOUBLE,1,[idim_rlatu],varid) |
---|
[38] | 128 | #else |
---|
[2573] | 129 | ierr=NF_DEF_VAR(nid,"latitude",NF_FLOAT,1,[idim_rlatu],varid) |
---|
[38] | 130 | #endif |
---|
| 131 | if (ierr.ne.NF_NOERR) then |
---|
| 132 | write(*,*)"iniwritesoil: Error, could not define latitude variable" |
---|
| 133 | endif |
---|
| 134 | |
---|
| 135 | ! Latitude attributes |
---|
| 136 | text="North latitude" |
---|
| 137 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
| 138 | text="degrees_north" |
---|
| 139 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
| 140 | |
---|
| 141 | ! Write latitude to file |
---|
| 142 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
| 143 | ! Write |
---|
| 144 | #ifdef NC_DOUBLE |
---|
[1528] | 145 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,lat_reg*(180./pi)) |
---|
[38] | 146 | #else |
---|
[1528] | 147 | ierr=NF_PUT_VAR_REAL(nid,varid,lat_reg*(180./pi)) |
---|
[38] | 148 | #endif |
---|
| 149 | if (ierr.ne.NF_NOERR) then |
---|
| 150 | write(*,*)"iniwritesoil: Error, could not write longitude variable" |
---|
| 151 | endif |
---|
| 152 | |
---|
| 153 | ! 2.3. Depth |
---|
| 154 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
| 155 | |
---|
| 156 | ! Define the variable |
---|
| 157 | #ifdef NC_DOUBLE |
---|
[2573] | 158 | ierr=NF_DEF_VAR(nid,"depth",NF_DOUBLE,1,[idim_depth],varid) |
---|
[38] | 159 | #else |
---|
[2573] | 160 | ierr=NF_DEF_VAR(nid,"depth",NF_FLOAT,1,[idim_depth],varid) |
---|
[38] | 161 | #endif |
---|
| 162 | if (ierr.ne.NF_NOERR) then |
---|
| 163 | write(*,*)"iniwritesoil: Error, could not define depth variable" |
---|
| 164 | endif |
---|
| 165 | |
---|
| 166 | ! Depth attributes |
---|
| 167 | text="Soil mid-layer depth" |
---|
| 168 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
| 169 | text="m" |
---|
| 170 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
| 171 | text="down" |
---|
| 172 | ierr=NF_PUT_ATT_TEXT(nid,varid,"positive",len_trim(text),text) |
---|
| 173 | |
---|
| 174 | ! Write depth to file |
---|
| 175 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
| 176 | ! Write |
---|
| 177 | #ifdef NC_DOUBLE |
---|
| 178 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,mlayer) |
---|
| 179 | #else |
---|
| 180 | ierr=NF_PUT_VAR_REAL(nid,varid,mlayer) |
---|
| 181 | #endif |
---|
| 182 | if (ierr.ne.NF_NOERR) then |
---|
| 183 | write(*,*)"iniwritesoil: Error, could not write depth variable" |
---|
| 184 | endif |
---|
| 185 | |
---|
| 186 | ! 2.4. Time |
---|
| 187 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
| 188 | |
---|
| 189 | ! Define the variable |
---|
| 190 | #ifdef NC_DOUBLE |
---|
[2573] | 191 | ierr=NF_DEF_VAR(nid,"time",NF_DOUBLE,1,[idim_time],varid) |
---|
[38] | 192 | #else |
---|
[2573] | 193 | ierr=NF_DEF_VAR(nid,"time",NF_FLOAT,1,[idim_time],varid) |
---|
[38] | 194 | #endif |
---|
| 195 | if (ierr.ne.NF_NOERR) then |
---|
| 196 | write(*,*)"iniwritesoil: Error, could not define depth variable" |
---|
| 197 | endif |
---|
| 198 | |
---|
| 199 | ! time attributes |
---|
| 200 | text="Time" |
---|
| 201 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
[3536] | 202 | text="days since 0000-00-00 00:00:00" |
---|
[38] | 203 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
| 204 | |
---|
| 205 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
| 206 | ! Note no need to write time variable here; it is done in writediagsoil. |
---|
| 207 | |
---|
| 208 | ! 3. Other variables to be included |
---|
| 209 | |
---|
| 210 | ! 3.1 mesh area surrounding each horizontal point |
---|
| 211 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
| 212 | |
---|
| 213 | ! Define the variable |
---|
| 214 | dimids(1)=idim_rlonv ! ID of the 'longitude' dimension |
---|
| 215 | dimids(2)=idim_rlatu ! ID of the 'latitude' dimension |
---|
| 216 | #ifdef NC_DOUBLE |
---|
| 217 | ierr=NF_DEF_VAR(nid,"area",NF_DOUBLE,2,dimids,varid) |
---|
| 218 | #else |
---|
| 219 | ierr=NF_DEF_VAR(nid,"area",NF_FLOAT,2,dimids,varid) |
---|
| 220 | #endif |
---|
| 221 | if (ierr.ne.NF_NOERR) then |
---|
| 222 | write(*,*)"iniwritesoil: Error, could not define area variable" |
---|
| 223 | endif |
---|
| 224 | |
---|
| 225 | ! Area attributes |
---|
| 226 | text="Mesh area" |
---|
| 227 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
| 228 | text="m2" |
---|
| 229 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
| 230 | |
---|
| 231 | ! Write area to file |
---|
| 232 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
| 233 | ! Write |
---|
| 234 | #ifdef NC_DOUBLE |
---|
[1528] | 235 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,area) |
---|
[38] | 236 | #else |
---|
[1528] | 237 | ierr=NF_PUT_VAR_REAL(nid,varid,area) |
---|
[38] | 238 | #endif |
---|
| 239 | if (ierr.ne.NF_NOERR) then |
---|
| 240 | write(*,*)"iniwritesoil: Error, could not write area variable" |
---|
| 241 | endif |
---|
| 242 | |
---|
| 243 | ! 3.2 Thermal inertia |
---|
| 244 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
| 245 | |
---|
| 246 | ! Define the variable |
---|
| 247 | dimids(1)=idim_rlonv ! ID of the 'longitude' dimension |
---|
| 248 | dimids(2)=idim_rlatu ! ID of the 'latitude' dimension |
---|
| 249 | dimids(3)=idim_depth ! ID of the 'depth' dimension |
---|
| 250 | #ifdef NC_DOUBLE |
---|
| 251 | ierr=NF_DEF_VAR(nid,"th_inertia",NF_DOUBLE,3,dimids,varid) |
---|
| 252 | #else |
---|
| 253 | ierr=NF_DEF_VAR(nid,"th_inertia",NF_FLOAT,3,dimids,varid) |
---|
| 254 | #endif |
---|
| 255 | if (ierr.ne.NF_NOERR) then |
---|
| 256 | write(*,*)"iniwritesoil: Error, could not define th_inertia variable" |
---|
| 257 | endif |
---|
| 258 | |
---|
| 259 | ! Attributes |
---|
| 260 | text="Thermal inertia" |
---|
| 261 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
| 262 | text="J.s-1/2.m-2.K-1" |
---|
| 263 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
| 264 | |
---|
| 265 | ! Write data2 to file |
---|
| 266 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
| 267 | ! Write |
---|
| 268 | #ifdef NC_DOUBLE |
---|
[1528] | 269 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,inertia) |
---|
[38] | 270 | #else |
---|
[1528] | 271 | ierr=NF_PUT_VAR_REAL(nid,varid,inertia) |
---|
[38] | 272 | #endif |
---|
| 273 | if (ierr.ne.NF_NOERR) then |
---|
| 274 | write(*,*)"iniwritesoil: Error, could not write th_inertia variable" |
---|
| 275 | endif |
---|
| 276 | |
---|
| 277 | end subroutine iniwritesoil |
---|
[3344] | 278 | |
---|
| 279 | end module iniwritesoil_mod |
---|