-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from NOAA-GFDL/main
Merging in changes from main
- Loading branch information
Showing
15 changed files
with
906 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
''' for fre.cmor imports ''' | ||
from .cmor_mixer import cmor_run_subtool | ||
from .cmor_lister import cmor_list_subtool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
''' fre cmor list | ||
because ian got tired of typing things like the following in bash... | ||
varname=sos; \ | ||
table_files=$(ls fre/tests/test_files/cmip6-cmor-tables/Tables/CMIP6_*.json); \ | ||
for table_file in $table_files; do \ | ||
echo $table_file; \ | ||
cat $table_file | grep -A 10 "\"$varname\""; \ | ||
done; | ||
''' | ||
|
||
#import os | ||
import glob | ||
import json | ||
#import shutil | ||
#import subprocess | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
DO_NOT_PRINT_LIST=[ 'comment', | ||
'ok_min_mean_abs', 'ok_max_mean_abs', | ||
'valid_min', 'valid_max' ] | ||
|
||
def print_var_content( table_config_file = None, var_name = None): | ||
''' one variable printing routine- looks for info regarding var_name in table_config_file ''' | ||
try: | ||
proj_table_vars=json.load(table_config_file) | ||
except Exception as exc: | ||
raise Exception(f'problem getting proj_table_vars... WHY') | ||
|
||
var_content = None | ||
try: | ||
var_content = proj_table_vars["variable_entry"].get(var_name) | ||
except: | ||
#print(f'(cmor_list_subtool) WARNING no "variable_entry" key. for {json_table_config}.' | ||
# ' not the right json file probably. moving on!') | ||
return | ||
|
||
if var_content is None: | ||
#print(f'(cmor_list_subtool) variable {var_name} not found in {Path(json_table_config).name}, moving on!') | ||
return | ||
|
||
table_name = None | ||
try: | ||
#print(f'(print_var_content) trying to get table_name from proj_table_vars...') | ||
#print(f' table header is {proj_table_vars["Header"]}') | ||
table_name = proj_table_vars["Header"].get('table_id').split(' ')[1] | ||
#print(f' table_name = {table_name}') | ||
except: | ||
print(f'print_var_content) WARNING couldnt get header and table_name field') | ||
pass | ||
|
||
if table_name is not None: | ||
print(f'(print_var_content) found {var_name} data in table {table_name}!') | ||
else: | ||
print(f'(print_var_content) found {var_name} data in table, but not its table_name!') | ||
|
||
print(f' variable key: {var_name}') | ||
for content in var_content: | ||
if content in DO_NOT_PRINT_LIST: | ||
continue | ||
print(f' {content}: {var_content[content]}') | ||
print('\n') | ||
|
||
return | ||
|
||
def cmor_list_subtool( json_var_list = None, json_table_config_dir = None, opt_var_name = None): | ||
''' | ||
finds tables in the CMIP json config directory containing variable data of interest. prints it | ||
out to screen, intended largely as a helper tool for cli users. | ||
''' | ||
if not Path(json_table_config_dir).exists(): | ||
raise OSError(f'(cmor_list_subtool) ERROR directory {json_table_config_dir} does not exist! exit.') | ||
|
||
print(f'(cmor_list_subtool) attempting to find and open files in dir: \n {json_table_config_dir} ') | ||
json_table_configs=glob.glob(f'{json_table_config_dir}/CMIP6_*.json') | ||
if json_table_configs is None: | ||
raise OSError(f'ERROR directory {json_table_config_dir} contains no JSON files, exit.') | ||
else: | ||
print(f'(cmor_list_subtool) found content in json_table_config_dir')#: {json_table_configs}') | ||
|
||
var_list = None | ||
if json_var_list is not None: | ||
with open( json_var_list, "r", encoding = "utf-8") as var_list_file : | ||
var_list=json.load(var_list_file) | ||
|
||
if opt_var_name is None and var_list is None: | ||
raise ValueError(f'(cmor_list_subtool) ERROR: no opt_var_name given but also no content in variable list!!! exit!') | ||
|
||
if opt_var_name is not None: | ||
print(f'(cmor_list_subtool) opt_var_name is not None: looking for only ONE variables worth of info!') | ||
for json_table_config in json_table_configs: | ||
#print(f'(cmor_list_subtool) attempting to open {json_table_config}') | ||
with open( json_table_config, "r", encoding = "utf-8") as table_config_file: | ||
print_var_content(table_config_file, opt_var_name) | ||
|
||
elif var_list is not None: | ||
print(f'(cmor_list_subtool) opt_var_name is None, and var_list is not None, looking for many variables worth of info!') | ||
for var in var_list: | ||
for json_table_config in json_table_configs: | ||
#print(f'(cmor_list_subtool) attempting to open {json_table_config}') | ||
with open( json_table_config, "r", encoding = "utf-8") as table_config_file: | ||
#print(f' var = {var}, var_list[{var}]={var_list[var]}') | ||
print_var_content(table_config_file, str(var_list[var])) | ||
else: | ||
print(f'(FATAL) this line should be unreachable!!!') | ||
|
||
return | ||
|
||
|
||
@click.command() | ||
def _cmor_list_subtool( json_var_list = None, json_table_config_dir = None, opt_var_name = None): | ||
''' entry point to fre cmor run for click. see cmor_list_subtool for argument descriptions.''' | ||
return cmor_list_subtool(json_var_list, json_table_config_dir, opt_var_name) | ||
|
||
|
||
if __name__ == '__main__': | ||
cmor_list_subtool() |
Oops, something went wrong.