%function: netcdf_ecco_recompose %object: recompose a netcdf data files in the "ECCO format" % from binary and tiled model outputs (the default) %author: Gael Forget (gforget@mit.edu) %date: Apr 17th, 2006 % warning off MATLAB:mir_warning_variable_used_as_function; %choose files: %[file_data,rep_data,tmp2]=uigetfile('*.nc','please pick a data file'); %tmp2=strfind(file_data,'.nc'); tmp1=file_data(1:tmp2-1); %[file_model,rep_model,tmp2]=uigetfile([tmp1 '*'],... %'please pick one of the \n associated model couterpart files'); %rep_out=uigetdir('please choose the output directory'); rep_model='./'; rep_data=rep_model; rep_out=rep_model; file_data='prof_exemple.nc'; file_model='prof_exemple.001.001.equi.data'; %detect mode file type: tmp2=strfind(file_model,'.'); model_file_type=file_model(tmp2(end)+1:end); %process all the data files at once? user_choice=input('Do you want to process all the other data files in this directory as well? y\n','s'); if ~isempty(findstr(user_choice,'y')) %build up the list: eval(['files_list=ls('' -1 ' rep_data '*.nc '');']); tmp1=files_list; tmp2=strfind(files_list,rep_data)+length(rep_data); tmp3=strfind(files_list,'.nc')+2; files_list=''; for tmp4=1:length(tmp2); files_list=strvcat(files_list, tmp1(tmp2(tmp4):tmp3(tmp4)) ); end else %one single file: files_list=file_data; end %loop over files: for f_cur=1:size(files_list,1) %initialize the process: clear prof_*; file_data2=files_list(f_cur,:); %test whether this file is relevant: eval(['ncload ' rep_data file_data2 ' prof_YYYYMMDD;' ]); if ~isempty(prof_YYYYMMDD)&isempty(findstr(file_data2,'equi')) %load the data: eval(['ncload ' rep_data file_data2 ';' ]); list_var=zeros(6,1); nbvars=0; if ~isempty(whos('prof_T')); nbvars=nbvars+1; list_var(1)=nbvars; prof_Tequi=0*prof_T; prof_Tmask=0*prof_T; end; if ~isempty(whos('prof_S')); nbvars=nbvars+1; list_var(2)=nbvars; prof_Sequi=0*prof_S; prof_Smask=0*prof_S; end; if ~isempty(whos('prof_U')); nbvars=nbvars+1; list_var(3)=nbvars; prof_Uequi=0*prof_U; prof_Umask=0*prof_U; end; if ~isempty(whos('prof_V')); nbvars=nbvars+1; list_var(4)=nbvars; prof_Vequi=0*prof_V; prof_Vmask=0*prof_V; end; if ~isempty(whos('prof_ptr')); nbvars=nbvars+1; list_var(5)=nbvars; prof_ptrequi=0*prof_ptr; prof_ptrmask=0*prof_ptr; end; if ~isempty(whos('prof_ssh')); nbvars=nbvars+1; list_var(6)=nbvars; prof_sshequi=0*prof_ssh; prof_sshmask=0*prof_ssh; end; nbdepths=length(depth); %make the list associated with tiles/processors: tmp1=strfind(file_data2,'.nc'); %I do the test with dir, because ls returns an error if no file is found eval(['model_files_list=dir(''' rep_model file_data2(1:tmp1-1) '*.' model_file_type ''');']); if size(model_files_list,1)~=0; %RK: model_files_list(1).name; => name of the first struct element eval(['model_files_list=ls('' -1 ' rep_model file_data2(1:tmp1-1) '*.' model_file_type ''');']); list_beg=findstr(model_files_list,file_data2(1:tmp1-1)); list_end=findstr(model_files_list,['.' model_file_type])+length(model_file_type); %OLD test: if ~isempty(list_beg) ... now ensured by the above test %loop over model files: for f_cur2=1:length(list_beg) file_model2=model_files_list(list_beg(f_cur2):list_end(f_cur2)); %fill the global array: %if ~isempty(findstr(file_model2(end-2:end),'bin')); if ~isempty(findstr(file_model2,'.equi.')); fid0=fopen([rep_model file_model2],'r','b'); irec=1; recl=(nbdepths+1)*8; while ~feof(fid0) position0=recl*nbvars*2*(irec-1); status=fseek(fid0,position0,'bof'); if list_var(1)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_Tequi(tmp1(end),:)=tmp1(1:end-1)'; tmp1=fread(fid0,nbdepths+1,'float64'); prof_Tmask(tmp1(end),:)=tmp1(1:end-1)'; end; end; if list_var(2)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_Sequi(tmp1(end),:)=tmp1(1:end-1)'; tmp1=fread(fid0,nbdepths+1,'float64'); prof_Smask(tmp1(end),:)=tmp1(1:end-1)'; end; end; if list_var(3)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_Uequi(tmp1(end),:)=tmp1(1:end-1)'; tmp1=fread(fid0,nbdepths+1,'float64'); prof_Umask(tmp1(end),:)=tmp1(1:end-1)'; end; end; if list_var(4)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_Vequi(tmp1(end),:)=tmp1(1:end-1)'; tmp1=fread(fid0,nbdepths+1,'float64'); prof_Vmask(tmp1(end),:)=tmp1(1:end-1)'; end; end; if list_var(5)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_ptrequi(tmp1(end),:)=tmp1(1:end-1)'; tmp1=fread(fid0,nbdepths+1,'float64'); prof_ptrmask(tmp1(end),:)=tmp1(1:end-1)'; end; end; if list_var(6)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_sshequi(tmp1(end),:)=tmp1(1:end-1)'; tmp1=fread(fid0,nbdepths+1,'float64'); prof_sshmask(tmp1(end),:)=tmp1(1:end-1)'; end; end; if ~isempty(find(tmp1>1e10)); 1; end; irec=irec+1; position0=recl*nbvars*2*(irec-1); status=fseek(fid0,position0,'bof'); tmp1=fread(fid0,1,'float64'); end;%while fclose(fid0); elseif ~isempty(findstr(file_model2(end-1:end),'nc')); nc=netcdf([rep_model file_model2 '.nc'],'nowrite'); if list_var(1)~=0; tmp1=nc{'prof_Tmask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:); tmp4=nc{'prof_T'}(:); prof_Tequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_Tmask(tmp3(tmp2),:)=tmp2(tmp3,:); end; if list_var(2)~=0; tmp1=nc{'prof_Smask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:); tmp4=nc{'prof_S'}(:); prof_Sequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_Smask(tmp3(tmp2),:)=tmp2(tmp3,:); end; if list_var(3)~=0; tmp1=nc{'prof_Umask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:); tmp4=nc{'prof_U'}(:); prof_Uequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_Umask(tmp3(tmp2),:)=tmp2(tmp3,:); end; if list_var(4)~=0; tmp1=nc{'prof_Vmask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:); tmp4=nc{'prof_V'}(:); prof_Vequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_Vmask(tmp3(tmp2),:)=tmp2(tmp3,:); end; if list_var(5)~=0; tmp1=nc{'prof_ptrmask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:); tmp4=nc{'prof_ptr'}(:); prof_ptrequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_ptrmask(tmp3(tmp2),:)=tmp2(tmp3,:); end; if list_var(6)~=0; tmp1=nc{'prof_sshmask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:); tmp4=nc{'prof_ssh'}(:); prof_sshequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_sshmask(tmp3(tmp2),:)=tmp2(tmp3,:); end; nc = close(nc); end;%if end; %for f_cur2 %save the global array to file: tmp1=findstr(file_data2,'.nc'); file_model3=[file_data2(1:tmp1-1) '.equi.all.nc']; tmp1=dir([rep_out file_model3]); %tmp1=ls([rep_out file_model3]); %complete an existing file: if ~isempty(tmp1) %if isempty(strfind(tmp1,'No such file')); fcdf=netcdf([rep_out file_model3],'write'); if ~isempty(whos('prof_T')); prof_tmp=prof_Tmask; prof_T=fcdf{'prof_T'}(:,:); prof_Tmask=fcdf{'prof_Tmask'}(:,:); tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0); if ~isempty(tmp1); prof_T(tmp1,:)=prof_Tequi(tmp1,:); prof_Tmask(tmp1,:)=prof_tmp(tmp1,:); end; fcdf{'prof_T'}(:,:)=prof_T(:,:); fcdf{'prof_Tmask'}(:,:)=prof_Tmask(:,:); end; if ~isempty(whos('prof_S')); prof_tmp=prof_Smask; prof_S=fcdf{'prof_S'}(:,:); prof_Smask=fcdf{'prof_Smask'}(:,:); tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0); if ~isempty(tmp1); prof_S(tmp1,:)=prof_Sequi(tmp1,:); prof_Smask(tmp1,:)=prof_tmp(tmp1,:); end; fcdf{'prof_S'}(:,:)=prof_S(:,:); fcdf{'prof_Smask'}(:,:)=prof_Smask(:,:); end; if ~isempty(whos('prof_U')); prof_tmp=prof_Umask; prof_U=fcdf{'prof_U'}(:,:); prof_Umask=fcdf{'prof_Umask'}(:,:); tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0); if ~isempty(tmp1); prof_U(tmp1,:)=prof_Uequi(tmp1,:); prof_Umask(tmp1,:)=prof_tmp(tmp1,:); end; fcdf{'prof_U'}(:,:)=prof_U(:,:); fcdf{'prof_Umask'}(:,:)=prof_Umask(:,:); end; if ~isempty(whos('prof_V')); prof_tmp=prof_Vmask; prof_V=fcdf{'prof_V'}(:,:); prof_Vmask=fcdf{'prof_Vmask'}(:,:); tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0); if ~isempty(tmp1); prof_V(tmp1,:)=prof_Vequi(tmp1,:); prof_Vmask(tmp1,:)=prof_tmp(tmp1,:); end; fcdf{'prof_V'}(:,:)=prof_V(:,:); fcdf{'prof_Vmask'}(:,:)=prof_Vmask(:,:); end; if ~isempty(whos('prof_ptr')); prof_tmp=prof_ptrmask; prof_ptr=fcdf{'prof_ptr'}(:,:); prof_ptrmask=fcdf{'prof_ptrmask'}(:,:); tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0); if ~isempty(tmp1); prof_ptr(tmp1,:)=prof_ptrequi(tmp1,:); prof_ptrmask(tmp1,:)=prof_tmp(tmp1,:); end; fcdf{'prof_ptr'}(:,:)=prof_ptr(:,:); fcdf{'prof_ptrmask'}(:,:)=prof_ptrmask(:,:); end; if ~isempty(whos('prof_ssh')); prof_tmp=prof_sshmask; prof_ssh=fcdf{'prof_ssh'}(:,:); prof_sshmask=fcdf{'prof_sshmask'}(:,:); tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0); if ~isempty(tmp1); prof_ssh(tmp1,:)=prof_sshequi(tmp1,:); prof_sshmask(tmp1,:)=prof_tmp(tmp1,:); end; fcdf{'prof_ssh'}(:,:)=prof_ssh(:,:); fcdf{'prof_sshmask'}(:,:)=prof_sshmask(:,:); end; %close the file : fcdf=close(fcdf); %create a new file else mystruct_txt='mystruct=struct('; if ~isempty(who('prof_T'));mystruct_txt=[mystruct_txt '''prof_T'',{prof_Tequi},']; mystruct_txt=[mystruct_txt '''prof_Tmask'',{prof_Tmask},'];end; if ~isempty(who('prof_S')); mystruct_txt=[mystruct_txt '''prof_S'',{prof_Sequi},']; mystruct_txt=[mystruct_txt '''prof_Smask'',{prof_Smask},'];end; if ~isempty(who('prof_U'));mystruct_txt=[mystruct_txt '''prof_U'',{prof_Uequi},']; mystruct_txt=[mystruct_txt '''prof_Umask'',{prof_Umask},'];end; if ~isempty(who('prof_V'));mystruct_txt=[mystruct_txt '''prof_V'',{prof_Vequi},']; mystruct_txt=[mystruct_txt '''prof_Vmask'',{prof_Vmask},'];end; if ~isempty(who('prof_ptr'));mystruct_txt=[mystruct_txt '''prof_ptr'',{prof_ptrequi},']; mystruct_txt=[mystruct_txt '''prof_ptrmask'',{prof_ptrmask},'];end; if ~isempty(who('prof_ssh'));mystruct_txt=[mystruct_txt '''prof_ssh'',{prof_sshequi},']; mystruct_txt=[mystruct_txt '''prof_sshmask'',{prof_sshmask},'];end; mystruct_txt=[mystruct_txt(1:end-1) ');']; eval(mystruct_txt); mystruct1D=struct('prof_YYYYMMDD',prof_YYYYMMDD,'prof_HHMMSS',prof_HHMMSS,... 'prof_lon',prof_lon,'prof_lat',prof_lat); file_characteristics=[length(prof_lon) 0 30 -9999]; prof_descr=ones(length(prof_lon),1)*double('model'); netcdf_ecco_create([rep_out file_model3], file_characteristics, depth, ... prof_descr, mystruct, mystruct1D); end; fprintf(['file: ' file_data2 ' \n has been processed \n']); else fprintf(['file: ' file_data2 ' \n, no model files found\n']); %OLD end; %if ~isempty(list_beg)... end %if size(model_files_list,1)~=0; else fprintf(['file: ' file_data2 ' \n appeared irrelevant, and was skipped \n']); end%if ~isempty(prof_YYYYMMDD)... end%for f_cur=1:size(files_list,1)...