source: trunk/src/read_civdata.m @ 1177

Last change on this file since 1177 was 1169, checked in by sommeria, 3 months ago

civ3 introduced

File size: 9.9 KB
RevLine 
[237]1%'read_civdata': reads new civ data from netcdf files
2%------------------------------------------------------------------
3%
[613]4% function [Field,VelTypeOut]=read_civdata(FileName,FieldNames,VelType)
[237]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)
[515]12%                    .Dt: time interval for the corresponding image pair
[237]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:
[613]32% FileName: file name (string).
[237]33% FieldNames =cell of field names to get, which can contain the strings:
[1135]34% 'U','V','norm(U,V)','curl(U,V)','div(U,V)','strain(U,V)'
[237]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)
[809]38%
[237]39% FUNCTIONS called:
40% 'varcivx_generator':, sets the names of vaiables to read in the netcdf file
41% 'nc2struct': reads a netcdf file
42
[809]43%=======================================================================
[1126]44% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]45%   http://www.legi.grenoble-inp.fr
[1127]46%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
[809]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
[1162]61function [Field,VelTypeOut,errormsg]=read_civdata(FileName,FieldNames,VelType,frame_index)
[406]62
[237]63%% default input
[1162]64if ~exist('frame_index','var')
65    frame_index=1;
66end
[237]67if ~exist('VelType','var')
[246]68    VelType='';
[237]69end
[1135]70if isempty(VelType)||strcmp(VelType,'*')
[246]71    VelType='';
[237]72end
73if ~exist('FieldNames','var')
[1135]74    FieldNames={}; %default
[237]75end
[613]76Field=[];
77VelTypeOut=VelType;
[406]78errormsg='';
[1162]79if ischar(FieldNames), FieldNames={FieldNames}; end
[581]80ProjModeRequest='';
[515]81for ilist=1:length(FieldNames)
82    if ~isempty(FieldNames{ilist})
83        switch FieldNames{ilist}
[987]84            case {'U','V','norm(U,V)'}
[654]85                if ~strcmp(FieldNames{1},'vec(U,V)')% if the scalar is not used as color of vectors
[581]86                ProjModeRequest='interp_lin';
[654]87                end
[515]88            case {'curl(U,V)','div(U,V)','strain(U,V)'}
[581]89                ProjModeRequest='interp_tps';
[515]90        end
91    end
92end
[237]93
94%% reading data
[1169]95[Data,tild,tild,errormsg]=nc2struct(FileName,'ListGlobalAttribute','Conventions','CivStage');% read the global attributes to get Data.CivStage
[1050]96if ~isempty(errormsg)
97     errormsg=['read_civdata: ' errormsg];
[612]98    return
99end
[822]100% set the list of variables to read and their role
[581]101[varlist,role,VelTypeOut]=varcivx_generator(ProjModeRequest,VelType,Data.CivStage);
[406]102if isempty(varlist)
[1050]103    errormsg=['read_civdata: unknow velocity type ' VelType];
[406]104    return
105else
[1162]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   
[613]109    [Field,vardetect]=nc2struct(FileName,varlist);%read the variables in the netcdf file
[1162]110    end
[406]111end
[237]112if isfield(Field,'Txt')
113    errormsg=Field.Txt;
114    return
115end
116if vardetect(1)==0
[613]117     errormsg=[ 'requested field not available in ' FileName '/' VelType ': need to run patch'];
[237]118     return
119end
[380]120switch VelTypeOut
[987]121    case {'civ1','filter1'}
[494]122        if isfield(Field,'Patch1_SubDomain')
123            Field.SubDomain=Field.Patch1_SubDomain;
124            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
[822]125        end
[594]126        if isfield(Field,'Civ1_Dt')
[822]127            Field.Dt=Field.Civ1_Dt;
[594]128        end
129        if isfield(Field,'Civ1_Time')
[822]130            Field.Time=Field.Civ1_Time;
[594]131        end
[987]132    case {'civ2','filter2'}
[494]133        if isfield(Field,'Patch2_SubDomain')
134            Field.SubDomain=Field.Patch2_SubDomain;
135            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
136        end
[594]137        if isfield(Field,'Civ2_Dt')
[380]138        Field.Dt=Field.Civ2_Dt;
[594]139        end
140        if isfield(Field,'Civ2_Time')
[494]141        Field.Time=Field.Civ2_Time;
[594]142        end
[372]143end
[494]144Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'Dt','Time'}];
[515]145ivar_U_tps=[];
[581]146ivar_V_tps=[];
[237]147var_ind=find(vardetect);
[389]148for ivar=1:numel(var_ind)
[237]149    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
[871]150    %Field.VarAttribute{ivar}.Mesh=0.025;%typical mesh for histograms O.025 pixel (used in series)
[674]151    Field.VarAttribute{ivar}.ProjModeRequest=ProjModeRequest;
[515]152    if strcmp(role{var_ind(ivar)},'vector_x')
[576]153        Field.VarAttribute{ivar}.FieldName=FieldNames;
[515]154        ivar_U=ivar;
155    end
156    if strcmp(role{var_ind(ivar)},'vector_x_tps')
[576]157        Field.VarAttribute{ivar}.FieldName=FieldNames;
[515]158        ivar_U_tps=ivar;
159    end
[581]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
[237]168end
[515]169if ~isempty(ivar_U_tps)
170    Field.VarAttribute{ivar_U}.VarIndex_tps=ivar_U_tps;
171end
[581]172if ~isempty(ivar_V_tps)
173    Field.VarAttribute{ivar_V}.VarIndex_tps=ivar_V_tps;
174end
[515]175
[581]176%% update list of global attributes
[237]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'...)
[380]192%            if vel_type=[] or'*', a  priority choice is done, civ2 considered better than civ1 )
[237]193
[581]194function [var,role,vel_type_out,errormsg]=varcivx_generator(ProjModeRequest,vel_type,CivStage)
[237]195
196%% default input values
[1135]197if ~exist('vel_type','var'),vel_type='';end
198if iscell(vel_type),vel_type=vel_type{1}; end%transform cell to string if needed
[406]199errormsg='';
[1153]200if CivStage>=6
201    CivStage=6;
202end
[237]203
204%% select the priority order for automatic vel_type selection
[1153]205if strcmp(ProjModeRequest,'derivatives')&& numel(vel_type)>=3 && strcmp(vel_type(1:3),'civ')
206    vel_type=['filter' vel_type(4:end)];
[492]207end
[406]208if isempty(vel_type)||strcmp(vel_type,'*')
[851]209    vel_type='filter2';% case CivStage >=6
[380]210    switch CivStage
[1153]211        case {1,2}% civ1 available but not filter1
212            vel_type='civ1';
213        case 3
214            vel_type='filter1';
215
[380]216        case {4,5}% civ2 available but not filter2
[581]217            if strcmp(ProjModeRequest,'derivatives')% derivatives needed
[492]218                vel_type='filter1';
[380]219            else
220                vel_type='civ2';
221            end
222    end
223end
[492]224
[406]225var={};
[246]226switch vel_type
[1161]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',...
[651]234            'Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps','Civ1_W_tps','Civ1_SubRange','Civ1_NbCentre','Civ1_NbCentres','Civ1_NbSites'};
[1161]235        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag','coord_tps','vector_x_tps',...
[651]236            'vector_y_tps','vector_z_tps','ancillary','ancillary','ancillary','ancillary'};
[1117]237          %  rmq: NbCentres and NbSites obsolete replaced by NbCentre, kept for consistency with previous data
[246]238end
[1117]239switch vel_type
240    case {'civ2','filter2'}
241        varin=regexprep(varin,'1','2');
[1161]242    % case {'civ3','filter3'}
243    %     varin=regexprep(varin,'1','3');
[1117]244end
245var=[varout;varin];
[581]246if ~strcmp(ProjModeRequest,'interp_tps')
[1161]247    var=var(:,1:8);%suppress tps if not needed
[515]248end
[380]249vel_type_out=vel_type;
[237]250
251
252
[246]253
254
Note: See TracBrowser for help on using the repository browser.