avni.tools.common module#
- avni.tools.common.stage(file: str, overwrite: bool = False)[source]#
Stages a file in the AVNI file directories for testing
- Parameters
- filestr
file name to be staged
- overwritebool, optional
overwite any existing symlink or file, by default False
- Raises
- IOError
File not found or a symlink already exists
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.parse_line(line: str, rx_dict: dict)[source]#
Function used to parse line with key word from rx_dict
- Parameters
- linestr
Line to search
- rx_dictdict
Do a regex search against all defined regexes
- Returns
- key,match
Result of the first matching regex, None if not found
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.ifwithindepth(start_depths: numpy.ndarray, end_depths: numpy.ndarray, depth_in_km: numpy.ndarray)[source]#
Check if a set of depths are within a range of depths
- Parameters
- start_depthsnp.ndarray
Starting depth for the range
- end_depthsnp.ndarray
End depth for the range
- depth_in_kmnp.ndarray
Depths to check for
- Returns
- output
index of depth range that each depth_in_km belongs to
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.makegrid(latitude: Union[list, tuple, numpy.ndarray], longitude: Union[list, tuple, numpy.ndarray], depth_in_km: Union[None, list, tuple, numpy.ndarray] = None)[source]#
Make a 2D or 3D grid out of input locations and depths.
- Parameters
- latitudetp.Union[list,tuple,np.ndarray]
Latitudes of locations queried
- longitudetp.Union[list,tuple,np.ndarray]
Longitudes of locations queried
- depth_in_kmtp.Union[None,list,tuple,np.ndarray], optional
Depths of locations queried, by default None
- Returns
- nrows,latitude,longitude,[optional: depth_in_km]
A 2D or 3D grid with nrows rows found by unraveling (depth_in_km,latitude,longitude)
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.convert2nparray(value: Union[str, list, tuple, float, numpy.int64, numpy.ndarray, bool], int2float: bool = True, allowstrings: bool = True) numpy.ndarray [source]#
Converts input value to a float numpy array. Boolean are returned as Boolean arrays.
- Parameters
- valuetp.Union[str,list,tuple,float,np.int64,np.ndarray,bool]
A single value or a set of values.
- int2floatbool, optional
Convert integer to floats, by default True
- allowstringsbool, optional
Check if value has strings, by default True
- Returns
- np.ndarray
Output values as a numpy array.
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.precision_and_scale(x: float, max_digits: int = 14) Tuple[int, int] [source]#
Returns precision and scale of a float
- Parameters
- xfloat
A floating point number
- max_digitsint, optional
Maximum digits or magnitude, by default 14
- Returns
- tp.Tuple[int, int]
Returns precision and scale of a float
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.alphanum_key(s: str) list [source]#
Helper tool to sort lists in ascending numerical order (natural sorting), rather than lexicographic sorting
- Parameters
- sstr
string to sort
- Returns
- list
Sorted list
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.diffdict(first_dict: dict, second_dict: dict) dict [source]#
Helper tool to get difference in two dictionaries
- Parameters
- first_dictdict
First dictionary
- second_dictdict
Second dictionary
- Returns
- dict
Difference
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.df2nparray(dataframe: pandas.core.frame.DataFrame) numpy.ndarray [source]#
Helper tool to return the named numpy array of the pandas dataframe
- Parameters
- dataframepd.DataFrame
Input Pandas Dataframe
- Returns
- np.ndarray
Equivalent named numpy array
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.krunge(n: int, x: float, h: Union[int, float], y: Union[list, tuple, numpy.ndarray], f: Union[list, tuple, numpy.ndarray], m: int = 0, phi: numpy.ndarray = array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0]), savey: numpy.ndarray = array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0]))[source]#
Some sort of integration or interpolation? x is incremented on second and fourth calls. Resets itself after 5’th call.
- Parameters
- nint
number of points
- xfloat
indepent variable for points
- htp.Union[int,float]
step size
- ytp.Union[list,tuple,np.ndarray]
function evaluated at each point
- ftp.Union[list,tuple,np.ndarray]
function evaluated at each point
- mint, optional
call number, by default 0
- phinp.ndarray, optional
Intermediate variable, by default np.zeros(6)
- saveynp.ndarray, optional
Intermediate variable, by default np.zeros(6)
- avni.tools.common.firstnonspaceindex(string: str) Tuple[int, int] [source]#
Gets the first and last non-space index of a string
- Parameters
- stringstr
String to search
- Returns
- tp.Tuple[int,int]
First and last non-space index
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.get_fullpath(path: str) str [source]#
Provides the full path by replacing . and ~ in path
- Parameters
- pathstr
Input file or folder path
- Returns
- str
Full path after replacing . and ~ in path
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.listfolders(path: str) list [source]#
Return a list of directories in a path
- Parameters
- pathstr
Input file or folder path
- Returns
- list
List of directories
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.get_installdir(module: str = 'avni', checkwrite: bool = True, checkenv: bool = True) str [source]#
Get the installation directory for any module in the current machine.
- Parameters
- modulestr, optional
Module to search for, by default ‘avni’
- checkwritebool, optional
Checks for write access to the files, by default True
- checkenvbool, optional
Checks if the directory is specified as an environment variable, by default True
- Returns
- str
Path to installation directory
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.get_filedir(module: str = 'avni', subdirectory: Union[None, str] = None, checkwrite: bool = True, makedir: bool = True) str [source]#
Get the local files directory for any module in the current machine.
- Parameters
- modulestr, optional
Module to search for, by default ‘avni’
- subdirectorytp.Union[None,str], optional
A directory inside the main directory, by default None
- checkwritebool, optional
Checks for write access to the files, by default True
- makedirbool, optional
Make a new directory if it doesn’t exist, by default True
- Returns
- str
Path to local files directory
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.get_cptdir(module: str = 'avni', checkwrite: bool = True, makedir: bool = True) str [source]#
Get the directory with color palettes for any module in the current machine.
- Parameters
- modulestr, optional
Module to search for, by default ‘avni’
- checkwritebool, optional
Checks for write access to the files, by default True
- makedirbool, optional
Make a new directory if it doesn’t exist, by default True
- Returns
- str
Path to local CPT color palette directory
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.get_configdir(module: str = 'avni', checkwrite: bool = True, makedir: bool = True) str [source]#
Get the directory containing configuration files.
- Parameters
- modulestr, optional
Module to search for, by default ‘avni’
- checkwritebool, optional
Checks for write access to the files, by default True
- makedirbool, optional
Make a new directory if it doesn’t exist, by default True
- Returns
- str
Path to local config directory as specified in
constants.configfolder()
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.get_projections(checkwrite: bool = True, makedir: bool = True, types: str = 'radial') Tuple[str, bool] [source]#
Get the file containing projection matrices.
- Parameters
- checkwritebool, optional
Checks for write access to the files, by default True
- makedirbool, optional
Make a new directory if doesn’t exist, by default True
- typesstr, optional
Type can be radial or lateral, by default ‘radial’
- Returns
- tp.Tuple[str,bool]
First entry is location of projection file Second end is whether this file already exists in the file system
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.writejson(nparray: numpy.ndarray, filename: str, encoding: str = 'utf-8')[source]#
Writes a json file from a numpy array
- Parameters
- nparraynp.ndarray
Input numpy array to write to JSON
- filenamestr
Output JSON file name
- encodingstr, optional
File encoding, by default ‘utf-8’
- :Authors:
Raj Moulik (moulik@caa.columbia.edu)
- :Last Modified:
2023.02.16 5.00
- avni.tools.common.readjson(filename: str, encoding: str = 'utf-8') numpy.ndarray [source]#
Reading from a JSON file to a numpy array
- Parameters
- filenamestr
Input JSON file name
- encodingstr, optional
File encoding, by default ‘utf-8’
- Returns
- np.ndarray
Output numpy array
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.uniquenumpyrow(a: numpy.ndarray)[source]#
Gets the unique rows from a numpy array and the indices. e.g. to get unique lat-lon values
- Parameters
- anp.ndarray
A numpy aray with multiple rows that needs to searched
- Returns
- unique_a,idx
Unique rows and their indices
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.sanitised_input(prompt: str, type_=None, min_=None, max_=None, range_=None)[source]#
Provide a user prompt with values between min-max or range of values
For specific values: user_input = sanitised_input(“Replace(r)/Ignore(i) this datum?”, str.lower, range_=(‘r’, ‘i’) For a range: age = sanitised_input(“Enter your age: “, int, range_=xrange(100))
- Parameters
- promptstr
Prompt to the user
- type_optional
type of input needed, by default None
- min_optional
Minimum value, by default None
- max_optional
Maximum value, by default None
- range_optional
Range of values, by default None
- Returns
- ui
User input
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.appendunits(ureg=None, system: str = 'mks', unitsfile: str = '/home/pm5113/Github/avni-private/avni/config/units.ini')[source]#
Append the custom units from unitsfile to ureg registry
- Parameters
- ureg_type_, optional
Input unit registry, by default constants.ureg
- systemstr, optional
Default unit system; if not the same as ureg changes it, by default ‘mks’
- unitsfilestr, optional
additional definitions to add to ureg, by default get_configdir()+’/’+constants.customunits
- :Authors:
Raj Moulik (moulik@caa.columbia.edu)
- :Last Modified:
2023.02.16 5.00
- avni.tools.common.convert2units(valstring: str)[source]#
Returns the value with units from a string that has units.
- Parameters
- valstringstr
A string to convert. Only space allowed is that between value and unit.
- Returns
- vals
Value with units
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00
- avni.tools.common.decimals(value: Union[list, tuple, numpy.ndarray]) numpy.ndarray [source]#
Returns the number of decimals in a set of floating point numbers.
- Parameters
- valuetp.Union[list,tuple,np.ndarray]
Set of floating point numbers
- Returns
- np.ndarray
Number of decimals in each float
- Authors
Raj Moulik (moulik@caa.columbia.edu)
- Last Modified
2023.02.16 5.00