Changeset 21647
- Timestamp:
- 03/30/17 06:36:17 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.m
r21645 r21647 52 52 %In each group gather the fields of the class 53 53 groupfields=fields(md.(groups{i})); 54 %Special treatment for the results 55 if strcmp(groups(i),'results'), 56 for j=1:length(groupfields)%looping on the differents solutions 57 netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype','results'); 58 subgroupID=netcdf.defGrp(groupID,groupfields{j}); 59 netcdf.putAtt(subgroupID,netcdf.getConstant('NC_GLOBAL'),'classtype',groupfields{j}); 60 %disp(sprintf('=====Field name in tree %s ',groupfields{j})); 61 if length(md.results.(groupfields{j}))>1, 62 %the solution have several timestep get last timesteps and output frequency 63 last_step = length(md.results.(groupfields{j})); 64 %grab first time step 65 subfields=fields(md.results.(groupfields{j})(1)); 66 for k=1:length(subfields), 67 if ~strcmp(subfields(k),'errlog') && ~strcmp(subfields(k),'outlog') && ~strcmp(subfields(k),'SolutionType'), 68 %disp(sprintf('==========SubField name in tree %s ',subfields{k})); 69 Var=md.results.(groupfields{j})(1).(subfields{k}); 70 [DimSize,DimValue]=DefCreateVar(ncid,Var,subgroupID,subfields{k},DimSize,DimValue,true,last_step,md,groupfields{j}); 71 end 72 end 73 elseif length(md.results.(groupfields{j}))==1, 74 %only one timestep 75 subfields=fields(md.results.(groupfields{j})); 76 for k=1:length(subfields), 77 %disp(sprintf('==========SubField name in tree %s ',subfields{k})); 78 if ~strcmp(subfields(k),'errlog') && ~strcmp(subfields(k),'outlog') && ~strcmp(subfields(k),'SolutionType'), 79 Var=md.results.(groupfields{1})(1).(subfields{k}); 80 [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,subfields{k},DimSize,DimValue,false); 81 end 82 end 83 else 84 print 'Result format not suported' 85 end 86 end 87 else 88 for j=1:length(groupfields), 89 Var=md.(groups{i}).(groupfields{j}); 90 if isa(Var,'cell') 91 Stdlist=false; 92 if length(Var) == 0 93 Stdlist=true; 94 else 95 for k=1:length(typelist) 54 for j=1:length(groupfields), 55 Var=md.(groups{i}).(groupfields{j}); 56 if isa(Var,'cell') 57 Stdlist=false; 58 if length(Var) == 0 59 Stdlist=true; 60 else 61 for k=1:length(typelist) 96 62 if isa(Var{1},typelist{k}) 97 63 Stdlist=true; … … 103 69 if(Stdlist) 104 70 disp(sprintf('=====Field name in tree %s ',groupfields{j})); 105 [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,groupfields{j},DimSize,DimValue ,false);71 [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,groupfields{j},DimSize,DimValue); 106 72 else 107 73 listsize=length(Var); … … 121 87 for m=1:length(subfields) 122 88 if ~strcmp(subfields{m},'outlog') 123 [DimSize,DimValue]=DefCreateVar(ncid,Var{l}.(subfields{m}),listgroupID,subfields{m},DimSize,DimValue ,false);89 [DimSize,DimValue]=DefCreateVar(ncid,Var{l}.(subfields{m}),listgroupID,subfields{m},DimSize,DimValue); 124 90 end 125 91 end … … 128 94 else 129 95 netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype',class(md.(groups{i}))); 130 [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,groupfields{j},DimSize,DimValue,false); 131 end 132 end 133 end 96 [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,groupfields{j},DimSize,DimValue); 97 end 98 end 134 99 end 135 100 netcdf.close(ncid); 136 101 end 137 102 138 function [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,field,DimSize,DimValue, istime,last,md,midfield)103 function [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,field,DimSize,DimValue,last,md,midfield) 139 104 varclass=class(Var); 140 105 varsize=size(Var); … … 150 115 netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),field,Var); 151 116 elseif isa(Var,'double'), %dealing with arrays 152 [dims,DimSize,DimValue]=GetDims(ncid,Var, groupID,field,DimSize,DimValue,istime);117 [dims,DimSize,DimValue]=GetDims(ncid,Var,DimSize,DimValue); 153 118 varid = netcdf.defVar(groupID,field,'NC_DOUBLE',dims); 154 if istime, 155 VarTab=Var; 156 for i=2:last, 157 TimeVar=md.results.(midfield)(i).(field); 158 VarTab=[VarTab TimeVar]; 159 end 160 netcdf.putVar(groupID,varid,VarTab); 161 else 162 if length(Var)==0, 163 netcdf.putVar(groupID,varid,NaN); 164 else 165 netcdf.putVar(groupID,varid,Var); 166 end 167 end 119 if length(Var)==0, 120 netcdf.putVar(groupID,varid,NaN); 121 else 122 netcdf.putVar(groupID,varid,Var); 123 end 168 124 elseif isa(Var,'cell'), 169 [dims,DimSize,DimValue]=GetDims(ncid,Var, groupID,field,DimSize,DimValue,istime);125 [dims,DimSize,DimValue]=GetDims(ncid,Var,DimSize,DimValue); 170 126 %dirty hack to be able to pass strings 171 127 varid = netcdf.defVar(groupID,field,'NC_CHAR',dims); … … 175 131 for i=1:length(Var), 176 132 if length(Var)>1, 177 endpoint=[ min(length(Var{i}),40) 1];178 startpoint=[ 0 1];133 endpoint=[1,min(length(Var{i}),40)]; 134 startpoint=[1 0]; 179 135 else 180 136 endpoint=min(length(Var{i}),40); … … 182 138 end 183 139 if length(Var{i})>40, 184 netcdf.putVar(groupID,varid,startpoint,e ndpoint,Var{i}(1:40))140 netcdf.putVar(groupID,varid,startpoint,extent,Var{i}(1:40)) 185 141 disp(sprintf('some variable have been truncated')); 186 142 else … … 192 148 %Start by getting the structure fields and size 193 149 locfields=fields(Var); 194 [dims,DimSize,DimValue]=GetDims(ncid,Var, groupID,locfields,DimSize,DimValue,istime);150 [dims,DimSize,DimValue]=GetDims(ncid,Var,DimSize,DimValue); 195 151 varid = netcdf.defVar(groupID,field,'NC_CHAR',dims); 196 152 if length(locfields)==0, 197 netcdf.putVar(groupID,varid,[0,0],[1 1,1],'emptystruct')153 netcdf.putVar(groupID,varid,[0,0],[1,11],'emptystruct') 198 154 else 199 155 for i=1:length(locfields), 200 156 for j=1:2, 201 157 if j==1, 202 startpoint=[0,0,i-1];203 158 CharVar=locfields{i}; 159 if length(CharVar)==0 160 CharVar='emptystruct'; 161 end 162 startpoint=[i-1,0,0]; 204 163 else 205 startpoint=[0,1,i-1];206 164 if isa(Var.(locfields{i}),'char'), 207 165 CharVar=Var.(locfields{i}); 208 166 else 209 167 CharVar=num2str(Var.(locfields{i})); 210 end 211 end 212 endpoint=[min(length(CharVar),40),1,1]; 168 end 169 if length(CharVar)==0 170 CharVar='emptystruct'; 171 end 172 startpoint=[i-1,1,0]; 173 end 174 175 extent=[1,1,min(length(CharVar),40)]; 213 176 if length(CharVar)>40, 214 netcdf.putVar(groupID,varid,startpoint,e ndpoint,CharVar(1:40))177 netcdf.putVar(groupID,varid,startpoint,extent,CharVar(1:40)) 215 178 disp(sprintf('some variable have been truncated')); 216 179 else 217 netcdf.putVar(groupID,varid,startpoint,e ndpoint,CharVar)218 219 220 180 netcdf.putVar(groupID,varid,startpoint,extent,CharVar) 181 end 182 end 183 end 221 184 end 222 185 else … … 226 189 end 227 190 228 function [dims,DimSize,DimValue]=GetDims(ncid,Var, groupID,field,DimSize,DimValue,istime)191 function [dims,DimSize,DimValue]=GetDims(ncid,Var,DimSize,DimValue) 229 192 dims=[]; 230 %specific treatment for structures 231 if isa(Var,'struct') 232 varsize=size(field); %we pass here the fields of the current structure 233 MatOrVec=varsize>1; %checking if we have a matrix (1 1) or vector (1 0) 234 for i=1:sum(MatOrVec), %loop on the number of (non 1) dimensions 235 currentdim=varsize(i); 236 dimexist=DimValue==currentdim; 237 if sum(dimexist)==0, %dimension is new to us, need to create it 238 dimname=strcat('Dimension',int2str(length(DimValue)+1)); 239 dimindex=length(DimSize)+1; 240 DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim); 241 [DimSize(dimindex).name,DimSize(dimindex).value]=netcdf.inqDim(ncid,DimSize(dimindex).index); 242 dims(i)=DimSize(dimindex).index; 243 DimValue(dimindex)=currentdim; 193 if isa(Var,'cell'), 194 varsize=size(Var'); 195 elseif isa(Var,'struct') 196 varsize=length(fields(Var)); 197 else 198 varsize=size(Var); 199 end 200 dim=sum(varsize>1); 201 if dim>0 202 for i=1:dim 203 indsize=find(varsize(i)==DimValue); 204 if length(indsize)>0 205 dims=[dims DimSize(indsize).index]; 244 206 else 245 dimindex=find(dimexist); 246 if DimSize(dimindex).value~=currentdim, 247 error('Indexation problem with the dimension structure') 248 end 249 end 250 dims(i)=DimSize(dimindex).index; 251 end 252 dims=[DimSize(6).index dims]; 253 else 254 %with a cell array need to grab the transposed size to work 255 if isa(Var,'cell'), 256 varsize=size(Var'); 257 else 258 varsize=size(Var); 259 end 260 MatOrVec=varsize>1; %checking if we have a matrix (1 1) or vector (1 0) 261 for i=1:sum(MatOrVec), %loop on the number of (non 1) dimensions 262 currentdim=varsize(i); 263 dimexist=DimValue==currentdim; 264 if sum(dimexist)==0, %dimension is new to us, need to create it 265 dimname=strcat('Dimension',int2str(length(DimValue)+1)); 266 dimindex=length(DimSize)+1; 267 DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim); 268 [DimSize(dimindex).name,DimSize(dimindex).value]=netcdf.inqDim(ncid,DimSize(dimindex).index); 269 dims(i)=DimSize(dimindex).index; 270 DimValue(dimindex)=currentdim; 207 indsize=length(DimSize)+1; 208 DimSize(indsize).index=netcdf.defDim(ncid,['Dimension' num2str(indsize)],varsize(i)); 209 [DimSize(indsize).name,DimSize(indsize).value]=netcdf.inqDim(ncid,DimSize(indsize).index); 210 DimValue(indsize)=DimSize(indsize).value; 211 dims=[dims DimSize(indsize).index]; 212 end 213 end 214 end 215 %if we have a cell variable we need to add a stringlength dimension 216 if isa(Var,'struct'), 217 if DimValue(3)~=2 218 if DimValue(2)~=2 219 dims=[dims DimSize(1).index]; 271 220 else 272 dimindex=find(dimexist); 273 DimValue 274 if DimSize(dimindex).value~=currentdim, 275 error('Indexation problem with the dimension structure') 276 end 277 end 278 dims(i)=DimSize(dimindex).index; 279 end 280 end 281 if istime, 282 dims=[dims DimSize(1).index];%adding the time dimension if necessary 283 end 284 %if we have a cell variable we need to add a stringlength dimension 221 dims=[dims DimSize(2).index]; 222 end 223 else 224 dims=[dims DimSize(3).index]; 225 end 226 end 285 227 if isa(Var,'cell') || isa(Var,'struct'), 286 228 if DimValue(2)~=40 287 dims=[ DimSize(1).index dims];288 else 289 dims=[ DimSize(2).index dims];290 end 291 229 dims=[dims DimSize(1).index]; 230 else 231 dims=[dims DimSize(2).index]; 232 end 233 end 292 234 end
Note:
See TracChangeset
for help on using the changeset viewer.