def add_cmd_to_checkplot(
        cpx,
        cmdpkl,
        require_cmd_magcolor=True,
        save_cmd_pngs=False
):
    '''This adds CMD figures to a checkplot dict or pickle.

    Looks up the CMDs in `cmdpkl`, adds the object from `cpx` as a gold(-ish)
    star in the plot, and then saves the figure to a base64 encoded PNG, which
    can then be read and used by the `checkplotserver`.

    Parameters
    ----------

    cpx : str or dict
        This is the input checkplot pickle or dict to add the CMD to.

    cmdpkl : str or dict
        The CMD pickle generated by the `colormagdiagram_cplist` or
        `colormagdiagram_cpdir` functions above, or the dict produced by reading
        this pickle in.

    require_cmd_magcolor : bool
        If this is True, a CMD plot will not be made if the color and mag keys
        required by the CMD are not present or are nan in this checkplot's
        objectinfo dict.

    save_cmd_png : bool
        If this is True, then will save the CMD plots that were generated and
        added back to the checkplotdict as PNGs to the same directory as
        `cpx`. If `cpx` is a dict, will save them to the current working
        directory.

    Returns
    -------

    str or dict
        If `cpx` was a str filename of checkplot pickle, this will return that
        filename to indicate that the CMD was added to the file. If `cpx` was a
        checkplotdict, this will return the checkplotdict with a new key called
        'colormagdiagram' containing the base64 encoded PNG binary streams of
        all CMDs generated.

    '''

    # get the checkplot
    if isinstance(cpx, str) and os.path.exists(cpx):
        cpdict = _read_checkplot_picklefile(cpx)
    elif isinstance(cpx, dict):
        cpdict = cpx
    else:
        LOGERROR('unknown type of checkplot provided as the cpx arg')
        return None

    # get the CMD
    if isinstance(cmdpkl, str) and os.path.exists(cmdpkl):
        with open(cmdpkl, 'rb') as infd:
            cmd = pickle.load(infd)
    elif isinstance(cmdpkl, dict):
        cmd = cmdpkl


    cpdict['colormagdiagram'] = {}

    # get the mags and colors from the CMD dict
    cplist_mags = cmd['mags']
    cplist_colors = cmd['colors']

    # now make the CMD plots for each color-mag combination in the CMD
    for c1, c2, ym, ind in zip(cmd['color_mag1'],
                               cmd['color_mag2'],
                               cmd['yaxis_mag'],
                               range(len(cmd['color_mag1']))):

        # get these from the checkplot for this object
        if (c1 in cpdict['objectinfo'] and
            cpdict['objectinfo'][c1] is not None):
            c1mag = cpdict['objectinfo'][c1]
        else:
            c1mag = np.nan

        if (c2 in cpdict['objectinfo'] and
            cpdict['objectinfo'][c2] is not None):
            c2mag = cpdict['objectinfo'][c2]
        else:
            c2mag = np.nan

        if (ym in cpdict['objectinfo'] and
            cpdict['objectinfo'][ym] is not None):
            ymmag = cpdict['objectinfo'][ym]
        else:
            ymmag = np.nan

        if (require_cmd_magcolor and
            not (np.isfinite(c1mag) and
                 np.isfinite(c2mag) and
                 np.isfinite(ymmag))):

            LOGWARNING("required color: %s-%s or mag: %s are not "
                       "in this checkplot's objectinfo dict "
                       "(objectid: %s), skipping CMD..." %
                       (c1, c2, ym, cpdict['objectid']))
            continue

        # make the CMD for this color-mag combination
        try:

            thiscmd_title = r'%s-%s/%s' % (CMD_LABELS[c1],
                                           CMD_LABELS[c2],
                                           CMD_LABELS[ym])

            # make the scatter plot
            fig = plt.figure(figsize=(10,8))
            plt.plot(cplist_colors[:,ind],
                     cplist_mags[:,ind],
                     rasterized=True,
                     marker='o',
                     linestyle='none',
                     mew=0,
                     ms=3)

            # put this object on the plot
            plt.plot([c1mag - c2mag], [ymmag],
                     ms=20,
                     color='#b0ff05',
                     marker='*',
                     mew=0)

            plt.xlabel(r'$%s - %s$' % (CMD_LABELS[c1], CMD_LABELS[c2]))
            plt.ylabel(r'$%s$' % CMD_LABELS[ym])
            plt.title('%s - $%s$ CMD' % (cpdict['objectid'], thiscmd_title))
            plt.gca().invert_yaxis()

            # now save the figure to StrIO and put it back in the checkplot
            cmdpng = StrIO()
            plt.savefig(cmdpng, bbox_inches='tight',
                        pad_inches=0.0, format='png')
            cmdpng.seek(0)
            cmdb64 = base64.b64encode(cmdpng.read())
            cmdpng.close()

            plt.close('all')
            plt.gcf().clear()

            cpdict['colormagdiagram']['%s-%s/%s' % (c1,c2,ym)] = cmdb64

            # if we're supposed to export to PNG, do so
            if save_cmd_pngs:

                if isinstance(cpx, str):
                    outpng = os.path.join(os.path.dirname(cpx),
                                          'cmd-%s-%s-%s.%s.png' %
                                          (cpdict['objectid'],
                                           c1,c2,ym))
                else:
                    outpng = 'cmd-%s-%s-%s.%s.png' % (cpdict['objectid'],
                                                      c1,c2,ym)

                _base64_to_file(cmdb64, outpng)

        except Exception as e:
            LOGEXCEPTION('CMD for %s-%s/%s does not exist in %s, skipping...' %
                         (c1, c2, ym, cmdpkl))
            continue


    #
    # end of making CMDs
    #

    if isinstance(cpx, str):
        cpf = _write_checkplot_picklefile(cpdict, outfile=cpx, protocol=4)
        return cpf
    elif isinstance(cpx, dict):
        return cpdict
