Extrapolate and interpolate from a file record.
269 character(len=*),
intent(in) :: filename
271 character(len=*),
intent(in) :: varnam
272 real,
intent(in) :: conversion
273 integer,
intent(in) :: recnum
274 type(ocean_grid_type),
intent(inout) :: G
275 real,
allocatable,
dimension(:,:,:) :: tr_z
277 real,
allocatable,
dimension(:,:,:) :: mask_z
279 real,
allocatable,
dimension(:) :: z_in
280 real,
allocatable,
dimension(:) :: z_edges_in
281 real,
intent(out) :: missing_value
282 logical,
intent(in) :: reentrant_x
283 logical,
intent(in) :: tripolar_n
284 logical,
optional,
intent(in) :: homogenize
286 real,
optional,
intent(in) :: m_to_Z
290 real,
dimension(:,:),
allocatable :: tr_in, tr_inp
293 real,
dimension(:,:),
allocatable :: mask_in
296 integer :: rcode, ncid, varid, ndims, id, jd, kd, jdp
298 integer,
dimension(4) :: start, count, dims, dim_id
299 real,
dimension(:,:),
allocatable :: x_in, y_in
300 real,
dimension(:),
allocatable :: lon_in, lat_in
301 real,
dimension(:),
allocatable :: lat_inp, last_row
302 real :: max_lat, min_lat, pole, max_depth, npole
304 real :: add_offset, scale_factor
306 character(len=8) :: laynum
307 type(horiz_interp_type) :: Interp
308 integer :: is, ie, js, je
309 integer :: isc,iec,jsc,jec
310 integer :: isg, ieg, jsg, jeg
311 integer :: isd, ied, jsd, jed
312 integer :: id_clock_read
313 character(len=12) :: dim_name(4)
314 logical :: debug=.false.
315 real :: npoints,varAvg
316 real,
dimension(SZI_(G),SZJ_(G)) :: lon_out, lat_out, tr_out, mask_out
317 real,
dimension(SZI_(G),SZJ_(G)) :: good, fill
318 real,
dimension(SZI_(G),SZJ_(G)) :: tr_outf,tr_prev
319 real,
dimension(SZI_(G),SZJ_(G)) :: good2,fill2
320 real,
dimension(SZI_(G),SZJ_(G)) :: nlevs
322 is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec
323 isd = g%isd ; ied = g%ied ; jsd = g%jsd ; jed = g%jed
324 isg = g%isg ; ieg = g%ieg ; jsg = g%jsg ; jeg = g%jeg
326 id_clock_read = cpu_clock_id(
'(Initialize tracer from Z) read', grain=clock_loop)
329 if (
allocated(tr_z))
deallocate(tr_z)
330 if (
allocated(mask_z))
deallocate(mask_z)
331 if (
allocated(z_edges_in))
deallocate(z_edges_in)
338 call cpu_clock_begin(id_clock_read)
340 rcode = nf90_open(filename, nf90_nowrite, ncid)
341 if (rcode /= 0)
call mom_error(fatal,
"error opening file "//trim(filename)//&
342 " in hinterp_extrap")
343 rcode = nf90_inq_varid(ncid, varnam, varid)
344 if (rcode /= 0)
call mom_error(fatal,
"error finding variable "//trim(varnam)//&
345 " in file "//trim(filename)//
" in hinterp_extrap")
347 rcode = nf90_inquire_variable(ncid, varid, ndims=ndims, dimids=dims)
348 if (rcode /= 0)
call mom_error(fatal,
'error inquiring dimensions hinterp_extrap')
349 if (ndims < 3)
call mom_error(fatal,
"Variable "//trim(varnam)//
" in file "// &
350 trim(filename)//
" has too few dimensions.")
352 rcode = nf90_inquire_dimension(ncid, dims(1), dim_name(1), len=id)
353 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 1 data for "// &
354 trim(varnam)//
" in file "// trim(filename)//
" in hinterp_extrap")
355 rcode = nf90_inq_varid(ncid, dim_name(1), dim_id(1))
356 if (rcode /= 0)
call mom_error(fatal,
"error finding variable "//trim(dim_name(1))//&
357 " in file "//trim(filename)//
" in hinterp_extrap")
358 rcode = nf90_inquire_dimension(ncid, dims(2), dim_name(2), len=jd)
359 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 2 data for "// &
360 trim(varnam)//
" in file "// trim(filename)//
" in hinterp_extrap")
361 rcode = nf90_inq_varid(ncid, dim_name(2), dim_id(2))
362 if (rcode /= 0)
call mom_error(fatal,
"error finding variable "//trim(dim_name(2))//&
363 " in file "//trim(filename)//
" in hinterp_extrap")
364 rcode = nf90_inquire_dimension(ncid, dims(3), dim_name(3), len=kd)
365 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 3 data for "// &
366 trim(varnam)//
" in file "// trim(filename)//
" in hinterp_extrap")
367 rcode = nf90_inq_varid(ncid, dim_name(3), dim_id(3))
368 if (rcode /= 0)
call mom_error(fatal,
"error finding variable "//trim(dim_name(3))//&
369 " in file "//trim(filename)//
" in hinterp_extrap")
372 rcode = nf90_get_att(ncid, varid,
"_FillValue", missing_value)
373 if (rcode /= 0)
call mom_error(fatal,
"error finding missing value for "//&
374 trim(varnam)//
" in file "// trim(filename)//
" in hinterp_extrap")
376 rcode = nf90_get_att(ncid, varid,
"add_offset", add_offset)
377 if (rcode /= 0) add_offset = 0.0
379 rcode = nf90_get_att(ncid, varid,
"scale_factor", scale_factor)
380 if (rcode /= 0) scale_factor = 1.0
382 if (
allocated(lon_in))
deallocate(lon_in)
383 if (
allocated(lat_in))
deallocate(lat_in)
384 if (
allocated(z_in))
deallocate(z_in)
385 if (
allocated(z_edges_in))
deallocate(z_edges_in)
386 if (
allocated(tr_z))
deallocate(tr_z)
387 if (
allocated(mask_z))
deallocate(mask_z)
389 allocate(lon_in(id),lat_in(jd),z_in(kd),z_edges_in(kd+1))
390 allocate(tr_z(isd:ied,jsd:jed,kd), mask_z(isd:ied,jsd:jed,kd))
392 start = 1; count = 1; count(1) = id
393 rcode = nf90_get_var(ncid, dim_id(1), lon_in, start, count)
394 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 1 values for var_name "// &
395 trim(varnam)//
",dim_name "//trim(dim_name(1))//
" in file "// trim(filename)//
" in hinterp_extrap")
396 start = 1; count = 1; count(1) = jd
397 rcode = nf90_get_var(ncid, dim_id(2), lat_in, start, count)
398 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 2 values for var_name "// &
399 trim(varnam)//
",dim_name "//trim(dim_name(2))//
" in file "// trim(filename)//
" in hinterp_extrap")
400 start = 1; count = 1; count(1) = kd
401 rcode = nf90_get_var(ncid, dim_id(3), z_in, start, count)
402 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 3 values for var_name "// &
403 trim(varnam//
",dim_name "//trim(dim_name(3)))//
" in file "// trim(filename)//
" in hinterp_extrap")
405 call cpu_clock_end(id_clock_read)
407 if (
present(m_to_z))
then ;
do k=1,kd ; z_in(k) = m_to_z * z_in(k) ;
enddo ;
endif
411 max_lat = maxval(lat_in)
413 if (max_lat < 90.0)
then
416 allocate(lat_inp(jdp))
417 lat_inp(1:jd)=lat_in(:)
420 allocate(lat_in(1:jdp))
430 z_edges_in(k)=0.5*(z_in(k-1)+z_in(k))
432 z_edges_in(kd+1)=2.0*z_in(kd) - z_in(kd-1)
434 call horiz_interp_init()
436 lon_in = lon_in*pi_180
437 lat_in = lat_in*pi_180
438 allocate(x_in(id,jdp),y_in(id,jdp))
439 call meshgrid(lon_in,lat_in, x_in, y_in)
441 lon_out(:,:) = g%geoLonT(:,:)*pi_180
442 lat_out(:,:) = g%geoLatT(:,:)*pi_180
445 allocate(tr_in(id,jd)) ; tr_in(:,:)=0.0
446 allocate(tr_inp(id,jdp)) ; tr_inp(:,:)=0.0
447 allocate(mask_in(id,jdp)) ; mask_in(:,:)=0.0
448 allocate(last_row(id)) ; last_row(:)=0.0
450 max_depth = maxval(g%bathyT)
451 call mpp_max(max_depth)
453 if (z_edges_in(kd+1)<max_depth) z_edges_in(kd+1)=max_depth
455 roundoff = 3.0*epsilon(missing_value)
461 write(laynum,
'(I8)') k ; laynum = adjustl(laynum)
463 if (is_root_pe())
then
464 start = 1 ; start(3) = k ; count(:) = 1 ; count(1) = id ; count(2) = jd
465 rcode = nf90_get_var(ncid,varid, tr_in, start, count)
466 if (rcode /= 0)
call mom_error(fatal,
"hinterp_and_extract_from_Fie: "//&
467 "error reading level "//trim(laynum)//
" of variable "//&
468 trim(varnam)//
" in file "// trim(filename))
471 last_row(:)=tr_in(:,jd); pole=0.0;npole=0.0
473 if (abs(tr_in(i,jd)-missing_value) > abs(roundoff*missing_value))
then
474 pole = pole+last_row(i)
483 tr_inp(:,1:jd) = tr_in(:,:)
486 tr_inp(:,:) = tr_in(:,:)
491 call mpp_broadcast(tr_inp, id*jdp, root_pe())
498 if (abs(tr_inp(i,j)-missing_value) > abs(roundoff*missing_value))
then
500 tr_inp(i,j) = (tr_inp(i,j)*scale_factor+add_offset) * conversion
502 tr_inp(i,j) = missing_value
510 call horiz_interp_new(interp,x_in,y_in,lon_out(is:ie,js:je),lat_out(is:ie,js:je), &
511 interp_method=
'bilinear',src_modulo=.true.)
515 call mystats(tr_inp,missing_value, is,ie,js,je,k,
'Tracer from file')
520 call horiz_interp(interp,tr_inp,tr_out(is:ie,js:je), missing_value=missing_value, new_missing_handle=.true.)
525 if (abs(tr_out(i,j)-missing_value) < abs(roundoff*missing_value)) mask_out(i,j)=0.
529 fill = 0.0; good = 0.0
531 npoints = 0 ; varavg = 0.
534 if (mask_out(i,j) < 1.0)
then
535 tr_out(i,j)=missing_value
538 npoints = npoints + 1
539 varavg = varavg + tr_out(i,j)
541 if (g%mask2dT(i,j) == 1.0 .and. z_edges_in(k) <= g%bathyT(i,j) .and. mask_out(i,j) < 1.0) &
545 call pass_var(fill,g%Domain)
546 call pass_var(good,g%Domain)
549 call mystats(tr_out,missing_value, is,ie,js,je,k,
'variable from horiz_interp()')
553 if (
PRESENT(homogenize))
then
555 call sum_across_pes(npoints)
556 call sum_across_pes(varavg)
558 varavg = varavg/real(npoints)
566 tr_outf(:,:) = tr_out(:,:)
567 if (k==1) tr_prev(:,:) = tr_outf(:,:)
568 good2(:,:) = good(:,:)
569 fill2(:,:) = fill(:,:)
571 call fill_miss_2d(tr_outf, good2, fill2, tr_prev, g, smooth=.true.)
572 call mystats(tr_outf, missing_value, is, ie, js, je, k,
'field from fill_miss_2d()')
574 tr_z(:,:,k) = tr_outf(:,:) * g%mask2dT(:,:)
575 mask_z(:,:,k) = good2(:,:) + fill2(:,:)
577 tr_prev(:,:) = tr_z(:,:,k)
580 call hchksum(tr_prev,
'field after fill ',g%HI)