<commit_before>#!/usr/bin/env python
 
from astropy.io import fits
from crrlpy.crrls import natural_sort

import logging
import sys
import re
import os
import argparse
import numpy as np

blankval = np.nan

def get_cube_dims(fitslist, chan_id='chan'):
    hdulist = fits.open(fitslist[0])
    head = hdulist[0].header
    nx = head['NAXIS1']
    ny = head['NAXIS2']
    #nv = len(fitslist)
    ch0 = int(re.search(r'{0}(.+?)\.'.format(chan_id), fitslist[0]).group(1))
    #print "file {0} is channel {1}".format(fitslist[0], ch0)
    chf = int(re.search(r'{0}(.+?)\.'.format(chan_id), fitslist[-1]).group(1))
    #print "file {0} is channel {1}".format(fitslist[-1], chf)
    nv = chf - ch0
    
    return nx, ny, nv, ch0

def make_fits(data):
    hdu = fits.PrimaryHDU(data)
    hdulist = fits.HDUList([hdu])
    return hdulist

def update_header(header, fitslist):
    hdulist = fits.open(fitslist[0])
    head = hdulist[0].header
    # copy the header from the first fits in the list
    header = head.copy()
    #for key in head.keys():
        ##if key != 'CRVAL4':
        #if key:
            #print "{0}:  {1}".format(key, head[key])
            #header.append((key, head[key]))
        
            
    #header.append('BSCALE', head['BSCALE'])
    #header.append('BZERO', head['BZERO'])
    #header.append('BMAJ', head['BMAJ'])
    #header.append('BMIN', head['BMIN'])
    #header.append('BPA', head['BPA'])
    #header.append('BTYPE', head['BTYPE'])
    #header.append('BUNIT', head['BUNIT'])
    #header.append('EQUINOX', head['EQUINOX'])
    #header.append('EQUINOX', head['LONPOLE'])

def main(outfits, fitslist, stokeslast=True, chan_id='chan', nzeros=4, clobber=False):
    
    # Get cube dimensions from first image
    nx, ny, nv, ch0 = get_cube_dims(fitslist, chan_id)
    print "Starting channel: {0}".format(ch0)
    print "Number of channels: {0}".format(nv)
    # Create the cube, only stokes I
    cube = np.empty([1, nv, ny, nx], dtype=float)
    
    # Get the first channel number
    #ch0 = int(re.search('chan(.+?).', fitslist[0]))
    #pch = ch0
    
    for i in range(nv):
        print '{0}{1}.'.format(chan_id, str(i+ch0).zfill(nzeros))
        fitsch = filter(lambda x: '{0}{1}.'.format(chan_id, str(i+ch0).zfill(nzeros)) in x, fitslist)
        if fitsch:
            print fitsch
            hdulist = fits.open(fitsch[0])
            data = hdulist[0].data[0][0]
        else:
            print "Inserting blank channel"
            data = np.ones([ny, nx])*blankval
        cube[0,i,:,:] = data
    
    # Loop over images copying their data to the cube
    #for i,image in enumerate(fitslist):
        #ch = int(re.search('chan(.+?).', fitslist[0]))
        #if ch - pch != 1:
            #for j in range(ch - pch):
                #cube[0,i+j,:,:] = data
        #hdulist = fits.open(image)
        ##head = hdulist[0].header<commit_msg>Removed need for dot after ch_id in ch2cube.py<commit_after>#!/usr/bin/env python
 
from astropy.io import fits
from crrlpy.crrls import natural_sort

import logging
import sys
import re
import os
import argparse
import numpy as np

blankval = np.nan

def get_cube_dims(fitslist, chan_id='chan'):
    hdulist = fits.open(fitslist[0])
    head = hdulist[0].header
    nx = head['NAXIS1']
    ny = head['NAXIS2']
    #nv = len(fitslist)
    regex = r'{0}(.+?)(\.|\_)'.format(chan_id)
    ch0 = int(re.search(r'{0}(.+?)(\.|\_)'.format(chan_id), fitslist[0]).group(1))
    #print "file {0} is channel {1}".format(fitslist[0], ch0)
    chf = int(re.search(r'{0}(.+?)(\.|\_)'.format(chan_id), fitslist[-1]).group(1))
    #print "file {0} is channel {1}".format(fitslist[-1], chf)
    nv = chf - ch0
    
    return nx, ny, nv, ch0

def make_fits(data):
    hdu = fits.PrimaryHDU(data)
    hdulist = fits.HDUList([hdu])
    return hdulist

def update_header(header, fitslist):
    hdulist = fits.open(fitslist[0])
    head = hdulist[0].header
    # copy the header from the first fits in the list
    header = head.copy()
    #for key in head.keys():
        ##if key != 'CRVAL4':
        #if key:
            #print "{0}:  {1}".format(key, head[key])
            #header.append((key, head[key]))
        
            
    #header.append('BSCALE', head['BSCALE'])
    #header.append('BZERO', head['BZERO'])
    #header.append('BMAJ', head['BMAJ'])
    #header.append('BMIN', head['BMIN'])
    #header.append('BPA', head['BPA'])
    #header.append('BTYPE', head['BTYPE'])
    #header.append('BUNIT', head['BUNIT'])
    #header.append('EQUINOX', head['EQUINOX'])
    #header.append('EQUINOX', head['LONPOLE'])

def main(outfits, fitslist, stokeslast=True, chan_id='chan', nzeros=4, clobber=False):
    
    # Get cube dimensions from first image
    nx, ny, nv, ch0 = get_cube_dims(fitslist, chan_id)
    print "Starting channel: {0}".format(ch0)
    print "Number of channels: {0}".format(nv)
    # Create the cube, only stokes I
    cube = np.empty([1, nv, ny, nx], dtype=float)
    
    # Get the first channel number
    #ch0 = int(re.search('chan(.+?).', fitslist[0]))
    #pch = ch0
    
    for i in range(nv):
        print '{0}{1}.'.format(chan_id, str(i+ch0).zfill(nzeros))
        fitsch = filter(lambda x: '{0}{1}'.format(chan_id, str(i+ch0).zfill(nzeros)) in x, fitslist)
        if fitsch:
            print fitsch
            hdulist = fits.open(fitsch[0])
            data = hdulist[0].data[0][0]
        else:
            print "Inserting blank channel"
            data = np.ones([ny, nx])*blankval
        cube[0,i,:,:] = data
    
    # Loop over images copying their data to the cube
    #for i,image in enumerate(fitslist):
        #ch = int(re.search('chan(.+?).', fitslist[0]))
        #if ch - pch != 1:
            #for j in range(ch - pch):
                #cube[0,i+j,:,:] = data
        #hdulist = fits.open(image)
        ##head = hdulist[0].header
{'fraction_non_alphanumeric': 0.1752436313899812, 'fraction_numerical': 0.017096939647803042, 'mean_word_length': 2.80859375, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1311350', 'n_tokens_mistral': 2260, 'n_tokens_neox': 2182, 'n_words': 490}