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

Merge 376 and 378 #379

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
works and pribts traceback if needed
  • Loading branch information
doutriaux1 committed Jul 7, 2016
commit d6e9adb62c658a5dd28332bcc5ff185a4e2373e8
33 changes: 32 additions & 1 deletion src/python/pcmdi/scripts/pcmdi_metrics_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,28 @@
obs_dic = json.loads(fjson.read())
fjson.close()


class DUP(object):

def __init__(self, outfile):
self.outfile = outfile
self.tb = False

def __call__(self, *args):
msg = ""
for a in args:
msg += " " + str(a)
print msg
print>>self.outfile, msg
if self.tb:
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
print "<<<<<<<<<<<< BEG TRACEBACK >>>>>>>>>>>>>>>>>>"
traceback.print_tb(exc_traceback)
print "<<<<<<<<<<<< END TRACEBACK >>>>>>>>>>>>>>>>>>"
print>>self.outfile, "<<<<<<<<<<<< BEG TRACEBACK >>>>>>>>>>>>>>>>>>"
traceback.print_tb(exc_traceback,file=self.outfile)
print>>self.outfile, "<<<<<<<<<<<< END TRACEBACK >>>>>>>>>>>>>>>>>>"



def applyCustomKeys(O, custom_dict, var):
Expand All @@ -81,6 +91,12 @@ def applyCustomKeys(O, custom_dict, var):
help="input parameter file containing local settings",
required=True)

P.add_argument("-t",
"--traceback",
default=False,
action="store_true",
help="Print traceback on errors (helps developers to debug)")

args = P.parse_args(sys.argv[1:])

pth, fnm = os.path.split(args.param)
Expand Down Expand Up @@ -169,7 +185,9 @@ def applyCustomKeys(O, custom_dict, var):
sftlf[model_version]["md5"] = sft.hash()
except:
# Hum no sftlf...
dup.tb = args.traceback
dup("No mask for ", sft())
dup.tb = False
sftlf[model_version] = {"raw": None}
sftlf[model_version]["filename"] = None
sftlf[model_version]["md5"] = None
Expand Down Expand Up @@ -322,6 +340,7 @@ def applyCustomKeys(O, custom_dict, var):
OBS.table = table_realm
OBS.case_id = case_id
applyCustomKeys(OBS, parameters.custom_keys, var)
area = None # Default no specific area selected
if region is not None:
# Select a sub region?
area = region.get("area",None)
Expand All @@ -338,8 +357,10 @@ def applyCustomKeys(O, custom_dict, var):
obs_var_ref["RefName"])
oMasknm = oMask()
except Exception as err:
dup.tb = args.traceback
dup("error retrieving mask for obs: %s, \n%s" %
(obs_dic[var][ref], err))
dup.tb = False
oMasknm = "%s_%s" % (var, ref)
tmpoMask = saved_obs_masks.get(oMasknm, None)
if tmpoMask is not None:
Expand All @@ -350,7 +371,9 @@ def applyCustomKeys(O, custom_dict, var):
oMask = oMask.get("sftlf")
# ok that failed falling back on autogenerate
except:
dup.tb = args.traceback
dup("Could not find obs mask, generating")
dup.tb = False
foGrd = cdms2.open(OBS())
oGrd = foGrd(var, time=slice(0, 1))
foGrd.close()
Expand All @@ -372,7 +395,9 @@ def applyCustomKeys(O, custom_dict, var):
else:
do = OBS.get(var)
except Exception as err:
dup.tb = args.traceback
dup('failed with 4D OBS', var, ref, err)
dup.tb = False
continue
grd["GridResolution"] = do.shape[1:]
metrics_dictionary["GridInfo"] = grd
Expand Down Expand Up @@ -480,9 +505,11 @@ def applyCustomKeys(O, custom_dict, var):
dm = dm(area)
except Exception as err:
success = False
dup.tb = args.traceback
dup('Failed to get variable %s ' % var +
'for version: %s, error:\n%s' % (
model_version, err))
dup.tb = False
break

dup(var,
Expand Down Expand Up @@ -688,9 +715,11 @@ def applyCustomKeys(O, custom_dict, var):

break
except Exception as err:
dup.tb = args.traceback
dup("Error while processing observation %s" % ref +
" for variable %s:\n\t%s" % (
var, str(err)))
dup.tb = False
# Done with obs and models loops , let's dum before next var
# Ok at this point we need to add the metrics def in the dictionary so
# that it is stored
Expand All @@ -706,4 +735,6 @@ def applyCustomKeys(O, custom_dict, var):
# CREATE OUTPUT AS ASCII FILE
OUT.write(metrics_dictionary, mode="w", type="txt")
except Exception as err:
dup.tb = args.traceback
dup("Error while processing variable %s:\n\t%s" % (var, err))
dup.tb = False