source: TOOLS/CMIP6_FORCING/SOLAR/regrid_ssi.m @ 3381

Last change on this file since 3381 was 3381, checked in by jgipsl, 4 years ago

Initial version of scripts to create solar forcings.

O Boucher

File size: 4.1 KB
Line 
1function ssi_out = regrid_ssi(ssi_in,wvl_edges_in,wvl_edges_out)
2
3% Tim Kruschke
4% mail: tkruschke[at]geomar.de
5%
6% Function to regrid spectral solar irradiance. Prepared in context of
7% ROMIC-SOLIC project and CMIP6 solar forcing, where SATIRE and NRL data
8% are considered.
9%
10% Input:
11% ssi_in: spectral solar irradiance (dimensions wavelengths x time)
12% wvl_edges_in: edges of original wavelength bins (dimensions wavelengths x 2)
13% wvl_edges_out: edges of target wavelength bins (dimensions wavelengths x 2)
14%
15% Output:
16% ssi_out: spectral solar irradiance fro specified target wavelength bins (bin means)
17%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18
19ssi_out=zeros(size(wvl_edges_out,1),size(ssi_in,2));
20
21for i=1:size(wvl_edges_out,1);
22    index_in=find(wvl_edges_in(:,2)>wvl_edges_out(i,1) & wvl_edges_in(:,1)<wvl_edges_out(i,2));
23    if ~isempty(index_in);
24        if wvl_edges_in(index_in(1),1)>wvl_edges_out(i,1); % Linear interpolation for part between low edge of target bin
25            tmp_ssi=ssi_in(index_in(1)-1,:)+ ...           %  and first original bin found inside
26                (ssi_in(index_in(1),:)-ssi_in(index_in(1)-1,:))/ ...
27                (wvl_edges_in(index_in(1),1)-wvl_edges_in(index_in(1)-1,2))* ...
28                ((wvl_edges_in(index_in(1),1)+wvl_edges_out(i,1))/2-wvl_edges_in(index_in(1)-1,2));
29            ssi_out(i,:)=ssi_out(i,:)+tmp_ssi*(wvl_edges_in(index_in(1),1)-wvl_edges_out(i,1));
30        end
31        for ii=1:length(index_in);
32            lowmarg=(wvl_edges_in(index_in(ii),1)<wvl_edges_out(i,1));
33            upmarg=(wvl_edges_in(index_in(ii),2)>wvl_edges_out(i,2));
34            if ~lowmarg && ~upmarg;
35                ssi_out(i,:)=ssi_out(i,:)+ssi_in(index_in(ii),:)* ...
36                    (wvl_edges_in(index_in(ii),2)-wvl_edges_in(index_in(ii),1));
37            elseif lowmarg && upmarg;
38                ssi_out(i,:)=ssi_out(i,:)+ssi_in(index_in(ii),:)*(wvl_edges_out(i,2)-wvl_edges_out(i,1));
39            elseif lowmarg
40                ssi_out(i,:)=ssi_out(i,:)+ssi_in(index_in(ii),:)* ...
41                    (wvl_edges_in(index_in(ii),2)-wvl_edges_out(i,1));
42            elseif upmarg
43                ssi_out(i,:)=ssi_out(i,:)+ssi_in(index_in(ii),:)* ...
44                    (wvl_edges_out(i,2)-wvl_edges_in(index_in(ii),1));
45                           
46            else
47                error('Logical error in function!')               
48            end
49            if ii~=length(index_in);
50                if wvl_edges_in(index_in(ii),2)~=wvl_edges_in(index_in(ii)+1,1); % Linear interpolation for gap between original bins
51                    tmp_ssi=(ssi_in(index_in(ii),:)+ssi_in(index_in(ii+1),:))/2;
52                    ssi_out(i,:)=ssi_out(i,:)+tmp_ssi* ...
53                        (wvl_edges_in(index_in(ii+1),1)-wvl_edges_in(index_in(ii),2));
54                end
55            end
56        end
57        if wvl_edges_in(index_in(end),2)<wvl_edges_out(i,2); % Linear interpolation for part between high edge of target bin
58            tmp_ssi=ssi_in(index_in(end),:)+ ...             %  and last original bin found inside
59                (ssi_in(index_in(end)+1,:)-ssi_in(index_in(end),:))/ ...
60                (wvl_edges_in(index_in(end)+1,1)-wvl_edges_in(index_in(end),2))* ...
61                ((wvl_edges_in(index_in(end),2)+wvl_edges_out(i,2))/2-wvl_edges_in(index_in(end),2));
62            ssi_out(i,:)=ssi_out(i,:)+tmp_ssi*(wvl_edges_out(i,2)-wvl_edges_in(index_in(end),2));
63        end
64    else % Linear interpolation between two original bins onto center of empty target bin
65        index_below=find(wvl_edges_in(:,2)<wvl_edges_out(i,1),1,'last');
66        index_above=find(wvl_edges_in(:,1)>wvl_edges_out(i,2),1,'first');
67        tmp_ssi=ssi_in(index_below,:)+ ...
68            (ssi_in(index_above,:)-ssi_in(index_below,:))/ ...
69            (wvl_edges_in(index_above,1)-wvl_edges_in(index_below,2))* ...
70            ((wvl_edges_out(i,1)+wvl_edges_out(i,2))/2-wvl_edges_in(index_below,2));
71        ssi_out(i,:)=tmp_ssi*(wvl_edges_out(i,2)-wvl_edges_out(i,1));
72    end
73    ssi_out(i,:)=ssi_out(i,:)/(wvl_edges_out(i,2)-wvl_edges_out(i,1));
74end
75
76return
Note: See TracBrowser for help on using the repository browser.