source: trunk/src/read_civdata.m @ 1195

Last change on this file since 1195 was 1195, checked in by sommeria, 5 days ago

format of PIV data made more compact/4

File size: 10.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
[1195]95[Data,~,~,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
[1195]100
[822]101% set the list of variables to read and their role
[1195]102CheckCompress=strcmp(Data.Conventions,'uvmat/civdata/compress');
103[varlist,role,VelTypeOut]=varcivx_generator(ProjModeRequest,VelType,Data.CivStage,CheckCompress);
[406]104if isempty(varlist)
[1050]105    errormsg=['read_civdata: unknow velocity type ' VelType];
[406]106    return
107else
[1162]108    if strcmp(Data.Conventions,'uvmat/civdata_3D')
[1195]109        [Field,vardetect,~,errormsg]=nc2struct(FileName,'TimeDimName','npz',frame_index,varlist);%read the variables in the netcdf file
110    else
111        [Field,vardetect,~,errormsg]=nc2struct(FileName,varlist);%read the variables in the netcdf file
[1162]112    end
[406]113end
[1195]114if ~isempty(errormsg)
115     errormsg=['read_civdata: ' errormsg];
[237]116    return
117end
118if vardetect(1)==0
[613]119     errormsg=[ 'requested field not available in ' FileName '/' VelType ': need to run patch'];
[237]120     return
121end
[1195]122if strcmp(Data.Conventions,'uvmat/civdata/compress')
123    Field.X=Field.X-0.5+Field.U/2;% shift to the convected position
124    Field.Y=Field.Y-0.5+Field.V/2;
125end
[380]126switch VelTypeOut
[987]127    case {'civ1','filter1'}
[494]128        if isfield(Field,'Patch1_SubDomain')
129            Field.SubDomain=Field.Patch1_SubDomain;
130            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
[822]131        end
[594]132        if isfield(Field,'Civ1_Dt')
[822]133            Field.Dt=Field.Civ1_Dt;
[594]134        end
135        if isfield(Field,'Civ1_Time')
[822]136            Field.Time=Field.Civ1_Time;
[594]137        end
[987]138    case {'civ2','filter2'}
[494]139        if isfield(Field,'Patch2_SubDomain')
140            Field.SubDomain=Field.Patch2_SubDomain;
141            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
142        end
[594]143        if isfield(Field,'Civ2_Dt')
[380]144        Field.Dt=Field.Civ2_Dt;
[594]145        end
146        if isfield(Field,'Civ2_Time')
[494]147        Field.Time=Field.Civ2_Time;
[594]148        end
[372]149end
[494]150Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'Dt','Time'}];
[515]151ivar_U_tps=[];
[581]152ivar_V_tps=[];
[237]153var_ind=find(vardetect);
[389]154for ivar=1:numel(var_ind)
[237]155    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
[871]156    %Field.VarAttribute{ivar}.Mesh=0.025;%typical mesh for histograms O.025 pixel (used in series)
[674]157    Field.VarAttribute{ivar}.ProjModeRequest=ProjModeRequest;
[515]158    if strcmp(role{var_ind(ivar)},'vector_x')
[576]159        Field.VarAttribute{ivar}.FieldName=FieldNames;
[515]160        ivar_U=ivar;
161    end
162    if strcmp(role{var_ind(ivar)},'vector_x_tps')
[576]163        Field.VarAttribute{ivar}.FieldName=FieldNames;
[515]164        ivar_U_tps=ivar;
165    end
[581]166    if strcmp(role{var_ind(ivar)},'vector_y')
167        Field.VarAttribute{ivar}.FieldName=FieldNames;
168        ivar_V=ivar;
169    end
170    if strcmp(role{var_ind(ivar)},'vector_y_tps')
171        Field.VarAttribute{ivar}.FieldName=FieldNames;
172        ivar_V_tps=ivar;
173    end
[237]174end
[515]175if ~isempty(ivar_U_tps)
176    Field.VarAttribute{ivar_U}.VarIndex_tps=ivar_U_tps;
177end
[581]178if ~isempty(ivar_V_tps)
179    Field.VarAttribute{ivar_V}.VarIndex_tps=ivar_V_tps;
180end
[515]181
[581]182%% update list of global attributes
[237]183Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'NbCoord','NbDim','TimeUnit','CoordUnit'}];
184Field.NbCoord=2;
185Field.NbDim=2;
186Field.TimeUnit='s';
187Field.CoordUnit='pixel';
188
189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190% [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
191%INPUT:
192% FieldNames =cell of field names to get, which can contain the strings:
193%             'ima_cor': image correlation, vec_c or vec2_C
194%             'vort','div','strain': requires velocity derivatives DUDX...
195%             'error': error estimate (vec_E or vec2_E)
196%             
197% vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)
[380]198%            if vel_type=[] or'*', a  priority choice is done, civ2 considered better than civ1 )
[237]199
[1195]200function [var,role,vel_type_out,errormsg]=varcivx_generator(ProjModeRequest,vel_type,CivStage,CheckCompress)
[237]201
202%% default input values
[1135]203if ~exist('vel_type','var'),vel_type='';end
204if iscell(vel_type),vel_type=vel_type{1}; end%transform cell to string if needed
[406]205errormsg='';
[1153]206if CivStage>=6
207    CivStage=6;
208end
[237]209
210%% select the priority order for automatic vel_type selection
[1153]211if strcmp(ProjModeRequest,'derivatives')&& numel(vel_type)>=3 && strcmp(vel_type(1:3),'civ')
212    vel_type=['filter' vel_type(4:end)];
[492]213end
[406]214if isempty(vel_type)||strcmp(vel_type,'*')
[851]215    vel_type='filter2';% case CivStage >=6
[380]216    switch CivStage
[1153]217        case {1,2}% civ1 available but not filter1
218            vel_type='civ1';
219        case 3
220            vel_type='filter1';
221
[380]222        case {4,5}% civ2 available but not filter2
[581]223            if strcmp(ProjModeRequest,'derivatives')% derivatives needed
[492]224                vel_type='filter1';
[380]225            else
226                vel_type='civ2';
227            end
228    end
229end
[492]230
[1195]231if CheckCompress
232    switch vel_type
233        case{'civ1','civ2'}
234            varout={'X','Y','Z','U','V','W','C','FF'};
235            varin= {'X','Y','Z','U','V','W','C','FF'};
236            role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag'};
237        case{'filter1','filter2'}
238            varout={'X','Y','Z','U','V','W','C','FF'};
239            varin={'X','Y','Z','U_smooth','V_smooth','W_smooth','C','FF'};
240            role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag'};
241            %  rmq: NbCentres and NbSites obsolete replaced by NbCentre, kept for consistency with previous data
242    end
243else
244    switch vel_type
245        case{'civ1','civ2'}
246            varout={'X','Y','Z','U','V','W','C','FF'};
247            varin= {'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U','Civ1_V','Civ1_W','Civ1_C','Civ1_FF'};
248            role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag'};
249        case{'filter1','filter2'}
250            varout={'X','Y','Z','U','V','W','C','FF','Coord_tps','U_tps','V_tps','W_tps','SubRange','NbCentre','NbCentre','NbCentre'};
251            varin={'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U_smooth','Civ1_V_smooth','Civ1_W','Civ1_C','Civ1_FF',...
252                'Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps','Civ1_W_tps','Civ1_SubRange','Civ1_NbCentre','Civ1_NbCentres','Civ1_NbSites'};
253            role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag','coord_tps','vector_x_tps',...
254                'vector_y_tps','vector_z_tps','ancillary','ancillary','ancillary','ancillary'};
255            %  rmq: NbCentres and NbSites obsolete replaced by NbCentre, kept for consistency with previous data
256    end
257    switch vel_type
258        case {'civ2','filter2'}
259            varin=regexprep(varin,'1','2');
260    end
[246]261end
[1117]262var=[varout;varin];
[581]263if ~strcmp(ProjModeRequest,'interp_tps')
[1161]264    var=var(:,1:8);%suppress tps if not needed
[515]265end
[380]266vel_type_out=vel_type;
[237]267
268
269
[246]270
271
Note: See TracBrowser for help on using the repository browser.