-
Notifications
You must be signed in to change notification settings - Fork 1
/
mimosa_fits.f90
441 lines (321 loc) · 9.65 KB
/
mimosa_fits.f90
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
!===========================
MODULE FITS_DEFS
!==========================
INTEGER,save :: BIT_PAR_PIXEL=-32 ! PIXEL LENGTH IN BITS
INTEGER,save :: naxis=2 ! NUMBER OF DIMENSIONS
INTEGER,save :: ieq = 2000 ! COORDINATE EPOCH
INTEGER,save :: lenh = 36 ! FITS HEADER LENGTH
CHARACTER(len=20)::radesys='FK5 '!MEAN PLACE ETC
!==============================
END MODULE FITS_DEFS
!==============================
!==============================
MODULE FITS_DECLARATIONS
!==============================
INTERFACE
SUBROUTINE FITS_FILE(num,image,proj,nom_out,iprint,info)
!-------------------------------------------------
IMPLICIT NONE
!INPUT VARIABLES
INTEGER::num ! num - POINTING NUMBER
REAL(kind=4),dimension(:,:),pointer :: image !image array
INTEGER :: proj
! PROJECTIONtype BETWEEN SPHERICAL(NATIVE) AND PLANE(WORLD) COORDINATES
CHARACTER(len=*) :: nom_out
! FITS FILE NAME
INTEGER,optional :: iprint
CHARACTER(len=*),Optional :: info
END SUBROUTINE FITS_FILE
SUBROUTINE FITS_FILE8(num,image,proj,nom_out,iprint,info)
!-------------------------------------------------
USE FITS_DEFS
IMPLICIT NONE
!INPUT VARIABLES
INTEGER::num ! num - POINTING NUMBER
REAL(kind=8),dimension(:,:):: image !image array
INTEGER :: proj
! PROJECTIONtype BETWEEN SPHERICAL(NATIVE) AND PLANE(WORLD) COORDINATES
CHARACTER(len=*):: nom_out
! FITS FILE NAME
INTEGER,optional::iprint
CHARACTER(len=*),Optional::info
END SUBROUTINE FITS_FILE8
END INTERFACE
!==============================
END MODULE FITS_DECLARATIONS
!==============================
!=============================================
SUBROUTINE FITS_FILE(num,image,proj,nom_out,iprint,info)
!=============================================
USE FITS_DEFS
IMPLICIT NONE
!INPUT VARIABLES
INTEGER::num ! num - POINTING NUMBER
REAL(kind=4),dimension(:,:),pointer:: image
INTEGER :: proj
! PROJECTION BETWEEN SPHERICAL(NATIVE) AND PLANE(WORLD) COORDINATES
CHARACTER(len=*):: nom_out
! FITS FILE NAME
INTEGER,optional::iprint
CHARACTER(len=*),optional::info
!AUXILIARY VARIABLES
CHARACTER(len=80):: hline(36)
INTEGER::nx,ny
nx = size(image,1)
ny = size(image,2)
! 36*80 = 2880 - 36 LINES OF HEADER
OPEN(90,FILE=nom_out,ACCESS='direct',RECL=2880)
CALL create_fits_header(num,nx,ny,proj,hline)
if(present(iprint))PRINT 101, hline
101 FORMAT(A80)
WRITE(90,REC=1) hline
CALL wf_data(image,nx*ny,720)
CLOSE(90)
if(present(info))then
write(info,*)' file (',nx,',',ny,') : ',TRIM(nom_out),' created '
endif
!=======================
END SUBROUTINE FITS_FILE
!=======================
!=============================================
SUBROUTINE FITS_FILE8(num,image,proj,nom_out,iprint,info)
!=============================================
USE FITS_DEFS
IMPLICIT NONE
!INPUT VARIABLES
INTEGER::num ! num - POINTING NUMBER
REAL(kind=8),dimension(:,:):: image
INTEGER :: proj
! PROJECTION BETWEEN SPHERICAL(NATIVE) AND PLANE(WORLD) COORDINATES
CHARACTER(len=*):: nom_out
! FITS FILE NAME
INTEGER,optional::iprint
CHARACTER(len=*),optional::info
!AUXILIARY VARIABLES
CHARACTER(len=80):: hline(36)
INTEGER::nx,ny
nx = size(image,1)
ny = size(image,2)
! 36*80 = 2880 - 36 LINES OF HEADER
OPEN(90,FILE=nom_out,ACCESS='direct',RECL=2880)
CALL create_fits_header(num,nx,ny,proj,hline)
if(present(iprint))PRINT 101, hline
101 FORMAT(A80)
WRITE(90,REC=1) hline
CALL wf_data8(image,nx*ny,720)
CLOSE(90)
if(present(info))then
write(info,*)' file (',nx,',',ny,') : ',TRIM(nom_out),' created '
endif
!=======================
END SUBROUTINE FITS_FILE8
!=======================
!=====================================================
SUBROUTINE create_fits_header(num,nx,ny,proj,hline)
!=====================================================
USE FITS_DEFS
USE ATTI_DEFS
IMPLICIT NONE
!INPUT VARIABLES
INTEGER :: num !POINTING NUMBER
INTEGER :: nx,ny !IMAGE DIMENSIONS IN PIXEL
INTEGER :: proj ! PROJECTION TYPE
! 0 - FOR CAR
! 1 - FOR TAN
!INPUT/OUTPUT VARIABLES
CHARACTER(len=80),dimension(lenh):: hline !HEADER
!AUXILIARY VARIABLES
REAL(kind=4)::cenx,ceny !IMAGE CENTRE
REAL(KIND=4)::alpha,delta ! POINTING RA DEC
REAL(KIND=4)::th,th_rad,st0,ct0 !ANTICLOCKWISE NORTH ANGLE
REAL(KIND=4)::incr ! PIXEL INCREMENT IN DEG
INTEGER ::i, nline ! LINE NUMBER
CHARACTER(len=80)::dum,cur
CHARACTER(len=20):: ctyp
LOGICAL ,save::simple=.true.
WRITE(dum,111)
111 FORMAT(80x)
! RA DEC OF POINTING
alpha = pointing_table(num,1)
delta = pointing_table(num,2)
th = pointing_table(num,3)
!print *,' alpha,delta,th : ', alpha,delta,th
th_rad = th*sdeg_to_rad
ct0 = cos(th_rad)
st0 = sin(th_rad)
!print *,' cos,sin : ',ct0,st0
!IMAGE CENTRE
! ATTENTION : PIXEL I RUNS FROM I-0.5 TO I+0.5
! 0 IS AT THE CENTRE OF 0 PIXEL
cenx = 0.5 *(1+nx)
ceny = 0.5 *(1+ny)
!PIXEL INCREMENT IN DEG
incr = srad_to_deg*atan(pixel_ratio)
! IF (ABS(ct0(1)) .GT. ABS(st0(1))) THEN
! theta = - ASIND(st0(1))
! IF (ct0(1) .LT. 0.) theta = theta + 180.
! ELSE
! theta = ACOSD(ct0(1))
! IF (st0(1) .GT. 0.) theta = theta + 180.
! ENDIF
!DATE
! annee = an(1) - 1900
! CALL idate(idtab)
! idtab(3) = idtab(3) - 1900
nline = 1
WRITE(hline(nline),101) 'SIMPLE ',simple,dum
nline = nline+1
cur = 'REAL*4' // dum
WRITE(hline(nline),102)'BITPIX ',BIT_PAR_PIXEL,cur
101 FORMAT(A8,'= ',L20,' / ',A47)
102 FORMAT(A8,'= ',I20,' / ',A47)
103 FORMAT(A8,'= ',A20,' / ',A47)
104 FORMAT(A8,'= ',A20,' / ',A47)
105 FORMAT(A8,'= ',F20.4,' / ',A47)
106 FORMAT(A8,'= ',1f20.4,' / ',A47)
107 FORMAT(A8,'= ',1PE20.4,' / ',A47)
!NAXIS
nline = nline+1
cur = 'NUMBER OF AXES' // dum
WRITE(hline(nline),102) 'NAXIS ',naxis,cur
!NAXIS 1
nline = nline+1
cur = 'IMAGE SIZE ALONG x AXIS ' // dum
WRITE(hline(nline),102) 'NAXIS1 ',nx,cur
!NAXIS 2
nline = nline+1
cur = 'IMAGE SIZE ALONG y AXIS ' // dum
WRITE(hline(nline),102) 'NAXIS2 ',ny,cur
!EQUINOX
nline = nline+1
cur = 'COORDINATES in 2000.0' // dum
WRITE(hline(nline),102) 'EQUINOX ',ieq,cur
!RADESYS
nline = nline+1
cur = 'MEAN PLACE, IAU 1984 system' // dum
WRITE(hline(nline),103) 'RADESYS ',radesys,cur
! DESCRIPTION OF THE FIRST AXIS
! CTYPE1 : x AXIS TYPE
select case (proj)
case (0)
nline = nline+1
ctyp = 'RA---CAR'
cur = 'CARTESIAN PROJECTION' // dum
WRITE(hline(nline),104) 'CTYPE1 ',ctyp,cur
case (1)
nline = nline+1
ctyp = 'RA---TAN'
cur = 'GNOMONIC PROJECTION' // dum
WRITE(hline(nline),104) 'CTYPE1 ',ctyp,cur
end select
! CRVAL1 : REFERENCE VALUE
nline = nline+1
cur = 'RIGHT ASCENSION OF POINTING DIRECTION' // dum
WRITE(hline(nline),105) 'CRVAL1 ',alpha,cur
!CRPIX1
nline = nline+1
cur = 'X COORDINATE OF IMAGE CENTER' // dum
WRITE(hline(nline),105) 'CRPIX1 ',cenx,cur
!CDELT1
nline = nline+1
cur = 'DEGREES PER PIXEL ALONG x AXIS ' // dum
WRITE(hline(nline),106) 'CDELT1 ',incr,cur
! DESCRIPTION OF THE SECOND AXIS
! CTYPE 2 : AXIS TYPE
select case(proj)
case(2)
nline = nline+1
ctyp = 'DEC--CAR'
cur = 'CARTESIAN PROJECTION' // dum
WRITE(hline(nline),104) 'CTYPE2 ',ctyp,cur
case(1)
nline = nline+1
ctyp = 'DEC--TAN'
cur = 'GNOMONIC PROJECTION' // dum
WRITE(hline(nline),104) 'CTYPE2 ',ctyp,cur
end select
! CRVAL2 : REFERENCE VALUE
nline = nline+1
cur = 'DECLINATION OF POINTING DIRECTION' // dum
WRITE(hline(nline),105) 'CRVAL2 ',delta,cur
!CRPIX2
nline = nline+1
cur = 'Y COORDINATE OF IMAGE CENTER' // dum
WRITE(hline(nline),105) 'CRPIX2 ',ceny,cur
!CDELT2
nline = nline+1
cur = 'DEGREES PER PIXEL ALONG y AXIS ' // dum
WRITE(hline(nline),106) 'CDELT2 ',incr,cur
! ROTATION MATRIX
nline = nline+1
cur = ' ROTATION MATRIX : CDELT1*cos(CROTA2)' // dum
WRITE(hline(nline),107) 'CD1_1 ',-incr*ct0,cur
nline = nline+1
cur = ' : -CDELT2*sin(CROTA2)' // dum
WRITE(hline(nline),107) 'CD1_2 ',-incr*st0,cur
nline = nline+1
cur = ' : CDELT1*sin(CROTA2)' // dum
WRITE(hline(nline),107) 'CD2_1 ',-incr*st0,cur
nline = nline+1
cur = ' : CDELT2*cos(CROTA2)' // dum
WRITE(hline(nline),107) 'CD2_2 ',incr*ct0,cur
!LONGPOLE
nline = nline+1
cur = ' NORTH POLE LATITUDE IN THE NATIVE SYSTEM' // dum
WRITE(hline(nline),104) 'LONGPOLE','180',cur
DO i = nline+1,lenh-1
hline(i) = dum
ENDDO
hline(lenh)='END'//dum
!do i=1,36
! hline(i) = hline(i)(1:78)//'A'//' '
!enddo
END SUBROUTINE create_fits_header
!==================================
SUBROUTINE wf_data8(image,dim,len)
!===================================
USE FITS_DEFS
IMPLICIT NONE
INTEGER::dim
REAL(kind=8),dimension(dim)::image
INTEGER ::nrec,i,n0,n1,n
INTEGER::len
! 720 = 2880/4
REAL(kind=4),dimension(len)::fin
REAL(kind=4),dimension(dim)::image4
image4 = image
fin = 0.0
nrec = (dim-1)/len + 1
DO i = 1,nrec-1
n0 = (i-1) *len
WRITE(90,REC=i+1) (image4(n),n=n0+1,n0+len)
ENDDO
! Ecriture du dernier enregistrement complete par des 0.
n0 = (nrec-1) *len
n1 = len - dim + n0
WRITE(90,REC=nrec+1) (image4(n),n=n0+1,dim),(fin(n),n=1,n1)
!==================================
END SUBROUTINE wf_data8
!==================================
!==================================
SUBROUTINE wf_data(image,dim,len)
!===================================
USE FITS_DEFS
IMPLICIT NONE
INTEGER::dim
REAL(kind=4),dimension(dim)::image
INTEGER ::nrec,i,n0,n1,n
INTEGER::len
! 720 = 2880/4
REAL(kind=4),dimension(len)::fin
fin = 0.0
nrec = (dim-1)/len + 1
DO i = 1,nrec-1
n0 = (i-1) *len
WRITE(90,REC=i+1) (image(n),n=n0+1,n0+len)
ENDDO
! Ecriture du dernier enregistrement complete par des 0.
n0 = (nrec-1) *len
n1 = len - dim + n0
WRITE(90,REC=nrec+1) (image(n),n=n0+1,dim),(fin(n),n=1,n1)
END SUBROUTINE wf_data