source: TOOLS/CMIP6_FORCING/SOLAR/CMIP6_piControl_6bands.m @ 3383

Last change on this file since 3383 was 3383, checked in by oboucher, 7 years ago

Updating the input filenames so they point to the prodigfs repository.

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