source: trunk/src/read_lai.m @ 327

Last change on this file since 327 was 327, checked in by pinsard, 13 years ago

change svn properties

  • Property svn:keywords set to URL
File size: 5.3 KB
Line 
1function [lon_value, lat_value, datestr_value, lai_value]=read_lai(yyyy)
2
3%READ_LAI fill array LAI 3d array and associated arrays date, lat, lon
4%          with data in :file:`${PROJECT_ID}/LAI/laisen{yyyy}_float.txt`
5
6%
7%+
8%
9% ==========
10% read_lai.m
11% ==========
12%
13% .. function:: read_lai(yyyy)
14%
15% DESCRIPTION
16% ===========
17%
18%    :param yyyy: year
19%    :type yyyy: int16
20%    :raise yyyy: required
21%
22% read a file :file:`${PROJECT_ID}/LAI/laisen{yyyy}_float.txt`
23%
24% Les LAI sont stockées dans le tableau 3D (temps, lat, lon) **lai_value**
25%
26% Les longitudes sont stockées dans le tableau 1D **lon_value**.
27%
28% Les latitudes sont stockées dans le tableau 1D **lat_value**.
29%
30% Les dates au format `yyyymmdd` sont stockées dans le tableau 1D **datestr_value**.
31%
32% EXAMPLES
33% ========
34%
35% To read :file:`${PROJECT_ID}/LAI/laisen2001_float.txt`::
36%
37%   $ octave
38%   octave> varamma_startup
39%   octave> more off
40%   octave> yyyy=2001
41%   octave> [lon_value, lat_value, datestr_value, lai_value]=read_lai(yyyy);
42%
43% A file :file:`${PROJECT_ID}/LAI/laisen2001_write_lai_float.txt` can be written
44% with data we just read::
45%
46%   octave> result=write_lai(lon_value, lat_value, datestr_value, lai_value);
47%
48% Files comparison might help detecting problems (either on files or in programs involved)::
49%
50%   $ diff ${PROJECT_ID}/LAI/laisen2001_float.txt ${PROJECT_ID}/LAI/laisen2001_write_lai_float.txt
51%
52% SEE ALSO
53% ========
54%
55% :ref:`guide data LAI <data_lai>`
56%
57% :func:`simul_lai`
58% :func:`write_lai`
59%
60% :func:`read_lai_2d`
61%
62% TODO
63% ====
64%
65% status de retour
66%
67%
68% description du résultat en ReST
69%
70% hard coded value of nb_data_line_max
71% should be calculated according to latitude and longitude range and resolution
72%
73% check argument
74%
75% EVOLUTIONS
76% ==========
77%
78% $Id: read_lai.m 325 2011-08-04 15:30:01Z pinsard $
79%
80% $URL$
81%
82% - fplod 20101202T141321Z aedon.locean-ipsl.upmc.fr (Darwin)
83%
84%   * new terminology and new format
85%   * add parameter information in header
86%
87% - fplod 20100903T094128Z aedon.locean-ipsl.upmc.fr (Darwin)
88%
89%   * remove unique on lat and lon
90%
91% - fplod 20100827T103733Z aedon.locean-ipsl.upmc.fr (Darwin)
92%
93%   * creation to validate :file:`write_lai.m`
94%
95%-
96%
97global PROJECT_ID
98%
99% check argument+todo+
100%
101% build LAI ASCII filename to be read
102fullfilename=[PROJECT_ID 'LAI/laisen'  num2str(yyyy) '_float.txt'];
103disp(['iii : opening for reading ', fullfilename]);
104% opening file
105fid=fopen(fullfilename,'r');
106%
107% +todo+ check opening ok
108%
109% first line contains "Y,X" and an unknown quantity of days of years
110% so we read this line and loop to find the number of columns
111%
112% get next line as a string
113header_line = fgetl(fid);
114%
115% loop to find the number of columns
116% and initialize the "day of year" array
117index_column = 0;
118% the maximum of doy is the number of days in year yyyy
119% divided by 8 (according to time resolution of this dataset)
120nb_doy_max=ceil((datenum(yyyy,12,eomday(yyyy,12)) - datenum(yyyy,1,1))/8);
121doy=zeros(nb_doy_max,1);
122doy(:)=NaN;
123datestr_value=doy;
124while ( ~isempty(header_line) )
125   % parse next column label
126   [next,header_line] = strtok(header_line,',');
127   switch next
128      case {'X'}
129      case {'Y'}
130      otherwise
131         doy(index_column - 1) = str2double(next);
132   end
133   clear next
134   % increase index of column
135   index_column = index_column + 1;
136end
137%
138% we know now how many columns are written on header
139nb_column = index_column;
140nb_doy = nb_column - 2;
141clear index_column
142%
143% remove extra doy NaN values due to preallocation
144doy=doy(~isnan(doy));
145%
146% initialisation d'un un tableau string de nb_doy elements à 9999999
147datestr_value=repmat('99999999',nb_doy,1);
148for index_doy=1:nb_doy
149    datestr_value(index_doy,:)=datestr((datenum(yyyy,1,1) + doy(index_doy) - 1),'yyyymmdd');
150end
151%
152% following lines contains lat, lon and LAI values
153% we first preallocate arrays (boite,dx,dy formula or
154% equivalent of IDL function file_lines ++)
155% and initialize values to NaN
156nb_data_line_max = 231002 - 2 ; %++
157lat_value=zeros(nb_data_line_max,1);
158lat_value(:)=NaN;
159lon_value=zeros(nb_data_line_max,1);
160lon_value(:)=NaN;
161lai_line=zeros(nb_doy,nb_data_line_max);
162lai_line(:,:)=NaN;
163%
164% we also need intermidiate arrays of lat and lon
165lat_multi=zeros(nb_data_line_max,1);
166lat_multi(:)=NaN;
167lon_multi=zeros(nb_data_line_max,1);
168lon_multi(:)=NaN;
169%
170% loop reading data from file (from second line till end of file)
171index_data_line = 1;
172while ( ~feof(fid) )
173   % read one line at a time
174   data_line = fgetl(fid)
175   %
176   % split data_line to values
177   data=sscanf(data_line,'%f,');
178   clear data_line
179   % put values into arrays to be used for plot or computation
180   lat_value(index_data_line)=data(1);
181   lon_value(index_data_line)=data(2);
182   lai_line(:,index_data_line)=data(3:nb_column);
183   clear data
184   %++disp(['data_line reading '  num2str(index_data_line)]);
185   index_data_line = index_data_line + 1;
186end
187%
188% close file
189fclose(fid);
190%
191nb_data_line = index_data_line;
192clear index_data_line
193%
194nb_lat=size(lat_value,1)
195nb_lon=size(lon_value,1)
196%
197% organize LAI values in a 3D array (idate,ilat,ilon)
198lai_value=zeros(nb_doy,nb_lat,nb_lon);
199lai_value(:,:,:)=NaN;
200for index_lat=1:nb_lat
201   for index_lon=1:nb_lon
202       index_data_line = index_lon + ((index_lat-1)* nb_lon);
203       %++disp(['data_line storage '  num2str(index_data_line)]);
204       lai_value(:,index_lat,index_lon)=lai_line(:,index_data_line);
205   end
206end
207%
208end
Note: See TracBrowser for help on using the repository browser.