repository_name
stringlengths
7
55
func_path_in_repository
stringlengths
4
223
func_name
stringlengths
1
134
whole_func_string
stringlengths
75
104k
language
stringclasses
1 value
func_code_string
stringlengths
75
104k
func_code_tokens
listlengths
19
28.4k
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
listlengths
1
1.97k
split_name
stringclasses
1 value
func_code_url
stringlengths
87
315
nickmckay/LiPD-utilities
Python/lipd/misc.py
get_ensemble_counts
def get_ensemble_counts(d): """ Determine if this is a 1 or 2 column ensemble. Then determine how many columns and rows it has. :param dict d: Metadata (table) :return dict _rows_cols: Row and column counts """ _rows_cols = {"rows": 0, "cols": 0} try: if len(d) == 1: for var, data in d.items(): # increment columns by one _rows_cols["cols"] += len(data["values"]) # get row count by getting len of column (since it's only one list _rows_cols["rows"] = len(data["values"][0]) break elif len(d) == 2: for var, data in d.items(): # multiple columns in one. list of lists if isinstance(data["number"], list): # add total amount of columns to the running total _rows_cols["cols"] += len(data["values"]) # single column. one list else: # increment columns by one _rows_cols["cols"] += 1 # get row count by getting len of column (since it's only one list _rows_cols["rows"] = len(data["values"]) except Exception as e: logger_misc.warn("get_ensemble_counts: {}".format(e)) return _rows_cols
python
def get_ensemble_counts(d): """ Determine if this is a 1 or 2 column ensemble. Then determine how many columns and rows it has. :param dict d: Metadata (table) :return dict _rows_cols: Row and column counts """ _rows_cols = {"rows": 0, "cols": 0} try: if len(d) == 1: for var, data in d.items(): # increment columns by one _rows_cols["cols"] += len(data["values"]) # get row count by getting len of column (since it's only one list _rows_cols["rows"] = len(data["values"][0]) break elif len(d) == 2: for var, data in d.items(): # multiple columns in one. list of lists if isinstance(data["number"], list): # add total amount of columns to the running total _rows_cols["cols"] += len(data["values"]) # single column. one list else: # increment columns by one _rows_cols["cols"] += 1 # get row count by getting len of column (since it's only one list _rows_cols["rows"] = len(data["values"]) except Exception as e: logger_misc.warn("get_ensemble_counts: {}".format(e)) return _rows_cols
[ "def", "get_ensemble_counts", "(", "d", ")", ":", "_rows_cols", "=", "{", "\"rows\"", ":", "0", ",", "\"cols\"", ":", "0", "}", "try", ":", "if", "len", "(", "d", ")", "==", "1", ":", "for", "var", ",", "data", "in", "d", ".", "items", "(", ")"...
Determine if this is a 1 or 2 column ensemble. Then determine how many columns and rows it has. :param dict d: Metadata (table) :return dict _rows_cols: Row and column counts
[ "Determine", "if", "this", "is", "a", "1", "or", "2", "column", "ensemble", ".", "Then", "determine", "how", "many", "columns", "and", "rows", "it", "has", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L265-L299
nickmckay/LiPD-utilities
Python/lipd/misc.py
get_missing_value_key
def get_missing_value_key(d): """ Get the Missing Value entry from a table of data. If none is found, try the columns. If still none found, prompt user. :param dict d: Table of data :return str _mv: Missing Value """ _mv = "nan" # Attempt to find a table-level missing value key try: # check for missing value key at the table root _mv = d["missingValue"] except KeyError as e: logger_misc.info("get_missing_value: No missing value key found: {}".format(e)) except AttributeError as e: logger_misc.warn("get_missing_value: Column is wrong data type: {}".format(e)) # No table-level missing value found. Attempt to find a column-level missing value key if not _mv: try: # loop for each column of data, searching for a missing value key for k, v in d["columns"].items(): # found a column with a missing value key. Store it and exit the loop. _mv = v["missingValue"] break except KeyError: # There are no columns in this table. We've got bigger problems! pass # No table-level or column-level missing value. Out of places to look. Ask the user to enter the missing value # used in this data # if not _mv: # print("No 'missingValue' key provided. Please type the missingValue used in this file: {}\n".format(filename)) # _mv = input("missingValue: ") return _mv
python
def get_missing_value_key(d): """ Get the Missing Value entry from a table of data. If none is found, try the columns. If still none found, prompt user. :param dict d: Table of data :return str _mv: Missing Value """ _mv = "nan" # Attempt to find a table-level missing value key try: # check for missing value key at the table root _mv = d["missingValue"] except KeyError as e: logger_misc.info("get_missing_value: No missing value key found: {}".format(e)) except AttributeError as e: logger_misc.warn("get_missing_value: Column is wrong data type: {}".format(e)) # No table-level missing value found. Attempt to find a column-level missing value key if not _mv: try: # loop for each column of data, searching for a missing value key for k, v in d["columns"].items(): # found a column with a missing value key. Store it and exit the loop. _mv = v["missingValue"] break except KeyError: # There are no columns in this table. We've got bigger problems! pass # No table-level or column-level missing value. Out of places to look. Ask the user to enter the missing value # used in this data # if not _mv: # print("No 'missingValue' key provided. Please type the missingValue used in this file: {}\n".format(filename)) # _mv = input("missingValue: ") return _mv
[ "def", "get_missing_value_key", "(", "d", ")", ":", "_mv", "=", "\"nan\"", "# Attempt to find a table-level missing value key", "try", ":", "# check for missing value key at the table root", "_mv", "=", "d", "[", "\"missingValue\"", "]", "except", "KeyError", "as", "e", ...
Get the Missing Value entry from a table of data. If none is found, try the columns. If still none found, prompt user. :param dict d: Table of data :return str _mv: Missing Value
[ "Get", "the", "Missing", "Value", "entry", "from", "a", "table", "of", "data", ".", "If", "none", "is", "found", "try", "the", "columns", ".", "If", "still", "none", "found", "prompt", "user", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L302-L338
nickmckay/LiPD-utilities
Python/lipd/misc.py
get_variable_name_col
def get_variable_name_col(d): """ Get the variable name from a table or column :param dict d: Metadata (column) :return str var: Variable name """ var = "" try: var = d["variableName"] except KeyError: try: var = d["name"] except KeyError: num = "unknown" if "number" in d: num = d["number"] print("Error: column number <{}> is missing a variableName. Please fix.".format(num)) logger_misc.info("get_variable_name_col: KeyError: missing key") return var
python
def get_variable_name_col(d): """ Get the variable name from a table or column :param dict d: Metadata (column) :return str var: Variable name """ var = "" try: var = d["variableName"] except KeyError: try: var = d["name"] except KeyError: num = "unknown" if "number" in d: num = d["number"] print("Error: column number <{}> is missing a variableName. Please fix.".format(num)) logger_misc.info("get_variable_name_col: KeyError: missing key") return var
[ "def", "get_variable_name_col", "(", "d", ")", ":", "var", "=", "\"\"", "try", ":", "var", "=", "d", "[", "\"variableName\"", "]", "except", "KeyError", ":", "try", ":", "var", "=", "d", "[", "\"name\"", "]", "except", "KeyError", ":", "num", "=", "\...
Get the variable name from a table or column :param dict d: Metadata (column) :return str var: Variable name
[ "Get", "the", "variable", "name", "from", "a", "table", "or", "column" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L341-L360
nickmckay/LiPD-utilities
Python/lipd/misc.py
get_table_key
def get_table_key(key, d, fallback=""): """ Try to get a table name from a data table :param str key: Key to try first :param dict d: Data table :param str fallback: (optional) If we don't find a table name, use this as a generic name fallback. :return str var: Data table name """ try: var = d[key] return var except KeyError: logger_misc.info("get_variable_name_table: KeyError: missing {}, use name: {}".format(key, fallback)) return fallback
python
def get_table_key(key, d, fallback=""): """ Try to get a table name from a data table :param str key: Key to try first :param dict d: Data table :param str fallback: (optional) If we don't find a table name, use this as a generic name fallback. :return str var: Data table name """ try: var = d[key] return var except KeyError: logger_misc.info("get_variable_name_table: KeyError: missing {}, use name: {}".format(key, fallback)) return fallback
[ "def", "get_table_key", "(", "key", ",", "d", ",", "fallback", "=", "\"\"", ")", ":", "try", ":", "var", "=", "d", "[", "key", "]", "return", "var", "except", "KeyError", ":", "logger_misc", ".", "info", "(", "\"get_variable_name_table: KeyError: missing {},...
Try to get a table name from a data table :param str key: Key to try first :param dict d: Data table :param str fallback: (optional) If we don't find a table name, use this as a generic name fallback. :return str var: Data table name
[ "Try", "to", "get", "a", "table", "name", "from", "a", "data", "table" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L363-L377
nickmckay/LiPD-utilities
Python/lipd/misc.py
is_ensemble
def is_ensemble(d): """ Check if a table of data is an ensemble table. Is the first values index a list? ensemble. Int/float? not ensemble. :param dict d: Table data :return bool: Ensemble or not ensemble """ for var, data in d.items(): try: if isinstance(data["number"], list): return True except Exception as e: logger_misc.debug("misc: is_ensemble: {}".format(e)) return False
python
def is_ensemble(d): """ Check if a table of data is an ensemble table. Is the first values index a list? ensemble. Int/float? not ensemble. :param dict d: Table data :return bool: Ensemble or not ensemble """ for var, data in d.items(): try: if isinstance(data["number"], list): return True except Exception as e: logger_misc.debug("misc: is_ensemble: {}".format(e)) return False
[ "def", "is_ensemble", "(", "d", ")", ":", "for", "var", ",", "data", "in", "d", ".", "items", "(", ")", ":", "try", ":", "if", "isinstance", "(", "data", "[", "\"number\"", "]", ",", "list", ")", ":", "return", "True", "except", "Exception", "as", ...
Check if a table of data is an ensemble table. Is the first values index a list? ensemble. Int/float? not ensemble. :param dict d: Table data :return bool: Ensemble or not ensemble
[ "Check", "if", "a", "table", "of", "data", "is", "an", "ensemble", "table", ".", "Is", "the", "first", "values", "index", "a", "list?", "ensemble", ".", "Int", "/", "float?", "not", "ensemble", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L380-L393
nickmckay/LiPD-utilities
Python/lipd/misc.py
load_fn_matches_ext
def load_fn_matches_ext(file_path, file_type): """ Check that the file extension matches the target extension given. :param str file_path: Path to be checked :param str file_type: Target extension :return bool correct_ext: Extension match or does not match """ correct_ext = False curr_ext = os.path.splitext(file_path)[1] exts = [curr_ext, file_type] try: # special case: if file type is excel, both extensions are valid. if ".xlsx" in exts and ".xls" in exts: correct_ext = True elif curr_ext == file_type: correct_ext = True else: print("Use '{}' to load this file: {}".format(FILE_TYPE_MAP[curr_ext]["load_fn"], os.path.basename(file_path))) except Exception as e: logger_misc.debug("load_fn_matches_ext: {}".format(e)) return correct_ext
python
def load_fn_matches_ext(file_path, file_type): """ Check that the file extension matches the target extension given. :param str file_path: Path to be checked :param str file_type: Target extension :return bool correct_ext: Extension match or does not match """ correct_ext = False curr_ext = os.path.splitext(file_path)[1] exts = [curr_ext, file_type] try: # special case: if file type is excel, both extensions are valid. if ".xlsx" in exts and ".xls" in exts: correct_ext = True elif curr_ext == file_type: correct_ext = True else: print("Use '{}' to load this file: {}".format(FILE_TYPE_MAP[curr_ext]["load_fn"], os.path.basename(file_path))) except Exception as e: logger_misc.debug("load_fn_matches_ext: {}".format(e)) return correct_ext
[ "def", "load_fn_matches_ext", "(", "file_path", ",", "file_type", ")", ":", "correct_ext", "=", "False", "curr_ext", "=", "os", ".", "path", ".", "splitext", "(", "file_path", ")", "[", "1", "]", "exts", "=", "[", "curr_ext", ",", "file_type", "]", "try"...
Check that the file extension matches the target extension given. :param str file_path: Path to be checked :param str file_type: Target extension :return bool correct_ext: Extension match or does not match
[ "Check", "that", "the", "file", "extension", "matches", "the", "target", "extension", "given", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L396-L419
nickmckay/LiPD-utilities
Python/lipd/misc.py
match_operators
def match_operators(inp, relate, cut): """ Compare two items. Match a string operator to an operator function :param str inp: Comparison item :param str relate: Comparison operator :param any cut: Comparison item :return bool truth: Comparison truth """ logger_misc.info("enter match_operators") ops = {'>': operator.gt, '<': operator.lt, '>=': operator.ge, '<=': operator.le, '=': operator.eq } try: truth = ops[relate](inp, cut) except KeyError as e: truth = False logger_misc.warn("get_truth: KeyError: Invalid operator input: {}, {}".format(relate, e)) logger_misc.info("exit match_operators") return truth
python
def match_operators(inp, relate, cut): """ Compare two items. Match a string operator to an operator function :param str inp: Comparison item :param str relate: Comparison operator :param any cut: Comparison item :return bool truth: Comparison truth """ logger_misc.info("enter match_operators") ops = {'>': operator.gt, '<': operator.lt, '>=': operator.ge, '<=': operator.le, '=': operator.eq } try: truth = ops[relate](inp, cut) except KeyError as e: truth = False logger_misc.warn("get_truth: KeyError: Invalid operator input: {}, {}".format(relate, e)) logger_misc.info("exit match_operators") return truth
[ "def", "match_operators", "(", "inp", ",", "relate", ",", "cut", ")", ":", "logger_misc", ".", "info", "(", "\"enter match_operators\"", ")", "ops", "=", "{", "'>'", ":", "operator", ".", "gt", ",", "'<'", ":", "operator", ".", "lt", ",", "'>='", ":", ...
Compare two items. Match a string operator to an operator function :param str inp: Comparison item :param str relate: Comparison operator :param any cut: Comparison item :return bool truth: Comparison truth
[ "Compare", "two", "items", ".", "Match", "a", "string", "operator", "to", "an", "operator", "function" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L422-L444
nickmckay/LiPD-utilities
Python/lipd/misc.py
match_arr_lengths
def match_arr_lengths(l): """ Check that all the array lengths match so that a DataFrame can be created successfully. :param list l: Nested arrays :return bool: Valid or invalid """ try: # length of first list. use as basis to check other list lengths against. inner_len = len(l[0]) # check each nested list for i in l: # if the length doesn't match the first list, then don't proceed. if len(i) != inner_len: return False except IndexError: # couldn't get index 0. Wrong data type given or not nested lists print("Error: Array data is not formatted correctly.") return False except TypeError: # Non-iterable data type given. print("Error: Array data missing") return False # all array lengths are equal. made it through the whole list successfully return True
python
def match_arr_lengths(l): """ Check that all the array lengths match so that a DataFrame can be created successfully. :param list l: Nested arrays :return bool: Valid or invalid """ try: # length of first list. use as basis to check other list lengths against. inner_len = len(l[0]) # check each nested list for i in l: # if the length doesn't match the first list, then don't proceed. if len(i) != inner_len: return False except IndexError: # couldn't get index 0. Wrong data type given or not nested lists print("Error: Array data is not formatted correctly.") return False except TypeError: # Non-iterable data type given. print("Error: Array data missing") return False # all array lengths are equal. made it through the whole list successfully return True
[ "def", "match_arr_lengths", "(", "l", ")", ":", "try", ":", "# length of first list. use as basis to check other list lengths against.", "inner_len", "=", "len", "(", "l", "[", "0", "]", ")", "# check each nested list", "for", "i", "in", "l", ":", "# if the length doe...
Check that all the array lengths match so that a DataFrame can be created successfully. :param list l: Nested arrays :return bool: Valid or invalid
[ "Check", "that", "all", "the", "array", "lengths", "match", "so", "that", "a", "DataFrame", "can", "be", "created", "successfully", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L447-L471
nickmckay/LiPD-utilities
Python/lipd/misc.py
mv_files
def mv_files(src, dst): """ Move all files from one directory to another :param str src: Source directory :param str dst: Destination directory :return none: """ # list the files in the src directory files = os.listdir(src) # loop for each file found for file in files: # move the file from the src to the dst shutil.move(os.path.join(src, file), os.path.join(dst, file)) return
python
def mv_files(src, dst): """ Move all files from one directory to another :param str src: Source directory :param str dst: Destination directory :return none: """ # list the files in the src directory files = os.listdir(src) # loop for each file found for file in files: # move the file from the src to the dst shutil.move(os.path.join(src, file), os.path.join(dst, file)) return
[ "def", "mv_files", "(", "src", ",", "dst", ")", ":", "# list the files in the src directory", "files", "=", "os", ".", "listdir", "(", "src", ")", "# loop for each file found", "for", "file", "in", "files", ":", "# move the file from the src to the dst", "shutil", "...
Move all files from one directory to another :param str src: Source directory :param str dst: Destination directory :return none:
[ "Move", "all", "files", "from", "one", "directory", "to", "another" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L474-L488
nickmckay/LiPD-utilities
Python/lipd/misc.py
normalize_name
def normalize_name(s): """ Remove foreign accents and characters to normalize the string. Prevents encoding errors. :param str s: String :return str s: String """ # Normalize the string into a byte string form s = unicodedata.normalize('NFKD', s).encode('ascii', 'ignore') # Remove the byte string and quotes from the string s = str(s)[2:-1] return s
python
def normalize_name(s): """ Remove foreign accents and characters to normalize the string. Prevents encoding errors. :param str s: String :return str s: String """ # Normalize the string into a byte string form s = unicodedata.normalize('NFKD', s).encode('ascii', 'ignore') # Remove the byte string and quotes from the string s = str(s)[2:-1] return s
[ "def", "normalize_name", "(", "s", ")", ":", "# Normalize the string into a byte string form", "s", "=", "unicodedata", ".", "normalize", "(", "'NFKD'", ",", "s", ")", ".", "encode", "(", "'ascii'", ",", "'ignore'", ")", "# Remove the byte string and quotes from the s...
Remove foreign accents and characters to normalize the string. Prevents encoding errors. :param str s: String :return str s: String
[ "Remove", "foreign", "accents", "and", "characters", "to", "normalize", "the", "string", ".", "Prevents", "encoding", "errors", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L491-L502
nickmckay/LiPD-utilities
Python/lipd/misc.py
path_type
def path_type(path, target): """ Determine if given path is file, directory, or other. Compare with target to see if it's the type we wanted. :param str path: Path :param str target: Target type wanted :return bool: Path is what it claims to be (True) or mismatch (False) """ if os.path.isfile(path) and target == "file": return True elif os.path.isdir(path) and target == "directory": return True else: print("Error: Path given is not a {}: {}".format(target, path)) return False
python
def path_type(path, target): """ Determine if given path is file, directory, or other. Compare with target to see if it's the type we wanted. :param str path: Path :param str target: Target type wanted :return bool: Path is what it claims to be (True) or mismatch (False) """ if os.path.isfile(path) and target == "file": return True elif os.path.isdir(path) and target == "directory": return True else: print("Error: Path given is not a {}: {}".format(target, path)) return False
[ "def", "path_type", "(", "path", ",", "target", ")", ":", "if", "os", ".", "path", ".", "isfile", "(", "path", ")", "and", "target", "==", "\"file\"", ":", "return", "True", "elif", "os", ".", "path", ".", "isdir", "(", "path", ")", "and", "target"...
Determine if given path is file, directory, or other. Compare with target to see if it's the type we wanted. :param str path: Path :param str target: Target type wanted :return bool: Path is what it claims to be (True) or mismatch (False)
[ "Determine", "if", "given", "path", "is", "file", "directory", "or", "other", ".", "Compare", "with", "target", "to", "see", "if", "it", "s", "the", "type", "we", "wanted", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L505-L519
nickmckay/LiPD-utilities
Python/lipd/misc.py
print_filename
def print_filename(path): """ Print out lipd filename that is being read or written :param str path: all file metadata :return str: filename """ if os.path.basename(path): return os.path.basename(path) return path
python
def print_filename(path): """ Print out lipd filename that is being read or written :param str path: all file metadata :return str: filename """ if os.path.basename(path): return os.path.basename(path) return path
[ "def", "print_filename", "(", "path", ")", ":", "if", "os", ".", "path", ".", "basename", "(", "path", ")", ":", "return", "os", ".", "path", ".", "basename", "(", "path", ")", "return", "path" ]
Print out lipd filename that is being read or written :param str path: all file metadata :return str: filename
[ "Print", "out", "lipd", "filename", "that", "is", "being", "read", "or", "written" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L522-L532
nickmckay/LiPD-utilities
Python/lipd/misc.py
prompt_protocol
def prompt_protocol(): """ Prompt user if they would like to save pickle file as a dictionary or an object. :return str: Answer """ stop = 3 ans = "" while True and stop > 0: ans = input("Save as (d)ictionary or (o)bject?\n" "* Note:\n" "Dictionaries are more basic, and are compatible with Python v2.7+.\n" "Objects are more complex, and are only compatible with v3.4+ ") if ans not in ("d", "o"): print("Invalid response: Please choose 'd' or 'o'") else: break # if a valid answer isn't captured, default to dictionary (safer, broader) if ans == "": ans = "d" return ans
python
def prompt_protocol(): """ Prompt user if they would like to save pickle file as a dictionary or an object. :return str: Answer """ stop = 3 ans = "" while True and stop > 0: ans = input("Save as (d)ictionary or (o)bject?\n" "* Note:\n" "Dictionaries are more basic, and are compatible with Python v2.7+.\n" "Objects are more complex, and are only compatible with v3.4+ ") if ans not in ("d", "o"): print("Invalid response: Please choose 'd' or 'o'") else: break # if a valid answer isn't captured, default to dictionary (safer, broader) if ans == "": ans = "d" return ans
[ "def", "prompt_protocol", "(", ")", ":", "stop", "=", "3", "ans", "=", "\"\"", "while", "True", "and", "stop", ">", "0", ":", "ans", "=", "input", "(", "\"Save as (d)ictionary or (o)bject?\\n\"", "\"* Note:\\n\"", "\"Dictionaries are more basic, and are compatible wit...
Prompt user if they would like to save pickle file as a dictionary or an object. :return str: Answer
[ "Prompt", "user", "if", "they", "would", "like", "to", "save", "pickle", "file", "as", "a", "dictionary", "or", "an", "object", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L534-L554
nickmckay/LiPD-utilities
Python/lipd/misc.py
put_tsids
def put_tsids(x): """ (Recursive) Add in TSids into any columns that do not have them. Look for "columns" keys, and then start looping and adding generated TSids to each column :param any x: Unknown :return any x: Unknown """ try: if isinstance(x, dict): try: for k, v in x.items(): # Is this the columns key? if k == "columns": try: # loop over each column of data. Sorted by variableName key for var, data in v.items(): try: # make a case-insensitive keys list for checking existence of "tsid" keys = [key.lower() for key in data.keys()] # If a TSid already exists, then we don't need to do anything. if "tsid" not in keys: # generate the TSid, and add it to the dictionary data["TSid"] = generate_tsid() logger_misc.info("put_tsids: Generated new TSid: {}".format(data["TSid"])) except AttributeError as e: logger_misc.debug("put_tsids: level 3: AttributeError: {}".format(e)) except Exception as e: logger_misc.debug("put_tsids: level 3: Exception: {}".format(e)) except Exception as e: print("put_tsids: level 2: Exception: {}, {}".format(k, e)) # If it's not "columns", then dive deeper. else: x[k] = put_tsids(v) except Exception as e: print("put_tsids: level 1: Exception: {}, {}".format(k, e)) # Item is a list, dive deeper for each item in the list elif isinstance(x, list): for idx, entry in enumerate(x): x[idx] = put_tsids(entry) except Exception as e: print("put_tsids: root: Exception: {}, {}".format(k, e)) return x
python
def put_tsids(x): """ (Recursive) Add in TSids into any columns that do not have them. Look for "columns" keys, and then start looping and adding generated TSids to each column :param any x: Unknown :return any x: Unknown """ try: if isinstance(x, dict): try: for k, v in x.items(): # Is this the columns key? if k == "columns": try: # loop over each column of data. Sorted by variableName key for var, data in v.items(): try: # make a case-insensitive keys list for checking existence of "tsid" keys = [key.lower() for key in data.keys()] # If a TSid already exists, then we don't need to do anything. if "tsid" not in keys: # generate the TSid, and add it to the dictionary data["TSid"] = generate_tsid() logger_misc.info("put_tsids: Generated new TSid: {}".format(data["TSid"])) except AttributeError as e: logger_misc.debug("put_tsids: level 3: AttributeError: {}".format(e)) except Exception as e: logger_misc.debug("put_tsids: level 3: Exception: {}".format(e)) except Exception as e: print("put_tsids: level 2: Exception: {}, {}".format(k, e)) # If it's not "columns", then dive deeper. else: x[k] = put_tsids(v) except Exception as e: print("put_tsids: level 1: Exception: {}, {}".format(k, e)) # Item is a list, dive deeper for each item in the list elif isinstance(x, list): for idx, entry in enumerate(x): x[idx] = put_tsids(entry) except Exception as e: print("put_tsids: root: Exception: {}, {}".format(k, e)) return x
[ "def", "put_tsids", "(", "x", ")", ":", "try", ":", "if", "isinstance", "(", "x", ",", "dict", ")", ":", "try", ":", "for", "k", ",", "v", "in", "x", ".", "items", "(", ")", ":", "# Is this the columns key?", "if", "k", "==", "\"columns\"", ":", ...
(Recursive) Add in TSids into any columns that do not have them. Look for "columns" keys, and then start looping and adding generated TSids to each column :param any x: Unknown :return any x: Unknown
[ "(", "Recursive", ")", "Add", "in", "TSids", "into", "any", "columns", "that", "do", "not", "have", "them", ".", "Look", "for", "columns", "keys", "and", "then", "start", "looping", "and", "adding", "generated", "TSids", "to", "each", "column" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L557-L599
nickmckay/LiPD-utilities
Python/lipd/misc.py
rm_empty_fields
def rm_empty_fields(x): """ (Recursive) Go through N number of nested data types and remove all empty entries. :param any x: Unknown :return any x: Unknown """ # No logger here because the function is recursive. # Int types don't matter. Return as-is. if not isinstance(x, int) and not isinstance(x, float): if isinstance(x, str) or x is None: try: # Remove new line characters and carriage returns x = x.rstrip() except AttributeError: # None types don't matter. Keep going. pass if x in EMPTY: # Substitute empty entries with "" x = '' elif isinstance(x, list): # Recurse once for each item in the list for i, v in enumerate(x): x[i] = rm_empty_fields(x[i]) # After substitutions, remove empty entries. for i in x: # Many 0 values are important (coordinates, m/m/m/m). Don't remove them. if not i and i not in [0, 0.0]: x.remove(i) elif isinstance(x, dict): # First, go through and substitute "" (empty string) entry for any values in EMPTY for k, v in x.items(): x[k] = rm_empty_fields(v) # After substitutions, go through and delete the key-value pair. # This has to be done after we come back up from recursion because we cannot pass keys down. for key in list(x.keys()): if not x[key] and x[key] not in [0, 0.0]: del x[key] return x
python
def rm_empty_fields(x): """ (Recursive) Go through N number of nested data types and remove all empty entries. :param any x: Unknown :return any x: Unknown """ # No logger here because the function is recursive. # Int types don't matter. Return as-is. if not isinstance(x, int) and not isinstance(x, float): if isinstance(x, str) or x is None: try: # Remove new line characters and carriage returns x = x.rstrip() except AttributeError: # None types don't matter. Keep going. pass if x in EMPTY: # Substitute empty entries with "" x = '' elif isinstance(x, list): # Recurse once for each item in the list for i, v in enumerate(x): x[i] = rm_empty_fields(x[i]) # After substitutions, remove empty entries. for i in x: # Many 0 values are important (coordinates, m/m/m/m). Don't remove them. if not i and i not in [0, 0.0]: x.remove(i) elif isinstance(x, dict): # First, go through and substitute "" (empty string) entry for any values in EMPTY for k, v in x.items(): x[k] = rm_empty_fields(v) # After substitutions, go through and delete the key-value pair. # This has to be done after we come back up from recursion because we cannot pass keys down. for key in list(x.keys()): if not x[key] and x[key] not in [0, 0.0]: del x[key] return x
[ "def", "rm_empty_fields", "(", "x", ")", ":", "# No logger here because the function is recursive.", "# Int types don't matter. Return as-is.", "if", "not", "isinstance", "(", "x", ",", "int", ")", "and", "not", "isinstance", "(", "x", ",", "float", ")", ":", "if", ...
(Recursive) Go through N number of nested data types and remove all empty entries. :param any x: Unknown :return any x: Unknown
[ "(", "Recursive", ")", "Go", "through", "N", "number", "of", "nested", "data", "types", "and", "remove", "all", "empty", "entries", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L602-L640
nickmckay/LiPD-utilities
Python/lipd/misc.py
rm_empty_doi
def rm_empty_doi(d): """ If an "identifier" dictionary has no doi ID, then it has no use. Delete it. :param dict d: Metadata :return dict d: Metadata """ logger_misc.info("enter remove_empty_doi") try: # Check each publication dictionary for pub in d['pub']: # If no identifier, then we can quit here. If identifier, then keep going. if 'identifier' in pub: if 'id' in pub['identifier'][0]: # If there's a DOI id, but it's EMPTY if pub['identifier'][0]['id'] in EMPTY: del pub['identifier'] else: # If there's an identifier section, with no DOI id del pub['identifier'] except KeyError as e: # What else could go wrong? logger_misc.warn("remove_empty_doi: KeyError: publication key not found, {}".format(e)) logger_misc.info("exit remove_empty_doi") return d
python
def rm_empty_doi(d): """ If an "identifier" dictionary has no doi ID, then it has no use. Delete it. :param dict d: Metadata :return dict d: Metadata """ logger_misc.info("enter remove_empty_doi") try: # Check each publication dictionary for pub in d['pub']: # If no identifier, then we can quit here. If identifier, then keep going. if 'identifier' in pub: if 'id' in pub['identifier'][0]: # If there's a DOI id, but it's EMPTY if pub['identifier'][0]['id'] in EMPTY: del pub['identifier'] else: # If there's an identifier section, with no DOI id del pub['identifier'] except KeyError as e: # What else could go wrong? logger_misc.warn("remove_empty_doi: KeyError: publication key not found, {}".format(e)) logger_misc.info("exit remove_empty_doi") return d
[ "def", "rm_empty_doi", "(", "d", ")", ":", "logger_misc", ".", "info", "(", "\"enter remove_empty_doi\"", ")", "try", ":", "# Check each publication dictionary", "for", "pub", "in", "d", "[", "'pub'", "]", ":", "# If no identifier, then we can quit here. If identifier, ...
If an "identifier" dictionary has no doi ID, then it has no use. Delete it. :param dict d: Metadata :return dict d: Metadata
[ "If", "an", "identifier", "dictionary", "has", "no", "doi", "ID", "then", "it", "has", "no", "use", ".", "Delete", "it", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L643-L667
nickmckay/LiPD-utilities
Python/lipd/misc.py
rm_files
def rm_files(path, extension): """ Remove all files in the given directory with the given extension :param str path: Directory :param str extension: File type to remove :return none: """ files = list_files(extension, path) for file in files: if file.endswith(extension): os.remove(os.path.join(path, file)) return
python
def rm_files(path, extension): """ Remove all files in the given directory with the given extension :param str path: Directory :param str extension: File type to remove :return none: """ files = list_files(extension, path) for file in files: if file.endswith(extension): os.remove(os.path.join(path, file)) return
[ "def", "rm_files", "(", "path", ",", "extension", ")", ":", "files", "=", "list_files", "(", "extension", ",", "path", ")", "for", "file", "in", "files", ":", "if", "file", ".", "endswith", "(", "extension", ")", ":", "os", ".", "remove", "(", "os", ...
Remove all files in the given directory with the given extension :param str path: Directory :param str extension: File type to remove :return none:
[ "Remove", "all", "files", "in", "the", "given", "directory", "with", "the", "given", "extension" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L670-L682
nickmckay/LiPD-utilities
Python/lipd/misc.py
rm_values_fields
def rm_values_fields(x): """ (Recursive) Remove all "values" fields from the metadata :param any x: Any data type :return dict x: Metadata (values removed) """ if isinstance(x, dict): if "values" in x: del x["values"] else: for k, v in x.items(): if isinstance(v, dict): rm_values_fields(v) elif isinstance(v, list): rm_values_fields(v) elif isinstance(x, list): for i in x: rm_values_fields(i) return x
python
def rm_values_fields(x): """ (Recursive) Remove all "values" fields from the metadata :param any x: Any data type :return dict x: Metadata (values removed) """ if isinstance(x, dict): if "values" in x: del x["values"] else: for k, v in x.items(): if isinstance(v, dict): rm_values_fields(v) elif isinstance(v, list): rm_values_fields(v) elif isinstance(x, list): for i in x: rm_values_fields(i) return x
[ "def", "rm_values_fields", "(", "x", ")", ":", "if", "isinstance", "(", "x", ",", "dict", ")", ":", "if", "\"values\"", "in", "x", ":", "del", "x", "[", "\"values\"", "]", "else", ":", "for", "k", ",", "v", "in", "x", ".", "items", "(", ")", ":...
(Recursive) Remove all "values" fields from the metadata :param any x: Any data type :return dict x: Metadata (values removed)
[ "(", "Recursive", ")", "Remove", "all", "values", "fields", "from", "the", "metadata" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L685-L705
nickmckay/LiPD-utilities
Python/lipd/misc.py
rm_missing_values_table
def rm_missing_values_table(d): """ Loop for each table column and remove the missingValue key & data :param dict d: Metadata (table) :return dict d: Metadata (table) """ try: for k, v in d["columns"].items(): d["columns"][k] = rm_keys_from_dict(v, ["missingValue"]) except Exception: # If we get a KeyError or some other error, it's not a big deal. Keep going. pass return d
python
def rm_missing_values_table(d): """ Loop for each table column and remove the missingValue key & data :param dict d: Metadata (table) :return dict d: Metadata (table) """ try: for k, v in d["columns"].items(): d["columns"][k] = rm_keys_from_dict(v, ["missingValue"]) except Exception: # If we get a KeyError or some other error, it's not a big deal. Keep going. pass return d
[ "def", "rm_missing_values_table", "(", "d", ")", ":", "try", ":", "for", "k", ",", "v", "in", "d", "[", "\"columns\"", "]", ".", "items", "(", ")", ":", "d", "[", "\"columns\"", "]", "[", "k", "]", "=", "rm_keys_from_dict", "(", "v", ",", "[", "\...
Loop for each table column and remove the missingValue key & data :param dict d: Metadata (table) :return dict d: Metadata (table)
[ "Loop", "for", "each", "table", "column", "and", "remove", "the", "missingValue", "key", "&", "data" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L708-L721
nickmckay/LiPD-utilities
Python/lipd/misc.py
rm_keys_from_dict
def rm_keys_from_dict(d, keys): """ Given a dictionary and a key list, remove any data in the dictionary with the given keys. :param dict d: Metadata :param list keys: Keys to be removed :return dict d: Metadata """ # Loop for each key given for key in keys: # Is the key in the dictionary? if key in d: try: d.pop(key, None) except KeyError: # Not concerned with an error. Keep going. pass return d
python
def rm_keys_from_dict(d, keys): """ Given a dictionary and a key list, remove any data in the dictionary with the given keys. :param dict d: Metadata :param list keys: Keys to be removed :return dict d: Metadata """ # Loop for each key given for key in keys: # Is the key in the dictionary? if key in d: try: d.pop(key, None) except KeyError: # Not concerned with an error. Keep going. pass return d
[ "def", "rm_keys_from_dict", "(", "d", ",", "keys", ")", ":", "# Loop for each key given", "for", "key", "in", "keys", ":", "# Is the key in the dictionary?", "if", "key", "in", "d", ":", "try", ":", "d", ".", "pop", "(", "key", ",", "None", ")", "except", ...
Given a dictionary and a key list, remove any data in the dictionary with the given keys. :param dict d: Metadata :param list keys: Keys to be removed :return dict d: Metadata
[ "Given", "a", "dictionary", "and", "a", "key", "list", "remove", "any", "data", "in", "the", "dictionary", "with", "the", "given", "keys", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L724-L741
nickmckay/LiPD-utilities
Python/lipd/misc.py
_replace_missing_values_table
def _replace_missing_values_table(values, mv): """ Receive all table column values as a list of lists. Loop for each column of values :param list values: Metadata (columns) :param any mv: Missing value currently in use :return list: Metadata (columns) """ for idx, column in enumerate(values): values[idx] = _replace_missing_values_column(column, mv) return values
python
def _replace_missing_values_table(values, mv): """ Receive all table column values as a list of lists. Loop for each column of values :param list values: Metadata (columns) :param any mv: Missing value currently in use :return list: Metadata (columns) """ for idx, column in enumerate(values): values[idx] = _replace_missing_values_column(column, mv) return values
[ "def", "_replace_missing_values_table", "(", "values", ",", "mv", ")", ":", "for", "idx", ",", "column", "in", "enumerate", "(", "values", ")", ":", "values", "[", "idx", "]", "=", "_replace_missing_values_column", "(", "column", ",", "mv", ")", "return", ...
Receive all table column values as a list of lists. Loop for each column of values :param list values: Metadata (columns) :param any mv: Missing value currently in use :return list: Metadata (columns)
[ "Receive", "all", "table", "column", "values", "as", "a", "list", "of", "lists", ".", "Loop", "for", "each", "column", "of", "values" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L781-L793
nickmckay/LiPD-utilities
Python/lipd/misc.py
_replace_missing_values_column
def _replace_missing_values_column(values, mv): """ Replace missing values in the values list where applicable :param list values: Metadata (column values) :return list values: Metadata (column values) """ for idx, v in enumerate(values): try: if v in EMPTY or v == mv: values[idx] = "nan" elif math.isnan(float(v)): values[idx] = "nan" else: values[idx] = v except (TypeError, ValueError): values[idx] = v return values
python
def _replace_missing_values_column(values, mv): """ Replace missing values in the values list where applicable :param list values: Metadata (column values) :return list values: Metadata (column values) """ for idx, v in enumerate(values): try: if v in EMPTY or v == mv: values[idx] = "nan" elif math.isnan(float(v)): values[idx] = "nan" else: values[idx] = v except (TypeError, ValueError): values[idx] = v return values
[ "def", "_replace_missing_values_column", "(", "values", ",", "mv", ")", ":", "for", "idx", ",", "v", "in", "enumerate", "(", "values", ")", ":", "try", ":", "if", "v", "in", "EMPTY", "or", "v", "==", "mv", ":", "values", "[", "idx", "]", "=", "\"na...
Replace missing values in the values list where applicable :param list values: Metadata (column values) :return list values: Metadata (column values)
[ "Replace", "missing", "values", "in", "the", "values", "list", "where", "applicable" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L796-L814
nickmckay/LiPD-utilities
Python/lipd/misc.py
split_path_and_file
def split_path_and_file(s): """ Given a full path to a file, split and return a path and filename :param str s: Path :return str _path: Directory Path :return str _filename: Filename """ _path = s _filename = "" try: x = os.path.split(s) _path = x[0] _filename = x[1] except Exception: print("Error: unable to split path") return _path, _filename
python
def split_path_and_file(s): """ Given a full path to a file, split and return a path and filename :param str s: Path :return str _path: Directory Path :return str _filename: Filename """ _path = s _filename = "" try: x = os.path.split(s) _path = x[0] _filename = x[1] except Exception: print("Error: unable to split path") return _path, _filename
[ "def", "split_path_and_file", "(", "s", ")", ":", "_path", "=", "s", "_filename", "=", "\"\"", "try", ":", "x", "=", "os", ".", "path", ".", "split", "(", "s", ")", "_path", "=", "x", "[", "0", "]", "_filename", "=", "x", "[", "1", "]", "except...
Given a full path to a file, split and return a path and filename :param str s: Path :return str _path: Directory Path :return str _filename: Filename
[ "Given", "a", "full", "path", "to", "a", "file", "split", "and", "return", "a", "path", "and", "filename" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L817-L834
nickmckay/LiPD-utilities
Python/lipd/misc.py
unwrap_arrays
def unwrap_arrays(l): """ Unwrap nested lists to be one "flat" list of lists. Mainly for prepping ensemble data for DataFrame() creation :param list l: Nested lists :return list l2: Flattened lists """ # keep processing until all nesting is removed process = True # fail safe: cap the loops at 20, so we don't run into an error and loop infinitely. # if it takes more than 20 loops then there is a problem with the data given. loops = 25 while process and loops > 0: try: # new "flat" list l2 = [] for k in l: # all items in this list are numeric, so this list is done. append to main list if all(isinstance(i, float) or isinstance(i, int) for i in k): l2.append(k) # this list has more nested lists inside. append each individual nested list to the main one. elif all(isinstance(i, list) or isinstance(i, np.ndarray) for i in k): for i in k: l2.append(i) except Exception: print("something went wrong during process") # verify the main list try: # if every list has a numeric at index 0, then there is no more nesting and we can stop processing if all(isinstance(i[0], (int, str, float)) for i in l2): process = False else: l = l2 except IndexError: # there's no index 0, so there must be mixed data types or empty data somewhere. print("something went wrong during verify") loops -= 1 return l2
python
def unwrap_arrays(l): """ Unwrap nested lists to be one "flat" list of lists. Mainly for prepping ensemble data for DataFrame() creation :param list l: Nested lists :return list l2: Flattened lists """ # keep processing until all nesting is removed process = True # fail safe: cap the loops at 20, so we don't run into an error and loop infinitely. # if it takes more than 20 loops then there is a problem with the data given. loops = 25 while process and loops > 0: try: # new "flat" list l2 = [] for k in l: # all items in this list are numeric, so this list is done. append to main list if all(isinstance(i, float) or isinstance(i, int) for i in k): l2.append(k) # this list has more nested lists inside. append each individual nested list to the main one. elif all(isinstance(i, list) or isinstance(i, np.ndarray) for i in k): for i in k: l2.append(i) except Exception: print("something went wrong during process") # verify the main list try: # if every list has a numeric at index 0, then there is no more nesting and we can stop processing if all(isinstance(i[0], (int, str, float)) for i in l2): process = False else: l = l2 except IndexError: # there's no index 0, so there must be mixed data types or empty data somewhere. print("something went wrong during verify") loops -= 1 return l2
[ "def", "unwrap_arrays", "(", "l", ")", ":", "# keep processing until all nesting is removed", "process", "=", "True", "# fail safe: cap the loops at 20, so we don't run into an error and loop infinitely.", "# if it takes more than 20 loops then there is a problem with the data given.", "loop...
Unwrap nested lists to be one "flat" list of lists. Mainly for prepping ensemble data for DataFrame() creation :param list l: Nested lists :return list l2: Flattened lists
[ "Unwrap", "nested", "lists", "to", "be", "one", "flat", "list", "of", "lists", ".", "Mainly", "for", "prepping", "ensemble", "data", "for", "DataFrame", "()", "creation" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/misc.py#L837-L874
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
extract
def extract(d, whichtables, mode, time): """ LiPD Version 1.3 Main function to initiate LiPD to TSOs conversion. Each object has a "paleoNumber" or "chronNumber" "tableNumber" "modelNumber" "time_id" "mode" - chronData or paleoData "tableType" - "meas" "ens" "summ" :param dict d: Metadata for one LiPD file :param str whichtables: all, meas, summ, or ens :param str mode: paleo or chron mode :return list _ts: Time series """ logger_ts.info("enter extract_main") _root = {} _ts = {} # _switch = {"paleoData": "chronData", "chronData": "paleoData"} _pc = "paleoData" if mode == "chron": _pc = "chronData" _root["mode"] = _pc _root["time_id"] = time try: # Build the root level data. # This will serve as the template for which column data will be added onto later. for k, v in d.items(): if k == "funding": _root = _extract_fund(v, _root) elif k == "geo": _root = _extract_geo(v, _root) elif k == 'pub': _root = _extract_pub(v, _root) # elif k in ["chronData", "paleoData"]: # # Store chronData and paleoData as-is. Need it to collapse without data loss. # _root[k] = copy.deepcopy(v) else: if k not in ["chronData", "paleoData"]: _root[k] = v # Create tso dictionaries for each individual column (build on root data) _ts = _extract_pc(d, _root, _pc, whichtables) except Exception as e: logger_ts.error("extract: Exception: {}".format(e)) print("extract: Exception: {}".format(e)) logger_ts.info("exit extract_main") return _ts
python
def extract(d, whichtables, mode, time): """ LiPD Version 1.3 Main function to initiate LiPD to TSOs conversion. Each object has a "paleoNumber" or "chronNumber" "tableNumber" "modelNumber" "time_id" "mode" - chronData or paleoData "tableType" - "meas" "ens" "summ" :param dict d: Metadata for one LiPD file :param str whichtables: all, meas, summ, or ens :param str mode: paleo or chron mode :return list _ts: Time series """ logger_ts.info("enter extract_main") _root = {} _ts = {} # _switch = {"paleoData": "chronData", "chronData": "paleoData"} _pc = "paleoData" if mode == "chron": _pc = "chronData" _root["mode"] = _pc _root["time_id"] = time try: # Build the root level data. # This will serve as the template for which column data will be added onto later. for k, v in d.items(): if k == "funding": _root = _extract_fund(v, _root) elif k == "geo": _root = _extract_geo(v, _root) elif k == 'pub': _root = _extract_pub(v, _root) # elif k in ["chronData", "paleoData"]: # # Store chronData and paleoData as-is. Need it to collapse without data loss. # _root[k] = copy.deepcopy(v) else: if k not in ["chronData", "paleoData"]: _root[k] = v # Create tso dictionaries for each individual column (build on root data) _ts = _extract_pc(d, _root, _pc, whichtables) except Exception as e: logger_ts.error("extract: Exception: {}".format(e)) print("extract: Exception: {}".format(e)) logger_ts.info("exit extract_main") return _ts
[ "def", "extract", "(", "d", ",", "whichtables", ",", "mode", ",", "time", ")", ":", "logger_ts", ".", "info", "(", "\"enter extract_main\"", ")", "_root", "=", "{", "}", "_ts", "=", "{", "}", "# _switch = {\"paleoData\": \"chronData\", \"chronData\": \"paleoData\"...
LiPD Version 1.3 Main function to initiate LiPD to TSOs conversion. Each object has a "paleoNumber" or "chronNumber" "tableNumber" "modelNumber" "time_id" "mode" - chronData or paleoData "tableType" - "meas" "ens" "summ" :param dict d: Metadata for one LiPD file :param str whichtables: all, meas, summ, or ens :param str mode: paleo or chron mode :return list _ts: Time series
[ "LiPD", "Version", "1", ".", "3", "Main", "function", "to", "initiate", "LiPD", "to", "TSOs", "conversion", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L15-L66
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_fund
def _extract_fund(l, _root): """ Creates flat funding dictionary. :param list l: Funding entries """ logger_ts.info("enter _extract_funding") for idx, i in enumerate(l): for k, v in i.items(): _root['funding' + str(idx + 1) + '_' + k] = v return _root
python
def _extract_fund(l, _root): """ Creates flat funding dictionary. :param list l: Funding entries """ logger_ts.info("enter _extract_funding") for idx, i in enumerate(l): for k, v in i.items(): _root['funding' + str(idx + 1) + '_' + k] = v return _root
[ "def", "_extract_fund", "(", "l", ",", "_root", ")", ":", "logger_ts", ".", "info", "(", "\"enter _extract_funding\"", ")", "for", "idx", ",", "i", "in", "enumerate", "(", "l", ")", ":", "for", "k", ",", "v", "in", "i", ".", "items", "(", ")", ":",...
Creates flat funding dictionary. :param list l: Funding entries
[ "Creates", "flat", "funding", "dictionary", ".", ":", "param", "list", "l", ":", "Funding", "entries" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L69-L78
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_geo
def _extract_geo(d, _root): """ Extract geo data from input :param dict d: Geo :return dict _root: Root data """ logger_ts.info("enter ts_extract_geo") # May not need these if the key names are corrected in the future. # COORDINATE ORDER: [LON, LAT, ELEV] x = ['geo_meanLon', 'geo_meanLat', 'geo_meanElev'] # Iterate through geo dictionary for k, v in d.items(): # Case 1: Coordinates special naming if k == 'coordinates': for idx, p in enumerate(v): try: # Check that our value is not in EMPTY. if isinstance(p, str): if p.lower() in EMPTY: # If elevation is a string or 0, don't record it if idx != 2: # If long or lat is empty, set it as 0 instead _root[x[idx]] = 0 else: # Set the value as a float into its entry. _root[x[idx]] = float(p) # Value is a normal number, or string representation of a number else: # Set the value as a float into its entry. _root[x[idx]] = float(p) except IndexError as e: logger_ts.warn("_extract_geo: IndexError: idx: {}, val: {}, {}".format(idx, p, e)) # Case 2: Any value that is a string can be added as-is elif isinstance(v, str): if k == 'meanElev': try: # Some data sets have meanElev listed under properties for some reason. _root['geo_' + k] = float(v) except ValueError as e: # If the value is a string, then we don't want it logger_ts.warn("_extract_geo: ValueError: meanElev is a string: {}, {}".format(v, e)) else: _root['geo_' + k] = v # Case 3: Nested dictionary. Recursion elif isinstance(v, dict): _root = _extract_geo(v, _root) return _root
python
def _extract_geo(d, _root): """ Extract geo data from input :param dict d: Geo :return dict _root: Root data """ logger_ts.info("enter ts_extract_geo") # May not need these if the key names are corrected in the future. # COORDINATE ORDER: [LON, LAT, ELEV] x = ['geo_meanLon', 'geo_meanLat', 'geo_meanElev'] # Iterate through geo dictionary for k, v in d.items(): # Case 1: Coordinates special naming if k == 'coordinates': for idx, p in enumerate(v): try: # Check that our value is not in EMPTY. if isinstance(p, str): if p.lower() in EMPTY: # If elevation is a string or 0, don't record it if idx != 2: # If long or lat is empty, set it as 0 instead _root[x[idx]] = 0 else: # Set the value as a float into its entry. _root[x[idx]] = float(p) # Value is a normal number, or string representation of a number else: # Set the value as a float into its entry. _root[x[idx]] = float(p) except IndexError as e: logger_ts.warn("_extract_geo: IndexError: idx: {}, val: {}, {}".format(idx, p, e)) # Case 2: Any value that is a string can be added as-is elif isinstance(v, str): if k == 'meanElev': try: # Some data sets have meanElev listed under properties for some reason. _root['geo_' + k] = float(v) except ValueError as e: # If the value is a string, then we don't want it logger_ts.warn("_extract_geo: ValueError: meanElev is a string: {}, {}".format(v, e)) else: _root['geo_' + k] = v # Case 3: Nested dictionary. Recursion elif isinstance(v, dict): _root = _extract_geo(v, _root) return _root
[ "def", "_extract_geo", "(", "d", ",", "_root", ")", ":", "logger_ts", ".", "info", "(", "\"enter ts_extract_geo\"", ")", "# May not need these if the key names are corrected in the future.", "# COORDINATE ORDER: [LON, LAT, ELEV]", "x", "=", "[", "'geo_meanLon'", ",", "'geo_...
Extract geo data from input :param dict d: Geo :return dict _root: Root data
[ "Extract", "geo", "data", "from", "input", ":", "param", "dict", "d", ":", "Geo", ":", "return", "dict", "_root", ":", "Root", "data" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L81-L127
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_pub
def _extract_pub(l, _root): """ Extract publication data from one or more publication entries. :param list l: Publication :return dict _root: Root data """ logger_ts.info("enter _extract_pub") # For each publication entry for idx, pub in enumerate(l): logger_ts.info("processing publication #: {}".format(idx)) # Get author data first, since that's the most ambiguously structured data. _root = _extract_authors(pub, idx, _root) # Go through data of this publication for k, v in pub.items(): # Case 1: DOI ID. Don't need the rest of 'identifier' dict if k == 'identifier': try: _root['pub' + str(idx + 1) + '_DOI'] = v[0]['id'] except KeyError as e: logger_ts.warn("_extract_pub: KeyError: no doi id: {}, {}".format(v, e)) # Case 2: All other string entries else: if k != 'authors' and k != 'author': _root['pub' + str(idx + 1) + '_' + k] = v return _root
python
def _extract_pub(l, _root): """ Extract publication data from one or more publication entries. :param list l: Publication :return dict _root: Root data """ logger_ts.info("enter _extract_pub") # For each publication entry for idx, pub in enumerate(l): logger_ts.info("processing publication #: {}".format(idx)) # Get author data first, since that's the most ambiguously structured data. _root = _extract_authors(pub, idx, _root) # Go through data of this publication for k, v in pub.items(): # Case 1: DOI ID. Don't need the rest of 'identifier' dict if k == 'identifier': try: _root['pub' + str(idx + 1) + '_DOI'] = v[0]['id'] except KeyError as e: logger_ts.warn("_extract_pub: KeyError: no doi id: {}, {}".format(v, e)) # Case 2: All other string entries else: if k != 'authors' and k != 'author': _root['pub' + str(idx + 1) + '_' + k] = v return _root
[ "def", "_extract_pub", "(", "l", ",", "_root", ")", ":", "logger_ts", ".", "info", "(", "\"enter _extract_pub\"", ")", "# For each publication entry", "for", "idx", ",", "pub", "in", "enumerate", "(", "l", ")", ":", "logger_ts", ".", "info", "(", "\"processi...
Extract publication data from one or more publication entries. :param list l: Publication :return dict _root: Root data
[ "Extract", "publication", "data", "from", "one", "or", "more", "publication", "entries", ".", ":", "param", "list", "l", ":", "Publication", ":", "return", "dict", "_root", ":", "Root", "data" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L130-L154
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_authors
def _extract_authors(pub, idx, _root): """ Create a concatenated string of author names. Separate names with semi-colons. :param any pub: Publication author structure is ambiguous :param int idx: Index number of Pub """ logger_ts.info("enter extract_authors") try: # DOI Author data. We'd prefer to have this first. names = pub['author'] except KeyError as e: try: # Manually entered author data. This is second best. names = pub['authors'] except KeyError as e: # Couldn't find any author data. Skip it altogether. names = False logger_ts.info("extract_authors: KeyError: author data not provided, {}".format(e)) # If there is author data, find out what type it is if names: # Build author names onto empty string auth = '' # Is it a list of dicts or a list of strings? Could be either # Authors: Stored as a list of dictionaries or list of strings if isinstance(names, list): for name in names: if isinstance(name, str): auth += name + ';' elif isinstance(name, dict): for k, v in name.items(): auth += v + ';' elif isinstance(names, str): auth = names # Enter finished author string into target _root['pub' + str(idx + 1) + '_author'] = auth[:-1] return _root
python
def _extract_authors(pub, idx, _root): """ Create a concatenated string of author names. Separate names with semi-colons. :param any pub: Publication author structure is ambiguous :param int idx: Index number of Pub """ logger_ts.info("enter extract_authors") try: # DOI Author data. We'd prefer to have this first. names = pub['author'] except KeyError as e: try: # Manually entered author data. This is second best. names = pub['authors'] except KeyError as e: # Couldn't find any author data. Skip it altogether. names = False logger_ts.info("extract_authors: KeyError: author data not provided, {}".format(e)) # If there is author data, find out what type it is if names: # Build author names onto empty string auth = '' # Is it a list of dicts or a list of strings? Could be either # Authors: Stored as a list of dictionaries or list of strings if isinstance(names, list): for name in names: if isinstance(name, str): auth += name + ';' elif isinstance(name, dict): for k, v in name.items(): auth += v + ';' elif isinstance(names, str): auth = names # Enter finished author string into target _root['pub' + str(idx + 1) + '_author'] = auth[:-1] return _root
[ "def", "_extract_authors", "(", "pub", ",", "idx", ",", "_root", ")", ":", "logger_ts", ".", "info", "(", "\"enter extract_authors\"", ")", "try", ":", "# DOI Author data. We'd prefer to have this first.", "names", "=", "pub", "[", "'author'", "]", "except", "KeyE...
Create a concatenated string of author names. Separate names with semi-colons. :param any pub: Publication author structure is ambiguous :param int idx: Index number of Pub
[ "Create", "a", "concatenated", "string", "of", "author", "names", ".", "Separate", "names", "with", "semi", "-", "colons", ".", ":", "param", "any", "pub", ":", "Publication", "author", "structure", "is", "ambiguous", ":", "param", "int", "idx", ":", "Inde...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L157-L193
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_pc
def _extract_pc(d, root, pc, whichtables): """ Extract all data from a PaleoData dictionary. :param dict d: PaleoData dictionary :param dict root: Time series root data :param str pc: paleoData or chronData :param str whichtables: all, meas, summ, or ens :return list _ts: Time series """ logger_ts.info("enter extract_pc") _ts = [] try: # For each table in pc for k, v in d[pc].items(): if whichtables == "all" or whichtables == "meas": for _table_name1, _table_data1 in v["measurementTable"].items(): _ts = _extract_table(_table_data1, copy.deepcopy(root), pc, _ts, "meas") if whichtables != "meas": if "model" in v: for _table_name1, _table_data1 in v["model"].items(): # get the method info for this model. This will be paired to all summ and ens table data _method = _extract_method(_table_data1["method"]) if whichtables == "all" or whichtables == "summ": if "summaryTable" in _table_data1: for _table_name2, _table_data2 in _table_data1["summaryTable"].items(): # take a copy of this tso root _tso = copy.deepcopy(root) # add in the method details _tso.update(_method) # add in the table details _ts = _extract_table(_table_data2, _tso, pc, _ts, "summ") if whichtables == "all" or whichtables == "ens": if "ensembleTable" in _table_data1: for _table_name2, _table_data2 in _table_data1["ensembleTable"].items(): _tso = copy.deepcopy(root) _tso.update(_method) _ts = _extract_table(_table_data2, _tso, pc, _ts, "ens") except Exception as e: logger_ts.warn("extract_pc: Exception: {}".format(e)) return _ts
python
def _extract_pc(d, root, pc, whichtables): """ Extract all data from a PaleoData dictionary. :param dict d: PaleoData dictionary :param dict root: Time series root data :param str pc: paleoData or chronData :param str whichtables: all, meas, summ, or ens :return list _ts: Time series """ logger_ts.info("enter extract_pc") _ts = [] try: # For each table in pc for k, v in d[pc].items(): if whichtables == "all" or whichtables == "meas": for _table_name1, _table_data1 in v["measurementTable"].items(): _ts = _extract_table(_table_data1, copy.deepcopy(root), pc, _ts, "meas") if whichtables != "meas": if "model" in v: for _table_name1, _table_data1 in v["model"].items(): # get the method info for this model. This will be paired to all summ and ens table data _method = _extract_method(_table_data1["method"]) if whichtables == "all" or whichtables == "summ": if "summaryTable" in _table_data1: for _table_name2, _table_data2 in _table_data1["summaryTable"].items(): # take a copy of this tso root _tso = copy.deepcopy(root) # add in the method details _tso.update(_method) # add in the table details _ts = _extract_table(_table_data2, _tso, pc, _ts, "summ") if whichtables == "all" or whichtables == "ens": if "ensembleTable" in _table_data1: for _table_name2, _table_data2 in _table_data1["ensembleTable"].items(): _tso = copy.deepcopy(root) _tso.update(_method) _ts = _extract_table(_table_data2, _tso, pc, _ts, "ens") except Exception as e: logger_ts.warn("extract_pc: Exception: {}".format(e)) return _ts
[ "def", "_extract_pc", "(", "d", ",", "root", ",", "pc", ",", "whichtables", ")", ":", "logger_ts", ".", "info", "(", "\"enter extract_pc\"", ")", "_ts", "=", "[", "]", "try", ":", "# For each table in pc", "for", "k", ",", "v", "in", "d", "[", "pc", ...
Extract all data from a PaleoData dictionary. :param dict d: PaleoData dictionary :param dict root: Time series root data :param str pc: paleoData or chronData :param str whichtables: all, meas, summ, or ens :return list _ts: Time series
[ "Extract", "all", "data", "from", "a", "PaleoData", "dictionary", ".", ":", "param", "dict", "d", ":", "PaleoData", "dictionary", ":", "param", "dict", "root", ":", "Time", "series", "root", "data", ":", "param", "str", "pc", ":", "paleoData", "or", "chr...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L196-L236
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_method
def _extract_method(method): """ Make a timeseries-formatted version of model method data :param dict method: Method data :return dict _method: Method data, formatted """ _method = {} for k,v in method.items(): _method["method_" + k] = v return _method
python
def _extract_method(method): """ Make a timeseries-formatted version of model method data :param dict method: Method data :return dict _method: Method data, formatted """ _method = {} for k,v in method.items(): _method["method_" + k] = v return _method
[ "def", "_extract_method", "(", "method", ")", ":", "_method", "=", "{", "}", "for", "k", ",", "v", "in", "method", ".", "items", "(", ")", ":", "_method", "[", "\"method_\"", "+", "k", "]", "=", "v", "return", "_method" ]
Make a timeseries-formatted version of model method data :param dict method: Method data :return dict _method: Method data, formatted
[ "Make", "a", "timeseries", "-", "formatted", "version", "of", "model", "method", "data" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L239-L249
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_special
def _extract_special(current, table_data): """ Extract year, age, and depth column from table data :param dict table_data: Data at the table level :param dict current: Current data :return dict current: """ logger_ts.info("enter extract_special") try: # Add age, year, and depth columns to ts_root where possible for k, v in table_data['columns'].items(): s = "" # special case for year bp, or any variation of it. Translate key to "age"" if "bp" in k.lower(): s = "age" # all other normal cases. clean key and set key. elif any(x in k.lower() for x in ('age', 'depth', 'year', "yr", "distance_from_top", "distance")): # Some keys have units hanging on them (i.e. 'year_ad', 'depth_cm'). We don't want units on the keys if re_pandas_x_und.match(k): s = k.split('_')[0] elif "distance" in k: s = "depth" else: s = k # create the entry in ts_root. if s: try: current[s] = v['values'] except KeyError as e: # Values key was not found. logger_ts.warn("extract_special: KeyError: 'values' not found, {}".format(e)) try: current[s + 'Units'] = v['units'] except KeyError as e: # Values key was not found. logger_ts.warn("extract_special: KeyError: 'units' not found, {}".format(e)) except Exception as e: logger_ts.error("extract_special: {}".format(e)) return current
python
def _extract_special(current, table_data): """ Extract year, age, and depth column from table data :param dict table_data: Data at the table level :param dict current: Current data :return dict current: """ logger_ts.info("enter extract_special") try: # Add age, year, and depth columns to ts_root where possible for k, v in table_data['columns'].items(): s = "" # special case for year bp, or any variation of it. Translate key to "age"" if "bp" in k.lower(): s = "age" # all other normal cases. clean key and set key. elif any(x in k.lower() for x in ('age', 'depth', 'year', "yr", "distance_from_top", "distance")): # Some keys have units hanging on them (i.e. 'year_ad', 'depth_cm'). We don't want units on the keys if re_pandas_x_und.match(k): s = k.split('_')[0] elif "distance" in k: s = "depth" else: s = k # create the entry in ts_root. if s: try: current[s] = v['values'] except KeyError as e: # Values key was not found. logger_ts.warn("extract_special: KeyError: 'values' not found, {}".format(e)) try: current[s + 'Units'] = v['units'] except KeyError as e: # Values key was not found. logger_ts.warn("extract_special: KeyError: 'units' not found, {}".format(e)) except Exception as e: logger_ts.error("extract_special: {}".format(e)) return current
[ "def", "_extract_special", "(", "current", ",", "table_data", ")", ":", "logger_ts", ".", "info", "(", "\"enter extract_special\"", ")", "try", ":", "# Add age, year, and depth columns to ts_root where possible", "for", "k", ",", "v", "in", "table_data", "[", "'column...
Extract year, age, and depth column from table data :param dict table_data: Data at the table level :param dict current: Current data :return dict current:
[ "Extract", "year", "age", "and", "depth", "column", "from", "table", "data", ":", "param", "dict", "table_data", ":", "Data", "at", "the", "table", "level", ":", "param", "dict", "current", ":", "Current", "data", ":", "return", "dict", "current", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L252-L295
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_table_root
def _extract_table_root(d, current, pc): """ Extract data from the root level of a paleoData table. :param dict d: paleoData table :param dict current: Current root data :param str pc: paleoData or chronData :return dict current: Current root data """ logger_ts.info("enter extract_table_root") try: for k, v in d.items(): if isinstance(v, str): current[pc + '_' + k] = v except Exception as e: logger_ts.error("extract_table_root: {}".format(e)) return current
python
def _extract_table_root(d, current, pc): """ Extract data from the root level of a paleoData table. :param dict d: paleoData table :param dict current: Current root data :param str pc: paleoData or chronData :return dict current: Current root data """ logger_ts.info("enter extract_table_root") try: for k, v in d.items(): if isinstance(v, str): current[pc + '_' + k] = v except Exception as e: logger_ts.error("extract_table_root: {}".format(e)) return current
[ "def", "_extract_table_root", "(", "d", ",", "current", ",", "pc", ")", ":", "logger_ts", ".", "info", "(", "\"enter extract_table_root\"", ")", "try", ":", "for", "k", ",", "v", "in", "d", ".", "items", "(", ")", ":", "if", "isinstance", "(", "v", "...
Extract data from the root level of a paleoData table. :param dict d: paleoData table :param dict current: Current root data :param str pc: paleoData or chronData :return dict current: Current root data
[ "Extract", "data", "from", "the", "root", "level", "of", "a", "paleoData", "table", ".", ":", "param", "dict", "d", ":", "paleoData", "table", ":", "param", "dict", "current", ":", "Current", "root", "data", ":", "param", "str", "pc", ":", "paleoData", ...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L298-L313
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_table_model
def _extract_table_model(table_data, current, tt): """ Add in modelNumber and summaryNumber fields if this is a summary table :param dict table_data: Table data :param dict current: LiPD root data :param str tt: Table type "summ", "ens", "meas" :return dict current: Current root data """ try: if tt in ["summ", "ens"]: m = re.match(re_sheet, table_data["tableName"]) if m: _pc_num= m.group(1) + "Number" current[_pc_num] = m.group(2) current["modelNumber"] = m.group(4) current["tableNumber"] = m.group(6) else: logger_ts.error("extract_table_summary: Unable to parse paleo/model/table numbers") except Exception as e: logger_ts.error("extract_table_summary: {}".format(e)) return current
python
def _extract_table_model(table_data, current, tt): """ Add in modelNumber and summaryNumber fields if this is a summary table :param dict table_data: Table data :param dict current: LiPD root data :param str tt: Table type "summ", "ens", "meas" :return dict current: Current root data """ try: if tt in ["summ", "ens"]: m = re.match(re_sheet, table_data["tableName"]) if m: _pc_num= m.group(1) + "Number" current[_pc_num] = m.group(2) current["modelNumber"] = m.group(4) current["tableNumber"] = m.group(6) else: logger_ts.error("extract_table_summary: Unable to parse paleo/model/table numbers") except Exception as e: logger_ts.error("extract_table_summary: {}".format(e)) return current
[ "def", "_extract_table_model", "(", "table_data", ",", "current", ",", "tt", ")", ":", "try", ":", "if", "tt", "in", "[", "\"summ\"", ",", "\"ens\"", "]", ":", "m", "=", "re", ".", "match", "(", "re_sheet", ",", "table_data", "[", "\"tableName\"", "]",...
Add in modelNumber and summaryNumber fields if this is a summary table :param dict table_data: Table data :param dict current: LiPD root data :param str tt: Table type "summ", "ens", "meas" :return dict current: Current root data
[ "Add", "in", "modelNumber", "and", "summaryNumber", "fields", "if", "this", "is", "a", "summary", "table" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L316-L337
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_table
def _extract_table(table_data, current, pc, ts, tt): """ Use the given table data to create a time series entry for each column in the table. :param dict table_data: Table data :param dict current: LiPD root data :param str pc: paleoData or chronData :param list ts: Time series (so far) :param bool summary: Summary Table or not :return list ts: Time series (so far) """ current["tableType"] = tt # Get root items for this table current = _extract_table_root(table_data, current, pc) # Add in modelNumber and tableNumber if this is "ens" or "summ" table current = _extract_table_model(table_data, current, tt) # Add age, depth, and year columns to root if available _table_tmp = _extract_special(current, table_data) try: # Start creating entries using dictionary copies. for _col_name, _col_data in table_data["columns"].items(): # Add column data onto root items. Copy so we don't ruin original data _col_tmp = _extract_columns(_col_data, copy.deepcopy(_table_tmp), pc) try: ts.append(_col_tmp) except Exception as e: logger_ts.warn("extract_table: Unable to create ts entry, {}".format(e)) except Exception as e: logger_ts.error("extract_table: {}".format(e)) return ts
python
def _extract_table(table_data, current, pc, ts, tt): """ Use the given table data to create a time series entry for each column in the table. :param dict table_data: Table data :param dict current: LiPD root data :param str pc: paleoData or chronData :param list ts: Time series (so far) :param bool summary: Summary Table or not :return list ts: Time series (so far) """ current["tableType"] = tt # Get root items for this table current = _extract_table_root(table_data, current, pc) # Add in modelNumber and tableNumber if this is "ens" or "summ" table current = _extract_table_model(table_data, current, tt) # Add age, depth, and year columns to root if available _table_tmp = _extract_special(current, table_data) try: # Start creating entries using dictionary copies. for _col_name, _col_data in table_data["columns"].items(): # Add column data onto root items. Copy so we don't ruin original data _col_tmp = _extract_columns(_col_data, copy.deepcopy(_table_tmp), pc) try: ts.append(_col_tmp) except Exception as e: logger_ts.warn("extract_table: Unable to create ts entry, {}".format(e)) except Exception as e: logger_ts.error("extract_table: {}".format(e)) return ts
[ "def", "_extract_table", "(", "table_data", ",", "current", ",", "pc", ",", "ts", ",", "tt", ")", ":", "current", "[", "\"tableType\"", "]", "=", "tt", "# Get root items for this table", "current", "=", "_extract_table_root", "(", "table_data", ",", "current", ...
Use the given table data to create a time series entry for each column in the table. :param dict table_data: Table data :param dict current: LiPD root data :param str pc: paleoData or chronData :param list ts: Time series (so far) :param bool summary: Summary Table or not :return list ts: Time series (so far)
[ "Use", "the", "given", "table", "data", "to", "create", "a", "time", "series", "entry", "for", "each", "column", "in", "the", "table", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L340-L369
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_extract_columns
def _extract_columns(d, tmp_tso, pc): """ Extract data from one paleoData column :param dict d: Column dictionary :param dict tmp_tso: TSO dictionary with only root items :return dict: Finished TSO """ logger_ts.info("enter extract_columns") for k, v in d.items(): if isinstance(v, dict): flat_data = _extract_nested(pc + "_" + k, v, {}) for n,m in flat_data.items(): tmp_tso[n] = m else: # Assume if it's not a special nested case, then it's a string value tmp_tso[pc + '_' + k] = v return tmp_tso
python
def _extract_columns(d, tmp_tso, pc): """ Extract data from one paleoData column :param dict d: Column dictionary :param dict tmp_tso: TSO dictionary with only root items :return dict: Finished TSO """ logger_ts.info("enter extract_columns") for k, v in d.items(): if isinstance(v, dict): flat_data = _extract_nested(pc + "_" + k, v, {}) for n,m in flat_data.items(): tmp_tso[n] = m else: # Assume if it's not a special nested case, then it's a string value tmp_tso[pc + '_' + k] = v return tmp_tso
[ "def", "_extract_columns", "(", "d", ",", "tmp_tso", ",", "pc", ")", ":", "logger_ts", ".", "info", "(", "\"enter extract_columns\"", ")", "for", "k", ",", "v", "in", "d", ".", "items", "(", ")", ":", "if", "isinstance", "(", "v", ",", "dict", ")", ...
Extract data from one paleoData column :param dict d: Column dictionary :param dict tmp_tso: TSO dictionary with only root items :return dict: Finished TSO
[ "Extract", "data", "from", "one", "paleoData", "column", ":", "param", "dict", "d", ":", "Column", "dictionary", ":", "param", "dict", "tmp_tso", ":", "TSO", "dictionary", "with", "only", "root", "items", ":", "return", "dict", ":", "Finished", "TSO" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L372-L388
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
collapse
def collapse(l, raw): """ LiPD Version 1.3 Main function to initiate time series to LiPD conversion Each object has a: "paleoNumber" or "chronNumber" "tableNumber" "modelNumber" "time_id" "mode" - chronData or paleoData "tableType" - "meas" "ens" "summ" :param list l: Time series :return dict _master: LiPD data, sorted by dataset name """ logger_ts.info("enter collapse") # LiPD data (in progress), sorted dataset name _master = {} _dsn = "" try: # Determine if we're collapsing a paleo or chron time series _pc = l[0]["mode"] # Loop the time series for entry in l: # Get notable keys dsn = entry['dataSetName'] _dsn = dsn _current = entry # Since root items are the same in each column of the same dataset, we only need these steps the first time. if dsn not in _master: logger_ts.info("collapsing: {}".format(dsn)) print("collapsing: {}".format(dsn)) _master, _current = _collapse_root(_master, _current, dsn, _pc) try: _master[dsn]["paleoData"] = raw[dsn]["paleoData"] if "chronData" in raw[dsn]: _master[dsn]["chronData"] = raw[dsn]["chronData"] except KeyError as e: print("collapse: Could not collapse an object the dataset: {}, {}".format(dsn, e)) # Collapse pc, calibration, and interpretation _master = _collapse_pc(_master, _current, dsn, _pc) # The result combined into a single dataset. Remove the extra layer on the data. if len(_master) == 1: _master = _master[_dsn] print("Created LiPD data: 1 dataset") else: print("Created LiPD data: {} datasets".format(len(_master))) except Exception as e: print("Error: Unable to collapse time series, {}".format(e)) logger_ts.error("collapse: Exception: {}".format(e)) logger_ts.info("exit collapse") return _master
python
def collapse(l, raw): """ LiPD Version 1.3 Main function to initiate time series to LiPD conversion Each object has a: "paleoNumber" or "chronNumber" "tableNumber" "modelNumber" "time_id" "mode" - chronData or paleoData "tableType" - "meas" "ens" "summ" :param list l: Time series :return dict _master: LiPD data, sorted by dataset name """ logger_ts.info("enter collapse") # LiPD data (in progress), sorted dataset name _master = {} _dsn = "" try: # Determine if we're collapsing a paleo or chron time series _pc = l[0]["mode"] # Loop the time series for entry in l: # Get notable keys dsn = entry['dataSetName'] _dsn = dsn _current = entry # Since root items are the same in each column of the same dataset, we only need these steps the first time. if dsn not in _master: logger_ts.info("collapsing: {}".format(dsn)) print("collapsing: {}".format(dsn)) _master, _current = _collapse_root(_master, _current, dsn, _pc) try: _master[dsn]["paleoData"] = raw[dsn]["paleoData"] if "chronData" in raw[dsn]: _master[dsn]["chronData"] = raw[dsn]["chronData"] except KeyError as e: print("collapse: Could not collapse an object the dataset: {}, {}".format(dsn, e)) # Collapse pc, calibration, and interpretation _master = _collapse_pc(_master, _current, dsn, _pc) # The result combined into a single dataset. Remove the extra layer on the data. if len(_master) == 1: _master = _master[_dsn] print("Created LiPD data: 1 dataset") else: print("Created LiPD data: {} datasets".format(len(_master))) except Exception as e: print("Error: Unable to collapse time series, {}".format(e)) logger_ts.error("collapse: Exception: {}".format(e)) logger_ts.info("exit collapse") return _master
[ "def", "collapse", "(", "l", ",", "raw", ")", ":", "logger_ts", ".", "info", "(", "\"enter collapse\"", ")", "# LiPD data (in progress), sorted dataset name", "_master", "=", "{", "}", "_dsn", "=", "\"\"", "try", ":", "# Determine if we're collapsing a paleo or chron ...
LiPD Version 1.3 Main function to initiate time series to LiPD conversion Each object has a: "paleoNumber" or "chronNumber" "tableNumber" "modelNumber" "time_id" "mode" - chronData or paleoData "tableType" - "meas" "ens" "summ" :param list l: Time series :return dict _master: LiPD data, sorted by dataset name
[ "LiPD", "Version", "1", ".", "3", "Main", "function", "to", "initiate", "time", "series", "to", "LiPD", "conversion" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L406-L465
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_get_current_names
def _get_current_names(current, dsn, pc): """ Get the table name and variable name from the given time series entry :param dict current: Time series entry :param str pc: paleoData or chronData :return str _table_name: :return str _variable_name: """ _table_name = "" _variable_name = "" # Get key info try: _table_name = current['{}_tableName'.format(pc)] _variable_name = current['{}_variableName'.format(pc)] except Exception as e: print("Error: Unable to collapse time series: {}, {}".format(dsn, e)) logger_ts.error("get_current: {}, {}".format(dsn, e)) return _table_name, _variable_name
python
def _get_current_names(current, dsn, pc): """ Get the table name and variable name from the given time series entry :param dict current: Time series entry :param str pc: paleoData or chronData :return str _table_name: :return str _variable_name: """ _table_name = "" _variable_name = "" # Get key info try: _table_name = current['{}_tableName'.format(pc)] _variable_name = current['{}_variableName'.format(pc)] except Exception as e: print("Error: Unable to collapse time series: {}, {}".format(dsn, e)) logger_ts.error("get_current: {}, {}".format(dsn, e)) return _table_name, _variable_name
[ "def", "_get_current_names", "(", "current", ",", "dsn", ",", "pc", ")", ":", "_table_name", "=", "\"\"", "_variable_name", "=", "\"\"", "# Get key info", "try", ":", "_table_name", "=", "current", "[", "'{}_tableName'", ".", "format", "(", "pc", ")", "]", ...
Get the table name and variable name from the given time series entry :param dict current: Time series entry :param str pc: paleoData or chronData :return str _table_name: :return str _variable_name:
[ "Get", "the", "table", "name", "and", "variable", "name", "from", "the", "given", "time", "series", "entry" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L468-L486
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_collapse_root
def _collapse_root(master, current, dsn, pc): """ Collapse the root items of the current time series entry :param dict master: LiPD data (so far) :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData (mode) :return dict master: :return dict current: """ logger_ts.info("enter collapse_root") _tmp_fund = {} _tmp_pub = {} # The tmp lipd data that we'll place in master later _tmp_master = {'pub': [], 'geo': {'geometry': {'coordinates': []}, 'properties': {}}, 'funding': [], 'paleoData': {}, "chronData": {}} # _raw = _switch[pc] _c_keys = ['meanLat', 'meanLon', 'meanElev'] _c_vals = [0, 0, 0] _p_keys = ['siteName', 'pages2kRegion', "location", "gcmdLocation", ""] try: # does not have # paleoData, chronData, mode, tableType, time_id, depth, depthUnits, age, ageUnits # does have # pub, geo, funding, proxy, archiveType, description, investigator, # For all keys in the current time series entry for k, v in current.items(): # Underscore present. Only underscore keys that belong here are funding, geo, and pub if "_" in k: # FUNDING if 'funding' in k: # Group funding items in tmp_funding by number m = re_fund_valid.match(k) try: _tmp_fund[m.group(1)][m.group(2)] = v except Exception: try: # If the first layer is missing, create it and try again _tmp_fund[m.group(1)] = {} _tmp_fund[m.group(1)][m.group(2)] = v except Exception: # Still not working. Give up. pass # GEO elif 'geo' in k: key = k.split('_') # Coordinates - [LON, LAT, ELEV] if key[1] in _c_keys: if key[1] == 'meanLon' or key[1] == "longitude": _c_vals[0] = v elif key[1] == 'meanLat' or key[1] == "latitude": _c_vals[1] = v elif key[1] == 'meanElev' or key[1] == "elevation": _c_vals[2] = v # Properties else: _tmp_master['geo']['properties'][key[1]] = v # All others # else: # _tmp_master['geo'][key[1]] = v # PUBLICATION elif 'pub' in k: # Group pub items in tmp_pub by number m = re_pub_valid.match(k.lower()) if m: number = int(m.group(1)) - 1 # 0 indexed behind the scenes, 1 indexed to user. key = m.group(2) # Authors ("Pu, Y.; Nace, T.; etc..") if key == 'author' or key == 'authors': try: _tmp_pub[number]['author'] = _collapse_author(v) except KeyError as e: # Dictionary not created yet. Assign one first. _tmp_pub[number] = {} _tmp_pub[number]['author'] = _collapse_author(v) # DOI ID elif key == 'DOI': try: _tmp_pub[number]['identifier'] = [{"id": v, "type": "doi", "url": "http://dx.doi.org/" + str(v)}] except KeyError: # Dictionary not created yet. Assign one first. _tmp_pub[number] = {} _tmp_pub[number]['identifier'] = [{"id": v, "type": "doi", "url": "http://dx.doi.org/" + str(v)}] # All others else: try: _tmp_pub[number][key] = v except KeyError: # Dictionary not created yet. Assign one first. _tmp_pub[number] = {} _tmp_pub[number][key] = v # No underscore in name, we can rule out the other obvious keys we don't want else: # Rule out any timeseries keys that we added, and paleoData/chronData prefixed keys. if not any(i in k.lower() or i is k.lower() for i in ["paleodata", "chrondata", "mode", "tabletype", "time_id", "depth", "depthunits", "age", "ageunits"]): # Root item: _tmp_master[k] = v continue # Append the compiled data into the master dataset data for k, v in _tmp_pub.items(): _tmp_master['pub'].append(v) for k, v in _tmp_fund.items(): _tmp_master['funding'].append(v) # Get rid of elevation coordinate if one was never added. if _c_vals[2] == 0: del _c_vals[2] _tmp_master['geo']['geometry']['coordinates'] = _c_vals # Create entry in object master, and set our new data to it. master[dsn] = _tmp_master except Exception as e: logger_ts.error("collapse_root: Exception: {}, {}".format(dsn, e)) logger_ts.info("exit collapse_root") return master, current
python
def _collapse_root(master, current, dsn, pc): """ Collapse the root items of the current time series entry :param dict master: LiPD data (so far) :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData (mode) :return dict master: :return dict current: """ logger_ts.info("enter collapse_root") _tmp_fund = {} _tmp_pub = {} # The tmp lipd data that we'll place in master later _tmp_master = {'pub': [], 'geo': {'geometry': {'coordinates': []}, 'properties': {}}, 'funding': [], 'paleoData': {}, "chronData": {}} # _raw = _switch[pc] _c_keys = ['meanLat', 'meanLon', 'meanElev'] _c_vals = [0, 0, 0] _p_keys = ['siteName', 'pages2kRegion', "location", "gcmdLocation", ""] try: # does not have # paleoData, chronData, mode, tableType, time_id, depth, depthUnits, age, ageUnits # does have # pub, geo, funding, proxy, archiveType, description, investigator, # For all keys in the current time series entry for k, v in current.items(): # Underscore present. Only underscore keys that belong here are funding, geo, and pub if "_" in k: # FUNDING if 'funding' in k: # Group funding items in tmp_funding by number m = re_fund_valid.match(k) try: _tmp_fund[m.group(1)][m.group(2)] = v except Exception: try: # If the first layer is missing, create it and try again _tmp_fund[m.group(1)] = {} _tmp_fund[m.group(1)][m.group(2)] = v except Exception: # Still not working. Give up. pass # GEO elif 'geo' in k: key = k.split('_') # Coordinates - [LON, LAT, ELEV] if key[1] in _c_keys: if key[1] == 'meanLon' or key[1] == "longitude": _c_vals[0] = v elif key[1] == 'meanLat' or key[1] == "latitude": _c_vals[1] = v elif key[1] == 'meanElev' or key[1] == "elevation": _c_vals[2] = v # Properties else: _tmp_master['geo']['properties'][key[1]] = v # All others # else: # _tmp_master['geo'][key[1]] = v # PUBLICATION elif 'pub' in k: # Group pub items in tmp_pub by number m = re_pub_valid.match(k.lower()) if m: number = int(m.group(1)) - 1 # 0 indexed behind the scenes, 1 indexed to user. key = m.group(2) # Authors ("Pu, Y.; Nace, T.; etc..") if key == 'author' or key == 'authors': try: _tmp_pub[number]['author'] = _collapse_author(v) except KeyError as e: # Dictionary not created yet. Assign one first. _tmp_pub[number] = {} _tmp_pub[number]['author'] = _collapse_author(v) # DOI ID elif key == 'DOI': try: _tmp_pub[number]['identifier'] = [{"id": v, "type": "doi", "url": "http://dx.doi.org/" + str(v)}] except KeyError: # Dictionary not created yet. Assign one first. _tmp_pub[number] = {} _tmp_pub[number]['identifier'] = [{"id": v, "type": "doi", "url": "http://dx.doi.org/" + str(v)}] # All others else: try: _tmp_pub[number][key] = v except KeyError: # Dictionary not created yet. Assign one first. _tmp_pub[number] = {} _tmp_pub[number][key] = v # No underscore in name, we can rule out the other obvious keys we don't want else: # Rule out any timeseries keys that we added, and paleoData/chronData prefixed keys. if not any(i in k.lower() or i is k.lower() for i in ["paleodata", "chrondata", "mode", "tabletype", "time_id", "depth", "depthunits", "age", "ageunits"]): # Root item: _tmp_master[k] = v continue # Append the compiled data into the master dataset data for k, v in _tmp_pub.items(): _tmp_master['pub'].append(v) for k, v in _tmp_fund.items(): _tmp_master['funding'].append(v) # Get rid of elevation coordinate if one was never added. if _c_vals[2] == 0: del _c_vals[2] _tmp_master['geo']['geometry']['coordinates'] = _c_vals # Create entry in object master, and set our new data to it. master[dsn] = _tmp_master except Exception as e: logger_ts.error("collapse_root: Exception: {}, {}".format(dsn, e)) logger_ts.info("exit collapse_root") return master, current
[ "def", "_collapse_root", "(", "master", ",", "current", ",", "dsn", ",", "pc", ")", ":", "logger_ts", ".", "info", "(", "\"enter collapse_root\"", ")", "_tmp_fund", "=", "{", "}", "_tmp_pub", "=", "{", "}", "# The tmp lipd data that we'll place in master later", ...
Collapse the root items of the current time series entry :param dict master: LiPD data (so far) :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData (mode) :return dict master: :return dict current:
[ "Collapse", "the", "root", "items", "of", "the", "current", "time", "series", "entry" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L489-L614
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_collapse_author
def _collapse_author(s): """ Split author string back into organized dictionary :param str s: Formatted names string "Last, F.; Last, F.; etc.." :return list of dict: One dictionary per author name """ logger_ts.info("enter collapse_author") l = [] authors = s.split(';') for author in authors: l.append({'name': author}) return l
python
def _collapse_author(s): """ Split author string back into organized dictionary :param str s: Formatted names string "Last, F.; Last, F.; etc.." :return list of dict: One dictionary per author name """ logger_ts.info("enter collapse_author") l = [] authors = s.split(';') for author in authors: l.append({'name': author}) return l
[ "def", "_collapse_author", "(", "s", ")", ":", "logger_ts", ".", "info", "(", "\"enter collapse_author\"", ")", "l", "=", "[", "]", "authors", "=", "s", ".", "split", "(", "';'", ")", "for", "author", "in", "authors", ":", "l", ".", "append", "(", "{...
Split author string back into organized dictionary :param str s: Formatted names string "Last, F.; Last, F.; etc.." :return list of dict: One dictionary per author name
[ "Split", "author", "string", "back", "into", "organized", "dictionary", ":", "param", "str", "s", ":", "Formatted", "names", "string", "Last", "F", ".", ";", "Last", "F", ".", ";", "etc", "..", ":", "return", "list", "of", "dict", ":", "One", "dictiona...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L617-L628
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_collapse_pc
def _collapse_pc(master, current, dsn, pc): """ Collapse the paleo or chron for the current time series entry :param dict master: LiPD data (so far) :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData :return dict master: """ logger_ts.info("enter collapse_paleo") _table_name, _variable_name = _get_current_names(current, dsn, pc) try: # Get the names we need to build the hierarchy _m = re.match(re_sheet_w_number, _table_name) # Is this a summary table or a measurement table? _switch = {"meas": "measurementTable", "summ": "summaryTable", "ens": "ensembleTable"} _ms = _switch[current["tableType"]] # This is a measurement table. Put it in the correct part of the structure # master[datasetname][chronData][chron0][measurementTable][chron0measurement0] if _ms == "measurementTable": # master[dsn] = _collapse_build_skeleton(master(dsn), _ms, _m) # Collapse the keys in the table root if a table does not yet exist if _table_name not in master[dsn][pc][_m.group(1)][_ms]: _tmp_table = _collapse_table_root(current, dsn, pc) master[dsn][pc][_m.group(1)][_ms][_table_name] = _tmp_table # Collapse the keys at the column level, and return the column data _tmp_column = _collapse_column(current, pc) # Create the column entry in the table master[dsn][pc][_m.group(1)][_ms][_table_name]['columns'][_variable_name] = _tmp_column # This is a summary table. Put it in the correct part of the structure # master[datasetname][chronData][chron0][model][chron0model0][summaryTable][chron0model0summary0] elif _ms in ["ensembleTable", "summaryTable"]: # Collapse the keys in the table root if a table does not yet exist if _table_name not in master[dsn][pc][_m.group(1)]["model"][_m.group(1) + _m.group(2)][_ms]: _tmp_table = _collapse_table_root(current, dsn, pc) master[dsn][pc][_m.group(1)]["model"][_m.group(1) + _m.group(2)][_ms][_table_name] = _tmp_table # Collapse the keys at the column level, and return the column data _tmp_column = _collapse_column(current, pc) # Create the column entry in the table master[dsn][pc][_m.group(1)]["model"][_m.group(1) + _m.group(2)][_ms][_table_name]["columns"][_variable_name] = _tmp_column except Exception as e: print("Error: Unable to collapse column data: {}, {}".format(dsn, e)) logger_ts.error("collapse_paleo: {}, {}, {}".format(dsn, _variable_name, e)) # If these sections had any items added to them, then add them to the column master. return master
python
def _collapse_pc(master, current, dsn, pc): """ Collapse the paleo or chron for the current time series entry :param dict master: LiPD data (so far) :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData :return dict master: """ logger_ts.info("enter collapse_paleo") _table_name, _variable_name = _get_current_names(current, dsn, pc) try: # Get the names we need to build the hierarchy _m = re.match(re_sheet_w_number, _table_name) # Is this a summary table or a measurement table? _switch = {"meas": "measurementTable", "summ": "summaryTable", "ens": "ensembleTable"} _ms = _switch[current["tableType"]] # This is a measurement table. Put it in the correct part of the structure # master[datasetname][chronData][chron0][measurementTable][chron0measurement0] if _ms == "measurementTable": # master[dsn] = _collapse_build_skeleton(master(dsn), _ms, _m) # Collapse the keys in the table root if a table does not yet exist if _table_name not in master[dsn][pc][_m.group(1)][_ms]: _tmp_table = _collapse_table_root(current, dsn, pc) master[dsn][pc][_m.group(1)][_ms][_table_name] = _tmp_table # Collapse the keys at the column level, and return the column data _tmp_column = _collapse_column(current, pc) # Create the column entry in the table master[dsn][pc][_m.group(1)][_ms][_table_name]['columns'][_variable_name] = _tmp_column # This is a summary table. Put it in the correct part of the structure # master[datasetname][chronData][chron0][model][chron0model0][summaryTable][chron0model0summary0] elif _ms in ["ensembleTable", "summaryTable"]: # Collapse the keys in the table root if a table does not yet exist if _table_name not in master[dsn][pc][_m.group(1)]["model"][_m.group(1) + _m.group(2)][_ms]: _tmp_table = _collapse_table_root(current, dsn, pc) master[dsn][pc][_m.group(1)]["model"][_m.group(1) + _m.group(2)][_ms][_table_name] = _tmp_table # Collapse the keys at the column level, and return the column data _tmp_column = _collapse_column(current, pc) # Create the column entry in the table master[dsn][pc][_m.group(1)]["model"][_m.group(1) + _m.group(2)][_ms][_table_name]["columns"][_variable_name] = _tmp_column except Exception as e: print("Error: Unable to collapse column data: {}, {}".format(dsn, e)) logger_ts.error("collapse_paleo: {}, {}, {}".format(dsn, _variable_name, e)) # If these sections had any items added to them, then add them to the column master. return master
[ "def", "_collapse_pc", "(", "master", ",", "current", ",", "dsn", ",", "pc", ")", ":", "logger_ts", ".", "info", "(", "\"enter collapse_paleo\"", ")", "_table_name", ",", "_variable_name", "=", "_get_current_names", "(", "current", ",", "dsn", ",", "pc", ")"...
Collapse the paleo or chron for the current time series entry :param dict master: LiPD data (so far) :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData :return dict master:
[ "Collapse", "the", "paleo", "or", "chron", "for", "the", "current", "time", "series", "entry" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L631-L687
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_collapse_table_root
def _collapse_table_root(current, dsn, pc): """ Create a table with items in root given the current time series entry :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData :return dict _tmp_table: Table data """ logger_ts.info("enter collapse_table_root") _table_name, _variable_name = _get_current_names(current, dsn, pc) _tmp_table = {'columns': {}} try: for k, v in current.items(): # These are the main table keys that we should be looking for for i in ['filename', 'googleWorkSheetKey', 'tableName', "missingValue", "tableMD5", "dataMD5"]: if i in k: try: _tmp_table[i] = v except Exception: # Not all keys are available. It's okay if we hit a KeyError. pass except Exception as e: print("Error: Unable to collapse: {}, {}".format(dsn, e)) logger_ts.error("collapse_table_root: Unable to collapse: {}, {}, {}".format(_table_name, dsn, e)) return _tmp_table
python
def _collapse_table_root(current, dsn, pc): """ Create a table with items in root given the current time series entry :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData :return dict _tmp_table: Table data """ logger_ts.info("enter collapse_table_root") _table_name, _variable_name = _get_current_names(current, dsn, pc) _tmp_table = {'columns': {}} try: for k, v in current.items(): # These are the main table keys that we should be looking for for i in ['filename', 'googleWorkSheetKey', 'tableName', "missingValue", "tableMD5", "dataMD5"]: if i in k: try: _tmp_table[i] = v except Exception: # Not all keys are available. It's okay if we hit a KeyError. pass except Exception as e: print("Error: Unable to collapse: {}, {}".format(dsn, e)) logger_ts.error("collapse_table_root: Unable to collapse: {}, {}, {}".format(_table_name, dsn, e)) return _tmp_table
[ "def", "_collapse_table_root", "(", "current", ",", "dsn", ",", "pc", ")", ":", "logger_ts", ".", "info", "(", "\"enter collapse_table_root\"", ")", "_table_name", ",", "_variable_name", "=", "_get_current_names", "(", "current", ",", "dsn", ",", "pc", ")", "_...
Create a table with items in root given the current time series entry :param dict current: Current time series entry :param str dsn: Dataset name :param str pc: paleoData or chronData :return dict _tmp_table: Table data
[ "Create", "a", "table", "with", "items", "in", "root", "given", "the", "current", "time", "series", "entry", ":", "param", "dict", "current", ":", "Current", "time", "series", "entry", ":", "param", "str", "dsn", ":", "Dataset", "name", ":", "param", "st...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L690-L716
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
_collapse_column
def _collapse_column(current, pc): """ Collapse the column data and :param current: :param pc: :return: """ _tmp_column = {} try: for k, v in current.items(): try: # We do not want to store these table keys at the column level. if not any(i in k for i in ["tableName", "google", "filename", "md5", "MD5"]): # ['paleoData', 'key'] m = k.split('_') # Is this a chronData or paleoData key? if pc in m[0] and len(m) >= 2: # Create a link to the growing column data tmp = _tmp_column # Loop for each key, not including the PC. Start at index 1 for idx, b in enumerate(m[1:]): # Are we at the last item in the list? if idx == len(m) - 2: # Set the value into the column data tmp[b] = v # All loops before the last item else: # Key already exists in the column if b in _tmp_column: # Move into the data structure and keep going tmp = _tmp_column[b] # Key does not exist yet else: # Create the data structure tmp[b] = {} # Move into the new data structure and keep going tmp = tmp[b] except Exception as e: logger_ts.error("collapse_column: loop: {}".format(e)) except Exception as e: logger_ts.error("collapse_column: {}".format(e)) return _tmp_column
python
def _collapse_column(current, pc): """ Collapse the column data and :param current: :param pc: :return: """ _tmp_column = {} try: for k, v in current.items(): try: # We do not want to store these table keys at the column level. if not any(i in k for i in ["tableName", "google", "filename", "md5", "MD5"]): # ['paleoData', 'key'] m = k.split('_') # Is this a chronData or paleoData key? if pc in m[0] and len(m) >= 2: # Create a link to the growing column data tmp = _tmp_column # Loop for each key, not including the PC. Start at index 1 for idx, b in enumerate(m[1:]): # Are we at the last item in the list? if idx == len(m) - 2: # Set the value into the column data tmp[b] = v # All loops before the last item else: # Key already exists in the column if b in _tmp_column: # Move into the data structure and keep going tmp = _tmp_column[b] # Key does not exist yet else: # Create the data structure tmp[b] = {} # Move into the new data structure and keep going tmp = tmp[b] except Exception as e: logger_ts.error("collapse_column: loop: {}".format(e)) except Exception as e: logger_ts.error("collapse_column: {}".format(e)) return _tmp_column
[ "def", "_collapse_column", "(", "current", ",", "pc", ")", ":", "_tmp_column", "=", "{", "}", "try", ":", "for", "k", ",", "v", "in", "current", ".", "items", "(", ")", ":", "try", ":", "# We do not want to store these table keys at the column level.", "if", ...
Collapse the column data and :param current: :param pc: :return:
[ "Collapse", "the", "column", "data", "and", ":", "param", "current", ":", ":", "param", "pc", ":", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L719-L761
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
mode_ts
def mode_ts(ec, mode="", ts=None): """ Get string for the mode :param str ec: extract or collapse :param str mode: "paleo" or "chron" mode :param list ts: Time series (for collapse) :return str phrase: Phrase """ phrase = "" if ec == "extract": if mode=="chron": phrase = "extracting chronData..." else: phrase = "extracting paleoData..." elif ec == "collapse": if ts[0]["mode"] == "chronData": phrase = "collapsing chronData" else: phrase = "collapsing paleoData..." return phrase
python
def mode_ts(ec, mode="", ts=None): """ Get string for the mode :param str ec: extract or collapse :param str mode: "paleo" or "chron" mode :param list ts: Time series (for collapse) :return str phrase: Phrase """ phrase = "" if ec == "extract": if mode=="chron": phrase = "extracting chronData..." else: phrase = "extracting paleoData..." elif ec == "collapse": if ts[0]["mode"] == "chronData": phrase = "collapsing chronData" else: phrase = "collapsing paleoData..." return phrase
[ "def", "mode_ts", "(", "ec", ",", "mode", "=", "\"\"", ",", "ts", "=", "None", ")", ":", "phrase", "=", "\"\"", "if", "ec", "==", "\"extract\"", ":", "if", "mode", "==", "\"chron\"", ":", "phrase", "=", "\"extracting chronData...\"", "else", ":", "phra...
Get string for the mode :param str ec: extract or collapse :param str mode: "paleo" or "chron" mode :param list ts: Time series (for collapse) :return str phrase: Phrase
[ "Get", "string", "for", "the", "mode", ":", "param", "str", "ec", ":", "extract", "or", "collapse", ":", "param", "str", "mode", ":", "paleo", "or", "chron", "mode", ":", "param", "list", "ts", ":", "Time", "series", "(", "for", "collapse", ")", ":",...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L767-L786
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
translate_expression
def translate_expression(expression): """ Check if the expression is valid, then check turn it into an expression that can be used for filtering. :return list of lists: One or more matches. Each list has 3 strings. """ logger_ts.info("enter translate_expression") m = re_filter_expr.findall(expression) matches = [] if m: for i in m: logger_ts.info("parse match: {}".format(i)) tmp = list(i[1:]) if tmp[1] in COMPARISONS: tmp[1] = COMPARISONS[tmp[1]] tmp[0] = cast_float(tmp[0]) tmp[2] = cast_float(tmp[2]) matches.append(tmp) else: logger_ts.warn("translate_expression: invalid expression: {}".format(expression)) print("Invalid input expression") logger_ts.info("exit translate_expression") return matches
python
def translate_expression(expression): """ Check if the expression is valid, then check turn it into an expression that can be used for filtering. :return list of lists: One or more matches. Each list has 3 strings. """ logger_ts.info("enter translate_expression") m = re_filter_expr.findall(expression) matches = [] if m: for i in m: logger_ts.info("parse match: {}".format(i)) tmp = list(i[1:]) if tmp[1] in COMPARISONS: tmp[1] = COMPARISONS[tmp[1]] tmp[0] = cast_float(tmp[0]) tmp[2] = cast_float(tmp[2]) matches.append(tmp) else: logger_ts.warn("translate_expression: invalid expression: {}".format(expression)) print("Invalid input expression") logger_ts.info("exit translate_expression") return matches
[ "def", "translate_expression", "(", "expression", ")", ":", "logger_ts", ".", "info", "(", "\"enter translate_expression\"", ")", "m", "=", "re_filter_expr", ".", "findall", "(", "expression", ")", "matches", "=", "[", "]", "if", "m", ":", "for", "i", "in", ...
Check if the expression is valid, then check turn it into an expression that can be used for filtering. :return list of lists: One or more matches. Each list has 3 strings.
[ "Check", "if", "the", "expression", "is", "valid", "then", "check", "turn", "it", "into", "an", "expression", "that", "can", "be", "used", "for", "filtering", ".", ":", "return", "list", "of", "lists", ":", "One", "or", "more", "matches", ".", "Each", ...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L789-L810
nickmckay/LiPD-utilities
Python/lipd/timeseries.py
get_matches
def get_matches(expr_lst, ts): """ Get a list of TimeSeries objects that match the given expression. :param list expr_lst: Expression :param list ts: TimeSeries :return list new_ts: Matched time series objects :return list idxs: Indices of matched objects """ logger_ts.info("enter get_matches") new_ts = [] idxs = [] match = False try: for idx, ts_data in enumerate(ts): for expr in expr_lst: try: val = ts_data[expr[0]] # Check what comparison operator is being used if expr[1] == 'in': # "IN" operator can't be used in get_truth. Handle first. if expr[2] in val: match = True elif match_operators(val, expr[1], expr[2]): # If it's a typical operator, check with the truth test. match = True else: # If one comparison is false, then it can't possibly be a match match = False break except KeyError as e: logger_ts.warn("get_matches: KeyError: getting value from TimeSeries object, {}, {}".format(expr, e)) match = False except IndexError as e: logger_ts.warn("get_matches: IndexError: getting value from TimeSeries object, {}, {}".format(expr, e)) match = False if match: idxs.append(idx) new_ts.append(ts_data) except AttributeError as e: logger_ts.debug("get_matches: AttributeError: unable to get expression matches, {}, {}".format(type(ts), e)) print("Error: Timeseries is an invalid data type") if not new_ts: print("No matches found for that expression") else: print("Found {} matches from {} columns".format(len(new_ts), len(ts))) logger_ts.info("exit get_matches") return new_ts, idxs
python
def get_matches(expr_lst, ts): """ Get a list of TimeSeries objects that match the given expression. :param list expr_lst: Expression :param list ts: TimeSeries :return list new_ts: Matched time series objects :return list idxs: Indices of matched objects """ logger_ts.info("enter get_matches") new_ts = [] idxs = [] match = False try: for idx, ts_data in enumerate(ts): for expr in expr_lst: try: val = ts_data[expr[0]] # Check what comparison operator is being used if expr[1] == 'in': # "IN" operator can't be used in get_truth. Handle first. if expr[2] in val: match = True elif match_operators(val, expr[1], expr[2]): # If it's a typical operator, check with the truth test. match = True else: # If one comparison is false, then it can't possibly be a match match = False break except KeyError as e: logger_ts.warn("get_matches: KeyError: getting value from TimeSeries object, {}, {}".format(expr, e)) match = False except IndexError as e: logger_ts.warn("get_matches: IndexError: getting value from TimeSeries object, {}, {}".format(expr, e)) match = False if match: idxs.append(idx) new_ts.append(ts_data) except AttributeError as e: logger_ts.debug("get_matches: AttributeError: unable to get expression matches, {}, {}".format(type(ts), e)) print("Error: Timeseries is an invalid data type") if not new_ts: print("No matches found for that expression") else: print("Found {} matches from {} columns".format(len(new_ts), len(ts))) logger_ts.info("exit get_matches") return new_ts, idxs
[ "def", "get_matches", "(", "expr_lst", ",", "ts", ")", ":", "logger_ts", ".", "info", "(", "\"enter get_matches\"", ")", "new_ts", "=", "[", "]", "idxs", "=", "[", "]", "match", "=", "False", "try", ":", "for", "idx", ",", "ts_data", "in", "enumerate",...
Get a list of TimeSeries objects that match the given expression. :param list expr_lst: Expression :param list ts: TimeSeries :return list new_ts: Matched time series objects :return list idxs: Indices of matched objects
[ "Get", "a", "list", "of", "TimeSeries", "objects", "that", "match", "the", "given", "expression", ".", ":", "param", "list", "expr_lst", ":", "Expression", ":", "param", "list", "ts", ":", "TimeSeries", ":", "return", "list", "new_ts", ":", "Matched", "tim...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/timeseries.py#L813-L859
mfussenegger/cr8
cr8/clients.py
_to_http_hosts
def _to_http_hosts(hosts: Union[Iterable[str], str]) -> List[str]: """Convert a string of whitespace or comma separated hosts into a list of hosts. Hosts may also already be a list or other iterable. Each host will be prefixed with 'http://' if it is not already there. >>> _to_http_hosts('n1:4200,n2:4200') ['http://n1:4200', 'http://n2:4200'] >>> _to_http_hosts('n1:4200 n2:4200') ['http://n1:4200', 'http://n2:4200'] >>> _to_http_hosts('https://n1:4200') ['https://n1:4200'] >>> _to_http_hosts(['http://n1:4200', 'n2:4200']) ['http://n1:4200', 'http://n2:4200'] """ if isinstance(hosts, str): hosts = hosts.replace(',', ' ').split() return [_to_http_uri(i) for i in hosts]
python
def _to_http_hosts(hosts: Union[Iterable[str], str]) -> List[str]: """Convert a string of whitespace or comma separated hosts into a list of hosts. Hosts may also already be a list or other iterable. Each host will be prefixed with 'http://' if it is not already there. >>> _to_http_hosts('n1:4200,n2:4200') ['http://n1:4200', 'http://n2:4200'] >>> _to_http_hosts('n1:4200 n2:4200') ['http://n1:4200', 'http://n2:4200'] >>> _to_http_hosts('https://n1:4200') ['https://n1:4200'] >>> _to_http_hosts(['http://n1:4200', 'n2:4200']) ['http://n1:4200', 'http://n2:4200'] """ if isinstance(hosts, str): hosts = hosts.replace(',', ' ').split() return [_to_http_uri(i) for i in hosts]
[ "def", "_to_http_hosts", "(", "hosts", ":", "Union", "[", "Iterable", "[", "str", "]", ",", "str", "]", ")", "->", "List", "[", "str", "]", ":", "if", "isinstance", "(", "hosts", ",", "str", ")", ":", "hosts", "=", "hosts", ".", "replace", "(", "...
Convert a string of whitespace or comma separated hosts into a list of hosts. Hosts may also already be a list or other iterable. Each host will be prefixed with 'http://' if it is not already there. >>> _to_http_hosts('n1:4200,n2:4200') ['http://n1:4200', 'http://n2:4200'] >>> _to_http_hosts('n1:4200 n2:4200') ['http://n1:4200', 'http://n2:4200'] >>> _to_http_hosts('https://n1:4200') ['https://n1:4200'] >>> _to_http_hosts(['http://n1:4200', 'n2:4200']) ['http://n1:4200', 'http://n2:4200']
[ "Convert", "a", "string", "of", "whitespace", "or", "comma", "separated", "hosts", "into", "a", "list", "of", "hosts", "." ]
train
https://github.com/mfussenegger/cr8/blob/a37d6049f1f9fee2d0556efae2b7b7f8761bffe8/cr8/clients.py#L59-L79
mfussenegger/cr8
cr8/clients.py
_plain_or_callable
def _plain_or_callable(obj): """Returns the value of the called object of obj is a callable, otherwise the plain object. Returns None if obj is None. >>> obj = None >>> _plain_or_callable(obj) >>> stmt = 'select * from sys.nodes' >>> _plain_or_callable(stmt) 'select * from sys.nodes' >>> def _args(): ... return [1, 'name'] >>> _plain_or_callable(_args) [1, 'name'] >>> _plain_or_callable((x for x in range(10))) 0 >>> class BulkArgsGenerator: ... def __call__(self): ... return [[1, 'foo'], [2, 'bar'], [3, 'foobar']] >>> _plain_or_callable(BulkArgsGenerator()) [[1, 'foo'], [2, 'bar'], [3, 'foobar']] """ if callable(obj): return obj() elif isinstance(obj, types.GeneratorType): return next(obj) else: return obj
python
def _plain_or_callable(obj): """Returns the value of the called object of obj is a callable, otherwise the plain object. Returns None if obj is None. >>> obj = None >>> _plain_or_callable(obj) >>> stmt = 'select * from sys.nodes' >>> _plain_or_callable(stmt) 'select * from sys.nodes' >>> def _args(): ... return [1, 'name'] >>> _plain_or_callable(_args) [1, 'name'] >>> _plain_or_callable((x for x in range(10))) 0 >>> class BulkArgsGenerator: ... def __call__(self): ... return [[1, 'foo'], [2, 'bar'], [3, 'foobar']] >>> _plain_or_callable(BulkArgsGenerator()) [[1, 'foo'], [2, 'bar'], [3, 'foobar']] """ if callable(obj): return obj() elif isinstance(obj, types.GeneratorType): return next(obj) else: return obj
[ "def", "_plain_or_callable", "(", "obj", ")", ":", "if", "callable", "(", "obj", ")", ":", "return", "obj", "(", ")", "elif", "isinstance", "(", "obj", ",", "types", ".", "GeneratorType", ")", ":", "return", "next", "(", "obj", ")", "else", ":", "ret...
Returns the value of the called object of obj is a callable, otherwise the plain object. Returns None if obj is None. >>> obj = None >>> _plain_or_callable(obj) >>> stmt = 'select * from sys.nodes' >>> _plain_or_callable(stmt) 'select * from sys.nodes' >>> def _args(): ... return [1, 'name'] >>> _plain_or_callable(_args) [1, 'name'] >>> _plain_or_callable((x for x in range(10))) 0 >>> class BulkArgsGenerator: ... def __call__(self): ... return [[1, 'foo'], [2, 'bar'], [3, 'foobar']] >>> _plain_or_callable(BulkArgsGenerator()) [[1, 'foo'], [2, 'bar'], [3, 'foobar']]
[ "Returns", "the", "value", "of", "the", "called", "object", "of", "obj", "is", "a", "callable", "otherwise", "the", "plain", "object", ".", "Returns", "None", "if", "obj", "is", "None", "." ]
train
https://github.com/mfussenegger/cr8/blob/a37d6049f1f9fee2d0556efae2b7b7f8761bffe8/cr8/clients.py#L97-L128
mfussenegger/cr8
cr8/clients.py
_to_dsn
def _to_dsn(hosts): """Convert a host URI into a dsn for aiopg. >>> _to_dsn('aiopg://myhostname:4242/mydb') 'postgres://crate@myhostname:4242/mydb' >>> _to_dsn('aiopg://myhostname:4242') 'postgres://crate@myhostname:4242/doc' >>> _to_dsn('aiopg://hoschi:pw@myhostname:4242/doc?sslmode=require') 'postgres://hoschi:pw@myhostname:4242/doc?sslmode=require' >>> _to_dsn('aiopg://myhostname') 'postgres://crate@myhostname:5432/doc' """ p = urlparse(hosts) try: user_and_pw, netloc = p.netloc.split('@', maxsplit=1) except ValueError: netloc = p.netloc user_and_pw = 'crate' try: host, port = netloc.split(':', maxsplit=1) except ValueError: host = netloc port = 5432 dbname = p.path[1:] if p.path else 'doc' dsn = f'postgres://{user_and_pw}@{host}:{port}/{dbname}' if p.query: dsn += '?' + '&'.join(k + '=' + v[0] for k, v in parse_qs(p.query).items()) return dsn
python
def _to_dsn(hosts): """Convert a host URI into a dsn for aiopg. >>> _to_dsn('aiopg://myhostname:4242/mydb') 'postgres://crate@myhostname:4242/mydb' >>> _to_dsn('aiopg://myhostname:4242') 'postgres://crate@myhostname:4242/doc' >>> _to_dsn('aiopg://hoschi:pw@myhostname:4242/doc?sslmode=require') 'postgres://hoschi:pw@myhostname:4242/doc?sslmode=require' >>> _to_dsn('aiopg://myhostname') 'postgres://crate@myhostname:5432/doc' """ p = urlparse(hosts) try: user_and_pw, netloc = p.netloc.split('@', maxsplit=1) except ValueError: netloc = p.netloc user_and_pw = 'crate' try: host, port = netloc.split(':', maxsplit=1) except ValueError: host = netloc port = 5432 dbname = p.path[1:] if p.path else 'doc' dsn = f'postgres://{user_and_pw}@{host}:{port}/{dbname}' if p.query: dsn += '?' + '&'.join(k + '=' + v[0] for k, v in parse_qs(p.query).items()) return dsn
[ "def", "_to_dsn", "(", "hosts", ")", ":", "p", "=", "urlparse", "(", "hosts", ")", "try", ":", "user_and_pw", ",", "netloc", "=", "p", ".", "netloc", ".", "split", "(", "'@'", ",", "maxsplit", "=", "1", ")", "except", "ValueError", ":", "netloc", "...
Convert a host URI into a dsn for aiopg. >>> _to_dsn('aiopg://myhostname:4242/mydb') 'postgres://crate@myhostname:4242/mydb' >>> _to_dsn('aiopg://myhostname:4242') 'postgres://crate@myhostname:4242/doc' >>> _to_dsn('aiopg://hoschi:pw@myhostname:4242/doc?sslmode=require') 'postgres://hoschi:pw@myhostname:4242/doc?sslmode=require' >>> _to_dsn('aiopg://myhostname') 'postgres://crate@myhostname:5432/doc'
[ "Convert", "a", "host", "URI", "into", "a", "dsn", "for", "aiopg", "." ]
train
https://github.com/mfussenegger/cr8/blob/a37d6049f1f9fee2d0556efae2b7b7f8761bffe8/cr8/clients.py#L146-L176
mfussenegger/cr8
cr8/clients.py
_verify_ssl_from_first
def _verify_ssl_from_first(hosts): """Check if SSL validation parameter is passed in URI >>> _verify_ssl_from_first(['https://myhost:4200/?verify_ssl=false']) False >>> _verify_ssl_from_first(['https://myhost:4200/']) True >>> _verify_ssl_from_first([ ... 'https://h1:4200/?verify_ssl=False', ... 'https://h2:4200/?verify_ssl=True' ... ]) False """ for host in hosts: query = parse_qs(urlparse(host).query) if 'verify_ssl' in query: return _to_boolean(query['verify_ssl'][0]) return True
python
def _verify_ssl_from_first(hosts): """Check if SSL validation parameter is passed in URI >>> _verify_ssl_from_first(['https://myhost:4200/?verify_ssl=false']) False >>> _verify_ssl_from_first(['https://myhost:4200/']) True >>> _verify_ssl_from_first([ ... 'https://h1:4200/?verify_ssl=False', ... 'https://h2:4200/?verify_ssl=True' ... ]) False """ for host in hosts: query = parse_qs(urlparse(host).query) if 'verify_ssl' in query: return _to_boolean(query['verify_ssl'][0]) return True
[ "def", "_verify_ssl_from_first", "(", "hosts", ")", ":", "for", "host", "in", "hosts", ":", "query", "=", "parse_qs", "(", "urlparse", "(", "host", ")", ".", "query", ")", "if", "'verify_ssl'", "in", "query", ":", "return", "_to_boolean", "(", "query", "...
Check if SSL validation parameter is passed in URI >>> _verify_ssl_from_first(['https://myhost:4200/?verify_ssl=false']) False >>> _verify_ssl_from_first(['https://myhost:4200/']) True >>> _verify_ssl_from_first([ ... 'https://h1:4200/?verify_ssl=False', ... 'https://h2:4200/?verify_ssl=True' ... ]) False
[ "Check", "if", "SSL", "validation", "parameter", "is", "passed", "in", "URI" ]
train
https://github.com/mfussenegger/cr8/blob/a37d6049f1f9fee2d0556efae2b7b7f8761bffe8/cr8/clients.py#L188-L207
nickmckay/LiPD-utilities
Python/lipd/tables.py
addTable
def addTable(D): """ Add any table type to the given dataset. Use prompts to determine index locations and table type. :param dict D: Metadata (dataset) :param dict dat: Metadata (table) :return dict D: Metadata (dataset) """ _swap = { "1": "measurement", "2": "summary", "3": "ensemble", "4": "distribution" } print("What type of table would you like to add?\n" "1: measurement\n" "2: summary\n" "3: ensemble (under development)\n" "4: distribution (under development)\n" "\n Note: if you want to add a whole model, use the addModel() function") _ans = input(">") if _ans in ["3", "4"]: print("I don't know how to do that yet.") # if this is a summary or measurement, split the csv into each column elif _ans in ["1", "2"]: # read in a csv file. have the user point to it print("Locate the CSV file with the values for this table: ") _path, _files = browse_dialog_file() _path = _confirm_file_path(_files) _values = read_csv_from_file(_path) _table = _build_table(_values) _placement = _prompt_placement(D, _swap[_ans]) D = _put_table(D, _placement, _table) else: print("That's not a valid option") return D
python
def addTable(D): """ Add any table type to the given dataset. Use prompts to determine index locations and table type. :param dict D: Metadata (dataset) :param dict dat: Metadata (table) :return dict D: Metadata (dataset) """ _swap = { "1": "measurement", "2": "summary", "3": "ensemble", "4": "distribution" } print("What type of table would you like to add?\n" "1: measurement\n" "2: summary\n" "3: ensemble (under development)\n" "4: distribution (under development)\n" "\n Note: if you want to add a whole model, use the addModel() function") _ans = input(">") if _ans in ["3", "4"]: print("I don't know how to do that yet.") # if this is a summary or measurement, split the csv into each column elif _ans in ["1", "2"]: # read in a csv file. have the user point to it print("Locate the CSV file with the values for this table: ") _path, _files = browse_dialog_file() _path = _confirm_file_path(_files) _values = read_csv_from_file(_path) _table = _build_table(_values) _placement = _prompt_placement(D, _swap[_ans]) D = _put_table(D, _placement, _table) else: print("That's not a valid option") return D
[ "def", "addTable", "(", "D", ")", ":", "_swap", "=", "{", "\"1\"", ":", "\"measurement\"", ",", "\"2\"", ":", "\"summary\"", ",", "\"3\"", ":", "\"ensemble\"", ",", "\"4\"", ":", "\"distribution\"", "}", "print", "(", "\"What type of table would you like to add?...
Add any table type to the given dataset. Use prompts to determine index locations and table type. :param dict D: Metadata (dataset) :param dict dat: Metadata (table) :return dict D: Metadata (dataset)
[ "Add", "any", "table", "type", "to", "the", "given", "dataset", ".", "Use", "prompts", "to", "determine", "index", "locations", "and", "table", "type", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/tables.py#L13-L55
nickmckay/LiPD-utilities
Python/lipd/tables.py
_get_available_placements
def _get_available_placements(D, tt): """ Called from: _prompt_placement() Get a list of possible places that we can put the new model data into. If no model exists yet, we'll use something like chron0model0. If other models exist, we'll go for the n+1 entry. ex: chron0model0 already exists, so we'll look to chron0model1 next. :param dict D: Metadata :param str tt: Table Type :return list _options: Possible placements """ _options = [] try: for _pc in ["paleoData", "chronData"]: if _pc in D: # for each entry in pc for section_name, section_data in D[_pc].items(): # looking for open spots for measurement tables if tt == "measurement": if "measurementTable" in section_data: _options.append(_get_available_placements_1(section_data["measurementTable"], section_name, "measurement")) # looking for open spots for model tables else: # Is there a model? Need model data to keep going if "model" in section_data: # this is for adding a whole model (all 4 tables, ens/dist/sum/method) if tt == "model": _options.append(_get_available_placements_1(section_data["model"], section_name, "model")) else: # for adding individual model tables for _k, _v in section_data["model"]: # keys here are stored as "<type>Table", so add "Table" to each table type _tt_table = "{}Table".format(tt) # does this table exist? if _tt_table in _v: # Get the first available position for this section _options.append( _get_available_placements_1(_v[_tt_table], _k, tt)) else: # Doesn't currently exist. Make the first option index 0. _options.append("{}{}0".format(_k, tt)) # no models present, so we automatically default placement options to the 0 index. else: if tt == "model": # adding a whole model, so no need to be specific _options.append("{}model0".format(section_name)) else: # adding a specific table, so the position is more specific also _options.append("{}model0{}0".format(section_name, tt)) except Exception as e: sys.exit("Looking for open table positions: Unable to find placement options, {}".format(e)) # remove empty names _options = [i for i in _options if i] # Is the whole list empty? that's not good. if not _options: sys.exit("Error: No available positions found to place new data. Something went wrong.") return _options
python
def _get_available_placements(D, tt): """ Called from: _prompt_placement() Get a list of possible places that we can put the new model data into. If no model exists yet, we'll use something like chron0model0. If other models exist, we'll go for the n+1 entry. ex: chron0model0 already exists, so we'll look to chron0model1 next. :param dict D: Metadata :param str tt: Table Type :return list _options: Possible placements """ _options = [] try: for _pc in ["paleoData", "chronData"]: if _pc in D: # for each entry in pc for section_name, section_data in D[_pc].items(): # looking for open spots for measurement tables if tt == "measurement": if "measurementTable" in section_data: _options.append(_get_available_placements_1(section_data["measurementTable"], section_name, "measurement")) # looking for open spots for model tables else: # Is there a model? Need model data to keep going if "model" in section_data: # this is for adding a whole model (all 4 tables, ens/dist/sum/method) if tt == "model": _options.append(_get_available_placements_1(section_data["model"], section_name, "model")) else: # for adding individual model tables for _k, _v in section_data["model"]: # keys here are stored as "<type>Table", so add "Table" to each table type _tt_table = "{}Table".format(tt) # does this table exist? if _tt_table in _v: # Get the first available position for this section _options.append( _get_available_placements_1(_v[_tt_table], _k, tt)) else: # Doesn't currently exist. Make the first option index 0. _options.append("{}{}0".format(_k, tt)) # no models present, so we automatically default placement options to the 0 index. else: if tt == "model": # adding a whole model, so no need to be specific _options.append("{}model0".format(section_name)) else: # adding a specific table, so the position is more specific also _options.append("{}model0{}0".format(section_name, tt)) except Exception as e: sys.exit("Looking for open table positions: Unable to find placement options, {}".format(e)) # remove empty names _options = [i for i in _options if i] # Is the whole list empty? that's not good. if not _options: sys.exit("Error: No available positions found to place new data. Something went wrong.") return _options
[ "def", "_get_available_placements", "(", "D", ",", "tt", ")", ":", "_options", "=", "[", "]", "try", ":", "for", "_pc", "in", "[", "\"paleoData\"", ",", "\"chronData\"", "]", ":", "if", "_pc", "in", "D", ":", "# for each entry in pc", "for", "section_name"...
Called from: _prompt_placement() Get a list of possible places that we can put the new model data into. If no model exists yet, we'll use something like chron0model0. If other models exist, we'll go for the n+1 entry. ex: chron0model0 already exists, so we'll look to chron0model1 next. :param dict D: Metadata :param str tt: Table Type :return list _options: Possible placements
[ "Called", "from", ":", "_prompt_placement", "()" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/tables.py#L99-L165
nickmckay/LiPD-utilities
Python/lipd/tables.py
_prompt_placement
def _prompt_placement(D, tt): """ Since automatic placement didn't work, find somewhere to place the model data manually with the help of the user. :param dict D: Metadata :param str tt: Table type :return str _model_name: Chosen model name for placement """ _model_name = "" # There wasn't a table name match, so we need prompts to fix it _placement_options = _get_available_placements(D, tt) print("Please choose where you'd like to place this model:") for _idx, _opt in enumerate(_placement_options): print("({}) {}".format(_idx, _opt)) _choice = input("> ") try: if int(_choice) <= len(_placement_options) and _choice: # Get the option the user chose _model_name = _placement_options[int(_choice)] else: # They user chose an option out of the placement list range print("Invalid choice input") return except Exception as e: # Choice was not a number or empty print("Invalid choice") return _model_name
python
def _prompt_placement(D, tt): """ Since automatic placement didn't work, find somewhere to place the model data manually with the help of the user. :param dict D: Metadata :param str tt: Table type :return str _model_name: Chosen model name for placement """ _model_name = "" # There wasn't a table name match, so we need prompts to fix it _placement_options = _get_available_placements(D, tt) print("Please choose where you'd like to place this model:") for _idx, _opt in enumerate(_placement_options): print("({}) {}".format(_idx, _opt)) _choice = input("> ") try: if int(_choice) <= len(_placement_options) and _choice: # Get the option the user chose _model_name = _placement_options[int(_choice)] else: # They user chose an option out of the placement list range print("Invalid choice input") return except Exception as e: # Choice was not a number or empty print("Invalid choice") return _model_name
[ "def", "_prompt_placement", "(", "D", ",", "tt", ")", ":", "_model_name", "=", "\"\"", "# There wasn't a table name match, so we need prompts to fix it", "_placement_options", "=", "_get_available_placements", "(", "D", ",", "tt", ")", "print", "(", "\"Please choose where...
Since automatic placement didn't work, find somewhere to place the model data manually with the help of the user. :param dict D: Metadata :param str tt: Table type :return str _model_name: Chosen model name for placement
[ "Since", "automatic", "placement", "didn", "t", "work", "find", "somewhere", "to", "place", "the", "model", "data", "manually", "with", "the", "help", "of", "the", "user", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/tables.py#L178-L205
nickmckay/LiPD-utilities
Python/lipd/tables.py
_put_table
def _put_table(D, name, table): """ Use the dataset and name to place the new table data into the dataset. :param dict D: Dataset :param str name: Table name / path to store new table :param dict table: Newly created table data :return dict D: Dataset """ try: # print("Placing table: {}".format(name)) table["tableName"] = name m = re.match(re_table_name, name) if m: _pc = m.group(1) + "Data" _section = m.group(1) + m.group(2) # place a measurement table if m.group(3) == "measurement": # This shouldn't happen. User chose one of our options. That should be an empty location. if name in D[_pc][_section]["measurementTable"]: print("Oops. This shouldn't happen. That table path is occupied in the dataset") # Place the data else: D[_pc][_section]["measurementTable"][name] = table # place a model table type else: _model = _section + m.group(3) + m.group(4) _tt = m.group(5) + "Table" if name in D[_pc][_model][_tt]: print("Oops. This shouldn't happen. That table path is occupied in the dataset") else: D[_pc][_model][_tt][name] = table else: print("Oops. This shouldn't happen. That table name doesn't look right. Please report this error") return except Exception as e: print("addTable: Unable to put the table data into the dataset, {}".format(e)) return D
python
def _put_table(D, name, table): """ Use the dataset and name to place the new table data into the dataset. :param dict D: Dataset :param str name: Table name / path to store new table :param dict table: Newly created table data :return dict D: Dataset """ try: # print("Placing table: {}".format(name)) table["tableName"] = name m = re.match(re_table_name, name) if m: _pc = m.group(1) + "Data" _section = m.group(1) + m.group(2) # place a measurement table if m.group(3) == "measurement": # This shouldn't happen. User chose one of our options. That should be an empty location. if name in D[_pc][_section]["measurementTable"]: print("Oops. This shouldn't happen. That table path is occupied in the dataset") # Place the data else: D[_pc][_section]["measurementTable"][name] = table # place a model table type else: _model = _section + m.group(3) + m.group(4) _tt = m.group(5) + "Table" if name in D[_pc][_model][_tt]: print("Oops. This shouldn't happen. That table path is occupied in the dataset") else: D[_pc][_model][_tt][name] = table else: print("Oops. This shouldn't happen. That table name doesn't look right. Please report this error") return except Exception as e: print("addTable: Unable to put the table data into the dataset, {}".format(e)) return D
[ "def", "_put_table", "(", "D", ",", "name", ",", "table", ")", ":", "try", ":", "# print(\"Placing table: {}\".format(name))", "table", "[", "\"tableName\"", "]", "=", "name", "m", "=", "re", ".", "match", "(", "re_table_name", ",", "name", ")", "if", "m",...
Use the dataset and name to place the new table data into the dataset. :param dict D: Dataset :param str name: Table name / path to store new table :param dict table: Newly created table data :return dict D: Dataset
[ "Use", "the", "dataset", "and", "name", "to", "place", "the", "new", "table", "data", "into", "the", "dataset", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/tables.py#L208-L249
nickmckay/LiPD-utilities
Python/lipd/tables.py
_update_table_names
def _update_table_names(name, dat): """ Model placement is subject to change. That means all names within the model (names are path-dependent) are also subject to change. Whichever name is decided, the inner data needs to match it. :param dict dat: Metadata :param str name: Table name :return dict dat: Metadata """ for _tabletype in ["summary", "distribution", "ensemble"]: _ttname = "{}Table".format(_tabletype) if _ttname in dat: _new_tables = OrderedDict() _idx = 0 # change all the top level table names for k,v in dat[_ttname].items(): _new_ttname= "{}{}{}".format(name, _tabletype, _idx) _idx +=1 #change all the table names in the table metadata v["tableName"] = _new_ttname # remove the filename. It shouldn't be stored anyway if "filename" in v: v["filename"] = "" # place dat into the new ordered dictionary _new_tables[_new_ttname] = v # place new tables into the original dat dat[_ttname] = _new_tables return dat
python
def _update_table_names(name, dat): """ Model placement is subject to change. That means all names within the model (names are path-dependent) are also subject to change. Whichever name is decided, the inner data needs to match it. :param dict dat: Metadata :param str name: Table name :return dict dat: Metadata """ for _tabletype in ["summary", "distribution", "ensemble"]: _ttname = "{}Table".format(_tabletype) if _ttname in dat: _new_tables = OrderedDict() _idx = 0 # change all the top level table names for k,v in dat[_ttname].items(): _new_ttname= "{}{}{}".format(name, _tabletype, _idx) _idx +=1 #change all the table names in the table metadata v["tableName"] = _new_ttname # remove the filename. It shouldn't be stored anyway if "filename" in v: v["filename"] = "" # place dat into the new ordered dictionary _new_tables[_new_ttname] = v # place new tables into the original dat dat[_ttname] = _new_tables return dat
[ "def", "_update_table_names", "(", "name", ",", "dat", ")", ":", "for", "_tabletype", "in", "[", "\"summary\"", ",", "\"distribution\"", ",", "\"ensemble\"", "]", ":", "_ttname", "=", "\"{}Table\"", ".", "format", "(", "_tabletype", ")", "if", "_ttname", "in...
Model placement is subject to change. That means all names within the model (names are path-dependent) are also subject to change. Whichever name is decided, the inner data needs to match it. :param dict dat: Metadata :param str name: Table name :return dict dat: Metadata
[ "Model", "placement", "is", "subject", "to", "change", ".", "That", "means", "all", "names", "within", "the", "model", "(", "names", "are", "path", "-", "dependent", ")", "are", "also", "subject", "to", "change", ".", "Whichever", "name", "is", "decided", ...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/tables.py#L254-L283
nickmckay/LiPD-utilities
Python/lipd/tables.py
addModel
def addModel(D, models): """ Insert model data into a LiPD dataset Examples of model naming: chron0model0 chron0model1 chron1model0 Example of 'models' variable: { chron0model0: { "method": {...}, "summaryTable": [...], "ensembleTable": [...], "distributionTable: [...] }, chron0model1:... } :param dict D: Metadata (dataset) :param dict models: Model data to add :return dict D: Metadata (dataset) """ try: # Loop for each model that needs to be added for _model_name, _model_data in models.items(): # split the table name into a path that we can use _m = re.match(re_model_name, _model_name) if _m: D = _put_model(D, _model_name, _model_data, _m) else: print("The table name found in the given model data isn't valid for automatic placement") _placement_name = _prompt_placement(D, "model") _m = re.match(re_model_name, _placement_name) if _m: D = _put_model(D, _placement_name, _model_data, _m) else: print("Oops. This shouldn't happen. That table name doesn't look right. Please report this error") return except Exception as e: print("addModel: Model data NOT added, {}".format(e)) return D
python
def addModel(D, models): """ Insert model data into a LiPD dataset Examples of model naming: chron0model0 chron0model1 chron1model0 Example of 'models' variable: { chron0model0: { "method": {...}, "summaryTable": [...], "ensembleTable": [...], "distributionTable: [...] }, chron0model1:... } :param dict D: Metadata (dataset) :param dict models: Model data to add :return dict D: Metadata (dataset) """ try: # Loop for each model that needs to be added for _model_name, _model_data in models.items(): # split the table name into a path that we can use _m = re.match(re_model_name, _model_name) if _m: D = _put_model(D, _model_name, _model_data, _m) else: print("The table name found in the given model data isn't valid for automatic placement") _placement_name = _prompt_placement(D, "model") _m = re.match(re_model_name, _placement_name) if _m: D = _put_model(D, _placement_name, _model_data, _m) else: print("Oops. This shouldn't happen. That table name doesn't look right. Please report this error") return except Exception as e: print("addModel: Model data NOT added, {}".format(e)) return D
[ "def", "addModel", "(", "D", ",", "models", ")", ":", "try", ":", "# Loop for each model that needs to be added", "for", "_model_name", ",", "_model_data", "in", "models", ".", "items", "(", ")", ":", "# split the table name into a path that we can use", "_m", "=", ...
Insert model data into a LiPD dataset Examples of model naming: chron0model0 chron0model1 chron1model0 Example of 'models' variable: { chron0model0: { "method": {...}, "summaryTable": [...], "ensembleTable": [...], "distributionTable: [...] }, chron0model1:... } :param dict D: Metadata (dataset) :param dict models: Model data to add :return dict D: Metadata (dataset)
[ "Insert", "model", "data", "into", "a", "LiPD", "dataset" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/tables.py#L289-L333
nickmckay/LiPD-utilities
Python/lipd/tables.py
_put_model
def _put_model(D, name, dat, m): """ Place the model data given, into the location (m) given. :param dict D: Metadata (dataset) :param str name: Model name (ex: chron0model0) :param dict dat: Model data :param regex m: Model name regex groups :return dict D: Metadata (dataset) """ try: # print("Placing model: {}".format(name)) _pc = m.group(1) + "Data" _section = m.group(1) + m.group(2) if _pc not in D: # Section missing entirely? Can't continue print("{} not found in the provided dataset. Please try again".format(_pc)) return else: if _section not in D[_pc]: # Creates section: Example: D[chronData][chron0] D[_pc][_section] = OrderedDict() if "model" not in D[_pc][_section]: # Creates model top level: Example: D[chronData][chron0]["model"] D[_pc][_section]["model"] = OrderedDict() if name not in D[_pc][_section]["model"]: dat = _update_table_names(name, dat) D[_pc][_section]["model"][name] = dat else: # Model already exists, should we overwrite it? _prompt_overwrite = input( "This model already exists in the dataset. Do you want to overwrite it? (y/n)") # Yes, overwrite with the model data provided if _prompt_overwrite == "y": dat = _update_table_names(name, dat) D[_pc][_section]["model"][name] = dat # No, do not overwrite. elif _prompt_overwrite == "n": _name2 = _prompt_placement(D, "model") _m = re.match(re_model_name, _name2) if _m: D = _put_model(D, _name2, dat, _m) else: print("Invalid choice") except Exception as e: print("addModel: Unable to put the model data into the dataset, {}".format(e)) return D
python
def _put_model(D, name, dat, m): """ Place the model data given, into the location (m) given. :param dict D: Metadata (dataset) :param str name: Model name (ex: chron0model0) :param dict dat: Model data :param regex m: Model name regex groups :return dict D: Metadata (dataset) """ try: # print("Placing model: {}".format(name)) _pc = m.group(1) + "Data" _section = m.group(1) + m.group(2) if _pc not in D: # Section missing entirely? Can't continue print("{} not found in the provided dataset. Please try again".format(_pc)) return else: if _section not in D[_pc]: # Creates section: Example: D[chronData][chron0] D[_pc][_section] = OrderedDict() if "model" not in D[_pc][_section]: # Creates model top level: Example: D[chronData][chron0]["model"] D[_pc][_section]["model"] = OrderedDict() if name not in D[_pc][_section]["model"]: dat = _update_table_names(name, dat) D[_pc][_section]["model"][name] = dat else: # Model already exists, should we overwrite it? _prompt_overwrite = input( "This model already exists in the dataset. Do you want to overwrite it? (y/n)") # Yes, overwrite with the model data provided if _prompt_overwrite == "y": dat = _update_table_names(name, dat) D[_pc][_section]["model"][name] = dat # No, do not overwrite. elif _prompt_overwrite == "n": _name2 = _prompt_placement(D, "model") _m = re.match(re_model_name, _name2) if _m: D = _put_model(D, _name2, dat, _m) else: print("Invalid choice") except Exception as e: print("addModel: Unable to put the model data into the dataset, {}".format(e)) return D
[ "def", "_put_model", "(", "D", ",", "name", ",", "dat", ",", "m", ")", ":", "try", ":", "# print(\"Placing model: {}\".format(name))", "_pc", "=", "m", ".", "group", "(", "1", ")", "+", "\"Data\"", "_section", "=", "m", ".", "group", "(", "1", ")", "...
Place the model data given, into the location (m) given. :param dict D: Metadata (dataset) :param str name: Model name (ex: chron0model0) :param dict dat: Model data :param regex m: Model name regex groups :return dict D: Metadata (dataset)
[ "Place", "the", "model", "data", "given", "into", "the", "location", "(", "m", ")", "given", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/tables.py#L337-L384
useblocks/sphinxcontrib-needs
sphinxcontrib/needs/needs.py
prepare_env
def prepare_env(app, env, docname): """ Prepares the sphinx environment to store sphinx-needs internal data. """ if not hasattr(env, 'needs_all_needs'): # Used to store all needed information about all needs in document env.needs_all_needs = {} if not hasattr(env, 'needs_functions'): # Used to store all registered functions for supporting dynamic need values. env.needs_functions = {} # needs_functions = getattr(app.config, 'needs_functions', []) needs_functions = app.needs_functions if needs_functions is None: needs_functions = [] if not isinstance(needs_functions, list): raise SphinxError('Config parameter needs_functions must be a list!') # Register built-in functions for need_common_func in needs_common_functions: register_func(env, need_common_func) # Register functions configured by user for needs_func in needs_functions: register_func(env, needs_func) app.config.needs_hide_options += ['hidden'] app.config.needs_extra_options['hidden'] = directives.unchanged if not hasattr(env, 'needs_workflow'): # Used to store workflow status information for already executed tasks. # Some tasks like backlink_creation need be be performed only once. # But most sphinx-events get called several times (for each single document file), which would also # execute our code several times... env.needs_workflow = { 'backlink_creation': False, 'dynamic_values_resolved': False }
python
def prepare_env(app, env, docname): """ Prepares the sphinx environment to store sphinx-needs internal data. """ if not hasattr(env, 'needs_all_needs'): # Used to store all needed information about all needs in document env.needs_all_needs = {} if not hasattr(env, 'needs_functions'): # Used to store all registered functions for supporting dynamic need values. env.needs_functions = {} # needs_functions = getattr(app.config, 'needs_functions', []) needs_functions = app.needs_functions if needs_functions is None: needs_functions = [] if not isinstance(needs_functions, list): raise SphinxError('Config parameter needs_functions must be a list!') # Register built-in functions for need_common_func in needs_common_functions: register_func(env, need_common_func) # Register functions configured by user for needs_func in needs_functions: register_func(env, needs_func) app.config.needs_hide_options += ['hidden'] app.config.needs_extra_options['hidden'] = directives.unchanged if not hasattr(env, 'needs_workflow'): # Used to store workflow status information for already executed tasks. # Some tasks like backlink_creation need be be performed only once. # But most sphinx-events get called several times (for each single document file), which would also # execute our code several times... env.needs_workflow = { 'backlink_creation': False, 'dynamic_values_resolved': False }
[ "def", "prepare_env", "(", "app", ",", "env", ",", "docname", ")", ":", "if", "not", "hasattr", "(", "env", ",", "'needs_all_needs'", ")", ":", "# Used to store all needed information about all needs in document", "env", ".", "needs_all_needs", "=", "{", "}", "if"...
Prepares the sphinx environment to store sphinx-needs internal data.
[ "Prepares", "the", "sphinx", "environment", "to", "store", "sphinx", "-", "needs", "internal", "data", "." ]
train
https://github.com/useblocks/sphinxcontrib-needs/blob/f49af4859a74e9fe76de5b9133c01335ac6ae191/sphinxcontrib/needs/needs.py#L311-L349
useblocks/sphinxcontrib-needs
sphinxcontrib/needs/directives/needflow.py
make_entity_name
def make_entity_name(name): """Creates a valid PlantUML entity name from the given value.""" invalid_chars = "-=!#$%^&*[](){}/~'`<>:;" for char in invalid_chars: name = name.replace(char, "_") return name
python
def make_entity_name(name): """Creates a valid PlantUML entity name from the given value.""" invalid_chars = "-=!#$%^&*[](){}/~'`<>:;" for char in invalid_chars: name = name.replace(char, "_") return name
[ "def", "make_entity_name", "(", "name", ")", ":", "invalid_chars", "=", "\"-=!#$%^&*[](){}/~'`<>:;\"", "for", "char", "in", "invalid_chars", ":", "name", "=", "name", ".", "replace", "(", "char", ",", "\"_\"", ")", "return", "name" ]
Creates a valid PlantUML entity name from the given value.
[ "Creates", "a", "valid", "PlantUML", "entity", "name", "from", "the", "given", "value", "." ]
train
https://github.com/useblocks/sphinxcontrib-needs/blob/f49af4859a74e9fe76de5b9133c01335ac6ae191/sphinxcontrib/needs/directives/needflow.py#L63-L68
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.main
def main(self): """ Load in the template file, and run through the parser :return none: """ logger_lpd_noaa.info("enter main") # Starting Directory: dir_tmp/dir_bag/data/ # convert all lipd keys to noaa keys # timestamp the conversion of the file # MISC SETUP FUNCTIONS self.noaa_data_sorted["File_Last_Modified_Date"]["Modified_Date"] = generate_timestamp() self.__get_table_count() # Get measurement tables from metadata, and sort into object self self.__put_tables_in_self(["paleo", "paleoData", "measurementTable"]) self.__put_tables_in_self(["chron", "chronData", "measurementTable"]) # how many measurement tables exist? this will tell use how many noaa files to create self.__get_table_pairs() # reorganize data into noaa sections self.__reorganize() # special case: earliest_year, most_recent_year, and time unit # self.__check_time_values() # self.__check_time_unit() self.__get_overall_data(self.lipd_data) self.__reorganize_sensor() self.__lists_to_str() self.__generate_study_name() # END MISC SETUP FUNCTIONS # Use data in steps_dict to write to # self.noaa_data_sorted = self.__key_conversion(self.noaa_data_sorted) self.__create_file() logger_lpd_noaa.info("exit main") return
python
def main(self): """ Load in the template file, and run through the parser :return none: """ logger_lpd_noaa.info("enter main") # Starting Directory: dir_tmp/dir_bag/data/ # convert all lipd keys to noaa keys # timestamp the conversion of the file # MISC SETUP FUNCTIONS self.noaa_data_sorted["File_Last_Modified_Date"]["Modified_Date"] = generate_timestamp() self.__get_table_count() # Get measurement tables from metadata, and sort into object self self.__put_tables_in_self(["paleo", "paleoData", "measurementTable"]) self.__put_tables_in_self(["chron", "chronData", "measurementTable"]) # how many measurement tables exist? this will tell use how many noaa files to create self.__get_table_pairs() # reorganize data into noaa sections self.__reorganize() # special case: earliest_year, most_recent_year, and time unit # self.__check_time_values() # self.__check_time_unit() self.__get_overall_data(self.lipd_data) self.__reorganize_sensor() self.__lists_to_str() self.__generate_study_name() # END MISC SETUP FUNCTIONS # Use data in steps_dict to write to # self.noaa_data_sorted = self.__key_conversion(self.noaa_data_sorted) self.__create_file() logger_lpd_noaa.info("exit main") return
[ "def", "main", "(", "self", ")", ":", "logger_lpd_noaa", ".", "info", "(", "\"enter main\"", ")", "# Starting Directory: dir_tmp/dir_bag/data/", "# convert all lipd keys to noaa keys", "# timestamp the conversion of the file", "# MISC SETUP FUNCTIONS", "self", ".", "noaa_data_sor...
Load in the template file, and run through the parser :return none:
[ "Load", "in", "the", "template", "file", "and", "run", "through", "the", "parser", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L104-L146
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__check_time_values
def __check_time_values(self): """ Rules 1. AD or CE units: bigger number is recent, smaller number is older 2. BP: bigger number is older, smaller number is recent. 3. No units: If max year is 1900-2017(current), then assume AD. Else, assume BP :return none: """ _earliest = float(self.noaa_data_sorted["Data_Collection"]["Earliest_Year"]) _recent = float(self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"]) try: _unit = self.noaa_data_sorted["Data_Collection"]["Time_Unit"] except Exception: _unit = "" if not _unit: # If the max value is between 1900 - 2017 (current), then assume "AD" _max = max([_earliest, _recent]) _min = min([_earliest, _recent]) if _max >= 1900 and _max <= 2018: self.noaa_data_sorted["Data_Collection"]["Time_Unit"] = "AD" self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_max) self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_min) # Else, assume it's BP else: # Units don't exist, assume BP self.noaa_data_sorted["Data_Collection"]["Time_Unit"] = "BP" self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_min) self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_max) else: # Units exist if _unit.lower() in ["ad", "ce"]: if _earliest > _recent: self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_earliest) self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_recent) else: if _recent > _earliest: self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_earliest) self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_recent) return
python
def __check_time_values(self): """ Rules 1. AD or CE units: bigger number is recent, smaller number is older 2. BP: bigger number is older, smaller number is recent. 3. No units: If max year is 1900-2017(current), then assume AD. Else, assume BP :return none: """ _earliest = float(self.noaa_data_sorted["Data_Collection"]["Earliest_Year"]) _recent = float(self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"]) try: _unit = self.noaa_data_sorted["Data_Collection"]["Time_Unit"] except Exception: _unit = "" if not _unit: # If the max value is between 1900 - 2017 (current), then assume "AD" _max = max([_earliest, _recent]) _min = min([_earliest, _recent]) if _max >= 1900 and _max <= 2018: self.noaa_data_sorted["Data_Collection"]["Time_Unit"] = "AD" self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_max) self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_min) # Else, assume it's BP else: # Units don't exist, assume BP self.noaa_data_sorted["Data_Collection"]["Time_Unit"] = "BP" self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_min) self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_max) else: # Units exist if _unit.lower() in ["ad", "ce"]: if _earliest > _recent: self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_earliest) self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_recent) else: if _recent > _earliest: self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_earliest) self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_recent) return
[ "def", "__check_time_values", "(", "self", ")", ":", "_earliest", "=", "float", "(", "self", ".", "noaa_data_sorted", "[", "\"Data_Collection\"", "]", "[", "\"Earliest_Year\"", "]", ")", "_recent", "=", "float", "(", "self", ".", "noaa_data_sorted", "[", "\"Da...
Rules 1. AD or CE units: bigger number is recent, smaller number is older 2. BP: bigger number is older, smaller number is recent. 3. No units: If max year is 1900-2017(current), then assume AD. Else, assume BP :return none:
[ "Rules", "1", ".", "AD", "or", "CE", "units", ":", "bigger", "number", "is", "recent", "smaller", "number", "is", "older", "2", ".", "BP", ":", "bigger", "number", "is", "older", "smaller", "number", "is", "recent", ".", "3", ".", "No", "units", ":",...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L177-L218
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__convert_keys_2
def __convert_keys_2(header, d): """ Convert lpd to noaa keys for this one section :param str header: Section header :param dict d: Metadata :return dict: Metadata w/ converted keys """ d_out = {} try: for k, v in d.items(): try: noaa_key = LIPD_NOAA_MAP_BY_SECTION[header][k] d_out[noaa_key] = v except Exception: logger_lpd_noaa.warn("lpd_noaa: convert_keys_section: ran into an error converting {}".format(k)) except KeyError: logger_lpd_noaa.warn("lpd_noaa: convert_keys_section: KeyError: header key {} is not in NOAA_ALL_DICT".format(header)) except AttributeError: logger_lpd_noaa.warn("lpd_noaa: convert_keys_section: AttributeError: metdata is wrong data type".format(header)) return d return d_out
python
def __convert_keys_2(header, d): """ Convert lpd to noaa keys for this one section :param str header: Section header :param dict d: Metadata :return dict: Metadata w/ converted keys """ d_out = {} try: for k, v in d.items(): try: noaa_key = LIPD_NOAA_MAP_BY_SECTION[header][k] d_out[noaa_key] = v except Exception: logger_lpd_noaa.warn("lpd_noaa: convert_keys_section: ran into an error converting {}".format(k)) except KeyError: logger_lpd_noaa.warn("lpd_noaa: convert_keys_section: KeyError: header key {} is not in NOAA_ALL_DICT".format(header)) except AttributeError: logger_lpd_noaa.warn("lpd_noaa: convert_keys_section: AttributeError: metdata is wrong data type".format(header)) return d return d_out
[ "def", "__convert_keys_2", "(", "header", ",", "d", ")", ":", "d_out", "=", "{", "}", "try", ":", "for", "k", ",", "v", "in", "d", ".", "items", "(", ")", ":", "try", ":", "noaa_key", "=", "LIPD_NOAA_MAP_BY_SECTION", "[", "header", "]", "[", "k", ...
Convert lpd to noaa keys for this one section :param str header: Section header :param dict d: Metadata :return dict: Metadata w/ converted keys
[ "Convert", "lpd", "to", "noaa", "keys", "for", "this", "one", "section", ":", "param", "str", "header", ":", "Section", "header", ":", "param", "dict", "d", ":", "Metadata", ":", "return", "dict", ":", "Metadata", "w", "/", "converted", "keys" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L221-L241
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__convert_keys_1
def __convert_keys_1(self, header, d): """ Loop over keys in a dictionary and replace the lipd keys with noaa keys :return: """ d2 = {} try: for k, v in d.items(): try: d2[self.__get_noaa_key_w_context(header, k)] = v except KeyError: pass except Exception: return d return d2
python
def __convert_keys_1(self, header, d): """ Loop over keys in a dictionary and replace the lipd keys with noaa keys :return: """ d2 = {} try: for k, v in d.items(): try: d2[self.__get_noaa_key_w_context(header, k)] = v except KeyError: pass except Exception: return d return d2
[ "def", "__convert_keys_1", "(", "self", ",", "header", ",", "d", ")", ":", "d2", "=", "{", "}", "try", ":", "for", "k", ",", "v", "in", "d", ".", "items", "(", ")", ":", "try", ":", "d2", "[", "self", ".", "__get_noaa_key_w_context", "(", "header...
Loop over keys in a dictionary and replace the lipd keys with noaa keys :return:
[ "Loop", "over", "keys", "in", "a", "dictionary", "and", "replace", "the", "lipd", "keys", "with", "noaa", "keys", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L243-L257
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__create_blanks
def __create_blanks(section_name, d): """ All keys need to be written to the output, with or without a value. Furthermore, only keys that have values exist at this point. We need to manually insert the other keys with a blank value. Loop through the global list to see what's missing in our dict. :param str section_name: Retrieve data from global dict for this section :return none: """ try: for key in NOAA_KEYS_BY_SECTION[section_name]: if key not in d: # Key not in our dict. Create the blank entry. d[key] = "" except Exception: logger_lpd_noaa.error("lpd_noaa: create_blanks: must section: {}, key".format(section_name, key)) return d
python
def __create_blanks(section_name, d): """ All keys need to be written to the output, with or without a value. Furthermore, only keys that have values exist at this point. We need to manually insert the other keys with a blank value. Loop through the global list to see what's missing in our dict. :param str section_name: Retrieve data from global dict for this section :return none: """ try: for key in NOAA_KEYS_BY_SECTION[section_name]: if key not in d: # Key not in our dict. Create the blank entry. d[key] = "" except Exception: logger_lpd_noaa.error("lpd_noaa: create_blanks: must section: {}, key".format(section_name, key)) return d
[ "def", "__create_blanks", "(", "section_name", ",", "d", ")", ":", "try", ":", "for", "key", "in", "NOAA_KEYS_BY_SECTION", "[", "section_name", "]", ":", "if", "key", "not", "in", "d", ":", "# Key not in our dict. Create the blank entry.", "d", "[", "key", "]"...
All keys need to be written to the output, with or without a value. Furthermore, only keys that have values exist at this point. We need to manually insert the other keys with a blank value. Loop through the global list to see what's missing in our dict. :param str section_name: Retrieve data from global dict for this section :return none:
[ "All", "keys", "need", "to", "be", "written", "to", "the", "output", "with", "or", "without", "a", "value", ".", "Furthermore", "only", "keys", "that", "have", "values", "exist", "at", "this", "point", ".", "We", "need", "to", "manually", "insert", "the"...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L260-L275
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__flatten_col
def __flatten_col(d): """ Flatten column so climateInterpretation and calibration are not nested. :param d: :return: """ try: for entry in ["climateInterpretation", "calibration"]: if entry in d: for k, v in d[entry].items(): d[k] = v del d[entry] except AttributeError: pass return d
python
def __flatten_col(d): """ Flatten column so climateInterpretation and calibration are not nested. :param d: :return: """ try: for entry in ["climateInterpretation", "calibration"]: if entry in d: for k, v in d[entry].items(): d[k] = v del d[entry] except AttributeError: pass return d
[ "def", "__flatten_col", "(", "d", ")", ":", "try", ":", "for", "entry", "in", "[", "\"climateInterpretation\"", ",", "\"calibration\"", "]", ":", "if", "entry", "in", "d", ":", "for", "k", ",", "v", "in", "d", "[", "entry", "]", ".", "items", "(", ...
Flatten column so climateInterpretation and calibration are not nested. :param d: :return:
[ "Flatten", "column", "so", "climateInterpretation", "and", "calibration", "are", "not", "nested", ".", ":", "param", "d", ":", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L278-L294
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__generate_study_name
def __generate_study_name(self): """ When a study name is not given, generate one with the format of " author - site name - year " :return str study_name: generated study name """ study_name = "" _exist = False try: if self.noaa_data_sorted["Top"]["Study_Name"]: _exist = True except KeyError: pass if not _exist: try: _site = self.noaa_data_sorted["Site_Information"]["properties"]["siteName"] _year = self.noaa_data_sorted["Publication"][0]["pubYear"] _author = self.noaa_data_sorted["Publication"][0]["author"] _author = self.__get_author_last_name(_author) study_name = "{}.{}.{}".format(_author, _site, _year) study_name = study_name.replace(" ", "_").replace(",", "_") except (KeyError, Exception): pass self.noaa_data_sorted["Top"]["Study_Name"] = study_name self.noaa_data_sorted["Title"]["Study_Name"] = study_name return
python
def __generate_study_name(self): """ When a study name is not given, generate one with the format of " author - site name - year " :return str study_name: generated study name """ study_name = "" _exist = False try: if self.noaa_data_sorted["Top"]["Study_Name"]: _exist = True except KeyError: pass if not _exist: try: _site = self.noaa_data_sorted["Site_Information"]["properties"]["siteName"] _year = self.noaa_data_sorted["Publication"][0]["pubYear"] _author = self.noaa_data_sorted["Publication"][0]["author"] _author = self.__get_author_last_name(_author) study_name = "{}.{}.{}".format(_author, _site, _year) study_name = study_name.replace(" ", "_").replace(",", "_") except (KeyError, Exception): pass self.noaa_data_sorted["Top"]["Study_Name"] = study_name self.noaa_data_sorted["Title"]["Study_Name"] = study_name return
[ "def", "__generate_study_name", "(", "self", ")", ":", "study_name", "=", "\"\"", "_exist", "=", "False", "try", ":", "if", "self", ".", "noaa_data_sorted", "[", "\"Top\"", "]", "[", "\"Study_Name\"", "]", ":", "_exist", "=", "True", "except", "KeyError", ...
When a study name is not given, generate one with the format of " author - site name - year " :return str study_name: generated study name
[ "When", "a", "study", "name", "is", "not", "given", "generate", "one", "with", "the", "format", "of", "author", "-", "site", "name", "-", "year", ":", "return", "str", "study_name", ":", "generated", "study", "name" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L296-L321
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__lists_to_str
def __lists_to_str(self): """ There are some data lists that we collected across the dataset that need to be concatenated into a single string before writing to the text file. :return none: """ # ["archive_type", "sensor_genus", "sensor_species", "investigator"] if self.lsts_tmp["archive"]: self.noaa_data_sorted["Top"]["Archive"] = ",".join(self.lsts_tmp["archive"]) if self.lsts_tmp["species"]: self.noaa_data_sorted["Species"]["Species_Name"] = ",".join(self.lsts_tmp["species"]) if self.lsts_tmp["genus"]: self.noaa_data_sorted["Species"]["Species_Code"] = ",".join(self.lsts_tmp["genus"]) if self.lsts_tmp["qc"]: if self.__is_notes(): self.noaa_data_sorted["Description_Notes_and_Keywords"]["Description"] = ";".join(self.lsts_tmp["qc"]) return
python
def __lists_to_str(self): """ There are some data lists that we collected across the dataset that need to be concatenated into a single string before writing to the text file. :return none: """ # ["archive_type", "sensor_genus", "sensor_species", "investigator"] if self.lsts_tmp["archive"]: self.noaa_data_sorted["Top"]["Archive"] = ",".join(self.lsts_tmp["archive"]) if self.lsts_tmp["species"]: self.noaa_data_sorted["Species"]["Species_Name"] = ",".join(self.lsts_tmp["species"]) if self.lsts_tmp["genus"]: self.noaa_data_sorted["Species"]["Species_Code"] = ",".join(self.lsts_tmp["genus"]) if self.lsts_tmp["qc"]: if self.__is_notes(): self.noaa_data_sorted["Description_Notes_and_Keywords"]["Description"] = ";".join(self.lsts_tmp["qc"]) return
[ "def", "__lists_to_str", "(", "self", ")", ":", "# [\"archive_type\", \"sensor_genus\", \"sensor_species\", \"investigator\"]", "if", "self", ".", "lsts_tmp", "[", "\"archive\"", "]", ":", "self", ".", "noaa_data_sorted", "[", "\"Top\"", "]", "[", "\"Archive\"", "]", ...
There are some data lists that we collected across the dataset that need to be concatenated into a single string before writing to the text file. :return none:
[ "There", "are", "some", "data", "lists", "that", "we", "collected", "across", "the", "dataset", "that", "need", "to", "be", "concatenated", "into", "a", "single", "string", "before", "writing", "to", "the", "text", "file", ".", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L338-L354
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__parse_dois
def __parse_dois(self, x): """ Parse the Dataset_DOI field. Could be one DOI string, or a list of DOIs :param any x: Str or List of DOI ids :return none: list is set to self """ # datasetDOI is a string. parse, validate and return a list of DOIs if isinstance(x, str): # regex cleans string, and returns a list with 1 entry for each regex doi match m = clean_doi(x) # make sure m is not an empty list if m: # set list directly into self self.doi = m # datasetDOI is a list. use regex to validate each doi entry. elif isinstance(x, list): for entry in x: # regex cleans string, and returns a list with 1 entry for each regex doi match m = clean_doi(entry) # make sure m is not an empty list if m: # combine lists with existing self list self.doi += m return
python
def __parse_dois(self, x): """ Parse the Dataset_DOI field. Could be one DOI string, or a list of DOIs :param any x: Str or List of DOI ids :return none: list is set to self """ # datasetDOI is a string. parse, validate and return a list of DOIs if isinstance(x, str): # regex cleans string, and returns a list with 1 entry for each regex doi match m = clean_doi(x) # make sure m is not an empty list if m: # set list directly into self self.doi = m # datasetDOI is a list. use regex to validate each doi entry. elif isinstance(x, list): for entry in x: # regex cleans string, and returns a list with 1 entry for each regex doi match m = clean_doi(entry) # make sure m is not an empty list if m: # combine lists with existing self list self.doi += m return
[ "def", "__parse_dois", "(", "self", ",", "x", ")", ":", "# datasetDOI is a string. parse, validate and return a list of DOIs", "if", "isinstance", "(", "x", ",", "str", ")", ":", "# regex cleans string, and returns a list with 1 entry for each regex doi match", "m", "=", "cle...
Parse the Dataset_DOI field. Could be one DOI string, or a list of DOIs :param any x: Str or List of DOI ids :return none: list is set to self
[ "Parse", "the", "Dataset_DOI", "field", ".", "Could", "be", "one", "DOI", "string", "or", "a", "list", "of", "DOIs", ":", "param", "any", "x", ":", "Str", "or", "List", "of", "DOI", "ids", ":", "return", "none", ":", "list", "is", "set", "to", "sel...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L356-L380
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__split_path
def __split_path(string): """ Used in the path_context function. Split the full path into a list of steps :param str string: Path string ("geo-elevation-height") :return list out: Path as a list of strings. One entry per path step.(["geo", "elevation", "height"]) """ out = [] position = string.find(':') if position != -1: # A position of 0+ means that ":" was found in the string key = string[:position] val = string[position+1:] out.append(key) out.append(val) if ('-' in key) and ('Funding' not in key) and ('Grant' not in key): out = key.split('-') out.append(val) return out
python
def __split_path(string): """ Used in the path_context function. Split the full path into a list of steps :param str string: Path string ("geo-elevation-height") :return list out: Path as a list of strings. One entry per path step.(["geo", "elevation", "height"]) """ out = [] position = string.find(':') if position != -1: # A position of 0+ means that ":" was found in the string key = string[:position] val = string[position+1:] out.append(key) out.append(val) if ('-' in key) and ('Funding' not in key) and ('Grant' not in key): out = key.split('-') out.append(val) return out
[ "def", "__split_path", "(", "string", ")", ":", "out", "=", "[", "]", "position", "=", "string", ".", "find", "(", "':'", ")", "if", "position", "!=", "-", "1", ":", "# A position of 0+ means that \":\" was found in the string", "key", "=", "string", "[", ":...
Used in the path_context function. Split the full path into a list of steps :param str string: Path string ("geo-elevation-height") :return list out: Path as a list of strings. One entry per path step.(["geo", "elevation", "height"])
[ "Used", "in", "the", "path_context", "function", ".", "Split", "the", "full", "path", "into", "a", "list", "of", "steps", ":", "param", "str", "string", ":", "Path", "string", "(", "geo", "-", "elevation", "-", "height", ")", ":", "return", "list", "ou...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L383-L400
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA._values_exist
def _values_exist(table): """ Check that values exist in this table, and we can write out data columns :param dict table: Table data :return bool: Values exist or not exist """ try: for var, data in table["columns"].items(): if "values" in data: return True except KeyError as e: logger_lpd_noaa.warn("values_exist: KeyError: {}".format(e)) except Exception as e: logger_lpd_noaa.warn("values_exist: Excpetion: {}".format(e)) return False
python
def _values_exist(table): """ Check that values exist in this table, and we can write out data columns :param dict table: Table data :return bool: Values exist or not exist """ try: for var, data in table["columns"].items(): if "values" in data: return True except KeyError as e: logger_lpd_noaa.warn("values_exist: KeyError: {}".format(e)) except Exception as e: logger_lpd_noaa.warn("values_exist: Excpetion: {}".format(e)) return False
[ "def", "_values_exist", "(", "table", ")", ":", "try", ":", "for", "var", ",", "data", "in", "table", "[", "\"columns\"", "]", ".", "items", "(", ")", ":", "if", "\"values\"", "in", "data", ":", "return", "True", "except", "KeyError", "as", "e", ":",...
Check that values exist in this table, and we can write out data columns :param dict table: Table data :return bool: Values exist or not exist
[ "Check", "that", "values", "exist", "in", "this", "table", "and", "we", "can", "write", "out", "data", "columns", ":", "param", "dict", "table", ":", "Table", "data", ":", "return", "bool", ":", "Values", "exist", "or", "not", "exist" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L403-L417
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__reorganize
def __reorganize(self): """ Reorganize the keys into their proper section order for the NOAA output file DO NOT parse data tables (paleoData or chronData). We will do those separately. :param str key: :param any value: :return none: """ logger_lpd_noaa.info("enter reorganize") # NOAA files are organized in sections differently than NOAA. try to translate these sections. for key, value in self.lipd_data.items(): # if this key has a noaa match, it'll be returned. otherwise, empty string for no match noaa_key = self.__get_noaa_key(key) # check if this lipd key is in the NOAA_KEYS conversion dictionary. # if it's not, then stash it in our ignore list. if key not in LIPD_NOAA_MAP_FLAT: self.noaa_data_sorted["Ignore"][noaa_key] = value # studyName is placed two times in file. Line #1, and under the 'title' section elif noaa_key == "Study_Name": # study name gets put in two locations self.noaa_data_sorted["Top"][noaa_key] = value self.noaa_data_sorted["Title"][noaa_key] = value # put archiveType in self, because we'll reuse it later for the 9-part-variables as well elif noaa_key == "Archive": self.lsts_tmp["archive"].append(value) # Dataset_DOI is a repeatable element. the key could be a single DOI, or a list of DOIs. elif noaa_key == "Dataset_DOI": self.__parse_dois(value) # all other keys. determine which noaa section they belong in. else: # noaa keys are sorted by section. for header, content in NOAA_KEYS_BY_SECTION.items(): try: # if our key is a noaa header key, then that means it's the ONLY key in the section. # set value directly if noaa_key == header: self.noaa_data_sorted[header] = value # all other cases, the key is part of the section elif noaa_key in content: self.noaa_data_sorted[header][noaa_key] = value except KeyError: # this shouldn't ever really happen, but just in case logger_lpd_noaa.warn("lpd_noaa: reorganize: KeyError: {}".format(noaa_key)) return
python
def __reorganize(self): """ Reorganize the keys into their proper section order for the NOAA output file DO NOT parse data tables (paleoData or chronData). We will do those separately. :param str key: :param any value: :return none: """ logger_lpd_noaa.info("enter reorganize") # NOAA files are organized in sections differently than NOAA. try to translate these sections. for key, value in self.lipd_data.items(): # if this key has a noaa match, it'll be returned. otherwise, empty string for no match noaa_key = self.__get_noaa_key(key) # check if this lipd key is in the NOAA_KEYS conversion dictionary. # if it's not, then stash it in our ignore list. if key not in LIPD_NOAA_MAP_FLAT: self.noaa_data_sorted["Ignore"][noaa_key] = value # studyName is placed two times in file. Line #1, and under the 'title' section elif noaa_key == "Study_Name": # study name gets put in two locations self.noaa_data_sorted["Top"][noaa_key] = value self.noaa_data_sorted["Title"][noaa_key] = value # put archiveType in self, because we'll reuse it later for the 9-part-variables as well elif noaa_key == "Archive": self.lsts_tmp["archive"].append(value) # Dataset_DOI is a repeatable element. the key could be a single DOI, or a list of DOIs. elif noaa_key == "Dataset_DOI": self.__parse_dois(value) # all other keys. determine which noaa section they belong in. else: # noaa keys are sorted by section. for header, content in NOAA_KEYS_BY_SECTION.items(): try: # if our key is a noaa header key, then that means it's the ONLY key in the section. # set value directly if noaa_key == header: self.noaa_data_sorted[header] = value # all other cases, the key is part of the section elif noaa_key in content: self.noaa_data_sorted[header][noaa_key] = value except KeyError: # this shouldn't ever really happen, but just in case logger_lpd_noaa.warn("lpd_noaa: reorganize: KeyError: {}".format(noaa_key)) return
[ "def", "__reorganize", "(", "self", ")", ":", "logger_lpd_noaa", ".", "info", "(", "\"enter reorganize\"", ")", "# NOAA files are organized in sections differently than NOAA. try to translate these sections.", "for", "key", ",", "value", "in", "self", ".", "lipd_data", ".",...
Reorganize the keys into their proper section order for the NOAA output file DO NOT parse data tables (paleoData or chronData). We will do those separately. :param str key: :param any value: :return none:
[ "Reorganize", "the", "keys", "into", "their", "proper", "section", "order", "for", "the", "NOAA", "output", "file", "DO", "NOT", "parse", "data", "tables", "(", "paleoData", "or", "chronData", ")", ".", "We", "will", "do", "those", "separately", ".", ":", ...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L421-L465
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__reorganize_author
def __reorganize_author(self): """ LiPD delimits author names by "and". Noaa wants them to be semi-colon delimited. :return none: """ try: for idx, pub in enumerate(self.noaa_data_sorted["Publication"]): if "author" in pub: _str = pub["author"] if " and " in _str: self.noaa_data_sorted["Publication"][idx]["author"] = _str.replace(" and ", "; ") if ";" in _str: self.noaa_data_sorted["Publication"][idx]["author"] = _str.replace(";", "; ") except Exception: pass return
python
def __reorganize_author(self): """ LiPD delimits author names by "and". Noaa wants them to be semi-colon delimited. :return none: """ try: for idx, pub in enumerate(self.noaa_data_sorted["Publication"]): if "author" in pub: _str = pub["author"] if " and " in _str: self.noaa_data_sorted["Publication"][idx]["author"] = _str.replace(" and ", "; ") if ";" in _str: self.noaa_data_sorted["Publication"][idx]["author"] = _str.replace(";", "; ") except Exception: pass return
[ "def", "__reorganize_author", "(", "self", ")", ":", "try", ":", "for", "idx", ",", "pub", "in", "enumerate", "(", "self", ".", "noaa_data_sorted", "[", "\"Publication\"", "]", ")", ":", "if", "\"author\"", "in", "pub", ":", "_str", "=", "pub", "[", "\...
LiPD delimits author names by "and". Noaa wants them to be semi-colon delimited. :return none:
[ "LiPD", "delimits", "author", "names", "by", "and", ".", "Noaa", "wants", "them", "to", "be", "semi", "-", "colon", "delimited", ".", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L467-L483
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__reorganize_coordinates
def __reorganize_coordinates(self): """ GEOJSON FORMAT : [ LONGITUDE, LATITUDE, ELEVATION] Reorganize coordinates based on how many values are available. :return: """ try: l = self.noaa_data_sorted["Site_Information"]['geometry']['coordinates'] locations = ["Northernmost_Latitude", "Southernmost_Latitude", "Easternmost_Longitude", "Westernmost_Longitude", "Elevation"] logger_lpd_noaa.info("coordinates: {} coordinates found".format(len(l))) # Amount of coordinates in the list _len_coords = len(l) # Odd number of coordinates. Elevation value exists if _len_coords % 2 == 1: # Store the elevation, which is always the last value in the list self.noaa_geo["Elevation"] = l[-1] # If elevation, then subtract one from the length _len_coords -= 1 # Start compiling the lat lon coordinates # 0 coordinate values. fill in locations with empty values if _len_coords == 0: for location in locations: self.noaa_geo[location] = ' ' # 2 coordinates values. duplicate to fill 4 location slots. elif _len_coords == 2: self.noaa_geo[locations[0]] = l[1] self.noaa_geo[locations[1]] = l[1] self.noaa_geo[locations[2]] = l[0] self.noaa_geo[locations[3]] = l[0] # 4 coordinate values. put each in its correct location slot. elif _len_coords == 4: for index, location in enumerate(locations): self.noaa_geo[locations[index]] = l[index] else: logger_lpd_noaa.info("coordinates: too many coordinates given") except KeyError: logger_lpd_noaa.info("lpd_noaa: coordinates: no coordinate information") except Exception: logger_lpd_noaa.error("lpd_noaa: coordinates: unknown exception") return
python
def __reorganize_coordinates(self): """ GEOJSON FORMAT : [ LONGITUDE, LATITUDE, ELEVATION] Reorganize coordinates based on how many values are available. :return: """ try: l = self.noaa_data_sorted["Site_Information"]['geometry']['coordinates'] locations = ["Northernmost_Latitude", "Southernmost_Latitude", "Easternmost_Longitude", "Westernmost_Longitude", "Elevation"] logger_lpd_noaa.info("coordinates: {} coordinates found".format(len(l))) # Amount of coordinates in the list _len_coords = len(l) # Odd number of coordinates. Elevation value exists if _len_coords % 2 == 1: # Store the elevation, which is always the last value in the list self.noaa_geo["Elevation"] = l[-1] # If elevation, then subtract one from the length _len_coords -= 1 # Start compiling the lat lon coordinates # 0 coordinate values. fill in locations with empty values if _len_coords == 0: for location in locations: self.noaa_geo[location] = ' ' # 2 coordinates values. duplicate to fill 4 location slots. elif _len_coords == 2: self.noaa_geo[locations[0]] = l[1] self.noaa_geo[locations[1]] = l[1] self.noaa_geo[locations[2]] = l[0] self.noaa_geo[locations[3]] = l[0] # 4 coordinate values. put each in its correct location slot. elif _len_coords == 4: for index, location in enumerate(locations): self.noaa_geo[locations[index]] = l[index] else: logger_lpd_noaa.info("coordinates: too many coordinates given") except KeyError: logger_lpd_noaa.info("lpd_noaa: coordinates: no coordinate information") except Exception: logger_lpd_noaa.error("lpd_noaa: coordinates: unknown exception") return
[ "def", "__reorganize_coordinates", "(", "self", ")", ":", "try", ":", "l", "=", "self", ".", "noaa_data_sorted", "[", "\"Site_Information\"", "]", "[", "'geometry'", "]", "[", "'coordinates'", "]", "locations", "=", "[", "\"Northernmost_Latitude\"", ",", "\"Sout...
GEOJSON FORMAT : [ LONGITUDE, LATITUDE, ELEVATION] Reorganize coordinates based on how many values are available. :return:
[ "GEOJSON", "FORMAT", ":", "[", "LONGITUDE", "LATITUDE", "ELEVATION", "]", "Reorganize", "coordinates", "based", "on", "how", "many", "values", "are", "available", ".", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L485-L531
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__reorganize_funding
def __reorganize_funding(self): """ Funding gets added to noaa_data_sorted with LiPD keys. Change those keys to NOAA :return none: """ _map = {"agency": "Funding_Agency_Name", "grant": "Grant"} try: _l = [] for item in self.noaa_data_sorted["Funding_Agency"]: _tmp = {} for lpd_name, noaa_name in _map.items(): val = "" if lpd_name in item: val = item[lpd_name] _tmp[noaa_name] = val _l.append(_tmp) self.noaa_data_sorted["Funding_Agency"] = _l except Exception: pass return
python
def __reorganize_funding(self): """ Funding gets added to noaa_data_sorted with LiPD keys. Change those keys to NOAA :return none: """ _map = {"agency": "Funding_Agency_Name", "grant": "Grant"} try: _l = [] for item in self.noaa_data_sorted["Funding_Agency"]: _tmp = {} for lpd_name, noaa_name in _map.items(): val = "" if lpd_name in item: val = item[lpd_name] _tmp[noaa_name] = val _l.append(_tmp) self.noaa_data_sorted["Funding_Agency"] = _l except Exception: pass return
[ "def", "__reorganize_funding", "(", "self", ")", ":", "_map", "=", "{", "\"agency\"", ":", "\"Funding_Agency_Name\"", ",", "\"grant\"", ":", "\"Grant\"", "}", "try", ":", "_l", "=", "[", "]", "for", "item", "in", "self", ".", "noaa_data_sorted", "[", "\"Fu...
Funding gets added to noaa_data_sorted with LiPD keys. Change those keys to NOAA :return none:
[ "Funding", "gets", "added", "to", "noaa_data_sorted", "with", "LiPD", "keys", ".", "Change", "those", "keys", "to", "NOAA", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L533-L552
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__reorganize_geo
def __reorganize_geo(self): """ Concat geo value and units, and reorganize the rest References geo data from self.noaa_data_sorted Places new data into self.noaa_geo temporarily, and then back into self.noaa_data_sorted. :return: """ logger_lpd_noaa.info("enter reorganize_geo") try: # Geo -> Properties for k, v in self.noaa_data_sorted["Site_Information"]['properties'].items(): noaa_key = self.__get_noaa_key(k) self.noaa_geo[noaa_key] = v except KeyError: logger_lpd_noaa.info("reorganize_geo: KeyError: geo properties") try: # Geo -> Geometry self.__reorganize_coordinates() except Exception: logger_lpd_noaa.warning("reorganize_geo: Exception: missing required data: coordinates") # put the temporarily organized data into the self.noaa_data_sorted self.noaa_data_sorted["Site_Information"] = self.noaa_geo return
python
def __reorganize_geo(self): """ Concat geo value and units, and reorganize the rest References geo data from self.noaa_data_sorted Places new data into self.noaa_geo temporarily, and then back into self.noaa_data_sorted. :return: """ logger_lpd_noaa.info("enter reorganize_geo") try: # Geo -> Properties for k, v in self.noaa_data_sorted["Site_Information"]['properties'].items(): noaa_key = self.__get_noaa_key(k) self.noaa_geo[noaa_key] = v except KeyError: logger_lpd_noaa.info("reorganize_geo: KeyError: geo properties") try: # Geo -> Geometry self.__reorganize_coordinates() except Exception: logger_lpd_noaa.warning("reorganize_geo: Exception: missing required data: coordinates") # put the temporarily organized data into the self.noaa_data_sorted self.noaa_data_sorted["Site_Information"] = self.noaa_geo return
[ "def", "__reorganize_geo", "(", "self", ")", ":", "logger_lpd_noaa", ".", "info", "(", "\"enter reorganize_geo\"", ")", "try", ":", "# Geo -> Properties", "for", "k", ",", "v", "in", "self", ".", "noaa_data_sorted", "[", "\"Site_Information\"", "]", "[", "'prope...
Concat geo value and units, and reorganize the rest References geo data from self.noaa_data_sorted Places new data into self.noaa_geo temporarily, and then back into self.noaa_data_sorted. :return:
[ "Concat", "geo", "value", "and", "units", "and", "reorganize", "the", "rest", "References", "geo", "data", "from", "self", ".", "noaa_data_sorted", "Places", "new", "data", "into", "self", ".", "noaa_geo", "temporarily", "and", "then", "back", "into", "self", ...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L554-L578
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__reorganize_sensor
def __reorganize_sensor(self): """ We have raw sensorGenus, and sensorSpecies in self, now clean and sort :return none: """ _code = [] _name = [] # Check if any of the sensor data is misplaced, and create corrected lists. if self.lsts_tmp["genus"]: for name in self.lsts_tmp["genus"]: if len(name) == 4 and name.isupper(): _code.append(name) else: _name.append(name) if self.lsts_tmp["species"]: for name in self.lsts_tmp["species"]: if len(name) != 4 and not name.isupper(): _name.append(name) else: _code.append(name) # Set the strings into the noaa data sorted self.lsts_tmp["species"] = _name self.lsts_tmp["genus"] = _code return
python
def __reorganize_sensor(self): """ We have raw sensorGenus, and sensorSpecies in self, now clean and sort :return none: """ _code = [] _name = [] # Check if any of the sensor data is misplaced, and create corrected lists. if self.lsts_tmp["genus"]: for name in self.lsts_tmp["genus"]: if len(name) == 4 and name.isupper(): _code.append(name) else: _name.append(name) if self.lsts_tmp["species"]: for name in self.lsts_tmp["species"]: if len(name) != 4 and not name.isupper(): _name.append(name) else: _code.append(name) # Set the strings into the noaa data sorted self.lsts_tmp["species"] = _name self.lsts_tmp["genus"] = _code return
[ "def", "__reorganize_sensor", "(", "self", ")", ":", "_code", "=", "[", "]", "_name", "=", "[", "]", "# Check if any of the sensor data is misplaced, and create corrected lists.", "if", "self", ".", "lsts_tmp", "[", "\"genus\"", "]", ":", "for", "name", "in", "sel...
We have raw sensorGenus, and sensorSpecies in self, now clean and sort :return none:
[ "We", "have", "raw", "sensorGenus", "and", "sensorSpecies", "in", "self", "now", "clean", "and", "sort", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L580-L607
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__put_names_on_csv_cols
def __put_names_on_csv_cols(names, cols): """ Put the variableNames with the corresponding column data. :param list names: variableNames :param list cols: List of Lists of column data :return dict: """ _combined = {} for idx, name in enumerate(names): # Use the variableName, and the column data from the same index _combined[name] = cols[idx] return _combined
python
def __put_names_on_csv_cols(names, cols): """ Put the variableNames with the corresponding column data. :param list names: variableNames :param list cols: List of Lists of column data :return dict: """ _combined = {} for idx, name in enumerate(names): # Use the variableName, and the column data from the same index _combined[name] = cols[idx] return _combined
[ "def", "__put_names_on_csv_cols", "(", "names", ",", "cols", ")", ":", "_combined", "=", "{", "}", "for", "idx", ",", "name", "in", "enumerate", "(", "names", ")", ":", "# Use the variableName, and the column data from the same index", "_combined", "[", "name", "]...
Put the variableNames with the corresponding column data. :param list names: variableNames :param list cols: List of Lists of column data :return dict:
[ "Put", "the", "variableNames", "with", "the", "corresponding", "column", "data", ".", ":", "param", "list", "names", ":", "variableNames", ":", "param", "list", "cols", ":", "List", "of", "Lists", "of", "column", "data", ":", "return", "dict", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L621-L635
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__put_year_col_first
def __put_year_col_first(d): """ Always write year column first. Reorder dictionary so that year is first :param dict d: data :return dict: Reordered data """ if "year" in d: D = OrderedDict() # store the year column first D["year"] = d["year"] for k,v in d.items(): if k != "year": # store the other columns D[k] = v return D else: # year is not found, return data as-is return d
python
def __put_year_col_first(d): """ Always write year column first. Reorder dictionary so that year is first :param dict d: data :return dict: Reordered data """ if "year" in d: D = OrderedDict() # store the year column first D["year"] = d["year"] for k,v in d.items(): if k != "year": # store the other columns D[k] = v return D else: # year is not found, return data as-is return d
[ "def", "__put_year_col_first", "(", "d", ")", ":", "if", "\"year\"", "in", "d", ":", "D", "=", "OrderedDict", "(", ")", "# store the year column first", "D", "[", "\"year\"", "]", "=", "d", "[", "\"year\"", "]", "for", "k", ",", "v", "in", "d", ".", ...
Always write year column first. Reorder dictionary so that year is first :param dict d: data :return dict: Reordered data
[ "Always", "write", "year", "column", "first", ".", "Reorder", "dictionary", "so", "that", "year", "is", "first", ":", "param", "dict", "d", ":", "data", ":", "return", "dict", ":", "Reordered", "data" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L638-L655
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__rm_duplicate_citation_2
def __rm_duplicate_citation_2(self, key): """ Remove exact duplicate entries for abstract or citation fields. Check all publication entries. :return: """ citations = [] # Before writing, remove any duplicate "Full_Citations" that are found for idx, pub in enumerate(self.noaa_data_sorted["Publication"]): try: citations.append(pub[key]) except KeyError: # Key was not found. Enter a blank entry in citations list citations.append("") # Create a backwards dictionary, that lists which indexes are duplicates d = defaultdict(list) for i, item in enumerate(citations): d[item].append(i) d = {k: v for k, v in d.items() if len(v) > 1} # Duplicates indexes are listed like so: # {"full citation info here": [0, 2, 5]} # Loop over duplicate indexes for citation, idxs in d.items(): # do not process any duplicate entries that were initially blanks. if citation: # Loop for [1:], since we want to keep the first citation and remove the rest. for idx in idxs[1:]: # Set citation to blank self.noaa_data_sorted["Publication"][idx][key] = ""
python
def __rm_duplicate_citation_2(self, key): """ Remove exact duplicate entries for abstract or citation fields. Check all publication entries. :return: """ citations = [] # Before writing, remove any duplicate "Full_Citations" that are found for idx, pub in enumerate(self.noaa_data_sorted["Publication"]): try: citations.append(pub[key]) except KeyError: # Key was not found. Enter a blank entry in citations list citations.append("") # Create a backwards dictionary, that lists which indexes are duplicates d = defaultdict(list) for i, item in enumerate(citations): d[item].append(i) d = {k: v for k, v in d.items() if len(v) > 1} # Duplicates indexes are listed like so: # {"full citation info here": [0, 2, 5]} # Loop over duplicate indexes for citation, idxs in d.items(): # do not process any duplicate entries that were initially blanks. if citation: # Loop for [1:], since we want to keep the first citation and remove the rest. for idx in idxs[1:]: # Set citation to blank self.noaa_data_sorted["Publication"][idx][key] = ""
[ "def", "__rm_duplicate_citation_2", "(", "self", ",", "key", ")", ":", "citations", "=", "[", "]", "# Before writing, remove any duplicate \"Full_Citations\" that are found", "for", "idx", ",", "pub", "in", "enumerate", "(", "self", ".", "noaa_data_sorted", "[", "\"Pu...
Remove exact duplicate entries for abstract or citation fields. Check all publication entries. :return:
[ "Remove", "exact", "duplicate", "entries", "for", "abstract", "or", "citation", "fields", ".", "Check", "all", "publication", "entries", ".", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L659-L688
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__rm_names_on_csv_cols
def __rm_names_on_csv_cols(d): """ Remove the variableNames from the columns. :param dict d: Named csv data :return list list: One list for names, one list for column data """ _names = [] _data = [] for name, data in d.items(): _names.append(name) _data.append(data) return _names, _data
python
def __rm_names_on_csv_cols(d): """ Remove the variableNames from the columns. :param dict d: Named csv data :return list list: One list for names, one list for column data """ _names = [] _data = [] for name, data in d.items(): _names.append(name) _data.append(data) return _names, _data
[ "def", "__rm_names_on_csv_cols", "(", "d", ")", ":", "_names", "=", "[", "]", "_data", "=", "[", "]", "for", "name", ",", "data", "in", "d", ".", "items", "(", ")", ":", "_names", ".", "append", "(", "name", ")", "_data", ".", "append", "(", "dat...
Remove the variableNames from the columns. :param dict d: Named csv data :return list list: One list for names, one list for column data
[ "Remove", "the", "variableNames", "from", "the", "columns", ".", ":", "param", "dict", "d", ":", "Named", "csv", "data", ":", "return", "list", "list", ":", "One", "list", "for", "names", "one", "list", "for", "column", "data" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L706-L717
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_author_last_name
def __get_author_last_name(author): """ Take a string of author(s), get the first author name, then get the first authors last name only. :param str author: Author(s) :return str _author: First author's last name """ _author = "" if isinstance(author, str): try: # example: 'Ahmed, Moinuddin and Anchukaitis, Kevin J and ...' # If this is a list of authors, try to split it and just get the first author. if " and " in author: _author = author.split(" and ")[0] # 'Ahmed, Moinuddin; Anchukaitis, Kevin J; ...' elif ";" in author: _author = author.split(";")[0] except Exception: _author = "" try: # example : 'Ahmed Moinuddin, Anchukaitis Kevin J, ...' _author = author.replace(" ", "") if "," in author: _author = author.split(",")[0] except Exception: _author = "" elif isinstance(author, list): try: # example: [{'name': 'Ahmed, Moinuddin'}, {'name': 'Anchukaitis, Kevin J.'}, ..] # just get the last name of the first author. this could get too long. _author = author[0]["name"].split(",")[0] except Exception as e: _author = "" return _author
python
def __get_author_last_name(author): """ Take a string of author(s), get the first author name, then get the first authors last name only. :param str author: Author(s) :return str _author: First author's last name """ _author = "" if isinstance(author, str): try: # example: 'Ahmed, Moinuddin and Anchukaitis, Kevin J and ...' # If this is a list of authors, try to split it and just get the first author. if " and " in author: _author = author.split(" and ")[0] # 'Ahmed, Moinuddin; Anchukaitis, Kevin J; ...' elif ";" in author: _author = author.split(";")[0] except Exception: _author = "" try: # example : 'Ahmed Moinuddin, Anchukaitis Kevin J, ...' _author = author.replace(" ", "") if "," in author: _author = author.split(",")[0] except Exception: _author = "" elif isinstance(author, list): try: # example: [{'name': 'Ahmed, Moinuddin'}, {'name': 'Anchukaitis, Kevin J.'}, ..] # just get the last name of the first author. this could get too long. _author = author[0]["name"].split(",")[0] except Exception as e: _author = "" return _author
[ "def", "__get_author_last_name", "(", "author", ")", ":", "_author", "=", "\"\"", "if", "isinstance", "(", "author", ",", "str", ")", ":", "try", ":", "# example: 'Ahmed, Moinuddin and Anchukaitis, Kevin J and ...'", "# If this is a list of authors, try to split it and just...
Take a string of author(s), get the first author name, then get the first authors last name only. :param str author: Author(s) :return str _author: First author's last name
[ "Take", "a", "string", "of", "author", "(", "s", ")", "get", "the", "first", "author", "name", "then", "get", "the", "first", "authors", "last", "name", "only", "." ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L722-L756
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__create_author_investigator_str
def __create_author_investigator_str(self): """ When investigators is empty, try to get authors from the first publication instead. :return str author: Author names """ _author = "" try: for pub in self.noaa_data_sorted["Publication"]: if "author" in pub: if pub["author"]: _author_src = pub["author"] if isinstance(_author_src, str): try: if " and " in _author_src: _author = _author_src.replace(" and ", "; ") elif ";" in _author_src: # If there is a semi-colon, add a space after it, just in case it didn't have one _author = _author_src.replace(";", "; ") break except Exception as e: _author = "" elif isinstance(_author_src, list): try: for _entry in _author_src: _author += _entry["name"].split(",")[0] + ", " except Exception as e: _author = "" except Exception: _author = "" return _author
python
def __create_author_investigator_str(self): """ When investigators is empty, try to get authors from the first publication instead. :return str author: Author names """ _author = "" try: for pub in self.noaa_data_sorted["Publication"]: if "author" in pub: if pub["author"]: _author_src = pub["author"] if isinstance(_author_src, str): try: if " and " in _author_src: _author = _author_src.replace(" and ", "; ") elif ";" in _author_src: # If there is a semi-colon, add a space after it, just in case it didn't have one _author = _author_src.replace(";", "; ") break except Exception as e: _author = "" elif isinstance(_author_src, list): try: for _entry in _author_src: _author += _entry["name"].split(",")[0] + ", " except Exception as e: _author = "" except Exception: _author = "" return _author
[ "def", "__create_author_investigator_str", "(", "self", ")", ":", "_author", "=", "\"\"", "try", ":", "for", "pub", "in", "self", ".", "noaa_data_sorted", "[", "\"Publication\"", "]", ":", "if", "\"author\"", "in", "pub", ":", "if", "pub", "[", "\"author\"",...
When investigators is empty, try to get authors from the first publication instead. :return str author: Author names
[ "When", "investigators", "is", "empty", "try", "to", "get", "authors", "from", "the", "first", "publication", "instead", ".", ":", "return", "str", "author", ":", "Author", "names" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L758-L787
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_filename
def __get_filename(table): """ Get filename from a data table :param dict table: :return str: """ try: filename = table['filename'] except KeyError as e: filename = "" logger_lpd_noaa.warning("get_filename: KeyError: Table missing filename, {}".format(e)) except TypeError: try: filename = table[0]["filename"] except Exception as e: filename = "" logger_lpd_noaa.warning("get_filename: Generic: Unable to get filename from table, {}".format(e)) return filename
python
def __get_filename(table): """ Get filename from a data table :param dict table: :return str: """ try: filename = table['filename'] except KeyError as e: filename = "" logger_lpd_noaa.warning("get_filename: KeyError: Table missing filename, {}".format(e)) except TypeError: try: filename = table[0]["filename"] except Exception as e: filename = "" logger_lpd_noaa.warning("get_filename: Generic: Unable to get filename from table, {}".format(e)) return filename
[ "def", "__get_filename", "(", "table", ")", ":", "try", ":", "filename", "=", "table", "[", "'filename'", "]", "except", "KeyError", "as", "e", ":", "filename", "=", "\"\"", "logger_lpd_noaa", ".", "warning", "(", "\"get_filename: KeyError: Table missing filename,...
Get filename from a data table :param dict table: :return str:
[ "Get", "filename", "from", "a", "data", "table", ":", "param", "dict", "table", ":", ":", "return", "str", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L806-L823
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_doi
def __get_doi(pub): """ Get DOI from this ONE publication entry. :param dict pub: Single publication entry :return: """ doi = "" # Doi location: d["pub"][idx]["identifier"][0]["id"] try: doi = pub["DOI"][0]["id"] doi = clean_doi(doi) except KeyError: logger_lpd_noaa.info("get_dois: KeyError: missing a doi key") except Exception: logger_lpd_noaa.info("get_dois: Exception: something went wrong") # if we received a doi that's a list, we want to concat into a single string if isinstance(doi, list): if len(doi) == 1: doi = doi[0] else: ", ".join(doi) return doi
python
def __get_doi(pub): """ Get DOI from this ONE publication entry. :param dict pub: Single publication entry :return: """ doi = "" # Doi location: d["pub"][idx]["identifier"][0]["id"] try: doi = pub["DOI"][0]["id"] doi = clean_doi(doi) except KeyError: logger_lpd_noaa.info("get_dois: KeyError: missing a doi key") except Exception: logger_lpd_noaa.info("get_dois: Exception: something went wrong") # if we received a doi that's a list, we want to concat into a single string if isinstance(doi, list): if len(doi) == 1: doi = doi[0] else: ", ".join(doi) return doi
[ "def", "__get_doi", "(", "pub", ")", ":", "doi", "=", "\"\"", "# Doi location: d[\"pub\"][idx][\"identifier\"][0][\"id\"]", "try", ":", "doi", "=", "pub", "[", "\"DOI\"", "]", "[", "0", "]", "[", "\"id\"", "]", "doi", "=", "clean_doi", "(", "doi", ")", "ex...
Get DOI from this ONE publication entry. :param dict pub: Single publication entry :return:
[ "Get", "DOI", "from", "this", "ONE", "publication", "entry", ".", ":", "param", "dict", "pub", ":", "Single", "publication", "entry", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L826-L848
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_noaa_key_w_context
def __get_noaa_key_w_context(header, lipd_key): """ Use the section header and key to get the proper noaa key. :param str header: :param str lipd_key: :return: """ try: noaa_key = LIPD_NOAA_MAP_BY_SECTION[header][lipd_key] except KeyError: return lipd_key return noaa_key
python
def __get_noaa_key_w_context(header, lipd_key): """ Use the section header and key to get the proper noaa key. :param str header: :param str lipd_key: :return: """ try: noaa_key = LIPD_NOAA_MAP_BY_SECTION[header][lipd_key] except KeyError: return lipd_key return noaa_key
[ "def", "__get_noaa_key_w_context", "(", "header", ",", "lipd_key", ")", ":", "try", ":", "noaa_key", "=", "LIPD_NOAA_MAP_BY_SECTION", "[", "header", "]", "[", "lipd_key", "]", "except", "KeyError", ":", "return", "lipd_key", "return", "noaa_key" ]
Use the section header and key to get the proper noaa key. :param str header: :param str lipd_key: :return:
[ "Use", "the", "section", "header", "and", "key", "to", "get", "the", "proper", "noaa", "key", ".", ":", "param", "str", "header", ":", ":", "param", "str", "lipd_key", ":", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L876-L887
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_overall_data
def __get_overall_data(self, x): """ (recursive) Collect all "sensorGenus" and "sensorSpecies" fields, set data to self :param any x: Any data type :return none: """ if isinstance(x, dict): if "sensorGenus" in x: if x["sensorGenus"] and x["sensorGenus"] not in self.lsts_tmp["genus"]: self.lsts_tmp["genus"].append(x["sensorGenus"]) if "sensorSpecies" in x: if x["sensorSpecies"] and x["sensorSpecies"] not in self.lsts_tmp["species"]: self.lsts_tmp["species"].append(x["sensorSpecies"]) if "archiveType" in x: if x["archiveType"] and x["archiveType"] not in self.lsts_tmp["archive"]: self.lsts_tmp["archive"].append(x["archiveType"]) if "QCnotes" in x: if x["QCnotes"] and x["QCnotes"] not in self.lsts_tmp["qc"]: self.lsts_tmp["qc"].append(x["QCnotes"]) for k, v in x.items(): if isinstance(v, dict): self.__get_overall_data(v) elif isinstance(v, list): self.__get_overall_data(v) elif isinstance(x, list): for i in x: self.__get_overall_data(i) return x
python
def __get_overall_data(self, x): """ (recursive) Collect all "sensorGenus" and "sensorSpecies" fields, set data to self :param any x: Any data type :return none: """ if isinstance(x, dict): if "sensorGenus" in x: if x["sensorGenus"] and x["sensorGenus"] not in self.lsts_tmp["genus"]: self.lsts_tmp["genus"].append(x["sensorGenus"]) if "sensorSpecies" in x: if x["sensorSpecies"] and x["sensorSpecies"] not in self.lsts_tmp["species"]: self.lsts_tmp["species"].append(x["sensorSpecies"]) if "archiveType" in x: if x["archiveType"] and x["archiveType"] not in self.lsts_tmp["archive"]: self.lsts_tmp["archive"].append(x["archiveType"]) if "QCnotes" in x: if x["QCnotes"] and x["QCnotes"] not in self.lsts_tmp["qc"]: self.lsts_tmp["qc"].append(x["QCnotes"]) for k, v in x.items(): if isinstance(v, dict): self.__get_overall_data(v) elif isinstance(v, list): self.__get_overall_data(v) elif isinstance(x, list): for i in x: self.__get_overall_data(i) return x
[ "def", "__get_overall_data", "(", "self", ",", "x", ")", ":", "if", "isinstance", "(", "x", ",", "dict", ")", ":", "if", "\"sensorGenus\"", "in", "x", ":", "if", "x", "[", "\"sensorGenus\"", "]", "and", "x", "[", "\"sensorGenus\"", "]", "not", "in", ...
(recursive) Collect all "sensorGenus" and "sensorSpecies" fields, set data to self :param any x: Any data type :return none:
[ "(", "recursive", ")", "Collect", "all", "sensorGenus", "and", "sensorSpecies", "fields", "set", "data", "to", "self", ":", "param", "any", "x", ":", "Any", "data", "type", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L889-L918
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_max_min_time_1
def __get_max_min_time_1(self, table): """ Search for either Age or Year to calculate the max, min, and time unit for this table/file. Preference: Look for Age first, and then Year second (if needed) :param dict table: Table data :return dict: Max, min, and time unit """ try: # find the values and units we need to calculate vals, units = self.__get_max_min_time_2(table, ["age", "yearbp", "yrbp"], True) if not vals and not units: vals, units = self.__get_max_min_time_2(table, ["year", "yr"], True) if not vals and not units: vals, units = self.__get_max_min_time_2(table, ["age", "yearbp", "yrbp"], False) if not vals and not units: vals, units = self.__get_max_min_time_2(table, ["year", "yr"], False) # now put this data into the noaa data sorted self for writing to file. # year farthest in the past _max = max(vals) _min = min(vals) if _min or _min in [0, 0.0]: self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_min) if _max or _max in [0, 0.0]: self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_max) # AD or... yrs BP? self.noaa_data_sorted["Data_Collection"]["Time_Unit"] = units except Exception as e: logger_lpd_noaa.debug("get_max_min_time_2: {}".format(e)) return
python
def __get_max_min_time_1(self, table): """ Search for either Age or Year to calculate the max, min, and time unit for this table/file. Preference: Look for Age first, and then Year second (if needed) :param dict table: Table data :return dict: Max, min, and time unit """ try: # find the values and units we need to calculate vals, units = self.__get_max_min_time_2(table, ["age", "yearbp", "yrbp"], True) if not vals and not units: vals, units = self.__get_max_min_time_2(table, ["year", "yr"], True) if not vals and not units: vals, units = self.__get_max_min_time_2(table, ["age", "yearbp", "yrbp"], False) if not vals and not units: vals, units = self.__get_max_min_time_2(table, ["year", "yr"], False) # now put this data into the noaa data sorted self for writing to file. # year farthest in the past _max = max(vals) _min = min(vals) if _min or _min in [0, 0.0]: self.noaa_data_sorted["Data_Collection"]["Earliest_Year"] = str(_min) if _max or _max in [0, 0.0]: self.noaa_data_sorted["Data_Collection"]["Most_Recent_Year"] = str(_max) # AD or... yrs BP? self.noaa_data_sorted["Data_Collection"]["Time_Unit"] = units except Exception as e: logger_lpd_noaa.debug("get_max_min_time_2: {}".format(e)) return
[ "def", "__get_max_min_time_1", "(", "self", ",", "table", ")", ":", "try", ":", "# find the values and units we need to calculate", "vals", ",", "units", "=", "self", ".", "__get_max_min_time_2", "(", "table", ",", "[", "\"age\"", ",", "\"yearbp\"", ",", "\"yrbp\"...
Search for either Age or Year to calculate the max, min, and time unit for this table/file. Preference: Look for Age first, and then Year second (if needed) :param dict table: Table data :return dict: Max, min, and time unit
[ "Search", "for", "either", "Age", "or", "Year", "to", "calculate", "the", "max", "min", "and", "time", "unit", "for", "this", "table", "/", "file", ".", "Preference", ":", "Look", "for", "Age", "first", "and", "then", "Year", "second", "(", "if", "need...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L920-L955
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_max_min_time_2
def __get_max_min_time_2(table, terms, exact): """ Search for either Age or Year to calculate the max, min, and time unit for this table/file. Preference: Look for Age first, and then Year second (if needed) :param dict table: Table data :param list terms: age, yearbp, yrbp, or year, yr :param bool exact: Look for exact key match, or no :return bool: found age or year info """ vals = [] unit = "" try: for k, v in table["columns"].items(): if exact: if k.lower() in terms: try: vals = v["values"] unit = v["units"] break except KeyError: pass elif not exact: for term in terms: if term in k: try: vals = v["values"] unit = v["units"] break except KeyError: pass except Exception as e: logger_lpd_noaa.debug("get_max_min_time_3: {}".format(e)) return vals, unit
python
def __get_max_min_time_2(table, terms, exact): """ Search for either Age or Year to calculate the max, min, and time unit for this table/file. Preference: Look for Age first, and then Year second (if needed) :param dict table: Table data :param list terms: age, yearbp, yrbp, or year, yr :param bool exact: Look for exact key match, or no :return bool: found age or year info """ vals = [] unit = "" try: for k, v in table["columns"].items(): if exact: if k.lower() in terms: try: vals = v["values"] unit = v["units"] break except KeyError: pass elif not exact: for term in terms: if term in k: try: vals = v["values"] unit = v["units"] break except KeyError: pass except Exception as e: logger_lpd_noaa.debug("get_max_min_time_3: {}".format(e)) return vals, unit
[ "def", "__get_max_min_time_2", "(", "table", ",", "terms", ",", "exact", ")", ":", "vals", "=", "[", "]", "unit", "=", "\"\"", "try", ":", "for", "k", ",", "v", "in", "table", "[", "\"columns\"", "]", ".", "items", "(", ")", ":", "if", "exact", "...
Search for either Age or Year to calculate the max, min, and time unit for this table/file. Preference: Look for Age first, and then Year second (if needed) :param dict table: Table data :param list terms: age, yearbp, yrbp, or year, yr :param bool exact: Look for exact key match, or no :return bool: found age or year info
[ "Search", "for", "either", "Age", "or", "Year", "to", "calculate", "the", "max", "min", "and", "time", "unit", "for", "this", "table", "/", "file", ".", "Preference", ":", "Look", "for", "Age", "first", "and", "then", "Year", "second", "(", "if", "need...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L958-L992
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_data_citation
def __get_data_citation(self, l): """ If originalDataURL / investigators not in root data, check for a dataCitation pub entry. :return: """ # loop once for each publication entry for pub in l: try: # at the moment, these are the only keys of interest inside of dataCitation. Check each. for key in ["url", "investigators"]: if pub["type"] == "dataCitation" and key in pub: noaa_key = self.__get_noaa_key(key) self.data_citation[noaa_key] = pub[key] except KeyError: # no "type" key in pub logger_lpd_noaa.info("lpd_noaa: get_data_citation: KeyError: pub is missing 'type'") return
python
def __get_data_citation(self, l): """ If originalDataURL / investigators not in root data, check for a dataCitation pub entry. :return: """ # loop once for each publication entry for pub in l: try: # at the moment, these are the only keys of interest inside of dataCitation. Check each. for key in ["url", "investigators"]: if pub["type"] == "dataCitation" and key in pub: noaa_key = self.__get_noaa_key(key) self.data_citation[noaa_key] = pub[key] except KeyError: # no "type" key in pub logger_lpd_noaa.info("lpd_noaa: get_data_citation: KeyError: pub is missing 'type'") return
[ "def", "__get_data_citation", "(", "self", ",", "l", ")", ":", "# loop once for each publication entry", "for", "pub", "in", "l", ":", "try", ":", "# at the moment, these are the only keys of interest inside of dataCitation. Check each.", "for", "key", "in", "[", "\"url\"",...
If originalDataURL / investigators not in root data, check for a dataCitation pub entry. :return:
[ "If", "originalDataURL", "/", "investigators", "not", "in", "root", "data", "check", "for", "a", "dataCitation", "pub", "entry", ".", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L994-L1010
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_output_filenames
def __get_output_filenames(self): """ Set up the output filenames. If more than one file is being written out, we need appended filenames. :return: """ # if there is only one file, use the normal filename with nothing appended. if len(self.noaa_data_sorted["Data"]) == 1: self.output_filenames.append(self.filename_txt) # if there are multiple files that need to be written out, (multiple data table pairs), then append numbers elif len(self.noaa_data_sorted["Data"]) > 1: for i in range(0, self.output_file_ct): tmp_name = "{}-{}.txt".format(self.dsn, i+1) self.output_filenames.append(tmp_name) return
python
def __get_output_filenames(self): """ Set up the output filenames. If more than one file is being written out, we need appended filenames. :return: """ # if there is only one file, use the normal filename with nothing appended. if len(self.noaa_data_sorted["Data"]) == 1: self.output_filenames.append(self.filename_txt) # if there are multiple files that need to be written out, (multiple data table pairs), then append numbers elif len(self.noaa_data_sorted["Data"]) > 1: for i in range(0, self.output_file_ct): tmp_name = "{}-{}.txt".format(self.dsn, i+1) self.output_filenames.append(tmp_name) return
[ "def", "__get_output_filenames", "(", "self", ")", ":", "# if there is only one file, use the normal filename with nothing appended.", "if", "len", "(", "self", ".", "noaa_data_sorted", "[", "\"Data\"", "]", ")", "==", "1", ":", "self", ".", "output_filenames", ".", "...
Set up the output filenames. If more than one file is being written out, we need appended filenames. :return:
[ "Set", "up", "the", "output", "filenames", ".", "If", "more", "than", "one", "file", "is", "being", "written", "out", "we", "need", "appended", "filenames", ".", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1012-L1026
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_table_pairs
def __get_table_pairs(self): """ Use the tables in self.paleos and self.chrons (sorted by idx) to put in self.noaa_data_sorted. :return: """ try: for _idx, _p in enumerate(self.data_paleos): _c = {} try: _c = self.data_chrons[_idx] except IndexError: pass # create entry in self object collection of data tables self.noaa_data_sorted["Data"].append({"paleo": _p, "chron": _c}) except KeyError: logger_lpd_noaa.warning("lpd_noaa: get_meas_table: 0 paleo data tables") self.output_file_ct = len(self.noaa_data_sorted["Data"]) return
python
def __get_table_pairs(self): """ Use the tables in self.paleos and self.chrons (sorted by idx) to put in self.noaa_data_sorted. :return: """ try: for _idx, _p in enumerate(self.data_paleos): _c = {} try: _c = self.data_chrons[_idx] except IndexError: pass # create entry in self object collection of data tables self.noaa_data_sorted["Data"].append({"paleo": _p, "chron": _c}) except KeyError: logger_lpd_noaa.warning("lpd_noaa: get_meas_table: 0 paleo data tables") self.output_file_ct = len(self.noaa_data_sorted["Data"]) return
[ "def", "__get_table_pairs", "(", "self", ")", ":", "try", ":", "for", "_idx", ",", "_p", "in", "enumerate", "(", "self", ".", "data_paleos", ")", ":", "_c", "=", "{", "}", "try", ":", "_c", "=", "self", ".", "data_chrons", "[", "_idx", "]", "except...
Use the tables in self.paleos and self.chrons (sorted by idx) to put in self.noaa_data_sorted. :return:
[ "Use", "the", "tables", "in", "self", ".", "paleos", "and", "self", ".", "chrons", "(", "sorted", "by", "idx", ")", "to", "put", "in", "self", ".", "noaa_data_sorted", ".", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1028-L1046
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__get_table_count
def __get_table_count(self): """ Check how many tables are in the json :return int: """ _count = 0 try: keys = ["paleo", "paleoData", "paleoMeasurementTable"] # get the count for how many tables we have. so we know to make appended filenames or not. for pd_name, pd_data in self.lipd_data["paleoData"].items(): for section_name, section_data in pd_data.items(): _count += len(section_data) except Exception: pass if _count > 1: self.append_filenames = True return
python
def __get_table_count(self): """ Check how many tables are in the json :return int: """ _count = 0 try: keys = ["paleo", "paleoData", "paleoMeasurementTable"] # get the count for how many tables we have. so we know to make appended filenames or not. for pd_name, pd_data in self.lipd_data["paleoData"].items(): for section_name, section_data in pd_data.items(): _count += len(section_data) except Exception: pass if _count > 1: self.append_filenames = True return
[ "def", "__get_table_count", "(", "self", ")", ":", "_count", "=", "0", "try", ":", "keys", "=", "[", "\"paleo\"", ",", "\"paleoData\"", ",", "\"paleoMeasurementTable\"", "]", "# get the count for how many tables we have. so we know to make appended filenames or not.", "for"...
Check how many tables are in the json :return int:
[ "Check", "how", "many", "tables", "are", "in", "the", "json", ":", "return", "int", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1048-L1067
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__put_tables_in_self
def __put_tables_in_self(self, keys): """ Metadata is sorted-by-name. Use this to put the table data into the object self. :param list keys: Paleo or Chron keys :return none: """ _idx = 1 try: # start adding the filenames to the JSON and adding the tables to the self tables for pd_name, pd_data in self.lipd_data[keys[1]].items(): for table_name, table_data in pd_data[keys[2]].items(): if self.append_filenames: _url = "{}/{}-{}.txt".format(self.wds_url, self.dsn, _idx) _url = re.sub("['{}@!$&*+,;?%#~`\[\]=]", "", _url) self.lipd_data[keys[1]][pd_name][keys[2]][table_name]["WDSPaleoUrl"] = _url table_data["WDSPaleoUrl"] = _url if keys[0] == "paleo": self.data_paleos.append(table_data) else: self.data_chrons.append(table_data) else: _url = re.sub("['{}@!$&*+,;?%#~`\[\]=]", "", self.txt_file_url) self.lipd_data[keys[1]][pd_name][keys[2]][table_name]["WDSPaleoUrl"] = _url table_data["WDSPaleoUrl"] = _url if keys[0] == "paleo": self.data_paleos.append(table_data) else: self.data_chrons.append(table_data) _idx += 1 except Exception: pass return
python
def __put_tables_in_self(self, keys): """ Metadata is sorted-by-name. Use this to put the table data into the object self. :param list keys: Paleo or Chron keys :return none: """ _idx = 1 try: # start adding the filenames to the JSON and adding the tables to the self tables for pd_name, pd_data in self.lipd_data[keys[1]].items(): for table_name, table_data in pd_data[keys[2]].items(): if self.append_filenames: _url = "{}/{}-{}.txt".format(self.wds_url, self.dsn, _idx) _url = re.sub("['{}@!$&*+,;?%#~`\[\]=]", "", _url) self.lipd_data[keys[1]][pd_name][keys[2]][table_name]["WDSPaleoUrl"] = _url table_data["WDSPaleoUrl"] = _url if keys[0] == "paleo": self.data_paleos.append(table_data) else: self.data_chrons.append(table_data) else: _url = re.sub("['{}@!$&*+,;?%#~`\[\]=]", "", self.txt_file_url) self.lipd_data[keys[1]][pd_name][keys[2]][table_name]["WDSPaleoUrl"] = _url table_data["WDSPaleoUrl"] = _url if keys[0] == "paleo": self.data_paleos.append(table_data) else: self.data_chrons.append(table_data) _idx += 1 except Exception: pass return
[ "def", "__put_tables_in_self", "(", "self", ",", "keys", ")", ":", "_idx", "=", "1", "try", ":", "# start adding the filenames to the JSON and adding the tables to the self tables", "for", "pd_name", ",", "pd_data", "in", "self", ".", "lipd_data", "[", "keys", "[", ...
Metadata is sorted-by-name. Use this to put the table data into the object self. :param list keys: Paleo or Chron keys :return none:
[ "Metadata", "is", "sorted", "-", "by", "-", "name", ".", "Use", "this", "to", "put", "the", "table", "data", "into", "the", "object", "self", ".", ":", "param", "list", "keys", ":", "Paleo", "or", "Chron", "keys", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1069-L1101
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__create_file
def __create_file(self): """ Open text file. Write one section at a time. Close text file. Move completed file to dir_root/noaa/ :return none: """ logger_lpd_noaa.info("enter create_file") self.__get_output_filenames() for idx, filename in enumerate(self.output_filenames): try: # self.noaa_txt = open(os.path.join(self.path, filename), "w+") # self.noaa_file_output[filename] = "" # self.noaa_txt = self.noaa_file_output[filename] self.noaa_txt = "" print("writing: {}".format(filename)) logger_lpd_noaa.info("write_file: opened output txt file") except Exception as e: logger_lpd_noaa.error("write_file: failed to open output txt file, {}".format(e)) return self.__get_max_min_time_1(self.noaa_data_sorted["Data"][idx]["paleo"]) self.__check_time_values() self.__write_top(filename) self.__write_generic('Contribution_Date') self.__write_generic('File_Last_Modified_Date') self.__write_generic('Title') self.__write_generic('Investigators') self.__write_generic('Description_Notes_and_Keywords') self.__write_pub() self.__write_funding() self.__write_geo() self.__write_generic('Data_Collection') self.__write_generic('Species') self.__write_data(idx) self.noaa_file_output[filename] = self.noaa_txt # logger_lpd_noaa.info("closed output text file") # reset the max min time unit to none self.max_min_time = {"min": "", "max": "", "time": ""} # shutil.copy(os.path.join(os.getcwd(), filename), self.dir_root) logger_lpd_noaa.info("exit create_file") return
python
def __create_file(self): """ Open text file. Write one section at a time. Close text file. Move completed file to dir_root/noaa/ :return none: """ logger_lpd_noaa.info("enter create_file") self.__get_output_filenames() for idx, filename in enumerate(self.output_filenames): try: # self.noaa_txt = open(os.path.join(self.path, filename), "w+") # self.noaa_file_output[filename] = "" # self.noaa_txt = self.noaa_file_output[filename] self.noaa_txt = "" print("writing: {}".format(filename)) logger_lpd_noaa.info("write_file: opened output txt file") except Exception as e: logger_lpd_noaa.error("write_file: failed to open output txt file, {}".format(e)) return self.__get_max_min_time_1(self.noaa_data_sorted["Data"][idx]["paleo"]) self.__check_time_values() self.__write_top(filename) self.__write_generic('Contribution_Date') self.__write_generic('File_Last_Modified_Date') self.__write_generic('Title') self.__write_generic('Investigators') self.__write_generic('Description_Notes_and_Keywords') self.__write_pub() self.__write_funding() self.__write_geo() self.__write_generic('Data_Collection') self.__write_generic('Species') self.__write_data(idx) self.noaa_file_output[filename] = self.noaa_txt # logger_lpd_noaa.info("closed output text file") # reset the max min time unit to none self.max_min_time = {"min": "", "max": "", "time": ""} # shutil.copy(os.path.join(os.getcwd(), filename), self.dir_root) logger_lpd_noaa.info("exit create_file") return
[ "def", "__create_file", "(", "self", ")", ":", "logger_lpd_noaa", ".", "info", "(", "\"enter create_file\"", ")", "self", ".", "__get_output_filenames", "(", ")", "for", "idx", ",", "filename", "in", "enumerate", "(", "self", ".", "output_filenames", ")", ":",...
Open text file. Write one section at a time. Close text file. Move completed file to dir_root/noaa/ :return none:
[ "Open", "text", "file", ".", "Write", "one", "section", "at", "a", "time", ".", "Close", "text", "file", ".", "Move", "completed", "file", "to", "dir_root", "/", "noaa", "/", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1113-L1156
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__write_top
def __write_top(self, filename_txt): """ Write the top section of the txt file. :param int section_num: Section number :return none: """ logger_lpd_noaa.info("writing section: {}".format("top")) self.__create_blanks("Top", self.noaa_data_sorted["Top"]) # Start writing the NOAA file section by section, starting at the very top of the template. self.noaa_txt += "# {}".format(self.noaa_data_sorted["Top"]['Study_Name']) self.__write_template_top() # We don't know what the full online resource path will be yet, so leave the base path only self.__write_k_v("Online_Resource", "{}/{}".format(self.wds_url, filename_txt), top=True) self.__write_k_v("Online_Resource_Description", " This file. NOAA WDS Paleo formatted metadata and data for version {} of this dataset.".format(self.version), indent=True) self.__write_k_v("Online_Resource", "{}".format(self.lpd_file_url), top=True) self.__write_k_v("Online_Resource_Description", " Linked Paleo Data (LiPD) formatted file containing the same metadata and data as this file, for version {} of this dataset.".format(self.version), indent=True) self.__write_k_v("Original_Source_URL", self.noaa_data_sorted["Top"]['Original_Source_URL'], top=True) self.noaa_txt += "\n# Description/Documentation lines begin with #\n# Data lines have no #\n#" self.__write_k_v("Archive", self.noaa_data_sorted["Top"]['Archive']) self.__write_k_v("Parameter_Keywords", self.noaa_data_sorted["Top"]['Parameter_Keywords']) # get the doi from the pub section of self.steps_dict[6] # doi = self.__get_doi() self.__write_k_v("Dataset_DOI", ', '.join(self.doi), False, True, False, False) # self.__write_k_v("Parameter_Keywords", self.steps_dict[section_num]['parameterKeywords']) self.__write_divider() logger_lpd_noaa.info("exit write_top") return
python
def __write_top(self, filename_txt): """ Write the top section of the txt file. :param int section_num: Section number :return none: """ logger_lpd_noaa.info("writing section: {}".format("top")) self.__create_blanks("Top", self.noaa_data_sorted["Top"]) # Start writing the NOAA file section by section, starting at the very top of the template. self.noaa_txt += "# {}".format(self.noaa_data_sorted["Top"]['Study_Name']) self.__write_template_top() # We don't know what the full online resource path will be yet, so leave the base path only self.__write_k_v("Online_Resource", "{}/{}".format(self.wds_url, filename_txt), top=True) self.__write_k_v("Online_Resource_Description", " This file. NOAA WDS Paleo formatted metadata and data for version {} of this dataset.".format(self.version), indent=True) self.__write_k_v("Online_Resource", "{}".format(self.lpd_file_url), top=True) self.__write_k_v("Online_Resource_Description", " Linked Paleo Data (LiPD) formatted file containing the same metadata and data as this file, for version {} of this dataset.".format(self.version), indent=True) self.__write_k_v("Original_Source_URL", self.noaa_data_sorted["Top"]['Original_Source_URL'], top=True) self.noaa_txt += "\n# Description/Documentation lines begin with #\n# Data lines have no #\n#" self.__write_k_v("Archive", self.noaa_data_sorted["Top"]['Archive']) self.__write_k_v("Parameter_Keywords", self.noaa_data_sorted["Top"]['Parameter_Keywords']) # get the doi from the pub section of self.steps_dict[6] # doi = self.__get_doi() self.__write_k_v("Dataset_DOI", ', '.join(self.doi), False, True, False, False) # self.__write_k_v("Parameter_Keywords", self.steps_dict[section_num]['parameterKeywords']) self.__write_divider() logger_lpd_noaa.info("exit write_top") return
[ "def", "__write_top", "(", "self", ",", "filename_txt", ")", ":", "logger_lpd_noaa", ".", "info", "(", "\"writing section: {}\"", ".", "format", "(", "\"top\"", ")", ")", "self", ".", "__create_blanks", "(", "\"Top\"", ",", "self", ".", "noaa_data_sorted", "["...
Write the top section of the txt file. :param int section_num: Section number :return none:
[ "Write", "the", "top", "section", "of", "the", "txt", "file", ".", ":", "param", "int", "section_num", ":", "Section", "number", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1158-L1186
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__write_generic
def __write_generic(self, header, d=None): """ Write a generic section to the .txt. This function is reused for multiple sections. :param str header: Header title for this section :param int section_num: :param dict d: Section from steps_dict :return none: """ logger_lpd_noaa.info("writing section: {}".format(header)) if not d: d = self.noaa_data_sorted[header] d = self.__create_blanks(header, d) self.__write_header_name(header) for key in NOAA_KEYS_BY_SECTION[header]: key = self.__get_noaa_key_w_context(header, key) # NOAA writes value and units on one line. Build the string here. # if key == 'coreLength': # value, unit = self.__get_corelength(val) # val = str(value) + " " + str(unit) # DOI id is nested in "identifier" block. Retrieve it. if key == "DOI": val = self.__get_doi(d) # Don't write out an empty DOI list. Write out empty string instead. if not val: val = "" elif key == "Investigators": # Investigators is a section by itself. "d" should be a direct list val = get_authors_as_str(d) # If we don't have investigator data, use the authors from the first publication entry if not val: val = self.__create_author_investigator_str() # lipd uses a singular "author" key, while NOAA uses plural "authors" key. elif key in ["Author", "Authors"]: # "d" has all publication data, so only pass through the d["Authors"] piece val = get_authors_as_str(d[key]) elif key == "Collection_Name": if not d[key]: # If there is not a collection name, then use the dsn so _something_ is there. val = self.dsn else: val = d[key] # Write the output line self.__write_k_v(str(self.__get_noaa_key(key)), val, indent=True) # Don't write a divider if there isn't a Chron section after species. It'll make a double. # if header == "Species" and not self.noaa_data_sorted["Species"]: # return self.__write_divider() return
python
def __write_generic(self, header, d=None): """ Write a generic section to the .txt. This function is reused for multiple sections. :param str header: Header title for this section :param int section_num: :param dict d: Section from steps_dict :return none: """ logger_lpd_noaa.info("writing section: {}".format(header)) if not d: d = self.noaa_data_sorted[header] d = self.__create_blanks(header, d) self.__write_header_name(header) for key in NOAA_KEYS_BY_SECTION[header]: key = self.__get_noaa_key_w_context(header, key) # NOAA writes value and units on one line. Build the string here. # if key == 'coreLength': # value, unit = self.__get_corelength(val) # val = str(value) + " " + str(unit) # DOI id is nested in "identifier" block. Retrieve it. if key == "DOI": val = self.__get_doi(d) # Don't write out an empty DOI list. Write out empty string instead. if not val: val = "" elif key == "Investigators": # Investigators is a section by itself. "d" should be a direct list val = get_authors_as_str(d) # If we don't have investigator data, use the authors from the first publication entry if not val: val = self.__create_author_investigator_str() # lipd uses a singular "author" key, while NOAA uses plural "authors" key. elif key in ["Author", "Authors"]: # "d" has all publication data, so only pass through the d["Authors"] piece val = get_authors_as_str(d[key]) elif key == "Collection_Name": if not d[key]: # If there is not a collection name, then use the dsn so _something_ is there. val = self.dsn else: val = d[key] # Write the output line self.__write_k_v(str(self.__get_noaa_key(key)), val, indent=True) # Don't write a divider if there isn't a Chron section after species. It'll make a double. # if header == "Species" and not self.noaa_data_sorted["Species"]: # return self.__write_divider() return
[ "def", "__write_generic", "(", "self", ",", "header", ",", "d", "=", "None", ")", ":", "logger_lpd_noaa", ".", "info", "(", "\"writing section: {}\"", ".", "format", "(", "header", ")", ")", "if", "not", "d", ":", "d", "=", "self", ".", "noaa_data_sorted...
Write a generic section to the .txt. This function is reused for multiple sections. :param str header: Header title for this section :param int section_num: :param dict d: Section from steps_dict :return none:
[ "Write", "a", "generic", "section", "to", "the", ".", "txt", ".", "This", "function", "is", "reused", "for", "multiple", "sections", ".", ":", "param", "str", "header", ":", "Header", "title", "for", "this", "section", ":", "param", "int", "section_num", ...
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1188-L1236
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__write_pub
def __write_pub(self): """ Write pub section. There may be multiple, so write a generic section for each one. :return none: """ try: self.__reorganize_author() # Check all publications, and remove possible duplicate Full_Citations self.__rm_duplicate_citation_1() if not self.noaa_data_sorted["Publication"]: self.noaa_data_sorted["Publication"].append({"pubYear": ""}) for idx, pub in enumerate(self.noaa_data_sorted["Publication"]): logger_lpd_noaa.info("publication: {}".format(idx)) # Do not write out Data Citation publications. Check, and skip if necessary is_data_citation = self.__get_pub_type(pub) if not is_data_citation or is_data_citation and len(self.noaa_data_sorted["Publication"]) == 1: pub = self.__convert_keys_1("Publication", pub) self.__write_generic('Publication', pub) except KeyError: logger_lpd_noaa.info("write_pub: KeyError: pub section not found") except TypeError: logger_lpd_noaa.debug("write_pub: TypeError: pub not a list type") return
python
def __write_pub(self): """ Write pub section. There may be multiple, so write a generic section for each one. :return none: """ try: self.__reorganize_author() # Check all publications, and remove possible duplicate Full_Citations self.__rm_duplicate_citation_1() if not self.noaa_data_sorted["Publication"]: self.noaa_data_sorted["Publication"].append({"pubYear": ""}) for idx, pub in enumerate(self.noaa_data_sorted["Publication"]): logger_lpd_noaa.info("publication: {}".format(idx)) # Do not write out Data Citation publications. Check, and skip if necessary is_data_citation = self.__get_pub_type(pub) if not is_data_citation or is_data_citation and len(self.noaa_data_sorted["Publication"]) == 1: pub = self.__convert_keys_1("Publication", pub) self.__write_generic('Publication', pub) except KeyError: logger_lpd_noaa.info("write_pub: KeyError: pub section not found") except TypeError: logger_lpd_noaa.debug("write_pub: TypeError: pub not a list type") return
[ "def", "__write_pub", "(", "self", ")", ":", "try", ":", "self", ".", "__reorganize_author", "(", ")", "# Check all publications, and remove possible duplicate Full_Citations", "self", ".", "__rm_duplicate_citation_1", "(", ")", "if", "not", "self", ".", "noaa_data_sort...
Write pub section. There may be multiple, so write a generic section for each one. :return none:
[ "Write", "pub", "section", ".", "There", "may", "be", "multiple", "so", "write", "a", "generic", "section", "for", "each", "one", ".", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1238-L1261
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__write_funding
def __write_funding(self): """ Write funding section. There are likely multiple entries. :param dict d: :return none: """ self.__reorganize_funding() # if funding is empty, insert a blank entry so that it'll still write the empty section on the template. if not self.noaa_data_sorted["Funding_Agency"]: self.noaa_data_sorted["Funding_Agency"].append({"grant": "", "agency": ""}) for idx, entry in enumerate(self.noaa_data_sorted["Funding_Agency"]): logger_lpd_noaa.info("funding: {}".format(idx)) self.__write_generic('Funding_Agency', entry) return
python
def __write_funding(self): """ Write funding section. There are likely multiple entries. :param dict d: :return none: """ self.__reorganize_funding() # if funding is empty, insert a blank entry so that it'll still write the empty section on the template. if not self.noaa_data_sorted["Funding_Agency"]: self.noaa_data_sorted["Funding_Agency"].append({"grant": "", "agency": ""}) for idx, entry in enumerate(self.noaa_data_sorted["Funding_Agency"]): logger_lpd_noaa.info("funding: {}".format(idx)) self.__write_generic('Funding_Agency', entry) return
[ "def", "__write_funding", "(", "self", ")", ":", "self", ".", "__reorganize_funding", "(", ")", "# if funding is empty, insert a blank entry so that it'll still write the empty section on the template.", "if", "not", "self", ".", "noaa_data_sorted", "[", "\"Funding_Agency\"", "...
Write funding section. There are likely multiple entries. :param dict d: :return none:
[ "Write", "funding", "section", ".", "There", "are", "likely", "multiple", "entries", ".", ":", "param", "dict", "d", ":", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1263-L1276
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__write_data
def __write_data(self, idx): """ Write out the measurement tables found in paleoData and chronData :return: """ pair = self.noaa_data_sorted["Data"][idx] # Run once for each pair (paleo+chron) of tables that was gathered earlier. # for idx, pair in enumerate(self.noaa_data_sorted["Data"]): lst_pc = ["chron", "paleo"] # loop once for paleo, once for chron for pc in lst_pc: # safeguard in case the table is an empty set. table = pair[pc] if pc == "paleo": self.__write_variables_1(table) self.__write_divider() self.__write_columns(pc, table) elif pc == "chron": # self.__write_variables_1(table) self.__write_columns(pc, table) self.__write_divider(nl=False) return
python
def __write_data(self, idx): """ Write out the measurement tables found in paleoData and chronData :return: """ pair = self.noaa_data_sorted["Data"][idx] # Run once for each pair (paleo+chron) of tables that was gathered earlier. # for idx, pair in enumerate(self.noaa_data_sorted["Data"]): lst_pc = ["chron", "paleo"] # loop once for paleo, once for chron for pc in lst_pc: # safeguard in case the table is an empty set. table = pair[pc] if pc == "paleo": self.__write_variables_1(table) self.__write_divider() self.__write_columns(pc, table) elif pc == "chron": # self.__write_variables_1(table) self.__write_columns(pc, table) self.__write_divider(nl=False) return
[ "def", "__write_data", "(", "self", ",", "idx", ")", ":", "pair", "=", "self", ".", "noaa_data_sorted", "[", "\"Data\"", "]", "[", "idx", "]", "# Run once for each pair (paleo+chron) of tables that was gathered earlier.", "# for idx, pair in enumerate(self.noaa_data_sorted[\"...
Write out the measurement tables found in paleoData and chronData :return:
[ "Write", "out", "the", "measurement", "tables", "found", "in", "paleoData", "and", "chronData", ":", "return", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1288-L1309
nickmckay/LiPD-utilities
Python/lipd/lpd_noaa.py
LPD_NOAA.__write_variables_1
def __write_variables_1(self, table): """ Retrieve variables from data table(s) and write to Variables section of txt file. :param dict table: Paleodata :return none: """ logger_lpd_noaa.info("writing section: {}".format("Variables")) # Write the template lines first self.__write_template_variable() try: self.noaa_txt += '#' # Special NOAA Request: Write the "year" column first always, if available # write year data first, when available for name, data in table["columns"].items(): if name == "year": # write first line in variables section here self.__write_variables_2(data) # leave the loop, because this is all we needed to accomplish break # all other cases for name, data in table["columns"].items(): # we already wrote out the year column, so don't duplicate. if name != "year": self.__write_variables_2(data) except KeyError as e: logger_lpd_noaa.warn("write_variables: KeyError: {} not found".format(e)) return
python
def __write_variables_1(self, table): """ Retrieve variables from data table(s) and write to Variables section of txt file. :param dict table: Paleodata :return none: """ logger_lpd_noaa.info("writing section: {}".format("Variables")) # Write the template lines first self.__write_template_variable() try: self.noaa_txt += '#' # Special NOAA Request: Write the "year" column first always, if available # write year data first, when available for name, data in table["columns"].items(): if name == "year": # write first line in variables section here self.__write_variables_2(data) # leave the loop, because this is all we needed to accomplish break # all other cases for name, data in table["columns"].items(): # we already wrote out the year column, so don't duplicate. if name != "year": self.__write_variables_2(data) except KeyError as e: logger_lpd_noaa.warn("write_variables: KeyError: {} not found".format(e)) return
[ "def", "__write_variables_1", "(", "self", ",", "table", ")", ":", "logger_lpd_noaa", ".", "info", "(", "\"writing section: {}\"", ".", "format", "(", "\"Variables\"", ")", ")", "# Write the template lines first", "self", ".", "__write_template_variable", "(", ")", ...
Retrieve variables from data table(s) and write to Variables section of txt file. :param dict table: Paleodata :return none:
[ "Retrieve", "variables", "from", "data", "table", "(", "s", ")", "and", "write", "to", "Variables", "section", "of", "txt", "file", ".", ":", "param", "dict", "table", ":", "Paleodata", ":", "return", "none", ":" ]
train
https://github.com/nickmckay/LiPD-utilities/blob/5dab6bbeffc5effd68e3a6beaca6b76aa928e860/Python/lipd/lpd_noaa.py#L1311-L1342