source: TOOLS/CMIP6_FORCING/SOLAR/CMIP6_historical_2bands.m @ 3595

Last change on this file since 3595 was 3595, checked in by tlurton, 4 years ago

20/02/2018 Th. Lurton
Updated paths to final location of input files on Ciclad: /prodigfs/project/input4MIPs/.

File size: 12.4 KB
Line 
1% Updated 05/02/2018, ThL
2% * changed paths to retrieve new version of input data;
3% * version considered is now 3.2.
4
5% MATLAB run script to integrate spectral solar irradiance (SSI) data
6% recommended and provided by SPARC/SOLARIS-HEPPA for use in CMIP6 to
7% spectral bands of any desired resolution (according to
8% radiation/photolysis scheme of employed (chemistry) climate model)
9%
10% Author: Tim Kruschke (tkruschke[at]geomar.de)
11% Last modified: 02 May 2016 (compliance with solarforcing_*_*_3.1.nc)
12%
13% Description:
14%    1. reading data from netcdf-file as provided by SPARC/SOLARIS-HEPPA
15%    2. reading definitions of spectral bands (user-specified by creating an
16%       ascii-textfile according to specific formatting, as described below)
17%    3. calling MATLAB-function "regrid_ssi" (provided with this run script as
18%       part of a joint zip-archive) to integrate SSI over specified bands
19%    4. writing new netcdf-file with same structure and attributes as original
20%       (input) file, but only with (unchanged) total solar irradiance
21%       (TSI), SSI integrated over spectral bands specified by user, and
22%       irradiance fraction (of TSI) in respective band (plus
23%       necessary/related dimensions/variables time, wlen, wlenbinsize)
24%
25% Formatting of ascii-textfile, containing definitions of spectral bands:
26%    NO HEADER LINES, NO UNITS, NO CHARACTER DELIMITERS
27%    It's as simple as this:
28%    - one line per spectral band
29%    - each line contains two floating point numbers: lower and upper edge
30%      of respective spectral band (in nanometers!!!), separated by
31%      blank space(s) or tabulator(s)
32%    See example_spectral_bands.dat (provided with this run script as
33%    part of a joint zip-archive) as an example.
34%--------------------------------------------------------------------------
35
36%  execute with run CMIP6.m
37
38%% User-defined paths and filenames
39
40% path to directory of input data; adapt to your file structure
41dir_in=['.'];
42
43% path to directory of output data; adapt to your file structure if desired
44% to differ from input directory
45dir_out=dir_in;
46
47% filename of ascii-textfile (to be placed in dir_in) containing
48% definitions of spectral bands (see explanations in script header);
49% adapt to name of your band definition file
50%filename_spectral_bands='example_spectral_bands.dat';
51%filename_spectral_bands='example_spectral_bands_6_SSI_FRAC=1.dat';
52filename_spectral_bands='example_spectral_bands_2_SSI_FRAC=1.dat';
53
54% filename of original data file, provided by SPARC/SOLARIS-HEPPA (to be
55% placed in dir_in); adapt to respective file that is to be considered
56%filename_in='solarforcing_ref_mon_3.1.nc';
57filename_in='/prodigfs/project/input4MIPs/CMIP6/CMIP/SOLARIS-HEPPA/SOLARIS-HEPPA-3-2/atmos/day/multiple/gz/v20170103/solarforcing-ref-day_input4MIPs_solar_CMIP_SOLARIS-HEPPA-3-2_gn_18500101-22991231.nc';
58
59% lines 53-54 only used to match input and output filenames
60% ATTENTION: works only on unix platforms; if run on other platforms,
61% delete/comment following two lines and enter complete filename_out in
62% l. 59
63[~,filename_in_base]=unix(['basename ' dir_in filesep filename_in ' .nc']);
64filename_in_base=strcat(filename_in_base);
65
66% filename of new file to be created, containing SSI data in spectral
67% resolutiuon as desired by the user; adapt to whatever name you like if
68% not desired to match input filename
69%filename_out=[filename_in_base '_SSI_regridded.nc'];
70filename_out=[filename_in_base '_SSI_regridded_2_TSI_anomaly.nc'];
71
72%% Usually, nothing should be edited below this line!!!!!!!!!!!!!!!!!!!!!!!
73
74ncid_in=netcdf.open([dir_in filesep filename_in],'NOWRITE');
75ncid_out=netcdf.create(strcat([dir_out filesep filename_out]),'CLOBBER'); % existing files are overwritten
76
77% adapting title of original file
78nc_title=netcdf.getAtt(ncid_in,-1,'title');
79nc_title=['Spectral solar irradiance (SSI) of ' nc_title ', converted to custom spectral resolution'];
80netcdf.putAtt(ncid_out,-1,'title',nc_title);
81
82%% time stamp
83netcdf.putAtt(ncid_out,-1,'creation_date',datestr(now)); % creates a time stamp, though not compliant with ISO 8601
84%c% You can use the following lines (commented out) instead to generate a
85%c% time stamp compliant to ISO 8601; change last digits according to
86%c% your time zone in this case (currently +02:00 for CEST)
87%     create_date_vec=datevec(now);
88%     create_date_str=cell(1,6);
89%     for id=1:6;
90%         if create_date_vec(id)<10;
91%             create_date_str{1,id}=['0' num2str(round(create_date_vec(id)))];
92%         else
93%             create_date_str{1,id}=num2str(round(create_date_vec(id)));
94%         end
95%     end
96%     netcdf.putAtt(ncid_out,-1,'creation_date',[create_date_str{1,1} '-' create_date_str{1,2} '-' ...
97%         create_date_str{1,3} 'T' create_date_str{1,4} ':' create_date_str{1,5} ':' create_date_str{1,6} '+02:00'])
98%%
99
100% copy global attributes to new file
101netcdf.copyAtt(ncid_in,-1,'institution',ncid_out,-1)
102netcdf.copyAtt(ncid_in,-1,'institution_id',ncid_out,-1)
103netcdf.copyAtt(ncid_in,-1,'activity_id',ncid_out,-1)
104netcdf.copyAtt(ncid_in,-1,'comment',ncid_out,-1)
105netcdf.copyAtt(ncid_in,-1,'time_coverage_start',ncid_out,-1)
106netcdf.copyAtt(ncid_in,-1,'time_coverage_end',ncid_out,-1)
107netcdf.copyAtt(ncid_in,-1,'frequency',ncid_out,-1)
108netcdf.copyAtt(ncid_in,-1,'source',ncid_out,-1)
109netcdf.copyAtt(ncid_in,-1,'source_id',ncid_out,-1)
110netcdf.copyAtt(ncid_in,-1,'realm',ncid_out,-1)
111netcdf.copyAtt(ncid_in,-1,'further_info_url',ncid_out,-1)
112netcdf.copyAtt(ncid_in,-1,'metadata_url',ncid_out,-1)
113netcdf.copyAtt(ncid_in,-1,'contributor_name',ncid_out,-1)
114netcdf.copyAtt(ncid_in,-1,'references',ncid_out,-1)
115netcdf.copyAtt(ncid_in,-1,'Conventions',ncid_out,-1)
116netcdf.putAtt(ncid_out,-1,'history',['Original SSI data from file ' filename_in ' integrated ' ...
117    'over spectral bands as defined in file ' filename_spectral_bands ' ' ...
118    'by MATLAB run script CMIP6_SSI_convert_resolution_run.m, using function regrid_ssi.m']);
119
120% reading SSI-related variables from CMIP6 input file
121wvl_in_ID=netcdf.inqVarID(ncid_in,'wlen');
122wvl_edges_in_ID=netcdf.inqVarID(ncid_in,'wlen_bnds');
123bw_in_ID=netcdf.inqVarID(ncid_in,'wlenbinsize');
124time_in_ID=netcdf.inqVarID(ncid_in,'time');
125time_bnds_in_ID=netcdf.inqVarID(ncid_in,'time_bnds');
126year_in_ID=netcdf.inqVarID(ncid_in,'calyear');
127month_in_ID=netcdf.inqVarID(ncid_in,'calmonth');
128day_in_ID=netcdf.inqVarID(ncid_in,'calday');
129tsi_in_ID=netcdf.inqVarID(ncid_in,'tsi');
130ssi_in_ID=netcdf.inqVarID(ncid_in,'ssi');
131
132wvl_in=netcdf.getVar(ncid_in,wvl_in_ID);
133wvl_edges_in=(netcdf.getVar(ncid_in,wvl_edges_in_ID))';
134bw_in=netcdf.getVar(ncid_in,bw_in_ID);
135ssi_in=netcdf.getVar(ncid_in,ssi_in_ID);
136tsi_in=netcdf.getVar(ncid_in,tsi_in_ID);
137time_in=netcdf.getVar(ncid_in,time_in_ID);
138time_bnds_in=netcdf.getVar(ncid_in,time_bnds_in_ID);
139year_in=netcdf.getVar(ncid_in,year_in_ID);
140month_in=netcdf.getVar(ncid_in,month_in_ID);
141day_in=netcdf.getVar(ncid_in,day_in_ID);
142
143% read user specified definitions of spectral bands
144fid=fopen([dir_in filesep filename_spectral_bands]);
145tmp=textscan(fid,'%f %f');
146fclose(fid);
147wvl_edges_out=tmp{1,1};
148wvl_edges_out(:,2)=tmp{1,2};
149clear tmp
150
151% calculation of output central wave lengths
152wvl_out=sum(wvl_edges_out,2)/2;
153
154% calculation of output bin width
155bw_out=wvl_edges_out(:,2)-wvl_edges_out(:,1);
156
157% check for consistency of output bin definitions
158if min(bw_out)<0;
159    error(['Error: For at least one spectral band, the specified upper edge \n' ...
160        '(right column) in your input file ' filename_in '\n ' ...
161        'is smaller than the respective lower edge (left column).'])
162end
163
164% check for spectral coverage of input data with respect to desired output
165% bins
166if min(wvl_edges_out(:,1))<min(wvl_edges_in(:,1)) || max(wvl_edges_out(:,2))>max(wvl_edges_in(:,2));
167    warning(['You are extrapolating into spectral ranges not covered by the input spectrum! ' ...
168        'This routine supports this by linearly extrapolating based on change between last two bins. ' ...
169        'However, this might be physically inappropriate!']);
170end
171
172% Integration of SSI data for each specified spectral band
173% (regrid_ssi.m calculates weighted means of SSI per nm, this is multiplied by binwidth)
174ssi_out=bsxfun(@times,regrid_ssi(ssi_in,wvl_edges_in,wvl_edges_out),bw_out);
175
176% Calculation of solar irradiance fraction (compared to total solar
177% irradiance) in each band
178ssi_frac_out=bsxfun(@rdivide,ssi_out,tsi_in');
179
180% Calculation the average value of tsi
181avtsi=mean(tsi_in)
182disp (avtsi)
183
184%Calculate the anomaly of tsi
185antsi(:,1)=tsi_in(:,1)-avtsi
186
187disp (antsi)
188
189format longEng
190disp (avtsi)
191
192% writing data (including dimensions) to output file
193  % dimension definitions
194time_dimID=netcdf.defDim(ncid_out,'time',0);
195wvl_dimID=netcdf.defDim(ncid_out,'wlen',length(wvl_out));
196bounds_dimID=netcdf.defDim(ncid_out,'nbd',2);
197
198  % variable definitions
199time_out_ID=netcdf.defVar(ncid_out,'time','double',time_dimID);
200time_bnds_out_ID=netcdf.defVar(ncid_out,'time_bnds','double',[bounds_dimID time_dimID]);
201wvl_out_ID=netcdf.defVar(ncid_out,'wlen','double',wvl_dimID);
202wvl_edges_out_ID=netcdf.defVar(ncid_out,'wlen_bnds','double',[wvl_dimID bounds_dimID]);
203bw_out_ID=netcdf.defVar(ncid_out,'wlenbinsize','double',wvl_dimID);
204year_out_ID=netcdf.defVar(ncid_out,'calyear','double',time_dimID);
205month_out_ID=netcdf.defVar(ncid_out,'calmonth','double',time_dimID);
206day_out_ID=netcdf.defVar(ncid_out,'calday','double',time_dimID);
207tsi_out_ID=netcdf.defVar(ncid_out,'tsi','float',time_dimID);
208antsi_out_ID=netcdf.defVar(ncid_out,'antsi','float',time_dimID);
209ssi_out_ID=netcdf.defVar(ncid_out,'ssi','float',[wvl_dimID time_dimID]);
210ssi_frac_out_ID=netcdf.defVar(ncid_out,'ssi_frac','float',[wvl_dimID time_dimID]);
211
212  % copy variable attributes from input to output file
213netcdf.copyAtt(ncid_in,time_in_ID,'axis',ncid_out,time_out_ID)
214netcdf.copyAtt(ncid_in,time_in_ID,'long_name',ncid_out,time_out_ID)
215netcdf.copyAtt(ncid_in,time_in_ID,'standard_name',ncid_out,time_out_ID)
216netcdf.copyAtt(ncid_in,time_in_ID,'units',ncid_out,time_out_ID)
217netcdf.copyAtt(ncid_in,time_in_ID,'calendar',ncid_out,time_out_ID)
218netcdf.copyAtt(ncid_in,time_in_ID,'_CoordinateAxisType',ncid_out,time_out_ID)
219netcdf.copyAtt(ncid_in,time_in_ID,'bounds',ncid_out,time_out_ID)
220
221netcdf.copyAtt(ncid_in,time_bnds_in_ID,'long_name',ncid_out,time_bnds_out_ID)
222netcdf.copyAtt(ncid_in,time_bnds_in_ID,'units',ncid_out,time_bnds_out_ID)
223
224netcdf.copyAtt(ncid_in,wvl_in_ID,'standard_name',ncid_out,wvl_out_ID)
225netcdf.copyAtt(ncid_in,wvl_in_ID,'long_name',ncid_out,wvl_out_ID)
226netcdf.copyAtt(ncid_in,wvl_in_ID,'units',ncid_out,wvl_out_ID)
227netcdf.copyAtt(ncid_in,wvl_in_ID,'bounds',ncid_out,wvl_out_ID)
228
229netcdf.copyAtt(ncid_out,wvl_edges_in_ID,'long_name',ncid_out,wvl_edges_out_ID)
230netcdf.copyAtt(ncid_out,wvl_edges_in_ID,'units',ncid_out,wvl_edges_out_ID)
231
232netcdf.copyAtt(ncid_in,bw_in_ID,'long_name',ncid_out,bw_out_ID)
233netcdf.copyAtt(ncid_in,bw_in_ID,'units',ncid_out,bw_out_ID)
234
235netcdf.copyAtt(ncid_in,year_in_ID,'long_name',ncid_out,year_out_ID)
236
237netcdf.copyAtt(ncid_in,month_in_ID,'long_name',ncid_out,month_out_ID)
238
239netcdf.copyAtt(ncid_in,day_in_ID,'long_name',ncid_out,day_out_ID)
240
241netcdf.copyAtt(ncid_in,tsi_in_ID,'standard_name',ncid_out,tsi_out_ID)
242netcdf.copyAtt(ncid_in,tsi_in_ID,'long_name',ncid_out,tsi_out_ID)
243netcdf.copyAtt(ncid_in,tsi_in_ID,'units',ncid_out,tsi_out_ID)
244netcdf.copyAtt(ncid_in,tsi_in_ID,'cell_methods',ncid_out,tsi_out_ID)
245
246netcdf.putAtt(ncid_out,antsi_out_ID,'standard_name','total solar_irradiance anomaly')
247netcdf.putAtt(ncid_out,antsi_out_ID,'long_name','reconstructed total solar irradiance anomaly at 1 AU')
248netcdf.putAtt(ncid_out,antsi_out_ID,'units','W m^-2')
249
250netcdf.putAtt(ncid_out,ssi_out_ID,'long_name','reconstructed solar irradiance at 1 AU within respective bands')
251netcdf.putAtt(ncid_out,ssi_out_ID,'units','W m^-2')
252
253netcdf.putAtt(ncid_out,ssi_frac_out_ID,'long_name','reconstructed fraction of total solar irradiance at 1 AU within respective bands')
254netcdf.putAtt(ncid_out,ssi_frac_out_ID,'units','1')
255
256netcdf.close(ncid_in);
257netcdf.endDef(ncid_out);
258
259netcdf.putVar(ncid_out,time_out_ID,0,length(time_in),time_in)
260netcdf.putVar(ncid_out,time_bnds_out_ID,time_bnds_in)
261netcdf.putVar(ncid_out,wvl_out_ID,wvl_out)
262netcdf.putVar(ncid_out,wvl_edges_out_ID,wvl_edges_out)
263netcdf.putVar(ncid_out,bw_out_ID,bw_out)
264netcdf.putVar(ncid_out,year_out_ID,year_in)
265netcdf.putVar(ncid_out,month_out_ID,month_in)
266netcdf.putVar(ncid_out,day_out_ID,day_in)
267netcdf.putVar(ncid_out,tsi_out_ID,tsi_in)
268netcdf.putVar(ncid_out,antsi_out_ID,antsi)
269netcdf.putVar(ncid_out,ssi_out_ID,ssi_out)
270netcdf.putVar(ncid_out,ssi_frac_out_ID,ssi_frac_out)
271
272netcdf.sync(ncid_out);
273netcdf.close(ncid_out);
Note: See TracBrowser for help on using the repository browser.