Package ComboCode :: Package cc :: Package tools :: Package readers :: Module SpectroscopyReader
[hide private]
[frames] | no frames]

Source Code for Module ComboCode.cc.tools.readers.SpectroscopyReader

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  A tool set for reading spectroscopy transition-based data. 
  5   
  6  Author: R. Lombaert 
  7   
  8  """ 
  9   
 10  import numpy as np, collections 
 11   
 12  from cc.tools.io import DataIO 
 13  from cc.tools.readers.Reader import Reader 
 14  from cc.data import Data 
 15   
 16   
17 -class SpectroscopyReader(Reader):
18 19 ''' 20 The SpectroscopyReader class. 21 22 Inherits from the Reader class, and thus functions much like a 23 dictionary. 24 25 SpectroscopyReader functions primarily as an intermediary class between 26 Reader and MolReader/CollisReader, both classes that make heavy use of 27 transition based indexing, and does relate to upper and lower level indices. 28 29 Typically not used stand-alone. Refer to MolReader and CollisReader for 30 practical uses. Both classes make use of this parent class' methods. 31 32 ''' 33
34 - def __init__(self,fn,*args,**kwargs):
35 36 ''' 37 Initialize an SpectroscopyReader object. 38 39 Additional args & kwargs passed to __init__ are passed to dict __init__. 40 41 @param fn: The filename of the file that is being read. 42 @type fn: str 43 44 ''' 45 46 #-- Create the dictionary instance 47 super(SpectroscopyReader,self).__init__(fn,*args,**kwargs)
48 49 50
51 - def get(self,ptype,prop,index=None):
52 53 ''' 54 Return a property for a given type of property possibly for a given 55 index. 56 57 In case a single value is requested via index, the property value is 58 extracted from the array. 59 60 @param ptype: The type of property. 'coll_trans', 'trans' or 'level'. 61 @type ptype: str 62 @param prop: The property itself. eg 'energy' for level, or 'lup' for 63 trans, or 'rates' for coll_trans. 64 @type prop: str 65 66 @keyword index: The index. In case of default, all are returned. Can be 67 any array-like object that includes indices 68 69 (default: None) 70 @type index: int/array 71 72 @return: The property sorted by property type index, or a single element 73 @rtype: float/int/array 74 75 ''' 76 77 #-- Return all if no index specified, otherwise set an iterable. 78 if index is None: 79 return self[ptype][prop] 80 81 #-- Prefer explicit selection on indexing. Using indexing instead of 82 # np.in1d to maintain the original shape of index. Assumes indexing in 83 # files goes 1 -> i_max. This is normally the case. Much faster. 84 #selection = self[ptype][prop][np.in1d(self[ptype]['index'],index)] 85 selection = self[ptype][prop][Data.arrayify(index)-1] 86 87 #-- If a non-iterable object was passed as index, return just one value 88 # if only one value was indeed found. Otherwise, just return as is. 89 if selection.shape != (1,) or isinstance(index,collections.Iterable): 90 return selection 91 else: 92 return selection[0]
93 94 95
96 - def getTI(self,itype,lup=None,llow=None):
97 98 ''' 99 Return the indices of the transitions read from the molecular 100 spectroscopy data or from the collisional rate data. 101 102 A specific index (or array of indices) can be requested by specifying 103 the lower and upper level index. 104 105 Multiple lup and/or llow can be given in one go to make a selection of 106 indices. 107 108 @param itype: The type of index. MolReader needs this to be 'trans'. 109 CollisReader needs this to be 'coll_trans'. 110 @type itype: str 111 112 @keyword lup: The index of the upper energy level in the transition. If 113 both this and llow are None, all transition indices are 114 returned. 115 116 (default: None) 117 @type lup: int/array 118 @keyword llow: The index of the lower energy level in the transition. If 119 both this and lup are None, all transition indices are 120 returned. 121 122 (default: None) 123 @type llow: int/array 124 125 @return: The transition indices. A single integer if both lup and llow 126 were single integers. 127 @rtype: int/array 128 129 ''' 130 131 #-- Return all if no index given 132 if lup is None and llow is None: 133 return self[itype]['index'] 134 135 #-- Check for matches for given lup and llow. Will be empty array if no 136 # match found. 137 bools = np.ones(shape=self[itype]['index'].shape,dtype=bool) 138 if not lup is None: 139 bools *= np.in1d(self[itype]['lup'],lup) 140 #self[itype]['lup'] == lup 141 if not llow is None: 142 bools *= np.in1d(self[itype]['llow'],llow) 143 #self[itype]['llow'] == llow 144 selection = self[itype]['index'][bools] 145 146 #-- If both lup and llow were defined, only one index should be returned 147 # at least if only one index was found. Else return the entire array 148 if selection.shape != (1,) or isinstance(lup,collections.Iterable) \ 149 or isinstance(llow,collections.Iterable): 150 return selection 151 else: 152 return selection[0]
153 154 155
156 - def getTUpper(self,index=None,itype='trans'):
157 158 ''' 159 Return the indices of the upper states of the <nline> included 160 transitions. 161 162 These are NOT the quantum numbers! Especially for CO-type molecules, 163 the J-level is equal to level_index-1. 164 165 In case a single upper level is requested via index, the level index is 166 extracted from the array. 167 168 @keyword index: The index. In case of default, all are returned. Can be 169 any array-like object that includes indices 170 171 (default: None) 172 @type index: int/array 173 @keyword itype: The type of index. Default is for MolReader. 174 CollisReader needs this to be 'coll_trans'. 175 176 (default: 'trans') 177 @type itype: str 178 179 @return: The upper level indices/x ordered by transition index. 180 @rtype: array 181 182 ''' 183 184 return self.get(itype,'lup',index)
185 186 187
188 - def getTLower(self,index=None,itype='trans'):
189 190 ''' 191 Return the indices of the lower states of the <nline> included 192 transitions. 193 194 These are NOT the quantum numbers! Especially for CO-type molecules, 195 the J-level is equal to level_index-1. 196 197 In case a single lower level is requested via index, the level index is 198 extracted from the array. 199 200 @keyword index: The index. In case of default, all are returned. Can be 201 any array-like object that includes indices 202 203 (default: None) 204 @type index: int/array 205 @keyword itype: The type of index. Default is for MolReader. 206 CollisReader needs this to be 'coll_trans'. 207 208 (default: 'trans') 209 @type itype: str 210 211 @return: The lower level indices/x ordered by transition index. 212 @rtype: array 213 214 ''' 215 216 return self.get(itype,'llow',index)
217