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");
103 void usage()
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 ("    [-p] <parameter_file> : Use inputs from <parameter_file>\n");
109     printf ("    [-h] help \n\n" );
110     printf (" In the absence of command line options, the program would\n");
111     printf (" read inputs on stellar  magnitude, reddening and spectral \n");
112     printf (" type from  the parameter file and generates  the  stellar \n");
113     printf (" spectrum using  kurucz stellar models.                    \n");
114     printf ("-----------------------------------------------------------\n");
115     printf ("Copyright (c) 2012 CADS Team, IIAp. [GPL v3.0 or later]\n");
116     printf ("Report Bugs to: cads.iiap.res.in/bugzilla\n" );
117     printf ("Documentation : cads.iiap.res.in/software\n\n");
118     exit(1);
120 /***************************************************************/
122 void READ_PARAMS(char *ParamFile, struct STARS *stars, char *OutFile)
124     FILE *file_ptr = NULL;
125     char   line[MAX_TEXT], name[MAX_TEXT], equal[MAX_TEXT], data[MAX_TEXT];
126     char   tmpstr[MAX_TEXT];  /* for temperory usage */
128     if ((file_ptr = fopen(ParamFile,"r")) == NULL)
129     {
130         fprintf(stderr,"ERROR  : Unable to open the input parameter file\n");
131         gen_stspec_params(ParamFile);
132         exit(EXIT_FAILURE);
133     }
135     /* read the parameter file */
136     while ((fgets(line, MAX_TEXT, file_ptr)) != NULL)
137     {
138         if ((line[0] != '#') && (line[0] != '\n'))
139         {
140             sscanf(line, "%s %s %s ", name, equal, data);
141             if (!strcmp("V_MAGNITUDE", name)) stars->mv = atof(data);
142             if (!strcmp("E(B-V)", name)) stars->Ebv = atof(data);
143             if (!strcmp("SPECTRAL_TYPE", name)) strcpy(stars->sp_type, data);
144             if (!strcmp("OUTPUT_FILE", name)) strcpy(OutFile, data);
145             if (!strcmp("SPDATA_DIR", name)) strcpy(stars->dir_root, data);
147             /* Now prepare default values for Wavefile and Sigmafile..
148                                    (will use these if they are not defined in paramfile ) */
149             strcpy(tmpstr, stars->dir_root);
150             strcat(tmpstr, "/");
151             strcpy(stars->WaveFile, tmpstr);
152             strcpy(stars->SigmaFile, tmpstr);
154             if (!strcmp("CROSS_SECTION_FILE", name)) strcpy(stars->SigmaFile, data);
155             if (!strcmp("WAVELENGTH_FILE", name)) strcpy(stars->WaveFile, data);
156             strcpy(line, "End of File");
157         }
158     }/*End File Read*/
159     fclose(file_ptr);/* close the parameter file */
161     /* Tell the user about the input values */
162     printf("Info   : V Magnitude        = %f\n", stars->mv);
163     printf("Info   : Spectral Type      = %s\n", stars->sp_type);
164     printf("Info   : Reddening          = %f\n", stars->Ebv);
165     printf("Info   : Spectral Data dir  = %s\n", stars->dir_root);
167     /* Print out cross section filename only if user specified the location.
168               Otherwise, load default value and keep quiet */
169     if (strcmp(stars->SigmaFile, tmpstr) != 0)
170         printf("Info   : Cross section file = %s\n", stars->SigmaFile);
171     else strcat(stars->SigmaFile, SIGMA_FILE);
174     /* Same for the Wavelength filename */
175     if (strcmp(stars->WaveFile, tmpstr) != 0)
176         printf("Info   : Wavelength file    = %s\n", stars->WaveFile);
177     else strcat(stars->WaveFile, WAVELENGTH_FILE);
179     printf("Info   : Output file        = %s\n", OutFile);
181 }/*End READ_PARAM*/
182 /****************************************************************************/
183 int GET_WAVE_NAME(FILE *wave_ptr, char *dir_root, struct S_TABLE *mkstTG)
185     float W0;
186     char wwww[6];
187     char filename[MAX_TEXT];
188     int tst;
190     tst = fscanf(wave_ptr, "%f", &W0);
191     if (tst < 0) return(EXIT_FAILURE);
192     mkstTG->OWlength=W0;
194     /* make the file name as MKSpTypewwww.in */
195     /* first copy the dir/root */
196     strcpy(filename, dir_root);
197     strcat(filename, "/");
198     strcat(filename, MKSPDATA_DIR);
199     strcat(filename, "/");
200     strcat(filename, MKSPDATA_FROOT);
201     sprintf(wwww,"%.0f",mkstTG->OWlength);
202     strcat(filename,wwww);
203     strcat(filename,".in");
205     /* copy the file name to structure field */
206     strcpy(mkstTG->w_filename, filename);
207     return(EXIT_SUCCESS);
209 /***************************************************************************/
211 void WRITE_INFO(FILE *out_ptr, struct STARS stars)
214     /* Write a header with some info on developers and all input values */
215     fprintf(out_ptr, "#------------------------------------------------#\n");
216     fprintf(out_ptr, "# Stellar Spectrum generated by %s %s\n", PROGRAM, VERSION);
217     fprintf(out_ptr, "# (based on KURUCZ model) \n");
218     fprintf(out_ptr, "# Copyright (c) by the CADS Team, IIAp\n");
219     fprintf(out_ptr, "# Bug reports: http://cads.iiap.res.in/bugzilla/\n");
220     fprintf(out_ptr, "# Project Website: http://cads.iiap.res.in\n");
221     fprintf(out_ptr, "#------------------------------------------------#\n");
222     fprintf(out_ptr, "# user inputs:\n");
223     fprintf(out_ptr, "#             V magnitude   = %f\n", stars.mv);
224     fprintf(out_ptr, "#             Spectral Type = %s\n", stars.sp_type);
225     fprintf(out_ptr, "#             E(B - V)      = %f\n", stars.Ebv);
226     fprintf(out_ptr, "#------------------------------------------------#\n");
227     fprintf(out_ptr, "# Column 1: Wavelength (Angstroms)                \n");
228     fprintf(out_ptr, "# Column 2: Flux density (ergs/cm^2/s/A)          \n");
229     fprintf(out_ptr, "#------------------------------------------------#\n");