forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpg2fits.c
515 lines (427 loc) · 10.4 KB
/
jpg2fits.c
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <qfits.h>
#include <jpeglib.h>
#include <libexif/exif-data.h>
#include <getopt.h>
struct image {
int nx,ny,nz;
float *z;
double mjd;
char nfd[32],observer[32];
int cospar,tracked;
float exptime;
};
struct image read_jpg(char *filename);
void write_fits(struct image img,char *filename);
double date2mjd(int year,int month,double day);
double nfd2mjd(char *date);
void mjd2nfd(double mjd,char *nfd);
// Read fits image
struct image read_fits(char *filename)
{
int i,j,k,l,m;
qfitsloader ql;
char key[FITS_LINESZ+1] ;
struct image img;
float s1,s2,avg,std;
// Set plane
ql.xtnum = 0;
ql.pnum = 0;
// Set loadtype
ql.ptype = PTYPE_FLOAT;
// Set filename
ql.filename=filename;
// Image size
img.nx=atoi(qfits_query_hdr(filename,"NAXIS1"));
img.ny=atoi(qfits_query_hdr(filename,"NAXIS2"));
img.nz=1;
// Initialize load
if (qfitsloader_init(&ql) != 0)
printf("Error initializing data loading\n");
// Test load
if (qfits_loadpix(&ql) != 0)
printf("Error loading actual data\n");
// Allocate image memory
img.z=(float *) malloc(sizeof(float) * img.nx*img.ny*img.nz);
// Fill z array
for (i=0,l=0,m=0;i<img.nx;i++) {
for (j=0;j<img.ny;j++) {
img.z[l]=ql.fbuf[l];
l++;
}
}
return img;
}
struct image rebin(struct image raw,int nbin)
{
int i,j,k;
int ii,jj,kk;
struct image img;
img.nx=raw.nx/nbin;
img.ny=raw.ny/nbin;
img.nz=1;
img.z=(float *) malloc(sizeof(float)*img.nx*img.ny*img.nz);
for (i=0;i<img.nx;i++) {
for (j=0;j<img.ny;j++) {
k=i+img.nx*j;
img.z[k]=0.0;
for (ii=0;ii<nbin;ii++) {
for (jj=0;jj<nbin;jj++) {
kk=ii+nbin*i+raw.nx*(jj+nbin*j);
img.z[k]+=raw.z[kk];
}
}
}
}
img.mjd=raw.mjd;
img.cospar=raw.cospar;
img.exptime=raw.exptime;
strcpy(img.nfd,raw.nfd);
strcpy(img.observer,raw.observer);
return img;
}
void usage(void)
{
printf("jpg2fits i:t:o:d:Z:c:T:O:b:hF\n\n");
printf("-i input JPG file\n");
printf("-o output FITS file\n");
printf("-t start time (YYYY-MM-DDTHH:MM:SS.sss)\n");
printf("-d delay (in seconds)\n");
printf("-Z timezone offset (in seconds)\n");
printf("-T exposure time (in seconds)\n");
printf("-c COSPAR site number\n");
printf("-O observer name\n");
printf("-b binning factor\n");
printf("-F FITS input\n");
printf("-h this help\n");
exit(0);
return;
}
int main(int argc,char *argv[])
{
int arg;
struct image img,raw;
char infile[64],outfile[64]="",nfd[32]="2000-01-01T00:00:00";
double mjd=51544.0,delay=0.0,tz=0.0;
int cospar=0;
char observer[32]="Cees Bassa";
float exptime=10.06;
int flag=0,nbin=1,readfits=0,tracked=0;
// Decode options
if (argc>1) {
while ((arg=getopt(argc,argv,"i:t:o:d:Z:c:T:O:b:hFs"))!=-1) {
switch(arg) {
case 's':
tracked=1;
break;
case 'i':
strcpy(infile,optarg);
break;
case 'd':
delay=(double) atof(optarg);
break;
case 'b':
nbin=atoi(optarg);
break;
case 'Z':
tz=(double) atof(optarg);
break;
case 'o':
strcpy(outfile,optarg);
flag=1;
break;
case 'F':
readfits=1;
break;
case 't':
strcpy(nfd,optarg);
break;
case 'c':
cospar=atoi(optarg);
break;
case 'T':
exptime=atof(optarg);
break;
case 'O':
strcpy(observer,optarg);
break;
case 'h':
usage();
break;
default:
usage();
return 0;
}
}
} else {
usage();
}
if (infile!=NULL) {
if (nbin==1) {
if (readfits==0)
img=read_jpg(infile);
else
img=read_fits(infile);
} else {
if (readfits==0)
raw=read_jpg(infile);
else
raw=read_fits(infile);
img=rebin(raw,nbin);
}
}
// Set tracked flag
if (tracked==1)
img.tracked=1;
else
img.tracked=0;
if (nfd!=NULL) {
// Compute time
mjd=nfd2mjd(nfd);
mjd+=(delay+tz)/86400.0;
mjd2nfd(mjd,nfd);
// Into file
strcpy(img.nfd,nfd);
img.mjd=mjd;
}
if (flag==0)
sprintf(outfile,"%s.fits",img.nfd);
// Set properties
img.cospar=cospar;
img.exptime=exptime;
strcpy(img.observer,observer);
if (outfile!=NULL)
write_fits(img,outfile);
// Free
free(img.z);
if (nbin!=1)
free(raw.z);
return 0;
}
struct image read_jpg(char *filename)
{
int i=0,j,k,l,m;
unsigned long location=0;
struct image img;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
unsigned char *raw_image=NULL;
FILE *file;
ExifData *ed;
ExifEntry *entry;
// Open file
file=fopen(filename,"rb");
if (!file)
perror("Error opening file");
// Get header info, decompress
cinfo.err=jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,file);
jpeg_read_header(&cinfo,TRUE);
jpeg_start_decompress(&cinfo);
// Allocate memory
raw_image=(unsigned char *) malloc(cinfo.output_width*cinfo.output_height*cinfo.num_components);
// Read image, one scan at a time
row_pointer[0]=(unsigned char *) malloc(cinfo.output_width*cinfo.num_components);
while(cinfo.output_scanline<cinfo.image_height) {
jpeg_read_scanlines(&cinfo,row_pointer,1);
for(i=0;i<cinfo.image_width*cinfo.num_components;i++)
raw_image[location++]=row_pointer[0][i];
}
// wrap up decompression, destroy objects, free pointers and close open files
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
// Copy image to image struct
img.nx=cinfo.image_width;
img.ny=cinfo.image_height;
img.nz=cinfo.num_components;
img.z=(float *) malloc(sizeof(float)*img.nx*img.ny*img.nz);
// Fill image
for (i=0;i<img.nx;i++) {
for (j=0;j<img.ny;j++) {
k=i+(img.ny-j-1)*img.nx;
img.z[k]=0.0;
for (l=0;l<img.nz;l++) {
m=img.nz*(i+img.nx*j)+l;
img.z[k]+=(float) raw_image[m];
}
img.z[k]/=3.0;
}
}
// Free allocated memory
free(row_pointer[0]);
free(raw_image);
// Close file
fclose(file);
/*
// Get exif info
ed=exif_data_new_from_file(filename);
if (!ed) {
printf("File not readable or no EXIF data in file %s\n",filename);
} else {
entry=exif_content_get_entry(ed->ifd[0],EXIF_TAG_DATE_TIME);
exif_entry_get_value(entry,img.nfd, sizeof(img.nfd));
img.nfd[4]='-';
img.nfd[7]='-';
img.nfd[10]='T';
img.nfd[20]='\0';
img.mjd=nfd2mjd(img.nfd);
}
*/
return img;
}
// Write fits file
void write_fits(struct image img,char *filename)
{
int i,j,k,l;
int *ibuf;
qfitsdumper qd;
qfits_header *qh;
char key[FITS_LINESZ+1] ;
char val[FITS_LINESZ+1] ;
char com[FITS_LINESZ+1] ;
char lin[FITS_LINESZ+1] ;
FILE *file;
// Create FITS header
qh=qfits_header_default();
// Add stuff
qfits_header_add(qh,"BITPIX","16"," ",NULL);
qfits_header_add(qh,"NAXIS","2"," ",NULL);
sprintf(val,"%i",img.nx);
qfits_header_add(qh,"NAXIS1",val," ",NULL);
sprintf(val,"%i",img.ny);
qfits_header_add(qh,"NAXIS2",val," ",NULL);
qfits_header_add(qh,"BSCALE","1.0"," ",NULL);
qfits_header_add(qh,"BZERO","0.0"," ",NULL);
qfits_header_add(qh,"DATAMAX","255.0"," ",NULL);
qfits_header_add(qh,"DATAMIN","0.0"," ",NULL);
// Astrometry keywors
sprintf(val,"%f",img.nx/2.0);
qfits_header_add(qh,"CRPIX1",val," ",NULL);
sprintf(val,"%f",img.ny/2.0);
qfits_header_add(qh,"CRPIX2",val," ",NULL);
qfits_header_add(qh,"CRVAL1","0.0"," ",NULL);
qfits_header_add(qh,"CRVAL2","0.0"," ",NULL);
qfits_header_add(qh,"CD1_1","0.0"," ",NULL);
qfits_header_add(qh,"CD1_2","0.0"," ",NULL);
qfits_header_add(qh,"CD2_1","0.0"," ",NULL);
qfits_header_add(qh,"CD2_2","0.0"," ",NULL);
qfits_header_add(qh,"CTYPE1","'RA---TAN'"," ",NULL);
qfits_header_add(qh,"CTYPE2","'DEC--TAN'"," ",NULL);
qfits_header_add(qh,"CUNIT1","'deg'"," ",NULL);
qfits_header_add(qh,"CUNIT2","'deg'"," ",NULL);
qfits_header_add(qh,"CRRES1","0.0"," ",NULL);
qfits_header_add(qh,"CRRES2","0.0"," ",NULL);
qfits_header_add(qh,"EQUINOX","2000.0"," ",NULL);
qfits_header_add(qh,"RADECSYS","ICRS"," ",NULL);
sprintf(val,"%s",img.nfd);
qfits_header_add(qh,"DATE-OBS",val," ",NULL);
sprintf(val,"%lf",img.mjd);
qfits_header_add(qh,"MJD-OBS",val," ",NULL);
sprintf(val,"%d",img.cospar);
qfits_header_add(qh,"COSPAR",val," ",NULL);
sprintf(val,"%f",img.exptime);
qfits_header_add(qh,"EXPTIME",val," ",NULL);
sprintf(val,"%s",img.observer);
qfits_header_add(qh,"OBSERVER",val," ",NULL);
sprintf(val,"%d",img.tracked);
qfits_header_add(qh,"TRACKED",val," ",NULL);
// Dump fitsheader
// qfits_header_dump(qh,stdout);
// Dump to file
file=fopen(filename,"w");
qfits_header_dump(qh,file);
fclose(file);
// Fill buffer
ibuf=malloc(img.nx*img.ny*sizeof(int));
for (i=0,l=0;i<img.nx;i++) {
for (j=img.ny-1;j>=0;j--) {
ibuf[l]=(int) img.z[l];
l++;
}
}
// Set parameters
qd.filename=filename;
qd.npix=img.nx*img.ny;
qd.ptype=PTYPE_INT;
qd.ibuf=ibuf;
qd.out_ptype=BPP_16_SIGNED;
// Dump
qfits_pixdump(&qd);
free(ibuf);
return;
}
// Compute Julian Day from Date
double date2mjd(int year,int month,double day)
{
int a,b;
double jd;
if (month<3) {
year--;
month+=12;
}
a=floor(year/100.);
b=2.-a+floor(a/4.);
if (year<1582) b=0;
if (year==1582 && month<10) b=0;
if (year==1582 && month==10 && day<=4) b=0;
jd=floor(365.25*(year+4716))+floor(30.6001*(month+1))+day+b-1524.5;
return jd-2400000.5;
}
// nfd2mjd
double nfd2mjd(char *date)
{
int year,month,day,hour,min;
float sec;
double mjd,dday;
sscanf(date,"%04d-%02d-%02dT%02d:%02d:%f",&year,&month,&day,&hour,&min,&sec);
dday=day+hour/24.0+min/1440.0+sec/86400.0;
mjd=date2mjd(year,month,dday);
return mjd;
}
// Compute Date from Julian Day
void mjd2nfd(double mjd,char *nfd)
{
double f,jd,dday;
int z,alpha,a,b,c,d,e;
int year,month,day,hour,min;
float sec,x;
jd=mjd+2400000.5;
jd+=0.5;
z=floor(jd);
f=fmod(jd,1.);
if (z<2299161)
a=z;
else {
alpha=floor((z-1867216.25)/36524.25);
a=z+1+alpha-floor(alpha/4.);
}
b=a+1524;
c=floor((b-122.1)/365.25);
d=floor(365.25*c);
e=floor((b-d)/30.6001);
dday=b-d-floor(30.6001*e)+f;
if (e<14)
month=e-1;
else
month=e-13;
if (month>2)
year=c-4716;
else
year=c-4715;
day=(int) floor(dday);
x=24.0*(dday-day);
x=3600.*fabs(x);
sec=fmod(x,60.);
x=(x-sec)/60.;
min=fmod(x,60.);
x=(x-min)/60.;
hour=x;
sec=floor(1000.0*sec)/1000.0;
sprintf(nfd,"%04d-%02d-%02dT%02d:%02d:%06.3f",year,month,day,hour,min,sec);
return;
}