Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string constructor #1008

Merged
merged 9 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmec/scripts/climatologies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
import sys

import genutil
from pcmdi_metrics.utils import StringConstructor


def make_climatologies(settings, model_dir, wk_dir):
Expand All @@ -12,7 +12,7 @@ def make_climatologies(settings, model_dir, wk_dir):
realization = settings.get("realization", "")
period = settings.get("period", "")
tmp = os.path.join(model_dir, filename_template)
model_file = genutil.StringConstructor(tmp)
model_file = StringConstructor(tmp)
model_file.period = period
model_file.realization = realization
out_base = os.path.join(wk_dir, "AC")
Expand Down
97 changes: 55 additions & 42 deletions doc/jupyter/Demo/Demo_2a_monsoon_wang.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"results_dir = 'demo_output/monsoon_wang'\n",
"\n",
"# Threshold\n",
"threshold = 2.5 / 86400\n"
"threshold = 2.5 / 86400\n",
"\n"
]
}
],
Expand Down Expand Up @@ -110,7 +111,6 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Deprecation warning: please use 'import pcmdi_metrics.driver.pmp_parser.PMPParser'\n",
"******************************************************************************************\n",
"demo_data/CMIP5_demo_clims/cmip5.historical.CanCM4.r1i1p1.mon.pr.198101-200512.AC.v20200426.nc\n"
]
Expand All @@ -119,7 +119,14 @@
"name": "stderr",
"output_type": "stream",
"text": [
"INFO::2021-11-15 14:52::pcmdi_metrics:: Results saved to a json file: /Users/ordonez4/Documents/git/pcmdi_metrics/doc/jupyter/Demo/demo_output/monsoon_wang/monsoon_wang.json\n"
"/Users/lee1043/mambaforge/envs/pmp_devel_20230223/lib/python3.9/site-packages/cdms2/avariable.py:1289: Warning: \n",
"avariable.regrid: regridTool = 'esmf' but your version does not\n",
"seems to be built with esmf, will switch to regridTool = 'libcf'\n",
" \n",
" warnings.warn(message, Warning)\n",
"INFO::2023-12-18 12:39::pcmdi_metrics:: Results saved to a json file: /Users/lee1043/Documents/Research/git/pcmdi_metrics_20230620_pcmdi/pcmdi_metrics/doc/jupyter/Demo/demo_output/monsoon_wang/monsoon_wang.json\n",
"2023-12-18 12:39:49,908 [INFO]: base.py(write:250) >> Results saved to a json file: /Users/lee1043/Documents/Research/git/pcmdi_metrics_20230620_pcmdi/pcmdi_metrics/doc/jupyter/Demo/demo_output/monsoon_wang/monsoon_wang.json\n",
"2023-12-18 12:39:49,908 [INFO]: base.py(write:250) >> Results saved to a json file: /Users/lee1043/Documents/Research/git/pcmdi_metrics_20230620_pcmdi/pcmdi_metrics/doc/jupyter/Demo/demo_output/monsoon_wang/monsoon_wang.json\n"
]
}
],
Expand Down Expand Up @@ -148,42 +155,42 @@
" \"CanCM4\": {\n",
" \"AllMW\": {\n",
" \"cor\": \"0.754\",\n",
" \"rmsn\": \"0.692\",\n",
" \"threat_score\": \"0.477\"\n",
" \"rmsn\": \"0.691\",\n",
" \"threat_score\": \"0.479\"\n",
" },\n",
" \"AllM\": {\n",
" \"cor\": \"0.757\",\n",
" \"rmsn\": \"0.691\",\n",
" \"threat_score\": \"0.477\"\n",
" \"rmsn\": \"0.689\",\n",
" \"threat_score\": \"0.479\"\n",
" },\n",
" \"NAMM\": {\n",
" \"cor\": \"0.788\",\n",
" \"rmsn\": \"0.656\",\n",
" \"cor\": \"0.791\",\n",
" \"rmsn\": \"0.650\",\n",
" \"threat_score\": \"0.474\"\n",
" },\n",
" \"SAMM\": {\n",
" \"cor\": \"0.770\",\n",
" \"rmsn\": \"0.698\",\n",
" \"threat_score\": \"0.459\"\n",
" \"rmsn\": \"0.695\",\n",
" \"threat_score\": \"0.456\"\n",
" },\n",
" \"NAFM\": {\n",
" \"cor\": \"0.775\",\n",
" \"rmsn\": \"0.649\",\n",
" \"threat_score\": \"0.409\"\n",
" \"cor\": \"0.776\",\n",
" \"rmsn\": \"0.646\",\n",
" \"threat_score\": \"0.411\"\n",
" },\n",
" \"SAFM\": {\n",
" \"cor\": \"0.782\",\n",
" \"rmsn\": \"0.673\",\n",
" \"cor\": \"0.780\",\n",
" \"rmsn\": \"0.674\",\n",
" \"threat_score\": \"0.645\"\n",
" },\n",
" \"ASM\": {\n",
" \"cor\": \"0.724\",\n",
" \"rmsn\": \"0.715\",\n",
" \"threat_score\": \"0.403\"\n",
" \"cor\": \"0.726\",\n",
" \"rmsn\": \"0.713\",\n",
" \"threat_score\": \"0.405\"\n",
" },\n",
" \"AUSM\": {\n",
" \"cor\": \"0.835\",\n",
" \"rmsn\": \"0.580\",\n",
" \"rmsn\": \"0.578\",\n",
" \"threat_score\": \"0.523\"\n",
" }\n",
" }\n",
Expand Down Expand Up @@ -222,7 +229,6 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Deprecation warning: please use 'import pcmdi_metrics.driver.pmp_parser.PMPParser'\n",
"******************************************************************************************\n",
"demo_data/CMIP5_demo_clims/cmip5.historical.CanCM4.r1i1p1.mon.pr.198101-200512.AC.v20200426.nc\n"
]
Expand All @@ -231,7 +237,14 @@
"name": "stderr",
"output_type": "stream",
"text": [
"INFO::2021-11-15 14:52::pcmdi_metrics:: Results saved to a json file: /Users/ordonez4/Documents/git/pcmdi_metrics/doc/jupyter/Demo/demo_output/monsoon_wang/monsoon_wang_ex2.json\n"
"/Users/lee1043/mambaforge/envs/pmp_devel_20230223/lib/python3.9/site-packages/cdms2/avariable.py:1289: Warning: \n",
"avariable.regrid: regridTool = 'esmf' but your version does not\n",
"seems to be built with esmf, will switch to regridTool = 'libcf'\n",
" \n",
" warnings.warn(message, Warning)\n",
"INFO::2023-12-18 12:40::pcmdi_metrics:: Results saved to a json file: /Users/lee1043/Documents/Research/git/pcmdi_metrics_20230620_pcmdi/pcmdi_metrics/doc/jupyter/Demo/demo_output/monsoon_wang/monsoon_wang_ex2.json\n",
"2023-12-18 12:40:38,357 [INFO]: base.py(write:250) >> Results saved to a json file: /Users/lee1043/Documents/Research/git/pcmdi_metrics_20230620_pcmdi/pcmdi_metrics/doc/jupyter/Demo/demo_output/monsoon_wang/monsoon_wang_ex2.json\n",
"2023-12-18 12:40:38,357 [INFO]: base.py(write:250) >> Results saved to a json file: /Users/lee1043/Documents/Research/git/pcmdi_metrics_20230620_pcmdi/pcmdi_metrics/doc/jupyter/Demo/demo_output/monsoon_wang/monsoon_wang_ex2.json\n"
]
}
],
Expand Down Expand Up @@ -265,43 +278,43 @@
" \"CanCM4\": {\n",
" \"AllMW\": {\n",
" \"cor\": \"0.754\",\n",
" \"rmsn\": \"0.692\",\n",
" \"rmsn\": \"0.691\",\n",
" \"threat_score\": \"0.457\"\n",
" },\n",
" \"AllM\": {\n",
" \"cor\": \"0.757\",\n",
" \"rmsn\": \"0.691\",\n",
" \"rmsn\": \"0.689\",\n",
" \"threat_score\": \"0.457\"\n",
" },\n",
" \"NAMM\": {\n",
" \"cor\": \"0.788\",\n",
" \"rmsn\": \"0.656\",\n",
" \"threat_score\": \"0.476\"\n",
" \"cor\": \"0.791\",\n",
" \"rmsn\": \"0.650\",\n",
" \"threat_score\": \"0.473\"\n",
" },\n",
" \"SAMM\": {\n",
" \"cor\": \"0.770\",\n",
" \"rmsn\": \"0.698\",\n",
" \"threat_score\": \"0.466\"\n",
" \"rmsn\": \"0.695\",\n",
" \"threat_score\": \"0.458\"\n",
" },\n",
" \"NAFM\": {\n",
" \"cor\": \"0.775\",\n",
" \"rmsn\": \"0.649\",\n",
" \"threat_score\": \"0.385\"\n",
" \"cor\": \"0.776\",\n",
" \"rmsn\": \"0.646\",\n",
" \"threat_score\": \"0.387\"\n",
" },\n",
" \"SAFM\": {\n",
" \"cor\": \"0.782\",\n",
" \"rmsn\": \"0.673\",\n",
" \"threat_score\": \"0.660\"\n",
" \"cor\": \"0.780\",\n",
" \"rmsn\": \"0.674\",\n",
" \"threat_score\": \"0.667\"\n",
" },\n",
" \"ASM\": {\n",
" \"cor\": \"0.724\",\n",
" \"rmsn\": \"0.715\",\n",
" \"threat_score\": \"0.365\"\n",
" \"cor\": \"0.726\",\n",
" \"rmsn\": \"0.713\",\n",
" \"threat_score\": \"0.368\"\n",
" },\n",
" \"AUSM\": {\n",
" \"cor\": \"0.835\",\n",
" \"rmsn\": \"0.580\",\n",
" \"threat_score\": \"0.429\"\n",
" \"rmsn\": \"0.578\",\n",
" \"threat_score\": \"0.435\"\n",
" }\n",
" }\n",
"}\n"
Expand All @@ -324,7 +337,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -338,7 +351,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
"version": "3.9.7"
},
"selected_variables": [],
"vcdat_file_path": "",
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/enso/enso_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
defCollection,
)
from EnsoMetrics.EnsoComputeMetricsLib import ComputeCollection
from genutil import StringConstructor

