예제 #1
0
def example():
    """
    This is just an example.  Cut and paste that on the python prompt.
    It can also be run as specplot.example().
    """
    import numpy as np
    import matplotlib.pyplot as plt
    from astropy import wcs
    from astrodata import AstroData
    
    ad = AstroData('JHK.fits')
    x_values = np.arange(ad.get_key_value('NAXIS1'))
    
    wcs_ad = wcs.WCS(ad.header.tostring())
    wlen = wcs_ad.wcs_pix2world(zip(x_values), 0)
    
    plt.plot(wlen, ad.data)
    plt.xlabel('Wavelength [Angstrom]')
    plt.ylabel('Counts')
    plt.axis('tight')
    plt.ylim(-100, 800)
    plt.show()
    
    ad.close()

    #plt.axis[[-100,1000,ymin,ymax]]
예제 #2
0
def mktable_helper(tablename, auto=True, rawdir="./"):
    """
    Create or append to an observation summary table.
    This function is interactive and requires input from the users.
    
    :param tablename: Filename for the table.  If it exists it will
        be extended.
    :type tablename: str
    :param auto: If True, get some of the information directly from
        the FITS headers.  [Default: True]
    :type auto: boolean
    :param rawdir: Path to raw data.  Required for auto=True.
        [Default: "./"]
    :type rawdir: str
    """
    import obstable
    import os.path
    if auto:
        from astrodata import AstroData
        
    # Create an ObsTable.  If the file already exists on disk,
    # then read it.  Otherwise, leave it empty.
    # Error handling: If the file exists but there's an read error,
    # raise, otherwise assume that you are creating a new file.
    table = obstable.ObsTable()
    table.filename = tablename
    try:
        table.read_table()
    except IOError:
        if os.path.exists(tablename):
            print "Error reading table %s\n" % tablename
            raise
        else:
            print "New table will be created."
    
    # Start the prompting the user and the data for the information
    # that needs to go in the table.
    
    user_not_done = True
    while user_not_done:
        user_inputs = {}
        if auto:
            filename_not_known = True
        
        # Get list of prompts for user or data supplied information.
        req_input_list = get_req_input_list()
        
        # Loop through record elements
        for input_request in req_input_list:
            if (not input_request['in_hdr'] or not auto):
                # if auto and this is a Science entry, get the targetname from
                # the header.  If not Science, then you need to prompt user.
                if auto and input_request['id'] == 'targetname' and \
                    user_inputs.has_key('datatype') and \
                    user_inputs['datatype'] == 'Science':
                    
                    input_value = query_header(ad, input_request['id'])
                else:
                    # prompt the user
                    input_value = raw_input(input_request['prompt'])

                user_inputs[input_request['id']] = input_value
                
                # Assume that the user has a brain.
                # Probe only the first file in 'filerange' since all the
                # files in 'filerange' should be similar.
                #
                # Once we know the name of the first MEF file, open it
                # and keep it open until we're done requesting inputs
                # (instead of opening and closing it every time).
                if auto and filename_not_known:
                    if user_inputs.has_key('rootname') and \
                       user_inputs.has_key('filerange'):
                        # parse filerange, build filename (with rawdir path)
                        filenumbers = parse_filerange(user_inputs['filerange'])
                        filename = "%sS%04d.fits" % \
                                (user_inputs['rootname'], filenumbers[0])
                        filename = os.path.join(rawdir, filename)

                        # open ad
                        ad = AstroData(filename)                        
                        filename_not_known = False
            else:
                
                # get value from header
                input_value = query_header(ad, input_request['id'])
                user_inputs[input_request['id']] = input_value
        
        if auto:
            ad.close()
                
        # Create record
        new_record = create_record(user_inputs)
        
        # Append to table
        table.add_records_to_table(new_record)
        
        # Prompt user: add another entry?
        answer = raw_input('Add another entry (y/n): ')
        user_not_done = ((answer=='y') or False)
    
    # All the info is now in the ObsTable.
    # Write the ObsTable to disk and close everything, we're done.
    table.write_table()
    table.pretty_table()
    
    return
예제 #3
0
def test_method_close_3():
    ad = AstroData(TESTFILE)
    ad.close()
    with pytest.raises(TypeError):
        ad[0]
예제 #4
0
def test_method_close_2():
    ad = AstroData(TESTFILE)
    ad.close()
    with pytest.raises(AstroDataError):
        ad.append(moredata=hdu1)
예제 #5
0
def test_method_close_1():
    ad = AstroData(TESTFILE)
    ad.close()
    assert not ad.hdulist
예제 #6
0
mem.memtrack("testmemprofile", lineno()); line += 1

