Generating resized & rotated images with coastlines from composites in satpy

249 views
Skip to first unread message

Miruna

unread,
Nov 27, 2017, 12:35:18 PM11/27/17
to pytroll
Dear Pytrollists,

I am trying to generate images of the Airmass composite from MSG HRIT files, of a certain size, and with coastlines on top. I managed to do this in mpop with pycoast (ContourWriterAGG): I load the data, reproject it, generate the composite, then do the following:
- img=global_data.image.airmass()
-img_pil=img.pil_image()   
-cw.add_coastlines(img_pil,areadef,outline='black',width=1)
then I resize the image and save it to disk.

With satpy, I have the following issues:
1) Colours
If I load the composite in my Scene object and then do Scene.save_dataset, I get the colours I expect to get for Airmass, but with the globe upside down. To counter this, I convert the composite to a TrollImage (img=to_image(scene['airmass']), then to a PIL image (because I couldn't find a rotate function inside TrollImage, maybe I am missing it). The problem is that when I save this rotated PIL Image to disk, I do not get the same colours as when saving it with save_dataset from the Scene object. Also, if I save the TrollImage obtained via to_image from the composite, I don't get the expected colours. What additional steps (stretching, gamma etc.) would I need to apply to the Image object in order to get the same thing as with save_dataset?

2) Coastlines
With mpop, I was using ContourWriterAGG from pycoast, which needs as input parameter an area definition (from mpop.projector). How can I add coastlines in satpy? In the documentation of satpy, I found the method satpy.writers.add_overlay(orig, area, coast_dir, color=(0, 0, 0), width=0.5, resolution=None) but it's not clear to me how to use it. What is the "area" input parameter here?

Thank you very much for your help.

Miruna


Martin Raspaud

unread,
Nov 29, 2017, 6:57:41 AM11/29/17
to pyt...@googlegroups.com
Dear Miruna,

1. trollimage doesn't have a rotate function, but it would be fairly easy to add: a pr is welcome😀. Maybe a more sustainable approach would be to flip the data at reading time. I think it shouldn't that hard to implement. 

2. For the coastlines, you could do the following, when you save the image:
scn.save_dataset('airmass', "./test.png", overlay={'coast_dir': '/data/my/shapefiles/', 'width':1})

Best regards
Martin 

--
You received this message because you are subscribed to the Google Groups "pytroll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pytroll+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Martin Raspaud, PhD
Software engineer
SMHI, SE-60176

Miruna

unread,
Nov 30, 2017, 10:27:09 AM11/30/17
to pytroll
Dear Martin,

Thank you! What about the colours, how can I obtain the same colours when saving the image (GeoImage or PIL) as when I save the dataset from the Scene object?

Best regards,

Miruna

miruna stoicescu

unread,
Nov 30, 2017, 10:39:56 AM11/30/17
to pyt...@googlegroups.com
Dear Martin,

What about the resizing and the colour issue? I was converting to PIL image to be able to rotate and resize but I encountered this problem with the colourscale, not being the same as when I save the dataset from Scene. 

Regards,

Miruna


--
You received this message because you are subscribed to a topic in the Google Groups "pytroll" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pytroll/bVspeF22z5s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pytroll+unsubscribe@googlegroups.com.

Christian Peters

unread,
Jan 21, 2018, 7:42:41 AM1/21/18
to pytroll
Dear Martin,

I tried to add coastlines with your provide line, but got this error:

local_scn.save_dataset('natural', 'my_nice_overview2_realistic.png', overlay={'coast_dir':'/root/GSHHS_shp/','width':'1'})

  File "/usr/local/lib/python2.7/dist-packages/satpy/scene.py", line 662, in save_dataset
    overlay=overlay, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 369, in save_dataset
    dataset, self.enhancer, fill_value, overlay=overlay, decorate=decorate)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 248, in get_enhanced_image
    add_overlay(img, dataset.info['area'], **overlay)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 114, in add_overlay
    resolution=resolution, width=width, level=level_coast)
