import yaml
import os
import matplotlib.pyplot as plt
import numpy as np
wdir = '.' + os.sep + 'working' + os.sep
odir = wdir + 'data' + os.sep
#if not os.path.exists(odir):
# os.system('mkdir -p ' + odir) # makedirs does not work for some reason
if not os.path.exists(odir):
os.makedirs(odir)
res = {}
models = ['MPI-ESM-LR', 'GFDL', 'MPI-ESM-MR']
observations = ['GlobSnow', 'GlobAlbedo', 'CCI-Aerosol']
modres = []
for p in models:
x = {}
obsdiff = []
f = plt.figure()
ax = f.add_subplot(111)
d = np.random.random((10,20))
ax.imshow(d, interpolation='nearest')
oname_png = p + '.png'
oname_gif = p + '.jpg'
ax.set_title(oname_png)
f.savefig(odir + oname_png)
ax.set_title(oname_gif)
f.savefig(odir + oname_gif)
for o in observations:
oname_obs = 'difference_' + p.upper() + '_' + o.upper() + '.png'
ax.set_title(oname_obs)
f.savefig(odir + oname_obs)
obsdiff.append({o : {'file' : oname_obs, 'caption' : 'This is a testcaption using model ' + p + ' and observation ' + o}})
x.update({'graphic1' : oname_png})
x.update({'graphic_test' : oname_gif})
x.update({'observations' : obsdiff})
modres.append({p : x}) # note that it is important to have a list here to ensure that the sequence information is preserved
del x
del obsdiff
res.update({'models' : modres})
# save results to interface file
dicname_s = odir + 'sea_ice.yml'
dicname_a = odir + 'albedo.yml'
if os.path.exists(dicname_s):
os.remove(dicname_s)
if os.path.exists(dicname_a):
os.remove(dicname_a)
yaml.dump(res, stream=open(dicname_s,'w'), default_flow_style=False) # same dictionaries are here used only for testing
yaml.dump(res, stream=open(dicname_a,'w'), default_flow_style=False)
!ls $odir
albedo.yml GFDL.jpg difference_GFDL_CCI-AEROSOL.png GFDL.png difference_GFDL_GLOBALBEDO.png MPI-ESM-LR.jpg difference_GFDL_GLOBSNOW.png MPI-ESM-LR.png difference_MPI-ESM-LR_CCI-AEROSOL.png MPI-ESM-MR.jpg difference_MPI-ESM-LR_GLOBALBEDO.png MPI-ESM-MR.png difference_MPI-ESM-LR_GLOBSNOW.png my_albedo_test.rst difference_MPI-ESM-MR_CCI-AEROSOL.png my_sea_ice_test.rst difference_MPI-ESM-MR_GLOBALBEDO.png sea_ice.yml difference_MPI-ESM-MR_GLOBSNOW.png
# reload interface file
o = yaml.load(open(dicname_s, 'r'))
print(o)
{'models': [{'MPI-ESM-LR': {'graphic_test': 'MPI-ESM-LR.jpg', 'observations': [{'GlobSnow': {'caption': 'This is a testcaption using model MPI-ESM-LR and observation GlobSnow', 'file': 'difference_MPI-ESM-LR_GLOBSNOW.png'}}, {'GlobAlbedo': {'caption': 'This is a testcaption using model MPI-ESM-LR and observation GlobAlbedo', 'file': 'difference_MPI-ESM-LR_GLOBALBEDO.png'}}, {'CCI-Aerosol': {'caption': 'This is a testcaption using model MPI-ESM-LR and observation CCI-Aerosol', 'file': 'difference_MPI-ESM-LR_CCI-AEROSOL.png'}}], 'graphic1': 'MPI-ESM-LR.png'}}, {'GFDL': {'graphic_test': 'GFDL.jpg', 'observations': [{'GlobSnow': {'caption': 'This is a testcaption using model GFDL and observation GlobSnow', 'file': 'difference_GFDL_GLOBSNOW.png'}}, {'GlobAlbedo': {'caption': 'This is a testcaption using model GFDL and observation GlobAlbedo', 'file': 'difference_GFDL_GLOBALBEDO.png'}}, {'CCI-Aerosol': {'caption': 'This is a testcaption using model GFDL and observation CCI-Aerosol', 'file': 'difference_GFDL_CCI-AEROSOL.png'}}], 'graphic1': 'GFDL.png'}}, {'MPI-ESM-MR': {'graphic_test': 'MPI-ESM-MR.jpg', 'observations': [{'GlobSnow': {'caption': 'This is a testcaption using model MPI-ESM-MR and observation GlobSnow', 'file': 'difference_MPI-ESM-MR_GLOBSNOW.png'}}, {'GlobAlbedo': {'caption': 'This is a testcaption using model MPI-ESM-MR and observation GlobAlbedo', 'file': 'difference_MPI-ESM-MR_GLOBALBEDO.png'}}, {'CCI-Aerosol': {'caption': 'This is a testcaption using model MPI-ESM-MR and observation CCI-Aerosol', 'file': 'difference_MPI-ESM-MR_CCI-AEROSOL.png'}}], 'graphic1': 'MPI-ESM-MR.png'}}]}
!cat $dicname_s
models: - MPI-ESM-LR: graphic1: MPI-ESM-LR.png graphic_test: MPI-ESM-LR.jpg observations: - GlobSnow: caption: This is a testcaption using model MPI-ESM-LR and observation GlobSnow file: difference_MPI-ESM-LR_GLOBSNOW.png - GlobAlbedo: caption: This is a testcaption using model MPI-ESM-LR and observation GlobAlbedo file: difference_MPI-ESM-LR_GLOBALBEDO.png - CCI-Aerosol: caption: This is a testcaption using model MPI-ESM-LR and observation CCI-Aerosol file: difference_MPI-ESM-LR_CCI-AEROSOL.png - GFDL: graphic1: GFDL.png graphic_test: GFDL.jpg observations: - GlobSnow: caption: This is a testcaption using model GFDL and observation GlobSnow file: difference_GFDL_GLOBSNOW.png - GlobAlbedo: caption: This is a testcaption using model GFDL and observation GlobAlbedo file: difference_GFDL_GLOBALBEDO.png - CCI-Aerosol: caption: This is a testcaption using model GFDL and observation CCI-Aerosol file: difference_GFDL_CCI-AEROSOL.png - MPI-ESM-MR: graphic1: MPI-ESM-MR.png graphic_test: MPI-ESM-MR.jpg observations: - GlobSnow: caption: This is a testcaption using model MPI-ESM-MR and observation GlobSnow file: difference_MPI-ESM-MR_GLOBSNOW.png - GlobAlbedo: caption: This is a testcaption using model MPI-ESM-MR and observation GlobAlbedo file: difference_MPI-ESM-MR_GLOBALBEDO.png - CCI-Aerosol: caption: This is a testcaption using model MPI-ESM-MR and observation CCI-Aerosol file: difference_MPI-ESM-MR_CCI-AEROSOL.png
Now we have generated a directory with sample files that can be read using smartreport
Note, that if you include images in your report, then the data directory where you have put the data should be the same as the report directory. Otherwise the link to the images will not be correct!
from easyreport import Report
template_seaice = 'seaice_template.rst' # name of template file
template_albedo = 'albedo_template.rst'
interface_file_s = odir + 'sea_ice.yml' # file which is the result of the processing above
interface_file_a = odir + 'albedo.yml' # file which is the result of the processing above
suffix = 'rst' # format to write results to
R = Report(template_seaice, interface_file_s, suffix=suffix, output_directory=odir, output_file='my_sea_ice_test.rst')
R.parse() # parse interface data
R1 = Report(template_albedo, interface_file_a, suffix=suffix, output_directory=odir, output_file='my_albedo_test.rst')
R1.parse() # parse interface data
# generate SPHINX files (run option False means that only files are generated, but no index.rst is created)
R.compile(run=False)
R1.compile(run=False)
# no run sphinx by providing a list of all files to be processed
R1._run_sphinx([R.output_file, R1.output_file])
--------------------------------------------------------------------------- IOError Traceback (most recent call last) <ipython-input-5-0affbc9d5590> in <module>() 18 19 # no run sphinx by providing a list of all files to be processed ---> 20 R1._run_sphinx([R.output_file, R1.output_file]) /home/m300028/shared/dev/svn/easyreport/easyreport/report.pyc in _run_sphinx(self, flist) 102 """ 103 --> 104 self._gen_sphinx_template(flist) 105 curdir=os.curdir 106 os.chdir(self.output_directory) /home/m300028/shared/dev/svn/easyreport/easyreport/report.pyc in _gen_sphinx_template(self, flist) 122 123 # modify index file --> 124 F = open(self.output_directory + 'index_template.rst') 125 ofile = self.output_directory + 'index.rst' 126 if os.path.exists(ofile): IOError: [Errno 2] No such file or directory: './working/data/index_template.rst'