/[MITgcm]/MITgcm_contrib/gael/profilesMatlabProcessing/profiles_IO/netcdf_ecco_recompose.m
ViewVC logotype

Contents of /MITgcm_contrib/gael/profilesMatlabProcessing/profiles_IO/netcdf_ecco_recompose.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download)
Wed Jan 5 22:07:04 2011 UTC (15 years, 7 months ago) by gforget
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
FILE REMOVED
- remove old codes.

1 %function: netcdf_ecco_recompose
2 %object: recompose a netcdf data files in the "ECCO format"
3 % from binary and tiled model outputs (the default)
4 %author: Gael Forget (gforget@mit.edu)
5 %date: Apr 17th, 2006
6 %
7
8 warning off MATLAB:mir_warning_variable_used_as_function;
9
10 %choose files:
11 %[file_data,rep_data,tmp2]=uigetfile('*.nc','please pick a data file');
12 %tmp2=strfind(file_data,'.nc'); tmp1=file_data(1:tmp2-1);
13 %[file_model,rep_model,tmp2]=uigetfile([tmp1 '*'],...
14 %'please pick one of the \n associated model couterpart files');
15 %rep_out=uigetdir('please choose the output directory');
16
17 rep_model='./'; rep_data=rep_model; rep_out=rep_model;
18 file_data='prof_exemple.nc'; file_model='prof_exemple.001.001.equi.data';
19
20 %detect mode file type:
21 tmp2=strfind(file_model,'.'); model_file_type=file_model(tmp2(end)+1:end);
22
23 %process all the data files at once?
24 user_choice=input('Do you want to process all the other data files in this directory as well? y\n','s');
25 if ~isempty(findstr(user_choice,'y'))
26 %build up the list:
27 eval(['files_list=ls('' -1 ' rep_data '*.nc '');']);
28 tmp1=files_list; tmp2=strfind(files_list,rep_data)+length(rep_data); tmp3=strfind(files_list,'.nc')+2;
29 files_list='';
30 for tmp4=1:length(tmp2);
31 files_list=strvcat(files_list, tmp1(tmp2(tmp4):tmp3(tmp4)) );
32 end
33 else
34 %one single file:
35 files_list=file_data;
36 end
37
38 %loop over files:
39 for f_cur=1:size(files_list,1)
40
41 %initialize the process:
42 clear prof_*; file_data2=files_list(f_cur,:);
43
44 %test whether this file is relevant:
45 eval(['ncload ' rep_data file_data2 ' prof_YYYYMMDD;' ]);
46 if ~isempty(prof_YYYYMMDD)&isempty(findstr(file_data2,'equi'))
47
48 %load the data:
49 eval(['ncload ' rep_data file_data2 ';' ]); list_var=zeros(6,1); nbvars=0;
50 if ~isempty(whos('prof_T')); nbvars=nbvars+1; list_var(1)=nbvars; prof_Tequi=0*prof_T; prof_Tmask=0*prof_T; end;
51 if ~isempty(whos('prof_S')); nbvars=nbvars+1; list_var(2)=nbvars; prof_Sequi=0*prof_S; prof_Smask=0*prof_S; end;
52 if ~isempty(whos('prof_U')); nbvars=nbvars+1; list_var(3)=nbvars; prof_Uequi=0*prof_U; prof_Umask=0*prof_U; end;
53 if ~isempty(whos('prof_V')); nbvars=nbvars+1; list_var(4)=nbvars; prof_Vequi=0*prof_V; prof_Vmask=0*prof_V; end;
54 if ~isempty(whos('prof_ptr')); nbvars=nbvars+1; list_var(5)=nbvars; prof_ptrequi=0*prof_ptr; prof_ptrmask=0*prof_ptr; end;
55 if ~isempty(whos('prof_ssh')); nbvars=nbvars+1; list_var(6)=nbvars; prof_sshequi=0*prof_ssh; prof_sshmask=0*prof_ssh; end;
56 nbdepths=length(depth);
57
58 %make the list associated with tiles/processors:
59 tmp1=strfind(file_data2,'.nc');
60 %I do the test with dir, because ls returns an error if no file is found
61 eval(['model_files_list=dir(''' rep_model file_data2(1:tmp1-1) '*.' model_file_type ''');']);
62 if size(model_files_list,1)~=0;
63 %RK: model_files_list(1).name; => name of the first struct element
64
65 eval(['model_files_list=ls('' -1 ' rep_model file_data2(1:tmp1-1) '*.' model_file_type ''');']);
66 list_beg=findstr(model_files_list,file_data2(1:tmp1-1));
67 list_end=findstr(model_files_list,['.' model_file_type])+length(model_file_type);
68
69 %OLD test: if ~isempty(list_beg) ... now ensured by the above test
70
71 %loop over model files:
72 for f_cur2=1:length(list_beg)
73 file_model2=model_files_list(list_beg(f_cur2):list_end(f_cur2));
74
75 %fill the global array:
76 %if ~isempty(findstr(file_model2(end-2:end),'bin'));
77 if ~isempty(findstr(file_model2,'.equi.'));
78 fid0=fopen([rep_model file_model2],'r','b');
79 irec=1; recl=(nbdepths+1)*8;
80 while ~feof(fid0)
81 position0=recl*nbvars*2*(irec-1); status=fseek(fid0,position0,'bof');
82 if list_var(1)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_Tequi(tmp1(end),:)=tmp1(1:end-1)';
83 tmp1=fread(fid0,nbdepths+1,'float64'); prof_Tmask(tmp1(end),:)=tmp1(1:end-1)'; end; end;
84 if list_var(2)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_Sequi(tmp1(end),:)=tmp1(1:end-1)';
85 tmp1=fread(fid0,nbdepths+1,'float64'); prof_Smask(tmp1(end),:)=tmp1(1:end-1)'; end; end;
86 if list_var(3)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0;
87 prof_Uequi(tmp1(end),:)=tmp1(1:end-1)';
88 tmp1=fread(fid0,nbdepths+1,'float64'); prof_Umask(tmp1(end),:)=tmp1(1:end-1)'; end; end;
89 if list_var(4)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_Vequi(tmp1(end),:)=tmp1(1:end-1)';
90 tmp1=fread(fid0,nbdepths+1,'float64'); prof_Vmask(tmp1(end),:)=tmp1(1:end-1)'; end; end;
91 if list_var(5)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_ptrequi(tmp1(end),:)=tmp1(1:end-1)';
92 tmp1=fread(fid0,nbdepths+1,'float64'); prof_ptrmask(tmp1(end),:)=tmp1(1:end-1)'; end; end;
93 if list_var(6)~=0; tmp1=fread(fid0,nbdepths+1,'float64'); if tmp1(end)~=0; prof_sshequi(tmp1(end),:)=tmp1(1:end-1)';
94 tmp1=fread(fid0,nbdepths+1,'float64'); prof_sshmask(tmp1(end),:)=tmp1(1:end-1)'; end; end;
95
96 if ~isempty(find(tmp1>1e10));
97 1;
98 end;
99
100
101 irec=irec+1;
102 position0=recl*nbvars*2*(irec-1); status=fseek(fid0,position0,'bof'); tmp1=fread(fid0,1,'float64');
103 end;%while
104 fclose(fid0);
105
106 elseif ~isempty(findstr(file_model2(end-1:end),'nc'));
107 nc=netcdf([rep_model file_model2 '.nc'],'nowrite');
108 if list_var(1)~=0; tmp1=nc{'prof_Tmask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:);
109 tmp4=nc{'prof_T'}(:); prof_Tequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_Tmask(tmp3(tmp2),:)=tmp2(tmp3,:); end;
110 if list_var(2)~=0; tmp1=nc{'prof_Smask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:);
111 tmp4=nc{'prof_S'}(:); prof_Sequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_Smask(tmp3(tmp2),:)=tmp2(tmp3,:); end;
112 if list_var(3)~=0; tmp1=nc{'prof_Umask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:);
113 tmp4=nc{'prof_U'}(:); prof_Uequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_Umask(tmp3(tmp2),:)=tmp2(tmp3,:); end;
114 if list_var(4)~=0; tmp1=nc{'prof_Vmask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:);
115 tmp4=nc{'prof_V'}(:); prof_Vequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_Vmask(tmp3(tmp2),:)=tmp2(tmp3,:); end;
116 if list_var(5)~=0; tmp1=nc{'prof_ptrmask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:);
117 tmp4=nc{'prof_ptr'}(:); prof_ptrequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_ptrmask(tmp3(tmp2),:)=tmp2(tmp3,:); end;
118 if list_var(6)~=0; tmp1=nc{'prof_sshmask'}(:); tmp2=find(sum(tmp1,1)>0); tmp3=nc{'prof_ind_glob'}(:);
119 tmp4=nc{'prof_ssh'}(:); prof_sshequi(tmp3(tmp2),:)=tmp4(tmp2,:); prof_sshmask(tmp3(tmp2),:)=tmp2(tmp3,:); end;
120 nc = close(nc);
121 end;%if
122 end; %for f_cur2
123
124 %save the global array to file:
125 tmp1=findstr(file_data2,'.nc'); file_model3=[file_data2(1:tmp1-1) '.equi.all.nc'];
126 tmp1=dir([rep_out file_model3]);
127 %tmp1=ls([rep_out file_model3]);
128 %complete an existing file:
129 if ~isempty(tmp1)
130 %if isempty(strfind(tmp1,'No such file'));
131 fcdf=netcdf([rep_out file_model3],'write');
132 if ~isempty(whos('prof_T'));
133 prof_tmp=prof_Tmask; prof_T=fcdf{'prof_T'}(:,:); prof_Tmask=fcdf{'prof_Tmask'}(:,:);
134 tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0);
135 if ~isempty(tmp1); prof_T(tmp1,:)=prof_Tequi(tmp1,:); prof_Tmask(tmp1,:)=prof_tmp(tmp1,:); end;
136 fcdf{'prof_T'}(:,:)=prof_T(:,:); fcdf{'prof_Tmask'}(:,:)=prof_Tmask(:,:); end;
137 if ~isempty(whos('prof_S'));
138 prof_tmp=prof_Smask; prof_S=fcdf{'prof_S'}(:,:); prof_Smask=fcdf{'prof_Smask'}(:,:);
139 tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0);
140 if ~isempty(tmp1); prof_S(tmp1,:)=prof_Sequi(tmp1,:); prof_Smask(tmp1,:)=prof_tmp(tmp1,:); end;
141 fcdf{'prof_S'}(:,:)=prof_S(:,:); fcdf{'prof_Smask'}(:,:)=prof_Smask(:,:); end;
142 if ~isempty(whos('prof_U'));
143 prof_tmp=prof_Umask; prof_U=fcdf{'prof_U'}(:,:); prof_Umask=fcdf{'prof_Umask'}(:,:);
144 tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0);
145 if ~isempty(tmp1); prof_U(tmp1,:)=prof_Uequi(tmp1,:); prof_Umask(tmp1,:)=prof_tmp(tmp1,:); end;
146 fcdf{'prof_U'}(:,:)=prof_U(:,:); fcdf{'prof_Umask'}(:,:)=prof_Umask(:,:); end;
147 if ~isempty(whos('prof_V'));
148 prof_tmp=prof_Vmask; prof_V=fcdf{'prof_V'}(:,:); prof_Vmask=fcdf{'prof_Vmask'}(:,:);
149 tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0);
150 if ~isempty(tmp1); prof_V(tmp1,:)=prof_Vequi(tmp1,:); prof_Vmask(tmp1,:)=prof_tmp(tmp1,:); end;
151 fcdf{'prof_V'}(:,:)=prof_V(:,:); fcdf{'prof_Vmask'}(:,:)=prof_Vmask(:,:); end;
152 if ~isempty(whos('prof_ptr'));
153 prof_tmp=prof_ptrmask; prof_ptr=fcdf{'prof_ptr'}(:,:); prof_ptrmask=fcdf{'prof_ptrmask'}(:,:);
154 tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0);
155 if ~isempty(tmp1); prof_ptr(tmp1,:)=prof_ptrequi(tmp1,:); prof_ptrmask(tmp1,:)=prof_tmp(tmp1,:); end;
156 fcdf{'prof_ptr'}(:,:)=prof_ptr(:,:); fcdf{'prof_ptrmask'}(:,:)=prof_ptrmask(:,:); end;
157 if ~isempty(whos('prof_ssh'));
158 prof_tmp=prof_sshmask; prof_ssh=fcdf{'prof_ssh'}(:,:); prof_sshmask=fcdf{'prof_sshmask'}(:,:);
159 tmp1=sum(prof_tmp,2); tmp1=find(tmp1>0);
160 if ~isempty(tmp1); prof_ssh(tmp1,:)=prof_sshequi(tmp1,:); prof_sshmask(tmp1,:)=prof_tmp(tmp1,:); end;
161 fcdf{'prof_ssh'}(:,:)=prof_ssh(:,:); fcdf{'prof_sshmask'}(:,:)=prof_sshmask(:,:); end;
162 %close the file :
163 fcdf=close(fcdf);
164
165 %create a new file
166 else
167 mystruct_txt='mystruct=struct(';
168 if ~isempty(who('prof_T'));mystruct_txt=[mystruct_txt '''prof_T'',{prof_Tequi},'];
169 mystruct_txt=[mystruct_txt '''prof_Tmask'',{prof_Tmask},'];end;
170 if ~isempty(who('prof_S')); mystruct_txt=[mystruct_txt '''prof_S'',{prof_Sequi},'];
171 mystruct_txt=[mystruct_txt '''prof_Smask'',{prof_Smask},'];end;
172 if ~isempty(who('prof_U'));mystruct_txt=[mystruct_txt '''prof_U'',{prof_Uequi},'];
173 mystruct_txt=[mystruct_txt '''prof_Umask'',{prof_Umask},'];end;
174 if ~isempty(who('prof_V'));mystruct_txt=[mystruct_txt '''prof_V'',{prof_Vequi},'];
175 mystruct_txt=[mystruct_txt '''prof_Vmask'',{prof_Vmask},'];end;
176 if ~isempty(who('prof_ptr'));mystruct_txt=[mystruct_txt '''prof_ptr'',{prof_ptrequi},'];
177 mystruct_txt=[mystruct_txt '''prof_ptrmask'',{prof_ptrmask},'];end;
178 if ~isempty(who('prof_ssh'));mystruct_txt=[mystruct_txt '''prof_ssh'',{prof_sshequi},'];
179 mystruct_txt=[mystruct_txt '''prof_sshmask'',{prof_sshmask},'];end;
180 mystruct_txt=[mystruct_txt(1:end-1) ');']; eval(mystruct_txt);
181
182 mystruct1D=struct('prof_YYYYMMDD',prof_YYYYMMDD,'prof_HHMMSS',prof_HHMMSS,...
183 'prof_lon',prof_lon,'prof_lat',prof_lat);
184 file_characteristics=[length(prof_lon) 0 30 -9999];
185 prof_descr=ones(length(prof_lon),1)*double('model');
186 netcdf_ecco_create([rep_out file_model3], file_characteristics, depth, ...
187 prof_descr, mystruct, mystruct1D);
188 end;
189
190 fprintf(['file: ' file_data2 ' \n has been processed \n']);
191 else
192 fprintf(['file: ' file_data2 ' \n, no model files found\n']);
193 %OLD end; %if ~isempty(list_beg)...
194 end %if size(model_files_list,1)~=0;
195
196 else
197 fprintf(['file: ' file_data2 ' \n appeared irrelevant, and was skipped \n']);
198 end%if ~isempty(prof_YYYYMMDD)...
199 end%for f_cur=1:size(files_list,1)...
200
201
202

  ViewVC Help
Powered by ViewVC 1.1.22