source: trunk/src/read_civdata.m @ 1165

Last change on this file since 1165 was 1162, checked in by sommeria, 2 months ago

further cleaning

File size: 9.9 KB
Line 
1%'read_civdata': reads new civ data from netcdf files
2%------------------------------------------------------------------
3%
4% function [Field,VelTypeOut]=read_civdata(FileName,FieldNames,VelType)
5%
6% OUTPUT:
7% Field: structure representing the selected field, containing
8%            .Txt: (char string) error message if any
9%            .ListGlobalAttribute: list of global attributes containing:
10%                    .NbCoord: number of vector components
11%                    .NbDim: number of dimensions (=2 or 3)
12%                    .Dt: time interval for the corresponding image pair
13%                    .Time: absolute time (average of the initial image pair)
14%                    .CivStage: =0,
15%                       =1, civ1 has been performed only
16%                       =2, fix1 has been performed
17%                       =3, pacth1 has been performed
18%                       =4, civ2 has been performed
19%                       =5, fix2 has been performed
20%                       =6, pacth2 has been performed
21%                     .CoordUnit: 'pixel'
22%             .ListVarName: {'X'  'Y'  'U'  'V'  'F'  'FF'}
23%                   .X, .Y, .Z: set of vector coordinates
24%                    .U,.V,.W: corresponding set of vector components
25%                    .F: warning flags
26%                    .FF: error flag, =0 for good vectors
27%                     .C: scalar associated with velocity (used for vector colors)
28%
29% VelTypeOut: velocity type corresponding to the selected field: ='civ1','interp1','interp2','civ2'....
30%
31% INPUT:
32% FileName: file name (string).
33% FieldNames =cell of field names to get, which can contain the strings:
34% 'U','V','norm(U,V)','curl(U,V)','div(U,V)','strain(U,V)'
35% VelType : character string indicating the types of velocity fields to read ('civ1','civ2'...)
36%            if vel_type=[] or'*', a  priority choice, given by vel_type_out{1,2}, is done depending
37%            if vel_type='filter'; a structured field is sought (filter2 in priority, then filter1)
38%
39% FUNCTIONS called:
40% 'varcivx_generator':, sets the names of vaiables to read in the netcdf file
41% 'nc2struct': reads a netcdf file
42
43%=======================================================================
44% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
45%   http://www.legi.grenoble-inp.fr
46%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
47%
48%     This file is part of the toolbox UVMAT.
49%
50%     UVMAT is free software; you can redistribute it and/or modify
51%     it under the terms of the GNU General Public License as published
52%     by the Free Software Foundation; either version 2 of the license,
53%     or (at your option) any later version.
54%
55%     UVMAT is distributed in the hope that it will be useful,
56%     but WITHOUT ANY WARRANTY; without even the implied warranty of
57%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
58%     GNU General Public License (see LICENSE.txt) for more details.
59%=======================================================================
60
61function [Field,VelTypeOut,errormsg]=read_civdata(FileName,FieldNames,VelType,frame_index)
62
63%% default input
64if ~exist('frame_index','var')
65    frame_index=1;
66end
67if ~exist('VelType','var')
68    VelType='';
69end
70if isempty(VelType)||strcmp(VelType,'*')
71    VelType='';
72end
73if ~exist('FieldNames','var')
74    FieldNames={}; %default
75end
76Field=[];
77VelTypeOut=VelType;
78errormsg='';
79if ischar(FieldNames), FieldNames={FieldNames}; end
80ProjModeRequest='';
81for ilist=1:length(FieldNames)
82    if ~isempty(FieldNames{ilist})
83        switch FieldNames{ilist}
84            case {'U','V','norm(U,V)'}
85                if ~strcmp(FieldNames{1},'vec(U,V)')% if the scalar is not used as color of vectors
86                ProjModeRequest='interp_lin';
87                end
88            case {'curl(U,V)','div(U,V)','strain(U,V)'}
89                ProjModeRequest='interp_tps';
90        end
91    end
92end
93
94%% reading data
95[Data,tild,tild,errormsg]=nc2struct(FileName,'ListGlobalAttribute','Conventions','CivStage');
96if ~isempty(errormsg)
97     errormsg=['read_civdata: ' errormsg];
98    return
99end
100% set the list of variables to read and their role
101[varlist,role,VelTypeOut]=varcivx_generator(ProjModeRequest,VelType,Data.CivStage);
102if isempty(varlist)
103    errormsg=['read_civdata: unknow velocity type ' VelType];
104    return
105else
106    if strcmp(Data.Conventions,'uvmat/civdata_3D')
107        [Field,vardetect]=nc2struct(FileName,'TimeDimName','npz',frame_index,varlist);%read the variables in the netcdf file
108    else   
109    [Field,vardetect]=nc2struct(FileName,varlist);%read the variables in the netcdf file
110    end
111end
112if isfield(Field,'Txt')
113    errormsg=Field.Txt;
114    return
115end
116if vardetect(1)==0
117     errormsg=[ 'requested field not available in ' FileName '/' VelType ': need to run patch'];
118     return
119end
120switch VelTypeOut
121    case {'civ1','filter1'}
122        if isfield(Field,'Patch1_SubDomain')
123            Field.SubDomain=Field.Patch1_SubDomain;
124            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
125        end
126        if isfield(Field,'Civ1_Dt')
127            Field.Dt=Field.Civ1_Dt;
128        end
129        if isfield(Field,'Civ1_Time')
130            Field.Time=Field.Civ1_Time;
131        end
132    case {'civ2','filter2'}
133        if isfield(Field,'Patch2_SubDomain')
134            Field.SubDomain=Field.Patch2_SubDomain;
135            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
136        end
137        if isfield(Field,'Civ2_Dt')
138        Field.Dt=Field.Civ2_Dt;
139        end
140        if isfield(Field,'Civ2_Time')
141        Field.Time=Field.Civ2_Time;
142        end
143end
144Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'Dt','Time'}];
145ivar_U_tps=[];
146ivar_V_tps=[];
147var_ind=find(vardetect);
148for ivar=1:numel(var_ind)
149    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
150    %Field.VarAttribute{ivar}.Mesh=0.025;%typical mesh for histograms O.025 pixel (used in series)
151    Field.VarAttribute{ivar}.ProjModeRequest=ProjModeRequest;
152    if strcmp(role{var_ind(ivar)},'vector_x')
153        Field.VarAttribute{ivar}.FieldName=FieldNames;
154        ivar_U=ivar;
155    end
156    if strcmp(role{var_ind(ivar)},'vector_x_tps')
157        Field.VarAttribute{ivar}.FieldName=FieldNames;
158        ivar_U_tps=ivar;
159    end
160    if strcmp(role{var_ind(ivar)},'vector_y')
161        Field.VarAttribute{ivar}.FieldName=FieldNames;
162        ivar_V=ivar;
163    end
164    if strcmp(role{var_ind(ivar)},'vector_y_tps')
165        Field.VarAttribute{ivar}.FieldName=FieldNames;
166        ivar_V_tps=ivar;
167    end
168end
169if ~isempty(ivar_U_tps)
170    Field.VarAttribute{ivar_U}.VarIndex_tps=ivar_U_tps;
171end
172if ~isempty(ivar_V_tps)
173    Field.VarAttribute{ivar_V}.VarIndex_tps=ivar_V_tps;
174end
175
176%% update list of global attributes
177Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'NbCoord','NbDim','TimeUnit','CoordUnit'}];
178Field.NbCoord=2;
179Field.NbDim=2;
180Field.TimeUnit='s';
181Field.CoordUnit='pixel';
182
183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184% [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
185%INPUT:
186% FieldNames =cell of field names to get, which can contain the strings:
187%             'ima_cor': image correlation, vec_c or vec2_C
188%             'vort','div','strain': requires velocity derivatives DUDX...
189%             'error': error estimate (vec_E or vec2_E)
190%             
191% vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)
192%            if vel_type=[] or'*', a  priority choice is done, civ2 considered better than civ1 )
193
194function [var,role,vel_type_out,errormsg]=varcivx_generator(ProjModeRequest,vel_type,CivStage)
195
196%% default input values
197if ~exist('vel_type','var'),vel_type='';end
198if iscell(vel_type),vel_type=vel_type{1}; end%transform cell to string if needed
199errormsg='';
200if CivStage>=6
201    CivStage=6;
202end
203
204%% select the priority order for automatic vel_type selection
205if strcmp(ProjModeRequest,'derivatives')&& numel(vel_type)>=3 && strcmp(vel_type(1:3),'civ')
206    vel_type=['filter' vel_type(4:end)];
207end
208if isempty(vel_type)||strcmp(vel_type,'*')
209    vel_type='filter2';% case CivStage >=6
210    switch CivStage
211        case {1,2}% civ1 available but not filter1
212            vel_type='civ1';
213        case 3
214            vel_type='filter1';
215
216        case {4,5}% civ2 available but not filter2
217            if strcmp(ProjModeRequest,'derivatives')% derivatives needed
218                vel_type='filter1';
219            else
220                vel_type='civ2';
221            end
222    end
223end
224
225var={};
226switch vel_type
227    case{'civ1','civ2'}
228        varout={'X','Y','Z','U','V','W','C','FF'};
229        varin= {'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U','Civ1_V','Civ1_W','Civ1_C','Civ1_FF'};
230         role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag'}; 
231    case{'filter1','filter2'} 
232        varout={'X','Y','Z','U','V','W','C','FF','Coord_tps','U_tps','V_tps','W_tps','SubRange','NbCentre','NbCentre','NbCentre'};
233        varin={'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U_smooth','Civ1_V_smooth','Civ1_W','Civ1_C','Civ1_FF',...
234            'Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps','Civ1_W_tps','Civ1_SubRange','Civ1_NbCentre','Civ1_NbCentres','Civ1_NbSites'};
235        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag','coord_tps','vector_x_tps',...
236            'vector_y_tps','vector_z_tps','ancillary','ancillary','ancillary','ancillary'};
237          %  rmq: NbCentres and NbSites obsolete replaced by NbCentre, kept for consistency with previous data
238end
239switch vel_type
240    case {'civ2','filter2'}
241        varin=regexprep(varin,'1','2');
242    % case {'civ3','filter3'}
243    %     varin=regexprep(varin,'1','3');
244end
245var=[varout;varin];
246if ~strcmp(ProjModeRequest,'interp_tps')
247    var=var(:,1:8);%suppress tps if not needed
248end
249vel_type_out=vel_type;
250
251
252
253
254
Note: See TracBrowser for help on using the repository browser.