diff options
Diffstat (limited to 'docs/plot')
| -rw-r--r-- | docs/plot/figure_caption.sh | 2 | ||||
| -rw-r--r-- | docs/plot/figure_captions.py | 79 | ||||
| -rwxr-xr-x | docs/plot/greyscale.sh | 10 | ||||
| -rw-r--r-- | docs/plot/plot.py | 23 |
4 files changed, 99 insertions, 15 deletions
diff --git a/docs/plot/figure_caption.sh b/docs/plot/figure_caption.sh new file mode 100644 index 00000000..c30babd6 --- /dev/null +++ b/docs/plot/figure_caption.sh @@ -0,0 +1,2 @@ + +python3 figure_captions.py plotdefs.json ../source/operations/projections/
\ No newline at end of file diff --git a/docs/plot/figure_captions.py b/docs/plot/figure_captions.py new file mode 100644 index 00000000..21aa69d0 --- /dev/null +++ b/docs/plot/figure_captions.py @@ -0,0 +1,79 @@ +import sys +import os +import json +import pathlib +import re + + +def update_caption(image_filename, proj_string, doc_directory, + doc_extension='rst'): + """ + Update the figure caption with a proj-string + + This function assumes that within the "doc_directory" there is a file with + the same basename as the "image_filename" (e.g. for the image "aea.png", + the function will assume that there is a documentation file "aea.rst"). + Within that documentation file, the function will try to find a section + starting with "..figure:: ./images/" and ending with a line like + " proj-string:...". + This line will be replaced with something like + " proj-string: ``+proj=aea``" + + :param filename: The name of the image that's used in the documentation + :param proj_string: The proj-string that was used to generate the image + :param doc_directory: The directory containing the projection documentation .rst files + :return: + """ + + basename, image_extension = os.path.splitext(image_filename) + doc_path = os.path.join(doc_directory, basename + os.extsep + doc_extension) + p = pathlib.Path(doc_path) + + if p.is_file(): + replacement_string = r'\1 proj-string: ``{}``\2'.format(proj_string) + new_doc_content = re.sub( + r'(\.\. figure\:\: \.\/images\/.*?^)[ \t]*proj-string.*?($)', + replacement_string, + p.read_text(), + flags=re.MULTILINE + re.DOTALL) + p.write_text(new_doc_content) + + else: + print("File {} not found.".format(doc_path)) + + +def main(): + """ + Main function of caption replacement script. + + Parses json-file with plot setups and runs the caption replacement + for each plot setup. + """ + + if os.path.exists(sys.argv[1]): + # first argument is the JSON plot definition setup file + with open(sys.argv[1]) as plotsetup: + plotdefs = json.load(plotsetup) + else: + raise ValueError('No plot definition file entered') + + plots = [] + # second argument is the projections documentation directory + outdir = sys.argv[2] + + for i, plotdef in enumerate(plotdefs): + if plots != [] and plotdef['name'] not in plots: + continue + + print(i, plotdef['projstring']) + if 'skip' in plotdef.keys(): + print('skipping') + continue + + update_caption(plotdef['name'], + plotdef['projstring'], + outdir) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/docs/plot/greyscale.sh b/docs/plot/greyscale.sh index 7a6e9b38..4ae861e9 100755 --- a/docs/plot/greyscale.sh +++ b/docs/plot/greyscale.sh @@ -1,7 +1,3 @@ - - -for f in $(ls *.png) -do - - convert $f -fx '(r+g+b)/3' -colorspace Gray $f -done; +# Use Imagemagick to convert all images to grayscale, +# printing progress information +mogrify -monitor -colorspace Gray *.png diff --git a/docs/plot/plot.py b/docs/plot/plot.py index 52bd7fd2..04a3d423 100644 --- a/docs/plot/plot.py +++ b/docs/plot/plot.py @@ -85,7 +85,7 @@ def interp_coords(coords, tol): The function tries to densify a list of coordinates based on a simple measure. If two adjacent points in the input coordinate list are further away from each - other than the specified tolerance, the list will be densied between those two + other than the specified tolerance, the list will be densified between those two points. Roughly speaking, a point will be inserted every `tol` between the points. @@ -230,7 +230,7 @@ def plotproj(plotdef, data, outdir): ''' Plot map. ''' - fig, axes = plt.subplots() + axes = plt.axes() bounds = (plotdef['lonmin'], plotdef['latmin'], plotdef['lonmax'], plotdef['latmax']) for geom in data.filter(bbox=bounds): @@ -288,23 +288,30 @@ def plotproj(plotdef, data, outdir): y = feature[:, 1] axes.plot(x, y, color=COLOR_GRAT, linewidth=0.4) - axes.axis('off') + # Switch off the axis lines... + plt.axis('off') + # ... and additionally switch off the visibility of the axis lines and + # labels so they can be removed by "bbox_inches='tight'" when saving + axes.get_xaxis().set_visible(False) + axes.get_yaxis().set_visible(False) + font = { 'family': 'serif', 'color': 'black', 'style': 'italic', 'size': 12 } - fig.suptitle(plotdef['projstring'], fontdict=font) - axes.autoscale(tight=True) + # Make sure the plot is not stretched + axes.set_aspect('equal') + if not os.path.exists(outdir): os.makedirs(outdir) - fig.savefig(outdir + '/' + plotdef['filename'], dpi=300) + plt.savefig(outdir + '/' + plotdef['filename'], + dpi=400, + bbox_inches='tight') # Clean up - fig = None - del fig axes = None del axes plt.close() |
