def create_climo_file(fp_in, fp_out, t_start, t_end, variable): ''' Generates climatological files from an input file and a selected time range Paramenters: f_in: input file path f_out: output file path t_start (datetime.datetime): start date of climo period t_end (datetime.datetime): end date of climo period variable (str): name of the variable which is being processed Requested date range MUST exist in the input file ''' supported_vars = { 'cddETCCDI', 'csdiETCCDI', 'cwdETCCDI', 'dtrETCCDI', 'fdETCCDI', 'gslETCCDI', 'idETCCDI', 'prcptotETCCDI', 'r10mmETCCDI', 'r1mmETCCDI', 'r20mmETCCDI', 'r95pETCCDI', 'r99pETCCDI', 'rx1dayETCCDI', 'rx5dayETCCDI', 'sdiiETCCDI', 'suETCCDI', 'thresholds', 'tn10pETCCDI', 'tn90pETCCDI', 'tnnETCCDI', 'tnxETCCDI', 'trETCCDI', 'tx10pETCCDI', 'tx90pETCCDI', 'txnETCCDI', 'txxETCCDI', 'wsdiETCCDI', 'tasmin', 'tasmax', 'pr' } if variable not in supported_vars: raise Exception("Unsupported variable: cant't yet process {}".format(variable)) # Allow different ops by variable? # op = 'sum' if variable == 'pr' else 'mean' op = 'mean' cdo = Cdo() date_range = '{},{}'.format(d2s(t_start), d2s(t_end)) if not os.path.exists(os.path.dirname(fp_out)): os.makedirs(os.path.dirname(fp_out)) with NamedTemporaryFile(suffix='.nc') as tempf: cdo.seldate(date_range, input=fp_in, output=tempf.name) # Add extra postprocessing for specific variables. vt = var_trans(variable) if 'yr' in fp_in: cdo_cmd = '{vt} -tim{op} {fname}'.format(fname=tempf.name, op=op, vt=vt) else: cdo_cmd = '{vt} -ymon{op} {fname} {vt} -yseas{op} {fname} {vt} -tim{op} {fname}'\ .format(fname=tempf.name, op=op, vt=vt) cdo.copy(input=cdo_cmd, output=fp_out)