1 |
roquet |
1.1 |
%function: MITprof_addVar |
2 |
|
|
% |
3 |
|
|
%usage: []=MITprof_addVar(filenc,varname,xtype,dimlist,varvalue); |
4 |
|
|
% ---> add a variable in a MITprof netcdf file |
5 |
|
|
% |
6 |
|
|
%inputs: filenc MITprof netcdf file name |
7 |
|
|
% varname,xtype,dimlist,varvalue: variable information |
8 |
|
|
|
9 |
|
|
function []=MITprof_addVar(filenc,varname,xtype,dimlist,varvalue); |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
% check that file exists and add prefix and suffix if necessary |
13 |
|
|
[pathstr, name, ext] = fileparts(filenc); |
14 |
|
|
if isempty(pathstr) | strcmp(pathstr,'.'), pathstr=pwd; end |
15 |
|
|
if isempty(ext) | ~strcmp(ext,'.nc'), ext='.nc'; end |
16 |
|
|
filenc=[pathstr '/' name ext]; |
17 |
|
|
if ~exist(filenc,'file'), error([filenc ' : file not found']); end |
18 |
|
|
|
19 |
|
|
%open file: |
20 |
|
|
nc=ncopen(filenc,'write'); |
21 |
|
|
|
22 |
|
|
%add variable: |
23 |
|
|
vars=ncvars(nc); |
24 |
|
|
if isempty(find(ismember(vars,varname))) |
25 |
|
|
ncaddVar(nc,varname,xtype,dimlist); |
26 |
|
|
end |
27 |
|
|
|
28 |
|
|
%write data |
29 |
|
|
ncputvar(nc,varname,varvalue); |
30 |
|
|
|
31 |
|
|
%close file: |
32 |
|
|
ncclose(nc); |
33 |
|
|
|
34 |
|
|
|