-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFUN_nc_easywrite_write_var.m
More file actions
59 lines (43 loc) · 1.53 KB
/
FUN_nc_easywrite_write_var.m
File metadata and controls
59 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function FUN_nc_easywrite_write_var( filename, var_name, data, varargin )
% FUN_nc_easywrite_write_var( filename, var_name, data )
% FUN_nc_easywrite_write_var( filename, var_name, data, start, count, stride )
% Write data into an existing variable
% Known problem:
% **This does not consider off_set and scale_factor**
%
% V1.03 by L. Chi: add support for auto cleanup of netcdf file handles
% V1.02 by L. Chi: clean useless codes
% V1.01 by L. Chi: fix a bug: an error occurred when varargin is not empty.
% V1.00 by L. Chi
%% ## 0. check ============================================================
% ####
if exist(filename,'file')
% ok
else
error('The file does not exist!');
end
% ####
is_var_exist = FUN_nc_is_exist_variable( filename, var_name );
if is_var_exist
%Pass
else
error(['Variable ' var_name ' does not exist! Please define it before writing data!']);
end
%% ## 1. open the netcdf ==================================================
% open file
ncid = netcdf.open(filename,'NC_WRITE');
cleanup_ncid = onCleanup(@() netcdf.close(ncid) ); % make sure the file will be closed
% get variable ID
varid = netcdf.inqVarID( ncid, var_name );
%% ## 2. write into netcdf ================================================
if nargin == 0
% write the entire variable
netcdf.putVar( ncid, varid, data );
else
% write a subset of the variable
netcdf.putVar( ncid, varid, varargin{:}, data );
end
%% ## 3. close the file ====================================================
%netcdf.close(ncid);
clear cleanup_ncid
return