[137] | 1 | program concatnc |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | ! ******************************************************** |
---|
| 5 | ! Program to concatenate data from Netcdf "diagfi" files, |
---|
| 6 | ! outputs from the martian GCM |
---|
| 7 | ! input : diagfi.nc / concat.nc / stats.nc kind of files |
---|
| 8 | ! author: Y. Wanherdrick |
---|
| 9 | ! + aps(), bps() and phisinit() are now also written |
---|
| 10 | ! to output file (E. Millour, september 2006) |
---|
| 11 | ! if available (F. Forget, october 2006) |
---|
[280] | 12 | ! + handle 1D data (EM, January 2007) |
---|
[137] | 13 | ! + ap(), bp() (F. Forget, February 2008) |
---|
[280] | 14 | ! + handle the possibility that number of GCM layers (aps, bps |
---|
| 15 | ! or sigma) may be different from number of vertical levels |
---|
| 16 | ! of data (which is the case for outputs from 'zrecast') |
---|
| 17 | ! (EM, April 2010) |
---|
[397] | 18 | ! + handle absence of ap() and bp() if aps and bps are available |
---|
| 19 | ! (case of stats file) FF, November 2011 |
---|
[137] | 20 | ! ******************************************************** |
---|
| 21 | |
---|
| 22 | implicit none |
---|
| 23 | |
---|
| 24 | include "netcdf.inc" ! NetCDF definitions |
---|
| 25 | |
---|
[410] | 26 | character (len=80), dimension(1000) :: file |
---|
[137] | 27 | ! file(): input file(s) names(s) |
---|
| 28 | character (len=30), dimension(15) :: notconcat |
---|
| 29 | ! notconcat(): names of the (15) variables that won't be concatenated |
---|
| 30 | character (len=50), dimension(:), allocatable :: var |
---|
| 31 | ! var(): name(s) of variable(s) that will be concatenated |
---|
| 32 | character (len=50) :: tmpvar,tmpfile,title,units |
---|
| 33 | ! tmpvar(): used to temporarily store a variable name |
---|
| 34 | ! tmpfile(): used to temporarily store a file name |
---|
| 35 | ! title(): [netcdf] title attribute |
---|
| 36 | ! units(): [netcdf] units attribute |
---|
| 37 | character (len=100) :: filename,vartmp |
---|
| 38 | ! filename(): output file name |
---|
| 39 | ! vartmp(): temporary variable name (read from netcdf input file) |
---|
| 40 | !character (len=1) :: ccopy |
---|
| 41 | ! ccpy: 'y' or 'n' answer |
---|
| 42 | character (len=4) :: axis |
---|
| 43 | ! axis: "ls" or "sol" |
---|
| 44 | integer :: nid,ierr,miss |
---|
| 45 | ! nid: [netcdf] file ID # |
---|
| 46 | ! ierr: [netcdf] subroutine returned error code |
---|
| 47 | ! miss: [netcdf] subroutine returned error code |
---|
| 48 | integer :: i,j,k,inter |
---|
| 49 | ! for various loops |
---|
| 50 | integer :: varid |
---|
| 51 | ! varid: [netcdf] variable ID # |
---|
[280] | 52 | integer :: memolatlen=0,memolonlen=0,memoaltlen=0 |
---|
[137] | 53 | ! memolatlen: # of elements of lat(), read from the first input file |
---|
| 54 | ! memolonlen: # of elements of lon(), read from the first input file |
---|
| 55 | ! memoaltlen: # of elements of alt(), read from the first input file |
---|
| 56 | real, dimension(:), allocatable:: lat,lon,alt,time |
---|
| 57 | ! lat(): array, stores latitude coordinates |
---|
| 58 | ! lon(): array, stores longitude coordinates |
---|
| 59 | ! alt(): array, stores altitude coordinates |
---|
| 60 | ! time(): array, stores time coordinates |
---|
| 61 | integer :: nbvar,nbfile,nbvarfile,ndim |
---|
| 62 | ! nbvar: # of variables to concatenate |
---|
| 63 | ! nbfile: # number of input file(s) |
---|
| 64 | ! nbvarfile: total # of variables in an input file |
---|
| 65 | ! ndim: [netcdf] # (3 or 4) of dimensions (for variables) |
---|
| 66 | integer :: latdim,londim,altdim,timedim |
---|
| 67 | ! latdim: [netcdf] "latitude" dim ID |
---|
| 68 | ! londim: [netcdf] "longitude" dim ID |
---|
| 69 | ! altdim: [netcdf] "altdim" dim ID |
---|
| 70 | ! timedim: [netcdf] "timedim" dim ID |
---|
[280] | 71 | integer :: gcmlayerdim ! NetCDF dimension ID for # of layers in GCM |
---|
[137] | 72 | integer :: latvar,lonvar,altvar,timevar |
---|
| 73 | ! latvar: [netcdf] ID of "latitude" variable |
---|
| 74 | ! lonvar: [netcdf] ID of "longitude" variable |
---|
| 75 | ! altvar: [netcdf] ID of "altitude" variable |
---|
| 76 | ! timevar: [netcdf] ID of "Time" variable |
---|
| 77 | integer :: latlen,lonlen,altlen,timelen |
---|
| 78 | ! latlen: # of elements of lat() array |
---|
| 79 | ! lonlen: # of elements of lon() array |
---|
| 80 | ! altvar: # of elements of alt() array |
---|
| 81 | ! timelen: # of elemnets of time() array |
---|
[280] | 82 | integer :: GCM_layers ! number of GCM atmospheric layers (may not be |
---|
| 83 | ! same as altlen if file is an output of zrecast) |
---|
| 84 | integer :: nout,latdimout,londimout,altdimout,timedimout,timevarout |
---|
[137] | 85 | ! nout: [netcdf] output file ID |
---|
| 86 | ! latdimout: [netcdf] output latitude (dimension) ID |
---|
| 87 | ! londimout: [netcdf] output longitude (dimension) ID |
---|
| 88 | ! altdimout: [netcdf] output altitude (dimension) ID |
---|
| 89 | ! timedimout: [netcdf] output time (dimension) ID |
---|
| 90 | ! timevarout: [netcdf] ID of output "Time" variable |
---|
[280] | 91 | integer :: layerdimout ! NetCDF dimension ID for # of layers in GCM |
---|
| 92 | integer :: interlayerdimout ! dimension ID for # of interlayers in GCM |
---|
[137] | 93 | integer :: reptime,rep,varidout |
---|
| 94 | ! reptime: total length of concatenated time() arrays |
---|
| 95 | ! rep: # number of elements of a time() array to write to the output file |
---|
| 96 | ! varidout: [netcdf] variable ID # (of a variable to write to the output file) |
---|
| 97 | integer :: Nnotconcat,var_ok |
---|
| 98 | ! Nnotconcat: # of (leading)variables that won't be concatenated |
---|
| 99 | ! var_ok: flag (0 or 1) |
---|
| 100 | integer, dimension(4) :: corner,edges,dim |
---|
| 101 | ! corner: [netcdf] |
---|
| 102 | ! edges: [netcdf] |
---|
| 103 | ! dim: [netcdf] |
---|
| 104 | real, dimension(:,:,:,:), allocatable :: var3d |
---|
| 105 | ! var3D(,,,): 4D array to store a field |
---|
| 106 | real :: memotime |
---|
| 107 | ! memotime: (cumulative) time value, in martian days (sols) |
---|
| 108 | real :: missing |
---|
| 109 | !PARAMETER(missing=1E+20) |
---|
| 110 | ! missing: [netcdf] to handle "missing" values when reading/writing files |
---|
| 111 | real, dimension(2) :: valid_range |
---|
| 112 | ! valid_range(2): [netcdf] interval in which a value is considered valid |
---|
| 113 | |
---|
| 114 | !============================================================================== |
---|
| 115 | ! 1.1. Get input file name(s) |
---|
| 116 | !============================================================================== |
---|
| 117 | write(*,*) |
---|
| 118 | write(*,*) "-- Program written specifically for NetCDF 'diagfi' files from the martian GCM --" |
---|
| 119 | write(*,*) |
---|
| 120 | write(*,*) "which files do you want to use?" |
---|
| 121 | write(*,*) "<Enter> when list ok" |
---|
| 122 | |
---|
| 123 | nbfile=0 |
---|
| 124 | read(*,'(a50)') tmpfile |
---|
| 125 | do While (len_trim(tmpfile).ne.0) |
---|
| 126 | nbfile=nbfile+1 |
---|
| 127 | file(nbfile)=tmpfile |
---|
| 128 | read(*,'(a50)') tmpfile |
---|
| 129 | enddo |
---|
| 130 | |
---|
| 131 | if(nbfile==0) then |
---|
| 132 | write(*,*) "no file... game over" |
---|
| 133 | stop "" |
---|
| 134 | endif |
---|
| 135 | |
---|
| 136 | !============================================================================== |
---|
| 137 | ! 1.2. Ask for starting day value (memotime) |
---|
| 138 | !============================================================================== |
---|
| 139 | |
---|
| 140 | write(*,*) |
---|
| 141 | !write(*,*) "Beginning day of the first specified file?" |
---|
| 142 | write(*,*) "Starting day of the run stored in the first input file?" |
---|
| 143 | write(*,*) "(e.g.: 100 if that run started at time=100 sols)" |
---|
| 144 | read(*,*) memotime |
---|
| 145 | |
---|
| 146 | !============================================================================== |
---|
| 147 | ! 1.3. Open the first input file |
---|
| 148 | !============================================================================== |
---|
| 149 | |
---|
| 150 | ierr = NF_OPEN(file(1),NF_NOWRITE,nid) |
---|
| 151 | if (ierr.NE.NF_NOERR) then |
---|
| 152 | write(*,*) 'ERROR: Pb opening file '//file(1) |
---|
| 153 | stop "" |
---|
| 154 | endif |
---|
| 155 | |
---|
| 156 | ierr=NF_INQ_NVARS(nid,nbvarfile) |
---|
| 157 | ! nbvarfile now set to be the (total) number of variables in file |
---|
| 158 | |
---|
| 159 | !============================================================================== |
---|
| 160 | ! 1.4. Ask for (output) "Time" axis type |
---|
| 161 | !============================================================================== |
---|
| 162 | |
---|
| 163 | write(*,*) "Warning: to read the result with grads, choose sol" |
---|
| 164 | write(*,*) "Warning: use ferret to read the non linear scale ls" |
---|
| 165 | write(*,*) "Which time axis should be given in the output? (sol/ls)" |
---|
| 166 | read(*,*) axis |
---|
| 167 | ! loop as long as axis is neither "sol" nor "ls" |
---|
| 168 | do while ((axis/="sol").AND.(axis/="ls")) |
---|
| 169 | read(*,*) axis |
---|
| 170 | enddo |
---|
| 171 | |
---|
| 172 | ! The following variables don't need to be concatenated |
---|
| 173 | notconcat(1)='Time' |
---|
| 174 | notconcat(2)='controle' |
---|
| 175 | notconcat(3)='rlonu' |
---|
| 176 | notconcat(4)='latitude' |
---|
| 177 | notconcat(5)='longitude' |
---|
| 178 | notconcat(6)='altitude' |
---|
| 179 | notconcat(7)='rlatv' |
---|
| 180 | notconcat(8)='aps' |
---|
| 181 | notconcat(9)='bps' |
---|
| 182 | notconcat(10)='ap' |
---|
| 183 | notconcat(11)='bp' |
---|
| 184 | notconcat(12)='cu' |
---|
| 185 | notconcat(13)='cv' |
---|
| 186 | notconcat(14)='aire' |
---|
| 187 | notconcat(15)='phisinit' |
---|
| 188 | |
---|
| 189 | !============================================================================== |
---|
| 190 | ! 1.5. Get (and check) list of variables to concatenate |
---|
| 191 | !============================================================================== |
---|
| 192 | write(*,*) |
---|
| 193 | Nnotconcat=0 |
---|
| 194 | do i=1,nbvarfile |
---|
| 195 | ierr=NF_INQ_VARNAME(nid,i,vartmp) |
---|
| 196 | ! vartmp now contains the "name" of variable of ID # i |
---|
| 197 | var_ok=0 |
---|
| 198 | do inter=1,15 |
---|
| 199 | if (vartmp.eq.notconcat(inter)) then |
---|
| 200 | var_ok=1 |
---|
| 201 | Nnotconcat=Nnotconcat+1 |
---|
| 202 | endif |
---|
| 203 | enddo |
---|
| 204 | if (var_ok.eq.0) write(*,*) trim(vartmp) |
---|
| 205 | enddo |
---|
| 206 | |
---|
| 207 | ! Nnotconcat: # of variables that won't be concatenated |
---|
| 208 | ! nbvarfile: total # of variables in file |
---|
| 209 | allocate(var(nbvarfile-Nnotconcat)) |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | write(*,*) |
---|
| 213 | write(*,*) "which variables do you want to concatenate?" |
---|
| 214 | write(*,*) "all / list of <variables> (separated by <Enter>s)" |
---|
| 215 | write(*,*) "(an empty line , i.e: just <Enter>, implies end of list)" |
---|
| 216 | nbvar=0 |
---|
| 217 | read(*,'(a50)') tmpvar |
---|
| 218 | do while ((tmpvar/=' ').AND.(trim(tmpvar)/='all')) |
---|
| 219 | nbvar=nbvar+1 |
---|
| 220 | var(nbvar)=tmpvar |
---|
| 221 | read(*,'(a50)') tmpvar |
---|
| 222 | enddo |
---|
| 223 | |
---|
| 224 | if (tmpvar=="all") then |
---|
| 225 | if (axis=="ls") then |
---|
| 226 | ! write(*,*) "Do you want to keep the original file? (y/n)" |
---|
| 227 | ! read(*,*) ccopy |
---|
| 228 | ! if ((ccopy=="n").or.(ccopy=="N")) then |
---|
| 229 | ! do i=1,nbfile |
---|
| 230 | ! ierr=NF_CLOSE(nid) |
---|
| 231 | ! ierr = NF_OPEN(file(1),NF_WRITE,nid) |
---|
| 232 | ! call change_time_axis(nid,ierr) |
---|
| 233 | ! ierr=NF_CLOSE(nid) |
---|
| 234 | ! STOP "" |
---|
| 235 | ! enddo |
---|
| 236 | ! else |
---|
| 237 | nbvar=nbvarfile-Nnotconcat |
---|
| 238 | do j=Nnotconcat+1,nbvarfile |
---|
| 239 | ierr=nf_inq_varname(nid,j,var(j-Nnotconcat)) |
---|
| 240 | enddo |
---|
| 241 | ! endif ! of if ((ccopy=="n").or.(ccopy=="N")) |
---|
| 242 | endif ! of if (axis=="ls") |
---|
| 243 | ! Variables names from the file are catched |
---|
| 244 | nbvar=nbvarfile-Nnotconcat |
---|
| 245 | do i=1,nbvar |
---|
| 246 | ierr=nf_inq_varname(nid,i+Nnotconcat,var(i)) |
---|
| 247 | write(*,'(a9,1x,i2,1x,a1,1x,a50)') "variable ",i,":",var(i) |
---|
| 248 | enddo |
---|
| 249 | else if(nbvar==0) then |
---|
| 250 | write(*,*) "no variable... game over" |
---|
| 251 | stop "" |
---|
| 252 | endif ! of if (tmpvar=="all") |
---|
| 253 | |
---|
| 254 | ! Name of the new file |
---|
| 255 | !========================================================== |
---|
| 256 | !filename=var(1) |
---|
| 257 | !do i=2, nbvar |
---|
| 258 | ! filename=trim(adjustl(filename))//"_"//var(i) |
---|
| 259 | !enddo |
---|
| 260 | !filename=trim(adjustl(filename))//".nc" |
---|
| 261 | |
---|
| 262 | !============================================================================== |
---|
| 263 | ! 1.6. Get output file name |
---|
| 264 | !============================================================================== |
---|
| 265 | filename="concat.nc" |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | !============================================================================== |
---|
| 269 | ! 2. Concatenate input file(s) into output file |
---|
| 270 | !============================================================================== |
---|
| 271 | |
---|
| 272 | reptime=0 |
---|
| 273 | |
---|
| 274 | do i=1,nbfile |
---|
| 275 | |
---|
| 276 | !============================================================================== |
---|
| 277 | ! 2.1. Open input file |
---|
| 278 | !============================================================================== |
---|
| 279 | |
---|
| 280 | if (i/=1) then |
---|
| 281 | write(*,*) |
---|
| 282 | write(*,*) "opening "//trim(file(i))//"..." |
---|
| 283 | ierr = NF_OPEN(file(i),NF_NOWRITE,nid) |
---|
| 284 | if (ierr.NE.NF_NOERR) then |
---|
| 285 | write(*,*) 'ERROR: Pb opening file '//file(i) |
---|
| 286 | write(*,*) NF_STRERROR(ierr) |
---|
| 287 | stop "" |
---|
| 288 | endif |
---|
| 289 | endif |
---|
| 290 | |
---|
| 291 | !============================================================================== |
---|
| 292 | ! 2.2. Read (and check) dimensions of variables from input file |
---|
| 293 | !============================================================================== |
---|
| 294 | |
---|
| 295 | ierr=NF_INQ_DIMID(nid,"latitude",latdim) |
---|
| 296 | ierr=NF_INQ_VARID(nid,"latitude",latvar) |
---|
| 297 | if (ierr.NE.NF_NOERR) then |
---|
| 298 | write(*,*) 'ERROR: Field <latitude> is missing in file'//file(i) |
---|
| 299 | stop "" |
---|
| 300 | endif |
---|
| 301 | ierr=NF_INQ_DIMLEN(nid,latdim,latlen) |
---|
| 302 | ! write(*,*) "latlen: ",latlen |
---|
| 303 | |
---|
| 304 | ierr=NF_INQ_DIMID(nid,"longitude",londim) |
---|
| 305 | ierr=NF_INQ_VARID(nid,"longitude",lonvar) |
---|
| 306 | if (ierr.NE.NF_NOERR) then |
---|
| 307 | write(*,*) 'ERROR: Field <longitude> is missing in file'//file(i) |
---|
| 308 | stop "" |
---|
| 309 | endif |
---|
| 310 | ierr=NF_INQ_DIMLEN(nid,londim,lonlen) |
---|
| 311 | ! write(*,*) "lonlen: ",lonlen |
---|
| 312 | |
---|
| 313 | ierr=NF_INQ_DIMID(nid,"altitude",altdim) |
---|
| 314 | ierr=NF_INQ_VARID(nid,"altitude",altvar) |
---|
| 315 | if (ierr.NE.NF_NOERR) then |
---|
| 316 | write(*,*) 'ERROR: Field <altitude> is missing in file'//file(i) |
---|
| 317 | stop "" |
---|
| 318 | endif |
---|
| 319 | ierr=NF_INQ_DIMLEN(nid,altdim,altlen) |
---|
| 320 | ! write(*,*) "altlen: ",altlen |
---|
| 321 | |
---|
[280] | 322 | ! load size of aps() or sigma() (in case it is not altlen) |
---|
| 323 | ! default is that GCM_layers=altlen |
---|
| 324 | ! but for outputs of zrecast, it may be a different value |
---|
| 325 | ierr=NF_INQ_DIMID(nid,"GCM_layers",gcmlayerdim) |
---|
| 326 | if (ierr.ne.NF_NOERR) then |
---|
| 327 | ! didn't find a GCM_layers dimension; therefore we have: |
---|
| 328 | GCM_layers=altlen |
---|
| 329 | else |
---|
| 330 | ! load value of GCM_layers |
---|
| 331 | ierr=NF_INQ_DIMLEN(nid,gcmlayerdim,GCM_layers) |
---|
| 332 | endif |
---|
| 333 | ! write(*,*) "GCM_layers=",GCM_layers |
---|
| 334 | |
---|
[137] | 335 | !============================================================================== |
---|
| 336 | ! 2.3. Read (and check compatibility of) dimensions of |
---|
| 337 | ! variables from input file |
---|
| 338 | !============================================================================== |
---|
| 339 | |
---|
| 340 | if (i==1) then ! First call; initialize/allocate |
---|
| 341 | memolatlen=latlen |
---|
| 342 | memolonlen=lonlen |
---|
| 343 | memoaltlen=altlen |
---|
| 344 | allocate(lat(latlen)) |
---|
| 345 | allocate(lon(lonlen)) |
---|
| 346 | allocate(alt(altlen)) |
---|
| 347 | #ifdef NC_DOUBLE |
---|
| 348 | ierr = NF_GET_VAR_DOUBLE(nid,latvar,lat) |
---|
| 349 | ierr = NF_GET_VAR_DOUBLE(nid,lonvar,lon) |
---|
| 350 | ierr = NF_GET_VAR_DOUBLE(nid,altvar,alt) |
---|
| 351 | #else |
---|
| 352 | ierr = NF_GET_VAR_REAL(nid,latvar,lat) |
---|
| 353 | ierr = NF_GET_VAR_REAL(nid,lonvar,lon) |
---|
| 354 | ierr = NF_GET_VAR_REAL(nid,altvar,alt) |
---|
| 355 | #endif |
---|
| 356 | ! Initialize output file's lat,lon,alt and time dimensions |
---|
[280] | 357 | call initiate (filename,lat,lon,alt,GCM_layers,nout,& |
---|
| 358 | latdimout,londimout,altdimout,timedimout,& |
---|
| 359 | layerdimout,interlayerdimout,timevarout) |
---|
[137] | 360 | ! Initialize output file's aps,bps,ap,bp and phisinit variables |
---|
[280] | 361 | call init2(nid,lonlen,latlen,altlen,GCM_layers,& |
---|
| 362 | nout,londimout,latdimout,altdimout,& |
---|
| 363 | layerdimout,interlayerdimout) |
---|
[137] | 364 | |
---|
| 365 | else ! Not a first call, |
---|
| 366 | ! Check that latitude,longitude and altitude of current input file |
---|
| 367 | ! are identical to those of the output file |
---|
| 368 | if (memolatlen/=latlen) then |
---|
| 369 | write(*,*) "ERROR: Not the same latitude axis" |
---|
| 370 | stop "" |
---|
| 371 | else if (memolonlen/=lonlen) then |
---|
| 372 | write(*,*) "ERROR: Not the same longitude axis" |
---|
| 373 | stop "" |
---|
| 374 | else if (memoaltlen/=altlen) then |
---|
| 375 | write(*,*) "ERROR: Not the same altitude axis" |
---|
| 376 | stop "" |
---|
| 377 | endif |
---|
| 378 | endif ! of if (i==1) |
---|
| 379 | |
---|
| 380 | !============================================================================== |
---|
| 381 | ! 2.4. Handle "Time" dimension from input file |
---|
| 382 | !============================================================================== |
---|
| 383 | |
---|
| 384 | !============================================================================== |
---|
| 385 | ! 2.4.1 Read "Time" dimension from input file |
---|
| 386 | !============================================================================== |
---|
| 387 | ierr=NF_INQ_DIMID(nid,"Time",timedim) |
---|
| 388 | if (ierr.NE.NF_NOERR) then |
---|
| 389 | write(*,*) 'ERROR: Dimension <Time> is missing in file'//file(i) |
---|
| 390 | stop "" |
---|
| 391 | endif |
---|
| 392 | ierr=NF_INQ_VARID(nid,"Time",timevar) |
---|
| 393 | if (ierr.NE.NF_NOERR) then |
---|
| 394 | write(*,*) 'ERROR: Field <Time> is missing in file'//file(i) |
---|
| 395 | stop "" |
---|
| 396 | endif |
---|
| 397 | ierr=NF_INQ_DIMLEN(nid,timedim,timelen) |
---|
| 398 | ! write(*,*) "timelen: ",timelen |
---|
| 399 | |
---|
| 400 | ! allocate time() array and fill it with values from input file |
---|
| 401 | allocate(time(timelen)) |
---|
| 402 | |
---|
| 403 | #ifdef NC_DOUBLE |
---|
| 404 | ierr = NF_GET_VAR_DOUBLE(nid,timevar,time) |
---|
| 405 | #else |
---|
| 406 | ierr = NF_GET_VAR_REAL(nid,timevar,time) |
---|
| 407 | #endif |
---|
| 408 | |
---|
| 409 | !============================================================================== |
---|
| 410 | ! 2.4.2 Write/extend "Time" dimension/values in output file |
---|
| 411 | !============================================================================== |
---|
| 412 | |
---|
| 413 | rep=0 |
---|
| 414 | write(*,*) |
---|
| 415 | write(*,'(a3,1x,f6.1)') "Sol",memotime |
---|
| 416 | |
---|
| 417 | ! Add (memotime) offset and write "concatenated" time values to output file |
---|
| 418 | do k=reptime+1,reptime+timelen |
---|
| 419 | rep=rep+1 |
---|
| 420 | #ifdef NC_DOUBLE |
---|
| 421 | ierr= NF_PUT_VARA_DOUBLE(nout,timevarout,k,1,memotime+time(rep)) |
---|
| 422 | #else |
---|
| 423 | ierr= NF_PUT_VARA_REAL(nout,timevarout,k,1,memotime+time(rep)) |
---|
| 424 | #endif |
---|
| 425 | enddo |
---|
| 426 | ! Compute new time offset (for further concatenations) |
---|
| 427 | memotime=memotime+time(timelen) |
---|
| 428 | |
---|
| 429 | !============================================================================== |
---|
| 430 | ! 2.5 Read/write variables |
---|
| 431 | !============================================================================== |
---|
| 432 | |
---|
| 433 | do j=1,nbvar ! loop on variables to read/write |
---|
| 434 | |
---|
| 435 | !============================================================================== |
---|
| 436 | ! 2.5.1 Check that variable to be read existe in input file |
---|
| 437 | !============================================================================== |
---|
| 438 | |
---|
| 439 | write(*,*) "variable ",var(j) |
---|
| 440 | ierr=nf_inq_varid(nid,var(j),varid) |
---|
| 441 | if (ierr.NE.NF_NOERR) then |
---|
| 442 | write(*,*) 'ERROR: Field <',var(j),'> not found in file'//file(i) |
---|
| 443 | stop "" |
---|
| 444 | endif |
---|
| 445 | ierr=nf_inq_varndims(nid,varid,ndim) |
---|
| 446 | |
---|
| 447 | !============================================================================== |
---|
| 448 | ! 2.5.2 Prepare things in order to read/write the variable |
---|
| 449 | !============================================================================== |
---|
| 450 | |
---|
| 451 | ! build dim(),corner() and edges() arrays |
---|
| 452 | ! and allocate var3d() array |
---|
[280] | 453 | if (ndim==1) then |
---|
| 454 | allocate(var3d(timelen,1,1,1)) |
---|
| 455 | dim(1)=timedimout |
---|
| 456 | |
---|
| 457 | ! start indexes (where data values will be written) |
---|
| 458 | corner(1)=reptime+1 |
---|
| 459 | corner(2)=1 |
---|
| 460 | corner(3)=1 |
---|
| 461 | corner(4)=1 |
---|
| 462 | |
---|
| 463 | ! length (along dimensions) of block of data to be written |
---|
| 464 | edges(1)=timelen |
---|
| 465 | edges(2)=1 |
---|
| 466 | edges(3)=1 |
---|
| 467 | edges(4)=1 |
---|
| 468 | |
---|
| 469 | else if (ndim==3) then |
---|
[137] | 470 | allocate(var3d(lonlen,latlen,timelen,1)) |
---|
| 471 | dim(1)=londimout |
---|
| 472 | dim(2)=latdimout |
---|
| 473 | dim(3)=timedimout |
---|
| 474 | |
---|
| 475 | ! start indexes (where data values will be written) |
---|
| 476 | corner(1)=1 |
---|
| 477 | corner(2)=1 |
---|
| 478 | corner(3)=reptime+1 |
---|
| 479 | corner(4)=1 |
---|
| 480 | |
---|
| 481 | ! length (along dimensions) of block of data to be written |
---|
| 482 | edges(1)=lonlen |
---|
| 483 | edges(2)=latlen |
---|
| 484 | edges(3)=timelen |
---|
| 485 | edges(4)=1 |
---|
| 486 | |
---|
| 487 | else if (ndim==4) then |
---|
| 488 | allocate(var3d(lonlen,latlen,altlen,timelen)) |
---|
| 489 | dim(1)=londimout |
---|
| 490 | dim(2)=latdimout |
---|
| 491 | dim(3)=altdimout |
---|
| 492 | dim(4)=timedimout |
---|
| 493 | |
---|
| 494 | ! start indexes (where data values will be written) |
---|
| 495 | corner(1)=1 |
---|
| 496 | corner(2)=1 |
---|
| 497 | corner(3)=1 |
---|
| 498 | corner(4)=reptime+1 |
---|
| 499 | |
---|
| 500 | ! length (along dimensions) of block of data to be written |
---|
| 501 | edges(1)=lonlen |
---|
| 502 | edges(2)=latlen |
---|
| 503 | edges(3)=altlen |
---|
| 504 | edges(4)=timelen |
---|
| 505 | endif |
---|
| 506 | |
---|
| 507 | if (i==1) then ! First call: write some definitions to output file |
---|
| 508 | units=" " |
---|
| 509 | title=" " |
---|
| 510 | ierr=nf_get_att_text(nid,varid,"title",title) |
---|
| 511 | ierr=nf_get_att_text(nid,varid,"units",units) |
---|
| 512 | call def_var(nout,var(j),title,units,ndim,dim,varidout,ierr) |
---|
| 513 | else |
---|
| 514 | ierr=NF_INQ_VARID(nout,var(j),varidout) |
---|
| 515 | endif |
---|
| 516 | |
---|
| 517 | !============================================================================== |
---|
| 518 | ! 2.5.3 Read from input file and write (append) to the output file |
---|
| 519 | !============================================================================== |
---|
| 520 | |
---|
| 521 | #ifdef NC_DOUBLE |
---|
| 522 | ierr = NF_GET_VAR_DOUBLE(nid,varid,var3d) |
---|
| 523 | ierr= NF_PUT_VARA_DOUBLE(nout,varidout,corner,edges,var3d) |
---|
| 524 | miss=NF_GET_ATT_DOUBLE(nid,varid,"missing_value",missing) |
---|
| 525 | miss=NF_GET_ATT_DOUBLE(nid,varid,"valid_range",valid_range) |
---|
| 526 | #else |
---|
| 527 | ierr = NF_GET_VAR_REAL(nid,varid,var3d) |
---|
| 528 | ierr= NF_PUT_VARA_REAL(nout,varidout,corner,edges,var3d) |
---|
| 529 | miss=NF_GET_ATT_REAL(nid,varid,"missing_value",missing) |
---|
| 530 | miss=NF_GET_ATT_REAL(nid,varid,"valid_range",valid_range) |
---|
| 531 | #endif |
---|
| 532 | |
---|
| 533 | if (ierr.ne.NF_NOERR) then |
---|
| 534 | write(*,*) 'PUT_VAR ERROR: ',NF_STRERROR(ierr) |
---|
| 535 | stop "" |
---|
| 536 | endif |
---|
| 537 | |
---|
| 538 | ! In case there is a "valid_range" attribute |
---|
| 539 | ! Write "valid_range" and "missing_value" attributes in output file |
---|
| 540 | if (miss.eq.NF_NOERR) then |
---|
| 541 | call missing_value(nout,varidout,valid_range,missing) |
---|
| 542 | endif |
---|
| 543 | |
---|
| 544 | ! free var3d() array |
---|
| 545 | deallocate(var3d) |
---|
| 546 | |
---|
| 547 | enddo ! of do j=1,nbvar |
---|
| 548 | |
---|
| 549 | ! Free time() and compute/store array length (for further concatenations) |
---|
| 550 | deallocate(time) |
---|
| 551 | reptime=reptime+timelen |
---|
| 552 | |
---|
| 553 | ! Close input file |
---|
| 554 | ierr=nf_close(nid) |
---|
| 555 | |
---|
| 556 | enddo ! of i=1,nbfile |
---|
| 557 | |
---|
| 558 | !============================================================================== |
---|
| 559 | ! 3. If required, change time axis (from sols to Ls) |
---|
| 560 | !============================================================================== |
---|
| 561 | |
---|
| 562 | if (axis=="ls") then |
---|
| 563 | call change_time_axis(nout,ierr) |
---|
| 564 | endif |
---|
| 565 | |
---|
| 566 | ! Close output file |
---|
| 567 | ierr=NF_CLOSE(nout) |
---|
| 568 | |
---|
| 569 | contains |
---|
| 570 | |
---|
| 571 | !****************************************************************************** |
---|
[280] | 572 | subroutine initiate (filename,lat,lon,alt,GCM_layers,nout,& |
---|
| 573 | latdimout,londimout,altdimout,timedimout,& |
---|
| 574 | layerdimout,interlayerdimout,timevarout) |
---|
[137] | 575 | !============================================================================== |
---|
| 576 | ! Purpose: |
---|
| 577 | ! Create and initialize a data file (NetCDF format) |
---|
| 578 | !============================================================================== |
---|
| 579 | ! Remarks: |
---|
| 580 | ! The NetCDF file (created in this subroutine) remains open |
---|
| 581 | !============================================================================== |
---|
| 582 | |
---|
| 583 | implicit none |
---|
| 584 | |
---|
| 585 | include "netcdf.inc" ! NetCDF definitions |
---|
| 586 | |
---|
| 587 | !============================================================================== |
---|
| 588 | ! Arguments: |
---|
| 589 | !============================================================================== |
---|
| 590 | character (len=*), intent(in):: filename |
---|
| 591 | ! filename(): the file's name |
---|
| 592 | real, dimension(:), intent(in):: lat |
---|
| 593 | ! lat(): latitude |
---|
| 594 | real, dimension(:), intent(in):: lon |
---|
| 595 | ! lon(): longitude |
---|
| 596 | real, dimension(:), intent(in):: alt |
---|
| 597 | ! alt(): altitude |
---|
[280] | 598 | integer,intent(in) :: GCM_layers ! number of GCM layers |
---|
[137] | 599 | integer, intent(out):: nout |
---|
| 600 | ! nout: [netcdf] file ID |
---|
| 601 | integer, intent(out):: latdimout |
---|
| 602 | ! latdimout: [netcdf] lat() (i.e.: latitude) ID |
---|
| 603 | integer, intent(out):: londimout |
---|
| 604 | ! londimout: [netcdf] lon() ID |
---|
| 605 | integer, intent(out):: altdimout |
---|
| 606 | ! altdimout: [netcdf] alt() ID |
---|
| 607 | integer, intent(out):: timedimout |
---|
| 608 | ! timedimout: [netcdf] "Time" ID |
---|
[280] | 609 | integer,intent(out) :: layerdimout |
---|
| 610 | ! layerdimout: [netcdf] "GCM_layers" ID |
---|
| 611 | integer,intent(out) :: interlayerdimout |
---|
| 612 | ! layerdimout: [netcdf] "GCM_layers+1" ID |
---|
[137] | 613 | integer, intent(out):: timevarout |
---|
| 614 | ! timevarout: [netcdf] "Time" (considered as a variable) ID |
---|
| 615 | |
---|
| 616 | !============================================================================== |
---|
| 617 | ! Local variables: |
---|
| 618 | !============================================================================== |
---|
| 619 | !integer :: latdim,londim,altdim,timedim |
---|
| 620 | integer :: nvarid,ierr |
---|
| 621 | ! nvarid: [netcdf] ID of a variable |
---|
| 622 | ! ierr: [netcdf] return error code (from called subroutines) |
---|
| 623 | |
---|
| 624 | !============================================================================== |
---|
| 625 | ! 1. Create (and open) output file |
---|
| 626 | !============================================================================== |
---|
| 627 | write(*,*) "creating "//trim(adjustl(filename))//'...' |
---|
[410] | 628 | ierr = NF_CREATE(filename,IOR(NF_CLOBBER,NF_64BIT_OFFSET),nout) |
---|
[137] | 629 | ! NB: setting NF_CLOBBER mode means that it's OK to overwrite an existing file |
---|
| 630 | if (ierr.NE.NF_NOERR) then |
---|
| 631 | WRITE(*,*)'ERROR: Impossible to create the file.' |
---|
| 632 | stop "" |
---|
| 633 | endif |
---|
| 634 | |
---|
| 635 | !============================================================================== |
---|
| 636 | ! 2. Define/write "dimensions" and get their IDs |
---|
| 637 | !============================================================================== |
---|
| 638 | |
---|
| 639 | ierr = NF_DEF_DIM(nout, "latitude", size(lat), latdimout) |
---|
| 640 | ierr = NF_DEF_DIM(nout, "longitude", size(lon), londimout) |
---|
| 641 | ierr = NF_DEF_DIM(nout, "altitude", size(alt), altdimout) |
---|
| 642 | ierr = NF_DEF_DIM(nout, "Time", NF_UNLIMITED, timedimout) |
---|
[280] | 643 | ierr = NF_DEF_DIM(nout, "GCM_layers", GCM_layers, layerdimout) |
---|
| 644 | ierr = NF_DEF_DIM(nout, "GCM_interlayers",GCM_layers+1,interlayerdimout) |
---|
[137] | 645 | |
---|
| 646 | ! End netcdf define mode |
---|
| 647 | ierr = NF_ENDDEF(nout) |
---|
| 648 | |
---|
| 649 | !============================================================================== |
---|
| 650 | ! 3. Write "Time" and its attributes |
---|
| 651 | !============================================================================== |
---|
| 652 | |
---|
| 653 | call def_var(nout,"Time","Time","years since 0000-00-0 00:00:00",1,& |
---|
| 654 | (/timedimout/),timevarout,ierr) |
---|
| 655 | |
---|
| 656 | !============================================================================== |
---|
| 657 | ! 4. Write "latitude" (data and attributes) |
---|
| 658 | !============================================================================== |
---|
| 659 | |
---|
| 660 | call def_var(nout,"latitude","latitude","degrees_north",1,& |
---|
| 661 | (/latdimout/),nvarid,ierr) |
---|
| 662 | |
---|
| 663 | #ifdef NC_DOUBLE |
---|
| 664 | ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lat) |
---|
| 665 | #else |
---|
| 666 | ierr = NF_PUT_VAR_REAL (nout,nvarid,lat) |
---|
| 667 | #endif |
---|
| 668 | |
---|
| 669 | !============================================================================== |
---|
| 670 | ! 4. Write "longitude" (data and attributes) |
---|
| 671 | !============================================================================== |
---|
| 672 | |
---|
| 673 | call def_var(nout,"longitude","East longitude","degrees_east",1,& |
---|
| 674 | (/londimout/),nvarid,ierr) |
---|
| 675 | |
---|
| 676 | #ifdef NC_DOUBLE |
---|
| 677 | ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lon) |
---|
| 678 | #else |
---|
| 679 | ierr = NF_PUT_VAR_REAL (nout,nvarid,lon) |
---|
| 680 | #endif |
---|
| 681 | |
---|
| 682 | !============================================================================== |
---|
| 683 | ! 4. Write "altitude" (data and attributes) |
---|
| 684 | !============================================================================== |
---|
| 685 | |
---|
| 686 | ! Switch to netcdf define mode |
---|
| 687 | ierr = NF_REDEF (nout) |
---|
| 688 | |
---|
| 689 | #ifdef NC_DOUBLE |
---|
| 690 | ierr = NF_DEF_VAR (nout,"altitude",NF_DOUBLE,1,altdimout,nvarid) |
---|
| 691 | #else |
---|
| 692 | ierr = NF_DEF_VAR (nout,"altitude",NF_FLOAT,1,altdimout,nvarid) |
---|
| 693 | #endif |
---|
| 694 | |
---|
| 695 | ierr = NF_PUT_ATT_TEXT (nout,nvarid,"long_name",8,"altitude") |
---|
| 696 | ierr = NF_PUT_ATT_TEXT (nout,nvarid,'units',2,"km") |
---|
| 697 | ierr = NF_PUT_ATT_TEXT (nout,nvarid,'positive',2,"up") |
---|
| 698 | |
---|
| 699 | ! End netcdf define mode |
---|
| 700 | ierr = NF_ENDDEF(nout) |
---|
| 701 | |
---|
| 702 | #ifdef NC_DOUBLE |
---|
| 703 | ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,alt) |
---|
| 704 | #else |
---|
| 705 | ierr = NF_PUT_VAR_REAL (nout,nvarid,alt) |
---|
| 706 | #endif |
---|
| 707 | |
---|
| 708 | end Subroutine initiate |
---|
| 709 | !****************************************************************************** |
---|
[280] | 710 | subroutine init2(infid,lonlen,latlen,altlen,GCM_layers, & |
---|
| 711 | outfid,londimout,latdimout,altdimout, & |
---|
| 712 | layerdimout,interlayerdimout) |
---|
[137] | 713 | !============================================================================== |
---|
| 714 | ! Purpose: |
---|
[632] | 715 | ! Copy ap() , bp(), aps(), bps(), aire() and phisinit() |
---|
| 716 | ! from input file to outpout file |
---|
[137] | 717 | !============================================================================== |
---|
| 718 | ! Remarks: |
---|
| 719 | ! The NetCDF files must be open |
---|
| 720 | !============================================================================== |
---|
| 721 | |
---|
| 722 | implicit none |
---|
| 723 | |
---|
| 724 | include "netcdf.inc" ! NetCDF definitions |
---|
| 725 | |
---|
| 726 | !============================================================================== |
---|
| 727 | ! Arguments: |
---|
| 728 | !============================================================================== |
---|
| 729 | integer, intent(in) :: infid ! NetCDF output file ID |
---|
| 730 | integer, intent(in) :: lonlen ! # of grid points along longitude |
---|
| 731 | integer, intent(in) :: latlen ! # of grid points along latitude |
---|
| 732 | integer, intent(in) :: altlen ! # of grid points along latitude |
---|
[280] | 733 | integer, intent(in) :: GCM_layers ! # of GCM atmospheric layers |
---|
[137] | 734 | integer, intent(in) :: outfid ! NetCDF output file ID |
---|
| 735 | integer, intent(in) :: londimout ! longitude dimension ID |
---|
| 736 | integer, intent(in) :: latdimout ! latitude dimension ID |
---|
| 737 | integer, intent(in) :: altdimout ! altitude dimension ID |
---|
[280] | 738 | integer, intent(in) :: layerdimout ! GCM_layers dimension ID |
---|
| 739 | integer, intent(in) :: interlayerdimout ! GCM_layers+1 dimension ID |
---|
[137] | 740 | !============================================================================== |
---|
| 741 | ! Local variables: |
---|
| 742 | !============================================================================== |
---|
| 743 | real,dimension(:),allocatable :: aps,bps ! hybrid vertical coordinates |
---|
| 744 | real,dimension(:),allocatable :: ap,bp ! hybrid vertical coordinates |
---|
[280] | 745 | real,dimension(:),allocatable :: sigma ! sigma levels |
---|
[632] | 746 | real,dimension(:,:),allocatable :: aire ! mesh areas |
---|
[137] | 747 | real,dimension(:,:),allocatable :: phisinit ! Ground geopotential |
---|
[280] | 748 | integer :: apsid,bpsid,sigmaid,phisinitid |
---|
[137] | 749 | integer :: apid,bpid |
---|
| 750 | integer :: ierr |
---|
| 751 | integer :: tmpvarid ! temporary variable ID |
---|
[632] | 752 | logical :: area ! is "aire" available ? |
---|
[137] | 753 | logical :: phis ! is "phisinit" available ? |
---|
[280] | 754 | logical :: hybrid ! are "aps" and "bps" available ? |
---|
[397] | 755 | logical :: apbp ! are "ap" and "bp" available ? |
---|
[137] | 756 | |
---|
| 757 | !============================================================================== |
---|
| 758 | ! 1. Read data from input file |
---|
| 759 | !============================================================================== |
---|
| 760 | |
---|
| 761 | ! hybrid coordinate aps |
---|
[280] | 762 | !write(*,*) "aps: altlen=",altlen," GCM_layers=",GCM_layers |
---|
| 763 | allocate(aps(GCM_layers),stat=ierr) |
---|
| 764 | if (ierr.ne.0) then |
---|
| 765 | write(*,*) "init2: failed to allocate aps!" |
---|
| 766 | stop |
---|
| 767 | endif |
---|
[137] | 768 | ierr=NF_INQ_VARID(infid,"aps",tmpvarid) |
---|
| 769 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 770 | write(*,*) "Ooops. Failed to get aps ID. OK, will look for sigma coord." |
---|
[137] | 771 | hybrid=.false. |
---|
| 772 | else |
---|
| 773 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,aps) |
---|
| 774 | hybrid=.true. |
---|
| 775 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 776 | stop "init2 Error: Failed reading aps" |
---|
[137] | 777 | endif |
---|
| 778 | |
---|
[280] | 779 | ! hybrid coordinate bps |
---|
| 780 | ! write(*,*) "bps: altlen=",altlen," GCM_layers=",GCM_layers |
---|
| 781 | allocate(bps(GCM_layers),stat=ierr) |
---|
| 782 | if (ierr.ne.0) then |
---|
| 783 | write(*,*) "init2: failed to allocate bps!" |
---|
| 784 | stop |
---|
| 785 | endif |
---|
| 786 | ierr=NF_INQ_VARID(infid,"bps",tmpvarid) |
---|
| 787 | if (ierr.ne.NF_NOERR) then |
---|
| 788 | stop "init2 Error: Failed to get bps ID." |
---|
| 789 | endif |
---|
[137] | 790 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,bps) |
---|
| 791 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 792 | stop "init2 Error: Failed reading bps" |
---|
[137] | 793 | endif |
---|
| 794 | endif |
---|
| 795 | |
---|
| 796 | ! hybrid coordinate ap |
---|
[280] | 797 | allocate(ap(GCM_layers+1),stat=ierr) |
---|
| 798 | if (ierr.ne.0) then |
---|
| 799 | write(*,*) "init2: failed to allocate ap!" |
---|
| 800 | stop |
---|
[137] | 801 | else |
---|
[280] | 802 | ierr=NF_INQ_VARID(infid,"ap",tmpvarid) |
---|
[137] | 803 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 804 | write(*,*) "Ooops. Failed to get ap ID. OK." |
---|
[397] | 805 | apbp=.false. |
---|
[280] | 806 | else |
---|
| 807 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,ap) |
---|
[397] | 808 | apbp=.true. |
---|
[280] | 809 | if (ierr.ne.NF_NOERR) then |
---|
| 810 | stop "Error: Failed reading ap" |
---|
| 811 | endif |
---|
[137] | 812 | endif |
---|
| 813 | endif |
---|
| 814 | |
---|
| 815 | ! hybrid coordinate bp |
---|
[280] | 816 | allocate(bp(GCM_layers+1),stat=ierr) |
---|
| 817 | if (ierr.ne.0) then |
---|
| 818 | write(*,*) "init2: failed to allocate bp!" |
---|
| 819 | stop |
---|
[137] | 820 | else |
---|
[280] | 821 | ierr=NF_INQ_VARID(infid,"bp",tmpvarid) |
---|
[137] | 822 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 823 | write(*,*) "Ooops. Failed to get bp ID. OK." |
---|
[397] | 824 | apbp=.false. |
---|
[280] | 825 | else |
---|
| 826 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,bp) |
---|
[397] | 827 | apbp=.true. |
---|
[280] | 828 | if (ierr.ne.NF_NOERR) then |
---|
| 829 | stop "Error: Failed reading bp" |
---|
| 830 | endif |
---|
[137] | 831 | endif |
---|
| 832 | endif |
---|
| 833 | |
---|
[280] | 834 | ! sigma levels (if any) |
---|
| 835 | if (.not.hybrid) then |
---|
| 836 | allocate(sigma(GCM_layers),stat=ierr) |
---|
| 837 | if (ierr.ne.0) then |
---|
| 838 | write(*,*) "init2: failed to allocate sigma" |
---|
| 839 | stop |
---|
| 840 | endif |
---|
| 841 | ierr=NF_INQ_VARID(infid,"sigma",tmpvarid) |
---|
| 842 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,sigma) |
---|
| 843 | if (ierr.ne.NF_NOERR) then |
---|
| 844 | stop "init2 Error: Failed reading sigma" |
---|
| 845 | endif |
---|
| 846 | endif ! of if (.not.hybrid) |
---|
| 847 | |
---|
[632] | 848 | ! mesh area |
---|
| 849 | allocate(aire(lonlen,latlen),stat=ierr) |
---|
| 850 | if (ierr.ne.0) then |
---|
| 851 | write(*,*) "init2: failed to allocate aire!" |
---|
| 852 | stop |
---|
| 853 | endif |
---|
| 854 | ierr=NF_INQ_VARID(infid,"aire",tmpvarid) |
---|
| 855 | if (ierr.ne.NF_NOERR) then |
---|
| 856 | write(*,*)"init2 warning: Failed to get aire ID." |
---|
| 857 | area = .false. |
---|
| 858 | else |
---|
| 859 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,aire) |
---|
| 860 | if (ierr.ne.NF_NOERR) then |
---|
| 861 | stop "init2 Error: Failed reading aire" |
---|
| 862 | endif |
---|
| 863 | area = .true. |
---|
| 864 | endif |
---|
| 865 | |
---|
[137] | 866 | ! ground geopotential phisinit |
---|
[280] | 867 | allocate(phisinit(lonlen,latlen),stat=ierr) |
---|
| 868 | if (ierr.ne.0) then |
---|
| 869 | write(*,*) "init2: failed to allocate phisinit!" |
---|
| 870 | stop |
---|
| 871 | endif |
---|
[137] | 872 | ierr=NF_INQ_VARID(infid,"phisinit",tmpvarid) |
---|
| 873 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 874 | write(*,*)"init2 warning: Failed to get phisinit ID." |
---|
[137] | 875 | phis = .false. |
---|
| 876 | else |
---|
| 877 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,phisinit) |
---|
| 878 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 879 | stop "init2 Error: Failed reading phisinit" |
---|
[137] | 880 | endif |
---|
| 881 | phis = .true. |
---|
| 882 | endif |
---|
| 883 | |
---|
| 884 | !============================================================================== |
---|
| 885 | ! 2. Write |
---|
| 886 | !============================================================================== |
---|
| 887 | |
---|
| 888 | !============================================================================== |
---|
[280] | 889 | ! 2.2. Hybrid coordinates ap() , bp(), aps() and bps() |
---|
[137] | 890 | !============================================================================== |
---|
| 891 | if(hybrid) then |
---|
| 892 | ! define aps |
---|
| 893 | call def_var(nout,"aps","hybrid pressure at midlayers"," ",1,& |
---|
[280] | 894 | (/layerdimout/),apsid,ierr) |
---|
[137] | 895 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 896 | stop "init2 Error: Failed to def_var aps" |
---|
[137] | 897 | endif |
---|
| 898 | |
---|
| 899 | ! write aps |
---|
| 900 | #ifdef NC_DOUBLE |
---|
| 901 | ierr=NF_PUT_VAR_DOUBLE(outfid,apsid,aps) |
---|
| 902 | #else |
---|
| 903 | ierr=NF_PUT_VAR_REAL(outfid,apsid,aps) |
---|
| 904 | #endif |
---|
| 905 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 906 | stop "init2 Error: Failed to write aps" |
---|
[137] | 907 | endif |
---|
| 908 | |
---|
| 909 | ! define bps |
---|
| 910 | call def_var(nout,"bps","hybrid sigma at midlayers"," ",1,& |
---|
[280] | 911 | (/layerdimout/),bpsid,ierr) |
---|
[137] | 912 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 913 | stop "init2 Error: Failed to def_var bps" |
---|
[137] | 914 | endif |
---|
| 915 | |
---|
| 916 | ! write bps |
---|
| 917 | #ifdef NC_DOUBLE |
---|
| 918 | ierr=NF_PUT_VAR_DOUBLE(outfid,bpsid,bps) |
---|
| 919 | #else |
---|
| 920 | ierr=NF_PUT_VAR_REAL(outfid,bpsid,bps) |
---|
| 921 | #endif |
---|
| 922 | if (ierr.ne.NF_NOERR) then |
---|
[280] | 923 | stop "init2 Error: Failed to write bps" |
---|
[137] | 924 | endif |
---|
| 925 | |
---|
[397] | 926 | if (apbp) then |
---|
| 927 | ! define ap |
---|
| 928 | call def_var(nout,"ap","hybrid sigma at interlayers"," ",1,& |
---|
[280] | 929 | (/interlayerdimout/),apid,ierr) |
---|
[397] | 930 | if (ierr.ne.NF_NOERR) then |
---|
| 931 | stop "Error: Failed to def_var ap" |
---|
| 932 | endif |
---|
[137] | 933 | |
---|
| 934 | ! write ap |
---|
| 935 | #ifdef NC_DOUBLE |
---|
[397] | 936 | ierr=NF_PUT_VAR_DOUBLE(outfid,apid,ap) |
---|
[137] | 937 | #else |
---|
[397] | 938 | ierr=NF_PUT_VAR_REAL(outfid,apid,ap) |
---|
[137] | 939 | #endif |
---|
[397] | 940 | if (ierr.ne.NF_NOERR) then |
---|
| 941 | stop "Error: Failed to write ap" |
---|
| 942 | endif |
---|
[137] | 943 | |
---|
| 944 | ! define bp |
---|
[397] | 945 | call def_var(nout,"bp","hybrid sigma at interlayers"," ",1,& |
---|
[280] | 946 | (/interlayerdimout/),bpid,ierr) |
---|
[397] | 947 | if (ierr.ne.NF_NOERR) then |
---|
| 948 | stop "Error: Failed to def_var bp" |
---|
| 949 | endif |
---|
[137] | 950 | |
---|
[397] | 951 | ! write bp |
---|
[137] | 952 | #ifdef NC_DOUBLE |
---|
[397] | 953 | ierr=NF_PUT_VAR_DOUBLE(outfid,bpid,bp) |
---|
[137] | 954 | #else |
---|
[397] | 955 | ierr=NF_PUT_VAR_REAL(outfid,bpid,bp) |
---|
[137] | 956 | #endif |
---|
[397] | 957 | if (ierr.ne.NF_NOERR) then |
---|
| 958 | stop "Error: Failed to write bp" |
---|
| 959 | endif |
---|
| 960 | endif ! of if (apbp) |
---|
[137] | 961 | |
---|
[280] | 962 | else |
---|
| 963 | ! define sigma |
---|
| 964 | call def_var(nout,"sigma","sigma at midlayers"," ",1,& |
---|
| 965 | (/layerdimout/),sigmaid,ierr) |
---|
| 966 | if (ierr.ne.NF_NOERR) then |
---|
| 967 | stop "init2 Error: Failed to def_var sigma" |
---|
| 968 | endif |
---|
| 969 | ! write sigma |
---|
| 970 | #ifdef NC_DOUBLE |
---|
| 971 | ierr=NF_PUT_VAR_DOUBLE(outfid,sigmaid,sigma) |
---|
| 972 | #else |
---|
| 973 | ierr=NF_PUT_VAR_REAL(outfid,sigmaid,sigma) |
---|
| 974 | #endif |
---|
| 975 | if (ierr.ne.NF_NOERR) then |
---|
| 976 | stop "init2 Error: Failed to write sigma" |
---|
| 977 | endif |
---|
| 978 | endif ! of if (hybrid) |
---|
| 979 | |
---|
[137] | 980 | !============================================================================== |
---|
[632] | 981 | ! 2.2. aire() and phisinit() |
---|
[137] | 982 | !============================================================================== |
---|
| 983 | |
---|
[632] | 984 | if (area) then |
---|
| 985 | ! define aire |
---|
| 986 | call def_var(nout,"aire","Mesh area","m2",2,& |
---|
| 987 | (/londimout,latdimout/),tmpvarid,ierr) |
---|
| 988 | if (ierr.ne.NF_NOERR) then |
---|
| 989 | stop "init2 Error: Failed to def_var aire" |
---|
| 990 | endif |
---|
| 991 | |
---|
| 992 | ! write aire |
---|
| 993 | #ifdef NC_DOUBLE |
---|
| 994 | ierr=NF_PUT_VAR_DOUBLE(outfid,tmpvarid,aire) |
---|
| 995 | #else |
---|
| 996 | ierr=NF_PUT_VAR_REAL(outfid,tmpvarid,aire) |
---|
| 997 | #endif |
---|
| 998 | if (ierr.ne.NF_NOERR) then |
---|
| 999 | stop "init2 Error: Failed to write aire" |
---|
| 1000 | endif |
---|
| 1001 | endif ! of if (area) |
---|
| 1002 | |
---|
[137] | 1003 | IF (phis) THEN |
---|
| 1004 | |
---|
[280] | 1005 | !define phisinit |
---|
| 1006 | call def_var(nout,"phisinit","Ground level geopotential"," ",2,& |
---|
[137] | 1007 | (/londimout,latdimout/),phisinitid,ierr) |
---|
[280] | 1008 | if (ierr.ne.NF_NOERR) then |
---|
| 1009 | stop "init2 Error: Failed to def_var phisinit" |
---|
| 1010 | endif |
---|
[137] | 1011 | |
---|
[280] | 1012 | ! write phisinit |
---|
[137] | 1013 | #ifdef NC_DOUBLE |
---|
[280] | 1014 | ierr=NF_PUT_VAR_DOUBLE(outfid,phisinitid,phisinit) |
---|
[137] | 1015 | #else |
---|
[280] | 1016 | ierr=NF_PUT_VAR_REAL(outfid,phisinitid,phisinit) |
---|
[137] | 1017 | #endif |
---|
[280] | 1018 | if (ierr.ne.NF_NOERR) then |
---|
| 1019 | stop "init2 Error: Failed to write phisinit" |
---|
| 1020 | endif |
---|
[137] | 1021 | |
---|
[280] | 1022 | ENDIF ! of IF (phis) |
---|
[137] | 1023 | |
---|
| 1024 | |
---|
| 1025 | ! Cleanup |
---|
[280] | 1026 | if (allocated(aps)) deallocate(aps) |
---|
| 1027 | if (allocated(bps)) deallocate(bps) |
---|
| 1028 | if (allocated(ap)) deallocate(ap) |
---|
| 1029 | if (allocated(bp)) deallocate(bp) |
---|
| 1030 | if (allocated(sigma)) deallocate(sigma) |
---|
| 1031 | if (allocated(phisinit)) deallocate(phisinit) |
---|
[632] | 1032 | if (allocated(aire)) deallocate(aire) |
---|
[137] | 1033 | |
---|
| 1034 | end subroutine init2 |
---|
| 1035 | !****************************************************************************** |
---|
| 1036 | subroutine def_var(nid,name,title,units,nbdim,dim,nvarid,ierr) |
---|
| 1037 | !============================================================================== |
---|
| 1038 | ! Purpose: Write a variable (i.e: add a variable to a dataset) |
---|
| 1039 | ! called "name"; along with its attributes "title", "units"... |
---|
| 1040 | ! to a file (following the NetCDF format) |
---|
| 1041 | !============================================================================== |
---|
| 1042 | ! Remarks: |
---|
| 1043 | ! The NetCDF file must be open |
---|
| 1044 | !============================================================================== |
---|
| 1045 | |
---|
| 1046 | implicit none |
---|
| 1047 | |
---|
| 1048 | include "netcdf.inc" ! NetCDF definitions |
---|
| 1049 | |
---|
| 1050 | !============================================================================== |
---|
| 1051 | ! Arguments: |
---|
| 1052 | !============================================================================== |
---|
| 1053 | integer, intent(in) :: nid |
---|
| 1054 | ! nid: [netcdf] file ID # |
---|
| 1055 | character (len=*), intent(in) :: name |
---|
| 1056 | ! name(): [netcdf] variable's name |
---|
| 1057 | character (len=*), intent(in) :: title |
---|
| 1058 | ! title(): [netcdf] variable's "title" attribute |
---|
| 1059 | character (len=*), intent(in) :: units |
---|
| 1060 | ! unit(): [netcdf] variable's "units" attribute |
---|
| 1061 | integer, intent(in) :: nbdim |
---|
| 1062 | ! nbdim: number of dimensions of the variable |
---|
| 1063 | integer, dimension(nbdim), intent(in) :: dim |
---|
| 1064 | ! dim(nbdim): [netcdf] dimension(s) ID(s) |
---|
| 1065 | integer, intent(out) :: nvarid |
---|
| 1066 | ! nvarid: [netcdf] ID # of the variable |
---|
| 1067 | integer, intent(out) :: ierr |
---|
| 1068 | ! ierr: [netcdf] subroutines returned error code |
---|
| 1069 | |
---|
| 1070 | ! Switch to netcdf define mode |
---|
| 1071 | ierr=NF_REDEF(nid) |
---|
| 1072 | |
---|
| 1073 | ! Insert the definition of the variable |
---|
| 1074 | #ifdef NC_DOUBLE |
---|
| 1075 | ierr=NF_DEF_VAR(nid,adjustl(name),NF_DOUBLE,nbdim,dim,nvarid) |
---|
| 1076 | #else |
---|
| 1077 | ierr=NF_DEF_VAR(nid,adjustl(name),NF_FLOAT,nbdim,dim,nvarid) |
---|
| 1078 | #endif |
---|
| 1079 | |
---|
| 1080 | ! Write the attributes |
---|
| 1081 | ierr=NF_PUT_ATT_TEXT(nid,nvarid,"title",len_trim(adjustl(title)),adjustl(title)) |
---|
| 1082 | ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",len_trim(adjustl(units)),adjustl(units)) |
---|
| 1083 | |
---|
| 1084 | ! End netcdf define mode |
---|
| 1085 | ierr=NF_ENDDEF(nid) |
---|
| 1086 | |
---|
| 1087 | end subroutine def_var |
---|
| 1088 | !****************************************************************************** |
---|
| 1089 | subroutine change_time_axis(nid,ierr) |
---|
| 1090 | !============================================================================== |
---|
| 1091 | ! Purpose: |
---|
| 1092 | ! Read "time" variable from a dataset, convert it from "sol" (martian |
---|
| 1093 | ! days) to "Ls" (solar longitude) and write the result back in the file |
---|
| 1094 | !============================================================================== |
---|
| 1095 | ! Remarks: |
---|
| 1096 | ! The NetCDF file must be opened before this subroutine is called |
---|
| 1097 | !============================================================================== |
---|
| 1098 | |
---|
| 1099 | implicit none |
---|
| 1100 | |
---|
| 1101 | include "netcdf.inc" ! NetCDF definitions |
---|
| 1102 | |
---|
| 1103 | !============================================================================== |
---|
| 1104 | ! Arguments: |
---|
| 1105 | !============================================================================== |
---|
| 1106 | integer, intent(in) :: nid |
---|
| 1107 | ! nid: [netcdf] file ID |
---|
| 1108 | integer, intent(out) :: ierr |
---|
| 1109 | ! ierr: [netcdf] return error code |
---|
| 1110 | |
---|
| 1111 | !============================================================================== |
---|
| 1112 | ! Local variables: |
---|
| 1113 | !============================================================================== |
---|
| 1114 | integer :: nvarid |
---|
| 1115 | ! nvarid: ID of the "Time" variable |
---|
| 1116 | integer :: timelen |
---|
| 1117 | ! timelen: size of the arrays |
---|
| 1118 | integer :: timedim |
---|
| 1119 | ! timedim: ID of the "Time" dimension |
---|
| 1120 | integer i |
---|
| 1121 | |
---|
| 1122 | real, dimension(:), allocatable :: time,ls |
---|
| 1123 | ! time(): time, given in sols |
---|
| 1124 | ! ls(): time, given in Ls (solar longitude) |
---|
| 1125 | |
---|
| 1126 | !============================================================================== |
---|
| 1127 | ! 1. Read |
---|
| 1128 | !============================================================================== |
---|
| 1129 | |
---|
| 1130 | ierr=NF_INQ_DIMID(nid,"Time",timedim) |
---|
| 1131 | ierr=NF_INQ_VARID(nid,"Time",nvarid) |
---|
| 1132 | if (ierr.NE.NF_NOERR) then |
---|
[322] | 1133 | write(*,*) 'ERROR in change_time_axis: Field <Time> not found' |
---|
[137] | 1134 | print*, NF_STRERROR(ierr) |
---|
| 1135 | stop "" |
---|
| 1136 | endif |
---|
| 1137 | |
---|
| 1138 | ierr=NF_INQ_DIMLEN(nid,timedim,timelen) |
---|
| 1139 | allocate(time(timelen),ls(timelen)) |
---|
| 1140 | #ifdef NC_DOUBLE |
---|
| 1141 | ierr = NF_GET_VAR_DOUBLE(nid,nvarid,time) |
---|
| 1142 | #else |
---|
| 1143 | ierr = NF_GET_VAR_REAL(nid,nvarid,time) |
---|
| 1144 | #endif |
---|
[322] | 1145 | if (ierr.ne.NF_NOERR) then |
---|
| 1146 | write(*,*) "ERROR in change_time_axis: Failed to load Time" |
---|
| 1147 | stop |
---|
| 1148 | endif |
---|
[137] | 1149 | |
---|
| 1150 | !============================================================================== |
---|
| 1151 | ! 2. Convert sols to Ls |
---|
| 1152 | !============================================================================== |
---|
| 1153 | |
---|
| 1154 | do i=1,timelen |
---|
| 1155 | call sol2ls(time(i),ls(i)) |
---|
| 1156 | enddo |
---|
| 1157 | |
---|
| 1158 | !============================================================================== |
---|
[322] | 1159 | ! 2.1 Check if there are not jumps in Ls (rounding problems in sol2ls) |
---|
[137] | 1160 | !============================================================================== |
---|
| 1161 | |
---|
[322] | 1162 | do i=1,timelen-1 |
---|
| 1163 | if ((ls(i+1)-ls(i)) > 350) then |
---|
| 1164 | write(*,*) "+ 360° Ls jump solved:", ls(i), ls(i+1), "at timestep", i |
---|
| 1165 | ls(i+1) = ls(i+1) - 360 |
---|
| 1166 | write(*,*) " corrected to now be:", ls(i), ls(i+1) |
---|
| 1167 | else if ((ls(i)-ls(i+1)) > 350) then |
---|
| 1168 | write(*,*) "- 360° Ls jump solved:", ls(i), ls(i+1), "at timestep", i |
---|
| 1169 | ls(i+1) = ls(i+1) + 360 |
---|
| 1170 | write(*,*) " corrected to now be:", ls(i), ls(i+1) |
---|
| 1171 | endif |
---|
| 1172 | enddo |
---|
| 1173 | |
---|
| 1174 | !============================================================================== |
---|
| 1175 | ! 3. Write |
---|
| 1176 | !============================================================================== |
---|
| 1177 | |
---|
[137] | 1178 | #ifdef NC_DOUBLE |
---|
| 1179 | ierr = NF_PUT_VAR_DOUBLE(nid,nvarid,ls) |
---|
| 1180 | #else |
---|
| 1181 | ierr = NF_PUT_VAR_REAL(nid,nvarid,ls) |
---|
| 1182 | #endif |
---|
[322] | 1183 | if (ierr.ne.NF_NOERR) then |
---|
| 1184 | write(*,*) "ERROR in change_time_axis: Failed to write Ls" |
---|
| 1185 | stop |
---|
| 1186 | endif |
---|
[137] | 1187 | |
---|
| 1188 | end subroutine change_time_axis |
---|
| 1189 | !****************************************************************************** |
---|
| 1190 | subroutine sol2ls(sol,Ls) |
---|
| 1191 | !============================================================================== |
---|
| 1192 | ! Purpose: |
---|
| 1193 | ! Convert a date/time, given in sol (martian day), |
---|
| 1194 | ! into solar longitude date/time, in Ls (in degrees), |
---|
| 1195 | ! where sol=0 is (by definition) the northern hemisphere |
---|
| 1196 | ! spring equinox (where Ls=0). |
---|
| 1197 | !============================================================================== |
---|
| 1198 | ! Notes: |
---|
| 1199 | ! Even though "Ls" is cyclic, if "sol" is greater than N (martian) year, |
---|
| 1200 | ! "Ls" will be increased by N*360 |
---|
| 1201 | ! Won't work as expected if sol is negative (then again, |
---|
| 1202 | ! why would that ever happen?) |
---|
| 1203 | !============================================================================== |
---|
| 1204 | |
---|
| 1205 | implicit none |
---|
| 1206 | |
---|
| 1207 | !============================================================================== |
---|
| 1208 | ! Arguments: |
---|
| 1209 | !============================================================================== |
---|
| 1210 | real,intent(in) :: sol |
---|
| 1211 | real,intent(out) :: Ls |
---|
| 1212 | |
---|
| 1213 | !============================================================================== |
---|
| 1214 | ! Local variables: |
---|
| 1215 | !============================================================================== |
---|
| 1216 | real year_day,peri_day,timeperi,e_elips,twopi,degrad |
---|
| 1217 | data year_day /669./ ! # of sols in a martian year |
---|
| 1218 | data peri_day /485.0/ |
---|
| 1219 | data timeperi /1.9082314/ |
---|
| 1220 | data e_elips /0.093358/ |
---|
| 1221 | data twopi /6.2831853/ ! 2.*pi |
---|
| 1222 | data degrad /57.2957795/ ! pi/180 |
---|
| 1223 | |
---|
| 1224 | real zanom,xref,zx0,zdx,zteta,zz |
---|
| 1225 | |
---|
| 1226 | integer count_years |
---|
| 1227 | integer iter |
---|
| 1228 | |
---|
| 1229 | !============================================================================== |
---|
| 1230 | ! 1. Compute Ls |
---|
| 1231 | !============================================================================== |
---|
| 1232 | |
---|
| 1233 | zz=(sol-peri_day)/year_day |
---|
| 1234 | zanom=twopi*(zz-nint(zz)) |
---|
| 1235 | xref=abs(zanom) |
---|
| 1236 | |
---|
| 1237 | ! The equation zx0 - e * sin (zx0) = xref, solved by Newton |
---|
| 1238 | zx0=xref+e_elips*sin(xref) |
---|
| 1239 | do iter=1,20 ! typically, 2 or 3 iterations are enough |
---|
| 1240 | zdx=-(zx0-e_elips*sin(zx0)-xref)/(1.-e_elips*cos(zx0)) |
---|
| 1241 | zx0=zx0+zdx |
---|
| 1242 | if(abs(zdx).le.(1.e-7)) then |
---|
| 1243 | ! write(*,*)'iter:',iter,' |zdx|:',abs(zdx) |
---|
| 1244 | exit |
---|
| 1245 | endif |
---|
| 1246 | enddo |
---|
| 1247 | |
---|
| 1248 | if(zanom.lt.0.) zx0=-zx0 |
---|
| 1249 | |
---|
| 1250 | zteta=2.*atan(sqrt((1.+e_elips)/(1.-e_elips))*tan(zx0/2.)) |
---|
| 1251 | Ls=zteta-timeperi |
---|
| 1252 | |
---|
| 1253 | if(Ls.lt.0.) then |
---|
| 1254 | Ls=Ls+twopi |
---|
| 1255 | else |
---|
| 1256 | if(Ls.gt.twopi) then |
---|
| 1257 | Ls=Ls-twopi |
---|
| 1258 | endif |
---|
| 1259 | endif |
---|
| 1260 | |
---|
| 1261 | Ls=degrad*Ls |
---|
| 1262 | ! Ls is now in degrees |
---|
| 1263 | |
---|
| 1264 | !============================================================================== |
---|
| 1265 | ! 1. Account for (eventual) years included in input date/time sol |
---|
| 1266 | !============================================================================== |
---|
| 1267 | |
---|
| 1268 | count_years=0 ! initialize |
---|
| 1269 | zz=sol ! use "zz" to store (and work on) the value of sol |
---|
| 1270 | do while (zz.ge.year_day) |
---|
| 1271 | count_years=count_years+1 |
---|
| 1272 | zz=zz-year_day |
---|
| 1273 | enddo |
---|
| 1274 | |
---|
| 1275 | ! Add 360 degrees to Ls for every year |
---|
| 1276 | if (count_years.ne.0) then |
---|
| 1277 | Ls=Ls+360.*count_years |
---|
| 1278 | endif |
---|
| 1279 | |
---|
| 1280 | |
---|
| 1281 | end subroutine sol2ls |
---|
| 1282 | !****************************************************************************** |
---|
| 1283 | subroutine missing_value(nout,nvarid,valid_range,missing) |
---|
| 1284 | !============================================================================== |
---|
| 1285 | ! Purpose: |
---|
| 1286 | ! Write "valid_range" and "missing_value" attributes (of a given |
---|
| 1287 | ! variable) to a netcdf file |
---|
| 1288 | !============================================================================== |
---|
| 1289 | ! Remarks: |
---|
| 1290 | ! NetCDF file must be open |
---|
| 1291 | ! Variable (nvarid) ID must be set |
---|
| 1292 | !============================================================================== |
---|
| 1293 | |
---|
| 1294 | implicit none |
---|
| 1295 | |
---|
| 1296 | include "netcdf.inc" ! NetCDF definitions |
---|
| 1297 | |
---|
| 1298 | !============================================================================== |
---|
| 1299 | ! Arguments: |
---|
| 1300 | !============================================================================== |
---|
| 1301 | integer, intent(in) :: nout |
---|
| 1302 | ! nout: [netcdf] file ID # |
---|
| 1303 | integer, intent(in) :: nvarid |
---|
| 1304 | ! varid: [netcdf] variable ID # |
---|
| 1305 | real, dimension(2), intent(in) :: valid_range |
---|
| 1306 | ! valid_range(2): [netcdf] "valid_range" attribute (min and max) |
---|
| 1307 | real, intent(in) :: missing |
---|
| 1308 | ! missing: [netcdf] "missing_value" attribute |
---|
| 1309 | |
---|
| 1310 | !============================================================================== |
---|
| 1311 | ! Local variables: |
---|
| 1312 | !============================================================================== |
---|
| 1313 | integer :: ierr |
---|
| 1314 | ! ierr: [netcdf] subroutine returned error code |
---|
| 1315 | ! INTEGER nout,nvarid,ierr |
---|
| 1316 | ! REAL missing |
---|
| 1317 | ! REAL valid_range(2) |
---|
| 1318 | |
---|
| 1319 | ! Switch to netcdf dataset define mode |
---|
| 1320 | ierr = NF_REDEF (nout) |
---|
| 1321 | if (ierr.ne.NF_NOERR) then |
---|
| 1322 | print*,'missing_value: ' |
---|
| 1323 | print*, NF_STRERROR(ierr) |
---|
| 1324 | endif |
---|
| 1325 | |
---|
| 1326 | ! Write valid_range() attribute |
---|
| 1327 | #ifdef NC_DOUBLE |
---|
| 1328 | ierr=NF_PUT_ATT_DOUBLE(nout,nvarid,'valid_range',NF_DOUBLE,2,valid_range) |
---|
| 1329 | #else |
---|
| 1330 | ierr=NF_PUT_ATT_REAL(nout,nvarid,'valid_range',NF_FLOAT,2,valid_range) |
---|
| 1331 | #endif |
---|
| 1332 | |
---|
| 1333 | if (ierr.ne.NF_NOERR) then |
---|
| 1334 | print*,'missing_value: valid_range attribution failed' |
---|
| 1335 | print*, NF_STRERROR(ierr) |
---|
| 1336 | !write(*,*) 'NF_NOERR', NF_NOERR |
---|
| 1337 | !CALL abort |
---|
| 1338 | stop "" |
---|
| 1339 | endif |
---|
| 1340 | |
---|
| 1341 | ! Write "missing_value" attribute |
---|
| 1342 | #ifdef NC_DOUBLE |
---|
| 1343 | ierr= NF_PUT_ATT_DOUBLE(nout,nvarid,'missing_value',NF_DOUBLE,1,missing) |
---|
| 1344 | #else |
---|
| 1345 | ierr= NF_PUT_ATT_REAL(nout,nvarid,'missing_value',NF_FLOAT,1,missing) |
---|
| 1346 | #endif |
---|
| 1347 | |
---|
| 1348 | if (ierr.NE.NF_NOERR) then |
---|
| 1349 | print*, 'missing_value: missing value attribution failed' |
---|
| 1350 | print*, NF_STRERROR(ierr) |
---|
| 1351 | ! WRITE(*,*) 'NF_NOERR', NF_NOERR |
---|
| 1352 | ! CALL abort |
---|
| 1353 | stop "" |
---|
| 1354 | endif |
---|
| 1355 | |
---|
| 1356 | ! End netcdf dataset define mode |
---|
| 1357 | ierr = NF_ENDDEF(nout) |
---|
| 1358 | |
---|
| 1359 | end subroutine missing_value |
---|
| 1360 | !****************************************************************************** |
---|
| 1361 | |
---|
| 1362 | end program concatnc |
---|