pycode.py 6.95 KB
Newer Older
Zhang Xin's avatar
init  
Zhang Xin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'''
Author: Zhang Xin zhangx@bao.ac.cn
Date: 2024-01-02 13:34:39
LastEditors: Zhang Xin zhangx@bao.ac.cn
LastEditTime: 2024-03-25 13:51:33
FilePath: /csst-simulation/Users/zhangxin/Work/SlitlessSim/CSST_SIM/Star_spec/csst_spec_interp_clean/code/pycode.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
##运行下面在mac/linux下执行 
# cc -fPIC -shared main_singlestar.c -I/usr/include -I/usr/include/cfitsio -lcfitsio -lm -o test.dylib

import os
import numpy as np
from astropy.io import fits
import ctypes
from astropy.table import Table
import produceSED
# from ctypes import *

# struct STAR
# {
#   float logte;
#   float logg;
#   //float logL;
#   float Mass;
#   float Av;
#   float mu0;
#   float Z;
#   //float mbolmag;
# };

class Star(ctypes.Structure):
    _fields_ = [
               ('logte',ctypes.c_float),
               ('logg',ctypes.c_float),
               ('Mass',ctypes.c_float),
               ('Av', ctypes.c_float),
               ('mu0', ctypes.c_float),
               ('Z', ctypes.c_float)]

#CHANGE
#topdir='/run/media/chen/1TS/dupe/gitlab/csst_spec_interp/'
topdir='/home/zhangxin/CSST_SIM/star_spec/csst_spec_interp_clean/code/'
code_name='test.so'

d = ctypes.CDLL(os.path.join(topdir,code_name))
d.loadSpecLibs.argtypes=[ctypes.c_char_p, ctypes.c_char_p]
d.loadExts.argtypes=[ctypes.c_char_p]


#######################################################################################
#CHANGE
#nwv = d.loadSpecLibs(str.encode(os.path.join(topdir,'file_CK04.par')),str.encode(topdir))
#d.loadExts(str.encode(os.path.join(topdir,"spec/Ext_odonnell94_R3.1_CK04W.fits")))
#TO
nwv = d.loadSpecLibs(str.encode(os.path.join(topdir,'file_BT-Settl_CSST_wl1000-24000_R1000.par')),str.encode(topdir))
d.loadExts(str.encode(os.path.join(topdir,"spec/Ext_odonnell94_R3.1_CSST_wl1000-24000_R1000.fits")))
########################################################################################

print("Done loading Exts")
spec = (ctypes.c_float*nwv)()
wave = (ctypes.c_float*nwv)()
d.interpSingleStar.argtypes=[ctypes.Structure, ctypes.POINTER(ctypes.c_float)]


'''
#example for a single star
#s=Star(3.620752, 4.701155, 0.599979, 0.067540, 11.200001, 0.008501)
s=Star(3.9739766, 8.108173, 0.6525841, 0.077022046, 9.05, 0.024376422)
#s=Star(3.9739766, 4.99, 0.6525841, 0.077022046, 9.05, 0.024376422)
d.interpSingleStar(s, spec)
spec_ = spec[:]
print(spec[500:509])
'''

from astropy.table import Table
catalogFn = "/nfsdata/nfsdata/share/CSSTsimInputCat_TH/C9_RA300_DECm60.fits"
cat = Table.read(catalogFn)

filters = ['nuv','u','g','r','i','z','y']
filters_other = ['2MASS_H','2MASS_J','2MASS_Ks','GAIA_GAIA3.G','GAIA_GAIA3.Gbp','GAIA_GAIA3.Grp','GALEX_GALEX.FUV','GALEX_GALEX.NUV','LSST_u','LSST_g','LSST_r','LSST_i','LSST_z','LSST_y','PAN-STARRS_PS1.g','PAN-STARRS_PS1.r','PAN-STARRS_PS1.i','PAN-STARRS_PS1.z','PAN-STARRS_PS1.y','SLOAN_SDSS.u','SLOAN_SDSS.g','SLOAN_SDSS.r','SLOAN_SDSS.i','SLOAN_SDSS.z']
res = {}

nrows = len(cat)
for fi in filters:
    res[fi] = np.zeros(nrows)
for fi in filters_other:
    res[fi] = np.zeros(nrows)

from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
rank_size = comm.Get_size()
print("------------------",rank_size, rank)
iterNum = 0    
for star in cat:
    if iterNum%10000==0:
        print(iterNum)
    if iterNum > 1:
        iterNum = iterNum + 1
        break
    if iterNum % rank_size != rank:
        iterNum = iterNum + 1
        continue
    specTable = np.zeros([nwv,2])
    s=Star(star['mwmsc_logte'], star['mwmsc_logg'], 1., star['mwmsc_av'], star['mwmsc_mu0'], star['mwmsc_z'])
    #print("star[logTe], star[logg], star[Mass], star[Av], star[mu0], star[Z]: ", star['logTe'], star['logg'], star['Mass'], star['Av'], star['mu0'], star['Z'])
    d.interpSingleStar(s, spec, wave)
    specTable[:,0] = wave[:]
    specTable[:,1] = spec[:]
    # print(spec[500:509])
    spec_out = Table(np.array([wave[:], np.power(10,spec[:])]).T, names=('WAVELENGTH', 'FLUX'))
    spec_norm_phot = produceSED.produceNormSED_photon(inputSED = spec_out,mag_norm = star['mwmsc_gsmag'], norm_filter_thr_fn= 'data/throughputs/SDSS/SLOAN_SDSS.g.fits',ws = 1000, we = 24000)
    mags = produceSED.calculatCSSTMAG(spec = spec_norm_phot, throughput_dir = 'data/throughputs/CSST/',ws= 2000, we = 11000)

    mags_others = produceSED.calculatCSSTMAG(spec = spec_norm_phot, throughput_dir = 'data/throughputs/filter_transp/',ws= 1000, we = 24000, filelist=filters_other, band_instr='other')

    for fi in filters:
        res[fi][iterNum] = mags[fi]
    for fi in filters_other:
        res[fi][iterNum] = mags_others[fi]
    iterNum = iterNum + 1
    print(mags, mags_others)
    


total_res=comm.gather(res, root=0)

if rank == 0:
    # new_res = res
    # total_res=comm.gather(res, root=0)
    # print("gather data size is ", size(total_res))
    for i in np.arange(1, rank_size, 1):
        for fi in filters:
            res[fi] = res[fi] + total_res[i][fi]
        for fi in filters_other:
            res[fi] = res[fi] + total_res[i][fi]

    for fi in filters:
        cat.add_column(np.round(res[fi],5),name='interSpec_'+fi)
    for fi in filters_other:
        cat.add_column(np.round(res[fi],5),name='interSpec_'+fi)

    # cat.write("C9_RA300_DECm60_calmag_.fits",overwrite=True)
    # print('--------------------------')
    # print(mags['nuv']-star['mwmsc_nuvmag'], mags['u']-star['mwmsc_umag'], mags['g']-star['mwmsc_gmag'], mags['r']-star['mwmsc_rmag'], mags['i']-star['mwmsc_imag'],mags['z']-star['mwmsc_zmag'],mags['y']-star['mwmsc_ymag'])
#exmple of handling a fits catalouge with many stars
#wvs=fits.open('spec/fp00k2odfnew.fits')[2].data['wave'] #in Angstrom
# cat_list=np.loadtxt(topdir+'cat.list', dtype='U')
# for listfile in cat_list:
#     hdu = fits.open(topdir+listfile)
#     cat=hdu[1].data
#     for star in cat:
#         specTable = np.zeros([nwv,2])
#         s=Star(star['logTe'], star['logg'], star['Mass'], star['Av'], star['mu0'], star['Z'])
#         #print("star[logTe], star[logg], star[Mass], star[Av], star[mu0], star[Z]: ", star['logTe'], star['logg'], star['Mass'], star['Av'], star['mu0'], star['Z'])
#         d.interpSingleStar(s, spec, wave)
#         specTable[:,0] = wave[:]
#         specTable[:,1] = spec[:]
#         # print(spec[500:509])
#         spec_out = Table(np.array([wave[:], np.power(10,spec[:])]).T, names=('WAVELENGTH', 'FLUX'))
#         spec_norm_phot = produceSED.produceNormSED_photon(inputSED = spec_out,mag_norm = 24.0, norm_filter_thr_fn= 'data/throughputs/SDSS/SLOAN_SDSS.g.fits',ws = 2500, we = 10000)
#         mags = produceSED.calculatCSSTMAG(spec = spec_norm_phot, throughput_dir = 'data/throughputs/CSST/',ws= 2500, we = 10000)

#         # print("MAG is ",mags)
#         print('--------------------------')
#         print(mags['nuv']-star['mwmsc_nuvmag'], mag['u']-star['mwmsc_umag'], mag['g']-star['mwmsc_gmag'], mag['r']-star['mwmsc_rmag'], mag['i']-star['mwmsc_imag'],mag['z']-star['mwmsc_zmag'],mag['y']-star['mwmsc_ymag'])