Skip to content

Commit e389ba0

Browse files
authored
Merge pull request #123 from valeriupredoi/dev_dpi
Added dpi and savepdf options to comparison report for publications
2 parents b086386 + 558c95e commit e389ba0

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ do_mass_download: <bool>
226226
master_suites: <str>
227227
auto_download: <bool>
228228
strictFileCheck: <bool>
229+
dpi: <int>
230+
savepdf: <bool>
229231
230232
jobs:
231233
<jobID1>:
@@ -268,6 +270,15 @@ These values are:
268270
- `strictFileCheck`:
269271
- A boolean which when True will raise an error if input model files are missing.
270272
- Default is True, set to False to skip this check.
273+
- `dpi`:
274+
- The resolution in dots per inch (dpi) of the output image.
275+
- dpi=100 is fine for most purposes but 300 is needed for highres posters/publications.
276+
- If set to `None`, then the dpi will be set to your default value.
277+
- `savepdf`:
278+
- Output the image as a pdf in addition to the standard png.
279+
- This doesn't replace the image in the report, but saves a pdf version of the image in the images directory.
280+
- The pdfs will be web-visible in the report image directory, but will not linked in the html.
281+
- To view a pdf from a report, simply click the image, and replace the `png` extension with `pdf` in the browser path.
271282
- `jobs`:
272283
- A list of jobIDs, and some options on how they will appear in the final report.
273284
- The options are:

