def save_boundaries_from_bounddict_ds9(bounddict, ds9_filename, numpix=100):
    """Export to ds9 region file the boundaries in bounddict.

    Parameters
    ----------
    bounddict : JSON structure
        Structure employed to store bounddict information.
    ds9_filename : str
        Output file name for the ds9 region file.
    numpix : int
        Number of points in which the X-range interval is subdivided
        in order to save each boundary as a connected set of line
        segments.

    """

    ds9_file = open(ds9_filename, 'w')

    ds9_file.write('# Region file format: DS9 version 4.1\n')
    ds9_file.write('global color=green dashlist=2 4 width=2 '
                   'font="helvetica 10 normal roman" select=1 '
                   'highlite=1 dash=1 fixed=0 edit=1 '
                   'move=1 delete=1 include=1 source=1\n')
    ds9_file.write('physical\n')

    uuid = bounddict['uuid']
    spfilter = bounddict['tags']['filter']
    grism = bounddict['tags']['grism']

    ds9_file.write('#\n# uuid (boundict file): {0}\n'.format(uuid))
    ds9_file.write('# filter..............: {0}\n'.format(spfilter))
    ds9_file.write('# grism...............: {0}\n'.format(grism))

    colorbox = ['green', 'green']
    for islitlet in range(1, EMIR_NBARS + 1):
        tmp_slitlet = 'slitlet' + str(islitlet).zfill(2)
        if tmp_slitlet in bounddict['contents'].keys():
            ds9_file.write('#\n# islitlet: {0}\n'.format(tmp_slitlet))
            read_dateobs = list(bounddict['contents'][tmp_slitlet].keys())
            read_dateobs.sort()
            for tmp_dateobs in read_dateobs:
                ds9_file.write('#\n# date-obs: {0}\n'.format(tmp_dateobs))
                tmp_dict = bounddict['contents'][tmp_slitlet][tmp_dateobs]
                # lower boundary
                pol_lower_measured = np.polynomial.Polynomial(
                    tmp_dict['boundary_coef_lower']
                )
                xmin_lower = tmp_dict['boundary_xmin_lower']
                xmax_lower = tmp_dict['boundary_xmax_lower']
                xdum = np.linspace(xmin_lower, xmax_lower, num=numpix)
                ydum = pol_lower_measured(xdum)
                for i in range(len(xdum) - 1):
                    ds9_file.write(
                        'line {0} {1} {2} {3}'.format(xdum[i], ydum[i],
                                                      xdum[i + 1], ydum[i + 1])
                    )
                    ds9_file.write(
                        ' # color={0}\n'.format(colorbox[islitlet % 2]))
                # upper boundary
                pol_upper_measured = np.polynomial.Polynomial(
                    tmp_dict['boundary_coef_upper']
                )
                xmin_upper = tmp_dict['boundary_xmin_upper']
                xmax_upper = tmp_dict['boundary_xmax_upper']
                xdum = np.linspace(xmin_upper, xmax_upper, num=numpix)
                ydum = pol_upper_measured(xdum)
                for i in range(len(xdum) - 1):
                    ds9_file.write(
                        'line {0} {1} {2} {3}'.format(xdum[i], ydum[i],
                                                      xdum[i + 1], ydum[i + 1])
                    )
                    ds9_file.write(
                        ' # color={0}\n'.format(colorbox[islitlet % 2]))
                # slitlet label
                xlabel = xmax_lower + xmax_upper + xmin_lower + xmin_upper
                xlabel /= 4
                yc_lower = pol_lower_measured(xlabel)
                yc_upper = pol_upper_measured(xlabel)
                ds9_file.write('text {0} {1} {{{2}}} # color={3} '
                               'font="helvetica 10 bold '
                               'roman"\n'.format(xlabel,
                                                 (yc_lower + yc_upper) / 2,
                                                 islitlet,
                                                 colorbox[islitlet % 2]))

    ds9_file.close()