from pcmdi_metrics import resources
from pcmdi_metrics.enso.lib import (
Expand All @@ -25,8 +24,8 @@
get_file,
match_obs_name,
metrics_to_json,
sort_human,
)
from pcmdi_metrics.utils import StringConstructor, sort_human

# To avoid below error when using multi cores
# OpenBLAS blas_thread_init: pthread_create failed for thread XX of 96: Resource temporarily unavailable
Expand Down
4 changes: 1 addition & 3 deletions pcmdi_metrics/enso/scripts_pcmdi/enso_driver_obsOnly.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
# =================================================
# Dependencies
# -------------------------------------------------
from __future__ import print_function

import glob
import json
import os

from EnsoMetrics.EnsoCollectionsLib import ReferenceObservations, defCollection
from EnsoMetrics.EnsoComputeMetricsLib import ComputeCollection_ObsOnly
from genutil import StringConstructor

from pcmdi_metrics import resources
from pcmdi_metrics.enso.lib import AddParserArgument, metrics_to_json
from pcmdi_metrics.utils import StringConstructor

# To avoid below error when using multi cores
# OpenBLAS blas_thread_init: pthread_create failed for thread XX of 96: Resource temporarily unavailable
Expand Down
4 changes: 1 addition & 3 deletions pcmdi_metrics/enso/scripts_pcmdi/parallel_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
import glob
import os

