Changeset 19096


Ignore:
Timestamp:
02/11/15 09:08:20 (10 years ago)
Author:
bdef
Message:

NEW; matlab->netcdf->matlab ability is OK

Location:
issm/trunk-jpl/src/m/contrib/netCDF
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/contrib/netCDF/export_netCDF.m

    r19080 r19096  
    2929 
    3030        %define netcdf dimensions
    31         DimSize(1).index=netcdf.defDim(ncid,'EltNum',md.mesh.numberofelements);
    32         DimSize(2).index=netcdf.defDim(ncid,'VertNum',md.mesh.numberofvertices);
    33         DimSize(3).index=netcdf.defDim(ncid,'VertperElt',size(md.mesh.elements,2));
    34         DimSize(4).index=netcdf.defDim(ncid,'Time',StepNum);
    35         DimSize(5).index=netcdf.defDim(ncid,'StringLength',20);
    36         DimSize(6).index=netcdf.defDim(ncid,'StructLength',2);
     31        DimSize(1).index=netcdf.defDim(ncid,'Dimension1',md.mesh.numberofelements);
     32        DimSize(2).index=netcdf.defDim(ncid,'Dimension2',md.mesh.numberofvertices);
     33        DimSize(3).index=netcdf.defDim(ncid,'Dimension3',size(md.mesh.elements,2));
     34        DimSize(4).index=netcdf.defDim(ncid,'Dimension4',StepNum);
     35        DimSize(5).index=netcdf.defDim(ncid,'Dimension5',40);
     36        DimSize(6).index=netcdf.defDim(ncid,'Dimension6',2);
    3737       
    3838        for i=1:length(DimSize),
     
    5353                %Special treatment for the results
    5454                if strcmp(groups(i),'results'),
    55                         for j=1:length(groupfields)%looping on the different solutions
    56                                                                                                                                  %disp(sprintf('=====Field name in tree %s ',groupfields{j}));
     55                        for j=1:length(groupfields)%looping on the differents solutions
     56                                netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype','results');
     57                                subgroupID=netcdf.defGrp(groupID,groupfields{j});
     58                                netcdf.putAtt(subgroupID,netcdf.getConstant('NC_GLOBAL'),'classtype',groupfields{j});
     59                                %disp(sprintf('=====Field name in tree %s ',groupfields{j}));
    5760                                if length(md.results.(groupfields{j}))>1,
    5861                                        %the solution have several timestep get last timesteps and output frequency
     
    6164                                        subfields=fields(md.results.(groupfields{j})(1));
    6265                                        for k=1:length(subfields),
    63                                                 netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype','results');
    6466                                                if ~strcmp(subfields(k),'errlog') && ~strcmp(subfields(k),'outlog') && ~strcmp(subfields(k),'SolutionType'),
    6567                                                        %disp(sprintf('==========SubField name in tree %s ',subfields{k}));
    6668                                                        Var=md.results.(groupfields{j})(1).(subfields{k});
    67                                                         [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,subfields{k},DimSize,DimValue,true,last_step,md,groupfields{j});
     69                                                        [DimSize,DimValue]=DefCreateVar(ncid,Var,subgroupID,subfields{k},DimSize,DimValue,true,last_step,md,groupfields{j});
    6870                              end
    6971                      end
     
    7375                                        for k=1:length(subfields),
    7476                                                %disp(sprintf('==========SubField name in tree %s ',subfields{k}));
    75                                                 netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype','results');
    7677                                                if ~strcmp(subfields(k),'errlog') && ~strcmp(subfields(k),'outlog') && ~strcmp(subfields(k),'SolutionType'),
    7778                                                        Var=md.results.(groupfields{1})(1).(subfields{k});
     
    8586                else
    8687                        for j=1:length(groupfields),
    87                                 %disp(sprintf('=====Field name in tree %s ',groupfields{j}));
     88                                disp(sprintf('=====Field name in tree %s ',groupfields{j}));
    8889                                netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype',class(md.(groups{i})));
    8990                                Var=md.(groups{i}).(groupfields{j});
     
    129130                %dirty hack to be able to pass strings
    130131                varid = netcdf.defVar(groupID,field,'NC_CHAR',dims);
    131                 for i=1:length(Var),
    132                         startpoint=zeros(size(Var));
    133                         startpoint(:,i)=i-1;
    134                         if length(Var)>1,
    135                                 endpoint=[min(length(Var{i}),20) 1];
    136                         else
    137                                 endpoint=min(length(Var{i}),20);
    138             end
    139                         if length(Var{i})>20,
    140                                 netcdf.putVar(groupID,varid,startpoint,endpoint,Var{i}(1:20))
    141                         else
    142                                 netcdf.putVar(groupID,varid,startpoint,endpoint,Var{i})
     132                if length(Var)==0,
     133                        netcdf.putVar(groupID,varid,0,9,'emptycell')
     134                else
     135                        for i=1:length(Var),
     136                                startpoint=zeros(size(Var));
     137                                startpoint(:,i)=i-1;
     138                                if length(Var)>1,
     139                                        endpoint=[min(length(Var{i}),40) 1];
     140                                else
     141                                        endpoint=min(length(Var{i}),40);
     142                    end
     143                                if length(Var{i})>40,
     144                                        netcdf.putVar(groupID,varid,startpoint,endpoint,Var{i}(1:40))
     145                                        disp(sprintf('some variable have been truncated'));
     146                          else
     147                                        netcdf.putVar(groupID,varid,startpoint,endpoint,Var{i})
     148              end
    143149            end
    144150          end
     
    148154                [dims,DimSize,DimValue]=GetDims(ncid,Var,groupID,locfields,DimSize,DimValue,istime);
    149155                varid = netcdf.defVar(groupID,field,'NC_CHAR',dims);
    150                 for i=1:length(locfields),
    151                         for j=1:2,
    152                                 if j==1,
    153                                         startpoint=[0,0,i-1];
    154                                         CharVar=locfields{i};
    155                                 else
    156                                         startpoint=[0,1,i-1];
    157                                         if isa(Var.(locfields{i}),'char'),
    158                                                 CharVar=Var.(locfields{i});
     156                if length(locfields)==0,
     157                        netcdf.putVar(groupID,varid,[0,0],[11,1],'emptystruct')
     158                else
     159                        for i=1:length(locfields),
     160                                for j=1:2,
     161                                        if j==1,
     162                                                startpoint=[0,0,i-1];
     163                                                CharVar=locfields{i};
    159164                                        else
    160                                                 CharVar=num2str(Var.(locfields{i}));
    161                       end
    162                     end
    163                                 endpoint=[min(length(CharVar),20),1,1];
    164                                 if length(CharVar)>20,
    165                                         netcdf.putVar(groupID,varid,startpoint,endpoint,CharVar(1:20))
    166                                 else
    167                                         netcdf.putVar(groupID,varid,startpoint,endpoint,CharVar)
    168                     end
    169             end
    170           end
     165                                                startpoint=[0,1,i-1];
     166                                                if isa(Var.(locfields{i}),'char'),
     167                                                        CharVar=Var.(locfields{i});
     168                                                else
     169                                                        CharVar=num2str(Var.(locfields{i}));
     170                              end
     171                      end
     172                                        endpoint=[min(length(CharVar),40),1,1];
     173                                        if length(CharVar)>40,
     174                                                netcdf.putVar(groupID,varid,startpoint,endpoint,CharVar(1:40))
     175                                                disp(sprintf('some variable have been truncated'));
     176                                        else
     177                                                netcdf.putVar(groupID,varid,startpoint,endpoint,CharVar)
     178                      end
     179                    end
     180            end
     181                end
    171182        else
    172183                disp(sprintf('no support for class %s of field %s',varclass,field));
     
    185196                        dimexist=DimValue==currentdim;
    186197                        if sum(dimexist)==0, %dimension is new to us, need to create it
    187                                 dimname=strcat(field{1},int2str(i));
     198                                dimname=strcat('Dimension',int2str(length(DimValue)+1));
    188199                                dimindex=length(DimSize)+1;
    189200                                DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim);
     
    212223                        dimexist=DimValue==currentdim;
    213224                        if sum(dimexist)==0, %dimension is new to us, need to create it
    214                                 dimname=strcat(field,int2str(i));
     225                                dimname=strcat('Dimension',int2str(length(DimValue)+1));
    215226                                dimindex=length(DimSize)+1;
    216227                                DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim);
     
    232243        %if we have a cell variable we need to add a stringlength dimension
    233244        if isa(Var,'cell') || isa(Var,'struct'),
    234                 dims=[DimSize(5).index dims];
     245                dims=[DimSize(5).index dims]
    235246  end
    236247end
  • issm/trunk-jpl/src/m/contrib/netCDF/export_netCDF.py

    r19080 r19096  
    2020                                except KeyError: #if not create it
    2121                                        if (shape[dim])>1:
    22                                                 NewDim=NCData.createDimension(str(field)+str(dim),(shape[dim]))
    23                                                 DimDict[len(NewDim)]=str(field)+str(dim)
     22                                                index=len(DimDict)+1
     23                                                NewDim=NCData.createDimension('Dimension'+str(index),(shape[dim]))
     24                                                DimDict[len(NewDim)]='Dimension'+str(index)
    2425                                                output=output+[str(DimDict[shape[dim]])]
    2526                                                print 'Defining dimension ' +str(shape[dim]) +' for '+str(field)+str(dim)
     
    2829                                        output=[str(DimDict[numpy.shape(shape)[0]])]+['DictDim']
    2930                                except KeyError:
    30                                         NewDim=NCData.createDimension(str(field),numpy.shape(shape)[0])
    31                                         DimDict[len(NewDim)]=str(field)
    32                                         output=[str(DimDict[numpy.shape(dict.keys(var))[0]])]+['DictDim']
     31                                        index=len(DimDict)+1
     32                                        NewDim=NCData.createDimension('Dimension'+str(index),numpy.shape(shape)[0])
     33                                        DimDict[len(NewDim)]='Dimension'+str(index)
     34                                        output=[str(DimDict[numpy.shape(dict.keys(var))[0]])]+['Dimension5']
    3335                                        print 'Defining dimension ' +str(numpy.shape(shape)[0]) +' for '+str(field)
    3436                                break
    3537                if istime:
    36                         output=output+['Time']
     38                        output=output+['Dimension4']
    3739                return tuple(output)
    3840        #============================================================================
    3941
    4042  #Define the variables
    41         def CreateVar(var,istime,*step_args):
     43        def CreateVar(var,Group,istime,*step_args):
    4244                #grab type
    4345                try:
     
    5658                #treating scalar string or bool as atribute
    5759                if val_type==str or val_type==bool:
    58                         NCgroup.__setattr__(str(field), str(var))
     60                        Group.__setattr__(str(field), str(var))
    5961
    6062                #treating list as string table
    6163                elif val_type==list:
    62                         ncvar = NCgroup.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
     64                        ncvar = Group.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
    6365                        for elt in range(0,val_dim):
    6466                                try:
     
    6870                #treating bool tables as string tables
    6971                elif val_type=='bool':
    70                         ncvar = NCgroup.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
     72                        ncvar = Group.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
    7173                        for elt in range(0,val_shape[0]):
    7274                                ncvar[elt] = str(var[elt])
    7375                #treating dictionaries as string tables of dim 2
    7476                elif val_type==collections.OrderedDict:
    75                         ncvar = NCgroup.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
     77                        ncvar = Group.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
    7678                        for elt in range(0,val_dim):
    7779                                ncvar[elt,0]=dict.keys(var)[elt]
     
    7981                #Now dealing with numeric variables
    8082                else:
    81                         ncvar = NCgroup.createVariable(str(field),TypeDict[val_type],GetDim(var,val_shape,val_dim,istime),zlib=True)
     83                        ncvar = Group.createVariable(str(field),TypeDict[val_type],GetDim(var,val_shape,val_dim,istime),zlib=True)
    8284                       
    8385                        if istime:
     
    127129                StepNum=1
    128130               
    129         EltNum=NCData.createDimension('EltNum',md.mesh.numberofelements)
    130         VertNum=NCData.createDimension('VertNum',md.mesh.numberofvertices)
    131         VertperElt=NCData.createDimension('VertperElt',numpy.shape(md.mesh.elements)[1])
    132         Time=NCData.createDimension('Time',StepNum)
    133         DictDim=NCData.createDimension('DictDim',2)
     131        Dimension1=NCData.createDimension('Dimension1',md.mesh.numberofelements)
     132        Dimension2=NCData.createDimension('Dimension2',md.mesh.numberofvertices)
     133        Dimension3=NCData.createDimension('Dimension3',numpy.shape(md.mesh.elements)[1])
     134        Dimension4=NCData.createDimension('Dimension4',StepNum)
     135        Dimension5=NCData.createDimension('Dimension5',2)
    134136
    135         DimDict = {len(EltNum):'EltNum',
    136                                                  len(VertNum):'VertNum',
    137                                                  len(VertperElt):'VertperElt',
    138                                                  len(Time):'Time',
    139                                                  len(DictDim):'DictDim'}
     137        DimDict = {len(Dimension1):'Dimension1',
     138                                                 len(Dimension2):'Dimension2',
     139                                                 len(Dimension3):'Dimension3',
     140                                                 len(Dimension4):'Dimension4',
     141                                                 len(Dimension5):'Dimension5'}
    140142
    141143        TypeDict = {float:'f8',
     
    154156                if str(group)=='results':
    155157                        for supfield in fields:#looping on the different solutions
     158                                NCgroup.__setattr__('classtype', "results")
     159                                Subgroup=NCgroup.createGroup(str(supfield))
     160                                Subgroup.__setattr__('classtype',str(supfield))
    156161                                if type(md.results.__dict__[supfield])==list:#the solution have several timestep
    157162                                        #get last timesteps and output frequency
     
    161166                                        subfields=dict.keys(md.results.__dict__[supfield].__getitem__(0).__dict__)
    162167                                        for field in subfields:
    163                                                 NCgroup.__setattr__('classtype', "results")
    164168                                                if str(field)!='errlog' and str(field)!='outlog' and str(field)!='SolutionType':
    165169                                                        Var=md.results.__dict__[supfield].__getitem__(0).__dict__[field]
    166                                                         CreateVar(Var,True,last_step,step_freq)
     170                                                        CreateVar(Var,Subgroup,True,last_step,step_freq)
    167171                                       
    168172                                elif type(md.results.__dict__[supfield])==results:#only one timestep
    169173                                        subfields=dict.keys(md.results.__dict__[supfield].__dict__)
    170174                                        for field in subfields:
    171                                                 NCgroup.__setattr__('classtype', "results")
    172175                                                if str(field)!='errlog' and str(field)!='outlog' and str(field)!='SolutionType':
    173176#                                                       print 'Treating '+str(group)+'.'+str(supfield)+'.'+str(field)
    174177                                                        Var=md.results.__dict__[supfield].__dict__[field]
    175                                                         CreateVar(Var,False)
     178                                                        CreateVar(Var,Subgroup,False)
    176179                                else:
    177180                                        print 'Result format not suported'
     
    182185                                NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__)
    183186                                Var=md.__dict__[group].__dict__[field]
    184                                 CreateVar(Var,False)
     187                                CreateVar(Var,NCgroup,False)
    185188        NCData.close()
    186189
Note: See TracChangeset for help on using the changeset viewer.