<commit_before>import numpy as np
import astropy
from astropy.coordinates import SkyCoord
from astropy import units as u
from astropy.io import fits
from astropy import wcs
import pickle
import dill
import XIDp_mod_beta as xid_mod
import os
import sys

#----output folder-----------------
output_folder='/research/astro/fir/HELP/XID_plus_output/100micron/uniform_prior/'

with open(output_folder+'Tiling_info.pkl', "rb") as f:
        obj = pickle.load(f)

tiles=obj['tiles']
tiling_list=obj['tiling_list']
nsources=tiling_list.shape[0]
sources_percentile=np.empty((nsources,14))
#hdulist_master=xid_mod.create_empty_XIDp_SPIREcat(nsources)
stan_fit_master=np.empty((500,4,(nsources+1.0)*3))
for i in np.arange(0,len(tiles)):
    print 'on tile '+str(i)+' of '+str(len(tiles))
    tile=tiles[i]
    #find which sources from master list are we interested in
    ind= (np.around(tiling_list[:,2],3) == np.around(tile[0,0],3)) & (np.around(tiling_list[:,3],3) == np.around(tile[1,0],3))
    if ind.sum() >0:
	    infile=output_folder+'lacy_uniform_fluxprior_'+str(tile[0,0]).replace('.','_')+'p'+str(tile[1,0]).replace('.','_')+'.pkl'
	    with open(infile, "rb") as f:
		dictname = pickle.load(f)
	    prior250=dictname['psw']
	    prior350=dictname['pmw']    
	    prior500=dictname['plw']
	    print '----wcs----'
	    print prior250.wcs._naxis1,prior250.wcs._naxis2
	    print '---------------'
	    posterior=dictname['posterior']
	    #posterior.stan_fit=np.power(10.0,posterior.stan_fit)
	    
	    #hdulist=xid_mod.create_XIDp_SPIREcat(posterior,prior250,prior350,prior500)
	    #table=hdulist[1].data
	    #hdulist.writeto(output_folder+'lacy_rband_19_8_normal_log10fluxprior_'+str(tile[0,0]).replace('.','_')+'p'+str(tile[1,0]).replace('.','_')+'.fits')

	    #match interested sources to those fitted
	    #c = SkyCoord(ra=table['ra']*u.degree, dec=table['dec']*u.degree)
	    c = SkyCoord(ra=prior250.sra*u.degree, dec=prior250.sdec*u.degree)

	    c2 = SkyCoord(ra=tiling_list[ind,0]*u.degree, dec=tiling_list[ind,1]*u.degree)
	    #get indices in prior list which match those in master list
	    idx, d2d, d3d = c2.match_to_catalog_sky(c)
	    #for i in range(len(hdulist_master[1].data.columns)):
		    
		#    hdulist_master[1].data.field(i)[ind]=table.field(i)[idx]

	    #sources_percentile[ind,:]=table[idx][1:]

	    #---fill in master stan_fit_array
	    stan_ind_oneband=np.append(ind,False)#don't add background
	    stan_ind=np.append(stan_ind_oneband,stan_ind_oneband)#adjoin psw and pmw
	    stan_ind=np.append(stan_ind,stan_ind_oneband)#adjoin with plw

	    idx_allbands=np.append(np.append(idx,prior250.nsrc+1+idx),2*prior250.nsrc+2+idx)
	    print stan_fit_master[:,:,stan_ind].shape,posterior.stan_fit[:,:,idx_allbands].shape,ind.sum(),idx.size
	    stan_fit_master[:,:,stan_ind]=posterior.stan_fit[:,:,idx_allbands]



output_folder='/research/astro/fir/HELP/XID_plus_output/Tiling/uniform_prior/'

#-------------since wcs info isnt saving properly, get it from original maps---------
#Folder containing maps
imfolder='/research/astro/fir/cclarke/lacey/released/'
#field
field='COSMOS'
#SMAP version
SMAPv='4.2'

pswfits=imfolder+'cosmos_itermap_lacey_07012015_simulated_observation_w_noise_PSW_hipe.fits.gz'#SPIRE 250 map
pmwfits=imfolder+'cosmos_itermap_lacey_07012015_simulated_observation_w_noise_PMW_hipe.fits.gz'#SPIRE 350 map
plwfits=imfolder+'cosmos_itermap_lacey_07012015_simulated_observation_w_noise_PLW_hipe.fits.gz'#SPIRE 500 map
<commit_msg>file output issue combine posteriors<commit_after>import numpy as np
import astropy
from astropy.coordinates import SkyCoord
from astropy import units as u
from astropy.io import fits
from astropy import wcs
import pickle
import dill
import XIDp_mod_beta as xid_mod
import os
import sys