def add_cmd_to_checkplot(
        cpx,
        cmdpkl,
        require_cmd_magcolor=True,
        save_cmd_pngs=False
):
    '''This adds CMD figures to a checkplot dict or pickle.

    Looks up the CMDs in `cmdpkl`, adds the object from `cpx` as a gold(-ish)
    star in the plot, and then saves the figure to a base64 encoded PNG, which
    can then be read and used by the `checkplotserver`.

    Parameters
    ----------

    cpx : str or dict
        This is the input checkplot pickle or dict to add the CMD to.

    cmdpkl : str or dict
        The CMD pickle generated by the `colormagdiagram_cplist` or
        `colormagdiagram_cpdir` functions above, or the dict produced by reading
        this pickle in.

    require_cmd_magcolor : bool
        If this is True, a CMD plot will not be made if the color and mag keys
        required by the CMD are not present or are nan in this checkplot's
        objectinfo dict.

    save_cmd_png : bool
        If this is True, then will save the CMD plots that were generated and
        added back to the checkplotdict as PNGs to the same directory as
        `cpx`. If `cpx` is a dict, will save them to the current working
        directory.

    Returns
    -------

    str or dict
        If `cpx` was a str filename of checkplot pickle, this will return that
        filename to indicate that the CMD was added to the file. If `cpx` was a
        checkplotdict, this will return the checkplotdict with a new key called
        'colormagdiagram' containing the base64 encoded PNG binary streams of
        all CMDs generated.

    '''

    # get the checkplot
    if isinstance(cpx, str) and os.path.exists(cpx):
        cpdict = _read_checkplot_picklefile(cpx)
    elif isinstance(cpx, dict):
        cpdict = cpx
    else:
        LOGERROR('unknown type of checkplot provided as the cpx arg')
        return None

    # get the CMD
    if isinstance(cmdpkl, str) and os.path.exists(cmdpkl):
        with open(cmdpkl, 'rb') as infd:
            cmd = pickle.load(infd)
    elif isinstance(cmdpkl, dict):
        cmd = cmdpkl


    cpdict['colormagdiagram'] = {}

    # get the mags and colors from the CMD dict
    cplist_mags = cmd['mags']
    cplist_colors = cmd['colors']

    # now make the CMD plots for each color-mag combination in the CMD
    for c1, c2, ym, ind in zip(cmd['color_mag1'],
                               cmd['color_mag2'],
                               cmd['yaxis_mag'],
                               range(len(cmd['color_mag1']))):

        # get these from the checkplot for this object
        if (c1 in cpdict['objectinfo'] and
            cpdict['objectinfo'][c1] is not None):
            c1mag = cpdict['objectinfo'][c1]
        else:
            c1mag = np.nan

        if (c2 in cpdict['objectinfo'] and
            cpdict['objectinfo'][c2] is not None):
            c2mag = cpdict['objectinfo'][c2]
        else:
            c2mag = np.nan

        if (ym in cpdict['objectinfo'] and
            cpdict['objectinfo'][ym] is not None):
            ymmag = cpdict['objectinfo'][ym]
        else:
            ymmag = np.nan

        if (require_cmd_magcolor and
            not (np.isfinite(c1mag) and
                 np.isfinite(c2mag) and
                 np.isfinite(ymmag))):

            LOGWARNING("required color: %s-%s or mag: %s are not "
                       "in this checkplot's objectinfo dict "
                       "(objectid: %s), skipping CMD..." %
                       (c1, c2, ym, cpdict['objectid']))
            continue

        # make the CMD for this color-mag combination
        try:

            thiscmd_title = r'%s-%s/%s' % (CMD_LABELS[c1],
                                           CMD_LABELS[c2],
                                           CMD_LABELS[ym])

            # make the scatter plot
            fig = plt.figure(figsize=(10,8))
            plt.plot(cplist_colors[:,ind],
                     cplist_mags[:,ind],
                     rasterized=True,
                     marker='o',
                     linestyle='none',
                     mew=0,
                     ms=3)

            # put this object on the plot
            plt.plot([c1mag - c2mag], [ymmag],
                     ms=20,
                     color='#b0ff05',
                     marker='*',
                     mew=0)

            plt.xlabel(r'$%s - %s$' % (CMD_LABELS[c1], CMD_LABELS[c2]))
            plt.ylabel(r'$%s$' % CMD_LABELS[ym])
            plt.title('%s - $%s$ CMD' % (cpdict['objectid'], thiscmd_title))
            plt.gca().invert_yaxis()

            # now save the figure to StrIO and put it back in the checkplot
            cmdpng = StrIO()
            plt.savefig(cmdpng, bbox_inches='tight',
                        pad_inches=0.0, format='png')
            cmdpng.seek(0)
            cmdb64 = base64.b64encode(cmdpng.read())
            cmdpng.close()

            plt.close('all')
            plt.gcf().clear()

            cpdict['colormagdiagram']['%s-%s/%s' % (c1,c2,ym)] = cmdb64

            # if we're supposed to export to PNG, do so
            if save_cmd_pngs:

                if isinstance(cpx, str):
                    outpng = os.path.join(os.path.dirname(cpx),
                                          'cmd-%s-%s-%s.%s.png' %
                                          (cpdict['objectid'],
                                           c1,c2,ym))
                else:
                    outpng = 'cmd-%s-%s-%s.%s.png' % (cpdict['objectid'],
                                                      c1,c2,ym)

                _base64_to_file(cmdb64, outpng)

        except Exception as e:
            LOGEXCEPTION('CMD for %s-%s/%s does not exist in %s, skipping...' %
                         (c1, c2, ym, cmdpkl))
            continue


    #
    # end of making CMDs
    #

    if isinstance(cpx, str):
        cpf = _write_checkplot_picklefile(cpdict, outfile=cpx, protocol=4)
        return cpf
    elif isinstance(cpx, dict):
        return cpdict
