<jupyter_start><jupyter_text>Test<jupyter_code>wideFrame = '/Users/songhuang/Desktop/gama_compare/database_dr15a/hsc_dr15a_wide_frame.fits'
table = fits.open(wideFrame)[1].data
print(len(table), " frames")
table.columns
a = Point(335.82, 0.096).buffer(2.5)<jupyter_output><empty_output><jupyter_text>Given (RA, DEC, RADIUS), returns all the overlapped (Visit, CCD) in Certain Filter<jupyter_code>def genCircle(ra, dec, rad): 
    """
    Generate a circular Shape using input (RA, DEC) as center 
    and input searching radius as radius
    """
    
    try: 
        cir = Point(ra, dec).buffer(rad)
    except NameError:
        from shapely.geometry import Point
        cir = Point(ra, dec).buffer(rad)
    
    return cir 


def ccdToPolygon(frame):
    """
    Convert one (VISIT, CCD) item in the HSC frame catalog
    into a Polygon shape 
    """
    
    ccdPoly = Polygon([(frame['llcra'], frame['llcdecl']), 
                       (frame['lrcra'], frame['lrcdecl']), 
                       (frame['urcra'], frame['urcdecl']),
                       (frame['ulcra'], frame['ulcdecl'])])
    
    return ccdPoly
def showFrameMatch(match, ra, dec, rad, dpi=80, 
                   outPNG='frame_radec_match.png', 
                   extra=''):
    """
    """
    
    minRa  = np.nanmin(match['ra2000']) - 0.12
    maxRa  = np.nanmax(match['ra2000']) + 0.12
    minDec = np.nanmin(match['decl2000']) - 0.08
    maxDec = np.nanmax(match['decl2000']) + 0.08
    
    xSize = 12.0 
    ySize = xSize * ((maxDec - minDec) / (maxRa - minRa))
    
    fig = plt.figure(figsize=(xSize, ySize), dpi=dpi)
    ax = fig.add_subplot(111)
    
    # Turn off scientifc notation
    #ax.ticklabel_format(axis='both', style='plain')
    #ax.get_xaxis().get_major_formatter().set_scientific(False)
    #ax.get_yaxis().get_major_formatter().set_scientific(False)
    ax.xaxis.set_major_formatter(FormatStrFormatter('%6.2f'))
    
    ax.set_xlim(minRa,  maxRa)
    ax.set_ylim(minDec, maxDec)
    
    ax.text(0.09, 0.94, ("%7.3f" % ra).strip() + '  ' + ("%7.3f" % dec).strip() + \
            '  ' + extra, 
            fontsize=20, transform = ax.transAxes)
        
    for frame in match: 
        ccdPoly = ccdToPolygon(frame)
        ccdShow = PolygonPatch(ccdPoly, fc='r', ec='None',
                               alpha=0.1, zorder=1)
        ax.add_patch(ccdShow)
        ccdEdge = PolygonPatch(ccdPoly, fc='None', ec='k',
                               alpha=0.8, zorder=1)
        ax.add_patch(ccdEdge)
        
    regSearch = plt.Circle((ra, dec), rad, color='b', 
                          fill=False, linewidth=3.5, linestyle='dashed', 
                          alpha=0.8)
    ax.add_artist(regSearch)
    ax.scatter(ra, dec, marker='+', s=300, c='k', linewidth=3.0)

    ax.set_xlabel(r'RA (deg)',  fontsize=25)
    ax.set_ylabel(r'DEC (deg)', fontsize=25)

    fontsize = 16
    for tick in ax.xaxis.get_major_ticks():
        tick.label1.set_fontsize(fontsize)
    for tick in ax.yaxis.get_major_ticks():
        tick.label1.set_fontsize(fontsize)

    ax.minorticks_on()
    plt.tick_params(which='major', width=2.0, length=8.0, labelsize=20)
    plt.tick_params(which='minor', width=1.8, length=6.0)

    for axis in ['top','bottom','left','right']:
        ax.spines[axis].set_linewidth(2.5)

    ax.grid(alpha=0.6, color='k', linewidth=1.5)

    fig.subplots_adjust(bottom=0.1, left=0.1,
                        top=0.98, right=0.98)

    #fig.savefig(outPNG, dpi=dpi)
    #plt.close(fig)
def frameRaDecSearch(catFrame, ra, dec, rad, filter='HSC-I', 
                     shortExp=False, verbose=True, fitsFile=True,
                     show=True, prefix=None, point=False): 
    """
    Find all HSC single frame CCD data that overlap with certain region 
    
    Input: 
        catFrame:   FITS catalog of frame information 
        ra, dec:    The (RA, DEC) of the field center (deg)
        rad:        Radius of the circular searching region (deg)
        
    Option: 
        filter    = 'HSC-I'   : HSC filter 
        shortExp  = False     : Whether including frame with expTime < 100s or not
    """
    
    if fitsFile:
        # Read in the Frame catalog 
        if os.path.isfile(catFrame): 
            frameTab = fits.open(catFrame)[1].data
        else: 
            raise Exception('# Can not find the input FRAME catalog : %s' % catFrame)
        
        # Filter the catalog 
        if shortExp: 
            frameUse = frameTab[frameTab['filter01'] == filter.strip()]
        else: 
            frameUse = frameTab[(frameTab['filter01'] == filter.strip()) & 
                                (frameTab['exptime'] > 35.0)]
    else: 
        frameUse = catFrame
        
    # Only use the frames that are near the search region 
    frameNear = frameUse[(np.abs(frameUse['ra2000'] - ra)    <= (rad + 0.3)) &
                         (np.abs(frameUse['decl2000'] - dec) <= (rad + 0.3))]
    if verbose:
        print("# %i frames are found in filter: %s" % (len(frameNear), filter))
    
    # Region to search 
    if point:
        cir = Point(ra, dec)
    else:
        cir = genCircle(ra, dec, rad)
    
    match = []
    for frame in frameNear: 
        ccdPoly = ccdToPolygon(frame)
        match.append(cir.intersects(ccdPoly))
        
    frameMatch = frameNear[np.asarray(match)]
    
    if verbose:
        print("# %i matched frames have been found! " % len(frameMatch))
        
    if show: 
        if prefix is None: 
            prefix = 'frame_' + ("%7.3f" % ra).strip() + '_' + ("%7.3f" % dec).strip()
        pngName = prefix + '_' + ("%3.1f" % rad).strip() + '_' + filter.strip() + '.png'
    
        showFrameMatch(frameMatch, ra, dec, rad, outPNG=pngName, 
                       extra=filter.strip())
        
    return frameMatch
match = frameRaDecSearch(wideFrame, 335.82, 0.096, 0.0653)
showFrameMatch(match, 335.82, 0.096, 0.0653)
for visit in np.unique(match['visit']):
    ccds = table[(table['visit'] == visit) & 
                 (table['filter01'] == 'HSC-I')]
    print(np.std(ccds['skylevel']))<jupyter_output>165.449
226.825
212.675
212.04
122.417
117.547
112.018
113.855
113.069
119.385
122.603
{'fraction_non_alphanumeric': 0.1437125748502994, 'fraction_numerical': 0.03867939796083509, 'mean_word_length': 2.2254697286012526, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '595300', 'license': 'Unlicense', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2098, 'n_words': 440, 'path': 'note/old/frameSearch.ipynb', 'repo_name': 'dr-guangtou/hsc_redbcg', 'size': '6179'}