#----output folder-----------------
output_folder='/research/astro/fir/HELP/XID_plus_output/100micron/uniform_prior/'

with open(output_folder+'Tiling_info.pkl', "rb") as f:
        obj = pickle.load(f)

tiles=obj['tiles']
tiling_list=obj['tiling_list']
nsources=tiling_list.shape[0]
sources_percentile=np.empty((nsources,14))
#hdulist_master=xid_mod.create_empty_XIDp_SPIREcat(nsources)
stan_fit_master=np.empty((500,4,(nsources+1.0)*3))
for i in np.arange(0,len(tiles)):
    print 'on tile '+str(i)+' of '+str(len(tiles))
    tile=tiles[i]
    #find which sources from master list are we interested in
    ind= (np.around(tiling_list[:,2],3) == np.around(tile[0,0],3)) & (np.around(tiling_list[:,3],3) == np.around(tile[1,0],3))
    if ind.sum() >0:
	    infile=output_folder+'lacy_uniform_fluxprior_'+str(tile[0,0]).replace('.','_')+'p'+str(tile[1,0]).replace('.','_')+'.pkl'
	    with open(infile, "rb") as f:
		dictname = pickle.load(f)
	    prior250=dictname['psw']
	    prior350=dictname['pmw']    
	    prior500=dictname['plw']
	    print '----wcs----'
	    print prior250.wcs._naxis1,prior250.wcs._naxis2
	    print '---------------'
	    posterior=dictname['posterior']
	    #posterior.stan_fit=np.power(10.0,posterior.stan_fit)
	    
	    #hdulist=xid_mod.create_XIDp_SPIREcat(posterior,prior250,prior350,prior500)
	    #table=hdulist[1].data
	    #hdulist.writeto(output_folder+'lacy_rband_19_8_normal_log10fluxprior_'+str(tile[0,0]).replace('.','_')+'p'+str(tile[1,0]).replace('.','_')+'.fits')

	    #match interested sources to those fitted
	    #c = SkyCoord(ra=table['ra']*u.degree, dec=table['dec']*u.degree)
	    c = SkyCoord(ra=prior250.sra*u.degree, dec=prior250.sdec*u.degree)

	    c2 = SkyCoord(ra=tiling_list[ind,0]*u.degree, dec=tiling_list[ind,1]*u.degree)
	    #get indices in prior list which match those in master list
	    idx, d2d, d3d = c2.match_to_catalog_sky(c)
	    #for i in range(len(hdulist_master[1].data.columns)):
		    
		#    hdulist_master[1].data.field(i)[ind]=table.field(i)[idx]

	    #sources_percentile[ind,:]=table[idx][1:]

	    #---fill in master stan_fit_array
	    stan_ind_oneband=np.append(ind,False)#don't add background
	    stan_ind=np.append(stan_ind_oneband,stan_ind_oneband)#adjoin psw and pmw
	    stan_ind=np.append(stan_ind,stan_ind_oneband)#adjoin with plw

	    idx_allbands=np.append(np.append(idx,prior250.nsrc+1+idx),2*prior250.nsrc+2+idx)
	    print stan_fit_master[:,:,stan_ind].shape,posterior.stan_fit[:,:,idx_allbands].shape,ind.sum(),idx.size
	    stan_fit_master[:,:,stan_ind]=posterior.stan_fit[:,:,idx_allbands]




#-------------since wcs info isnt saving properly, get it from original maps---------
#Folder containing maps
imfolder='/research/astro/fir/cclarke/lacey/released/'
#field
field='COSMOS'
#SMAP version
SMAPv='4.2'

pswfits=imfolder+'cosmos_itermap_lacey_07012015_simulated_observation_w_noise_PSW_hipe.fits.gz'#SPIRE 250 map
pmwfits=imfolder+'cosmos_itermap_lacey_07012015_simulated_observation_w_noise_PMW_hipe.fits.gz'#SPIRE 350 map
plwfits=imfolder+'cosmos_itermap_lacey_07012015_simulated_observation_w_noise_PLW_hipe.fits.gz'#SPIRE 500 map

{'fraction_non_alphanumeric': 0.1636284722222222, 'fraction_numerical': 0.03674768518518518, 'mean_word_length': 5.13943161634103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5255379', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2684, 'n_words': 375}