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

Annotation of /MITgcm_contrib/gael/profilesMatlabProcessing/netcdf_ecco_recompose.m

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


Revision 1.1 - (hide annotations) (download)
Fri Jun 15 05:39:42 2007 UTC (18 years, 1 month ago) by gforget
Branch: MAIN
matlab scripts to process pkg/profiles netcdf input files.

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

  ViewVC Help
Powered by ViewVC 1.1.22