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

Read ECCO-Darwin field from NASA Earthdata #328

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/DataWrangling/ECCO/ECCO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function ECCO_field(metadata::ECCOMetadata;
# ECCO4 data is on a -180, 180 longitude grid as opposed to ECCO2 data that
# is on a 0, 360 longitude grid. To make the data consistent, we shift ECCO4
# data by 180 degrees in longitude
if metadata.version isa ECCO4Monthly
if metadata.version isa ECCO4Monthly || metadata.version isa ECCO4DarwinMonthly
Copy link
Member

Choose a reason for hiding this comment

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

Will be slightly cleaner to use abstract type or type union to express this. This pattern does not generalize well; this makes it more work to change how we express version for example.

Copy link
Author

Choose a reason for hiding this comment

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

as in if metadata.version isa Union{ECCO4Monthly, ECCO4DarwinMonthly}?

Nx = size(data, 1)
if variable_is_three_dimensional(metadata)
shift = (Nx ÷ 2, 0, 0)
Expand Down
32 changes: 19 additions & 13 deletions src/DataWrangling/ECCO/ECCO_darwin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Base.size(data::ECCOMetadata{<:Any, <:ECCO4DarwinMonthly}) = (720, 360, 50, leng
function metadata_filename(metadata::ECCOMetadata{<:AbstractCFDateTime, <:ECCO4DarwinMonthly})
shortname = short_name(metadata)

reference_date = DateTimeProlepticGregorian(1992, 1, 1, 0, 0, 0)
reference_date = DateTimeProlepticGregorian(1992, 1, 1, 12, 0, 0)
timestep_size = 3600

iternum = Dates.value((metadata.dates - reference_date) / (timestep_size * 1e3))
Expand Down Expand Up @@ -37,14 +37,14 @@ ECCO_darwin_short_names = Dict(
)

ECCO_darwin_scale_factor = Dict(
:DIC => 1000,
:ALK => 1000,
:PO₄ => 1000,
:NO₃ => 1000,
:DOP => 1000,
:POP => 1000,
:Fe => 1000,
:Siᵀ => 1000,
:DIC => 1e-3,
:ALK => 1e-3,
:PO₄ => 1e-3,
:NO₃ => 1e-3,
:DOP => 1e-3,
:POP => 1e-3,
:Fe => 1e-3,
:Siᵀ => 1e-3,
)

# URLs for the ECCO datasets specific to each version
Expand All @@ -61,25 +61,31 @@ Read a ECCO4DarwinMonthly data file and regrid using MeshArrays on to regular la
function ECCO_darwin_model_data(metadata, path)
native_size = ECCO_darwin_native_size(metadata.version)
native_grid = ECCO_darwin_native_grid(metadata.version)
native_data = zeroes(Float32, prod(native_size)) # Native LLC90 grid at precision of the input binary file
native_data = zeros(Float32, prod(native_size)) # Native LLC90 grid at precision of the input binary file

read!(path, native_data)
native_data = bswap.(native_data)

meshed_data = read(reshape(native_data, native_size...), native_grid)
Nx, Ny, Nz, _ = size(metadata)
coeffs = interpolation_setup()
ll_data = zeroes(Float32, Nx, Ny, Nz) # Native LLC90 grid at precision of the input binary file
data = zeros(Float32, Nx, Ny, Nz) # Native LLC90 grid at precision of the input binary file

# Interpolate each layer
for k in 1:Nz
i, j, c = Interpolate(meshed_data[:, k], coeffs)
ll_data[:, :, k] = c
data[:, :, k] = c
end

# Reverse the z-axis
data = reverse(data, dims=3)

# Fill NaNs in Antarctica with zeros
data[isnan.(data)] .= 0.f0

scale_factor = ECCO_darwin_scale_factor[metadata.name]
# Scale data according to metadata.scale_factor
return ll_data .* scale_factor
return data .* scale_factor
end

retrieve_data(metadata::ECCOMetadata{<:Any, <:ECCO4DarwinMonthly}, path) = ECCO_darwin_model_data(metadata, path)
2 changes: 1 addition & 1 deletion src/DataWrangling/ECCO/ECCO_mask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function ECCO_mask(metadata, architecture = CPU();

# ECCO4 has zeros in place of the missing values, while
# ECCO2 expresses missing values with values < -1e5
if metadata.version isa ECCO4Monthly
if metadata.version isa ECCO4Monthly || metadata.version isa ECCO4DarwinMonthly
_set_mask! = _set_ECCO4_mask!
else
_set_mask! = _set_ECCO2_mask!
Expand Down
7 changes: 6 additions & 1 deletion test/test_ecco.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using Dates
using ClimaOcean

using ClimaOcean.ECCO
using ClimaOcean.ECCO: ECCO_field, metadata_path, ECCO_times
using ClimaOcean.ECCO: ECCO_field, metadata_path, ECCO_times, ECCO4DarwinMonthly
using ClimaOcean.DataWrangling: NearestNeighborInpainting

using Oceananigans.Grids: topology
Expand Down Expand Up @@ -138,6 +138,11 @@ end
set!(field, ECCOMetadata(:salinity))
true
end

@test begin
set!(field, ECCOMetadata(:DIC; version=ECCO4DarwinMonthly()))
true
end
end
end

Expand Down
Loading