Skip to content

Commit 711bb61

Browse files
Merge pull request #169 from milankl/H-output
Option to output H
2 parents 18e240f + 4186d7a commit 711bb61

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

src/default_parameters.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
# OUTPUT OPTIONS
106106
output::Bool=false # netcdf output?
107-
output_vars::Array{String,1}=["u","v","η","sst"] # which variables to output? "du","dv","dη" also allowed
107+
output_vars::Array{String,1}=["u","v","η","sst"] # which variables to output? "du","dv","dη","H","ζ" also allowed
108108
output_dt::Real=24 # output time step [hours]
109109
outpath::String=pwd() # path to output folder
110110
compression_level::Int=3 # compression level

src/output.jl

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ struct NcFiles
88
du::Union{NcFile,Nothing} # tendency of u [m^2/s^2]
99
dv::Union{NcFile,Nothing} # tendency of v [m^2/s^2]
1010
::Union{NcFile,Nothing} # tendency of η [m^2/s]
11+
H::Union{NcFile,Nothing} # layer thickness at rest [m]
1112
iout::Array{Integer,1} # output index, stored in array for mutability
1213
end
1314

1415
"""Generator function for "empty" NcFiles struct."""
15-
NcFiles(x::Nothing) = NcFiles(x,x,x,x,x,x,x,x,x,[0])
16+
NcFiles(x::Nothing) = NcFiles(x,x,x,x,x,x,x,x,x,x,[0])
1617

1718
"""Generator function for NcFiles struct, creating the underlying netCDF files."""
1819
function NcFiles(feedback::Feedback,S::ModelSetup)
@@ -35,16 +36,22 @@ function NcFiles(feedback::Feedback,S::ModelSetup)
3536
ncdu = if "du" in output_vars nc_create(x_u,y_u,"du",runpath,"m^2/s^2","zonal velocity tendency",P) else nothing end
3637
ncdv = if "dv" in output_vars nc_create(x_v,y_v,"dv",runpath,"m^2/s^2","meridional velocity tendency",P) else nothing end
3738
ncdη = if "" in output_vars nc_create(x_T,y_T,"deta",runpath,"m^2/s","sea surface height tendency",P) else nothing end
39+
nH = if "H" in output_vars nc_create(x_T,y_T,"H",runpath,"m","undisturbed layer thickness",P,false) else nothing end
3840

3941
for nc in (ncu,ncv,ncη,ncsst,ncq,ncζ,ncdu,ncdv,ncdη)
40-
if nc != nothing
42+
if !isnothing(nc)
4143
NetCDF.putatt(nc,"t",Dict("units"=>"s","long_name"=>"time"))
4244
NetCDF.putatt(nc,"x",Dict("units"=>"m","long_name"=>"zonal coordinate"))
4345
NetCDF.putatt(nc,"y",Dict("units"=>"m","long_name"=>"meridional coordinate"))
4446
end
4547
end
4648

47-
return NcFiles(ncu,ncv,ncη,ncsst,ncq,ncζ,ncdu,ncdv,ncdη,[0])
49+
if !isnothing(nH) # output constant fields
50+
NetCDF.putatt(nH,"x",Dict("units"=>"m","long_name"=>"zonal coordinate"))
51+
NetCDF.putatt(nH,"y",Dict("units"=>"m","long_name"=>"meridional coordinate"))
52+
end
53+
54+
return NcFiles(ncu,ncv,ncη,ncsst,ncq,ncζ,ncdu,ncdv,ncdη,nH,[0])
4855
else
4956
return NcFiles(nothing)
5057
end
@@ -57,18 +64,24 @@ function nc_create( x::Array{T,1},
5764
path::String,
5865
unit::String,
5966
long_name::String,
60-
P::Parameter) where {T<:Real}
67+
P::Parameter,
68+
output_tdim::Bool=true) where {T<:Real}
6169

6270
@unpack compression_level = P
6371

6472
xdim = NcDim("x",length(x),values=x)
6573
ydim = NcDim("y",length(y),values=y)
66-
tdim = NcDim("t",0,unlimited=true)
74+
75+
if output_tdim
76+
tdim = NcDim("t",0,unlimited=true)
77+
var = NcVar(name,[xdim,ydim,tdim],t=Float32,compress=compression_level)
78+
tvar = NcVar("t",tdim,t=Int32)
79+
nc = NetCDF.create(joinpath(path,name*".nc"),[var,tvar],mode=NC_NETCDF4)
80+
else
81+
var = NcVar(name,[xdim,ydim],t=Float32,compress=compression_level)
82+
nc = NetCDF.create(joinpath(path,name*".nc"),[var],mode=NC_NETCDF4)
83+
end
6784

