Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MITgcm/N-BLING with Compressed Staggered Grids #640

Merged
merged 67 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
d67b475
Initial commit of the adaptive close_state_caching
Jun 6, 2022
874cf22
Comment out the close_state_cach = .false. in get_close_state_cached
Jun 6, 2022
0428283
Add do_output for the print statements
Jun 10, 2022
774b9cc
New I/O scheme with offline processing of large input state
Jul 8, 2022
5a8b812
Merge branch 'main' into netcdf-reduce
hkershaw-brown Aug 16, 2022
7d5867f
moved dart_nc_reduce one directory up to compile
hkershaw-brown Aug 16, 2022
d376f80
grid size is harded coded - change to small case
hkershaw-brown Aug 17, 2022
869d89d
using dart netcdf utilites and types modules
hkershaw-brown Aug 17, 2022
fbbb201
mirror of dart_nc_reduce. untested.
hkershaw-brown Aug 18, 2022
b14007d
ZC is double - check dart_nc_reduce
hkershaw-brown Aug 19, 2022
d9e2979
zc double dart_nc_expand
hkershaw-brown Aug 22, 2022
d197e63
netcdf and model_mod_check for comparision with main
hkershaw-brown Aug 22, 2022
e9273c8
Merge branch 'main' into netcdf-reduce
hkershaw-brown Aug 22, 2022
a409ff4
function for defining variables mit2dart
hkershaw-brown Aug 25, 2022
3d0c053
function for dart to mit. untested.
hkershaw-brown Aug 25, 2022
e007252
bug-fix: returns need to be inside if statement
hkershaw-brown Aug 26, 2022
fba76b4
bitwise mit_to_dart non-compressed with main, log and nolog
hkershaw-brown Aug 26, 2022
951235c
bitwise with main dart_to_mit, no compression
hkershaw-brown Aug 26, 2022
60c742c
size of compressed variables
hkershaw-brown Aug 26, 2022
8b556ba
partway through compressed write, I think separate writes for 2d vs
hkershaw-brown Aug 29, 2022
b239419
write compressed. untested. missing coord
hkershaw-brown Aug 29, 2022
1a7bf39
note on delX,Y - does delX,Y vary?
hkershaw-brown Aug 30, 2022
d4c3905
record indices for X,Y,Z
hkershaw-brown Aug 31, 2022
39568db
compressing out vals=0.0
hkershaw-brown Aug 31, 2022
329cd42
replace hardcoded 0.0_r8 with binary_fill variable
hkershaw-brown Aug 31, 2022
6fbbdf0
somthing funky with ETA
hkershaw-brown Sep 1, 2022
a7d60db
bug-fix: 2D ETA variable is th k=1 slice
hkershaw-brown Sep 1, 2022
0c2c781
revert assim_tools_mod to main
hkershaw-brown Sep 2, 2022
80e22dc
move initializing to fill outside read_compressed
hkershaw-brown Sep 2, 2022
0a30429
Merge branch 'main' into netcdf-reduce
hkershaw-brown Sep 16, 2022
3d1746f
bug fix: was not setting binary fill correctly for 2d
hkershaw-brown Sep 16, 2022
5709e68
Merge branch 'main' into netcdf-reduce
hkershaw-brown Sep 19, 2022
9951fd8
removed dart_nc_reduce/expand as these functions are now part of mit_…
hkershaw-brown Sep 19, 2022
720f763
revert mpas input.nml to main
hkershaw-brown Sep 19, 2022
b2b218d
remove whitespace only differences
hkershaw-brown Sep 19, 2022
1e7e186
get_state_meta data and get_val
hkershaw-brown Sep 19, 2022
25769ca
compressed lon,lat is r4. compressed depth r8
hkershaw-brown Sep 19, 2022
3e92f58
note on perturbing compressed vs non-compressed state
hkershaw-brown Sep 19, 2022
43e74ca
bug-fix: masked initialized to false for compresed and not compressed
hkershaw-brown Sep 23, 2022
e641695
style: switch tabs for spaces
hkershaw-brown Sep 23, 2022
32df048
2d and staggered variables are incorrect
hkershaw-brown Sep 23, 2022
12fa6c0
Merge branch 'main' into netcdf-reduce
hkershaw-brown Sep 23, 2022
8dcb481
fix: depth dimension first in compression so 2d index search is correct
hkershaw-brown Sep 30, 2022
03564a4
program to expand compressed netcdf to full X,Y,Z
hkershaw-brown Oct 3, 2022
63c19b5
recl2d and recl3d set in static_init_trans
hkershaw-brown Oct 7, 2022
d54cbfb
doc: compressed netcdf files
fnrliu Oct 7, 2022
e926fe0
Merge branch 'main' into netcdf-reduce
hkershaw-brown Oct 11, 2022
cd107d1
Option to output CHL for dart_to_mit
hkershaw-brown Oct 11, 2022
2c0b275
Merge branch 'main' into netcdf-reduce
hkershaw-brown Oct 21, 2022
5fe6781
Merge branch 'main' into netcdf-reduce
hkershaw-brown Nov 3, 2022
630b5a1
Merge branch 'main' into netcdf-reduce
hkershaw-brown Nov 9, 2022
d048a05
one place to set recl3d recl2d
hkershaw-brown Feb 9, 2023
134570b
Merge branch 'main' into netcdf-reduce
hkershaw-brown Feb 9, 2023
f0da199
Merge branch 'main' into netcdf-reduce
hkershaw-brown May 8, 2023
de015c6
Merge branch 'main' into netcdf-reduce
hkershaw-brown May 10, 2023
a2c0d26
Merge branch 'main' into netcdf-reduce
hkershaw-brown Jun 22, 2023
41b35e3
Merge branch 'main' into netcdf-reduce
hkershaw-brown Jun 27, 2023
92142d8
Merge branch 'main' into netcdf-reduce
hkershaw-brown Nov 7, 2023
e0dd62b
Merge branch 'main' into netcdf-reduce
hkershaw-brown Nov 9, 2023
58f3c97
Compression that supports staggered grids
mgharamti Jan 26, 2024
e9001e2
Changes after cycling DA testing
mgharamti Feb 20, 2024
0febdfe
Merge branch 'main' into MITgcm_comp
hkershaw-brown Feb 29, 2024
ecfdb88
remove expand netcdf program
hkershaw-brown Mar 11, 2024
ce1fcb6
Merge branch 'main' into MITgcm_comp
hkershaw-brown Mar 11, 2024
058cdac
Merge branch 'main' into MITgcm_comp
hkershaw-brown Mar 12, 2024
1fe14ea
revert netcdf utilties to main, since expand_netcdf.f90 no longer par…
hkershaw-brown Mar 12, 2024
1044da3
bump conf.py and CHANGELOG for release
hkershaw-brown Mar 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2415,7 +2415,7 @@ function nc_create_file(filename, context)
character(len=*), parameter :: routine = 'nc_create_file'
integer :: ret, ncid, oldmode

