cloudy
trunk
|
00001 /* This file is part of Cloudy and is copyright (C)1978-2008 by Gary J. Ferland and 00002 * others. For conditions of distribution and use see copyright notice in license.txt */ 00003 /*cdGetLineList routine to read in master list of emission line wavelengths and ids, for 00004 * generating loc grids */ 00005 #include "cddefines.h" 00006 #include "cddrive.h" 00007 00008 /* return value is number of lines, -1 if file could not be opened */ 00009 long int cdGetLineList( 00010 /* chFile is optional filename, if void then use BLRLineList, 00011 * if not void then use file specified */ 00012 const char chFile[] , 00013 /* 2d array of null term strings giving line labels char chLabels[nLines][10] */ 00014 char ***chLabels , 00015 /* a 1-d array of line wavelengths */ 00016 realnum **wl ) 00017 { 00018 long int i , 00019 nLines; 00020 bool lgDONE; 00021 FILE *ioData; 00022 00023 char chLine[FILENAME_PATH_LENGTH_2]; 00024 const char* chFilename; 00025 00026 DEBUG_ENTRY( "cdGetLineList()" ); 00027 00028 /* first check that cdInit has been called, since we may have to write 00029 * error output */ 00030 if( !lgcdInitCalled ) 00031 { 00032 fprintf(stderr," cdInit must be called before cdGetLineList.\n"); 00033 cdEXIT(EXIT_FAILURE); 00034 } 00035 00036 /* use default filename LineList_BLR.dat if void string, else use file specified */ 00037 chFilename = ( strlen(chFile) == 0 ) ? "LineList_BLR.dat" : chFile; 00038 00039 /* we will check local space first, then on path if not present */ 00040 ioData = open_data( chFilename, "r", AS_LOCAL_DATA_TRY ); 00041 00042 if( ioData == NULL ) 00043 { 00044 /* did not find file, return -1 */ 00045 return -1; 00046 } 00047 00048 /* count how many lines are in the file, ignoring all lines 00049 * starting with '#' */ 00050 nLines = 0; 00051 lgDONE = false; 00052 while( (read_whole_line( chLine , (int)sizeof(chLine) , ioData ) != NULL) && !lgDONE ) 00053 { 00054 if( chLine[0] == '\n') 00055 { 00056 lgDONE = true; 00057 continue; 00058 } 00059 00060 /* we want to count the lines that do not start with # 00061 * since these contain data */ 00062 if( (chLine[0] != '#') ) 00063 ++nLines; 00064 } 00065 00066 *wl = (realnum *)MALLOC( (size_t)(nLines+1)*sizeof(realnum ) ); 00067 00068 /* create 1-d array of string labels */ 00069 *chLabels = (char**)MALLOC((size_t)(nLines+1)*sizeof(char *) ); 00070 00071 /* now rewind the file so we can read it a second time*/ 00072 if( fseek( ioData , 0 , SEEK_SET ) != 0 ) 00073 { 00074 fprintf( ioQQQ, " cdGetLineList could not rewind line list.\n"); 00075 return( -1 ); 00076 } 00077 00078 /* actually read and save the lines */ 00079 i = 0; 00080 lgDONE = false; 00081 while( (read_whole_line( chLine , (int)sizeof(chLine) , ioData ) != NULL) && !lgDONE) 00082 { 00083 long j; 00084 bool lgEOL; 00085 00086 if( chLine[0] == '\n') 00087 { 00088 lgDONE = true; 00089 continue; 00090 } 00091 /* skip lines that begin with # */ 00092 if( chLine[0] == '#') 00093 continue; 00094 00095 /* create second dim of space for labels */ 00096 (*chLabels)[i] = (char*)MALLOC(5*sizeof(char) ); 00097 00098 strncpy( (*chLabels)[i] , chLine , 4); 00099 (*chLabels)[i][4] = 0; 00100 00101 /* get and save the wavelength */ 00102 j = 5; 00103 (*wl)[i] = (realnum)FFmtRead(chLine,&j,INPUT_LINE_LENGTH,&lgEOL); 00104 00105 /* check for optional micron or cm units, else interpret as Angstroms */ 00106 if( chLine[j-1] == 'M' || chLine[j-1] == 'm') 00107 { 00108 /* microns */ 00109 (*wl)[i] *= 1e4; 00110 } 00111 else if( chLine[j-1] == 'C' || chLine[j-1] == 'c') 00112 { 00113 /* centimeters */ 00114 (*wl)[i] *= 1e8; 00115 } 00116 00117 ++i; 00118 } 00119 00120 fclose( ioData ); 00121 00122 /* return number of lines we found */ 00123 return nLines; 00124 }