<commit_before>from . import first_look
import os
import numpy as np
from spectral_cube import SpectralCube
import astropy.units as u
import textwrap
from astropy.table import Table, join
from . import gasPipeline


quit_message=textwrap.dedent("""\
    Release parameters not defined. This region is either not
    processed in this release or it is not yet implemented.""")

def GenerateRegions():
    gasPipeline.updateLogs()
    gasPipeline.updateCatalog()
    obs = Table.read('ObservationLog.csv')
    cat = Table.read('RegionCatalog.csv')

# This takes out rows that are empty
# This needs to be done twice for some reason... 
    for idx, row in enumerate(cat):
        if not row['BoxName']:
            cat.remove_row(idx)

    for idx, row in enumerate(cat):
        if not row['BoxName']:
            cat.remove_row(idx)
    obs.rename_column('Source','BoxName')
    joincat = join(obs,cat,keys='BoxName')
    groupcat = joincat.group_by('Region name')
    min_values = groupcat.groups.aggregate(np.min)
    max_values = groupcat.groups.aggregate(np.max)
    mean_values = groupcat.groups.aggregate(np.mean)
    vavg = 0.5*(min_values['VLSR'] + max_values['VLSR'])
    vrange = max_values['VLSR']- min_values['VLSR']
    mean_values['VAVG'] = vavg
    mean_values['VRANGE'] = vrange

    return(mean_values)
            
def FirstLook(regions=None, file_extension='_all',
              region_parameters='region_parameters.csv'):

    RegionCatalog = GenerateRegions()
    for ThisRegion in RegionCatalog:
        region_name=ThisRegion['Region name']
        print("Now NH3(1,1)")
        a_rms = [  0, 150, 310, 420, 530, 690]
        b_rms = [ 60, 230, 330, 440, 610, 760]
        index_rms=first_look.create_index(a_rms, b_rms)
        index_peak=np.arange(340,420)
        file_in='{0}/{0}_NH3_11{1}.fits'.format(region_name,file_extension)
        file_out=file_in.replace(file_extension+'.fits',
                                 '_base'+file_extension+'.fits')

        first_look.baseline( file_in, file_out, index_clean=index_rms, polyorder=1)
        first_look.peak_rms( file_out, index_rms=index_rms, index_peak=index_peak)
    # 
        linelist = ['NH3_22','NH3_33','C2S','HC5N','HC7N_21_20','HC7N_22_21']
        vsys = ThisRegion['VAVG']*u.km/u.s
        throw = 5*u.km/u.s + ThisRegion['VRANGE']*u.km/u.s/2
        for line in linelist:
            file_in = '{0}/{0}_{1}{2}.fits'.format(region_name,line,file_extension)
            s = SpectralCube.read(file_in)
            s = s.with_spectral_unit(u.km/u.s,velocity_convention='radio')
            a_rms = [s.closest_spectral_channel(vsys+2*throw),
                     s.closest_spectral_channel(vsys-throw)]
            b_rms = [s.closest_spectral_channel(vsys+throw),
                     s.closest_spectral_channel(vsys-2*throw)]
            index_peak = np.arange(s.closest_spectral_channel(vsys+3*u.km/u.s),
                                   s.closest_spectral_channel(vsys-3*u.km/u.s))
            index_rms=first_look.create_index( a_rms, b_rms)
            
            file_out=file_in.replace(file_extension+'.fits',
                                     '_base'+file_extension+'.fits')
            first_look.baseline( file_in, file_out, 
                                 index_clean=index_rms, polyorder=1)
            first_look.peak_rms( file_out, index_rms=index_rms, 
                                 index_peak=index_peak)
    

    

def FirstLook_OrionA(file_extension='_all'):
    """
    Function to create First Look products for OrionA. The file_extension 
    parameter is used to select the proper files to be processed. 
    """
    region_name='OrionA'
    print("Now NH3(1,1)")
    a_rms = [  0, 158, 315, 428, 530, 693]
    b_rms = [ 60, 230, 327, 438, 604, 735]
    index_rms=first_look.create_index( a_rms, b_rms)
    index_peak=np.arange(326,470)<commit_msg>Trap missing files<commit_after>from . import first_look
import os
import numpy as np
from spectral_cube import SpectralCube
import astropy.units as u
import textwrap
from astropy.table import Table, join
from . import gasPipeline


