cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cdgetlinelist.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2017 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 #include "lines.h"
8 #include "parser.h"
9 
10 /* return value is number of lines, -1 if file could not be opened */
11 long int cdGetLineList(
12  /* chFile is optional filename, if void then use BLRLineList,
13  * if not void then use file specified */
14  const char chFile[],
15  /* array of null term strings giving line labels */
16  vector<string>& chLabels,
17  /* a 1-d array of line wavelengths */
18  vector<realnum>& wl)
19 {
20  DEBUG_ENTRY( "cdGetLineList()" );
21 
22  /* first check that cdInit has been called, since we may have to write
23  * error output */
24  if( !lgcdInitCalled )
25  {
26  fprintf(stderr," cdInit must be called before cdGetLineList.\n");
28  }
29 
30  /* use default filename LineList_BLR.dat if void string, else use file specified */
31  const char* chFilename = ( strlen(chFile) == 0 ) ? "LineList_BLR.dat" : chFile;
32 
33  /* we will check local space first, then on path if not present */
34  FILE* ioData = open_data( chFilename, "r", AS_LOCAL_DATA_TRY );
35 
36  if( ioData == NULL )
37  {
38  /* did not find file, return -1 */
39  return -1;
40  }
41 
42  // make sure we are not leaking memory
43  ASSERT( chLabels.size() == 0 && wl.size() == 0 );
44 
45  Parser p;
46  char chLine[FILENAME_PATH_LENGTH_2];
47 
48  /* actually read and save the lines */
49  while( read_whole_line( chLine, (int)sizeof(chLine), ioData ) != NULL )
50  {
51  if( chLine[0] == '\n' )
52  break;
53 
54  /* skip lines that begin with # */
55  if( chLine[0] == '#' )
56  continue;
57 
58  p.setline(chLine);
59  LineID line = p.getLineID();
60  chLabels.push_back(line.chLabel);
61  wl.push_back(line.wave);
62  }
63 
64  fclose( ioData );
65 
66  /* return number of lines we found */
67  return chLabels.size();
68 }
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition: cpu.cpp:751
realnum wave
Definition: lines.h:18
const int FILENAME_PATH_LENGTH_2
Definition: cddefines.h:296
void setline(const char *const card)
Definition: parser.h:79
long int cdGetLineList(const char chFile[], vector< string > &chLabels, vector< realnum > &wl)
Definition: lines.h:14
bool lgcdInitCalled
Definition: cdinit.cpp:27
string chLabel
Definition: lines.h:17
Definition: parser.h:42
#define EXIT_FAILURE
Definition: cddefines.h:168
#define cdEXIT(FAIL)
Definition: cddefines.h:484
#define ASSERT(exp)
Definition: cddefines.h:617
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:729
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1217
char * read_whole_line(char *chLine, int nChar, FILE *ioIN)
Definition: service.cpp:72