Skip to content

Commit

Permalink
Adding int2real function and new save functions for split
Browse files Browse the repository at this point in the history
  • Loading branch information
natgeo-wong committed Jun 20, 2022
1 parent 100d25e commit 47f00d3
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ function ncoffsetscale(data::AbstractArray{<:Real})

end

function int2real!(
oarray :: AbstractArray{FT},
iarray :: AbstractArray{Int16};
scale :: Real,
offset :: Real,
fvalue :: Int16,
mvalue :: Int16
) where FT <: Real

for ii = 1 : length(iarray)

if (iarray[ii] == fvalue) || (iarray[ii] == mvalue)
oarray[ii] = FT(NaN)
else; oarray[ii] = iarray[ii] * scale + offset
end

end

return

end

function real2int16!(
oarray :: AbstractArray{Int16},
iarray :: AbstractArray{<:Real},
Expand Down
94 changes: 93 additions & 1 deletion src/downloads/split.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,101 @@ function split(

p = pvec[ip]
evarii = PressureVariable(evar.varID,hPa=p)
save(dataflt,dt,e5ds,evarii,ereg,lsd)
save(dataflt,dt,e5ds,evarii,ereg,lsd,sc,of)
end

close(ds)

end

function save(
data :: AbstractArray{<:Real,3},
dt :: Date,
e5ds :: ERA5Hourly,
evar :: ERA5Variable,
ereg :: ERA5Region,
lsd :: LandSea,
scale :: Real,
offset :: Real
)

@info "$(modulelog()) - Saving raw $(e5ds.lname) $(evar.vname) data in $(ereg.geo.name) (Horizontal Resolution: $(ereg.gres)) for $(year(dt)) $(Dates.monthname(dt)) ..."

ds,fnc = save_createds(e5ds,evar,ereg,dt)

nhr = 24 * daysinmonth(dt)

ds.dim["longitude"] = length(lsd.lon)
ds.dim["latitude"] = length(lsd.lat)
ds.dim["time"] = nhr

nclon,nclat = save_definelonlat!(ds)

nctime = defVar(ds,"time",Int32,("time",),attrib = Dict(
"units" => "hours since $(dt) 00:00:00.0",
"long_name" => "time",
"calendar" => "gregorian",
))

ncvar = save_definevar!(ds,evar,scale,offset)

nclon[:] = lsd.lon
nclat[:] = lsd.lat
nctime[:] = collect(1:nhr) .- 1

if iszero(sum(isnan.(data)))
ncvar[:] = data
else; ncvar.var[:] = real2int16(data,scale,offset)
end

close(ds)

@info "$(modulelog()) - Raw $(uppercase(e5ds.lname)) $(evar.vname) in $(ereg.geo.name) (Horizontal Resolution: $(ereg.gres)) for $(year(dt)) $(Dates.monthname(dt)) has been saved into $(fnc)."

end

function save(
data :: AbstractArray{<:Real,3},
dt :: Date,
e5ds :: ERA5Monthly,
evar :: ERA5Variable,
ereg :: ERA5Region,
lsd :: LandSea,
scale :: Real,
offset :: Real
)

@info "$(modulelog()) - Saving raw $(e5ds.lname) $(evar.vname) data in $(ereg.geo.name) (Horizontal Resolution: $(ereg.gres)) for $(year(dt)) ..."

ds,fnc = save_createds(e5ds,evar,ereg,dt)

nt = 12; if e5ds.hours; nt = nt * 24 end

ds.dim["longitude"] = length(lsd.lon)
ds.dim["latitude"] = length(lsd.lat)
ds.dim["time"] = nt

nclon,nclat = save_definelonlat!(ds)

nctime = defVar(ds,"time",Int32,("time",),attrib = Dict(
"units" => "hours since $(dt) 00:00:00.0",
"long_name" => "time",
"calendar" => "gregorian",
))

ncvar = save_definevar!(ds,evar,scale,offset)

nclon[:] = lsd.lon
nclat[:] = lsd.lat
nctime[:] = save_definetimes(e5ds,dt)

if iszero(sum(isnan.(data)))
ncvar[:] = data
else; ncvar.var[:] = real2int16(data,scale,offset)
end

close(ds)

@info "$(modulelog()) - Raw $(uppercase(e5ds.lname)) $(evar.vname) in $(ereg.geo.name) (Horizontal Resolution: $(ereg.gres)) for $(year(dt)) has been saved into $(fnc)."

end

2 comments on commit 47f00d3

@natgeo-wong
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release Notes:

  • Adding analysis and compilation function for hourly and monthly data
  • Downloading pressure variables at multiple pressure levels at one shot and splitting into individual levels
  • Updated time-vector during the saving of data into files

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/63039

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.3 -m "<description of version>" 47f00d359a9d4482c1974a628a664a07ab0f25bc
git push origin v0.1.3

Please sign in to comment.