ImageHeader.py 13 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"""
generate image header
"""
import numpy as np

from astropy.io import fits
import astropy.wcs as pywcs
from collections import OrderedDict

from scipy import math
import random

import os
import sys

def chara2digit(char):
    """ Function to judge and convert characters to digitals

    Parameters
    ----------

    """

    try:
        float(char) # for int, long and float
    except ValueError:
        pass
        return char
    else:
        data = float(char)
        return data


def read_header_parameter(filename='global_header.param'):
    """ Function to read the header parameters

    Parameters
    ----------

    """

    name = []
    value = []
    description = []
    for line in open(filename):
        line = line.strip("\n")
        arr = line.split('|')
#        csvReader = csv.reader(csvDataFile)
#        for arr in csvReader:
        name.append(arr[0])
        # print(arr[0],arr[1])
        value.append(chara2digit(arr[1]))
        description.append(arr[2])

#    print(value)
    return name, value, description

def rotate_CD_matrix(cd, pa_aper):
    """Rotate CD matrix
    
    Parameters
    ----------
    cd: (2,2) array
        CD matrix
    
    pa_aper: float
        Position angle, in degrees E from N, of y axis of the detector
    
    Returns
    -------
    cd_rot: (2,2) array
        Rotated CD matrix
    
    Comments
    --------
    `astropy.wcs.WCS.rotateCD` doesn't work for non-square pixels in that it
    doesn't preserve the pixel scale!  The bug seems to come from the fact
    that `rotateCD` assumes a transposed version of its own CD matrix.
    
    """
    rad = np.deg2rad(-pa_aper)
    mat = np.zeros((2,2))
    mat[0,:] = np.array([np.cos(rad),-np.sin(rad)])
    mat[1,:] = np.array([np.sin(rad),np.cos(rad)])
    cd_rot = np.dot(mat, cd)
    return cd_rot


def Header_extention(xlen = 9216, ylen = 9232, gain = 1.0, readout = 5.0, dark = 0.02,saturation=90000, row_num = 1, col_num = 1):

    """ Creat an image frame for CCST with multiple extensions

    Parameters
    ----------

    """

    flag_ltm_x = [0,1,-1,1,-1]
    flag_ltm_y = [0,1,1,-1,-1]
    flag_ltv_x = [0,0,1,0,1]
    flag_ltv_y = [0,0,0,1,1]

    detector_size_x = int(xlen)
    detector_size_y = int(ylen)

    data_x = str(int(detector_size_x))
    data_y = str(int(detector_size_y))

    data_sec = '[1:'+data_x+',1:'+data_y+']'
    e_header_fn = os.path.split(os.path.realpath(__file__))[0] + '/extension_header.param'
    name, value, description = read_header_parameter(e_header_fn)
    f = open(os.path.split(os.path.realpath(__file__))[0] + '/filter.lst')
    s = f.readline()
    s = s.strip("\n")
    filters = s.split(' ')
    s = f.readline()
    s = s.strip("\n")
    filterID = s.split()

    s = f.readline()
    s = s.strip("\n")
    CCDID = s.split()

    k = (row_num-1)*6+col_num

    h_iter = 0
    for n1,v1,d1 in zip(name, value, description):
        if n1=='EXTNAME':
            value[h_iter] = 'RAW,'+CCDID[k-1].rjust(2,'0')
        if n1=='CCDNAME':
            value[h_iter] = 'ccd' + CCDID[k-1].rjust(2,'0')
        if n1=='AMPNAME':
            value[h_iter] = 'ccd' + CCDID[k-1].rjust(2,'0') + ':A'
        if n1=='GAIN':
            value[h_iter] = gain
        if n1=='RDNOISE':
            value[h_iter] = readout
        if n1=='SATURATE':
            value[h_iter] = saturation
        if n1=='CCDCHIP':
            value[h_iter] = 'ccd' + CCDID[k-1].rjust(2,'0')
        if n1=='CCDLABEL':
            value[h_iter] = filters[k-1] + '-' + filterID[k-1]
        if n1=='DATASEC':
            value[h_iter] = data_sec

        h_iter = h_iter + 1

   
    return name, value, description


