[Thread Prev][Thread Next][Index]

Re: [ferret_users] inverting cmocean color bar



Hi Saurabh,

On Thu, Mar 5, 2020 at 9:29 AM saurabh rathore <rohitsrb2020@xxxxxxxxx> wrote:

I am using cmocean color bar and I like to plot the color bar in reverse direction means upside down. I tried a lot using Matlab also to flip it upside down but unsuccessful and too much of time-consuming. So is it possible to invert the colour the cmocean color bar like blue_orange to orange_blue?


I'm attaching my script which does that.

$ ruby ferret-invert-spk.rb your-rainbow.spk my-grayscale.spk . . .

I hope it works for you.

Cheers,
Ryo
#!/usr/bin/ruby

#-- return the next non-comment, non-blank line.
def next_line(f)
  line = nil
  while (line = f.gets) && (line =~ /^\s*$/ or line =~ /^\s*!/)
  end
  return line
end

#-- Main loop over palette files ---
ARGV.each {|fname|
  inf = File.open(fname,"r")

  line = next_line(inf) #-- first non-comment line

  begin
    if ! line then raise "#{fname}: empty?  Skipping. . ." end
    case line
    when /\s*(rgb_mapping\s+by_value)/i, /\s*(rgb_mapping\s+by_level)/i
      raise "#{fname}: cannot invert #{$1} ."
    end
  rescue RuntimeError => err
    $stderr.puts err
    inf.close
    next
  end

  ofname = "inverse-" + File.basename(fname)
  ouf = File.open(ofname, "w")

  if line =~ /\s*(rgb_mapping)/i
    ouf.puts line
    line = next_line(inf)
  end

  buff = Array.new
  while line
    v, *rest = line.split
    buff << "#{100 - v.to_f} #{rest.join(' ')}"
    line = next_line(inf)
  end
  inf.close

  buff.reverse_each {|e| ouf.puts e }

  ouf.close
}

[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement