source: trunk/src/read_civdata.m

Last change on this file was 1135, checked in by sommeria, 41 hours ago

fct for python added

File size: 9.7 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)
62
63%% default input
64if ~exist('VelType','var')
65    VelType='';
66end
67if isempty(VelType)||strcmp(VelType,'*')
68    VelType='';
69end
70if ~exist('FieldNames','var')
71    FieldNames={}; %default
72end
73Field=[];
74VelTypeOut=VelType;
75errormsg='';
76if ischar(FieldNames), FieldNames={FieldNames}; end;
77ProjModeRequest='';
78for ilist=1:length(FieldNames)
79    if ~isempty(FieldNames{ilist})
80        switch FieldNames{ilist}
81            case {'U','V','norm(U,V)'}
82                if ~strcmp(FieldNames{1},'vec(U,V)')% if the scalar is not used as color of vectors
83                ProjModeRequest='interp_lin';
84                end
85            case {'curl(U,V)','div(U,V)','strain(U,V)'}
86                ProjModeRequest='interp_tps';
87        end
88    end
89end
90
91%% reading data
92[Data,tild,tild,errormsg]=nc2struct(FileName,'ListGlobalAttribute','CivStage');
93if ~isempty(errormsg)
94     errormsg=['read_civdata: ' errormsg];
95    return
96end
97% set the list of variables to read and their role
98[varlist,role,VelTypeOut]=varcivx_generator(ProjModeRequest,VelType,Data.CivStage);
99if isempty(varlist)
100    errormsg=['read_civdata: unknow velocity type ' VelType];
101    return
102else
103    [Field,vardetect]=nc2struct(FileName,varlist);%read the variables in the netcdf file
104end
105if isfield(Field,'Txt')
106    errormsg=Field.Txt;
107    return
108end
109if vardetect(1)==0
110     errormsg=[ 'requested field not available in ' FileName '/' VelType ': need to run patch'];
111     return
112end
113switch VelTypeOut
114    case {'civ1','filter1'}
115        if isfield(Field,'Patch1_SubDomain')
116            Field.SubDomain=Field.Patch1_SubDomain;
117            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
118        end
119        if isfield(Field,'Civ1_Dt')
120            Field.Dt=Field.Civ1_Dt;
121        end
122        if isfield(Field,'Civ1_Time')
123            Field.Time=Field.Civ1_Time;
124        end
125    case {'civ2','filter2'}
126        if isfield(Field,'Patch2_SubDomain')
127            Field.SubDomain=Field.Patch2_SubDomain;
128            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
129        end
130        if isfield(Field,'Civ2_Dt')
131        Field.Dt=Field.Civ2_Dt;
132        end
133        if isfield(Field,'Civ2_Time')
134        Field.Time=Field.Civ2_Time;
135        end
136end
137Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'Dt','Time'}];
138ivar_U_tps=[];
139ivar_V_tps=[];
140var_ind=find(vardetect);
141for ivar=1:numel(var_ind)
142    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
143    %Field.VarAttribute{ivar}.Mesh=0.025;%typical mesh for histograms O.025 pixel (used in series)
144    Field.VarAttribute{ivar}.ProjModeRequest=ProjModeRequest;
145    if strcmp(role{var_ind(ivar)},'vector_x')
146        Field.VarAttribute{ivar}.FieldName=FieldNames;
147        ivar_U=ivar;
148    end
149    if strcmp(role{var_ind(ivar)},'vector_x_tps')
150        Field.VarAttribute{ivar}.FieldName=FieldNames;
151        ivar_U_tps=ivar;
152    end
153    if strcmp(role{var_ind(ivar)},'vector_y')
154        Field.VarAttribute{ivar}.FieldName=FieldNames;
155        ivar_V=ivar;
156    end
157    if strcmp(role{var_ind(ivar)},'vector_y_tps')
158        Field.VarAttribute{ivar}.FieldName=FieldNames;
159        ivar_V_tps=ivar;
160    end
161end
162if ~isempty(ivar_U_tps)
163    Field.VarAttribute{ivar_U}.VarIndex_tps=ivar_U_tps;
164end
165if ~isempty(ivar_V_tps)
166    Field.VarAttribute{ivar_V}.VarIndex_tps=ivar_V_tps;
167end
168
169%% update list of global attributes
170Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'NbCoord','NbDim','TimeUnit','CoordUnit'}];
171Field.NbCoord=2;
172Field.NbDim=2;
173Field.TimeUnit='s';
174Field.CoordUnit='pixel';
175
176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177% [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
178%INPUT:
179% FieldNames =cell of field names to get, which can contain the strings:
180%             'ima_cor': image correlation, vec_c or vec2_C
181%             'vort','div','strain': requires velocity derivatives DUDX...
182%             'error': error estimate (vec_E or vec2_E)
183%             
184% vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)
185%            if vel_type=[] or'*', a  priority choice is done, civ2 considered better than civ1 )
186
187function [var,role,vel_type_out,errormsg]=varcivx_generator(ProjModeRequest,vel_type,CivStage)
188
189%% default input values
190if ~exist('vel_type','var'),vel_type='';end
191if iscell(vel_type),vel_type=vel_type{1}; end%transform cell to string if needed
192errormsg='';
193
194%% select the priority order for automatic vel_type selection
195if strcmp(vel_type,'civ2') && strcmp(ProjModeRequest,'derivatives')
196    vel_type='filter2';
197elseif strcmp(vel_type,'civ1') && strcmp(ProjModeRequest,'derivatives')
198    vel_type='filter1';
199end
200if isempty(vel_type)||strcmp(vel_type,'*')
201    vel_type='filter2';% case CivStage >=6
202    switch CivStage
203        case {4,5}% civ2 available but not filter2
204            if strcmp(ProjModeRequest,'derivatives')% derivatives needed
205                vel_type='filter1';
206            else
207                vel_type='civ2';
208            end
209        case 3
210            vel_type='filter1';
211        case {1,2}% civ1 available but not filter1
212            vel_type='civ1';
213    end
214end
215
216var={};
217switch vel_type
218    case{'civ1','civ2','civ3'}
219        varout={'X','Y','Z','U','V','W','C','F','FF'};
220        varin= {'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U','Civ1_V','Civ1_W','Civ1_C','Civ1_F','Civ1_FF'};
221         role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag'}; 
222    case{'filter1','filter2','filter3'} 
223        varout={'X','Y','Z','U','V','W','C','F','FF','Coord_tps','U_tps','V_tps','W_tps','SubRange','NbCentre','NbCentre','NbCentre'};
224        varin={'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U_smooth','Civ1_V_smooth','Civ1_W','Civ1_C','Civ1_F','Civ1_FF',...
225            'Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps','Civ1_W_tps','Civ1_SubRange','Civ1_NbCentre','Civ1_NbCentres','Civ1_NbSites'};
226        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag','coord_tps','vector_x_tps',...
227            'vector_y_tps','vector_z_tps','ancillary','ancillary','ancillary','ancillary'};
228          %  rmq: NbCentres and NbSites obsolete replaced by NbCentre, kept for consistency with previous data
229end
230switch vel_type
231    case {'civ2','filter2'}
232        varin=regexprep(varin,'1','2');
233    case {'civ3','filter3'}
234        varin=regexprep(varin,'1','3');
235end
236var=[varout;varin];
237if ~strcmp(ProjModeRequest,'interp_tps')
238    var=var(:,1:9);%suppress tps if not needed
239end
240vel_type_out=vel_type;
241
242
243
244
245
Note: See TracBrowser for help on using the repository browser.