Skip to content

Commit 7ffe647

Browse files
GFSv16 netcdf post ficein cpl (ufs-community#48)
* fv3atm issue ufs-community#37: fix the real(8) lat/lon in netcdf file * fv3atm ufs-community#35: Reducing background vertical diffusivities in the inversion layers * fv3atm ufs-community#24: bug in gfsphysics/physics/moninedmf_hafs.f * fv3atm ufs-community#18: Optimize netcdf write component and bugfix for post and samfdeepcnv.f * set (0-1) bounds for ficein_cpl * remove cache_size due to lower netcdf verion 4.5.1 on mars * Change ice falling to 0.9 in gfsphysics/physics/gfdl_cloud_microphys.F90
1 parent b1ddd67 commit 7ffe647

18 files changed

+148
-104
lines changed

.gitmodules

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
[submodule "ccpp/framework"]
66
path = ccpp/framework
77
url = https://github.com/NCAR/ccpp-framework
8+
branch = master
89
[submodule "ccpp/physics"]
910
path = ccpp/physics
1011
url = https://github.com/NCAR/ccpp-physics
12+
branch = master

atmos_model.F90

+1-1
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ subroutine assign_importdata(rc)
17201720
IPD_Data(nb)%Coupling%ficein_cpl(ix) = zero
17211721
if (IPD_Data(nb)%Sfcprop%oceanfrac(ix) > zero) then
17221722
if (datar8(i,j) >= IPD_control%min_seaice*IPD_Data(nb)%Sfcprop%oceanfrac(ix)) then
1723-
IPD_Data(nb)%Coupling%ficein_cpl(ix) = datar8(i,j)
1723+
IPD_Data(nb)%Coupling%ficein_cpl(ix) = max(zero, min(datar8(i,j),one))
17241724
! if (IPD_Data(nb)%Sfcprop%oceanfrac(ix) == one) IPD_Data(nb)%Sfcprop%slmsk(ix) = 2. !slmsk=2 crashes in gcycle on partial land points
17251725
IPD_Data(nb)%Sfcprop%slmsk(ix) = 2. !slmsk=2 crashes in gcycle on partial land points
17261726
IPD_Data(nb)%Coupling%slimskin_cpl(ix) = 4.

gfsphysics/physics/gfdl_cloud_microphys.F90

+1-1
Original file line numberDiff line numberDiff line change
@@ -3266,7 +3266,7 @@ subroutine fall_speed (ktop, kbot, den, qs, qi, qg, ql, tk, vts, vti, vtg)
32663266
else
32673267
tc (k) = tk (k) - tice
32683268
vti (k) = (3. + log10 (qi (k) * den (k))) * (tc (k) * (aa * tc (k) + bb) + cc) + dd * tc (k) + ee
3269-
vti (k) = vi0 * exp (log_10 * vti (k)) * 0.8
3269+
vti (k) = vi0 * exp (log_10 * vti (k)) * 0.9
32703270
vti (k) = min (vi_max, max (vf_min, vti (k)))
32713271
endif
32723272
enddo

gfsphysics/physics/module_sf_noahmp_glacier.f90

100755100644
File mode changed.

gfsphysics/physics/module_sf_noahmplsm.f90

100755100644
File mode changed.

gfsphysics/physics/module_wrf_utl.f90

100755100644
File mode changed.

gfsphysics/physics/moninedmf_hafs.f

+5-1
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,11 @@ subroutine moninedmf_hafs(ix,im,km,ntrac,ntcw,dv,du,tau,rtg, &
13601360
tem = 0.5 * (diss(i,k-1)+diss(i,k))
13611361
tem = max(tem, 0.)
13621362
ttend = tem / cp
1363-
tau(i,k) = tau(i,k) + 0.5*ttend
1363+
if (alpha .gt. 0.0) then
1364+
tau(i,k) = tau(i,k) + 0.5*ttend
1365+
else
1366+
tau(i,k) = tau(i,k) + 0.7*ttend ! in HWRF/HMON, use 0.7
1367+
endif
13641368
enddo
13651369
enddo
13661370
!

gfsphysics/physics/noahmp_tables.f90

100755100644
File mode changed.

gfsphysics/physics/samfdeepcnv.f

+14-14
Original file line numberDiff line numberDiff line change
@@ -1547,22 +1547,22 @@ subroutine samfdeepcnv(im,ix,km,delt,itc,ntc,ntk,ntr,delp,
15471547
enddo
15481548
enddo
15491549
do i = 1, im
1550-
betamn = betas
1551-
if(islimsk(i) == 1) betamn = betal
1552-
if(ntk > 0) then
1553-
betamx = betamn + dbeta
1554-
if(tkemean(i) > tkemx) then
1555-
beta = betamn
1556-
else if(tkemean(i) < tkemn) then
1557-
beta = betamx
1550+
if(cnvflg(i)) then
1551+
betamn = betas
1552+
if(islimsk(i) == 1) betamn = betal
1553+
if(ntk > 0) then
1554+
betamx = betamn + dbeta
1555+
if(tkemean(i) > tkemx) then
1556+
beta = betamn
1557+
else if(tkemean(i) < tkemn) then
1558+
beta = betamx
1559+
else
1560+
tem = (betamx - betamn) * (tkemean(i) - tkemn)
1561+
beta = betamx - tem / dtke
1562+
endif
15581563
else
1559-
tem = (betamx - betamn) * (tkemean(i) - tkemn)
1560-
beta = betamx - tem / dtke
1564+
beta = betamn
15611565
endif
1562-
else
1563-
beta = betamn
1564-
endif
1565-
if(cnvflg(i)) then
15661566
dz = (sumx(i)+zi(i,1))/float(kbcon(i))
15671567
tem = 1./float(kbcon(i))
15681568
xlamd(i) = (1.-beta**tem)/dz

gfsphysics/physics/satmedmfvdifq.f

+7-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ subroutine satmedmfvdifq(ix,im,km,ntrac,ntcw,ntiw,ntke,
148148
& epsi, beta, chx, cqx,
149149
& rdt, rdz, qmin, qlmin,
150150
& rimin, rbcr, rbint, tdzmin,
151-
& rlmn, rlmn1, rlmx, elmx,
151+
& rlmn, rlmn1, rlmn2,
152+
& rlmx, elmx,
152153
& ttend, utend, vtend, qtend,
153154
& zfac, zfmin, vk, spdk2,
154155
& tkmin, tkminx, xkzinv, xkgdx,
@@ -172,7 +173,8 @@ subroutine satmedmfvdifq(ix,im,km,ntrac,ntcw,ntiw,ntke,
172173
parameter(gamcrt=3.,gamcrq=0.,sfcfrac=0.1)
173174
parameter(vk=0.4,rimin=-100.)
174175
parameter(rbcr=0.25,zolcru=-0.02,tdzmin=1.e-3)
175-
parameter(rlmn=30.,rlmn1=5.,rlmx=300.,elmx=300.)
176+
parameter(rlmn=30.,rlmn1=5.,rlmn2=10.)
177+
parameter(rlmx=300.,elmx=300.)
176178
parameter(prmin=0.25,prmax=4.0)
177179
parameter(pr0=1.0,prtke=1.0,prscu=0.67)
178180
parameter(f0=1.e-4,crbmin=0.15,crbmax=0.35)
@@ -698,8 +700,9 @@ subroutine satmedmfvdifq(ix,im,km,ntrac,ntcw,ntiw,ntke,
698700
! if(tem1 > 1.e-5) then
699701
tem1 = tvx(i,k+1)-tvx(i,k)
700702
if(tem1 > 0.) then
701-
xkzo(i,k) = min(xkzo(i,k),xkzinv)
702-
xkzmo(i,k) = min(xkzmo(i,k),xkzinv)
703+
xkzo(i,k) = min(xkzo(i,k), xkzinv)
704+
xkzmo(i,k) = min(xkzmo(i,k), xkzinv)
705+
rlmnz(i,k) = min(rlmnz(i,k), rlmn2)
703706
endif
704707
enddo
705708
enddo

gfsphysics/physics/sfc_noahmp_drv.f

100755100644
File mode changed.

io/FV3GFS_io.F90

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ subroutine FV3GFS_IPD_checksum (Model, IPD_Data, Atm_block)
285285
temp2d(i,j,55) = IPD_Data(nb)%Coupling%visbmui(ix)
286286
temp2d(i,j,56) = IPD_Data(nb)%Coupling%visdfui(ix)
287287
temp2d(i,j,57) = IPD_Data(nb)%Coupling%sfcdsw(ix)
288-
temp2d(i,j,59) = IPD_Data(nb)%Coupling%sfcnsw(ix)
288+
temp2d(i,j,58) = IPD_Data(nb)%Coupling%sfcnsw(ix)
289289
temp2d(i,j,59) = IPD_Data(nb)%Coupling%sfcdlw(ix)
290290
temp2d(i,j,60) = IPD_Data(nb)%Grid%xlon(ix)
291291
temp2d(i,j,61) = IPD_Data(nb)%Grid%xlat(ix)

io/module_write_nemsio.F90

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ subroutine nemsio_first_call(fieldbundle, imo, jmo, &
5151
integer, intent(in) :: wrt_mype, wrt_ntasks, wrt_mpi_comm
5252
integer, intent(in) :: wrt_nbdl, mybdl
5353
integer, intent(in) :: inidate(7)
54-
real, intent(in) :: lat(:), lon(:)
54+
real(8), intent(in) :: lat(:), lon(:)
5555
integer, optional,intent(out) :: rc
5656

5757
!** local vars

io/module_write_netcdf.F90

+61-54
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
4242
real(4), dimension(:,:,:), allocatable :: arrayr4_3d,arrayr4_3d_save
4343
real(8), dimension(:,:,:), allocatable :: arrayr8_3d
4444

45-
real(8) rad2dg,x(im),y(jm)
45+
real(8) x(im),y(jm)
4646
integer :: fieldCount, fieldDimCount, gridDimCount
4747
integer, dimension(:), allocatable :: ungriddedLBound, ungriddedUBound
4848

@@ -56,7 +56,8 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
5656
character(len=ESMF_MAXSTR) :: attName, fldName
5757

5858
integer :: varival
59-
real(4) :: varr4val, scale_fact, compress_err, offset, dataMin, dataMax
59+
real(4) :: varr4val, scale_fact, offset, dataMin, dataMax
60+
real(4), allocatable, dimension(:) :: compress_err
6061
real(8) :: varr8val
6162
character(len=ESMF_MAXSTR) :: varcval
6263

@@ -71,10 +72,10 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
7172
!
7273
!!
7374
!
74-
rad2dg = 45./atan(1.0)
7575

7676
call ESMF_FieldBundleGet(fieldbundle, fieldCount=fieldCount, rc=rc); ESMF_ERR_RETURN(rc)
7777

78+
allocate(compress_err(fieldCount)); compress_err=-999.
7879
allocate(fldlev(fieldCount)) ; fldlev = 0
7980
allocate(fcstField(fieldCount))
8081
allocate(varids(fieldCount))
@@ -117,13 +118,13 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
117118
if (mype==0) then
118119

119120
if (ideflate == 0) then
120-
ncerr = nf90_create(trim(filename), cmode=IOR(NF90_CLOBBER,NF90_64BIT_OFFSET), &
121+
ncerr = nf90_create(trim(filename), cmode=IOR(IOR(NF90_CLOBBER,NF90_64BIT_OFFSET),NF90_SHARE), &
121122
ncid=ncid); NC_ERR_STOP(ncerr)
122123
ncerr = nf90_set_fill(ncid, NF90_NOFILL, oldMode); NC_ERR_STOP(ncerr)
123124
else
124125
ncerr = nf90_create(trim(filename), cmode=IOR(IOR(NF90_CLOBBER,NF90_NETCDF4),NF90_CLASSIC_MODEL), &
125126
ncid=ncid); NC_ERR_STOP(ncerr)
126-
! if compression on use HDF5 format with default _FillValue
127+
ncerr = nf90_set_fill(ncid, NF90_NOFILL, oldMode); NC_ERR_STOP(ncerr)
127128
endif
128129

129130
! define dimensions
@@ -156,28 +157,32 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
156157
! define variables
157158
if (fldlev(i) == 1) then
158159
if (typekind == ESMF_TYPEKIND_R4) then
159-
ncerr = nf90_def_var(ncid, trim(fldName), NF90_FLOAT, &
160-
(/im_dimid,jm_dimid,time_dimid/), varids(i)); NC_ERR_STOP(ncerr)
161160
if (ideflate > 0) then
162-
! shuffle filter on for lossless compression
163-
ncerr = nf90_def_var_deflate(ncid, varids(i), 1, 1, ideflate)
164-
NC_ERR_STOP(ncerr)
161+
ncerr = nf90_def_var(ncid, trim(fldName), NF90_FLOAT, &
162+
(/im_dimid,jm_dimid,time_dimid/), varids(i), &
163+
shuffle=.true.,deflate_level=ideflate, &
164+
chunksizes=(/im,jm,1/)); NC_ERR_STOP(ncerr)
165+
else
166+
ncerr = nf90_def_var(ncid, trim(fldName), NF90_FLOAT, &
167+
(/im_dimid,jm_dimid,time_dimid/), varids(i)); NC_ERR_STOP(ncerr)
165168
endif
166169
else if (typekind == ESMF_TYPEKIND_R8) then
167170
ncerr = nf90_def_var(ncid, trim(fldName), NF90_DOUBLE, &
168-
(/im_dimid,jm_dimid,time_dimid/), varids(i)); NC_ERR_STOP(ncerr)
171+
(/im_dimid,jm_dimid,time_dimid/), varids(i)); NC_ERR_STOP(ncerr)
169172
else
170173
write(0,*)'Unsupported typekind ', typekind
171174
stop
172175
end if
173176
else if (fldlev(i) > 1) then
174177
if (typekind == ESMF_TYPEKIND_R4) then
175-
ncerr = nf90_def_var(ncid, trim(fldName), NF90_FLOAT, &
176-
(/im_dimid,jm_dimid,pfull_dimid,time_dimid/), varids(i)); NC_ERR_STOP(ncerr)
177178
if (ideflate > 0) then
178-
! shuffle filter off since lossy compression used
179-
ncerr = nf90_def_var_deflate(ncid, varids(i), 0, 1, ideflate)
180-
NC_ERR_STOP(ncerr)
179+
ncerr = nf90_def_var(ncid, trim(fldName), NF90_FLOAT, &
180+
(/im_dimid,jm_dimid,pfull_dimid,time_dimid/), varids(i), &
181+
shuffle=.false.,deflate_level=ideflate, &
182+
chunksizes=(/im,jm,1,1/)); NC_ERR_STOP(ncerr)
183+
else
184+
ncerr = nf90_def_var(ncid, trim(fldName), NF90_FLOAT, &
185+
(/im_dimid,jm_dimid,pfull_dimid,time_dimid/), varids(i)); NC_ERR_STOP(ncerr)
181186
endif
182187
else if (typekind == ESMF_TYPEKIND_R8) then
183188
ncerr = nf90_def_var(ncid, trim(fldName), NF90_DOUBLE, &
@@ -219,8 +224,8 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
219224
call ESMF_AttributeGet(fcstField(i), convention="NetCDF", purpose="FV3", &
220225
name=trim(attName), value=varr8val, &
221226
rc=rc); ESMF_ERR_RETURN(rc)
222-
if (trim(attName) /= '_FillValue' .or. ideflate == 0) then
223-
! FIXME: _FillValue must be cast to var type when using NF90_NETCDF4
227+
if (trim(attName) /= '_FillValue' ) then
228+
! FIXME: _FillValue must be cast to var type for recent versions of netcdf
224229
ncerr = nf90_put_att(ncid, varids(i), trim(attName), varr8val); NC_ERR_STOP(ncerr)
225230
endif
226231

@@ -236,6 +241,25 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
236241

237242
end do ! i=1,fieldCount
238243

244+
! write grid_xt, grid_yt attributes
245+
if (trim(output_grid) == 'gaussian_grid' .or. &
246+
trim(output_grid) == 'regional_latlon') then
247+
ncerr = nf90_put_att(ncid, im_varid, "long_name", "T-cell longitude"); NC_ERR_STOP(ncerr)
248+
ncerr = nf90_put_att(ncid, im_varid, "units", "degrees_E"); NC_ERR_STOP(ncerr)
249+
ncerr = nf90_put_att(ncid, jm_varid, "long_name", "T-cell latiitude"); NC_ERR_STOP(ncerr)
250+
ncerr = nf90_put_att(ncid, jm_varid, "units", "degrees_N"); NC_ERR_STOP(ncerr)
251+
else if (trim(output_grid) == 'rotated_latlon') then
252+
ncerr = nf90_put_att(ncid, im_varid, "long_name", "rotated T-cell longiitude"); NC_ERR_STOP(ncerr)
253+
ncerr = nf90_put_att(ncid, im_varid, "units", "degrees"); NC_ERR_STOP(ncerr)
254+
ncerr = nf90_put_att(ncid, jm_varid, "long_name", "rotated T-cell latiitude"); NC_ERR_STOP(ncerr)
255+
ncerr = nf90_put_att(ncid, jm_varid, "units", "degrees"); NC_ERR_STOP(ncerr)
256+
else if (trim(output_grid) == 'lambert_conformal') then
257+
ncerr = nf90_put_att(ncid, im_varid, "long_name", "x-coordinate of projection"); NC_ERR_STOP(ncerr)
258+
ncerr = nf90_put_att(ncid, im_varid, "units", "meters"); NC_ERR_STOP(ncerr)
259+
ncerr = nf90_put_att(ncid, jm_varid, "long_name", "y-coordinate of projection"); NC_ERR_STOP(ncerr)
260+
ncerr = nf90_put_att(ncid, jm_varid, "units", "meters"); NC_ERR_STOP(ncerr)
261+
endif
262+
239263
ncerr = nf90_enddef(ncid); NC_ERR_STOP(ncerr)
240264
end if
241265

@@ -247,63 +271,39 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
247271
if (mype==0) then
248272
if (trim(output_grid) == 'gaussian_grid' .or. &
249273
trim(output_grid) == 'regional_latlon') then
250-
ncerr = nf90_put_var(ncid, im_varid, values=rad2dg*arrayr8(:,1) ); NC_ERR_STOP(ncerr)
251-
ncerr = nf90_redef(ncid=ncid); NC_ERR_STOP(ncerr)
252-
ncerr = nf90_put_att(ncid, im_varid, "long_name", "T-cell longitude"); NC_ERR_STOP(ncerr)
253-
ncerr = nf90_put_att(ncid, im_varid, "units", "degrees_E"); NC_ERR_STOP(ncerr)
254-
ncerr = nf90_enddef(ncid=ncid); NC_ERR_STOP(ncerr)
274+
ncerr = nf90_put_var(ncid, im_varid, values=arrayr8(:,1) ); NC_ERR_STOP(ncerr)
255275
else if (trim(output_grid) == 'rotated_latlon') then
256276
do i=1,im
257277
x(i) = lon1 + (lon2-lon1)/(im-1) * (i-1)
258278
enddo
259279
ncerr = nf90_put_var(ncid, im_varid, values=x ); NC_ERR_STOP(ncerr)
260-
ncerr = nf90_redef(ncid=ncid); NC_ERR_STOP(ncerr)
261-
ncerr = nf90_put_att(ncid, im_varid, "long_name", "rotated T-cell longiitude"); NC_ERR_STOP(ncerr)
262-
ncerr = nf90_put_att(ncid, im_varid, "units", "degrees"); NC_ERR_STOP(ncerr)
263-
ncerr = nf90_enddef(ncid=ncid); NC_ERR_STOP(ncerr)
264280
else if (trim(output_grid) == 'lambert_conformal') then
265281
do i=1,im
266282
x(i) = dx * (i-1)
267283
enddo
268284
ncerr = nf90_put_var(ncid, im_varid, values=x ); NC_ERR_STOP(ncerr)
269-
ncerr = nf90_redef(ncid=ncid); NC_ERR_STOP(ncerr)
270-
ncerr = nf90_put_att(ncid, im_varid, "long_name", "x-coordinate of projection"); NC_ERR_STOP(ncerr)
271-
ncerr = nf90_put_att(ncid, im_varid, "units", "meters"); NC_ERR_STOP(ncerr)
272-
ncerr = nf90_enddef(ncid=ncid); NC_ERR_STOP(ncerr)
273285
endif
274-
ncerr = nf90_put_var(ncid, lon_varid, values=rad2dg*arrayr8 ); NC_ERR_STOP(ncerr)
286+
ncerr = nf90_put_var(ncid, lon_varid, values=arrayr8 ); NC_ERR_STOP(ncerr)
275287
endif
276288

277289
call ESMF_GridGetCoord(wrtGrid, coordDim=2, array=array, rc=rc); ESMF_ERR_RETURN(rc)
278290
call ESMF_ArrayGather(array, arrayr8, rootPet=0, rc=rc); ESMF_ERR_RETURN(rc)
279291
if (mype==0) then
280292
if (trim(output_grid) == 'gaussian_grid' .or. &
281293
trim(output_grid) == 'regional_latlon') then
282-
ncerr = nf90_put_var(ncid, jm_varid, values=rad2dg*arrayr8(1,:) ); NC_ERR_STOP(ncerr)
283-
ncerr = nf90_redef(ncid=ncid); NC_ERR_STOP(ncerr)
284-
ncerr = nf90_put_att(ncid, jm_varid, "long_name", "T-cell latiitude"); NC_ERR_STOP(ncerr)
285-
ncerr = nf90_put_att(ncid, jm_varid, "units", "degrees_N"); NC_ERR_STOP(ncerr)
286-
ncerr = nf90_enddef(ncid=ncid); NC_ERR_STOP(ncerr)
294+
ncerr = nf90_put_var(ncid, jm_varid, values=arrayr8(1,:) ); NC_ERR_STOP(ncerr)
287295
else if (trim(output_grid) == 'rotated_latlon') then
288296
do j=1,jm
289297
y(j) = lat1 + (lat2-lat1)/(jm-1) * (j-1)
290298
enddo
291299
ncerr = nf90_put_var(ncid, jm_varid, values=y ); NC_ERR_STOP(ncerr)
292-
ncerr = nf90_redef(ncid=ncid); NC_ERR_STOP(ncerr)
293-
ncerr = nf90_put_att(ncid, jm_varid, "long_name", "rotated T-cell latiitude"); NC_ERR_STOP(ncerr)
294-
ncerr = nf90_put_att(ncid, jm_varid, "units", "degrees"); NC_ERR_STOP(ncerr)
295-
ncerr = nf90_enddef(ncid=ncid); NC_ERR_STOP(ncerr)
296300
else if (trim(output_grid) == 'lambert_conformal') then
297301
do j=1,jm
298302
y(j) = dy * (j-1)
299303
enddo
300304
ncerr = nf90_put_var(ncid, jm_varid, values=y ); NC_ERR_STOP(ncerr)
301-
ncerr = nf90_redef(ncid=ncid); NC_ERR_STOP(ncerr)
302-
ncerr = nf90_put_att(ncid, jm_varid, "long_name", "y-coordinate of projection"); NC_ERR_STOP(ncerr)
303-
ncerr = nf90_put_att(ncid, jm_varid, "units", "meters"); NC_ERR_STOP(ncerr)
304-
ncerr = nf90_enddef(ncid=ncid); NC_ERR_STOP(ncerr)
305305
endif
306-
ncerr = nf90_put_var(ncid, lat_varid, values=rad2dg*arrayr8 ); NC_ERR_STOP(ncerr)
306+
ncerr = nf90_put_var(ncid, lat_varid, values=arrayr8 ); NC_ERR_STOP(ncerr)
307307
endif
308308

309309
do i=1, fieldCount
@@ -344,11 +344,7 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
344344
arrayr4_3d = quantized(arrayr4_3d_save, nbits, dataMin, dataMax)
345345
! compute max abs compression error, save as a variable
346346
! attribute.
347-
compress_err = maxval(abs(arrayr4_3d_save-arrayr4_3d))
348-
ncerr = nf90_redef(ncid=ncid); NC_ERR_STOP(ncerr)
349-
ncerr = nf90_put_att(ncid, varids(i), 'max_abs_compression_error', compress_err); NC_ERR_STOP(ncerr)
350-
ncerr = nf90_put_att(ncid, varids(i), 'nbits', nbits); NC_ERR_STOP(ncerr)
351-
ncerr = nf90_enddef(ncid=ncid); NC_ERR_STOP(ncerr)
347+
compress_err(i) = maxval(abs(arrayr4_3d_save-arrayr4_3d))
352348
endif
353349
ncerr = nf90_put_var(ncid, varids(i), values=arrayr4_3d, start=(/1,1,1/),count=(/im,jm,lm,1/) ); NC_ERR_STOP(ncerr)
354350
end if
@@ -363,6 +359,17 @@ subroutine write_netcdf(fieldbundle, wrtfb, filename, mpi_comm, mype, im, jm, rc
363359

364360
end do
365361

362+
if (ideflate > 0 .and. nbits > 0 .and. mype == 0) then
363+
ncerr = nf90_redef(ncid=ncid); NC_ERR_STOP(ncerr)
364+
do i=1, fieldCount
365+
if (compress_err(i) > 0) then
366+
ncerr = nf90_put_att(ncid, varids(i), 'max_abs_compression_error', compress_err(i)); NC_ERR_STOP(ncerr)
367+
ncerr = nf90_put_att(ncid, varids(i), 'nbits', nbits); NC_ERR_STOP(ncerr)
368+
endif
369+
enddo
370+
ncerr = nf90_enddef(ncid=ncid); NC_ERR_STOP(ncerr)
371+
endif
372+
366373
deallocate(arrayr4)
367374
deallocate(arrayr8)
368375
deallocate(arrayr4_3d,arrayr4_3d_save)
@@ -484,9 +491,9 @@ subroutine get_grid_attr(grid, prefix, ncid, varid, rc)
484491
else if (typekind==ESMF_TYPEKIND_R8) then
485492
call ESMF_AttributeGet(grid, convention="NetCDF", purpose="FV3", &
486493
name=trim(attName), value=varr8val, rc=rc); ESMF_ERR_RETURN(rc)
487-
if (trim(attName) /= '_FillValue' .or. ideflate == 0) then
488-
! FIXME: _FillValue must be cast to var type when using
489-
! NF90_NETCDF4. Until this is fixed, using netCDF default _FillValue.
494+
if (trim(attName) /= '_FillValue') then
495+
! FIXME: _FillValue must be cast to var type for recent versions
496+
! of netcdf
490497
ncerr = nf90_put_att(ncid, varid, trim(attName(ind+1:len(attName))), varr8val); NC_ERR_STOP(ncerr)
491498
endif
492499

0 commit comments

Comments
 (0)