Commit a0155ea2 authored by Pierre Cazenave's avatar Pierre Cazenave

Fix nasty bug wherein only the last field in the input struct would be written...

Fix nasty bug wherein only the last field in the input struct would be written to the output NetCDF file. This is because the loop to check the current input NetCDF file variable and the list of fields to use as replacements would run through all three variables and write the data if the test was true, resulting in the earlier replacements being overwritten by the existing data. The fix is to move the writing of the existing data outside the check loop and create a new variable which is set to one once the new data have been added to the NetCDF
parent 9f42d3d6
...@@ -31,7 +31,7 @@ function write_FVCOM_restart(fv_restart, out_restart, indata) ...@@ -31,7 +31,7 @@ function write_FVCOM_restart(fv_restart, out_restart, indata)
% %
%========================================================================== %==========================================================================
subname = 'write_FVCOM_tsobc'; subname = 'write_FVCOM_restart';
global ftbverbose global ftbverbose
if ftbverbose if ftbverbose
...@@ -142,15 +142,19 @@ for ii = 1:numvars ...@@ -142,15 +142,19 @@ for ii = 1:numvars
% Iterate through the field names to see if we're on one of the % Iterate through the field names to see if we're on one of the
% variables to be replaced. % variables to be replaced.
% Set variable so we know if we've already written this variable to the
% output file.
writtenAlready = 0;
for vv = 1:nf for vv = 1:nf
if strcmp(varname, fnames{vv}) if strcmp(varname, fnames{vv}) && writtenAlready == 0
if ftbverbose if ftbverbose
fprintf('new data... ') fprintf('new data... ')
end end
% To make the scaling go from the initial value to the POLCOMS % To make the scaling go from the initial value to the POLCOMS
% value, we need to take the scale the difference between the end % value, we need to take the scale the difference between the
% members by the scaling factor at each time and add to the current % end members by the scaling factor at each time and add to the
% time's value. % current time's value.
sfvdata = nan(nd, ns, nt); sfvdata = nan(nd, ns, nt);
ss = 0:1 / (nt - 1):1; % scale from 0 to 1. ss = 0:1 / (nt - 1):1; % scale from 0 to 1.
startdata = squeeze(data(:, :, 1)); % use the first modelled time step startdata = squeeze(data(:, :, 1)); % use the first modelled time step
...@@ -162,8 +166,15 @@ for ii = 1:numvars ...@@ -162,8 +166,15 @@ for ii = 1:numvars
sfvdata(:, :, tt) = startdata + (ss(tt) .* td); sfvdata(:, :, tt) = startdata + (ss(tt) .* td);
end end
end end
% Replace the values with the scaled interpolated values. % Replace the values with the scaled interpolated values,
netcdf.putVar(ncout, varid, sfvdata) % checking for unlimited dimensions as we go.
if wasUnlimited < 0
netcdf.putVar(ncout, varid, sfvdata)
else
netcdf.putVar(ncout, varid, zeros(length(currDimsLengths), 1), currDimsLengths, sfvdata)
end
writtenAlready = 1;
% % We might also want to replace the time. If so, uncomment these % % We might also want to replace the time. If so, uncomment these
% % lines to replace with an arbitrary time period. We also need an % % lines to replace with an arbitrary time period. We also need an
...@@ -182,21 +193,27 @@ for ii = 1:numvars ...@@ -182,21 +193,27 @@ for ii = 1:numvars
% tmp_start_time = greg2mjulian(start_date(1), start_date(2), start_date(3) - 7, start_date(4), start_date(5), start_date(6)); % tmp_start_time = greg2mjulian(start_date(1), start_date(2), start_date(3) - 7, start_date(4), start_date(5), start_date(6));
% tmp_time = tmp_start_time:(tmp_start_time + nt - 1); % tmp_time = tmp_start_time:(tmp_start_time + nt - 1);
% netcdf.putVar(ncout, varid, floor(tmp_time)) % netcdf.putVar(ncout, varid, floor(tmp_time))
else end
end
% We need to check if the dimension is unlimited, and use a start % If writtenAlready is zero, we haven't had one of the variables we're
% and end with netcdf.putVar if it is. This is largely because % replacing, so just dump the existing data.
% MATLAB can't handle unlimited dimensions in the same way as it if writtenAlready == 0
% does finite dimensions. if ftbverbose
if wasUnlimited < 0 fprintf('existing data... ')
% We can just dump the entire data without specifying over what end
% indices. % We need to check if the dimension is unlimited, and use a
netcdf.putVar(ncout, varid, data); % start and end with netcdf.putVar if it is. This is largely
else % because MATLAB can't handle unlimited dimensions in the same
% Use the dimension length we extracted above to output the % way as it does finite dimensions.
% data with the valid unlimited dimension format. if wasUnlimited < 0
netcdf.putVar(ncout, varid, zeros(length(currDimsLengths), 1), currDimsLengths, data); % We can just dump the entire data without specifying over
end % what indices.
netcdf.putVar(ncout, varid, data);
else
% Use the dimension length we extracted above to output the
% data with the valid unlimited dimension format.
netcdf.putVar(ncout, varid, zeros(length(currDimsLengths), 1), currDimsLengths, data);
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment