<jupyter_start><jupyter_text>SECTION TO INITIALIZE<jupyter_code>import urllib
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import ndimage
# import stuff

import PynPoint

from PynPoint import Pypeline
from PynPoint.IOmodules.Hdf5Reading import Hdf5ReadingModule
from PynPoint.IOmodules.FitsWriting import FitsWritingModule
from PynPoint.IOmodules.FitsReading import FitsReadingModule
from PynPoint.IOmodules.TextReading import ParangReadingModule, \
                                            AttributeReadingModule
from PynPoint.ProcessingModules import PSFpreparationModule, \
                                       PcaPsfSubtractionModule, \
                                       ContrastCurveModule, \
                                       FluxAndPosition, \
                                    StackingAndSubsampling
from PynPoint.ProcessingModules.StarAlignment import StarCenteringModule
#from PynPoint.ProcessingModules.StackingAndSubsampling import DerotateAndStackModule
from PynPoint.ProcessingModules.FluxAndPosition import FakePlanetModule
#from PynPoint.Util import AnalysisTools
import os
from astropy.io import fits
from astropy.modeling import models, fitting
# stem 

stem_base = ('/home/../../media/unasemaje/Elements/lbti_data_reduction/180507_fizeau_altair/')
stem_retrieve = (stem_base+'03_ramp_removed_pca_background_subted/')
stem_place = (stem_base+'04_gross_cookie_cutout/')
stem_place_centered = (stem_base+'05_centered/')
stem_place_fftinfo = (stem_base+'05b_fftinfo/')<jupyter_output><empty_output><jupyter_text>Make PSF cutouts<jupyter_code># Make cookie cut-outs of Fizeau PSFs (note the centering will not be great)
# retrieve from /03_ramp_removed_pca_background_subted/
# deposit in /04_gross_cookie_cutout/
def make_cutout(framenum):
    '''
    Write out cut-outs of PSFs
    '''
    
    img_string = 'lm_180507_'+str("{:0>6d}".format(framenum))+'_nPCA164.fits'
    
    # if file exists
    if os.path.isfile(stem_retrieve+img_string):
        
        # read in image
        sciImg, header = fits.getdata(stem_retrieve+img_string,0,header=True)
        
        if (framenum < 6303):
            psf_loc = [310,850] # (y,x) convention; for most frames <7735, where the PSF is in the up nod position
        elif ((framenum > 6302) and (framenum < 7735)):
            psf_loc = [343,807]
        elif ((framenum > 7772) and (framenum < 7927)):
            psf_loc = [153,824]            
        elif (framenum > 7926):
            psf_loc = [115,865] # (y,x) convention; for frames >7772, where the PSF is in the down nod position
        else:
            print("Frame number not legit!")
        
        # write a cookie-cut frame back out
        hdu = fits.PrimaryHDU(sciImg[psf_loc[0]-50:psf_loc[0]+50,
                                     psf_loc[1]-50:psf_loc[1]+50].astype(np.float64), 
                              header=header)
        hdul = fits.HDUList([hdu])
    
        hdul.writeto(stem_place+img_string, overwrite=True)
        #print('Saved '+str("{:0>6d}".format(framenum)))
        
    else:
        
        print('File '+img_string+' not found')
# make the cutouts

for frameNum in range(6303,7926):
    make_cutout(frameNum)<jupyter_output>File lm_180507_006733_nPCA164.fits not found
File lm_180507_006753_nPCA164.fits not found
File lm_180507_006945_nPCA164.fits not found
File lm_180507_006947_nPCA164.fits not found
File lm_180507_007088_nPCA164.fits not found
File lm_180507_007735_nPCA164.fits not found
File lm_180507_007736_nPCA164.fits not found
File lm_180507_007737_nPCA164.fits not found
File lm_180507_007738_nPCA164.fits not found
File lm_180507_007739_nPCA164.fits not found
File lm_180507_007740_nPCA164.fits not found
File lm_180507_007741_nPCA164.fits not found
File lm_180507_007742_nPCA164.fits not found
File lm_180507_007743_nPCA164.fits not found
File lm_180507_007744_nPCA164.fits not found
File lm_180507_007745_nPCA164.fits not found
File lm_180507_007746_nPCA164.fits not found
File lm_180507_007747_nPCA164.fits not found
File lm_180507_007748_nPCA164.fits not found
File lm_180507_007749_nPCA164.fits not found
File lm_180507_007750_nPCA164.fits not found
File lm_180507_007751_nPCA164.fits not found
File lm_18[...]<jupyter_text>Define workspaces for PynPoint<jupyter_code># define workspaces and initialize Pypeline

working_place = stem_base+"./pynpoint_testing/pynpoint_experimentation_altair/working_place/jan12_working/"
input_place = stem_place
output_place = stem_base+"./pynpoint_testing/pynpoint_experimentation_altair/output_place/jan12_output/"

pipeline = Pypeline(working_place_in=working_place,
                    input_place_in=input_place,
                    output_place_in=output_place)

# now a *.ini file has been generated (this includes the PIXSCALE), if no pre-existing one was there
## IF THE *INI FILE WAS NEWLY GENERATED, EDIT THE *INI FILE TO SET
## PIXSCALE = 0.0107

# confirm PIXSCALE change
'''
pixscale_config = pipeline.get_attribute("config", "PIXSCALE")
print("Plate scale for /config/ is "+pixscale_config)
pixscale_sci = pipeline.get_attribute("science", "PIXSCALE")
print("Plate scale for /science/ is "+pixscale_sci)
'''<jupyter_output><empty_output><jupyter_text>READ IN DATA<jupyter_code># Read in all PSF cookie cut-outs from FITS files 

