1 |
roquet |
1.1 |
function MITprof_nc2odv(fileIn,varargin) |
2 |
|
|
% function MITprof_nc2odv(fileIn,[fileOut]) |
3 |
|
|
% |
4 |
|
|
% convert a file in MITprof netcdf format into an ODV spreadsheet. |
5 |
|
|
% fileIn: path (absolute or relative) to MITprof netcdf file |
6 |
|
|
% fileOut: path (absolute or relative) to ODV spreadsheet. |
7 |
|
|
% use a .txt prefix (either added or replaced in fileOut name) |
8 |
|
|
% by default, fileOut is the same than fileIn but with a .txt suffix |
9 |
|
|
% if the file already exist, it will be replaced. |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
fileOut=fileIn; |
13 |
|
|
if nargin>1, fileOut=varargin{1}; end |
14 |
|
|
|
15 |
|
|
[pathstr, name, ext] = fileparts(fileIn); |
16 |
|
|
if isempty(pathstr) | strcmp(pathstr,'.'), pathstr=pwd; end |
17 |
|
|
fileIn=[pathstr '/' name ext]; |
18 |
|
|
|
19 |
|
|
[pathstr, name, ext] = fileparts(fileOut); |
20 |
|
|
if isempty(pathstr) | strcmp(pathstr,'.'), pathstr=pwd; end |
21 |
|
|
if isempty(ext) | ~strcmp(ext,'.txt'), ext='.txt'; end |
22 |
|
|
fileOut=[pathstr '/' name ext]; |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
dat=datestr(now,'yyyy-mm-ddTHH:MM:SS'); |
28 |
|
|
|
29 |
|
|
str={'//<Version>ODV Spreadsheet V4.0</Version>' |
30 |
|
|
['//<CreateTime>' dat '</CreateTime>'] |
31 |
|
|
['//<Software>MITprof on Matlab ' version '</Software>'] |
32 |
|
|
['//<Source>' fileIn '</Source>'] |
33 |
|
|
'//<DataField>Ocean</DataField>' |
34 |
|
|
'//<DataType>Profiles</DataType>' |
35 |
|
|
'//' |
36 |
|
|
}; |
37 |
|
|
|
38 |
|
|
% open fileOut |
39 |
|
|
fid=fopen(fileOut,'w'); |
40 |
|
|
|
41 |
|
|
% write headerlines |
42 |
|
|
for kk=1:length(str) |
43 |
|
|
fprintf(fid,'%s\n',str{kk}); |
44 |
|
|
end |
45 |
|
|
|
46 |
|
|
% load fileIn |
47 |
|
|
M=MITprof_load(fileIn); |
48 |
|
|
|
49 |
|
|
str=['Cruise Station Type yyyy-mm-ddThh:mm:ss.sss ' ... |
50 |
|
|
'Longitude [degrees_east] Latitude [degrees_north] ' ... |
51 |
|
|
'Depth [m] QV:WOD Temperature [¬?C] QV:WOD Salinity [psu] QV:WOD QV:ODV:SAMPLE']; |
52 |
|
|
fprintf(fid,'%s\n',str); |
53 |
|
|
format_head='%s\t%s\t%s\t%s\t%6.4f\t%6.4f\t'; |
54 |
|
|
format_nohead='\t\t\t\t\t\t'; |
55 |
|
|
format_TS='%3.1f\t0\t%6.4f\t%d\t%6.4f\t%d\t1\n'; |
56 |
|
|
format_Tonly='%3.1f\t0\t%6.4f\t%d\t\t\t1\n'; |
57 |
|
|
format_Sonly='%3.1f\t0\t\t\t%6.4f\t%d\t1\n'; |
58 |
|
|
for ii=1:length(M.prof_lon), |
59 |
|
|
prof_descr=M.prof_descr{ii}; |
60 |
|
|
I=strfind(prof_descr,'//'); |
61 |
|
|
cruise=prof_descr(1:I-1); |
62 |
|
|
station=prof_descr(I+2:end); |
63 |
|
|
type='B'; |
64 |
|
|
dat=datestr(M.prof_date(ii),'yyyy-mm-ddTHH:MM:SS'); |
65 |
|
|
lon=M.prof_lon(ii); |
66 |
|
|
lat=M.prof_lat(ii); |
67 |
|
|
D=M.prof_depth; |
68 |
|
|
T=M.prof_T(ii,:); |
69 |
|
|
Tflag=M.prof_Tflag(ii,:); |
70 |
|
|
S=M.prof_S(ii,:); |
71 |
|
|
Sflag=M.prof_Sflag(ii,:); |
72 |
|
|
iFormat=(1:length(D))*0; |
73 |
|
|
Its=find(~isnan(T)&~isnan(S));iFormat(Its)=1; |
74 |
|
|
It=find(~isnan(T)&isnan(S));iFormat(It)=2; |
75 |
|
|
Is=find(isnan(T)&~isnan(S));iFormat(Is)=3; |
76 |
|
|
I=find(iFormat~=0); |
77 |
|
|
if length(I)==0, continue, end |
78 |
|
|
pp=I(1); |
79 |
|
|
switch iFormat(pp) |
80 |
|
|
case 1 |
81 |
|
|
fprintf(fid,[format_head format_TS], ... |
82 |
|
|
cruise,station,type,dat,lon,lat,D(pp),T(pp),Tflag(pp),S(pp),Sflag(pp)); |
83 |
|
|
case 2 |
84 |
|
|
fprintf(fid,[format_head format_Tonly], ... |
85 |
|
|
cruise,station,type,dat,lon,lat,D(pp),T(pp),Tflag(pp)); |
86 |
|
|
case 3 |
87 |
|
|
fprintf(fid,[format_head format_Sonly], ... |
88 |
|
|
cruise,station,type,dat,lon,lat,D(pp),S(pp),Sflag(pp)); |
89 |
|
|
end |
90 |
|
|
for ii=2:length(I), |
91 |
|
|
pp=I(ii); |
92 |
|
|
switch iFormat(pp) |
93 |
|
|
case 1 |
94 |
|
|
fprintf(fid,[format_nohead format_TS], ... |
95 |
|
|
D(pp),T(pp),Tflag(pp),S(pp),Sflag(pp)); |
96 |
|
|
case 2 |
97 |
|
|
fprintf(fid,[format_nohead format_Tonly], ... |
98 |
|
|
D(pp),T(pp),Tflag(pp)); |
99 |
|
|
case 3 |
100 |
|
|
fprintf(fid,[format_nohead format_Sonly], ... |
101 |
|
|
D(pp),S(pp),Sflag(pp)); |
102 |
|
|
end |
103 |
|
|
end |
104 |
|
|
end |
105 |
|
|
|
106 |
|
|
fclose(fid); |
107 |
|
|
|