1 | function [lon_value, lat_value, datestr_value, lai_value]=read_lai_2d(yyyy) |
---|
2 | |
---|
3 | %READ_LAI_2D fill array LAI 2D 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_2d.m |
---|
11 | % ============= |
---|
12 | % |
---|
13 | % .. function:: read_lai_2d(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 2D (temps,ligne) **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 | % see demo 1 |
---|
38 | % |
---|
39 | % A file :file:`${PROJECT_ID}/LAI/laisen2001_write_lai_2d_float.txt` can be written |
---|
40 | % with data we just read:: |
---|
41 | % |
---|
42 | % octave> result=write_lai_2d(lon_value, lat_value, datestr_value, lai_value); |
---|
43 | % |
---|
44 | % Files comparison might help detecting problems (either on files or in programs involved):: |
---|
45 | % |
---|
46 | % $ diff ${PROJECT_ID}/LAI/laisen2001_float.txt ${PROJECT_ID}/LAI/laisen2001_write_lai_2d_float.txt |
---|
47 | % |
---|
48 | % SEE ALSO |
---|
49 | % ======== |
---|
50 | % |
---|
51 | % :ref:`guide data LAI <data_lai>` |
---|
52 | % |
---|
53 | % :func:`simul_lai_2d` |
---|
54 | % :func:`write_lai_2d` |
---|
55 | % :func:`showgrid` |
---|
56 | % |
---|
57 | % TODO |
---|
58 | % ==== |
---|
59 | % |
---|
60 | % status de retour |
---|
61 | % |
---|
62 | % description du résultat en ReST |
---|
63 | % |
---|
64 | % hard coded value of nb_data_line_max; |
---|
65 | % should be calculated according to latitude and longitude range and resolution |
---|
66 | % |
---|
67 | % EVOLUTIONS |
---|
68 | % ========== |
---|
69 | % |
---|
70 | % $Id: read_lai_2d.m 312 2011-07-07 17:13:54Z pinsard $ |
---|
71 | % |
---|
72 | % $URL$ |
---|
73 | % |
---|
74 | % - fplod 20110228T142213Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
75 | % |
---|
76 | % * move example to demo |
---|
77 | % * use int2str instead of num2str |
---|
78 | % |
---|
79 | % - fplod 20101208T102711Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
80 | % |
---|
81 | % * explicit yyyy type int16 (side effect : modification of datenum call) |
---|
82 | % * check argument |
---|
83 | % |
---|
84 | % - fplod 20101206T152338Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
85 | % |
---|
86 | % * check argument |
---|
87 | % |
---|
88 | % - fplod 20101206T103915Z adonis.locean-ipsl.upmc.fr (Linux) |
---|
89 | % |
---|
90 | % * cleanning thanks to mlint |
---|
91 | % |
---|
92 | % - fplod 20101203T171418Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
93 | % |
---|
94 | % * comma also on the first line (for qgis) |
---|
95 | % |
---|
96 | % - fplod 20101201T153337Z zeus.locean-ipsl.upmc.fr (Linux) |
---|
97 | % |
---|
98 | % * new terminology and new format |
---|
99 | % * add parameter information in header |
---|
100 | % |
---|
101 | % - fplod 20101022T140456Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
102 | % |
---|
103 | % * add test on octave version because of dlmread problem running |
---|
104 | % octave 3.0.2 |
---|
105 | % |
---|
106 | % - fplod 20101022T104521Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
107 | % |
---|
108 | % * bug fix : replace '' by ' ' in dlmread instruction |
---|
109 | % |
---|
110 | % - fplod 20100913T100256Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
111 | % |
---|
112 | % * creation from read_lai.m because impossible to define |
---|
113 | % lai_values(46,839041,839041) - too big |
---|
114 | % * faster using dlmread |
---|
115 | % |
---|
116 | %- |
---|
117 | % |
---|
118 | global PROJECT_ID |
---|
119 | % |
---|
120 | usage='lon_value, lat_value, datestr_value, lai_value]=read_lai_2d(yyyy)'; |
---|
121 | % |
---|
122 | if nargin~=1 |
---|
123 | disp(['Incorrect number of arguments']); |
---|
124 | error(usage); |
---|
125 | end |
---|
126 | % |
---|
127 | arg_info=whos('yyyy'); |
---|
128 | if ~strcmp(arg_info.class,'int16') |
---|
129 | disp(['Incorrect type of arg yyyy']); |
---|
130 | whos yyyy |
---|
131 | error(usage); |
---|
132 | end |
---|
133 | clear arg_info |
---|
134 | % |
---|
135 | % build LAI ASCII filename to be read |
---|
136 | fullfilename=[PROJECT_ID 'LAI/laisen' int2str(yyyy) '_float.txt']; |
---|
137 | disp(['iii : opening for reading ', fullfilename]); |
---|
138 | % opening file |
---|
139 | fid=fopen(fullfilename,'r'); |
---|
140 | % test if fullfilename exists |
---|
141 | if (fid == -1) |
---|
142 | disp(message) |
---|
143 | error([ fullfilename ' not exist']) |
---|
144 | end |
---|
145 | clear fullfilename |
---|
146 | % |
---|
147 | % read file |
---|
148 | % |
---|
149 | % first line contains "Y,X" and an unknown quantity of days of years |
---|
150 | % so we read this line and loop to find the number of columns |
---|
151 | % |
---|
152 | % get next line as a string |
---|
153 | header_line = fgetl(fid); |
---|
154 | % |
---|
155 | % loop to find the number of columns |
---|
156 | % and initialize the "day of year" array |
---|
157 | index_column = 0; |
---|
158 | % the maximum of doy is the number of days in year yyyy |
---|
159 | % divided by 8 (according to time resolution of this dataset) |
---|
160 | yyyy_d=double(yyyy); |
---|
161 | nb_doy_max=ceil((datenum(yyyy_d,12,eomday(yyyy_d,12)) - datenum(yyyy_d,1,1))/8); |
---|
162 | doy=zeros(nb_doy_max,1); |
---|
163 | doy(:)=NaN; |
---|
164 | datestr_value=doy; |
---|
165 | while ( ~isempty(header_line) ) |
---|
166 | % parse next column label |
---|
167 | [next,header_line] = strtok(header_line,','); |
---|
168 | switch next |
---|
169 | case {'X'} |
---|
170 | case {'Y'} |
---|
171 | otherwise |
---|
172 | doy(index_column - 1) = str2double(next); |
---|
173 | end |
---|
174 | clear next |
---|
175 | % increase index of column |
---|
176 | index_column = index_column + 1; |
---|
177 | end |
---|
178 | % close file |
---|
179 | fclose(fid); |
---|
180 | % |
---|
181 | % we know now how many columns are written on header |
---|
182 | nb_column = index_column; |
---|
183 | nb_doy = nb_column - 2; |
---|
184 | clear index_column |
---|
185 | % |
---|
186 | % remove extra doy NaN values due to preallocation |
---|
187 | doy=doy(~isnan(doy)); |
---|
188 | % |
---|
189 | % initialisation d'un tableau string de nb_doy elements à 9999999 |
---|
190 | datestr_value=repmat('99999999',nb_doy,1); |
---|
191 | for index_doy=1:nb_doy |
---|
192 | datestr_value(index_doy,:)=datestr((datenum(yyyy_d,1,1) + doy(index_doy) - 1),'yyyymmdd'); |
---|
193 | end |
---|
194 | % |
---|
195 | % following lines contains lat, lon and LAI values |
---|
196 | % we first preallocate arrays (boite,dx,dy formula or |
---|
197 | % equivalent of IDL function file_lines ++) |
---|
198 | % and initialize values to NaN |
---|
199 | nb_data_line_max = 231002 - 2 ; %++ |
---|
200 | lat_value=zeros(1,nb_data_line_max); |
---|
201 | lat_value(:)=NaN; |
---|
202 | lon_value=zeros(1,nb_data_line_max); |
---|
203 | lon_value(:)=NaN; |
---|
204 | lai_value=zeros(nb_doy,nb_data_line_max); |
---|
205 | lai_value(:,:)=NaN; |
---|
206 | % |
---|
207 | % reading data from file (from second line till end of file) |
---|
208 | data = dlmread(fullfilename,',', 1, 0); |
---|
209 | % |
---|
210 | % put values into arrays to be used for plot or computation |
---|
211 | lat_value=data(:,1); |
---|
212 | lon_value=data(:,2); |
---|
213 | lai_value=data(:,3:size(data,2)); |
---|
214 | clear data |
---|
215 | % |
---|
216 | end |
---|
217 | |
---|
218 | %!demo |
---|
219 | %! % To read :file:`${PROJECT_ID}/LAI/laisen2001_float.txt`:: |
---|
220 | %! varamma_startup |
---|
221 | %! more off |
---|
222 | %! yyyy=int16(2001); |
---|
223 | %! [lon_value, lat_value, datestr_value, lai_value]=read_lai_2d(yyyy); |
---|