##9232 9216  898 534 1309 60 -40  -23.4333
def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, ra = 60, dec = -40, pa = -23.433,psize = 0.074, row_num = 1, col_num = 1):

    """ Creat a wcs frame for CCST with multiple extensions

    Parameters
    ----------

    """

    flag_x = [0, 1, -1, 1, -1]
    flag_y = [0, 1, 1, -1, -1]
    flag_ext_x = [0,-1,1,-1,1]
    flag_ext_y = [0,-1,-1,1,1]
    x_num = 6
    y_num = 5
    detector_num = x_num*y_num


    detector_size_x = xlen
    detector_size_y = ylen
    gap_y = gapy
    gap_x = [gapx1,gapx2]
    ra_ref = ra
    dec_ref = dec

    pa_aper = pa

    pixel_size = psize

    gap_x1_num = 3
    gap_x2_num = 2

    y_center = (detector_size_y*y_num+gap_y*(y_num-1))/2

    x_center = (detector_size_x*x_num+gap_x[0]*gap_x1_num+gap_x[1]*gap_x2_num)/2

    gap_x_map = np.array([[0,0,0,0,0],[gap_x[0],gap_x[1],gap_x[1],gap_x[1],gap_x[1]],[gap_x[1],gap_x[0],gap_x[0],gap_x[0],gap_x[0]],[gap_x[0],gap_x[0],gap_x[0],gap_x[0],gap_x[0]],[gap_x[0],gap_x[0],gap_x[0],gap_x[0],gap_x[1]],[gap_x[1],gap_x[1],gap_x[1],gap_x[1],gap_x[0]]])


    # frame_array = np.empty((5,6),dtype=np.float64)
    # print(x_center,y_center)
    
    j = row_num
    i = col_num
    # ccdnum = str((j-1)*5+i)

    x_ref, y_ref = detector_size_x*i + sum(gap_x_map[0:i,j-1]) - detector_size_x/2. , (detector_size_y+gap_y)*j-gap_y-detector_size_y/2

    # print(i,j,x_ref,y_ref,ra_ref,dec_ref)

    r_dat = OrderedDict()

    # name = []
    # value = []
    # description = []
    
    for k in range(1,2):
        
        cd = np.array([[ pixel_size,  0], [0, pixel_size]])/3600.*flag_x[k]
        cd_rot = rotate_CD_matrix(cd, pa_aper)

        # f = open("CCD"+ccdnum.rjust(2,'0')+"_extension"+str(k)+"_wcs.param","w")

        r_dat['EQUINOX'] = 2000.0
        r_dat['WCSDIM'] = 2.0
        r_dat['CTYPE1'] = 'RA---TAN'
        r_dat['CTYPE2'] = 'DEC--TAN'
        r_dat['CRVAL1'] = ra_ref
        r_dat['CRVAL2'] = dec_ref
        r_dat['CRPIX1'] = flag_ext_x[k]*((x_ref+flag_ext_x[k]*detector_size_x/2)-x_center)
        r_dat['CRPIX2'] = flag_ext_y[k]*((y_ref+flag_ext_y[k]*detector_size_y/2)-y_center)
        r_dat['CD1_1'] = cd_rot[0,0]
        r_dat['CD1_2'] = cd_rot[0,1]
        r_dat['CD2_1'] = cd_rot[1,0]
        r_dat['CD2_2'] = cd_rot[1,1]
        return r_dat


    #     name = ['EQUINOX',
    #             'WCSDIM',
    #             'CTYPE1',
    #             'CTYPE2',
    #             'CRVAL1',
    #             'CRVAL2',
    #             'CRPIX1',
    #             'CRPIX2',
    #             'CD1_1',
    #             'CD1_2',
    #             'CD2_1',
    #             'CD2_2']
    #     value = [2000.0,
    #              2.0,
    #              'RA---TAN',
    #              'DEC--TAN',
    #              ra_ref,
    #              dec_ref,
    #              flag_ext_x[k]*((x_ref+flag_ext_x[k]*detector_size_x/2)-x_center),
    #              flag_ext_y[k]*((y_ref+flag_ext_y[k]*detector_size_y/2)-y_center),
    #              cd_rot[0,0],
    #              cd_rot[0,1],
    #              cd_rot[1,0],
    #              cd_rot[1,1]]
    #     description = ['Equinox of WCS',
    #                    'WCS Dimensionality',
    #                    'Coordinate type',
    #                    'Coordinate typ',
    #                    'Coordinate reference value',
    #                    'Coordinate reference value',
    #                    'Coordinate reference pixel',
    #                    'Coordinate reference pixel',
    #                    'Coordinate matrix',
    #                    'Coordinate matrix',
    #                    'Coordinate matrix',
    #                    'Coordinate matrix']

    # return name, value, description



