1 | function add_ini_chla(inifile,gridfile,seas_datafile,cycle,Roa); |
---|
2 | |
---|
3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
4 | % |
---|
5 | % function [longrd,latgrd,chla]=add_ini_chla(inifile,gridfile,... |
---|
6 | % seas_datafile,... |
---|
7 | % cycle); |
---|
8 | % |
---|
9 | % pierrick 2001 |
---|
10 | % |
---|
11 | % Add chlorophyll in a ROMS initial file. |
---|
12 | % take seasonal data for the surface levels and extrapole |
---|
13 | % using Morel and Berthon (1989) parameterization for the |
---|
14 | % lower levels. warning ! the unit is (micro mole/l) in the |
---|
15 | % dataset. |
---|
16 | % do a temporal interpolation to have values at initial |
---|
17 | % time. |
---|
18 | % |
---|
19 | % ref: Morel and Berthon, Surface pigments, algal biomass |
---|
20 | % profiles, and potential production of the euphotic layer: |
---|
21 | % Relationships reinvestigated in view of remote-sensing |
---|
22 | % applications. Limnol. Oceanogr., 34, 1989, 1545-1562. |
---|
23 | % |
---|
24 | % input: |
---|
25 | % |
---|
26 | % inifile : roms initial file to process (netcdf) |
---|
27 | % gridfile : roms grid file (netcdf) |
---|
28 | % seas_datafile : regular longitude - latitude - z seasonal data |
---|
29 | % file used for the upper levels (netcdf) |
---|
30 | % ann_datafile : regular longitude - latitude - z annual data |
---|
31 | % file used for the lower levels (netcdf) |
---|
32 | % cycle : time length (days) of climatology cycle (ex:360 for |
---|
33 | % annual cycle) - 0 if no cycle. |
---|
34 | % |
---|
35 | % output: |
---|
36 | % |
---|
37 | % [longrd,latgrd,chla] : surface field to plot (as an illustration) |
---|
38 | % |
---|
39 | % Further Information: |
---|
40 | % http://www.brest.ird.fr/Roms_tools/ |
---|
41 | % |
---|
42 | % This file is part of ROMSTOOLS |
---|
43 | % |
---|
44 | % ROMSTOOLS is free software; you can redistribute it and/or modify |
---|
45 | % it under the terms of the GNU General Public License as published |
---|
46 | % by the Free Software Foundation; either version 2 of the License, |
---|
47 | % or (at your option) any later version. |
---|
48 | % |
---|
49 | % ROMSTOOLS is distributed in the hope that it will be useful, but |
---|
50 | % WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
51 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
52 | % GNU General Public License for more details. |
---|
53 | % |
---|
54 | % You should have received a copy of the GNU General Public License |
---|
55 | % along with this program; if not, write to the Free Software |
---|
56 | % Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
57 | % MA 02111-1307 USA |
---|
58 | % |
---|
59 | % Copyright (c) 2001-2006 by Pierrick Penven |
---|
60 | % e-mail:Pierrick.Penven@ird.fr |
---|
61 | % |
---|
62 | % Updated August-2006 by Pierrick Penven |
---|
63 | % |
---|
64 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
65 | % |
---|
66 | disp('Add_ini_chla: creating variable and attribute') |
---|
67 | default=NaN; |
---|
68 | % |
---|
69 | % read in the datafile |
---|
70 | % |
---|
71 | ncseas=netcdf(seas_datafile); |
---|
72 | x=ncseas{'X'}(:); |
---|
73 | y=ncseas{'Y'}(:); |
---|
74 | datatime=ncseas{'T'}(:); |
---|
75 | datatime=datatime*30; % !!! if the time in the dataset is in months !!! |
---|
76 | tlen=length(datatime); |
---|
77 | % |
---|
78 | % open the grid file |
---|
79 | % |
---|
80 | % |
---|
81 | % open the grid file |
---|
82 | % |
---|
83 | ng=netcdf(gridfile); |
---|
84 | lon=ng{'lon_rho'}(:); |
---|
85 | %lon(lon<0)=lon(lon<0)+360; |
---|
86 | lat=ng{'lat_rho'}(:); |
---|
87 | h=ng{'h'}(:); |
---|
88 | close(ng); |
---|
89 | [M,L]=size(lon); |
---|
90 | dl=0.5; |
---|
91 | minlon=min(min(lon))-dl; |
---|
92 | maxlon=max(max(lon))+dl; |
---|
93 | minlat=min(min(lat))-dl; |
---|
94 | maxlat=max(max(lat))+dl; |
---|
95 | imin=max(find(x<=minlon)); |
---|
96 | imax=min(find(x>=maxlon)); |
---|
97 | jmin=max(find(y<=minlat)); |
---|
98 | jmax=min(find(y>=maxlat)); |
---|
99 | x=x(imin:imax); |
---|
100 | y=y(jmin:jmax); |
---|
101 | % |
---|
102 | % open the initial file |
---|
103 | % |
---|
104 | nc=netcdf(inifile,'write'); |
---|
105 | theta_s = nc{'theta_s'}(:); |
---|
106 | if isempty(theta_s) |
---|
107 | disp('Restart file') |
---|
108 | theta_s=nc.theta_s(:); |
---|
109 | theta_b=nc.theta_b(:); |
---|
110 | hc=nc.hc(:); |
---|
111 | else |
---|
112 | theta_b = nc{'theta_b'}(:); |
---|
113 | hc = nc{'hc'}(:); |
---|
114 | end |
---|
115 | N = length(nc('s_rho')); |
---|
116 | scrum_time = nc{'scrum_time'}(:); |
---|
117 | scrum_time = scrum_time / (24*3600); |
---|
118 | tinilen = length(scrum_time); |
---|
119 | redef(nc); |
---|
120 | nc{'CHLA'} = ncdouble('time','s_rho','eta_rho','xi_rho') ; |
---|
121 | nc{'CHLA'}.long_name = ncchar('Chlorophyll'); |
---|
122 | nc{'CHLA'}.long_name = 'Chlorophyll'; |
---|
123 | nc{'CHLA'}.units = ncchar('mg C'); |
---|
124 | nc{'CHLA'}.units = 'mg C'; |
---|
125 | nc{'CHLA'}.fields = ncchar('CHLA, scalar, series'); |
---|
126 | nc{'CHLA'}.fields = 'CHLA, scalar, series'; |
---|
127 | % |
---|
128 | endef(nc); |
---|
129 | % |
---|
130 | % Get the missing values |
---|
131 | % |
---|
132 | missval=ncseas{'chlorophyll'}.missing_value(:); |
---|
133 | % |
---|
134 | % loop on time |
---|
135 | % |
---|
136 | for l=1:tinilen |
---|
137 | disp(['time index: ',num2str(l),' of total: ',num2str(tinilen)]) |
---|
138 | % |
---|
139 | % get data time indices and weights for temporal interpolation |
---|
140 | % |
---|
141 | if cycle~=0 |
---|
142 | modeltime=mod(scrum_time(l),cycle); |
---|
143 | else |
---|
144 | modeltime=scrum_time; |
---|
145 | end |
---|
146 | l1=find(modeltime==datatime); |
---|
147 | if isempty(l1) |
---|
148 | disp('temporal interpolation') |
---|
149 | l1=max(find(datatime<modeltime)); |
---|
150 | time1=datatime(l1); |
---|
151 | if isempty(l1) |
---|
152 | if cycle~=0 |
---|
153 | l1=tlen; |
---|
154 | time1=datatime(l1)-cycle; |
---|
155 | else |
---|
156 | error('No previous time in the dataset') |
---|
157 | end |
---|
158 | end |
---|
159 | l2=min(find(datatime>modeltime)); |
---|
160 | time2=datatime(l2); |
---|
161 | if isempty(l2) |
---|
162 | if cycle~=0 |
---|
163 | l2=1; |
---|
164 | time2=datatime(l2)+cycle; |
---|
165 | else |
---|
166 | error('No posterious time in the dataset') |
---|
167 | end |
---|
168 | end |
---|
169 | disp(['Initialisation time: ',num2str(modeltime),... |
---|
170 | ' - Time 1: ',num2str(time1),... |
---|
171 | ' - Time 2: ',num2str(time2)]) |
---|
172 | cff1=(modeltime-time2)/(time1-time2); |
---|
173 | cff2=(time1-modeltime)/(time1-time2); |
---|
174 | else |
---|
175 | cff1=1; |
---|
176 | l2=l1; |
---|
177 | cff2=0; |
---|
178 | end |
---|
179 | % |
---|
180 | % interpole the annual dataset on the horizontal roms grid |
---|
181 | % |
---|
182 | disp('Add_ini_chla: horizontal extrapolation of surface data') |
---|
183 | surfchla=squeeze(ncseas{'chlorophyll'}(l1,jmin:jmax,imin:imax)); |
---|
184 | surfchla=get_missing_val(x,y,surfchla,missval,Roa,default); |
---|
185 | surfchla2=squeeze(ncseas{'chlorophyll'}(l2,jmin:jmax,imin:imax)); |
---|
186 | surfchla2=get_missing_val(x,y,surfchla2,missval,Roa,default); |
---|
187 | surfchla=cff1*surfchla + cff2*surfchla2; |
---|
188 | surfchlaroms=interp2(x,y,surfchla,lon,lat); |
---|
189 | % |
---|
190 | % extrapole the chlorophyll on the vertical |
---|
191 | % |
---|
192 | zeta = squeeze(nc{'zeta'}(l,:,:)); |
---|
193 | zroms=zlevs(h,zeta,theta_s,theta_b,hc,N,'r'); |
---|
194 | disp(['Add_ini_chla: vertical ',... |
---|
195 | 'extrapolation of chlorophyll']) |
---|
196 | chlaroms=extr_chlo(surfchlaroms,zroms); |
---|
197 | nc{'CHLA'}(l,:,:,:)=chlaroms; |
---|
198 | end |
---|
199 | close(nc); |
---|
200 | close(ncseas); |
---|
201 | return |
---|