def add_cmd_to_checkplot ( cpx , cmdpkl , require_cmd_magcolor = True , save_cmd_pngs = False ) : # get the checkplot if isinstance ( cpx , str ) and os . path . exists ( cpx ) : cpdict = _read_checkplot_picklefile ( cpx ) elif isinstance ( cpx , dict ) : cpdict = cpx else : LOGERROR ( 'unknown type of checkplot provided as the cpx arg' ) return None # get the CMD if isinstance ( cmdpkl , str ) and os . path . exists ( cmdpkl ) : with open ( cmdpkl , 'rb' ) as infd : cmd = pickle . load ( infd ) elif isinstance ( cmdpkl , dict ) : cmd = cmdpkl cpdict [ 'colormagdiagram' ] = { } # get the mags and colors from the CMD dict cplist_mags = cmd [ 'mags' ] cplist_colors = cmd [ 'colors' ] # now make the CMD plots for each color-mag combination in the CMD for c1 , c2 , ym , ind in zip ( cmd [ 'color_mag1' ] , cmd [ 'color_mag2' ] , cmd [ 'yaxis_mag' ] , range ( len ( cmd [ 'color_mag1' ] ) ) ) : # get these from the checkplot for this object if ( c1 in cpdict [ 'objectinfo' ] and cpdict [ 'objectinfo' ] [ c1 ] is not None ) : c1mag = cpdict [ 'objectinfo' ] [ c1 ] else : c1mag = np . nan if ( c2 in cpdict [ 'objectinfo' ] and cpdict [ 'objectinfo' ] [ c2 ] is not None ) : c2mag = cpdict [ 'objectinfo' ] [ c2 ] else : c2mag = np . nan if ( ym in cpdict [ 'objectinfo' ] and cpdict [ 'objectinfo' ] [ ym ] is not None ) : ymmag = cpdict [ 'objectinfo' ] [ ym ] else : ymmag = np . nan if ( require_cmd_magcolor and not ( np . isfinite ( c1mag ) and np . isfinite ( c2mag ) and np . isfinite ( ymmag ) ) ) : LOGWARNING ( "required color: %s-%s or mag: %s are not " "in this checkplot's objectinfo dict " "(objectid: %s), skipping CMD..." % ( c1 , c2 , ym , cpdict [ 'objectid' ] ) ) continue # make the CMD for this color-mag combination try : thiscmd_title = r'%s-%s/%s' % ( CMD_LABELS [ c1 ] , CMD_LABELS [ c2 ] , CMD_LABELS [ ym ] ) # make the scatter plot fig = plt . figure ( figsize = ( 10 , 8 ) ) plt . plot ( cplist_colors [ : , ind ] , cplist_mags [ : , ind ] , rasterized = True , marker = 'o' , linestyle = 'none' , mew = 0 , ms = 3 ) # put this object on the plot plt . plot ( [ c1mag - c2mag ] , [ ymmag ] , ms = 20 , color = '#b0ff05' , marker = '*' , mew = 0 ) plt . xlabel ( r'$%s - %s$' % ( CMD_LABELS [ c1 ] , CMD_LABELS [ c2 ] ) ) plt . ylabel ( r'$%s$' % CMD_LABELS [ ym ] ) plt . title ( '%s - $%s$ CMD' % ( cpdict [ 'objectid' ] , thiscmd_title ) ) plt . gca ( ) . invert_yaxis ( ) # now save the figure to StrIO and put it back in the checkplot cmdpng = StrIO ( ) plt . savefig ( cmdpng , bbox_inches = 'tight' , pad_inches = 0.0 , format = 'png' ) cmdpng . seek ( 0 ) cmdb64 = base64 . b64encode ( cmdpng . read ( ) ) cmdpng . close ( ) plt . close ( 'all' ) plt . gcf ( ) . clear ( ) cpdict [ 'colormagdiagram' ] [ '%s-%s/%s' % ( c1 , c2 , ym ) ] = cmdb64 # if we're supposed to export to PNG, do so if save_cmd_pngs : if isinstance ( cpx , str ) : outpng = os . path . join ( os . path . dirname ( cpx ) , 'cmd-%s-%s-%s.%s.png' % ( cpdict [ 'objectid' ] , c1 , c2 , ym ) ) else : outpng = 'cmd-%s-%s-%s.%s.png' % ( cpdict [ 'objectid' ] , c1 , c2 , ym ) _base64_to_file ( cmdb64 , outpng ) except Exception as e : LOGEXCEPTION ( 'CMD for %s-%s/%s does not exist in %s, skipping...' % ( c1 , c2 , ym , cmdpkl ) ) continue # # end of making CMDs # if isinstance ( cpx , str ) : cpf = _write_checkplot_picklefile ( cpdict , outfile = cpx , protocol = 4 ) return cpf elif isinstance ( cpx , dict ) : return cpdict
This adds CMD figures to a checkplot dict or pickle.

    Looks up the CMDs in `cmdpkl`, adds the object from `cpx` as a gold(-ish)
    star in the plot, and then saves the figure to a base64 encoded PNG, which
    can then be read and used by the `checkplotserver`.

    Parameters
    ----------

    cpx : str or dict
        This is the input checkplot pickle or dict to add the CMD to.

    cmdpkl : str or dict
        The CMD pickle generated by the `colormagdiagram_cplist` or
        `colormagdiagram_cpdir` functions above, or the dict produced by reading
        this pickle in.

    require_cmd_magcolor : bool
        If this is True, a CMD plot will not be made if the color and mag keys
        required by the CMD are not present or are nan in this checkplot's
        objectinfo dict.

    save_cmd_png : bool
        If this is True, then will save the CMD plots that were generated and
        added back to the checkplotdict as PNGs to the same directory as
        `cpx`. If `cpx` is a dict, will save them to the current working
        directory.

    Returns
    -------

    str or dict
        If `cpx` was a str filename of checkplot pickle, this will return that
        filename to indicate that the CMD was added to the file. If `cpx` was a
        checkplotdict, this will return the checkplotdict with a new key called
        'colormagdiagram' containing the base64 encoded PNG binary streams of
        all CMDs generated.
This adds CMD figures to a checkplot dict or pickle .