1#!/usr/bin/env bash
2#
3# This script takes the downloaded zip content from
4# Crameri, Fabio. (2021, September 12). Scientific colour maps
5# (Version 7.0.1). Zenodo. https://doi.org/10.5281/zenodo.5501399
6# and converts the *.cpt files into proper GMT master
7# CPT files with correct attribution and hinge info
8# Run from the ScientificColourMapsV directory (V is version) after the
9# zip has downloaded and been expanded.  But first you need to manually
10# update the /tmp/cpt.info entries below with one line per CPT subdir in the
11# downloaded directory.  It will create a gmt subdirectory with all the CPTs.
12# You also need to edit gmt_cpt_masters.h after adding the CPTs to share/cpt
13#
14# Last setup and run for ScientificColourMaps7 on 20/09/2021 for GMT 6.3 (master)
15# Gave 51 CPTS: The same as prior release with minor changes for categorical cpts.
16#
17
18if [ $# -eq 0 ]; then
19	cat <<- EOF  >&2
20	Usage: build-scientific-colors-cpt.sh <SCM-directory>
21
22	Will create the GMT CPT versions of Crameri's scientific colour maps.
23	Give the full path to the expanded zip file top directory, such as
24	~/Download/ScientificColourMaps7.
25	Before running you must update this script with:
26	  1. Any new CPT entries since the last release to /tmp/cpt.info
27	  2. Flag those with a soft hinge as S and a hard hinge as H
28	  3. Manually set the current version number/doi (see the zip PDF docs)
29	Afterwards you must:
30	  1. Update src/gmt_cpt_masters.h with any new entries (copy lines from /tmp/cpt_strings.txt)
31	  2. Adding the CPTs to share/cpt (overwriting the previous versions)
32	  3. Probably mess with doc/scripts/GMT_App_M*.sh for new layouts
33	EOF
34	exit 1
35fi
36
37DIR=$1
38VERSION=7.0.1
39cat << EOF > /tmp/cpt.info
40acton|Perceptually uniform sequential colormap, by Fabio Crameri [C=RGB]
41actonS|Perceptually uniform sequential categorical colormap, by Fabio Crameri [C=RGB]
42bam|Perceptually uniform bimodal colormap, light, by Fabio Crameri [C=RGB]
43bamO|Perceptually uniform bimodal cyclic colormap, light, by Fabio Crameri [C=RGB]
44bamako|Perceptually uniform, low-lightness gradient colormap by Fabio Crameri [C=RGB]
45bamakoS|Perceptually uniform, low-lightness gradient categorical colormap by Fabio Crameri [C=RGB]
46batlow|Perceptually uniform 'rainbow' colormap by Fabio Crameri [C=RGB]
47batlowK|Perceptually uniform 'rainbow' colormap with black ending by Fabio Crameri [C=RGB]
48batlowS|Perceptually uniform 'rainbow' categorical colormap by Fabio Crameri [C=RGB]
49batlowW|Perceptually uniform 'rainbow' colormap with white ending by Fabio Crameri [C=RGB]
50berlin|Perceptually uniform bimodal colormap, dark, by Fabio Crameri [S,C=RGB]
51bilbao|Perceptually uniform colormap by Fabio Crameri [C=RGB]
52bilbaoS|Perceptually uniform categorical colormap by Fabio Crameri [C=RGB]
53broc|Perceptually uniform bimodal colormap, light, by Fabio Crameri [S,C=RGB]
54brocO|Perceptually uniform bimodal cyclic colormap, light, by Fabio Crameri [C=RGB]
55buda|Perceptually uniform, low-lightness gradient colormap, by Fabio Crameri [C=RGB]
56budaS|Perceptually uniform, low-lightness gradient categorical colormap, by Fabio Crameri [C=RGB]
57bukavu|Perceptually uniform multi-sequential colormap by Fabio Crameri [H,C=RGB]
58cork|Perceptually uniform bimodal colormap, light, by Fabio Crameri [S,C=RGB]
59corkO|Perceptually uniform bimodal cyclic colormap, light, by Fabio Crameri [C=RGB]
60davos|Perceptually uniform colormap by Fabio Crameri [C=RGB]
61davosS|Perceptually uniform categorical colormap by Fabio Crameri [C=RGB]
62devon|Perceptually uniform sequential colormap, by Fabio Crameri [C=RGB]
63devonS|Perceptually uniform sequential categorical colormap, by Fabio Crameri [C=RGB]
64fes|Perceptually uniform multi-sequential colormap by Fabio Crameri [H,C=RGB]
65grayC|Perceptually uniform 'gray' colormap by Fabio Crameri [C=RGB]
66grayCS|Perceptually uniform 'gray' categorical colormap by Fabio Crameri [C=RGB]
67hawaii|Perceptually uniform lush colormap by Fabio Crameri [C=RGB]
68hawaiiS|Perceptually uniform lush categorical colormap by Fabio Crameri [C=RGB]
69imola|Perceptually uniform, low-lightness gradient colormap, by Fabio Crameri [C=RGB]
70imolaS|Perceptually uniform, low-lightness gradient categorical colormap, by Fabio Crameri [C=RGB]
71lajolla|Perceptually uniform colormap, without black or white, by Fabio Crameri [C=RGB]
72lajollaS|Perceptually uniform categorical colormap, without black or white, by Fabio Crameri [C=RGB]
73lapaz|Perceptually uniform 'rainbow' colormap by Fabio Crameri [C=RGB]
74lapazS|Perceptually uniform 'rainbow' categorical colormap by Fabio Crameri [C=RGB]
75lisbon|Perceptually uniform bimodal colormap, dark, by Fabio Crameri [S,C=RGB]
76nuuk|Perceptually uniform, low-lightness gradient colormap, by Fabio Crameri [C=RGB]
77nuukS|Perceptually uniform, low-lightness gradient categorical colormap, by Fabio Crameri [C=RGB]
78oleron|Perceptually uniform topography colormap, by Fabio Crameri [H,C=RGB]
79oslo|Perceptually uniform, B&W limits, by Fabio Crameri [C=RGB]
80osloS|Perceptually uniform, B&W limits, categorical colormap, by Fabio Crameri [C=RGB]
81roma|Perceptually uniform 'seis' colormap by Fabio Crameri [S,C=RGB]
82romaO|Perceptually uniform cyclic colormap by Fabio Crameri [C=RGB]
83tofino|Perceptually uniform bimodal colormap, dark, by Fabio Crameri [S,C=RGB]
84tokyo|Perceptually uniform colormap without black or white, by Fabio Crameri [C=RGB]
85tokyoS|Perceptually uniform categorical colormap without black or white, by Fabio Crameri [C=RGB]
86turku|Perceptually uniform colormap by Fabio Crameri [C=RGB]
87turkuS|Perceptually uniform categorical colormap by Fabio Crameri [C=RGB]
88vanimo|Perceptually uniform bimodal colormap, dark, by Fabio Crameri [C=RGB]
89vik|Perceptually uniform bimodal colormap, light, by Fabio Crameri [S,C=RGB]
90vikO|Perceptually uniform bimodal cyclic colormap, light, by Fabio Crameri [C=RGB]
91EOF
92here=`pwd`
93cd $DIR
94# Make formatted list of lines suitable for copying into gmt_cpt_masters.h
95awk -F'|' '{printf "\"%-10s  : %s\",\n", $1, $2}' /tmp/cpt.info > /tmp/cpt_strings.txt
96# Make list of CPTs with a hinge of some soft since these need to insert a true z = 0 slice
97grep "\[H," /tmp/cpt.info | awk -F'|' '{print $1}' > /tmp/hinge.lis
98grep "\[S," /tmp/cpt.info | awk -F'|' '{print $1}' >> /tmp/hinge.lis
99
100rm -rf gmt_cpts
101mkdir gmt_cpts
102cat <<- EOF > /tmp/front
103#
104#----------------------------------------------------------
105# COLOR_MODEL = RGB
106EOF
107while read line; do
108	cpt=`echo $line | awk -F'|' '{print $1}'`
109	cat <<- EOF > gmt_cpts/$cpt.cpt
110	#
111	EOF
112	echo $line | awk -F'|' '{printf "# %s\n", $2}' >> gmt_cpts/$cpt.cpt
113	last_char=$(echo $cpt | awk '{print substr($1,length($1),1)}')
114	if [ "X${last_char}" = "XS" ]; then
115		tmp=$(echo $cpt | awk '{print substr($1,1, length($1)-1)}')
116		cptdir=${tmp}/CategoricalPalettes
117	else
118		cptdir=${cpt}
119	fi
120	cat <<- EOF >> gmt_cpts/$cpt.cpt
121	#
122	#	www.fabiocrameri.ch/visualisation
123	#
124	# License: MIT License
125	# Copyright (c) 2021, Fabio Crameri.
126	# Crameri, F., (2021). Scientific colour maps. Zenodo. https://zenodo.org/record/5501399
127	# This is Scientific Colour Maps version $VERSION
128	# Note: Original file converted to GMT version >= 5 CPT format.
129	EOF
130	#if [ "$cpt" = "broc" ] || [ "$cpt" = "cork" ] || [ "$cpt" = "vik" ] || [ "$cpt" = "lisbon" ] || [ "$cpt" = "tofino" ] || [ "$cpt" = "berlin" ] || [ "$cpt" = "oleron" ] ; then
131	if [ $(echo $line | grep -c "\[H") -eq 1 ]; then
132		hinge="HARD_HINGE"
133	elif [ $(echo $line | grep -c "\[S") -eq 1 ]; then
134		hinge="SOFT_HINGE"
135	else
136		hinge=""
137	fi
138	if [ "X${last_char}" = "XS" ]; then
139		cat /tmp/front >> gmt_cpts/$cpt.cpt
140		echo "#----------------------------------------------------------" >> gmt_cpts/$cpt.cpt
141		egrep -v '^#|^F|^B|^N' $cptdir/$cpt.cpt | awk '{if (NR == 1) { printf "%d\t%s/%s/%s\n%d\t%s/%s/%s\n", 0, $2, $3, $4, 1, $6, $7, $8} else {printf "%d\t%s/%s/%s\n", NR+1, $6, $7, $8}}' > /tmp/tmp.cpt
142	elif [ "X$hinge" = "X" ]; then
143		cat /tmp/front >> gmt_cpts/$cpt.cpt
144		if [ "X${last_char}" = "XO" ]; then
145			echo "# CYCLIC" >> gmt_cpts/$cpt.cpt
146		fi
147		echo "#----------------------------------------------------------" >> gmt_cpts/$cpt.cpt
148		egrep -v '^#|^F|^B|^N' $cptdir/$cpt.cpt | awk '{printf "%.6f\t%s/%s/%s\t%.6f\t%s/%s/%s\n", $1, $2, $3, $4, $5, $6, $7, $8}' > /tmp/tmp.cpt
149	else
150		echo "# Note: Range changed from 0-1 to -1/+1 to place hinge at zero." >> gmt_cpts/$cpt.cpt
151		cat /tmp/front >> gmt_cpts/$cpt.cpt
152		echo "# $hinge" >> gmt_cpts/$cpt.cpt
153		echo "#----------------------------------------------------------" >> gmt_cpts/$cpt.cpt
154		# Convert to -1/1 range
155		egrep -v '^#|^F|^B|^N' $cptdir/$cpt.cpt | awk '{printf "%.6f\t%s/%s/%s\t%.6f\t%s/%s/%s\n", 2*($1-0.5), $2, $3, $4, 2*($5-0.5), $6, $7, $8}' > /tmp/tmp.cpt
156	fi
157	cat /tmp/tmp.cpt >> gmt_cpts/$cpt.cpt
158	if [ "X${last_char}" = "XS" ] || [ "X${last_char}" = "XO" ]; then	# Categorical or cyclical CPTS have no F or B, only NaN
159		egrep '^N' $cptdir/$cpt.cpt | awk '{printf "%s\t%s/%s/%s\n", $1, $2, $3, $4}' >> gmt_cpts/$cpt.cpt
160	else
161		egrep '^F|^B|^N' $cptdir/$cpt.cpt | awk '{printf "%s\t%s/%s/%s\n", $1, $2, $3, $4}' >> gmt_cpts/$cpt.cpt
162	fi
163done < /tmp/cpt.info
164# Fix the zero hinges
165while read cpt; do
166	grep '^#' gmt_cpts/${cpt}.cpt > /tmp/${cpt}.cpt
167	egrep -v '^#|B|N|F' gmt_cpts/${cpt}.cpt | awk '{if (NR == 127) {printf "%s\t%s\t0.0\t\t\t%s\n", $1, $2, $4} else if (NR == 129) {printf "0.0\t\t\t%s\t%s\t%s\n", $2, $3, $4} else if (NR != 128) { print $0}}' >> /tmp/${cpt}.cpt
168	egrep '^B|^N|^F' gmt_cpts/${cpt}.cpt >> /tmp/${cpt}.cpt
169	mv -f /tmp/${cpt}.cpt gmt_cpts
170done < /tmp/hinge.lis
171rm -f tmp
172cd $here
173echo "Folder with new cpts is $DIR/gmt_cpts"
174