prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Copy colormap when rasters are merged
  I'm running `rio merge` over a few single band images that contain a colormap. During the merge, the colormap is not copied to the new raster. Can we modify `rio merge` to preserve the colormap?
  
  I have an initial pass of this change at:
  
  https://github.com/kapadia/rasterio/tree/rio-merge-colormap
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `rasterio/rio/merge.py`
  Content:
  ```
  1 """Merge command."""
  2 
  3 import logging
  4 
  5 import click
  6 from cligj import files_inout_arg, format_opt
  7 
  8 from .helpers import resolve_inout
  9 from . import options
  10 import rasterio
  11 
  12 
  13 @click.command(short_help="Merge a stack of raster datasets.")
  14 @files_inout_arg
  15 @options.output_opt
  16 @format_opt
  17 @options.bounds_opt
  18 @options.resolution_opt
  19 @options.nodata_opt
  20 @options.force_overwrite_opt
  21 @click.option('--precision', type=int, default=7,
  22               help="Number of decimal places of precision in alignment of "
  23                    "pixels")
  24 @options.creation_options
  25 @click.pass_context
  26 def merge(ctx, files, output, driver, bounds, res, nodata, force_overwrite,
  27           precision, creation_options):
  28     """Copy valid pixels from input files to an output file.
  29 
  30     All files must have the same number of bands, data type, and
  31     coordinate reference system.
  32 
  33     Input files are merged in their listed order using the reverse
  34     painter's algorithm. If the output file exists, its values will be
  35     overwritten by input values.
  36 
  37     Geospatial bounds and resolution of a new output file in the
  38     units of the input file coordinate reference system may be provided
  39     and are otherwise taken from the first input file.
  40 
  41     Note: --res changed from 2 parameters in 0.25.
  42 
  43     \b
  44       --res 0.1 0.1  => --res 0.1 (square)
  45       --res 0.1 0.2  => --res 0.1 --res 0.2  (rectangular)
  46     """
  47     from rasterio.merge import merge as merge_tool
  48 
  49     verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 1
  50 
  51     output, files = resolve_inout(
  52         files=files, output=output, force_overwrite=force_overwrite)
  53 
  54     with rasterio.Env(CPL_DEBUG=verbosity > 2):
  55         sources = [rasterio.open(f) for f in files]
  56         dest, output_transform = merge_tool(sources, bounds=bounds, res=res,
  57                                             nodata=nodata, precision=precision)
  58 
  59         profile = sources[0].profile
  60         profile.pop('affine')
  61         profile['transform'] = output_transform
  62         profile['height'] = dest.shape[1]
  63         profile['width'] = dest.shape[2]
  64         profile['driver'] = driver
  65 
  66         profile.update(**creation_options)
  67 
  68         with rasterio.open(output, 'w', **profile) as dst:
  69             dst.write(dest)
  70
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/rasterio/rio/merge.py b/rasterio/rio/merge.py
  --- a/rasterio/rio/merge.py
  +++ b/rasterio/rio/merge.py
  @@ -67,3 +67,10 @@
   
           with rasterio.open(output, 'w', **profile) as dst:
               dst.write(dest)
  +
  +            # uses the colormap in the first input raster.
  +            try:
  +                colormap = sources[0].colormap(1)
  +                dst.write_colormap(1, colormap)
  +            except ValueError:
  +                pass
  
verification_info: {"golden_diff": "diff --git a/rasterio/rio/merge.py b/rasterio/rio/merge.py\n--- a/rasterio/rio/merge.py\n+++ b/rasterio/rio/merge.py\n@@ -67,3 +67,10 @@\n \n         with rasterio.open(output, 'w', **profile) as dst:\n             dst.write(dest)\n+\n+            # uses the colormap in the first input raster.\n+            try:\n+                colormap = sources[0].colormap(1)\n+                dst.write_colormap(1, colormap)\n+            except ValueError:\n+                pass\n", "issue": "Copy colormap when rasters are merged\nI'm running `rio merge` over a few single band images that contain a colormap. During the merge, the colormap is not copied to the new raster. Can we modify `rio merge` to preserve the colormap?\n\nI have an initial pass of this change at:\n\nhttps://github.com/kapadia/rasterio/tree/rio-merge-colormap\n\n", "before_files": [{"content": "\"\"\"Merge command.\"\"\"\n\nimport logging\n\nimport click\nfrom cligj import files_inout_arg, format_opt\n\nfrom .helpers import resolve_inout\nfrom . import options\nimport rasterio\n\n\n@click.command(short_help=\"Merge a stack of raster datasets.\")\n@files_inout_arg\n@options.output_opt\n@format_opt\n@options.bounds_opt\n@options.resolution_opt\n@options.nodata_opt\n@options.force_overwrite_opt\n@click.option('--precision', type=int, default=7,\n              help=\"Number of decimal places of precision in alignment of \"\n                   \"pixels\")\n@options.creation_options\n@click.pass_context\ndef merge(ctx, files, output, driver, bounds, res, nodata, force_overwrite,\n          precision, creation_options):\n    \"\"\"Copy valid pixels from input files to an output file.\n\n    All files must have the same number of bands, data type, and\n    coordinate reference system.\n\n    Input files are merged in their listed order using the reverse\n    painter's algorithm. If the output file exists, its values will be\n    overwritten by input values.\n\n    Geospatial bounds and resolution of a new output file in the\n    units of the input file coordinate reference system may be provided\n    and are otherwise taken from the first input file.\n\n    Note: --res changed from 2 parameters in 0.25.\n\n    \\b\n      --res 0.1 0.1  => --res 0.1 (square)\n      --res 0.1 0.2  => --res 0.1 --res 0.2  (rectangular)\n    \"\"\"\n    from rasterio.merge import merge as merge_tool\n\n    verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 1\n\n    output, files = resolve_inout(\n        files=files, output=output, force_overwrite=force_overwrite)\n\n    with rasterio.Env(CPL_DEBUG=verbosity > 2):\n        sources = [rasterio.open(f) for f in files]\n        dest, output_transform = merge_tool(sources, bounds=bounds, res=res,\n                                            nodata=nodata, precision=precision)\n\n        profile = sources[0].profile\n        profile.pop('affine')\n        profile['transform'] = output_transform\n        profile['height'] = dest.shape[1]\n        profile['width'] = dest.shape[2]\n        profile['driver'] = driver\n\n        profile.update(**creation_options)\n\n        with rasterio.open(output, 'w', **profile) as dst:\n            dst.write(dest)\n", "path": "rasterio/rio/merge.py"}], "after_files": [{"content": "\"\"\"Merge command.\"\"\"\n\nimport logging\n\nimport click\nfrom cligj import files_inout_arg, format_opt\n\nfrom .helpers import resolve_inout\nfrom . import options\nimport rasterio\n\n\n@click.command(short_help=\"Merge a stack of raster datasets.\")\n@files_inout_arg\n@options.output_opt\n@format_opt\n@options.bounds_opt\n@options.resolution_opt\n@options.nodata_opt\n@options.force_overwrite_opt\n@click.option('--precision', type=int, default=7,\n              help=\"Number of decimal places of precision in alignment of \"\n                   \"pixels\")\n@options.creation_options\n@click.pass_context\ndef merge(ctx, files, output, driver, bounds, res, nodata, force_overwrite,\n          precision, creation_options):\n    \"\"\"Copy valid pixels from input files to an output file.\n\n    All files must have the same number of bands, data type, and\n    coordinate reference system.\n\n    Input files are merged in their listed order using the reverse\n    painter's algorithm. If the output file exists, its values will be\n    overwritten by input values.\n\n    Geospatial bounds and resolution of a new output file in the\n    units of the input file coordinate reference system may be provided\n    and are otherwise taken from the first input file.\n\n    Note: --res changed from 2 parameters in 0.25.\n\n    \\b\n      --res 0.1 0.1  => --res 0.1 (square)\n      --res 0.1 0.2  => --res 0.1 --res 0.2  (rectangular)\n    \"\"\"\n    from rasterio.merge import merge as merge_tool\n\n    verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 1\n\n    output, files = resolve_inout(\n        files=files, output=output, force_overwrite=force_overwrite)\n\n    with rasterio.Env(CPL_DEBUG=verbosity > 2):\n        sources = [rasterio.open(f) for f in files]\n        dest, output_transform = merge_tool(sources, bounds=bounds, res=res,\n                                            nodata=nodata, precision=precision)\n\n        profile = sources[0].profile\n        profile.pop('affine')\n        profile['transform'] = output_transform\n        profile['height'] = dest.shape[1]\n        profile['width'] = dest.shape[2]\n        profile['driver'] = driver\n\n        profile.update(**creation_options)\n\n        with rasterio.open(output, 'w', **profile) as dst:\n            dst.write(dest)\n\n            # uses the colormap in the first input raster.\n            try:\n                colormap = sources[0].colormap(1)\n                dst.write_colormap(1, colormap)\n            except ValueError:\n                pass\n", "path": "rasterio/rio/merge.py"}]}