bgcval2/analysis_compare.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ def timeseries_compare(jobs,
192192
labels = {},
193193
ensembles={},
194194
config_user=None,
195+
dpi=None,
196+
savepdf=False,
195197
):
196198
"""
197199
timeseries_compare:
@@ -423,6 +425,8 @@ def timeseries_compare(jobs,
423425
thicknesses=lineThicknesses,
424426
linestyles=linestyles,
425427
labels=labels,
428+
dpi=dpi,
429+
savepdf=savepdf,
426430
)
427431

428432
# Generate a list of comparison images:
@@ -504,6 +508,19 @@ def load_comparison_yml(master_compare_yml_fn):
504508
details['do_mass_download'] = input_yml_dict.get('do_mass_download', False)
505509
details['master_suites'] = input_yml_dict.get('master_suites', [])
506510

511+
# Image output settings:
512+
# dpi: pixels per inch (image resolution)
513+
# savepdf: also save the image as a pdf.
514+
details['dpi'] = input_yml_dict.get('dpi', None)
515+
details['savepdf'] = input_yml_dict.get('savepdf', False)
516+
517+
if details['dpi']: # None is valid!
518+
try:
519+
int(details['dpi'])
520+
except:
521+
raise ValueError(''.join(["Loading yml error: `dpi` needs to be an integer. Current value:",
522+
str(details['dpi'])]))
523+
507524
# auto download, can differ for each job.
508525
auto_download = input_yml_dict.get('auto_download', True)
509526
auto_download_dict = {jobID: auto_download for jobID in details['jobs'].keys()}
@@ -579,6 +596,9 @@ def load_yml_and_run(compare_yml, config_user, skip_timeseries):
579596
suites = details['suites']
580597
auto_download = details['auto_download']
581598
strictFileCheck = details.get('strictFileCheck', True)
599+
dpi = details.get('dpi', None)
600+
savepdf = details.get('savepdf', False)
601+
582602
print('---------------------')
583603
print('timeseries_compare:', analysis_name)
584604
print('job ids:', jobs.keys())
@@ -632,6 +652,9 @@ def load_yml_and_run(compare_yml, config_user, skip_timeseries):
632652
linestyles=linestyles,
633653
labels=labels,
634654
config_user=config_user,
655+
dpi=dpi,
656+
savepdf=savepdf,
657+
635658
)
636659

637660

bgcval2/bgcval2_make_report.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,42 @@ def addImageToHtml(fn, imagesfold, reportdir, debug=True):
8181
"\n\tnewfn:", newfn,
8282
"\n\trelfn:", relfn)
8383

84-
84+
# sending a pdf as well.
85+
pdf_fn = fn.replace('.png', '.pdf')
86+
if os.path.exists(pdf_fn):
87+
new_pdf_fn = newfn.replace('.png', '.pdf')
88+
# Check if file is newer than the one in images.
89+
if shouldIMakeFile(
90+
pdf_fn,
91+
new_pdf_fn,
92+
):
93+
if debug:
94+
print("addImageToHtml:\tAdding new pdf to report.", new_pdf_fn)
95+
if os.path.exists(new_pdf_fn):
96+
os.remove(new_pdf_fn)
97+
shutil.copy2(pdf_fn, new_pdf_fn)
98+
8599
if not os.path.exists(newfn):
86-
if debug: print("addImageToHtml:\tcopytree", fn, newfn)
100+
if debug:
101+
print("addImageToHtml:\tcopytree", fn, newfn)
87102
basedir = folder(os.path.dirname(newfn))
88-
#copytree(fn, newfn)
89103
if os.path.isdir(fn):
90104
shutil.copytree(fn, newfn, symlinks, ignore)
91105
else:
92106
shutil.copy2(fn, newfn)
93107
else:
94108
####
95109
# Check if the newer file is the same one from images.
96-
if os.path.getmtime(fn) == os.path.getmtime(newfn): return relfn
110+
if os.path.getmtime(fn) == os.path.getmtime(newfn):
111+
return relfn
97112
####
98113
# Check if file is newer than the one in images.
99114
if shouldIMakeFile(
100115
fn,
101116
newfn,
102117
):
103-
if debug: print("addImageToHtml:\tremoving old file", fn)
118+
if debug:
119+
print("addImageToHtml:\tremoving old file", fn)
104120
os.remove(newfn)
105121
shutil.copy2(fn, newfn)
106122
if debug:
@@ -1636,7 +1652,6 @@ def newImageLocation(fn):
16361652

16371653
if len(otherFilenames):
16381654
# I think this is never happens anymore.
1639-
assert 0
16401655
href = 'OtherPlots-others'
16411656

16421657
hrefs.append(href)

bgcval2/timeseries/timeseriesPlots.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import matplotlib.patches as mpatches
3636
import cartopy
3737
import numpy as np
38+
import pathlib
39+
3840
from numpy import hanning, hamming, bartlett, blackman
3941
from scipy import interpolate
4042
from collections import defaultdict
@@ -706,6 +708,8 @@ def multitimeseries(
706708
thicknesses=defaultdict(lambda: 1),
707709
linestyles=defaultdict(lambda: '-'),
708710
labels={},
711+
dpi=None,
712+
savepdf=False,
709713
):
710714

711715
if 0 in [len(timesD), len(list(timesD.keys()))]: return
@@ -1053,7 +1057,16 @@ def multitimeseries(
10531057
pyplot.suptitle(title)
10541058

10551059
print("multitimeseries:\tsimpletimeseries:\tSaving:", filename)
1056-
pyplot.savefig(filename)
1060+
if not dpi:
1061+
pyplot.savefig(filename)
1062+
imext = pathlib.Path(filename).suffix
1063+
1064+
# save image as pdf (publication?)
1065+
if savepdf:
1066+
pyplot.savefig(filename.replace(imext, '.pdf'))
1067+
1068+
if pathlib.Path(filename).suffix in ['.png', '.jpg', '.jpeg']:
1069+
pyplot.savefig(filename, dpi=dpi)
10571070
pyplot.close()
10581071

10591072

input_yml/TerraFIRMA_overshoot_runs_minimal.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ strict_file_check: False
1616

1717
clean: True
1818

19+
dpi: 300
20+
savepdf: True
21+
1922
jobs:
2023
u-cs495:
2124
description: 'PI-Control'

0 commit comments

Comments
 (0)