Extrapolate and interpolate from a file record.
278 character(len=*),
intent(in) :: filename
280 character(len=*),
intent(in) :: varnam
281 real,
intent(in) :: conversion
282 integer,
intent(in) :: recnum
283 type(ocean_grid_type),
intent(inout) :: G
284 real,
allocatable,
dimension(:,:,:) :: tr_z
286 real,
allocatable,
dimension(:,:,:) :: mask_z
288 real,
allocatable,
dimension(:) :: z_in
289 real,
allocatable,
dimension(:) :: z_edges_in
290 real,
intent(out) :: missing_value
291 logical,
intent(in) :: reentrant_x
292 logical,
intent(in) :: tripolar_n
293 logical,
optional,
intent(in) :: homogenize
295 real,
optional,
intent(in) :: m_to_Z
297 logical,
optional,
intent(in) :: answers_2018
300 logical,
optional,
intent(in) :: ongrid
305 real,
dimension(:,:),
allocatable :: tr_in, tr_inp
308 real,
dimension(:,:),
allocatable :: mask_in
311 integer :: rcode, ncid, varid, ndims, id, jd, kd, jdp
313 integer,
dimension(4) :: start, count, dims, dim_id
314 real,
dimension(:,:),
allocatable :: x_in, y_in
315 real,
dimension(:),
allocatable :: lon_in, lat_in
316 real,
dimension(:),
allocatable :: lat_inp, last_row
317 real :: max_lat, min_lat, pole, max_depth, npole
319 real :: add_offset, scale_factor
322 character(len=8) :: laynum
323 type(horiz_interp_type) :: Interp
324 integer :: is, ie, js, je
325 integer :: isc,iec,jsc,jec
326 integer :: isg, ieg, jsg, jeg
327 integer :: isd, ied, jsd, jed
328 integer :: id_clock_read
329 character(len=12) :: dim_name(4)
330 logical :: debug=.false.
331 real :: npoints,varAvg
332 real,
dimension(SZI_(G),SZJ_(G)) :: lon_out, lat_out, tr_out, mask_out
333 real,
dimension(SZI_(G),SZJ_(G)) :: good, fill
334 real,
dimension(SZI_(G),SZJ_(G)) :: tr_outf,tr_prev
335 real,
dimension(SZI_(G),SZJ_(G)) :: good2,fill2
336 real,
dimension(SZI_(G),SZJ_(G)) :: nlevs
338 is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec
339 isd = g%isd ; ied = g%ied ; jsd = g%jsd ; jed = g%jed
340 isg = g%isg ; ieg = g%ieg ; jsg = g%jsg ; jeg = g%jeg
342 id_clock_read = cpu_clock_id(
'(Initialize tracer from Z) read', grain=clock_loop)
345 if (
present(ongrid)) is_ongrid=ongrid
347 if (
allocated(tr_z))
deallocate(tr_z)
348 if (
allocated(mask_z))
deallocate(mask_z)
349 if (
allocated(z_edges_in))
deallocate(z_edges_in)
356 call cpu_clock_begin(id_clock_read)
358 rcode = nf90_open(filename, nf90_nowrite, ncid)
359 if (rcode /= 0)
call mom_error(fatal,
"error opening file "//trim(filename)//&
360 " in hinterp_extrap")
361 rcode = nf90_inq_varid(ncid, varnam, varid)
362 if (rcode /= 0)
call mom_error(fatal,
"error finding variable "//trim(varnam)//&
363 " in file "//trim(filename)//
" in hinterp_extrap")
365 rcode = nf90_inquire_variable(ncid, varid, ndims=ndims, dimids=dims)
366 if (rcode /= 0)
call mom_error(fatal,
'error inquiring dimensions hinterp_extrap')
367 if (ndims < 3)
call mom_error(fatal,
"Variable "//trim(varnam)//
" in file "// &
368 trim(filename)//
" has too few dimensions.")
370 rcode = nf90_inquire_dimension(ncid, dims(1), dim_name(1), len=id)
371 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 1 data for "// &
372 trim(varnam)//
" in file "// trim(filename)//
" in hinterp_extrap")
373 rcode = nf90_inq_varid(ncid, dim_name(1), dim_id(1))
374 if (rcode /= 0)
call mom_error(fatal,
"error finding variable "//trim(dim_name(1))//&
375 " in file "//trim(filename)//
" in hinterp_extrap")
376 rcode = nf90_inquire_dimension(ncid, dims(2), dim_name(2), len=jd)
377 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 2 data for "// &
378 trim(varnam)//
" in file "// trim(filename)//
" in hinterp_extrap")
379 rcode = nf90_inq_varid(ncid, dim_name(2), dim_id(2))
380 if (rcode /= 0)
call mom_error(fatal,
"error finding variable "//trim(dim_name(2))//&
381 " in file "//trim(filename)//
" in hinterp_extrap")
382 rcode = nf90_inquire_dimension(ncid, dims(3), dim_name(3), len=kd)
383 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 3 data for "// &
384 trim(varnam)//
" in file "// trim(filename)//
" in hinterp_extrap")
385 rcode = nf90_inq_varid(ncid, dim_name(3), dim_id(3))
386 if (rcode /= 0)
call mom_error(fatal,
"error finding variable "//trim(dim_name(3))//&
387 " in file "//trim(filename)//
" in hinterp_extrap")
390 rcode = nf90_get_att(ncid, varid,
"_FillValue", missing_value)
391 if (rcode /= 0)
call mom_error(fatal,
"error finding missing value for "//&
392 trim(varnam)//
" in file "// trim(filename)//
" in hinterp_extrap")
394 rcode = nf90_get_att(ncid, varid,
"add_offset", add_offset)
395 if (rcode /= 0) add_offset = 0.0
397 rcode = nf90_get_att(ncid, varid,
"scale_factor", scale_factor)
398 if (rcode /= 0) scale_factor = 1.0
400 if (
allocated(lon_in))
deallocate(lon_in)
401 if (
allocated(lat_in))
deallocate(lat_in)
402 if (
allocated(z_in))
deallocate(z_in)
403 if (
allocated(z_edges_in))
deallocate(z_edges_in)
404 if (
allocated(tr_z))
deallocate(tr_z)
405 if (
allocated(mask_z))
deallocate(mask_z)
407 allocate(lon_in(id),lat_in(jd),z_in(kd),z_edges_in(kd+1))
408 allocate(tr_z(isd:ied,jsd:jed,kd), mask_z(isd:ied,jsd:jed,kd))
410 start = 1; count = 1; count(1) = id
411 rcode = nf90_get_var(ncid, dim_id(1), lon_in, start, count)
412 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 1 values for var_name "// &
413 trim(varnam)//
",dim_name "//trim(dim_name(1))//
" in file "// trim(filename)//
" in hinterp_extrap")
414 start = 1; count = 1; count(1) = jd
415 rcode = nf90_get_var(ncid, dim_id(2), lat_in, start, count)
416 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 2 values for var_name "// &
417 trim(varnam)//
",dim_name "//trim(dim_name(2))//
" in file "// trim(filename)//
" in hinterp_extrap")
418 start = 1; count = 1; count(1) = kd
419 rcode = nf90_get_var(ncid, dim_id(3), z_in, start, count)
420 if (rcode /= 0)
call mom_error(fatal,
"error reading dimension 3 values for var_name "// &
421 trim(varnam//
",dim_name "//trim(dim_name(3)))//
" in file "// trim(filename)//
" in hinterp_extrap")
423 call cpu_clock_end(id_clock_read)
425 if (
present(m_to_z))
then ;
do k=1,kd ; z_in(k) = m_to_z * z_in(k) ;
enddo ;
endif 430 if (.not. is_ongrid)
then 431 max_lat = maxval(lat_in)
432 if (max_lat < 90.0)
then 435 allocate(lat_inp(jdp))
436 lat_inp(1:jd)=lat_in(:)
439 allocate(lat_in(1:jdp))
447 z_edges_in(k)=0.5*(z_in(k-1)+z_in(k))
449 z_edges_in(kd+1)=2.0*z_in(kd) - z_in(kd-1)
452 allocate(tr_in(is:ie,js:je)) ; tr_in(:,:)=0.0
453 allocate(mask_in(is:ie,js:je)) ; mask_in(:,:)=0.0
455 call horiz_interp_init()
456 lon_in = lon_in*pi_180
457 lat_in = lat_in*pi_180
458 allocate(x_in(id,jdp),y_in(id,jdp))
459 call meshgrid(lon_in,lat_in, x_in, y_in)
460 lon_out(:,:) = g%geoLonT(:,:)*pi_180
461 lat_out(:,:) = g%geoLatT(:,:)*pi_180
462 allocate(tr_in(id,jd)) ; tr_in(:,:)=0.0
463 allocate(tr_inp(id,jdp)) ; tr_inp(:,:)=0.0
464 allocate(mask_in(id,jdp)) ; mask_in(:,:)=0.0
465 allocate(last_row(id)) ; last_row(:)=0.0
470 max_depth = maxval(g%bathyT)
471 call mpp_max(max_depth)
473 if (z_edges_in(kd+1)<max_depth) z_edges_in(kd+1)=max_depth
474 roundoff = 3.0*epsilon(missing_value)
480 write(laynum,
'(I8)') k ; laynum = adjustl(laynum)
483 start(1) = is+g%HI%idg_offset ; start(2) = js+g%HI%jdg_offset ; start(3) = k
484 count(1) = ie-is+1 ; count(2) = je-js+1; count(3) = 1
485 rcode = nf90_get_var(ncid,varid, tr_in, start, count)
486 if (rcode /= 0)
call mom_error(fatal,
"hinterp_and_extract_from_Fie: "//&
487 "error reading level "//trim(laynum)//
" of variable "//&
488 trim(varnam)//
" in file "// trim(filename))
492 if (abs(tr_in(i,j)-missing_value) > abs(roundoff*missing_value))
then 494 tr_in(i,j) = (tr_in(i,j)*scale_factor+add_offset) * conversion
496 tr_in(i,j) = missing_value
502 if (is_root_pe())
then 503 start = 1 ; start(3) = k ; count(:) = 1 ; count(1) = id ; count(2) = jd
504 rcode = nf90_get_var(ncid,varid, tr_in, start, count)
505 if (rcode /= 0)
call mom_error(fatal,
"hinterp_and_extract_from_Fie: "//&
506 "error reading level "//trim(laynum)//
" of variable "//&
507 trim(varnam)//
" in file "// trim(filename))
510 last_row(:)=tr_in(:,jd); pole=0.0;npole=0.0
512 if (abs(tr_in(i,jd)-missing_value) > abs(roundoff*missing_value))
then 513 pole = pole+last_row(i)
522 tr_inp(:,1:jd) = tr_in(:,:)
525 tr_inp(:,:) = tr_in(:,:)
530 call mpp_broadcast(tr_inp, id*jdp, root_pe())
535 if (abs(tr_inp(i,j)-missing_value) > abs(roundoff*missing_value))
then 537 tr_inp(i,j) = (tr_inp(i,j)*scale_factor+add_offset) * conversion
539 tr_inp(i,j) = missing_value
549 if (.not. is_ongrid)
then 551 call horiz_interp_new(interp,x_in,y_in,lon_out(is:ie,js:je),lat_out(is:ie,js:je), &
552 interp_method=
'bilinear',src_modulo=.true.)
556 call mystats(tr_inp,missing_value, is,ie,js,je,k,
'Tracer from file')
562 tr_out(is:ie,js:je)=tr_in(is:ie,js:je)
564 call horiz_interp(interp,tr_inp,tr_out(is:ie,js:je), missing_value=missing_value, new_missing_handle=.true.)
570 if (abs(tr_out(i,j)-missing_value) < abs(roundoff*missing_value)) mask_out(i,j)=0.
574 fill = 0.0; good = 0.0
576 npoints = 0 ; varavg = 0.
579 if (mask_out(i,j) < 1.0)
then 580 tr_out(i,j)=missing_value
583 npoints = npoints + 1
584 varavg = varavg + tr_out(i,j)
586 if (g%mask2dT(i,j) == 1.0 .and. z_edges_in(k) <= g%bathyT(i,j) .and. mask_out(i,j) < 1.0) &
590 call pass_var(fill,g%Domain)
591 call pass_var(good,g%Domain)
594 call mystats(tr_out,missing_value, is,ie,js,je,k,
'variable from horiz_interp()')
598 if (
PRESENT(homogenize))
then 600 call sum_across_pes(npoints)
601 call sum_across_pes(varavg)
603 varavg = varavg/
real(nPoints)
611 tr_outf(:,:) = tr_out(:,:)
612 if (k==1) tr_prev(:,:) = tr_outf(:,:)
613 good2(:,:) = good(:,:)
614 fill2(:,:) = fill(:,:)
616 call fill_miss_2d(tr_outf, good2, fill2, tr_prev, g, smooth=.true., answers_2018=answers_2018)
617 call mystats(tr_outf, missing_value, is, ie, js, je, k,
'field from fill_miss_2d()')
619 tr_z(:,:,k) = tr_outf(:,:) * g%mask2dT(:,:)
620 mask_z(:,:,k) = good2(:,:) + fill2(:,:)
622 tr_prev(:,:) = tr_z(:,:,k)
625 call hchksum(tr_prev,
'field after fill ',g%HI)