from genutil import StringConstructor

from pcmdi_metrics.enso.lib import AddParserArgument, find_realm
from pcmdi_metrics.misc.scripts import parallel_submitter
from pcmdi_metrics.variability_mode.lib import sort_human
from pcmdi_metrics.utils import StringConstructor, sort_human

# =================================================
# Collect user defined options
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/enso/scripts_pcmdi/post_process_merge_jsons.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import json
import os

from genutil import StringConstructor

from pcmdi_metrics.utils import StringConstructor
from pcmdi_metrics.variability_mode.lib import dict_merge


Expand Down
3 changes: 2 additions & 1 deletion pcmdi_metrics/graphics/deprecated/portraits.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import numpy
import pkg_resources
import vcs
from genutil import StringConstructor

from pcmdi_metrics.utils import StringConstructor

pmp_egg_path = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share"
Expand Down
7 changes: 4 additions & 3 deletions pcmdi_metrics/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import pcmdi_metrics
from pcmdi_metrics import LOG_LEVEL
from pcmdi_metrics.utils import StringConstructor

value = 0
cdms2.setNetcdfShuffleFlag(value) # where value is either 0 or 1
Expand Down Expand Up @@ -143,9 +144,9 @@ def default(self, o):
return {o.id: "cdutil.region.domain(%s)" % args}


