Code Monkey home page Code Monkey logo

cmocean_ncwms_palettes's Introduction

Convert cmocean colormaps to ".pal" files for ncWMS2

cmocean is a Python module that provides perceptually uniform colormaps broadly aimed at the earth, ocean and atmospheric sciences. You can read more here https://matplotlib.org/cmocean/.

A paper describing the motivation and selected colormaps can be found here, which can be cited as:

Thyng, K.M., C.A. Greene, R.D. Hetland, H.M. Zimmerle, and S.F. DiMarco. 2016. True colors of oceanography: Guidelines for effective and accurate colormap selection. Oceanography 29(3):9โ€“13, http://dx.doi.org/10.5670/oceanog.2016.66.

ncWMS2 is is an OGC Web Map Service designed to serve geospatial data stored in netCDF files to GIS and web map clients (https://reading-escience-centre.gitbooks.io/ncwms-user-guide/content/). It comes with a set of default colormaps, but we would like to leverage the work done in cmocean in addition to providing consistent colormaps with figures generated by Python.

The Java-based web service provides an easy way to add new styles to the server, and for colormaps we can simply add ".pal" files for each colormap where the name of the file will be the name of the colormap. Each ".pal" palette file contains one line for each color provided by the colormap (intermediate colors are interpolated by the server if necessary) which are expressed as hexadecimal colors (i.e. #RRGGBB).

Note: Skip below to use the files we have already generated.

Install cmocean

# With pip
pip install cmocean

# or with conda using conda-forge channel
conda install -c conda-forge cmocean

Generate ncWMS2 colormap files

  1. Import cmocean
import cmocean
cmocean.__version__
'2.0'
  1. How many individual colors do we have for each cmap?
cmocean.cm.algae.N
256
  1. Apparently cmocean has an rgb2hex method that we can use for each color in a cmap:
cmocean.cm.colors.rgb2hex(cmocean.cm.algae(254))
'#122515'
  1. Put this all together in a simple but perhaps not the most efficient way to loop through each color and create an ncWMS2 ".pal" file for each colormap in cmocean:
for cname in cmocean.cm.cmapnames:
    print(cname)
    with open("cmo_{0}.pal".format(cname), "w") as f:
        cmap = cmocean.cm.__getattribute__(cname)
        for x in range(cmap.N):
            ahex = cmocean.cm.colors.rgb2hex(cmap(x))
            f.write("{0}\n".format(ahex))
thermal
haline
solar
ice
gray
oxy
deep
dense
algae
matter
turbid
speed
amp
tempo
rain
phase
topo
balance
delta
curl
diff
tarn
  1. Create the reverse colormaps and append _r to the name to be complete:
for cname in cmocean.cm.cmapnames:
    print("{0}_r".format(cname))
    with open("cmo_{0}_r.pal".format(cname), "w") as f:
        cmap = cmocean.cm.__getattribute__(cname)
        for x in range(cmap.N, 0, -1):
            ahex = cmocean.cm.colors.rgb2hex(cmap(x))
            f.write("{0}\n".format(ahex))
thermal_r
haline_r
solar_r
ice_r
gray_r
oxy_r
deep_r
dense_r
algae_r
matter_r
turbid_r
speed_r
amp_r
tempo_r
rain_r
phase_r
topo_r
balance_r
delta_r
curl_r
diff_r
tarn_r

Add to correct ncWMS2 path

According to the ncWMS2 documentation:

The palette files can be placed in a directory named .palettes within the main config directory (i.e. ~/.ncWMS2/.palettes/ by default) and will be picked up automatically after a server restart.

Although they can be placed in other locations with additional configuration.

# Copy generated colormap files to ncWMS2 palette path
cp cmo*.pal /path/to/.ncWMS2/.palettes/

cmocean_ncwms_palettes's People

Contributors

acrosby avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.