ret = nf90_create(filename, NF90_CLOBBER, ncid)
ret = nf90_create(filename, ior(NF90_CLOBBER,NF90_64BIT_OFFSET), ncid)
call nc_check(ret, routine, 'create '//trim(filename)//' read/write', context)

call add_fh_to_list(ncid, filename)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ subroutine check_meta_data( iloc )
kind_index=qty_index, &
kind_string=qty_string)

write(string1,'("index ",i11," is i,j,k",3(1x,i4)," and is in domain ",i2)') &
write(string1,'("index ",i11," is i,j,k",3(1x,i10)," and is in domain ",i2)') &
iloc, ix, iy, iz, dom_id
write(string2,'("is quantity ", I4,", ",A)') var_type, trim(qty_string)//' at location'
call write_location(0,loc,charstring=string3)
Expand Down Expand Up @@ -556,9 +556,12 @@ subroutine check_all_meta_data()
kind_string=qty_string)

! CLM has (potentially many) columns and needs i7 ish precision
write(string1,'(i11,1x,''i,j,k'',3(1x,i7),'' domain '',i2)') &
! write(string1,'(i11,1x,''i,j,k'',3(1x,i7),'' domain '',i2)') &
! iloc, ix, iy, iz, dom_id
! EL: integer to short for the new I/O method
! Change to long int to avoid problems
Comment on lines 558 to +562
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can tidy this comment i21 for long ints.

