function []=profiles_prep_write_nc(InterOrFinal); %InterOrFinal 1 -> intermediate stage, store in mat file % 2 -> final stage, write nc global rep_out filename_out; global t_std s_std t_test s_test t_w s_w t_equi s_equi; global ymd hms pnum pnum_txt lon lat direc dmod ilon ilat imonth; global fill_value_output initChkFile; %global vaiables used here only -> keep in memory 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; buffer_size=1000; nb_lev=length(dmod); nb_char=30; if fill_value_output~=-9999; fprint('MITgcm assumes fill_value_output=-9999 \n'); return; end if InterOrFinal==1 %initialize arrays : if isempty(data_pnum_txt) num_cur_tmpfile=0; data_value2D=NaN*zeros(2,buffer_size,length(dmod)); data_test2D=data_value2D; data_weights2D=data_value2D; data_equi2D=data_value2D; data_i=NaN*zeros(buffer_size,1); data_j=data_i;data_t=data_i; data_t2=data_i; data_dir=data_i; data_pnum_txt=char(double(zeros(buffer_size,nb_char))); end %restriction of dates and positions : %store information : tmp_indice=min(find(isnan(data_i))); data_value2D(1,tmp_indice,:)=t_std; data_weights2D(1,tmp_indice,:)=t_w; data_equi2D(1,tmp_indice,:)=t_equi; data_test2D(1,tmp_indice,:)=t_test; data_value2D(2,tmp_indice,:)=s_std; data_weights2D(2,tmp_indice,:)=s_w; data_equi2D(2,tmp_indice,:)=s_equi; data_test2D(2,tmp_indice,:)=s_test; data_t(tmp_indice)=ymd; data_t2(tmp_indice)=hms; data_pnum_txt(tmp_indice,1:length(pnum_txt))=pnum_txt; data_i(tmp_indice)=lon; data_j(tmp_indice)=lat; data_dir(tmp_indice)=direc; if tmp_indice>0.9*buffer_size; num_cur_tmpfile=num_cur_tmpfile+1; eval(['save ' rep_out '/' filename_out 'tmp' num2str(num_cur_tmpfile) '.mat data_*;' ]); data_value2D=NaN*zeros(2,buffer_size,length(dmod)); data_test2D=data_value2D; data_weights2D=data_value2D; data_equi2D=data_value2D; data_i=NaN*zeros(buffer_size,1); data_j=data_i;data_t=data_i; data_t2=data_i; data_dir=data_i; data_pnum_txt=char(double(zeros(buffer_size,nb_char))); end; elseif InterOrFinal==2 num_cur_tmpfile=num_cur_tmpfile+1; eval(['save ' rep_out '/' filename_out 'tmp' num2str(num_cur_tmpfile) '.mat data_*;' ]); %remark : moving to the rep_out directory seems needed because % the netcdf file creation can fail otherwise eval('rep_bak=pwd;'); eval(['cd ' rep_out]); for file_content=1:2 %open the netcdf file : if file_content==1%the main file fcdf=netcdf([filename_out '.nc'], 'clobber'); else fcdf=netcdf([filename_out 'CHECK.nc'], 'clobber'); end %nb of profiles to be assimilated : nb_prof=0; for cur_tmpfile=1:num_cur_tmpfile eval(['load ' rep_out '/' filename_out 'tmp' num2str(cur_tmpfile) '.mat;' ]); tmp_indice=min(find(isnan(data_i))); if isempty(tmp_indice); tmp_indice=length(data_i)+1; end; nb_prof=nb_prof+tmp_indice-1; end %define netcdf dimensions : fcdf('iPROF') = nb_prof; fcdf('iDEPTH') = nb_lev; fcdf('lTXT') = nb_char; %define netcdf variables : fcdf{'depth'} = ncdouble('iDEPTH'); fcdf{'depth'}.units='meters'; fcdf{'prof_YYYYMMDD'} = ncdouble('iPROF'); fcdf{'prof_YYYYMMDD'}.missing_value=fill_value_output; fcdf{'prof_YYYYMMDD'}.long_name='year (4 digits), month (2 digits), day (2 digits)'; fcdf{'prof_HHMMSS'} = ncdouble('iPROF'); fcdf{'prof_HHMMSS'}.missing_value=fill_value_output; fcdf{'prof_HHMMSS'}.long_name='hour (2 digits), minute (2 digits), seconde (2 digits)'; fcdf{'prof_dir'} = ncdouble('iPROF'); fcdf{'prof_dir'}.missing_value=0; fcdf{'prof_dir'}.long_name='profile direction 1=asc 2=desc'; fcdf{'prof_lon'} = ncdouble('iPROF'); fcdf{'prof_lon'}.units='(degree E)'; fcdf{'prof_lon'}.missing_value=fill_value_output; fcdf{'prof_lat'} = ncdouble('iPROF'); fcdf{'prof_lat'}.units='(degree N)'; fcdf{'prof_lat'}.missing_value=fill_value_output; fcdf{'prof_T'} = ncdouble('iPROF','iDEPTH'); fcdf{'prof_T'}.long_name='potential temperature'; fcdf{'prof_T'}.units='degree Celsius'; fcdf{'prof_T'}.missing_value=fill_value_output; fcdf{'prof_Tweight'} = ncdouble('iPROF','iDEPTH'); fcdf{'prof_Tweight'}.long_name='weights'; fcdf{'prof_Tweight'}.units='(degree Celsius)^-2'; fcdf{'prof_Tweight'}.missing_value=0; fcdf{'prof_Ttest'} = ncdouble('iPROF','iDEPTH'); fcdf{'prof_Ttest'}.long_name='temperature flag'; fcdf{'prof_Ttest'}.units='(no units)'; fcdf{'prof_Ttest'}.missing_value=0; fcdf{'prof_S'} = ncdouble('iPROF','iDEPTH'); fcdf{'prof_S'}.long_name='salinity'; fcdf{'prof_S'}.units='(no units)'; fcdf{'prof_S'}.missing_value=fill_value_output; fcdf{'prof_Sweight'} = ncdouble('iPROF','iDEPTH'); fcdf{'prof_Sweight'}.long_name='weights'; fcdf{'prof_Sweight'}.units='(no units)^-2'; fcdf{'prof_Sweight'}.missing_value=0; fcdf{'prof_Stest'} = ncdouble('iPROF','iDEPTH'); fcdf{'prof_Stest'}.long_name='salinity flag'; fcdf{'prof_Stest'}.units='(no units)'; fcdf{'prof_Stest'}.missing_value=0; fcdf{'prof_descr'} = ncchar('iPROF','lTXT'); fcdf{'prof_descr'}.long_name='profile description'; %fill the vector of the standard depths : fcdf{'depth'}(1:nb_lev)=dmod; %fill the profiles arrays: cur_prof_interv=[0 0]; for cur_tmpfile=1:num_cur_tmpfile eval(['load ' rep_out '/' filename_out 'tmp' num2str(cur_tmpfile) '.mat;' ]); tmp_indice=min(find(isnan(data_i)))-1; if tmp_indice>0 cur_prof_interv=[cur_prof_interv(2)+1 cur_prof_interv(2)+tmp_indice]; %pre-treatment of the data : if file_content==1%the main file prof_T=squeeze(data_value2D(1,1:tmp_indice,:)); else prof_T=squeeze(data_equi2D(1,1:tmp_indice,:)); end prof_Tweight=squeeze(data_weights2D(1,1:tmp_indice,:)); prof_Ttest=squeeze(data_test2D(1,1:tmp_indice,:)); if file_content==1%the main file prof_Tweight(prof_T==fill_value_output|prof_Ttest>0)=0; else prof_T(isnan(prof_T))=fill_value_output; end if file_content==1%the main file prof_S=squeeze(data_value2D(2,1:tmp_indice,:)); else prof_S=squeeze(data_equi2D(2,1:tmp_indice,:)); end prof_Sweight=squeeze(data_weights2D(2,1:tmp_indice,:)); prof_Stest=squeeze(data_test2D(2,1:tmp_indice,:)); if file_content==1%the main file prof_Sweight(prof_S==fill_value_output|prof_Stest>0)=0; else prof_S(isnan(prof_S))=fill_value_output; end tmp3=[1:tmp_indice]; %fill the date and position vectors : fcdf{'prof_YYYYMMDD'}(cur_prof_interv(1):cur_prof_interv(2))=data_t(tmp3); fcdf{'prof_HHMMSS'}(cur_prof_interv(1):cur_prof_interv(2))=data_t2(tmp3); fcdf{'prof_dir'}(cur_prof_interv(1):cur_prof_interv(2))=data_dir(tmp3); fcdf{'prof_lon'}(cur_prof_interv(1):cur_prof_interv(2))=data_i(tmp3); fcdf{'prof_lat'}(cur_prof_interv(1):cur_prof_interv(2))=data_j(tmp3); %fill the data matrices : fcdf{'prof_T'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_T(tmp3,:); fcdf{'prof_Tweight'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_Tweight(tmp3,:); fcdf{'prof_Ttest'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_Ttest(tmp3,:); fcdf{'prof_S'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_S(tmp3,:); fcdf{'prof_Sweight'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_Sweight(tmp3,:); fcdf{'prof_Stest'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_lev)=prof_Stest(tmp3,:); %fill the field describing the instruments : fcdf{'prof_descr'}(cur_prof_interv(1):cur_prof_interv(2),1:nb_char)=data_pnum_txt(tmp3,:); end; end; %close the file : fcdf=close(fcdf); end %for file_content=1:2 eval(['cd ' rep_bak]); for cur_tmpfile=1:num_cur_tmpfile eval(['!rm -f ' rep_out '/' filename_out 'tmp' num2str(cur_tmpfile) '.mat;' ]); end %needed to reinitialize arrays in the next case : data_pnum_txt=[]; end