def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointNum = '1', ra = 60, dec = -40, psize = 0.074, row_num = 1, col_num = 1, date='200930', time_obs='120000'):

    # array_size1, array_size2, flux, sigma = int(argv[1]), int(argv[2]), 1000.0, 5.0

    
    k = (row_num-1)*6+col_num
    # ccdnum = str(k)

    g_header_fn = os.path.split(os.path.realpath(__file__))[0] + '/global_header.param'
    name, value, description = read_header_parameter(g_header_fn)
    f = open(os.path.split(os.path.realpath(__file__))[0] + '/filter.lst')
    s = f.readline()
    s = s.strip("\n")
    filters = s.split(' ')
    s = f.readline()
    s = s.strip("\n")
    filterID = s.split()

    s = f.readline()
    s = s.strip("\n")
    CCDID = s.split()

    h_prim = fits.Header()

    for i in range(len(name)):
        
        if(name[i]=='FILENAME'):
            # value[i] = 'CSST_' + date + '_' +time_obs + '_' + pointNum.rjust(6,'0') + '_' +ccdnum.rjust(2,'0')+'_raw'
            # value[i] = 'CSST_' + date + '_' +time_obs + '_' + pointNum.rjust(6,'0') + '_' +CCDID[k-1].rjust(2,'0')+'_raw'
            value[i] = 'MSC_' + date + time_obs + '_' + pointNum.rjust(7,'0') + '_' +CCDID[k-1].rjust(2,'0')+'_raw'
        
        if(name[i]=='DETSIZE'):
            value[i] = '[1:' + str(int(xlen)) + ',1:'+  str(int(ylen)) + ']'

        if(name[i]=='PIXSCAL1'):
            value[i] = str(psize)

        if(name[i]=='PIXSCAL2'):
            value[i] = str(psize)
        

        h_prim[name[i]] = (value[i],description[i])

    h_prim.add_comment('==================================================================',after='FILETYPE')
    h_prim.add_comment('Target information')
    h_prim.add_comment('==================================================================')

    h_prim.add_comment('==================================================================',after='EQUINOX')
    h_prim.add_comment('Exposure information')
    h_prim.add_comment('==================================================================')

    h_prim.add_comment('==================================================================',after='MJDEND')
    h_prim.add_comment('Telescope information')
    h_prim.add_comment('==================================================================')

    h_prim.add_comment('==================================================================',after='REFFRAME')
    h_prim.add_comment('Detector information')
    h_prim.add_comment('==================================================================')

    h_prim.add_comment('==================================================================',after='DETETEMP')
    h_prim.add_comment('Other information')
    h_prim.add_comment('==================================================================')

    return h_prim

def generateExtensionHeader(xlen = 9216, ylen = 9232,ra = 60, dec = -40, pa = -23.433, gain = 1.0, readout = 5.0, dark = 0.02, saturation=90000, psize = 0.074, row_num = 1, col_num = 1):

    h_ext = fits.Header()

    for i in range(1,2):
        
        # NAXIS1:Number of pixels per row;  NAXIS2:Number of rows
        h_ext['NAXIS1'] = xlen
        h_ext['NAXIS2'] = ylen
        name, value, description = Header_extention(xlen = xlen, ylen = ylen, gain = gain, readout = readout, dark = dark, saturation=saturation, row_num = row_num, col_num = col_num)

        for j in range(len(name)):
            # print(name[j],value[j],description[j])
            h_ext[name[j]] = (value[j],description[j])

        header_wcs = WCS_def(xlen = xlen, ylen = ylen, gapy = 898.0, gapx1 = 534, gapx2 = 1309, ra = ra, dec = dec, pa = pa ,psize = psize, row_num = row_num, col_num = col_num)

        for k,v in header_wcs.items():
            h_ext.set(k,v)


        # for j in range(len(name)):
        #     h_ext[name[j]] = (value[j],description[j])

        h_ext.add_comment('==================================================================',after='OBSID')
        h_ext.add_comment('Readout information')
        h_ext.add_comment('==================================================================')

        h_ext.add_comment('==================================================================',after='CHIPTEMP')
        h_ext.add_comment('Chip information')
        h_ext.add_comment('==================================================================')

        h_ext.add_comment('==================================================================',after='TRIMSEC')
        h_ext.add_comment('WCS information')
        h_ext.add_comment('==================================================================')

        h_ext.add_comment('==================================================================',after='CD2_2')
        h_ext.add_comment('Other information')
        h_ext.add_comment('==================================================================')

    return h_ext



def main(argv):

    xlen = int(argv[1])
    ylen = int(argv[2])
    pointingNum = argv[3]
    ra = float(argv[4])
    dec = float(argv[5])
    pSize = float(argv[6])
    ccd_row_num = int(argv[7])
    ccd_col_num = int(argv[8])
    pa_aper = float(argv[9])
    gain = float(argv[10])
    readout = float(argv[11])
    dark = float(argv[12])
    fw = float(argv[13])



    h_prim = generatePrimaryHeader(xlen = xlen, ylen = ylen,ra = ra, dec = dec, psize = pSize, row_num = ccd_row_num, col_num = ccd_col_num, pointNum = pointingNum)

    h_ext = generateExtensionHeader(xlen = xlen, ylen = ylen,ra = ra, dec = dec, pa = pa_aper, gain = gain, readout = readout, dark = dark, saturation=fw, psize = pSize, row_num = ccd_row_num, col_num = ccd_col_num)
    hdu1 = fits.PrimaryHDU(header=h_prim)
    hdu2 = fits.ImageHDU(np.zeros([ylen,xlen]),header = h_ext)

    hdul = fits.HDUList([hdu1,hdu2])

    hdul.writeto(h_prim['FILENAME']+'.fits',output_verify='ignore')

# if __name__ == "__main__":
#     main(sys.argv)