def test_library_file(test_directory):
    """Download and save locally a segment PSF library file from Box for testing

    Yields
    -------
    test_lib_filename : str
        Path to test library used for testing
    """
    # Download file and yield its name
    test_lib_filename = os.path.join(test_directory, 'test_library',
                                     'nircam_nrca3_f212n_fovp1024_samp1_npsf1_seg12.fits')
    ensure_dir_exists(os.path.dirname(test_lib_filename))
    url = "https://stsci.box.com/shared/static/4c0em1yhb1qsvrpku7j0jw1tztucvkih.fits"
    with fits.open(url) as hdulist:
        hdulist.writeto(test_lib_filename)
        yield test_lib_filename
示例#2
0
def path_check():
    """Check that the CRDS_PATH environment variable is set. This will be
    the location to which CRDS reference files are downloaded. If the env
    variable is not set, default to use $HOME/crds_cache/

    Returns
    -------
    crds_path : str
        Full path to the location of the CRDS reference files
    """
    crds_path = os.environ.get('CRDS_PATH')
    if crds_path is None:
        reffile_dir = '{}/crds_cache'.format(os.environ.get('HOME'))
        os.environ["CRDS_PATH"] = reffile_dir
        ensure_dir_exists(reffile_dir)
        print('CRDS_PATH environment variable not set. Setting to {}'.format(reffile_dir))
        return reffile_dir
    else:
        return crds_path
示例#3
0
def test_directory(test_dir=TEMP_TEST_DIRECTORY):
    """Create a test directory.

    Parameters
    ----------
    test_dir : str
        Path to directory used for testing

    Yields
    -------
    test_dir : str
        Path to directory used for testing
    """
    # Create directory and yield its name
    ensure_dir_exists(test_dir)  # creates directory with default mode=511
    yield test_dir

    # Remove directory
    if os.path.isdir(test_dir):
        shutil.rmtree(test_dir)
