Creating new map sub-classes

22 views
Skip to first unread message

Richard Morton

unread,
May 25, 2021, 6:42:48 AM5/25/21
to SunPy
Hi,

I am writing a map sub-class for CoMP (Coronal Multi-channel Polarimeter) data and I am just trying to get the basic functionality working.

One of the current sticking points is with the plotting through peek. Currently the time on the plots is set to 00.00.00 - which is incorrect. This is likely to do with the fact that the CoMP header does not have a single card that has both date and time information in.

I have tried looking in the map base definitions, but cannot see which attribute is being used to create the plot title. So, which attribute do I need to edit in order to get it displaying the correct date?

Thanks!

Richard 

Richard Morton

unread,
May 25, 2021, 7:11:07 AM5/25/21
to SunPy
I think I may have fixed it, maybe not in a preferred way (I always seem to come up with an answer as soon as I post here....?!)

I combined two cards (date and time) and set to the date-obs card in the header before passing to super().__init__ - should I be doing something different?

Also, just out of interest, is there a mechanism to access fits file extensions in the Map class? The CoMP data comes in a fits file with 4 ImageHDUs (Intensity, Doppler, etc). At the moment, I am using a separate function to split off the data product I want before passing in the data and header to GenericMap. Is it possible for me to pass in the extension name to GenericMap?

For example, with the astropy fits reading tools, I can say HDUL['Intensity'].data and get the intensity ImageHDU.

However, lets say I just wanted to pass the file path to GenericMap and the name of extension I required. Can this be done?

will.t...@gmail.com

unread,
May 26, 2021, 11:29:07 AM5/26/21
to SunPy
Hi Richard,

Passing in a map with multiple image-like HDUs to the map factory (i.e. Map as opposed to GenericMap) should work. This should then return a list of maps, one for each of the images in your file. If this doesn't work, it is a bug. Map is the recommended entry point for creating maps as it accepts a wide variety of inputs whereas the only entry point for GenericMap is a data, header pair.

To use a custom map class with the factory, it may be useful to have a look at this section of the docs: https://docs.sunpy.org/en/stable/code_ref/map.html#writing-a-new-instrument-map-class. The important bit is the _is_datasource_for method. You'll just need to have some key in the header of your map that you can use to identify the map as a CoMP measurement. In it's current state, Map does not support multiple data arrays, so I think the best option would probably be to create a separate map class for each measurement (e.g. CompIntensityMap, CompDopplerMap, etc.) and then adjust the _is_datasource_for method appropriately to flag each data, header pair as such. This should allow you to provide a single multi-HDU file to Map and get back a list of maps, one for each of your measurements. 

Regarding your question about date-obs, that is one way to do it. When writing new custom map classes, we have tried to avoid altering the metadata itself (though this is not always true!) and instead override the derived properties on the subclasses themselves, e.g. for the new EUIMap class, https://github.com/sunpy/sunpy/blob/c40707fbbc6d438917c3c71de139237eda16e1e2/sunpy/map/sources/solo.py#L46. So in your case, you would override the date property and then construct the date from the appropriate date and time cards without needing to insert a new card into the header.

Best,

Will

Richard Morton

unread,
May 27, 2021, 5:54:35 AM5/27/21
to SunPy

Hi Will,

Thanks for the answer. I am not sure I completely follow what you’ve said, maybe as I have some confusion about the internal workings of  the Map class. So, I have a few further questions.

* In order for the _is_datasource_for method to work, do I have to place my custom map class within the Sunpy package structure, i.e. in the sources directory? I am guessing yes.

* The fits files for CoMP have some quirks, i.e. most of the info is in the primaryHDU header, and only a few supplementary cards are in the ImageHDU headers. From my limited understanding, the use of header in the Map code refers to the ImageHDU header. Will this limit me instantiating the Map class by only passing in the primaryHDU header as a data header pair (as opposed to passing the path to the fits file)?

* The type of the data (e.g. intensity, etc) is stored in a card in the ImageHDU. So following on from above, I think I would need to pass in both headers, or have access to both headers within the class. Can this be done? 

Richard 

Russell Hewett

unread,
May 28, 2021, 6:36:18 AM5/28/21
to su...@googlegroups.com
Hi Richard,

The Map factory is a bit magic.  All you need to do is import the map module and define your new subclass of GenericMap with the `is_datasource_for` member function.  SunPy's infrastructure takes care of the rest.  An example is here, at the bottom: https://github.com/sunpy/sunpy/blob/cb37a0e5d9ce88de05ff789801879cd633198d05/docs/code_ref/map.rst

This all works because of a special function in GenericMap which identifies its own subclasses and builds the registry automatically.  You used to have to register new classes manually but now all you have to do is define them and import them and they will be registered with the factory.  SunPy guarantees this for the provided maps by always importing all of its packaged sources when the map module is imported, but you will have to import your new type everywhere you want to use it (or as part of a subpackage).

Cheers,
Russell

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sunpy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sunpy/d50c73d8-c36f-4f25-a796-e18075e4f290n%40googlegroups.com.


--
Russell J. Hewett, Ph.D
Assistant Professor
Department of Mathematics
CMDA Affiliated Faculty
Virginia Tech
www.rjh.io
Reply all
Reply to author
Forward
0 new messages