source: trunk/src/uvmat_doc/FUNCTIONS_DOC/nc2struct_toolbox.html @ 37

Last change on this file since 37 was 37, checked in by sommeria, 14 years ago

create_grid.fig ,
uvmat_doc and all the included files added

File size: 21.0 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2                "http://www.w3.org/TR/REC-html40/loose.dtd">
3<html>
4<head>
5  <title>Description of nc2struct_toolbox</title>
6  <meta name="keywords" content="nc2struct_toolbox">
7  <meta name="description" content="'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY">
8  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
9  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
10  <meta name="robots" content="index, follow">
11  <link type="text/css" rel="stylesheet" href="../m2html.css">
12</head>
13<body>
14<a name="_top"></a>
15<div><a href="../index.html">Home</a> &gt;  <a href="index.html">.</a> &gt; nc2struct_toolbox.m</div>
16
17<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
18<td align="right"><a href="index.html">Index for .&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->
19
20<h1>nc2struct_toolbox
21</h1>
22
23<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
24<div class="box"><strong>'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY</strong></div>
25
26<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
27<div class="box"><strong>function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName) </strong></div>
28
29<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
30<div class="fragment"><pre class="comment">'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY
31----------------------------------------------------------------------
32 function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName)
33
34 OUTPUT:
35  Data: structure containing all the information of the netcdf file (or netcdf object)
36           with fields:
37    .ListGlobalAttribute: cell listing the names of the global attributes
38        .Att_1,Att_2... : values of the global attributes
39            .ListDimName: cell listing the names of the array dimensions
40               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
41            .ListVarName: cell listing the names of the variables
42            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
43            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName
44           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
45        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
46  var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else.
47  ichoice: = line
48
49INPUT:
50     nc:      name of a netcdf file (char string) or netcdf object   
51 ListVarName: optional list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
52         if ListVarName=[] or{}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes)
53         if ListVarName is absent or ='*', ALL the variables are read.
54        if ListVarName is a cell array with n lines, the set of variables
55                        will be sought by order of priority in the list, while output names will be set by the first line
56 
57AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
58  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
59AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
60     This file is part of the toolbox UVMAT.
61 
62     UVMAT is free software; you can redistribute it and/or modify
63     it under the terms of the GNU General Public License as published by
64     the Free Software Foundation; either version 2 of the License, or
65     (at your option) any later version.
66 
67     UVMAT is distributed in the hope that it will be useful,
68     but WITHOUT ANY WARRANTY; without even the implied warranty of
69     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
70     GNU General Public License (file UVMAT/COPYING.txt) for more details.
71AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</pre></div>
72
73<!-- crossreference -->
74<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
75This function calls:
76<ul style="list-style-image:url(../matlabicon.gif)">
77</ul>
78This function is called by:
79<ul style="list-style-image:url(../matlabicon.gif)">
80<li><a href="nc2struct.html" class="code" title="function [Data,var_detect,ichoice]=nc2struct(nc,ListVarName)">nc2struct</a>    'nc2struct': transform a netcdf file in a corresponding matlab structure</li></ul>
81<!-- crossreference -->
82
83
84<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
85<div class="fragment"><pre>0001 <span class="comment">%'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY</span>
860002 <span class="comment">%----------------------------------------------------------------------</span>
870003 <span class="comment">% function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName)</span>
880004 <span class="comment">%</span>
890005 <span class="comment">% OUTPUT:</span>
900006 <span class="comment">%  Data: structure containing all the information of the netcdf file (or netcdf object)</span>
910007 <span class="comment">%           with fields:</span>
920008 <span class="comment">%    .ListGlobalAttribute: cell listing the names of the global attributes</span>
930009 <span class="comment">%        .Att_1,Att_2... : values of the global attributes</span>
940010 <span class="comment">%            .ListDimName: cell listing the names of the array dimensions</span>
950011 <span class="comment">%               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName</span>
960012 <span class="comment">%            .ListVarName: cell listing the names of the variables</span>
970013 <span class="comment">%            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName</span>
980014 <span class="comment">%            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName</span>
990015 <span class="comment">%           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName</span>
1000016 <span class="comment">%        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName</span>
1010017 <span class="comment">%  var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else.</span>
1020018 <span class="comment">%  ichoice: = line</span>
1030019 <span class="comment">%</span>
1040020 <span class="comment">%INPUT:</span>
1050021 <span class="comment">%     nc:      name of a netcdf file (char string) or netcdf object</span>
1060022 <span class="comment">% ListVarName: optional list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )</span>
1070023 <span class="comment">%         if ListVarName=[] or{}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes)</span>
1080024 <span class="comment">%         if ListVarName is absent or ='*', ALL the variables are read.</span>
1090025 <span class="comment">%        if ListVarName is a cell array with n lines, the set of variables</span>
1100026 <span class="comment">%                        will be sought by order of priority in the list, while output names will be set by the first line</span>
1110027 <span class="comment">%</span>
1120028 <span class="comment">%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</span>
1130029 <span class="comment">%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.</span>
1140030 <span class="comment">%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</span>
1150031 <span class="comment">%     This file is part of the toolbox UVMAT.</span>
1160032 <span class="comment">%</span>
1170033 <span class="comment">%     UVMAT is free software; you can redistribute it and/or modify</span>
1180034 <span class="comment">%     it under the terms of the GNU General Public License as published by</span>
1190035 <span class="comment">%     the Free Software Foundation; either version 2 of the License, or</span>
1200036 <span class="comment">%     (at your option) any later version.</span>
1210037 <span class="comment">%</span>
1220038 <span class="comment">%     UVMAT is distributed in the hope that it will be useful,</span>
1230039 <span class="comment">%     but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
1240040 <span class="comment">%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
1250041 <span class="comment">%     GNU General Public License (file UVMAT/COPYING.txt) for more details.</span>
1260042 <span class="comment">%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</span>
1270043
1280044 <a name="_sub0" href="#_subfunctions" class="code">function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName)</a>
1290045
1300046 <span class="comment">%default output</span>
1310047 Data=[];
1320048 <span class="comment">% ListIndex=[];</span>
1330049 var_detect=[];
1340050 ichoice=[];<span class="comment">%default</span>
1350051 <span class="comment">%open the netcdf file for reading</span>
1360052 <span class="keyword">if</span> ischar(nc)
1370053     <span class="keyword">if</span> exist(nc,<span class="string">'file'</span>)
1380054         nc=netcdf(nc,<span class="string">'nowrite'</span>);
1390055         testfile=1;
1400056     <span class="keyword">else</span>
1410057        Data.Txt=[<span class="string">'ERROR:file '</span> nc <span class="string">' does not exist'</span>];
1420058        <span class="keyword">return</span>
1430059     <span class="keyword">end</span>
1440060 <span class="keyword">else</span>
1450061     testfile=0;
1460062 <span class="keyword">end</span>
1470063     
1480064 <span class="comment">%  -------- read global attributes -----------</span>
1490065 att_read=att(nc);<span class="comment">%cell of 'global attributes' (nc objects)</span>
1500066 att_key={};<span class="comment">%default</span>
1510067 iatt_g=0;
1520068 <span class="keyword">for</span> iatt=1:length(att_read)
1530069     aa=att_read{iatt};
1540070     keystr=name(aa);
1550071     indstr1=regexp(keystr,<span class="string">'\\'</span>);<span class="comment">%replace dots'</span>
1560072     indstr2=regexp(keystr,<span class="string">'\.'</span>);<span class="comment">%replace dots'</span>
1570073     <span class="keyword">if</span> ~isequal(keystr,<span class="string">'title'</span>) <span class="comment">% PROBLEM WITH civx files do not read 'title'</span>
1580074     <span class="keyword">if</span>  isempty(indstr1) &amp;&amp; isempty(indstr2) <span class="comment">%</span>
1590075        eval([<span class="string">'valuestr=nc.'</span> keystr <span class="string">'(:);'</span>])
1600076         <span class="keyword">if</span> ischar(valuestr) &amp;&amp; length(valuestr)&lt;200
1610077             iatt_g=iatt_g+1;
1620078             indstr1=regexp(keystr,<span class="string">'\\'</span>);<span class="comment">%replace dots'</span>
1630079             indstr2=regexp(keystr,<span class="string">'\.'</span>);<span class="comment">%replace dots'</span>
1640080             <span class="keyword">if</span> isempty(indstr1) &amp;&amp; isempty(indstr2)
1650081                 eval([<span class="string">'Data.'</span> keystr <span class="string">'='''</span> valuestr <span class="string">''';'</span>])
1660082                 att_key{iatt_g}=keystr;
1670083             <span class="keyword">end</span>
1680084         <span class="keyword">elseif</span> isempty(valuestr)
1690085             iatt_g=iatt_g+1;
1700086             eval([<span class="string">'Data.'</span> keystr <span class="string">'=[];'</span>])
1710087             att_key{iatt_g}=keystr;
1720088         <span class="keyword">elseif</span> isnumeric(valuestr)
1730089             iatt_g=iatt_g+1;
1740090             eval([<span class="string">'Data.'</span> keystr <span class="string">'=valuestr;'</span>])
1750091             att_key{iatt_g}=keystr;
1760092         <span class="keyword">end</span>
1770093     <span class="keyword">end</span>
1780094     <span class="keyword">end</span>
1790095 <span class="keyword">end</span>
1800096 Data.ListGlobalAttribute=att_key;
1810097 nbattr=length(att_key);
1820098 neworder=[nbattr+1 [1:nbattr]];
1830099 Data=orderfields(Data,neworder);
1840100
1850101 <span class="comment">%  -------- read dimensions -----------</span>
1860102 dim_read=dim(nc);<span class="comment">%cell of variable dimension names (nc objects)</span>
1870103 dim_name={};
1880104 dim_value=[];
1890105 <span class="keyword">for</span> idim=1:length(dim_read);
1900106     aa=dim_read{idim};
1910107     <span class="keyword">if</span> ~isempty(aa)
1920108     dim_name{idim}=name(aa);
1930109     dim_value(idim)=length(aa);
1940110     <span class="keyword">end</span>
1950111 <span class="keyword">end</span>
1960112 <span class="keyword">if</span> ~isempty(dim_name) &amp;&amp; ~isempty(dim_value)
1970113     Data.ListDimName=dim_name;
1980114     Data.DimValue=dim_value;
1990115     used=zeros(1,length(dim_value));<span class="comment">%initialize test of used dimensions</span>
2000116 <span class="keyword">end</span>
2010117
2020118 <span class="comment">%  -------- read variables -----------</span>
2030119 var_read={}; <span class="comment">%default</span>
2040120 testmulti=0;
2050121 OutputList=[];
2060122 <span class="keyword">if</span> isequal(ListVarName,<span class="string">'*'</span>)|| isempty(ListVarName)
2070123      var_read=var(nc);<span class="comment">%cell of all variables</span>
2080124 <span class="keyword">elseif</span> ~isempty(ListVarName)
2090125     sizvar=size(ListVarName);
2100126     testmulti=(sizvar(1)&gt;1);
2110127     <span class="keyword">if</span> testmulti
2120128         OutputList=ListVarName(1,:);
2130129         testend=0;
2140130         <span class="keyword">for</span> iline=1:sizvar(1)
2150131             <span class="keyword">if</span> testend
2160132                 <span class="keyword">break</span>
2170133             <span class="keyword">end</span>
2180134             <span class="keyword">for</span> ivar=1:sizvar(2)
2190135                 var_read{ivar}=[];<span class="comment">%default</span>
2200136                 var_detect(ivar)=0;<span class="comment">%default</span>
2210137                 VarName=ListVarName{iline,ivar};
2220138                 <span class="keyword">if</span> ~isempty(VarName)
2230139                      var_read{ivar}=nc{VarName};<span class="comment">%select the input variable names</span>
2240140                      <span class="keyword">if</span> ivar==1
2250141                         <span class="keyword">if</span> isempty (var_read{ivar})
2260142                             <span class="keyword">break</span><span class="comment">%go to next line if the first nc variable is not found</span>
2270143                         <span class="keyword">else</span>
2280144                             testend=1; <span class="comment">%this line will be read</span>
2290145                             ichoice=iline-1; <span class="comment">%selectedline number in the list of input names of variables</span>
2300146                             var_detect(ivar)=1;
2310147                         <span class="keyword">end</span>
2320148                      <span class="keyword">else</span>
2330149                           var_detect(ivar)=~isempty (var_read{ivar});
2340150                      <span class="keyword">end</span>
2350151                 <span class="keyword">end</span>
2360152             <span class="keyword">end</span>
2370153         <span class="keyword">end</span>
2380154         <span class="keyword">if</span> ~isempty(find(var_detect))
2390155             OutputList=OutputList(find(var_detect)); 
2400156         <span class="keyword">end</span>
2410157     <span class="keyword">else</span>   <span class="comment">%single list of input variables</span>
2420158         var_detect=ones(size(ListVarName));
2430159         <span class="keyword">for</span> ivar=1:sizvar(2)
2440160             var_read{ivar}=nc{ListVarName{ivar}};<span class="comment">%select the input variable names</span>
2450161             var_detect(ivar)=~isempty(var_read{ivar});
2460162         <span class="keyword">end</span>
2470163     <span class="keyword">end</span>
2480164     var_read=var_read(find(var_detect));
2490165 <span class="keyword">end</span>
2500166 <span class="comment">% var_dim_index=[]; %default</span>
2510167 Data.ListVarName={};<span class="comment">%default</span>
2520168 <span class="keyword">for</span> ivar=1:length(var_read)
2530169     vv=var_read{ivar};
2540170     Data.ListVarName{ivar}=name(vv);<span class="comment">%name of the variable</span>
2550171     <span class="keyword">if</span> testmulti
2560172         Data.ListVarName{ivar}=OutputList{ivar};
2570173     <span class="keyword">else</span>
2580174         Data.ListVarName{ivar}=name(vv);<span class="comment">%name of the variable</span>
2590175     <span class="keyword">end</span>
2600176     var_dim=dim(vv);<span class="comment">%dimension netcdf object of the variable</span>
2610177     <span class="keyword">for</span> ivardim=1:length(var_dim)
2620178         var_dim_name=name(var_dim{ivardim});<span class="comment">%name of the dimension</span>
2630179         <span class="keyword">for</span> idim=1:length(dim_name)<span class="comment">% find the index of the current dimension in the list of dimensions</span>
2640180             <span class="keyword">if</span> isequal(dim_name{idim},var_dim_name)
2650181                 Data.VarDimIndex{ivar}(ivardim)=idim;
2660182                 used(idim)=1;
2670183                 <span class="keyword">break</span>
2680184             <span class="keyword">end</span>
2690185         <span class="keyword">end</span>
2700186     <span class="keyword">end</span> 
2710187     Data.VarDimName{ivar}={};
2720188     <span class="comment">%variable attributes</span>
2730189 <span class="comment">%     if ivar==1</span>
2740190     Data.VarAttribute{ivar}=[];<span class="comment">%initialisation of the list of variable attributes</span>
2750191 <span class="comment">%     end</span>
2760192     <span class="comment">%variable attributes</span>
2770193     att_read=att(vv);
2780194     <span class="keyword">for</span> iatt=1:length(att_read)
2790195         aa=att_read{iatt};
2800196         eval([<span class="string">'valuestr=vv.'</span> name(aa) <span class="string">'(:);'</span>])
2810197         <span class="keyword">if</span> ischar(valuestr)
2820198             eval([<span class="string">'Data.VarAttribute{ivar}.'</span> name(aa) <span class="string">'='''</span> valuestr <span class="string">''';'</span>])
2830199         <span class="keyword">elseif</span> isempty(valuestr)
2840200             eval([<span class="string">'Data.VarAttribute{ivar}.'</span> name(aa) <span class="string">'=[];'</span>])
2850201         <span class="keyword">elseif</span> isnumeric(valuestr)
2860202             eval([<span class="string">'Data.VarAttribute{ivar}.'</span> name(aa) <span class="string">'=valuestr;'</span>])
2870203         <span class="keyword">end</span>
2880204     <span class="keyword">end</span>
2890205 <span class="keyword">end</span>
2900206
2910207 <span class="comment">%select the used dimensions</span>
2920208 <span class="keyword">if</span> isempty(var_read)
2930209     <span class="keyword">if</span> isfield(Data,<span class="string">'ListDimName'</span>) &amp;&amp; isfield(Data,<span class="string">'DimValue'</span>)
2940210     Data=rmfield(Data,<span class="string">'ListDimName'</span>);
2950211     Data=rmfield(Data,<span class="string">'DimValue'</span>);
2960212     <span class="keyword">end</span>
2970213 <span class="keyword">else</span>
2980214     old_dim_index=find(used); <span class="comment">%dimension indices which are used by the selected variables</span>
2990215     old2new=cumsum(used);
3000216     Data.ListDimName=Data.ListDimName(old_dim_index);
3010217     Data.DimValue=Data.DimValue(old_dim_index);
3020218 <span class="keyword">end</span>
3030219 <span class="keyword">for</span> ivar=1:length(var_read)
3040220     Data.VarDimIndex{ivar}=(old2new(Data.VarDimIndex{ivar}));
3050221     Data.VarDimName{ivar}=(Data.ListDimName(Data.VarDimIndex{ivar}));
3060222 <span class="keyword">end</span>
3070223 <span class="comment">%variable values</span>
3080224
3090225 <span class="keyword">if</span>  ~isempty(ListVarName)
3100226     <span class="keyword">for</span> ivar=1:length(Data.ListVarName)
3110227         vv=var_read{ivar};
3120228         vdata=vv(:);<span class="comment">%data array of the field variable</span>
3130229         eval([<span class="string">'Data.'</span> Data.ListVarName{ivar} <span class="string">'=vdata;'</span>])<span class="comment">%read the variable data</span>
3140230     <span class="keyword">end</span>
3150231 <span class="keyword">end</span>
3160232 <span class="comment">%  -------- close fle-----------</span>
3170233 <span class="keyword">if</span> testfile==1
3180234     close(nc)
3190235 <span class="keyword">end</span>
3200236</pre></div>
321<hr><address>Generated on Fri 13-Nov-2009 11:17:03 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
322</body>
323</html>
Note: See TracBrowser for help on using the repository browser.