source: trunk/src/read_civdata.m @ 675

Last change on this file since 675 was 674, checked in by sommeria, 11 years ago

various bugs repaired, in particula timing

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