1 /***************************************************************************
2 * This File is a part of the CADS/UV Stellar Spectrum model software *
3 * Copyright (C) 2012 by CADS/UV Software Team, *
4 * Indian Institute of Astrophysics *
5 * Bangalore 560034 *
6 * cads_AT_iiap.res.in *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23 #include "stellar_spectrum.h"
25 void gen_stspec_params(char *ParamFile)
26 {
27 FILE *fparam = NULL;
28 printf("Info : Creating default parameter file %s\n", ParamFile);
30 fparam = fopen(ParamFile,"w");
31 if (fparam == NULL)
32 {
33 fprintf(stderr, "ERROR : Unable to create %s\n", ParamFile);
34 exit(EXIT_FAILURE);
35 }
37 fprintf(fparam,
38 "#-----------------------------------------------------#\n"
39 "# CADS/UVS Stellar Spectrum Calculator - Parameter FILE\n"
40 "# \n"
41 "# Purpose: Input parameters for generating a stellar \n"
42 "# spectrum using KURUCZ models. \n"
43 "# \n"
44 "# NOTE: Any line beginning with a hash (\"#\") or\n"
45 "# those enclosed within \"/* -- */ \" are \n"
46 "# comments and will be ignored by the software. \n"
47 "# \n"
48 "# format of this file is: KEY = VALUE \n"
49 "#-----------------------------------------------------#\n\n\n"
50 );
52 fprintf(fparam,
53 "# Source Information: \n"
54 "# ------------------- \n"
55 "# 1. Source V magnitude\n"
56 "V_MAGNITUDE = 0.0 \n\n"
57 "# 2. Spectral type \n"
58 "SPECTRAL_TYPE = G2V \n\n"
59 "# 3. E(B - V) \n"
60 "E(B-V) = 0.0 \n\n\n"
61 );
63 fprintf(fparam,
64 "# Output file \n"
65 "# ------------------- \n"
66 "OUTPUT_FILE = spectrum.dat\n\n\n"
67 );
69 fprintf(fparam,
70 "# Stellar Data files information: \n"
71 "# ---------------------------------- \n"
72 "# 1. Directory containing Spectral data \n"
73 "SPDATA_DIR = %sspectral_data \n\n", CADSDATA
74 );
76 fprintf(fparam,
77 "#************************************** \n"
78 "# NO MORE CHANGES ARE REQUIRED IN THIS \n"
79 "# SECTION UNDER NORMAL CIRCUMSTANCES \n"
80 "#************************************** \n"
81 "# 2. The Cross Section File. Default is \n"
82 "# <SPDATA_DIR>/crossec0.dat \n"
83 "# CROSS_SECTION_FILE = crossec0.dat \n\n"
84 "# 3. File containing list of wavelengths\n"
85 "# for which flux will be calculated \n"
86 "# Default is <SPDATA_DIR>/wave.dat \n"
87 "# WAVELENGTH_FILE = wave.dat \n\n"
88 );
90 printf("Info : Modify the file as per your taste and re-run\n\n");
91 fclose(fparam);
92 }
94 /****************************************************************************/
95 void PRINT_BANNER()
96 {
97 printf("-----------------------------------------------------------\n");
98 printf(" %s %s \n", PROGRAM, VERSION);
99 printf(" Calculates stellar flux using KURUCZ models\n");
100 printf("-----------------------------------------------------------\n");
101 }
103 void usage()
104 {
105 printf (" Usage: uvs_stellar_spectrum [options]\n\n");
106 printf (" options are:\n");
107 printf (" [-g] Generates the default parameter file \n");
108 printf (" [-h] help \n\n" );
109 printf (" In the absence of command line options, the program would\n");
110 printf (" read inputs on stellar magnitude, reddening and spectral \n");
111 printf (" type from the parameter file and generates the stellar \n");
112 printf (" spectrum using kurucz stellar models. \n");
113 printf ("-----------------------------------------------------------\n");
114 printf ("Copyright (c) 2012 CADS Team, IIAp. [GPL v3.0 or later]\n");
115 printf ("Report Bugs to: cads.iiap.res.in/bugzilla\n" );
116 printf ("Documentation : cads.iiap.res.in/software\n\n");
117 exit(1);
118 }
119 /***************************************************************/
121 void READ_PARAMS(char *ParamFile, struct STARS *stars, char *OutFile)
122 {
123 FILE *file_ptr = NULL;
124 char line[MAX_TEXT], name[MAX_TEXT], equal[MAX_TEXT], data[MAX_TEXT];
125 char tmpstr[MAX_TEXT]; /* for temperory usage */
127 if ((file_ptr = fopen(ParamFile,"r")) == NULL)
128 {
129 fprintf(stderr,"ERROR : Unable to open the input parameter file\n");
130 gen_stspec_params(ParamFile);
131 exit(EXIT_FAILURE);
132 }
134 /* read the parameter file */
135 while ((fgets(line, MAX_TEXT, file_ptr)) != NULL)
136 {
137 if ((line[0] != '#') && (line[0] != '\n'))
138 {
139 sscanf(line, "%s %s %s ", name, equal, data);
140 if (!strcmp("V_MAGNITUDE", name)) stars->mv = atof(data);
141 if (!strcmp("E(B-V)", name)) stars->Ebv = atof(data);
142 if (!strcmp("SPECTRAL_TYPE", name)) strcpy(stars->sp_type, data);
143 if (!strcmp("OUTPUT_FILE", name)) strcpy(OutFile, data);
144 if (!strcmp("SPDATA_DIR", name)) strcpy(stars->dir_root, data);
146 /* Now prepare default values for Wavefile and Sigmafile..
147 (will use these if they are not defined in paramfile ) */
148 strcpy(tmpstr, stars->dir_root);
149 strcat(tmpstr, "/");
150 strcpy(stars->WaveFile, tmpstr);
151 strcpy(stars->SigmaFile, tmpstr);
153 if (!strcmp("CROSS_SECTION_FILE", name)) strcpy(stars->SigmaFile, data);
154 if (!strcmp("WAVELENGTH_FILE", name)) strcpy(stars->WaveFile, data);
155 strcpy(line, "End of File");
156 }
157 }/*End File Read*/
158 fclose(file_ptr);/* close the parameter file */
160 /* Tell the user about the input values */
161 printf("Info : V Magnitude = %f\n", stars->mv);
162 printf("Info : Spectral Type = %s\n", stars->sp_type);
163 printf("Info : Reddening = %f\n", stars->Ebv);
164 printf("Info : Spectral Data dir = %s\n", stars->dir_root);
166 /* Print out cross section filename only if user specified the location.
167 Otherwise, load default value and keep quiet */
168 if (strcmp(stars->SigmaFile, tmpstr) != 0)
169 printf("Info : Cross section file = %s\n", stars->SigmaFile);
170 else strcat(stars->SigmaFile, SIGMA_FILE);
173 /* Same for the Wavelength filename */
174 if (strcmp(stars->WaveFile, tmpstr) != 0)
175 printf("Info : Wavelength file = %s\n", stars->WaveFile);
176 else strcat(stars->WaveFile, WAVELENGTH_FILE);
178 printf("Info : Output file = %s\n", OutFile);
180 }/*End READ_PARAM*/
181 /****************************************************************************/
182 int GET_WAVE_NAME(FILE *wave_ptr, char *dir_root, struct S_TABLE *mkstTG)
183 {
184 float W0;
185 char wwww[6];
186 char filename[MAX_TEXT];
187 int tst;
189 tst = fscanf(wave_ptr, "%f", &W0);
190 if (tst < 0) return(EXIT_FAILURE);
191 mkstTG->OWlength=W0;
193 /* make the file name as MKSpTypewwww.in */
194 /* first copy the dir/root */
195 strcpy(filename, dir_root);
196 strcat(filename, "/");
197 strcat(filename, MKSPDATA_DIR);
198 strcat(filename, "/");
199 strcat(filename, MKSPDATA_FROOT);
200 sprintf(wwww,"%.0f",mkstTG->OWlength);
201 strcat(filename,wwww);
202 strcat(filename,".in");
204 /* copy the file name to structure field */
205 strcpy(mkstTG->w_filename, filename);
206 return(EXIT_SUCCESS);
207 }
208 /***************************************************************************/
210 void WRITE_INFO(FILE *out_ptr, struct STARS stars)
211 {
213 /* Write a header with some info on developers and all input values */
214 fprintf(out_ptr, "#------------------------------------------------#\n");
215 fprintf(out_ptr, "# Stellar Spectrum generated by %s %s\n", PROGRAM, VERSION);
216 fprintf(out_ptr, "# (based on KURUCZ model) \n");
217 fprintf(out_ptr, "# Copyright (c) by the CADS Team, IIAp\n");
218 fprintf(out_ptr, "# Bug reports: http://cads.iiap.res.in/bugzilla/\n");
219 fprintf(out_ptr, "# Project Website: http://cads.iiap.res.in\n");
220 fprintf(out_ptr, "#------------------------------------------------#\n");
221 fprintf(out_ptr, "# user inputs:\n");
222 fprintf(out_ptr, "# V magnitude = %f\n", stars.mv);
223 fprintf(out_ptr, "# Spectral Type = %s\n", stars.sp_type);
224 fprintf(out_ptr, "# E(B - V) = %f\n", stars.Ebv);
225 fprintf(out_ptr, "#------------------------------------------------#\n");
226 fprintf(out_ptr, "# Column 1: Wavelength (Angstroms) \n");
227 fprintf(out_ptr, "# Column 2: Flux density (ergs/cm^2/s/A) \n");
228 fprintf(out_ptr, "#------------------------------------------------#\n");
229 }