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  ***************************************************************************/
24 /*--------------------------------------------------------------------------
25 File          : mag2flux.c
26 Author        : Sujatha N V
27 Date          : 12th August, 2004
28 Version       : 1.0
30 Program Description:
31 This program reads sp_type, magnitude & E(B-V) from an input
32 parameter file & compares the spectral type in the  TABLE in file :
33 MKSpTypewwww.in. Selects the corresponding KURUCZ flux file
34 coresponding to Temp. T and gravity g of the sp. type. Then
35 calculate scale value for Kurucz flux and calculate the flux at
36 Earth for different wavelengths read from 'wave.dat'
37 output file contains: Wavelength, Flux at Earth F(lambda)
39 ---------------------------------------------------------------------------*/
41 /*--------------------------------------------------------------------------
42 Rivision            : Author, dd/mm/yyyy
43 Revision history    :     -----
45 Reks, 12th July, 2005.
46 1. Introduced signal handling (signal_handling.c and fun_prototypes.h)
47 2. Modified all printf for error messages to fprintf->STDERR
48    Those two modifications are a must to be able to run this on web...
50 Fayaz, Sept., 2005.
51 1. Added UVS_msgs.c, to print error messages (supersedes #2 in previous
52    revision) and other fancy things like a prologue() before starting.
54 Reks, 08th Dec. 2005
55 1. Can take care of comments in input parameter file.
57 Reks, 07th July, 2007
58 1. configure-make
59 2. renamed project to mag2counts
60 3. renamed file to mag2flux
61 4. Better error messages and overall readability
62 5. moved all functions into kurucz_functions.c
64 JM: 10 July, 2007
65 1. Corrected several memory leaks in text handling
67 JM: 15 July, 2007
68 1. Cleaned up code in main program; moved parameter reading etc to functions.
69    (Still have to go through other functions.)
71 Reks: 21 July, 2007
72 1. Removed SPDATA_FROOT from paramfile.
73 2. SPDATA_DIR now points to the top level directory that contain all data
74    files, subdirectories KURUCZ_FLUX and MkSpDATA
75 3. shifted wave.dat and crossec0.dat into SPDATA_DIR (default location)
76    experienced users may over-ride this from parameter file.
77 4. added command line options to create default paramfile and help message
78 5. Still have to fix the memory leak problems with sptype inside GET_REF1!
80 JM: 21 July, 2007
81 1. fixed all memory leaks!
83 Reks: 01 July 2012:
84 1. Rebranded to CADS/UVS, reset version to 1.0 in new cads git repository
85 2. Renamed to stellar_spectrum
86 3. read/write ops shifted to read_write.c
87 ----------------------------------------------------------------------------*/
89 #include "stellar_spectrum.h"
90 #include <sys/types.h>
92 /* Main program starts here*/
93 /****************************************************************************/
94 int main (int argc, char *argv[])
95 {
97     FILE            *out_ptr = NULL, *wave_ptr = NULL;
98     struct STARS     stars;
99     struct S_TABLE   mkstTG;
100     int              gr = 0, tst;
101     char             OutFile[MAX_TEXT];
102     char             ParamFile[80];
103     int c;           /* For getopt */
105     /* Before we begin... set the signals and print a banner */
106     set_signals();
107     PRINT_BANNER();
109     strcpy(ParamFile, paramfile);/*paramfile is defined in header file*/
111     /* Command line options*/
112     while ( (c = my_getopt (argc, argv, "gh") ) != EOF)
113         switch (c)
114         {
115         case 'g':
116             gen_stspec_params(ParamFile);
117             return(EXIT_SUCCESS);
118             break;
119         case 'h':
120             usage();
121             break;
122         default:
123             usage();
124             break;
125         }
127     /* Open and read the parameter file */
128     READ_PARAMS(paramfile, &stars, OutFile);
129     /* try opening the wave file */
130     if ((wave_ptr = fopen(stars.WaveFile,"r")) == NULL)
131     {
132         fprintf(stderr, "ERROR  : Unable to open file %s\n", stars.WaveFile);
133         exit(EXIT_FAILURE);
134     }
135     /*Output file*/
136     if ((out_ptr = fopen(OutFile, "w")) == NULL)
137     {
138         fprintf(stderr, "ERROR: Unable to write to file %s\n", OutFile);
139         exit(EXIT_FAILURE);
140     }
142     WRITE_INFO(out_ptr, stars);/*Writes program information to file*/
144     /* for all wavelengths listed in this file */
145     while ((feof(wave_ptr))==0)
146     {
148         tst = GET_WAVE_NAME(wave_ptr, stars.dir_root, &mkstTG);
149         if (tst == EXIT_SUCCESS)
150         {
152             /* Read the wavelength file*/
153             printf("\rWorking: |");
154             READ_TABLE(&mkstTG);
156             /* get the crossection */
157             printf("\rWorking: /");
158             RCross_Sec(&stars, mkstTG.w_filename);
160             /*Reads ISM Absorption CrosSection*/
161             printf("\rWorking: -");
162             gr  = GET_REF1(&stars, &mkstTG);
164             printf("\rWorking: \\");
165             if (gr == 0)
166             {
167                 ALPHA(&stars);
168                 if (stars.alpha != 0.0) WRITE_LINE(out_ptr, &stars);
169             }
171         }/*End tst*/
172     } /* end of while loop */
173     printf("\rInfo   : Done\n");
174     fclose(wave_ptr);
175     fclose(out_ptr);
177     return(EXIT_SUCCESS);