68-
var = NcVar(name,[xdim,ydim,tdim],t=Float32,compress=compression_level)
69-
tvar = NcVar("t",tdim,t=Int32)
70-
71-
nc = NetCDF.create(joinpath(path,name*".nc"),[var,tvar],mode=NC_NETCDF4)
7285
# add missing_value although irrelevant for ncview compatibility
7386
NetCDF.putatt(nc,name,Dict("units"=>unit,"long_name"=>long_name,"missing_value"=>-999999f0))
7487
return nc
@@ -96,51 +109,55 @@ function output_nc!(i::Int,
96109
# As output is before copyto!(u,u0), take u0,v0,η0
97110
# Tendencies calculate from the last time step, du = u_n+1-u_n etc
98111
# WRITING THE VARIABLES
99-
if ncs.u != nothing
112+
if !isnothing(ncs.u)
100113
@views u = Float32.(scale_inv*Diag.RungeKutta.u0[halo+1:end-halo,halo+1:end-halo])
101114
NetCDF.putvar(ncs.u,"u",u,start=[1,1,iout],count=[-1,-1,1])
102115
end
103-
if ncs.v != nothing
116+
if !isnothing(ncs.v)
104117
@views v = Float32.(scale_inv*Diag.RungeKutta.v0[halo+1:end-halo,halo+1:end-halo])
105118
NetCDF.putvar(ncs.v,"v",v,start=[1,1,iout],count=[-1,-1,1])
106119
end
107-
if ncs.η != nothing
120+
if !isnothing(ncs.η)
108121
@views η = Float32.(Diag.RungeKutta.η0[haloη+1:end-haloη,haloη+1:end-haloη])
109122
NetCDF.putvar(ncs.η,"eta",η,start=[1,1,iout],count=[-1,-1,1])
110123
end
111-
if ncs.sst != nothing
124+
if !isnothing(ncs.sst)
112125
@views sst = Float32.(Prog.sst[halosstx+1:end-halosstx,halossty+1:end-halossty]/scale_sst)
113126
NetCDF.putvar(ncs.sst,"sst",sst,start=[1,1,iout],count=[-1,-1,1])
114127
end
115-
if ncs.q != nothing
128+
if !isnothing(ncs.q)
116129
@views q = Float32.(scale_inv*Diag.Vorticity.q[haloη+1:end-haloη,haloη+1:end-haloη])
117130
NetCDF.putvar(ncs.q,"q",q,start=[1,1,iout],count=[-1,-1,1])
118131
end
119-
if ncs.ζ != nothing
132+
if !isnothing(ncs.ζ)
120133
@unpack dvdx,dudy = Diag.Vorticity
121134
@unpack f_q = S.grid
122135
@views ζ = Float32.((dvdx[2:end-1,2:end-1]-dudy[2+ep:end-1,2:end-1])./abs.(f_q))
123136
NetCDF.putvar(ncs.ζ,"relvort",ζ,start=[1,1,iout],count=[-1,-1,1])
124137
end
125-
if ncs.du != nothing
138+
if !isnothing(ncs.du)
126139
@views u = Float32.(scale_inv*Diag.RungeKutta.u0[halo+1:end-halo,halo+1:end-halo])
127140
@views du = u-Float32.(scale_inv*Prog.u[halo+1:end-halo,halo+1:end-halo])
128141
NetCDF.putvar(ncs.du,"du",du,start=[1,1,iout],count=[-1,-1,1])
129142
end
130-
if ncs.dv != nothing
143+
if !isnothing(ncs.dv)
131144
@views v = Float32.(scale_inv*Diag.RungeKutta.v0[halo+1:end-halo,halo+1:end-halo])
132145
@views dv = v-Float32.(scale_inv*Prog.v[halo+1:end-halo,halo+1:end-halo])
133146
NetCDF.putvar(ncs.dv,"dv",dv,start=[1,1,iout],count=[-1,-1,1])
134147
end
135-
if ncs. != nothing
148+
if !isnothing(ncs.)
136149
@views η = Float32.(Diag.RungeKutta.η0[haloη+1:end-haloη,haloη+1:end-haloη])
137150
@views= η-Float32.(Prog.η[haloη+1:end-haloη,haloη+1:end-haloη])
138151
NetCDF.putvar(ncs.dη,"deta",dη,start=[1,1,iout],count=[-1,-1,1])
139152
end
153+
if i==0 && !isnothing(ncs.H) # constant field, output only once, before the timestepping
154+
@views H = Float32.(S.forcing.H[haloη+1:end-haloη,haloη+1:end-haloη])
155+
NetCDF.putvar(ncs.H,"H",H,start=[1,1],count=[-1,-1])
156+
end
140157

141158
# WRITING THE TIME
142159
for nc in (ncs.u,ncs.v,ncs.η,ncs.sst,ncs.q,ncs.ζ,ncs.du,ncs.dv,ncs.dη)
143-
if nc != nothing
160+
if !isnothing(nc)
144161
NetCDF.putvar(nc,"t",Int64[i*dtint],start=[iout])
145162
NetCDF.sync(nc) # sync to view netcdf while model is still running
146163
end

0 commit comments

Comments
 (0)