TypeError: add_coastlines() got an unexpected keyword argument 'width'

I figured out that I need to install AggDraw 1.3.
Nwo I get:

local_scn.save_dataset('natural', 'my_nice_overview2_realistic.png', overlay={'coast_dir':'/root/','width':'1'})
  File "/usr/local/lib/python2.7/dist-packages/satpy/scene.py", line 662, in save_dataset
    overlay=overlay, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 369, in save_dataset
    dataset, self.enhancer, fill_value, overlay=overlay, decorate=decorate)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 248, in get_enhanced_image
    add_overlay(img, dataset.info['area'], **overlay)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 114, in add_overlay
    resolution=resolution, width=width, level=level_coast)
  File "/usr/local/lib/python2.7/dist-packages/pycoast/cw_agg.py", line 418, in add_coastlines
    y_offset=y_offset)
  File "/usr/local/lib/python2.7/dist-packages/pycoast/cw_base.py", line 598, in _add_feature
    x_offset=x_offset, y_offset=y_offset, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pycoast/cw_base.py", line 575, in add_shapes
    **new_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pycoast/cw_agg.py", line 62, in _draw_polygon
    kwargs['outline_opacity'])
TypeError: a float is required


What am I doing wrong??

Regards,

Christian

Martin Raspaud

unread,
Jan 22, 2018, 7:12:10 AM1/22/18
to pyt...@googlegroups.com
Hi Christian,

Whithout having testing myself, did you try providing the width as a float ?

Best regards,
Martin

Christian Peters

unread,
Jan 22, 2018, 8:19:13 AM1/22/18
to pytroll
Martin,

yes...I tried.

But it dosn't change anything.

The log with setting width=1.0 :

[INFO: 2018-01-22 14:09:39 : satpy.writers] Add coastlines and political borders to image.
[DEBUG: 2018-01-22 14:09:39 : satpy.writers] Automagically choose resolution h
Traceback (most recent call last):
  File "compute2.py", line 84, in <module>
    local_scn.save_dataset('natural', 'my_nice_overview2_realistic.png', overlay={'coast_dir':'/root/','width':'1.0'})

  File "/usr/local/lib/python2.7/dist-packages/satpy/scene.py", line 662, in save_dataset
    overlay=overlay, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 369, in save_dataset
    dataset, self.enhancer, fill_value, overlay=overlay, decorate=decorate)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 248, in get_enhanced_image
    add_overlay(img, dataset.info['area'], **overlay)
  File "/usr/local/lib/python2.7/dist-packages/satpy/writers/__init__.py", line 114, in add_overlay
    resolution=resolution, width=width, level=level_coast)
  File "/usr/local/lib/python2.7/dist-packages/pycoast/cw_agg.py", line 418, in add_coastlines
    y_offset=y_offset)
  File "/usr/local/lib/python2.7/dist-packages/pycoast/cw_base.py", line 598, in _add_feature
    x_offset=x_offset, y_offset=y_offset, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pycoast/cw_base.py", line 575, in add_shapes
    **new_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pycoast/cw_agg.py", line 62, in _draw_polygon
    kwargs['outline_opacity'])
TypeError: a float is required

Regards,

Christian

Martin Raspaud

unread,
May 17, 2018, 8:26:35 AM5/17/18
to pyt...@googlegroups.com
Hi Christian,

I realise it's been a long time since your last email, but I was wondering if you had found a solution for this ? Otherwise, it might be an aggdraw problem.

Best regards,
Martin

Christian Peters

unread,
Jun 9, 2018, 2:07:42 AM6/9/18
to pytroll
HI Martin,

no...I didn't got it working.
I hope there will be some more examples for satpy (with adding coastlines) in the future to get an idea how it works.

I managed it with mpop but not with satpy.

Regards,

Christian
Reply all
Reply to author
Forward
0 new messages