root/branches/newmole/source/cdgetlinelist.cpp

Revision 2023, 3.1 kB (checked in by rjrw, 8 months ago)

Merged from trunk r1941:2022

  • Property svn:eol-style set to native
Line 
1/* This file is part of Cloudy and is copyright (C)1978-2008 by Gary J. Ferland and
2 * others.  For conditions of distribution and use see copyright notice in license.txt */
3/*cdGetLineList routine to read in master list of emission line wavelengths and ids, for
4 * generating loc grids */
5#include "cddefines.h"
6#include "cddrive.h"
7
8/* return value is number of lines, -1 if file could not be opened */
9long int cdGetLineList(
10        /* chFile is optional filename, if void then use BLRLineList,
11         * if not void then use file specified */
12        const char chFile[] ,
13        /* 2d array of null term strings giving line labels char chLabels[nLines][10] */
14        char ***chLabels ,
15        /* a 1-d array of line wavelengths */
16        realnum **wl )
17{
18        long int i ,
19                nLines;
20        bool lgDONE;
21        FILE *ioData;
22
23        char chLine[FILENAME_PATH_LENGTH_2];
24        const char* chFilename;
25
26        DEBUG_ENTRY( "cdGetLineList()" );
27
28        /* first check that cdInit has been called, since we may have to write
29         * error output */
30        if( !lgcdInitCalled )
31        {
32                fprintf(stderr," cdInit must be called before cdGetLineList.\n");
33                cdEXIT(EXIT_FAILURE);
34        }
35
36        /* use default filename LineList_BLR.dat if void string, else use file specified */
37        chFilename = ( strlen(chFile) == 0 ) ? "LineList_BLR.dat" : chFile;
38
39        /* we will check local space first, then on path if not present */
40        ioData = open_data( chFilename, "r", AS_LOCAL_DATA_TRY );
41
42        if( ioData == NULL )
43        {
44                /* did not find file, return -1 */
45                return -1;
46        }
47
48        /* count how many lines are in the file, ignoring all lines
49         * starting with '#' */
50        nLines = 0;
51        lgDONE = false;
52        while( (read_whole_line( chLine , (int)sizeof(chLine) , ioData ) != NULL) && !lgDONE )
53        {
54                if( chLine[0] == '\n')
55                {
56                        lgDONE = true;
57                        continue;
58                }
59
60                /* we want to count the lines that do not start with #
61                 * since these contain data */
62                if( (chLine[0] != '#') )
63                        ++nLines;
64        }
65
66        *wl = (realnum *)MALLOC( (size_t)(nLines+1)*sizeof(realnum ) );
67
68        /* create 1-d array of string labels */
69        *chLabels = (char**)MALLOC((size_t)(nLines+1)*sizeof(char *) );
70
71        /* now rewind the file so we can read it a second time*/
72        if( fseek( ioData , 0 , SEEK_SET ) != 0 )
73        {
74                fprintf( ioQQQ, " cdGetLineList could not rewind line list.\n");
75                return( -1 );
76        }
77
78        /* actually read and save the lines */
79        i = 0;
80        lgDONE = false;
81        while( (read_whole_line( chLine , (int)sizeof(chLine) , ioData ) != NULL) && !lgDONE)
82        {
83                long j;
84                bool lgEOL;
85
86                if( chLine[0] == '\n')
87                {
88                        lgDONE = true;
89                        continue;
90                }
91                /* skip lines that begin with # */
92                if( chLine[0] == '#')
93                        continue;
94
95                /* create second dim of space for labels */
96                (*chLabels)[i] = (char*)MALLOC(5*sizeof(char) );
97
98                strncpy( (*chLabels)[i] , chLine , 4);
99                (*chLabels)[i][4] = 0;
100
101                /* get and save the wavelength */
102                j = 5;
103                (*wl)[i] = (realnum)FFmtRead(chLine,&j,INPUT_LINE_LENGTH,&lgEOL);
104
105                /* check for optional micron or cm units, else interpret as Angstroms */
106                if( chLine[j-1] == 'M' || chLine[j-1] == 'm')
107                {
108                        /* microns */
109                        (*wl)[i] *= 1e4;
110                }
111                else if( chLine[j-1] == 'C'  || chLine[j-1] == 'c')
112                {
113                        /* centimeters */
114                        (*wl)[i] *= 1e8;
115                }
116
117                ++i;
118        }
119
120        fclose( ioData );
121
122        /* return number of lines we found */
123        return nLines;
124}
Note: See TracBrowser for help on using the browser.