read_all_psfs = FitsReadingModule(name_in="read_all_psfs",
                                 input_dir=input_place,
                                 image_tag="all_psfs_precentering",
                                 check=True)

pipeline.add_module(read_all_psfs)<jupyter_output><empty_output><jupyter_text>CENTER PSFS<jupyter_code>center_psfs = StarCenteringModule(name_in="center_psfs", 
                                              image_in_tag="all_psfs_precentering", 
                                              image_out_tag="all_psfs_centered", 
                                              mask_out_tag=None, 
                                              fit_out_tag="center_fit", 
                                              method="full", 
                                              interpolation="spline", 
                                              radius=0.16, 
                                              sign="positive")

pipeline.add_module(center_psfs)


# write cube of centered frames (no PA correction)
write_centered_psfs = FitsWritingModule(file_name="centering_test.fits",
                              name_in="write_centered_psfs",
                              output_dir=output_place,
                              data_tag="all_psfs_centered")
                              
pipeline.add_module(write_centered_psfs)
pipeline.run()
########################################################################################################################<jupyter_output><empty_output><jupyter_text>Now upsample and center the images again, without using PynPoint (requires Python 3.5 for plots)<jupyter_code>#%matplotlib qt
# read in cutout frames

def center_cutouts(framenum):
    '''
    Center PSF cutouts without using PynPoint
    '''

    # fit 2D Gaussian
    img_string = 'lm_180507_'+str("{:0>6d}".format(framenum))+'_nPCA164.fits'
    
    # if file exists
    if os.path.isfile(stem_place+img_string):
        
        # read in image
        sciImg, header = fits.getdata(stem_place+img_string,0,header=True)
        
        # get coordinate grid info
        y, x = np.mgrid[0:np.shape(sciImg)[0],0:np.shape(sciImg)[1]]
        z = np.copy(sciImg)
        
        # make an initial Gaussian guess
        p_init = models.Gaussian2D(amplitude=60000.,
                                   x_mean=50.,
                                   y_mean=50.,
                                   x_stddev=6.,
                                   y_stddev=6.)
        fit_p = fitting.LevMarLSQFitter()

        # fit the data
        p = fit_p(p_init, x, y, z)
        ampl, x_mean, y_mean, x_stdev, y_stdev, theat = p._parameters
        
        # Plot the data with the best-fit model
        '''
        plt.clf()
        plt.figure(figsize=(8, 2.5))
        plt.subplot(1, 3, 1)
        plt.imshow(z, origin='lower', interpolation='nearest', vmin=-10, vmax=60000)
        plt.title("Data")
        plt.subplot(1, 3, 2)
        plt.imshow(p(x, y), origin='lower', interpolation='nearest', vmin=-10, vmax=60000)
        plt.title("Model")
        plt.subplot(1, 3, 3)
        plt.imshow(z - p(x, y), origin='lower', interpolation='nearest', vmin=-10, vmax=60000)
        plt.title("Residual")
        plt.suptitle("Frame "+str("{:0>6d}".format(framenum)))
        plt.savefig(stem_place_centered+'pngs_fits/png_fit_lm_180507_'+str("{:0>6d}".format(framenum))+'.png',overwrite=True)
        plt.close()
        '''
                
        # center the frame
        # N.b. for a 100x100 image, the physical center is at Python coordinate (49.5,49.5)
        # i.e., in between pixels 49 and 50 in both dimensions (Python convention),
        # or at coordinate (50.5,50.5) in DS9 convention
        sciImg_shifted = scipy.ndimage.interpolation.shift(sciImg, shift = [49.5-y_mean, 49.5-x_mean]) # shift in +y,+x convention
        
        # write a cookie-cut frame back out
        hdu = fits.PrimaryHDU(sciImg_shifted,header=header)
        hdul = fits.HDUList([hdu])
        hdul.writeto(stem_place_centered+'lm_180507_'+str("{:0>6d}".format(framenum))+'_nPCA164.fits', 
                     overwrite=True)
        
        print("Centered frame "+str("{:0>6d}".format(framenum)))
for frameNum in range(6303,7926):
    center_cutouts(frameNum)<jupyter_output>Centered frame 006303
Centered frame 006304
Centered frame 006305
Centered frame 006306
Centered frame 006307
Centered frame 006308
Centered frame 006309
Centered frame 006310
Centered frame 006311
Centered frame 006312
Centered frame 006313
Centered frame 006314
Centered frame 006315
Centered frame 006316
Centered frame 006317
Centered frame 006318
Centered frame 006319
Centered frame 006320
Centered frame 006321
Centered frame 006322
Centered frame 006323
Centered frame 006324
Centered frame 006325
Centered frame 006326
Centered frame 006327
Centered frame 006328
Centered frame 006329
Centered frame 006330
Centered frame 006331
Centered frame 006332
Centered frame 006333
Centered frame 006334
Centered frame 006335
Centered frame 006336
Centered frame 006337
Centered frame 006338
Centered frame 006339
Centered frame 006340
Centered frame 006341
Centered frame 006342
Centered frame 006343
Centered frame 006344
Centered frame 006345
Centered frame 006346
Centered frame 006347
Centered f[...]
{'fraction_non_alphanumeric': 0.09433251741668236, 'fraction_numerical': 0.08002259461495011, 'mean_word_length': 2.83086909484313, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '330750', 'license': 'MIT', 'n_tokens_mistral': 4062, 'n_tokens_neox': 3428, 'n_words': 748, 'path': 'pynpoint_testing_altair_3.ipynb', 'repo_name': 'mwanakijiji/lbti_altair_fizeau', 'size': '10622'}