write(string1,'(i21,1x,''i,j,k'',3(1x,i21),'' domain '',i2)') &
iloc, ix, iy, iz, dom_id

call get_state_meta_data(iloc, loc, var_type)
metadata_qty_string = trim(get_name_for_quantity(var_type))

Expand Down
189 changes: 162 additions & 27 deletions models/MITgcm_ocean/model_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module model_mod
get_close_state, get_close_obs, set_location, &
VERTISHEIGHT, get_location, is_vertical, &
convert_vertical_obs, convert_vertical_state

! EL use only nc_check was here, deleted for now for testing
use utilities_mod, only : error_handler, E_ERR, E_WARN, E_MSG, &
logfileunit, get_unit, do_output, to_upper, &
find_namelist_in_file, check_namelist_read, &
Expand Down Expand Up @@ -56,7 +56,10 @@ module model_mod
get_index_start, get_index_end, &
get_dart_vector_index, get_num_variables, &
get_domain_size, &
get_io_clamping_minval
get_io_clamping_minval, get_kind_index

use netcdf_utilities_mod, only : nc_open_file_readonly, nc_get_variable, &
nc_get_dimension_size, nc_close_file

use netcdf

Expand Down Expand Up @@ -255,9 +258,16 @@ module model_mod
! standard MITgcm namelist and filled in here.

integer :: Nx=-1, Ny=-1, Nz=-1 ! grid counts for each field
integer :: comp2d = -1, comp3d=-1, comp3dU = -1, comp3dV = -1 ! size of commpressed variables

! locations of cell centers (C) and edges (G) for each axis.
real(r8), allocatable :: XC(:), XG(:), YC(:), YG(:), ZC(:), ZG(:)
real(r4), allocatable :: XC_sq(:), YC_sq(:), XG_sq(:), YG_sq(:)
real(r8), allocatable :: ZC_sq(:)

integer, allocatable :: Xc_Ti(:), Yc_Ti(:), Zc_Ti(:)
integer, allocatable :: Xc_Ui(:), Yc_Ui(:), Zc_Ui(:)
integer, allocatable :: Xc_Vi(:), Yc_Vi(:), Zc_Vi(:)

real(r8) :: ocean_dynamics_timestep = 900.0_r4
integer :: timestepcount = 0
Expand All @@ -277,7 +287,6 @@ module model_mod
integer, parameter :: NUM_STATE_TABLE_COLUMNS = 5
character(len=vtablenamelength) :: mitgcm_variables(NUM_STATE_TABLE_COLUMNS, MAX_STATE_VARIABLES ) = ' '


character(len=256) :: model_shape_file = ' '
integer :: assimilation_period_days = 7
integer :: assimilation_period_seconds = 0
Expand All @@ -292,8 +301,9 @@ module model_mod
logical :: go_to_dart = .false.
logical :: do_bgc = .false.
logical :: log_transform = .false.
logical :: compress = .false.

namelist /trans_mitdart_nml/ go_to_dart, do_bgc, log_transform
namelist /trans_mitdart_nml/ go_to_dart, do_bgc, log_transform, compress

! /pkg/mdsio/mdsio_write_meta.F writes the .meta files
type MIT_meta_type
Expand Down Expand Up @@ -327,6 +337,7 @@ subroutine static_init_model()

integer :: i, iunit, io
integer :: ss, dd
integer :: ncid ! for reading compressed coordinates

! The Plan:
!
Expand Down Expand Up @@ -528,13 +539,62 @@ subroutine static_init_model()
domain_id = add_domain(model_shape_file, nvars, &
var_names, quantity_list, clamp_vals, update_list )

if (compress) then ! read in compressed coordinates

ncid = nc_open_file_readonly(model_shape_file)
comp2d = nc_get_dimension_size(ncid, 'comp2d' , 'static_init_model', model_shape_file)
comp3d = nc_get_dimension_size(ncid, 'comp3d' , 'static_init_model', model_shape_file)
comp3dU = nc_get_dimension_size(ncid, 'comp3dU', 'static_init_model', model_shape_file)
comp3dV = nc_get_dimension_size(ncid, 'comp3dV', 'static_init_model', model_shape_file)

allocate(XC_sq(comp3d))
allocate(YC_sq(comp3d))
allocate(ZC_sq(comp3d)) ! ZC is r8