quit_message=textwrap.dedent("""\
    Release parameters not defined. This region is either not
    processed in this release or it is not yet implemented.""")

def GenerateRegions():
    gasPipeline.updateLogs()
    gasPipeline.updateCatalog()
    obs = Table.read('ObservationLog.csv')
    cat = Table.read('RegionCatalog.csv')

# This takes out rows that are empty
# This needs to be done twice for some reason... 
    for idx, row in enumerate(cat):
        if not row['BoxName']:
            cat.remove_row(idx)

    for idx, row in enumerate(cat):
        if not row['BoxName']:
            cat.remove_row(idx)
    obs.rename_column('Source','BoxName')
    joincat = join(obs,cat,keys='BoxName')
    groupcat = joincat.group_by('Region name')
    min_values = groupcat.groups.aggregate(np.min)
    max_values = groupcat.groups.aggregate(np.max)
    mean_values = groupcat.groups.aggregate(np.mean)
    vavg = 0.5*(min_values['VLSR'] + max_values['VLSR'])
    vrange = max_values['VLSR']- min_values['VLSR']
    mean_values['VAVG'] = vavg
    mean_values['VRANGE'] = vrange

    return(mean_values)
            
def FirstLook(regions=None, file_extension='_all',
              region_parameters='region_parameters.csv'):

    RegionCatalog = GenerateRegions()
    for ThisRegion in RegionCatalog:
        region_name=ThisRegion['Region name']
        print("Now NH3(1,1)")
        a_rms = [  0, 150, 310, 420, 530, 690]
        b_rms = [ 60, 230, 330, 440, 610, 760]
        index_rms=first_look.create_index(a_rms, b_rms)
        index_peak=np.arange(340,420)
        file_in='{0}/{0}_NH3_11{1}.fits'.format(region_name,file_extension)
        file_out=file_in.replace(file_extension+'.fits',
                                 '_base'+file_extension+'.fits')

        try:
            first_look.baseline( file_in, file_out, index_clean=index_rms, polyorder=1)
            first_look.peak_rms( file_out, index_rms=index_rms, index_peak=index_peak)
        except IOError:
            warnings.warn("File not found: {0}".format(file_in))

        linelist = ['NH3_22','NH3_33','C2S','HC5N','HC7N_21_20','HC7N_22_21']
        vsys = ThisRegion['VAVG']*u.km/u.s
        throw = 5*u.km/u.s + ThisRegion['VRANGE']*u.km/u.s/2
        for line in linelist:
            file_in = '{0}/{0}_{1}{2}.fits'.format(region_name,line,file_extension)
            try:
                s = SpectralCube.read(file_in)

                s = s.with_spectral_unit(u.km/u.s,velocity_convention='radio')
                a_rms = [s.closest_spectral_channel(vsys+2*throw),
                         s.closest_spectral_channel(vsys-throw)]
                b_rms = [s.closest_spectral_channel(vsys+throw),
                         s.closest_spectral_channel(vsys-2*throw)]
                index_peak = np.arange(s.closest_spectral_channel(vsys+3*u.km/u.s),
                                       s.closest_spectral_channel(vsys-3*u.km/u.s))
                index_rms=first_look.create_index( a_rms, b_rms)

                file_out=file_in.replace(file_extension+'.fits',
                                         '_base'+file_extension+'.fits')
                first_look.baseline( file_in, file_out, 
                                     index_clean=index_rms, polyorder=1)
                first_look.peak_rms( file_out, index_rms=index_rms, 
                                     index_peak=index_peak)
            except IOError:
                warnings.warn("File not found {0}".format(file_in)

    

def FirstLook_OrionA(file_extension='_all'):
    """
    Function to create First Look products for OrionA. The file_extension 
    parameter is used to select the proper files to be processed. 
    """
    region_name='OrionA'
    print("Now NH3(1,1)")
    a_rms = [  0, 158, 315, 428, 530, 693]
    b_rms = [ 60, 230, 327, 438, 604, 735]
    index_rms=first_look.create_index( a_rms, b_rms)
    index_peak=np.arange(326,470)
{'fraction_non_alphanumeric': 0.11818295270185948, 'fraction_numerical': 0.03070011231748409, 'mean_word_length': 2.6034172661870505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1961802', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2674, 'n_words': 493}