forked from MTMurphy77/UVES_popler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVES_rFITSlist.c
294 lines (284 loc) · 12.3 KB
/
UVES_rFITSlist.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
/****************************************************************************
* Read in list of FITS files with absolute path names
****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "UVES_popler.h"
#include "file.h"
#include "error.h"
int UVES_rFITSlist(char *infile, spectrum **spec, int *nspec, params *par) {
int i=0;
long ranseed=0;
char buffer[LNGSTRLEN]="\0";
char *cptr;
FILE *data_file=NULL;
/* Open input file */
if ((data_file=faskropen("Valid input UPL file, FITS file, FITS file list?",
infile,5))==NULL)
errormsg("UVES_rFITSlist(): Can not open file %s",infile);
/* Read in first line */
i++; if ((cptr=fgets(buffer,LNGSTRLEN,data_file))==NULL) {
fclose(data_file);
errormsg("UVES_rFITSlist(): Problem reading file %s on line %d",infile,i);
}
/* File should be a list of FITS file names with absolute paths */
/* Check for absolute path names ... a weak check anyway */
if (strncmp(buffer,"/",1)) {
fclose(data_file);
errormsg("UVES_rFITSlist(): FITS file path invalid on line %d\n\
\tin file %s.\n\
\tYou must use absolute path names - FITS file names must begin with '/'.",
1,infile);
}
/* See if it is a list of valid FITS files */
while ((cptr=fgets(buffer,LNGSTRLEN,data_file))!=NULL) {
/* Check for absolute path names ... a weak check anyway */
if (strncmp(buffer,"/",1)) {
fclose(data_file);
errormsg("UVES_rFITSlist(): FITS file path invalid on line %d\n\
\tin file %s.\n\
\tYou must use absolute path names - FITS file names must begin with '/'.",
i+1,infile);
}
i++;
}
if (!feof(data_file)) {
fclose(data_file);
errormsg("UVES_rFITSlist(): Error reading line %d in file %s",i,infile);
}
else {
rewind(data_file); *nspec=i;
/* Allocate memory for nspec spectra */
if (!(*spec=(spectrum *)malloc((size_t)(*nspec*sizeof(spectrum)))))
errormsg("UVES_rFITSlist(): Could not allocate memory for\n\
\tspectrum array of size %d",*nspec);
}
/* Read in list of names of FITS files and assign identity numbers */
for (i=0; i<*nspec; i++) {
cptr=fgets(buffer,LNGSTRLEN,data_file);
if (par->filetype==FTMIX) {
if (sscanf(buffer,"%s %d",(*spec)[i].file,&((*spec)[i].ftype))!=2) {
fclose(data_file);
errormsg("UVES_rFITSlist(): Incorrect format in line %d\n\t of file\n\
\t%s.\n\tWhen using mixed file-types, you must enter a digit (%d-%d) after each\n\
\tfile name of the input file",i+1,infile,FTUVES,FTHARP);
}
} else if (sscanf(buffer,"%s",(*spec)[i].file)!=1) {
fclose(data_file);
errormsg("UVES_rFITSlist(): Incorrect format in line %d\n\t of file %s",
i+1,infile);
} else if (par->filetype==-2) (*spec)[i].ftype=FILETYPE;
else (*spec)[i].ftype=par->filetype;
/* Get file name and path etc. */
cptr=strrchr((*spec)[i].file,'/'); strcpy((*spec)[i].abfile,cptr+1);
cptr=strrchr(strcpy((*spec)[i].path,(*spec)[i].file),'/'); *(cptr+1)='\0';
/* Assign identity number */
(*spec)[i].id=i;
}
fclose(data_file);
/* Generate list of error and wpol file names */
for (i=0; i<*nspec; i++) {
if (par->thar<=1) {
if ((*spec)[i].ftype<=FTUVES) {
if ((cptr=strstr((*spec)[i].abfile,"fxb_"))==NULL ||
strncmp((*spec)[i].abfile,"fxb_",4))
errormsg("UVES_rFITSlist(): Cannot identify error and\n\
\twavelength polynomial array files for flux file\n\t%s\n\
\tUVES flux files must have 'fxb_' prefix",(*spec)[i].file);
cptr+=4;
sprintf((*spec)[i].erfile,"%serr_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].aberfile,"err_%s",cptr);
if (par->thar==1) {
sprintf((*spec)[i].thfile,"%sthar_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abthfile,"thar_%s",cptr);
}
sprintf((*spec)[i].wlfile,"%swpol_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abwlfile,"wpol_%s",cptr);
} else if ((*spec)[i].ftype==FTIRAF) {
if ((cptr=strstr((*spec)[i].abfile,"flx_"))==NULL ||
strncmp((*spec)[i].abfile,"flx_",4))
errormsg("UVES_rFITSlist(): Cannot identify error and\n\
\twavelength polynomial array files for flux file\n\t%s\n\
\tIRAF flux files must have 'flx_' prefix",(*spec)[i].file);
cptr+=4;
sprintf((*spec)[i].erfile,"%ssig_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].aberfile,"sig_%s",cptr);
if (par->thar==1) {
sprintf((*spec)[i].thfile,"%swav_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abthfile,"wav_%s",cptr);
}
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
} else if ((*spec)[i].ftype==FTMAKE) {
if ((cptr=strstr((*spec)[i].abfile,"Flux-"))==NULL ||
strncmp((*spec)[i].abfile,"Flux-",5))
errormsg("UVES_rFITSlist(): Cannot identify error and\n\
\twavelength polynomial array files for flux file\n\t%s\n\
\tMAKEE flux files must have 'Flux-' prefix",(*spec)[i].file);
cptr+=5;
sprintf((*spec)[i].erfile,"%sErr-%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].aberfile,"Err-%s",cptr);
if (par->thar==1) {
sprintf((*spec)[i].thfile,"%sWav-%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abthfile,"Wav-%s",cptr);
}
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
} else if ((*spec)[i].ftype==FTIRLS) {
if ((cptr=strstr((*spec)[i].abfile,"flx_"))==NULL ||
strncmp((*spec)[i].abfile,"flx_",4))
errormsg("UVES_rFITSlist(): Cannot identify error and\n\
\twavelength polynomial array files for flux file\n\t%s\n\
\tIRAFLSS flux files must have 'flx_' prefix",(*spec)[i].file);
cptr+=4;
sprintf((*spec)[i].erfile,"%ssig_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].aberfile,"sig_%s",cptr);
if (par->thar==1) {
sprintf((*spec)[i].thfile,"%swav_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abthfile,"wav_%s",cptr);
}
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
} else if ((*spec)[i].ftype==FTHIRX) {
sprintf((*spec)[i].erfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].aberfile,"%s",(*spec)[i].abfile);
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
if (par->thar==1) {
if ((cptr=strstr((*spec)[i].abfile,"spec_"))==NULL ||
strncmp((*spec)[i].abfile,"spec_",5))
errormsg("UVES_rFITSlist(): Cannot identify extracted ThAr\n\
\tarray files for flux file\n\t%s\n\tHIREDUX flux files must have 'spec_' prefix",
(*spec)[i].file);
cptr+=5;
sprintf((*spec)[i].thfile,"%sarc_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abthfile,"arc_%s",cptr);
}
} else if ((*spec)[i].ftype==FTESOM) {
if ((cptr=strstr((*spec)[i].abfile,"UV_SRED_"))==NULL ||
strncmp((*spec)[i].abfile,"UV_SRED_",8))
errormsg("UVES_rFITSlist(): Cannot identify error and\n\
\twavelength polynomial array files for flux file\n\t%s\n\
\tUVES ESO-merged flux files must have 'UV_SRED_' prefix",(*spec)[i].file);
cptr+=8;
sprintf((*spec)[i].erfile,"%sUV_SERR_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].aberfile,"UV_SERR_%s",cptr);
if (par->thar==1) {
sprintf((*spec)[i].thfile,"%sthar_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abthfile,"thar_%s",cptr);
}
sprintf((*spec)[i].wlfile,"%sUV_SRED_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abwlfile,"UV_SRED_%s",cptr);
} else if ((*spec)[i].ftype==FTMAGE) {
sprintf((*spec)[i].aberfile,"%s",(*spec)[i].abfile);
if ((cptr=strstr((*spec)[i].aberfile,"spec_cal"))==NULL)
errormsg("UVES_rFITSlist(): Cannot identify error and\n\
\twavelength polynomial array files for flux file\n\t%s\n\
\tMagE flux files must have 'spec_cal' in their names",(*spec)[i].file);
sprintf(buffer,"%s",cptr+8); sprintf(cptr,"%s","err_cal");
sprintf(cptr+7,"%s",buffer);
sprintf((*spec)[i].erfile,"%s%s",(*spec)[i].path,(*spec)[i].aberfile);
if (par->thar==1) {
sprintf((*spec)[i].abthfile,"%s",(*spec)[i].abfile);
cptr=strstr((*spec)[i].abthfile,"spec_cal");
sprintf(cptr,"%s","thar"); sprintf(cptr+4,"%s",buffer);
sprintf((*spec)[i].thfile,"%s%s",(*spec)[i].path,(*spec)[i].abthfile);
}
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
cptr=strstr((*spec)[i].abwlfile,"spec_cal");
sprintf(cptr,"%s","wspec"); sprintf(cptr+5,"%s",buffer);
sprintf((*spec)[i].wlfile,"%s%s",(*spec)[i].path,(*spec)[i].abwlfile);
} else if ((*spec)[i].ftype==FTIESI) {
sprintf((*spec)[i].erfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].aberfile,"%s",(*spec)[i].abfile);
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
} else if ((*spec)[i].ftype==FTHARP) {
if ((cptr=strstr((*spec)[i].abfile,"HARP"))==NULL ||
strncmp((*spec)[i].abfile,"HARP",4) ||
(cptr=strstr((*spec)[i].abfile,"e2ds"))==NULL)
errormsg("UVES_rFITSlist(): Cannot identify HARPS filename\n\
\tcharacteristics for flux file\n\t%s\n\
\tHARPS (S or N) file names must have 'HARP[S or N]' prefix\n\
\tand contain 'e2ds'.",(*spec)[i].file);
sprintf((*spec)[i].erfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].aberfile,"%s",(*spec)[i].abfile);
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
}
} else {
if ((*spec)[i].ftype<=FTUVES) {
if ((cptr=strstr((*spec)[i].abfile,"thar_"))==NULL ||
strncmp((*spec)[i].abfile,"thar_",5))
errormsg("UVES_rFITSlist(): Cannot identify extracted\n\
\tThAr files for flux file\n\t%s\n\
\tUVES ThAr file must have 'thar_' prefix",(*spec)[i].file);
cptr+=5;
sprintf((*spec)[i].wlfile,"%swpol_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abwlfile,"wpol_%s",cptr);
} else if ((*spec)[i].ftype==FTIRAF) {
if ((cptr=strstr((*spec)[i].abfile,"wav_"))==NULL ||
strncmp((*spec)[i].abfile,"wav_",4))
errormsg("UVES_rFITSlist(): Cannot identify extracted\n\
\tThAr files for flux file\n\t%s\n\
\tIRAF ThAr file must have 'wav_' prefix",(*spec)[i].file);
cptr+=4;
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
} else if ((*spec)[i].ftype==FTMAKE) {
if ((cptr=strstr((*spec)[i].abfile,"Wav-"))==NULL ||
strncmp((*spec)[i].abfile,"Wav-",4))
errormsg("UVES_rFITSlist(): Cannot identify extracted\n\
\tThAr flux files for flux file\n\t%s\n\
\tMAKEE ThAr file must have 'Wav-' prefix",(*spec)[i].file);
cptr+=4;
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
} else if ((*spec)[i].ftype==FTIRLS) {
if ((cptr=strstr((*spec)[i].abfile,"wav_"))==NULL ||
strncmp((*spec)[i].abfile,"wav_",4))
errormsg("UVES_rFITSlist(): Cannot identify extracted\n\
\tThAr flux files for flux file\n\t%s\n\
\tIRAFLSS ThAr file must have 'wav_' prefix",(*spec)[i].file);
cptr+=4;
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
} else if ((*spec)[i].ftype==FTHIRX) {
if ((cptr=strstr((*spec)[i].abfile,"Arc_"))==NULL ||
strncmp((*spec)[i].abfile,"Arc_",4))
errormsg("UVES_rFITSlist(): Cannot identify extracted\n\
\tThAr flux files for flux file\n\t%s\n\
\tHIREDUX ThAr file must have 'Arc_' prefix",(*spec)[i].file);
cptr+=4;
sprintf((*spec)[i].wlfile,"%s",(*spec)[i].file);
sprintf((*spec)[i].abwlfile,"%s",(*spec)[i].abfile);
} else if ((*spec)[i].ftype==FTESOM) {
if ((cptr=strstr((*spec)[i].abfile,"thar_"))==NULL ||
strncmp((*spec)[i].abfile,"thar_",5))
errormsg("UVES_rFITSlist(): Cannot identify extracted\n\
\tThAr files for flux file\n\t%s\n\
\tUVES ThAr file must have 'thar_' prefix",(*spec)[i].file);
cptr+=5;
sprintf((*spec)[i].wlfile,"%swpol_%s",(*spec)[i].path,cptr);
sprintf((*spec)[i].abwlfile,"wpol_%s",cptr);
} else if ((*spec)[i].ftype==FTMAGE) {
errormsg("UVES_rFITSlist(): Do not know how to read in\n\
\tThAr MagE files for flux file\n\t%s",(*spec)[i].file);
} else if ((*spec)[i].ftype==FTIESI) {
errormsg("UVES_rFITSlist(): Do not know how to read in\n\
\tThAr IRAF-ESI files for flux file\n\t%s",(*spec)[i].file);
} else if ((*spec)[i].ftype==FTHARP) {
errormsg("UVES_rFITSlist(): Do not know how to read in\n\
\tThAr HARPS spectrum for flux file\n\t%s",(*spec)[i].file);
}
}
}
/* Initialize the user-supplied velocity shift to be applied to each
spectrum */
for (i=0; i<*nspec; i++) (*spec)[i].vshift=(*spec)[i].vslope=(*spec)[i].refwav=0.0;
/* Initialize the random number seeds for the wavelength distortions */
ranseed=-time(NULL);
for (i=0; i<*nspec; i++) (*spec)[i].distort_seed=ranseed++;
return 1;
}