class Base(cdp.cdp_io.CDPIO, genutil.StringConstructor):
class Base(cdp.cdp_io.CDPIO, StringConstructor):
def __init__(self, root, file_template, file_mask_template=None):
genutil.StringConstructor.__init__(self, root + "/" + file_template)
StringConstructor.__init__(self, root + "/" + file_template)
self.target_grid = None
self.mask = None
self.target_mask = None
Expand All @@ -156,7 +157,7 @@ def __init__(self, root, file_template, file_mask_template=None):
self.setup_cdms2()

def __call__(self):
path = os.path.abspath(genutil.StringConstructor.__call__(self))
path = os.path.abspath(StringConstructor.__call__(self))
if self.type in path:
return path
else:
Expand Down
10 changes: 5 additions & 5 deletions pcmdi_metrics/mean_climate/lib/pmp_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os

import cdp.cdp_parameter
import genutil

from pcmdi_metrics import LOG_LEVEL
from pcmdi_metrics.utils import StringConstructor

try:
basestring # noqa
Expand All @@ -17,12 +17,12 @@ def __init__(self):
logging.getLogger("pmp").setLevel(LOG_LEVEL)

def process_templated_argument(self, name, default_value="*", extras=None):
"""Applies arg parse values to a genutil.StringConstructor template type argument
"""Applies arg parse values to a StringConstructor template type argument
Input:
name: name of the argument to process
extra: other object(s) to get keys from, superseeds argparse object
Output:
formatted argument as a genutil.StringConstructor
formatted argument as a StringConstructor
"""

process = getattr(self, name, None)
Expand All @@ -31,7 +31,7 @@ def process_templated_argument(self, name, default_value="*", extras=None):
): # Ok not an argument from arg_parse maybe a template or string constructor itself
if isinstance(name, basestring):
process = name
elif isinstance(name, genutil.StringConstructor):
elif isinstance(name, StringConstructor):
process = name.template
else:
raise RuntimeError(
Expand All @@ -49,7 +49,7 @@ def process_templated_argument(self, name, default_value="*", extras=None):
sources = [extras]

sources.insert(0, self) # will use itself as default source
process = genutil.StringConstructor(process)
process = StringConstructor(process)
for key in process.keys():
for source in sources:
setattr(process, key, getattr(source, key, default_value))
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/mean_climate/pcmdi_compute_climatologies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import datetime

from genutil import StringConstructor

from pcmdi_metrics.mean_climate.lib import calculate_climatology
from pcmdi_metrics.mean_climate.lib.pmp_parser import PMPMetricsParser
from pcmdi_metrics.utils import StringConstructor

ver = datetime.datetime.now().strftime("v%Y%m%d")

Expand Down
Loading
Loading