ad = AstroData("data/N20131223S0243.fits")

mem.memtrack("testmemprofile", lineno()); line += 1

if True:
    datas = []
    for ext in ad:
        mem.memtrack("data pull in %s,%d" % (ext.extname(), ext.extver()), line); line += 1
        #print np.median(ext.data)
        #malloc = np.ones((2000,2000), dtype = np.float32)
        datas.append(ext.data)
    
    
mem.memtrack("before ad.write(..)", lineno());

ad.write("tmp.fits", clobber = True)

mem.memtrack("before ad.close()", lineno());

ad.close()

mem.memtrack("before del(ad)", lineno()); 

del(ad)

mem.memtrack("sleep .25 seconds", lineno()); 
time.sleep(.25)    
mem.memtrack("end sleep .25 seconds", lineno()); 
예제 #7
0
def mktable_helper(tablename, auto=True, rawdir="./"):
    """
    Create or append to an observation summary table.
    This function is interactive and requires input from the users.

    :param tablename: Filename for the table.  If it exists it will
        be extended.
    :type tablename: str
    :param auto: If True, get some of the information directly from
        the FITS headers.  [Default: True]
    :type auto: boolean
    :param rawdir: Path to raw data.  Required for auto=True.
        [Default: "./"]
    :type rawdir: str
    """
    from klpyastro.utils import obstable
    import os.path
    # TODO: change to new astrodata when ready
    if auto:
        from astrodata import AstroData

    # Create an ObsTable.  If the file already exists on disk,
    # then read it.  Otherwise, leave it empty.
    # Error handling: If the file exists but there's an read error,
    # raise, otherwise assume that you are creating a new file.
    table = obstable.ObsTable()
    table.filename = tablename
    try:
        table.read_table()
    except IOError:
        if os.path.exists(tablename):
            print("Error reading table %s\n" % tablename)
            raise
        else:
            print("New table will be created.")

    # Start the prompting the user and the data for the information
    # that needs to go in the table.

    user_not_done = True
    while user_not_done:
        user_inputs = {}
        if auto:
            filename_not_known = True

        # Get list of prompts for user or data supplied information.
        req_input_list = get_req_input_list()

        # Loop through record elements
        for input_request in req_input_list:
            if not input_request['in_hdr'] or not auto:
                # if auto and this is a Science entry, get the targetname from
                # the header.  If not Science, then you need to prompt user.
                if auto and input_request['id'] == 'targetname' and \
                        'datatype' in user_inputs and \
                                user_inputs['datatype'] == 'Science':

                    input_value = query_header(ad, input_request['id'])
                else:
                    # prompt the user
                    try:
                        input = raw_input
                    except NameError:
                        pass
                    input_value = input(input_request['prompt'])

                user_inputs[input_request['id']] = input_value

                # Assume that the user has a brain.
                # Probe only the first file in 'filerange' since all the
                # files in 'filerange' should be similar.
                #
                # Once we know the name of the first MEF file, open it
                # and keep it open until we're done requesting inputs
                # (instead of opening and closing it every time).
                if auto and filename_not_known:
                    if 'rootname' in user_inputs and 'filerange' in user_inputs:
                        # parse filerange, build filename (with rawdir path)
                        filenumbers = parse_filerange(user_inputs['filerange'])
                        filename = "%sS%04d.fits" % \
                                   (user_inputs['rootname'], filenumbers[0])
                        filename = os.path.join(rawdir, filename)

                        # open ad
                        ad = AstroData(filename)
                        filename_not_known = False
            else:

                # get value from header
                input_value = query_header(ad, input_request['id'])
                user_inputs[input_request['id']] = input_value

        if auto:
            ad.close()

        # Create record
        new_record = create_record(user_inputs)

        # Append to table
        table.add_records_to_table(new_record)

        # Prompt user: add another entry?
        try:
            input = raw_input
        except NameError:
            pass
        answer = input('Add another entry (y/n): ')
        user_not_done = ((answer == 'y') or False)

    # All the info is now in the ObsTable.
    # Write the ObsTable to disk and close everything, we're done.
    table.write_table()
    table.pretty_table()

    return
예제 #8
0
def test_method_close_3():
    ad = AstroData(TESTFILE)
    ad.close()
    with pytest.raises(TypeError):
        ad[0]
예제 #9
0
def test_method_close_2():
    ad = AstroData(TESTFILE)
    ad.close()
    with pytest.raises(AstroDataError):
        ad.append(moredata=hdu1)
예제 #10
0
def test_method_close_1():
    ad = AstroData(TESTFILE)
    ad.close()
    assert not ad.hdulist