allocate(XG_sq(comp3d))
allocate(YG_sq(comp3d))

allocate(Xc_Ti(comp3d))
allocate(Yc_Ti(comp3d))
allocate(Zc_Ti(comp3d))

allocate(Xc_Ui(comp3dU))
allocate(Yc_Ui(comp3dU))
allocate(Zc_Ui(comp3dU))

allocate(Xc_Vi(comp3dV))
allocate(Yc_Vi(comp3dV))
allocate(Zc_Vi(comp3dV))

call nc_get_variable(ncid, 'XCcomp', XC_sq)
call nc_get_variable(ncid, 'YCcomp', YC_sq)
call nc_get_variable(ncid, 'ZCcomp', ZC_sq)

call nc_get_variable(ncid, 'XGcomp', XG_sq)
call nc_get_variable(ncid, 'YGcomp', YG_sq)

call nc_get_variable(ncid, 'Xcomp_ind', Xc_Ti)
call nc_get_variable(ncid, 'Ycomp_ind', Yc_Ti)
call nc_get_variable(ncid, 'Zcomp_ind', Zc_Ti)

call nc_get_variable(ncid, 'Xcomp_indU', Xc_Ui)
call nc_get_variable(ncid, 'Ycomp_indU', Yc_Ui)
call nc_get_variable(ncid, 'Zcomp_indU', Zc_Ui)

call nc_get_variable(ncid, 'Xcomp_indV', Xc_Vi)
call nc_get_variable(ncid, 'Ycomp_indV', Yc_Vi)
call nc_get_variable(ncid, 'Zcomp_indV', Zc_Vi)

call nc_close_file(ncid)

endif

model_size = get_domain_size(domain_id)

if (do_output()) write(*,*) 'model_size = ', model_size

end subroutine static_init_model


function get_model_size()
!------------------------------------------------------------------
!
Expand Down Expand Up @@ -954,6 +1014,63 @@ function lon_dist(lon1, lon2)
end function lon_dist


function get_compressed_dart_vector_index(iloc, jloc, kloc, dom_id, var_id)
!=======================================================================
!

! returns the dart vector index for the compressed state

integer, intent(in) :: iloc, jloc, kloc
integer, intent(in) :: dom_id, var_id
integer(i8) :: get_compressed_dart_vector_index

integer :: i ! loop counter
integer :: qty
integer(i8) :: offset

offset = get_index_start(dom_id, var_id)

qty = get_kind_index(dom_id, var_id)

get_compressed_dart_vector_index = -1

! MEG: Using the already established compressed indices
!
! 2D compressed variables
if (qty == QTY_SEA_SURFACE_HEIGHT .or. qty == QTY_SURFACE_CHLOROPHYLL ) then
do i = 1, comp2d
if (Xc_Ti(i) == iloc .and. Yc_Ti(i) == jloc .and. Zc_Ti(i) == 1) then
get_compressed_dart_vector_index = offset + i - 1
endif
enddo
return
endif

! 3D compressed variables
if (qty == QTY_U_CURRENT_COMPONENT) then
do i = 1, comp3dU
if (Xc_Ui(i) == iloc .and. Yc_Ui(i) == jloc .and. Zc_Ui(i) == kloc) then
get_compressed_dart_vector_index = offset + i - 1
endif
enddo
elseif (qty == QTY_V_CURRENT_COMPONENT) then
do i = 1, comp3dV
if (Xc_Vi(i) == iloc .and. Yc_Vi(i) == jloc .and. Zc_Vi(i) == kloc) then
get_compressed_dart_vector_index = offset + i - 1
endif
enddo
else
do i = 1, comp3d
if (Xc_Ti(i) == iloc .and. Yc_Ti(i) == jloc .and. Zc_Ti(i) == kloc) then
get_compressed_dart_vector_index = offset + i - 1
endif
enddo
endif


end function get_compressed_dart_vector_index


