/[MITgcm]/MITgcm_contrib/gael/profilesMatlabProcessing/profiles_misc/MITprof_resample.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/profilesMatlabProcessing/profiles_misc/MITprof_resample.m

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


Revision 1.6 - (hide annotations) (download)
Thu Jan 28 01:54:59 2016 UTC (9 years, 5 months ago) by gforget
Branch: MAIN
Changes since 1.5: +5 -1 lines
- MITprof_resample: bug fix

1 gforget 1.1 function [profOut]=MITprof_resample(profIn,fldIn,filOut,method,varargin);
2     %object: resample a set of fields in file filFldIn with specified time
3     % line timeIn to the positions of profIn and add to file filOut
4     %inputs: profIn is a gcmfaces field (nan-masked; up to N3,N4 dimensions)
5     % fldIn is a description of the fields being resampled including
6     % the corresponding file name and additional specs :
7     % fldIn.name, fldIn.long_name, fldIn.units, fldIn.fil (file
8     % name) and fldIn.tim (time axis specification). Supported
9     % fldIn.tim spec: 'const' (for time invariant climatology),
10     % 'monclim' (for monthly climatology), 'monser' (for monthly
11     % time series)
12     % filOut is the output MITprof file name (if un-specified
13     % the resul may only be returned as a function argument)
14     % method may be 'polygons' (or 'TriScatteredInterp' ... via
15     % gcmfaces_interp_2d in a loop ... to be implemented later)
16     %outputs: profOut is the MITprof structure where the interpolated values
17     % were appended to profIn (if un-specified the result
18     % may only be returned to output file)
19     %
20     %filFldIn is assumed to be 3D and binary at this point
21    
22     gcmfaces_global;
23    
24     doOut=~isempty(who('filOut')); doOutInit=false;
25     if doOut; doOut=~isempty(filOut); end;
26     if doOut; doOutInit=isempty(dir(filOut)); end;
27    
28     if isempty(who('method')); method='polygons'; end;
29    
30     %1) deal with time line
31     if strcmp(fldIn.tim,'monclim');
32     tim_fld=[-0.5:12.5]; rec_fld=[12 1:12 1];
33     tmp1=datevec(profIn.prof_date);
34     tmp2=datenum([tmp1(:,1) ones(profIn.np,2) zeros(profIn.np,3)]);
35     tim_prof=(profIn.prof_date-tmp2);
36     tim_prof(tim_prof>365)=365;
37     tim_prof=tim_prof/365*12;%neglecting differences in months length
38 gforget 1.5 elseif strcmp(fldIn.tim,'monloop');
39     tmp1=dir(fldIn.fil);
40     nt=tmp1.bytes/prod(mygrid.ioSize)/length(mygrid.RC)/4;
41     tmp1=[1:nt]'; tmp2=ones(nt,1)*[1992 1 15 0 0 0]; tmp2(:,2)=tmp1;
42     tim_fld=datenum(tmp2);
43     tim_fld=[tim_fld(1)-31 tim_fld' tim_fld(end)+31];
44     rec_fld=[nt 1:nt 1];
45     tmp1=datenum([1992 1 1 0 0 0]);
46     tmp2=datenum([1992+nt/12 1 1 0 0 0]);;
47 gforget 1.6 tim_prof=tmp1+mod(profIn.prof_date-tmp1,tmp2-tmp1);
48     %round up tim_prof to prevent interpolation in time:
49     % tmp3=tim_prof*ones(1,length(tim_fld))-ones(length(tim_prof),1)*tim_fld;
50     % tmp4=sum(tmp3>0,2);
51     % tim_prof=tim_fld(tmp4)';
52 gforget 1.3 elseif strcmp(fldIn.tim,'const')|strcmp(fldIn.tim,'std');
53     tim_fld=[1 2]; rec_fld=[1 1];
54 gforget 1.1 tim_prof=1.5*ones(profIn.np,1);
55     else;
56     error('this case remains to be implemented');
57     end;
58    
59     lon=profIn.prof_lon; lat=profIn.prof_lat;
60     depIn=-mygrid.RC; depOut=profIn.prof_depth;
61     profOut=NaN*ones(profIn.np,profIn.nr);
62    
63     %2) loop over record pairs
64 gforget 1.5 if ~strcmp(method,'bindata'); gcmfaces_bindata; end;
65 gforget 1.1 for tt=1:length(rec_fld)-1;
66 gforget 1.5 tt
67     %
68 gforget 1.2 fld0=mygrid.mskC.*read_bin(fldIn.fil,rec_fld(tt));
69     fld1=mygrid.mskC.*read_bin(fldIn.fil,rec_fld(tt+1));
70 gforget 1.1 ndim=length(size(fld0{1}));
71     fld=cat(ndim+1,fld0,fld1);
72     %
73     ii=find(tim_prof>=tim_fld(tt)&tim_prof<tim_fld(tt+1));
74     if ~isempty(ii);
75 gforget 1.5 if ~strcmp(method,'bindata');
76 gforget 1.3 arr=gcmfaces_interp(fld,lon(ii),lat(ii),method);
77 gforget 1.1 arr2=gcmfaces_interp_1d(2,depIn,arr,depOut);
78 gforget 1.5 %now linear in time:
79     k0=floor(tim_prof(ii)); k1=k0+1;
80     a0=tim_prof(ii)-k0; a0=a0*ones(1,profIn.nr);
81     profOut(ii,:)=(1-a0).*arr2(:,:,1)+a0.*arr2(:,:,2);
82     else;
83     [prof_i,prof_j]=gcmfaces_bindata(lon(ii),lat(ii));
84     FLD=convert2array(fld(:,:,:,1));
85     nk=length(mygrid.RC); kk=ones(1,nk);
86     np=length(ii); pp=ones(np,1);
87     ind2prof=sub2ind(size(FLD),prof_i*kk,prof_j*kk,pp*[1:nk]);
88     arr=FLD(ind2prof);
89     arr2=gcmfaces_interp_1d(2,depIn,arr,depOut);
90     profOut(ii,:)=arr2;
91     end;
92     %
93     if strcmp(fldIn.tim,'std');
94 gforget 1.4 profOut(ii,:)=profOut(ii,:).*randn(size(profOut(ii,:)));
95 gforget 1.5 end;
96 gforget 1.4 end;
97 gforget 1.1 end;
98    
99     %3) deal with file output
100     if doOutInit;
101     %create a header only file to later append resampled fields
102     prof=profIn;
103     tmp1=fieldnames(prof);
104     nt=length(prof.prof_date);
105     nr=length(prof.prof_depth);
106     for ii=1:length(tmp1);
107     tmp2=prod(size(getfield(prof,tmp1{ii})));
108     if tmp2==nt*nr; prof=rmfield(prof,tmp1{ii}); end;
109     end;
110     MITprof_write(filOut,prof);
111     end;
112    
113     if doOut;
114     %add the array itelf
115     MITprof_addVar(filOut,fldIn.name,'double',{'iDEPTH','iPROF'},profOut);
116    
117     %add its attributes
118     nc=ncopen(filOut,'write');
119     ncaddAtt(nc,fldIn.name,'long_name',fldIn.long_name);
120     ncaddAtt(nc,fldIn.name,'units',fldIn.units);
121     ncaddAtt(nc,fldIn.name,'missing_value',fldIn.missing_value);
122     ncaddAtt(nc,fldIn.name,'_FillValue',fldIn.FillValue);
123     ncclose(nc);
124     end;
125    
126     %4) deal with argument output
127     if nargout>0; profOut=setfield(profIn,fldIn.name,profOut); end;
128    
129    

  ViewVC Help
Powered by ViewVC 1.1.22