1 |
function []=profiles_prep_write_nc(InterOrFinal); |
2 |
|
3 |
%InterOrFinal 1 -> intermediate stage, store in mat file |
4 |
% 2 -> final stage, write nc |
5 |
|
6 |
global rep_out filename_out; |
7 |
global t_std s_std t_test s_test t_w s_w t_equi s_equi; |
8 |
global ymd hms pnum pnum_txt lon lat direc dmod ilon ilat imonth; |
9 |
global fill_value_output initChkFile; |
10 |
|
11 |
%global vaiables used here only -> keep in memory |
12 |
global num_cur_tmpfile data_value2D data_weights2D data_equi2D data_i data_j data_t data_t2 data_test2D data_pnum_txt data_dir; |
13 |
|
14 |
buffer_size=1000; |
15 |
nb_lev=length(dmod); |
16 |
nb_char=30; |
17 |
|
18 |
if fill_value_output~=-9999; |
19 |
fprint('MITgcm assumes fill_value_output=-9999 \n'); |
20 |
return; |
21 |
end |
22 |
|
23 |
if InterOrFinal==1 |
24 |
|
25 |
%initialize arrays : |
26 |
if isempty(data_pnum_txt) |
27 |
num_cur_tmpfile=0; |
28 |
data_value2D=NaN*zeros(2,buffer_size,length(dmod)); |
29 |
data_test2D=data_value2D; data_weights2D=data_value2D; data_equi2D=data_value2D; |
30 |
data_i=NaN*zeros(buffer_size,1); |
31 |
data_j=data_i;data_t=data_i; data_t2=data_i; data_dir=data_i; |
32 |
data_pnum_txt=char(double(zeros(buffer_size,nb_char))); |
33 |
end |
34 |
|
35 |
%restriction of dates and positions : |
36 |
|
37 |
%store information : |
38 |
tmp_indice=min(find(isnan(data_i))); |
39 |
|
40 |
data_value2D(1,tmp_indice,:)=t_std; |
41 |
data_weights2D(1,tmp_indice,:)=t_w; |
42 |
data_equi2D(1,tmp_indice,:)=t_equi; |
43 |
data_test2D(1,tmp_indice,:)=t_test; |
44 |
|
45 |
data_value2D(2,tmp_indice,:)=s_std; |
46 |
data_weights2D(2,tmp_indice,:)=s_w; |
47 |
data_equi2D(2,tmp_indice,:)=s_equi; |
48 |
data_test2D(2,tmp_indice,:)=s_test; |
49 |
|
50 |
data_t(tmp_indice)=ymd; |
51 |
data_t2(tmp_indice)=hms; |
52 |
data_pnum_txt(tmp_indice,1:length(pnum_txt))=pnum_txt; |
53 |
data_i(tmp_indice)=lon; |
54 |
data_j(tmp_indice)=lat; |
55 |
data_dir(tmp_indice)=direc; |
56 |
|
57 |
if tmp_indice>0.9*buffer_size; |
58 |
num_cur_tmpfile=num_cur_tmpfile+1; |
59 |
eval(['save ' rep_out '/' filename_out 'tmp' num2str(num_cur_tmpfile) '.mat data_*;' ]); |
60 |
data_value2D=NaN*zeros(2,buffer_size,length(dmod)); |
61 |
data_test2D=data_value2D; data_weights2D=data_value2D; data_equi2D=data_value2D; |
62 |
data_i=NaN*zeros(buffer_size,1); |
63 |
data_j=data_i;data_t=data_i; data_t2=data_i; data_dir=data_i; |
64 |
data_pnum_txt=char(double(zeros(buffer_size,nb_char))); |
65 |
end; |
66 |
|
67 |
|
68 |
elseif InterOrFinal==2 |
69 |
|
70 |
num_cur_tmpfile=num_cur_tmpfile+1; |
71 |
eval(['save ' rep_out '/' filename_out 'tmp' num2str(num_cur_tmpfile) '.mat data_*;' ]); |
72 |
|
73 |
%remark : moving to the rep_out directory seems needed because |
74 |
% the netcdf file creation can fail otherwise |
75 |
eval('rep_bak=pwd;'); eval(['cd ' rep_out]); |
76 |
|
77 |
|
78 |
for file_content=1:2 |
79 |
|
80 |
%open the netcdf file : |
81 |
if file_content==1%the main file |
82 |
fcdf=netcdf([filename_out '.nc'], 'clobber'); |
83 |
else |
84 |
fcdf=netcdf([filename_out 'CHECK.nc'], 'clobber'); |
85 |
end |
86 |
|
87 |
%nb of profiles to be assimilated : |
88 |
nb_prof=0; |
89 |
for cur_tmpfile=1:num_cur_tmpfile |
90 |
eval(['load ' rep_out '/' filename_out 'tmp' num2str(cur_tmpfile) '.mat;' ]); |
91 |
tmp_indice=min(find(isnan(data_i))); |
92 |
if isempty(tmp_indice); tmp_indice=length(data_i)+1; end; |
93 |
nb_prof=nb_prof+tmp_indice-1; |
94 |
end |
95 |
|
96 |
|
97 |
%define netcdf dimensions : |
98 |
fcdf('iPROF') = nb_prof; |
99 |
fcdf('iDEPTH') = nb_lev; |
100 |
fcdf('lTXT') = nb_char; |
101 |
|
102 |
%define netcdf variables : |
103 |
fcdf{'depth'} = ncdouble('iDEPTH'); |
104 |
fcdf{'depth'}.units='meters'; |
105 |
fcdf{'prof_YYYYMMDD'} = ncdouble('iPROF'); |
106 |
fcdf{'prof_YYYYMMDD'}.missing_value=fill_value_output; |
107 |
fcdf{'prof_YYYYMMDD'}.long_name='year (4 digits), month (2 digits), day (2 digits)'; |
108 |
fcdf{'prof_HHMMSS'} = ncdouble('iPROF'); |
109 |
fcdf{'prof_HHMMSS'}.missing_value=fill_value_output; |
110 |
fcdf{'prof_HHMMSS'}.long_name='hour (2 digits), minute (2 digits), seconde (2 digits)'; |
111 |
fcdf{'prof_dir'} = ncdouble('iPROF'); |
112 |
fcdf{'prof_dir'}.missing_value=0; |
113 |
fcdf{'prof_dir'}.long_name='profile direction 1=asc 2=desc'; |
114 |
fcdf{'prof_lon'} = ncdouble('iPROF'); |
115 |
fcdf{'prof_lon'}.units='(degree E)'; |
116 |
fcdf{'prof_lon'}.missing_value=fill_value_output; |
117 |
fcdf{'prof_lat'} = ncdouble('iPROF'); |
118 |
fcdf{'prof_lat'}.units='(degree N)'; |
119 |
fcdf{'prof_lat'}.missing_value=fill_value_output; |
120 |
fcdf{'prof_T'} = ncdouble('iPROF','iDEPTH'); |
121 |
fcdf{'prof_T'}.long_name='potential temperature'; |
122 |
fcdf{'prof_T'}.units='degree Celsius'; |
123 |
fcdf{'prof_T'}.missing_value=fill_value_output; |
124 |
fcdf{'prof_Tweight'} = ncdouble('iPROF','iDEPTH'); |
125 |
fcdf{'prof_Tweight'}.long_name='weights'; |
126 |
fcdf{'prof_Tweight'}.units='(degree Celsius)^-2'; |
127 |
fcdf{'prof_Tweight'}.missing_value=0; |
128 |
fcdf{'prof_Ttest'} = ncdouble('iPROF','iDEPTH'); |
129 |
fcdf{'prof_Ttest'}.long_name='temperature flag'; |
130 |
fcdf{'prof_Ttest'}.units='(no units)'; |
131 |
fcdf{'prof_Ttest'}.missing_value=0; |
132 |
fcdf{'prof_S'} = ncdouble('iPROF','iDEPTH'); |
133 |
fcdf{'prof_S'}.long_name='salinity'; |
134 |
fcdf{'prof_S'}.units='(no units)'; |
135 |
fcdf{'prof_S'}.missing_value=fill_value_output; |
136 |
fcdf{'prof_Sweight'} = ncdouble('iPROF','iDEPTH'); |
137 |
fcdf{'prof_Sweight'}.long_name='weights'; |
138 |
fcdf{'prof_Sweight'}.units='(no units)^-2'; |
139 |
fcdf{'prof_Sweight'}.missing_value=0; |
140 |
fcdf{'prof_Stest'} = ncdouble('iPROF','iDEPTH'); |
141 |
fcdf{'prof_Stest'}.long_name='salinity flag'; |
142 |
fcdf{'prof_Stest'}.units='(no units)'; |
143 |
fcdf{'prof_Stest'}.missing_value=0; |
144 |
fcdf{'prof_descr'} = ncchar('iPROF','lTXT'); |
145 |
fcdf{'prof_descr'}.long_name='profile description'; |
146 |
|
147 |
|
148 |
%fill the vector of the standard depths : |
149 |
fcdf{'depth'}(1:nb_lev)=dmod; |
150 |
|
151 |
%fill the profiles arrays: |
152 |
cur_prof_interv=[0 0]; |
153 |
for cur_tmpfile=1:num_cur_tmpfile |
154 |
eval(['load ' rep_out '/' filename_out 'tmp' num2str(cur_tmpfile) '.mat;' ]); |
155 |
tmp_indice=min(find(isnan(data_i)))-1; |
156 |
if tmp_indice>0 |
157 |
cur_prof_interv=[cur_prof_interv(2)+1 cur_prof_interv(2)+tmp_indice]; |
158 |
|
159 |
%pre-treatment of the data : |
160 |
if file_content==1%the main file |
161 |
prof_T=squeeze(data_value2D(1,1:tmp_indice,:)); |
162 |
else |
163 |
prof_T=squeeze(data_equi2D(1,1:tmp_indice,:)); |
164 |
end |
165 |
prof_Tweight=squeeze(data_weights2D(1,1:tmp_indice,:)); |
166 |
prof_Ttest=squeeze(data_test2D(1,1:tmp_indice,:)); |
167 |
if file_content==1%the main file |
168 |
prof_Tweight(prof_T==fill_value_output|prof_Ttest>0)=0; |
169 |
else |
170 |
prof_T(isnan(prof_T))=fill_value_output; |
171 |
end |
172 |
|
173 |
|
174 |
if file_content==1%the main file |
175 |
prof_S=squeeze(data_value2D(2,1:tmp_indice,:)); |
176 |
else |
177 |
prof_S=squeeze(data_equi2D(2,1:tmp_indice,:)); |
178 |
end |
179 |
prof_Sweight=squeeze(data_weights2D(2,1:tmp_indice,:)); |
180 |
prof_Stest=squeeze(data_test2D(2,1:tmp_indice,:)); |
181 |
if file_content==1%the main file |
182 |
prof_Sweight(prof_S==fill_value_output|prof_Stest>0)=0; |
183 |
else |
184 |
prof_S(isnan(prof_S))=fill_value_output; |
185 |
end |
186 |
|
187 |
|
188 |
|
189 |
tmp3=[1:tmp_indice]; |
190 |
|
191 |
%fill the date and position vectors : |
192 |
fcdf{'prof_YYYYMMDD'}(cur_prof_interv(1):cur_prof_interv(2))=data_t(tmp3); |
193 |
fcdf{'prof_HHMMSS'}(cur_prof_interv(1):cur_prof_interv(2))=data_t2(tmp3); |
194 |
fcdf{'prof_dir'}(cur_prof_interv(1):cur_prof_interv(2))=data_dir(tmp3); |
195 |
fcdf{'prof_lon'}(cur_prof_interv(1):cur_prof_interv(2))=data_i(tmp3); |
196 |
fcdf{'prof_lat'}(cur_prof_interv(1):cur_prof_interv(2))=data_j(tmp3); |
197 |
|
198 |
%fill the data matrices : |
199 |
fcdf{'prof_T'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_T(tmp3,:); |
200 |
fcdf{'prof_Tweight'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_Tweight(tmp3,:); |
201 |
fcdf{'prof_Ttest'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_Ttest(tmp3,:); |
202 |
fcdf{'prof_S'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_S(tmp3,:); |
203 |
fcdf{'prof_Sweight'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_Sweight(tmp3,:); |
204 |
fcdf{'prof_Stest'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_Stest(tmp3,:); |
205 |
%fill the field describing the instruments : |
206 |
fcdf{'prof_descr'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_char)=data_pnum_txt(tmp3,:); |
207 |
end; end; |
208 |
|
209 |
%close the file : |
210 |
fcdf=close(fcdf); |
211 |
|
212 |
end %for file_content=1:2 |
213 |
|
214 |
eval(['cd ' rep_bak]); |
215 |
|
216 |
for cur_tmpfile=1:num_cur_tmpfile |
217 |
eval(['!rm -f ' rep_out '/' filename_out 'tmp' num2str(cur_tmpfile) '.mat;' ]); |
218 |
end |
219 |
|
220 |
%needed to reinitialize arrays in the next case : |
221 |
data_pnum_txt=[]; |
222 |
|
223 |
end |