Repository: sunpy/sunpy

Issue:
Crash while saving sunpy Map if it has comments
<!-- This comments are hidden when you submit the issue so you do not need to remove them!
Please be sure to check out our contributing guidelines: https://github.com/sunpy/sunpy/blob/master/CONTRIBUTING.rst
Please be sure to check out our code of conduct:
https://github.com/sunpy/sunpy/blob/master/CODE_OF_CONDUCT.rst -->

<!-- Please have a search on our GitHub repository to see if a similar issue has already been posted.
If a similar issue is closed, have a quick look to see if you are satisfied by the resolution.
If not please go ahead and open an issue! -->

### Description
If a level1 fits file has comments in it, then to make it level 1.5, aiaprep is invoked, then saving the resultant Map will lead to crash throwing Value Error
<!-- Provide a general description of the bug. -->

### Expected behavior
The save method should run normally and save the map in FITS file.
<!-- What did you expect to happen. -->

### Actual behavior
The Map save method threw Value Error
<!-- What actually happened. -->
<!-- Was the output confusing or poorly described? -->

### Steps to Reproduce
<!-- Ideally a code example could be provided so we can run it ourselves. -->
<!-- If you are pasting code, use tripe backticks (```) around your code snippet. -->

1. Download hmi.ic_nolimbdark_720s.20151217_000000_TAI.1.continuum.fits HMI 720 s Limb Darkening corrected fits file, for the given date (the fits file has comments).
2. ```fits_hdu = fits.open('hmi.ic_nolimbdark_720s.20151217_000000_TAI.1.continuum.fits')[1]```
3. ```fits_hdu.verify('fix')```
4. ```fits_hdu.header['HGLN_OBS'] = 0.0```
4. ```aia_map = sunpy.map.Map(fits_hdu.data,fits_hdu.header)```
5. ```aia_map = aiaprep(aia_map)```
6. ```aia_map.save('aiaprep/hmi.ic_nolimbdark_720s.20151217_000000_TAI.1.continuum.fits')```

### System Details
<!-- We at least need to know the SunPy version you are using. -->
<!-- We provide a short function in SunPy that will provide some of the below information. -->
<!-- It is sunpy.util.system_info(), this is optional but strongly recommended. -->

  - SunPy Version: 0.9.3
  - Python Version: 3.7.0
  - OS information: macOS Mojave



Discussion:

Resolving patch:
diff --git a/sunpy/instr/aia.py b/sunpy/instr/aia.py
--- a/sunpy/instr/aia.py
+++ b/sunpy/instr/aia.py
@@ -43,7 +43,7 @@ def aiaprep(aiamap):
     """
 
     if not isinstance(aiamap, (AIAMap, HMIMap)):
-        raise ValueError("Input must be an AIAMap")
+        raise ValueError("Input must be an AIAMap or HMIMap.")
 
     # Target scale is 0.6 arcsec/pixel, but this needs to be adjusted if the map
     # has already been rescaled.
diff --git a/sunpy/io/fits.py b/sunpy/io/fits.py
--- a/sunpy/io/fits.py
+++ b/sunpy/io/fits.py
@@ -120,7 +120,7 @@ def get_header(afile):
         close = True
 
     try:
-        headers= []
+        headers = []
         for hdu in hdulist:
             try:
                 comment = "".join(hdu.header['COMMENT']).strip()
@@ -139,7 +139,7 @@ def get_header(afile):
             keydict = {}
             for card in hdu.header.cards:
                 if card.comment != '':
-                    keydict.update({card.keyword:card.comment})
+                    keydict.update({card.keyword: card.comment})
             header['KEYCOMMENTS'] = keydict
             header['WAVEUNIT'] = extract_waveunit(header)
 
@@ -204,11 +204,11 @@ def header_to_fits(header):
 
     for k, v in header.items():
         if isinstance(v, fits.header._HeaderCommentaryCards):
-            if k == 'comments':
+            if k.upper() == 'COMMENT':
                 comments = str(v).split('\n')
                 for com in comments:
-                    fits_header.add_comments(com)
-            elif k == 'history':
+                    fits_header.add_comment(com)
+            elif k.upper() == 'HISTORY':
                 hists = str(v).split('\n')
                 for hist in hists:
                     fits_header.add_history(hist)
@@ -306,5 +306,5 @@ def parse_waveunit_comment(waveunit_comment):
                 waveunit = m.group(1)
                 break
     if waveunit == '':
-        return None # To fix problems associated with HMI FITS.
+        return None  # To fix problems associated with HMI FITS.
     return waveunit


Test patch:
diff --git a/sunpy/io/tests/test_fits.py b/sunpy/io/tests/test_fits.py
--- a/sunpy/io/tests/test_fits.py
+++ b/sunpy/io/tests/test_fits.py
@@ -1,11 +1,12 @@
-from astropy.io import fits
+import os
+from collections import OrderedDict
 
-import sunpy.io.fits
-from sunpy.io.fits import get_header, extract_waveunit
+import astropy.io.fits as fits
 
+import sunpy.io.fits
 import sunpy.data.test
-import os
-
+from sunpy.util import MetaDict
+from sunpy.io.fits import get_header, extract_waveunit
 from sunpy.data.test.waveunit import MEDN_IMAGE, MQ_IMAGE, NA_IMAGE, SVSM_IMAGE
 
 testpath = sunpy.data.test.rootdir
@@ -107,3 +108,11 @@ def test_simple_write_compressed(tmpdir):
     with fits.open(str(outfile)) as hdul:
         assert len(hdul) == 2
         assert isinstance(hdul[1], fits.CompImageHDU)
+
+def test_write_with_metadict_header_astropy(tmpdir):
+    fits_file = fits.open(AIA_171_IMAGE)
+    data, header = fits_file[0].data, fits_file[0].header
+    meta_header = MetaDict(OrderedDict(header))
+    temp_file = tmpdir / "temp.fits"
+    sunpy.io.fits.write(str(temp_file), data, meta_header)
+    assert temp_file.exists()