-
Notifications
You must be signed in to change notification settings - Fork 0
/
CR2toFITS.c
163 lines (130 loc) · 4 KB
/
CR2toFITS.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
/** @file CR2toFITS.c
*/
#include "CommandLineInterface/CLIcore.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "readPGM.h"
static int CR2toFITS_NORM = 0;
// 1 if FITS should be normalized to ISO = 1, exposure = 1 sec, and F/1.0
// ==========================================
// Forward declaration(s)
// ==========================================
imageID CR2toFITS(const char *__restrict fnameCR2,
const char *__restrict fnameFITS);
// ==========================================
// Command line interface wrapper function(s)
// ==========================================
static errno_t CR2toFITS_cli()
{
// if(CLI_checkarg(1, 3)+CLI_checkarg(2, 3))
CR2toFITS(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.string);
// else
// return(0);
return RETURN_SUCCESS;
}
// ==========================================
// Register CLI command(s)
// ==========================================
errno_t CR2toFITS_addCLIcmd()
{
RegisterCLIcommand(
"cr2tofits",
__FILE__,
CR2toFITS_cli,
"convert cr2 file to fits",
"<input CR2 file> <output FITS file>",
"cr2tofits im01.CR2 im01.fits",
"int CR2toFITS(const char *fnameCR2, const char *fnameFITS)");
return RETURN_SUCCESS;
}
/**
* ## Purpose
*
* Convert CR2 to FITS
*
* @note assumes dcraw is installed
*/
imageID CR2toFITS(const char *__restrict fnameCR2,
const char *__restrict fnameFITS)
{
FILE *fp;
float iso;
float shutter;
float aperture;
imageID ID;
long xsize, ysize;
long ii;
EXECUTE_SYSTEM_COMMAND("dcraw -t 0 -D -4 -c %s > _tmppgm.pgm", fnameCR2);
ID = read_PGMimage("_tmppgm.pgm", "tmpfits1");
if(system("rm _tmppgm.pgm") != 0)
{
PRINT_ERROR("system() returns non-zero value");
}
if(CR2toFITS_NORM == 1)
{
EXECUTE_SYSTEM_COMMAND(
"dcraw -i -v %s | grep \"ISO speed\"| awk '{print $3}' > "
"iso_tmp.txt",
fnameCR2);
if((fp = fopen("iso_tmp.txt", "r")) == NULL)
{
PRINT_ERROR("Cannot open file");
}
if(fscanf(fp, "%f\n", &iso) != 1)
{
PRINT_ERROR("fscanf returns value != 1");
}
fclose(fp);
if(system("rm iso_tmp.txt") != 0)
{
PRINT_ERROR("system() returns non-zero value");
}
printf("iso = %f\n", iso);
EXECUTE_SYSTEM_COMMAND(
"dcraw -i -v %s | grep \"Shutter\"| awk '{print $2}' > "
"shutter_tmp.txt",
fnameCR2);
if((fp = fopen("shutter_tmp.txt", "r")) == NULL)
{
PRINT_ERROR("Cannot open file");
}
if(fscanf(fp, "%f\n", &shutter) != 1)
{
PRINT_ERROR("fscanf returns value != 1");
}
fclose(fp);
if(system("rm shutter_tmp.txt") != 0)
{
PRINT_ERROR("system() returns non-zero value");
}
printf("shutter = %f\n", shutter);
EXECUTE_SYSTEM_COMMAND(
"dcraw -i -v %s | grep \"Aperture\"| awk '{print $2}' > "
"aperture_tmp.txt",
fnameCR2);
if((fp = fopen("aperture_tmp.txt", "r")) == NULL)
{
PRINT_ERROR("Cannot open file");
}
if(fscanf(fp, "f/%f\n", &aperture) != 1)
{
PRINT_ERROR("fscanf returns value != 1");
}
fclose(fp);
if(system("rm aperture_tmp.txt") != 0)
{
PRINT_ERROR("system() returns non-zero value");
}
printf("aperture = %f\n", aperture);
ID = image_ID("tmpfits1");
xsize = data.image[ID].md[0].size[0];
ysize = data.image[ID].md[0].size[1];
for(ii = 0; ii < xsize * ysize; ii++)
{
data.image[ID].array.F[ii] /= (shutter * aperture * aperture * iso);
}
}
save_fl_fits("tmpfits1", fnameFITS);
delete_image_ID("tmpfits1", DELETE_IMAGE_ERRMODE_WARNING);
return ID;
}