6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6173
6174
6175
6176 integer, intent(in) :: ng, model
6177
6178 integer, intent(in), optional :: ncid
6179 integer, intent(in), optional :: start(:)
6180 integer, intent(in), optional :: total(:)
6181
6182 character (len=*), intent(in) :: ncname
6183 character (len=*), intent(in) :: myVarName
6184
6185 real(dp), intent(in) :: Rdate(2)
6186
6187 real(dp), intent(out), optional :: min_val
6188 real(dp), intent(out), optional :: max_val
6189
6190 real(dp), intent(out) :: A(:)
6191
6192
6193
6194 logical :: JulianOffset = .false.
6195 logical :: Ldebug = .false.
6196
6197 logical, dimension(1) :: got_units
6198 logical, dimension(2) :: foundit
6199
6200 integer :: i, ind, lstr, my_ncid, status, varid
6201 integer :: year, month, day, hour, minutes
6202
6203 integer, dimension(1) :: Asize
6204#if !defined PARALLEL_IO && defined DISTRIBUTE
6205 integer, dimension(2) :: ibuffer
6206#endif
6207
6208 real(dp) :: Afactor, Aoffset, my_Rdate(2), seconds
6209 real(dp) :: dnum_old, dnum_new, scale
6210
6211 real(r8), dimension(2) :: AttValue
6212
6213 character (len=12) :: AttName(2)
6214 character (len=22) :: dstr_old, dstr_new
6215 character (len=40) :: UnitsAtt(1), UnitsValue(1)
6216 character (len=40) :: Units, Ustring
6217
6218 character (len=*), parameter :: MyFile = &
6219 & __FILE__//", netcdf_get_time_1d"
6220
6221
6222
6223
6224
6225 IF (PRESENT(start).and.PRESENT(total)) THEN
6226 asize(1)=1
6227 DO i=1,SIZE(total)
6228 asize(1)=asize(1)*total(i)
6229 END DO
6230 ELSE
6231 asize(1)=ubound(a, dim=1)
6232 END IF
6233
6234
6235
6236 IF (.not.PRESENT(ncid)) THEN
6237 CALL netcdf_open (ng, model, trim(ncname), 0, my_ncid)
6238 IF (
founderror(exit_flag, noerror, __line__, myfile))
RETURN
6239 ELSE
6240 my_ncid=ncid
6241 END IF
6242
6243
6244
6245 IF (inpthread) THEN
6246 status=nf90_inq_varid(my_ncid, trim(myvarname), varid)
6247 IF (status.eq.nf90_noerr) THEN
6248 IF (PRESENT(start).and.PRESENT(total)) THEN
6249 status=nf90_get_var(my_ncid, varid, a, start, total)
6250 ELSE
6251 status=nf90_get_var(my_ncid, varid, a)
6252 END IF
6253 IF (
founderror(status, nf90_noerr, __line__, myfile))
THEN
6254 WRITE (stdout,10) trim(myvarname), trim(ncname), &
6255 & trim(sourcefile), nf90_strerror(status)
6256 exit_flag=2
6257 ioerror=status
6258 END IF
6259 ELSE
6260 WRITE (stdout,20) trim(myvarname), trim(ncname), &
6261 & trim(sourcefile), nf90_strerror(status)
6262 exit_flag=2
6263 ioerror=status
6264 END IF
6265 END IF
6266
6267#if !defined PARALLEL_IO && defined DISTRIBUTE
6268
6269
6270
6271 ibuffer(1)=exit_flag
6272 ibuffer(2)=ioerror
6273 CALL mp_bcasti (ng, model, ibuffer)
6274 exit_flag=ibuffer(1)
6275 ioerror=ibuffer(2)
6276 IF (exit_flag.eq.noerror) THEN
6277 CALL mp_bcastf (ng, model, a)
6278 END IF
6279#endif
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294 attname(1)='scale_factor'
6295 attname(2)='add_offset '
6296
6297 CALL netcdf_get_fatt (ng, model, ncname, varid, attname, &
6298 & attvalue, foundit, &
6299 & ncid = my_ncid)
6300
6301 IF (exit_flag.eq.noerror) THEN
6302 IF (.not.foundit(1)) THEN
6303 afactor=1.0_r8
6304 ELSE
6305 afactor=real(attvalue(1),dp)
6306 END IF
6307
6308 IF (.not.foundit(2)) THEN
6309 aoffset=0.0_r8
6310 ELSE
6311 aoffset=real(attvalue(2),dp)
6312 END IF
6313
6314 IF (foundit(1)) THEN
6315 DO i=1,asize(1)
6316 a(i)=afactor*a(i)
6317 END DO
6318 END IF
6319
6320 IF (foundit(2)) THEN
6321 DO i=1,asize(1)
6322 a(i)=a(i)+aoffset
6323 END DO
6324 IF (time_ref.eq.-2) julianoffset=.true.
6325 END IF
6326 END IF
6327
6328
6329
6330
6331 unitsatt(1)='units'
6332
6333 CALL netcdf_get_satt (ng, model, ncname, varid, unitsatt, &
6334 & unitsvalue, got_units, &
6335 & ncid = my_ncid)
6336
6337 IF (exit_flag.eq.noerror) THEN
6338 IF (got_units(1)) THEN
6340 lstr=len_trim(units)
6341 ind=index(units,'since')
6342 IF (ind.gt.0) THEN
6343 CALL time_units (trim(units), year, month, day, hour, &
6344 & minutes, seconds)
6345 CALL datenum (my_rdate, year, month, day, hour, minutes, &
6346 & seconds)
6347 IF (rdate(1).ne.my_rdate(1)) THEN
6348 ustring=units(1:ind-2)
6349 SELECT CASE (trim(ustring))
6350 CASE ('second', 'seconds')
6351 IF (ldebug) THEN
6352 IF (julianoffset) THEN
6353 dnum_old=a(1)
6354 ELSE
6355 dnum_old=my_rdate(2)+a(1)
6356 END IF
6357 CALL datestr (dnum_old, .false., dstr_old)
6358 END IF
6359 IF (julianoffset) THEN
6360 DO i=1,asize(1)
6361 a(i)=a(i)-rdate(2)
6362 END DO
6363 ELSE
6364 DO i=1,asize(1)
6365 a(i)=(my_rdate(2)+a(i))-rdate(2)
6366 END DO
6367 END IF
6368 IF (ldebug) THEN
6369 dnum_new=rdate(2)+a(1)
6370 CALL datestr (dnum_new, .false., dstr_new)
6371 END IF
6372 CASE ('hour', 'hours')
6373 scale=3600.0_dp
6374 IF (ldebug) THEN
6375 IF (julianoffset) THEN
6376 dnum_old=a(1)*scale
6377 ELSE
6378 dnum_old=my_rdate(2)+a(1)*scale
6379 END IF
6380 CALL datestr (dnum_old, .false., dstr_old)
6381 END IF
6382 scale=24.0_dp
6383 IF (julianoffset) THEN
6384 DO i=1,asize(1)
6385 a(i)=a(i)-rdate(1)*scale
6386 END DO
6387 ELSE
6388 DO i=1,asize(1)
6389 a(i)=(my_rdate(1)*scale+a(i))-rdate(1)*scale
6390 END DO
6391 END IF
6392 IF (ldebug) THEN
6393 scale=3600.0_dp
6394 dnum_new=rdate(2)+a(1)*scale
6395 CALL datestr (dnum_new, .false., dstr_new)
6396 END IF
6397 CASE ('day', 'days')
6398 IF (ldebug) THEN
6399 IF (julianoffset) THEN
6400 dnum_old=a(1)
6401 ELSE
6402 dnum_old=my_rdate(1)+a(1)
6403 END IF
6404 CALL datestr (dnum_old, .true., dstr_old)
6405 END IF
6406 IF (julianoffset) THEN
6407 DO i=1,asize(1)
6408 a(i)=a(i)-rdate(1)
6409 END DO
6410 ELSE
6411 DO i=1,asize(1)
6412 a(i)=(my_rdate(1)+a(i))-rdate(1)
6413 END DO
6414 END IF
6415 IF (ldebug) THEN
6416 dnum_new=rdate(1)+a(1)
6417 CALL datestr (dnum_new, .true., dstr_new)
6418 END IF
6419 END SELECT
6420 END IF
6421 END IF
6422 END IF
6423 END IF
6424
6425
6426
6427 IF (PRESENT(min_val)) THEN
6428 min_val=minval(a)
6429 END IF
6430 IF (PRESENT(max_val)) THEN
6431 max_val=maxval(a)
6432 END IF
6433
6434
6435
6436 IF (.not.PRESENT(ncid)) THEN
6437 CALL netcdf_close (ng, model, my_ncid, ncname, .false.)
6438 END IF
6439
6440 10 FORMAT (/,' NETCDF_GET_TIME_1D - error while reading variable:', &
6441 & 2x,a,/,22x,'in input file:',2x,a,/,22x,'call from:',2x,a, &
6442 & /,22x,a)
6443 20 FORMAT (/,' NETCDF_GET_TIME_1D - error while inquiring ID for ', &
6444 & 'variable:',2x,a,/,22x,'in input file:',2x,a,/,22x, &
6445 & 'call from:',2x,a,/,22x,a)
6446
6447 RETURN