Skip to content

Commit 817e29b

Browse files
Ciheim BrownCiheim Brown
authored andcommitted
Change yaml import + remove parenthesis
1 parent 45a3a86 commit 817e29b

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

catalogbuilder/intakebuilder/getinfo.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from csv import writer
66
import os
77
import xarray as xr
8-
from . import configparser
8+
from . import configparser
9+
import yaml
910
import warnings
1011
warnings.simplefilter(action='ignore', category=FutureWarning)
1112

@@ -19,16 +20,15 @@ def getProject(projectdir,dictInfo):
1920
:param drsstructure:
2021
:return: dictionary with project key
2122
'''
22-
if ("archive" in projectdir or "pp" in projectdir):
23+
if "archive" in projectdir or "pp" in projectdir:
2324
project = "dev"
2425
dictInfo["activity_id"]=project
2526
return dictInfo
2627

2728
def getinfoFromYAML(dictInfo,yamlfile,miptable=None):
28-
import yaml
2929
with open(yamlfile) as f:
3030
mappings = yaml.load(f, Loader=yaml.FullLoader)
31-
if(miptable):
31+
if miptable:
3232
try:
3333
dictInfo["frequency"] = mappings[miptable]["frequency"]
3434
except KeyError:
@@ -37,20 +37,19 @@ def getinfoFromYAML(dictInfo,yamlfile,miptable=None):
3737
dictInfo["realm"] = mappings[miptable]["realm"]
3838
except KeyError:
3939
dictInfo["realm"] = "NA"
40-
return(dictInfo)
40+
return dictInfo
4141

4242
def getFreqFromYAML(yamlfile,gfdlfreq=None):
4343
#returns cmip freq for gfdl pp freq
44-
import yaml
4544
cmipfreq = None
4645
with open(yamlfile) as f:
4746
mappings = yaml.load(f, Loader=yaml.FullLoader)
48-
if(gfdlfreq):
47+
if gfdlfreq:
4948
try:
5049
cmipfreq = mappings[gfdlfreq]["frequency"]
5150
except KeyError:
5251
cmipfreq = None
53-
return(cmipfreq)
52+
return cmipfreq
5453

5554
def getStem(dirpath,projectdir):
5655
'''
@@ -66,7 +65,7 @@ def getStem(dirpath,projectdir):
6665

6766
def getInfoFromFilename(filename,dictInfo,logger):
6867
# 5 AR: WE need to rework this, not being used in gfdl set up get the following from the netCDF filename e.g.rlut_Amon_GFDL-ESM4_histSST_r1i1p1f1_gr1_195001-201412.nc
69-
if(filename.endswith(".nc")):
68+
if filename.endswith(".nc"):
7069
ncfilename = filename.split(".")[0].split("_")
7170
varname = ncfilename[0]
7271
dictInfo["variable"] = varname
@@ -91,7 +90,7 @@ def getInfoFromFilename(filename,dictInfo,logger):
9190
#adding this back to trace back some old errors
9291
def getInfoFromGFDLFilename(filename,dictInfo,logger,configyaml):
9392
# 5 AR: get the following from the netCDF filename e.g. atmos.200501-200912.t_ref.nc
94-
if ( (filename.endswith(".nc"))):
93+
if filename.endswith(".nc"):
9594
stemdir = filename.split(".")
9695
#lets go backwards and match given input directory to the template, add things to dictInfo
9796
j = -2
@@ -101,7 +100,7 @@ def getInfoFromGFDLFilename(filename,dictInfo,logger,configyaml):
101100
else:
102101
logger.debug("No input_path_template found. Check configuration. Please open an issue with details if problem persists.Exiting")
103102
sys.exit("No input_path_template found. Check configuration. Please open an issue with details if problem persists.Exiting")
104-
if( ".static" in filename ):
103+
if ".static" in filename :
105104
## For static we handle this differently . The GFDL PP expected pattern is atmos.static.nc
106105
#TODO error checking as needed
107106
input_file_template = ['realm','NA']
@@ -110,7 +109,7 @@ def getInfoFromGFDLFilename(filename,dictInfo,logger,configyaml):
110109
nlen = len(input_file_template)
111110
for i in range(nlen-1,-1,-1): #nlen = 3
112111
try:
113-
if(input_file_template[i] != "NA"):
112+
if input_file_template[i] != "NA":
114113
try:
115114
dictInfo[input_file_template[i]] = stemdir[(j)]
116115
except IndexError:
@@ -120,8 +119,8 @@ def getInfoFromGFDLFilename(filename,dictInfo,logger,configyaml):
120119
j = j - 1
121120
cnt = cnt + 1
122121

123-
if (".static" in filename):
124-
if ("ocean" in dictInfo["realm"]):
122+
if ".static" in filename:
123+
if "ocean" in dictInfo["realm"]:
125124
dictInfo["table_id"] = "Ofx"
126125
else:
127126
dictInfo["table_id"] = "fx"
@@ -153,12 +152,12 @@ def getInfoFromGFDLDRS(dirpath,projectdir,dictInfo,configyaml,variable_id,logger
153152
logger.debug("No input_path_template found in config yaml. Check configuration, open a github issue with details if problem persists. ")
154153
sys.exit("No input_path_template found in config yaml. Check configuration, open a github issue with details if problem persists. ")
155154
#If variable_id is fixed, it's a GFDL PP static dataset and the input path template in config is aligned only up to a particular directory structure as this does not have the ts and frequency or time chunks
156-
if(variable_id == "fixed"):
155+
if variable_id == "fixed" :
157156
input_path_template = input_path_template[:-3 or None]
158157
nlen = len(input_path_template)
159158
for i in range(nlen-1,0,-1):
160159
try:
161-
if(input_path_template[i] != "NA"):
160+
if input_path_template[i] != "NA":
162161
try:
163162
dictInfo[input_path_template[i]] = stemdir[(j)]
164163
except IndexError:
@@ -173,10 +172,10 @@ def getInfoFromGFDLDRS(dirpath,projectdir,dictInfo,configyaml,variable_id,logger
173172
#TODO Make this elegant and intuitive
174173
#TODO logger messages, not print
175174
if "cell_methods" in dictInfo.keys():
176-
if (dictInfo["cell_methods"] == "av"):
175+
if dictInfo["cell_methods"] == "av":
177176
print("Skipping time-average data")
178177
return {}
179-
elif (dictInfo["cell_methods"] == "ts"):
178+
elif dictInfo["cell_methods"] == "ts":
180179
logger.debug("time-series data")
181180
else:
182181
logger.debug("This is likely static")
@@ -276,11 +275,11 @@ def getStandardName(list_variable_id,list_realm):
276275
for realm in list_realm:
277276
cfname = df[(df['GFDL_varname'] == variable_id) & (realm in df['modeling_realm'])]["standard_name"]
278277
list_cfname = cfname.tolist()
279-
if(len(list_cfname) == 0):
278+
if len(list_cfname) == 0:
280279
cfname = (df[df['CMOR_varname'] == variable_id]["standard_name"])
281280
list_cfname = cfname.tolist()
282281
if len(list_cfname) > 0:
283282
unique_cf = list(set(list_cfname))[0]
284283
varrealm = "{0},{1}".format(variable_id,realm)
285284
dictCF[varrealm] = unique_cf
286-
return (dictCF)
285+
return dictCF

0 commit comments

Comments
 (0)