function get_val(lon_index, lat_index, level, var_id, state_handle,ens_size, masked)
!=======================================================================
!
Expand All @@ -971,24 +1088,28 @@ function get_val(lon_index, lat_index, level, var_id, state_handle,ens_size, mas

if ( .not. module_initialized ) call static_init_model

state_index = get_dart_vector_index(lon_index, lat_index, level, domain_id, var_id)
get_val = get_state(state_index,state_handle)
masked = .false.

! Masked returns false if the value is masked
! A grid variable is assumed to be masked if its value is FVAL.
! Just to maintain legacy, we also assume that A grid variable is assumed
! to be masked if its value is exactly 0.
! See discussion in lat_lon_interpolate.
if (compress) then

! MEG CAUTION: THE ABOVE STATEMENT IS INCORRECT
! trans_mitdart already looks for 0.0 and makes them FVAL
! So, in the condition below we don't need to check for zeros
! The only mask is FVAL
masked = .false.
do i=1,ens_size
! if(get_val(i) == FVAL .or. get_val(i) == 0.0_r8 ) masked = .true.
if(get_val(i) == FVAL) masked = .true.
enddo
state_index = get_compressed_dart_vector_index(lon_index, lat_index, level, domain_id, var_id)

if (state_index .ne. -1) then
get_val = get_state(state_index,state_handle)
else
masked = .true.
endif

else

state_index = get_dart_vector_index(lon_index, lat_index, level, domain_id, var_id)
get_val = get_state(state_index,state_handle)

do i=1,ens_size ! HK this is checking the whole ensemble, can you have different masks for each ensemble member?
if(get_val(i) == FVAL) masked = .true.
enddo

endif

end function get_val

Expand Down Expand Up @@ -1079,16 +1200,28 @@ subroutine get_state_meta_data(index_in, location, qty)

call get_model_variable_indices(index_in, iloc, jloc, kloc, kind_index = qty)

lon = XC(iloc)
lat = YC(jloc)
depth = ZC(kloc)
if (compress) then ! all variables ae 1D
lon = XC_sq(iloc)
lat = YC_sq(iloc)
depth = ZC_sq(iloc)
! Acounting for variables those on staggered grids
if (qty == QTY_U_CURRENT_COMPONENT) lon = XG_sq(iloc)
if (qty == QTY_V_CURRENT_COMPONENT) lat = YG_sq(iloc)
else

lon = XC(iloc)
lat = YC(jloc)
depth = ZC(kloc)

! Acounting for variables those on staggered grids
if (qty == QTY_U_CURRENT_COMPONENT) lon = XG(iloc)
if (qty == QTY_V_CURRENT_COMPONENT) lat = YG(jloc)

endif

! Acounting for surface variables and those on staggered grids
! MEG: check chl's depth here
if (qty == QTY_SEA_SURFACE_HEIGHT .or. &
qty == QTY_SURFACE_CHLOROPHYLL) depth = 0.0_r8
if (qty == QTY_U_CURRENT_COMPONENT) lon = XG(iloc)
if (qty == QTY_V_CURRENT_COMPONENT) lat = YG(jloc)

location = set_location(lon, lat, depth, VERTISHEIGHT)

Expand Down Expand Up @@ -1297,6 +1430,8 @@ end subroutine nc_write_model_atts
!------------------------------------------------------------------
! Create an ensemble of states from a single state.

! Note if you perturb a compressed state, this will not be bitwise
! with perturbing a non-compressed state.
subroutine pert_model_copies(state_ens_handle, ens_size, pert_amp, interf_provided)

type(ensemble_type), intent(inout) :: state_ens_handle
Expand Down
1 change: 1 addition & 0 deletions models/MITgcm_ocean/model_mod.nml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
assimilation_period_days = 7
assimilation_period_seconds = 0
model_perturbation_amplitude = 0.2
model_shape_file = 'mem01_reduced.nc'
/

6 changes: 6 additions & 0 deletions models/MITgcm_ocean/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ can be set in the ``&trans_mitdart_nml`` namelist in ``input.nml``.
&trans_mitdart_nml
do_bgc = .false. ! change to .true. if doing bio-geo-chemistry
log_transform = .false. ! change to .true. if using log_transform
compress = .false. ! change to .true. to compress the state vector
/

``compress = .true.`` can be used to generate netcdf files for use with DART which has missing values (land) removed.
For some datasets this reduces the state vector size significantly. For example, the state vector size is
reduced by approximately 90% for the Red Sea. The program ``expand_netcdf`` can be used to uncompress the netcdf
file to view the data in a convenient form.


.. Warning::

Expand Down
Loading