1 |
roquet |
1.6 |
function []=MITprof_write(fileOut,MITprof,varargin); |
2 |
gforget |
1.1 |
%function: MITprof_write |
3 |
roquet |
1.6 |
%object: write data in a MITprof netcdf file |
4 |
gforget |
1.1 |
%author: Gael Forget (gforget@mit.edu) |
5 |
gforget |
1.3 |
%date: Nov 5th, 2010 |
6 |
gforget |
1.1 |
% |
7 |
roquet |
1.6 |
%usage: []=MITprof_write(fileOut,MITprof, [list_vars] ); |
8 |
|
|
% |
9 |
|
|
%inputs: fileOut data file name (absolute/relative path) |
10 |
|
|
% .nc suffix is added if not already present |
11 |
|
|
% MITprof struct containing profile data |
12 |
|
|
% list_vars variable list (optional) |
13 |
|
|
% |
14 |
|
|
% Format description of MITprof struct input variable: |
15 |
|
|
% list of fields always available: |
16 |
|
|
% prof_depth [nDepth x 1] |
17 |
|
|
% prof_descr {nProf x 1} --> will be converted into a char array |
18 |
|
|
% prof_date [nProf x 1] |
19 |
|
|
% prof_YYYYMMDD [nProf x 1] |
20 |
|
|
% prof_HHMMSS [nProf x 1] |
21 |
|
|
% prof_lon [nProf x 1] |
22 |
|
|
% prof_lat [nProf x 1] |
23 |
|
|
% prof_basin [nProf x 1] |
24 |
|
|
% prof_point [nProf x 1] |
25 |
|
|
% |
26 |
|
|
% other fields generally available of size [nProf x nLev] : |
27 |
|
|
% prof_T, prof_Tflag, prof_Terr, prof_Tweight, prof_Testim |
28 |
|
|
% prof_S, prof_Sflag, prof_Serr, prof_Sweight, prof_Sestim |
29 |
|
|
% |
30 |
|
|
% if list_vars is not specified, every variables with a name starting |
31 |
|
|
% with 'prof_', except prof_depth, will be saved in the MITprof |
32 |
|
|
% netcdf file. An error will occur if the program try to save a |
33 |
|
|
% variable that is not initialized in the MITprof netcdf file. |
34 |
|
|
% |
35 |
|
|
% if list_vars is specified, only variables present in the list will be |
36 |
|
|
% saved. |
37 |
|
|
% |
38 |
|
|
% NaN are auomatically replaced by missing_value attribute (or _FillValue). |
39 |
|
|
% the file fileOut will be created using MITprof_create if needed. |
40 |
gforget |
1.1 |
% |
41 |
|
|
|
42 |
gforget |
1.2 |
|
43 |
gforget |
1.3 |
|
44 |
roquet |
1.6 |
% check that file exists and add prefix and suffix if necessary |
45 |
roquet |
1.5 |
[pathstr, name, ext] = fileparts(fileOut); |
46 |
|
|
if isempty(pathstr) | strcmp(pathstr,'.'), pathstr=pwd; end |
47 |
|
|
if isempty(ext) | ~strcmp(ext,'.nc'), ext='.nc'; end |
48 |
|
|
fileOut=[pathstr '/' name ext]; |
49 |
gforget |
1.3 |
|
50 |
roquet |
1.6 |
% build the list of variables that must be loaded |
51 |
|
|
if nargin>2, |
52 |
|
|
list_vars=varargin{1}; |
53 |
|
|
else |
54 |
|
|
list1=fieldnames(MITprof); list_vars={}; |
55 |
|
|
I=strfind(list1,'prof_'); |
56 |
|
|
for kk=1:length(I), |
57 |
|
|
if I{kk}==1 & ~strcmp(list1{kk},'prof_depth'), |
58 |
|
|
list_vars{end+1}=list1{kk}; |
59 |
|
|
end |
60 |
|
|
end |
61 |
|
|
end |
62 |
|
|
|
63 |
roquet |
1.7 |
% stop if there are no profile in MITprof |
64 |
|
|
nProf=length(MITprof.prof_lon); |
65 |
|
|
if nProf==0, return, end |
66 |
|
|
|
67 |
roquet |
1.6 |
% create the file if needed |
68 |
|
|
if ~exist(fileOut,'file'), |
69 |
|
|
prof_depth=MITprof.prof_depth; |
70 |
|
|
MITprof_create(fileOut,nProf,prof_depth,list_vars); |
71 |
|
|
end |
72 |
|
|
|
73 |
|
|
% prof_descr: convert back from cell 2 array format |
74 |
|
|
if isfield(MITprof,'prof_descr') |
75 |
|
|
ncload(fileOut,'prof_descr'); nb_char=size(prof_descr,2); |
76 |
|
|
prof_descr=char(MITprof.prof_descr); |
77 |
|
|
if size(prof_descr,2)<nb_char, |
78 |
|
|
prof_descr(:,size(prof_descr,2)+1:nb_char)=' '; |
79 |
|
|
elseif size(prof_descr,2)>nb_char |
80 |
|
|
disp('some profile descriptive strings have been shortened'); |
81 |
|
|
prof_descr=prof_descr(:,1:nb_char); |
82 |
|
|
end |
83 |
|
|
MITprof.prof_descr=prof_descr; |
84 |
|
|
end |
85 |
|
|
|
86 |
|
|
% write to file: |
87 |
roquet |
1.4 |
nc=ncopen(fileOut,'write'); |
88 |
gforget |
1.1 |
for ii=1:length(list_vars); |
89 |
roquet |
1.6 |
varname=list_vars{ii}; |
90 |
|
|
data=getfield(MITprof,varname); |
91 |
|
|
spval=ncgetFillVal(nc,varname); |
92 |
|
|
if isnumeric(data) |
93 |
|
|
data(isnan(data))=spval; |
94 |
|
|
MITprof=setfield(MITprof,varname,data); |
95 |
|
|
end; |
96 |
|
|
ncputvar(nc,varname,data); |
97 |
gforget |
1.1 |
end; |
98 |
roquet |
1.4 |
ncclose(nc); |
99 |
roquet |
1.6 |
|
100 |
gforget |
1.1 |
|