1 | function [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$ |
---|
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 | % |
---|
97 | global PROJECT_ID |
---|
98 | % |
---|
99 | % check argument+todo+ |
---|
100 | % |
---|
101 | % build LAI ASCII filename to be read |
---|
102 | fullfilename=[PROJECT_ID 'LAI/laisen' num2str(yyyy) '_float.txt']; |
---|
103 | disp(['iii : opening for reading ', fullfilename]); |
---|
104 | % opening file |
---|
105 | fid=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 |
---|
113 | header_line = fgetl(fid); |
---|
114 | % |
---|
115 | % loop to find the number of columns |
---|
116 | % and initialize the "day of year" array |
---|
117 | index_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) |
---|
120 | nb_doy_max=ceil((datenum(yyyy,12,eomday(yyyy,12)) - datenum(yyyy,1,1))/8); |
---|
121 | doy=zeros(nb_doy_max,1); |
---|
122 | doy(:)=NaN; |
---|
123 | datestr_value=doy; |
---|
124 | while ( ~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; |
---|
136 | end |
---|
137 | % |
---|
138 | % we know now how many columns are written on header |
---|
139 | nb_column = index_column; |
---|
140 | nb_doy = nb_column - 2; |
---|
141 | clear index_column |
---|
142 | % |
---|
143 | % remove extra doy NaN values due to preallocation |
---|
144 | doy=doy(~isnan(doy)); |
---|
145 | % |
---|
146 | % initialisation d'un un tableau string de nb_doy elements à 9999999 |
---|
147 | datestr_value=repmat('99999999',nb_doy,1); |
---|
148 | for index_doy=1:nb_doy |
---|
149 | datestr_value(index_doy,:)=datestr((datenum(yyyy,1,1) + doy(index_doy) - 1),'yyyymmdd'); |
---|
150 | end |
---|
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 |
---|
156 | nb_data_line_max = 231002 - 2 ; %++ |
---|
157 | lat_value=zeros(nb_data_line_max,1); |
---|
158 | lat_value(:)=NaN; |
---|
159 | lon_value=zeros(nb_data_line_max,1); |
---|
160 | lon_value(:)=NaN; |
---|
161 | lai_line=zeros(nb_doy,nb_data_line_max); |
---|
162 | lai_line(:,:)=NaN; |
---|
163 | % |
---|
164 | % we also need intermidiate arrays of lat and lon |
---|
165 | lat_multi=zeros(nb_data_line_max,1); |
---|
166 | lat_multi(:)=NaN; |
---|
167 | lon_multi=zeros(nb_data_line_max,1); |
---|
168 | lon_multi(:)=NaN; |
---|
169 | % |
---|
170 | % loop reading data from file (from second line till end of file) |
---|
171 | index_data_line = 1; |
---|
172 | while ( ~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; |
---|
186 | end |
---|
187 | % |
---|
188 | % close file |
---|
189 | fclose(fid); |
---|
190 | % |
---|
191 | nb_data_line = index_data_line; |
---|
192 | clear index_data_line |
---|
193 | % |
---|
194 | nb_lat=size(lat_value,1) |
---|
195 | nb_lon=size(lon_value,1) |
---|
196 | % |
---|
197 | % organize LAI values in a 3D array (idate,ilat,ilon) |
---|
198 | lai_value=zeros(nb_doy,nb_lat,nb_lon); |
---|
199 | lai_value(:,:,:)=NaN; |
---|
200 | for 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 |
---|
206 | end |
---|
207 | % |
---|
208 | end |
---|