示例#4
0
def download_reffiles(directory,
                      instrument='all',
                      dark_type='linearized',
                      skip_darks=False,
                      skip_cosmic_rays=False,
                      skip_psfs=False):
    """Download tarred and gzipped reference files. Expand, unzip and
    organize into the necessary directory structure such that Mirage
    can use them.

    Parameters
    ----------
    directory : str
        Directory into which the reference files are placed. This will
        be the directory set to the MIRAGE_DATA environment variable

    instrument : str
        Instruments for which to download data. Single string with
        comma-separated instrument names.

        If ``all``: download files for NIRCam, NIRISS, and FGS.

        If the name of an individual instrument (e.g. 'nircam'),
        download only the data for that instrument.

        If a list of instrument names, (e.g. 'nircam, niriss') download
        all data for those instruments.

    dark_type : str
        Type of dark current files to download. Options are:
        'linearized': download linearize dark current ramps
        'raw': download raw dark current ramps
        'both': download both raw and linearized dark current ramps

    skip_darks : bool
        If False (default), download the requested darks. If True,
        do not download the darks

    skip_comsic_rays : bool
        If False (default), download the requested cosmic ray libraries.
        If True, do not download the cosmic ray library.

    skip_psfs : bool
        If False (default), download the requested PSF libraries.
        If True, do not download the libraries.
    """
    # Be sure the input instrument is a list
    file_list = get_file_list(instrument.lower(),
                              dark_type.lower(),
                              skip_darks=skip_darks,
                              skip_cosmic_rays=skip_cosmic_rays,
                              skip_psfs=skip_psfs)

    # Download everything first
    for file_url in file_list:
        filename = os.path.split(file_url)[-1]
        local_file = os.path.join(directory, filename)
        download_file(file_url, filename, directory)

    # Now untar/organize. This way if the download is interrupted, it can
    # pick up where it left off, since no downloaded files will have been
    # moved yet.
    for file_url in file_list:
        filename = os.path.split(file_url)[-1]
        local_file = os.path.join(directory, filename)
        # Unzip and untar file
        if 'tar.gz' in local_file:
            print('Unzipping/extracting {}'.format(filename))
            file_object = tarfile.open(name=local_file, mode='r:gz')
            file_object.extractall(path=directory)
        else:
            # Darks need to be unzipped into the correct directory
            if 'linearized' in filename.lower():
                cal = 'linearized'
            else:
                cal = 'raw'

            # Determine directory
            if 'NRCNRC' in filename:
                det_str = filename.split('NRCNRC')[1].split('-')[0]
                if 'LONG' in det_str:
                    det_str.replace('LONG', '5')
                darks_dir = os.path.join(directory, 'mirage_data', 'nircam',
                                         'darks')
                sub_directory = os.path.join(darks_dir, cal, det_str)
            elif 'NIRISS' in filename:
                darks_dir = os.path.join(directory, 'mirage_data', 'niriss',
                                         'darks')
                sub_directory = os.path.join(darks_dir, cal)
                #sub_directory = os.path.join(directory, 'mirage_data', 'niriss', 'darks', cal)
            elif 'FGS' in filename:
                darks_dir = os.path.join(directory, 'mirage_data', 'fgs',
                                         'darks')
                sub_directory = os.path.join(darks_dir, cal)

            # Create the directory if it does not yet exist
            ensure_dir_exists(darks_dir)
            ensure_dir_exists(os.path.join(darks_dir, cal))
            ensure_dir_exists(sub_directory)

            final_location = os.path.join(sub_directory, filename)

            # Move the zipped file into the correct subdirectory
            if not os.path.isfile(final_location):
                print('Moving {} to {}'.format(filename, sub_directory))
                shutil.move(local_file, final_location)

            # Unzip
            unzipped_filename = final_location.replace('.gz', '')
            if not os.path.isfile(unzipped_filename):
                unzip_file(final_location)
            else:
                print(
                    'Unzipped file {} already exists. Skipping unzip.'.format(
                        unzipped_filename))

    full_dir = os.path.abspath(directory)
    print(('Mirage reference files downloaded and extracted. \nBefore '
           'using Mirage, be sure to set the MIRAGE_DATA environment '
           'variable to point to:\n{}/mirage_data'.format(full_dir)))
    print('In bash: ')
    print('export MIRAGE_DATA="{}"'.format(
        os.path.join(full_dir, 'mirage_data')))
示例#5
0
from mirage.catalogs.catalog_generator import GrismTSOCatalog, ImagingTSOCatalog, PointSourceCatalog
from mirage.catalogs.catalog_generator import TSO_GRISM_INDEX
from mirage.grism_tso_simulator import GrismTSO
from mirage.imaging_simulator import ImgSim
from mirage.seed_image.catalog_seed_image import Catalog_seed
from mirage.utils.utils import ensure_dir_exists
from mirage.yaml import yaml_generator

# ----------   Define Paths to organize Inputs and Outputs  ----------

input_data_path = os.path.abspath(
    '/home/anadkarni/NIRCam_AZ_TSO_Data_Challenge/GJ436_apt_data/')
output_dir = '/home/anadkarni/NIRCam_AZ_TSO_Data_Challenge/GJ436_2102/'
output_yaml_dir = os.path.abspath(
    '/home/anadkarni/NIRCam_AZ_TSO_Data_Challenge/GJ436_2102/GJ436_yaml_data/')
ensure_dir_exists(output_yaml_dir)
output_data_dir = os.path.abspath(
    '/home/anadkarni/NIRCam_AZ_TSO_Data_Challenge/GJ436_2102/GJ436_sim_data/')
ensure_dir_exists(output_data_dir)
tsdir = '/home/anadkarni/NIRCam_AZ_TSO_Data_Challenge/GJ436_LC_TS_Params/'

# ----------  Prepare Inputs  ----------

xml_file = os.path.join(input_data_path, 'GJ436.xml')
pointing_file = xml_file.replace('.xml', '.pointing')

# ----------  Stellar Spectrum  ----------

t_eff = 3500  # surface temperature
metallicity = 0.02  # Fe/H
log_g = 5.0  # surface gravity = 182 m/s^2