def concatenate_piControl(self, season=None, compressed=False): experiment = "piControl" fnames = sorted( get_ensemble_filenames(self.variable, self.region, experiment)) #models=sorted(self.ensemble_dict.keys()) models = get_ok_models(self.region) nmod = len(models) ntimes = [] model_names = [] #Loop over without loading data to figure out the shortest length control run for model in models: # print(model) I = np.where([x.split(".")[2] == model for x in fnames])[0] if len(I) > 0: first_member = int(I[0]) fname = fnames[first_member] model_names += [fname] f = cdms.open(fname) ntimes += [int(f[self.variable].shape[0])] f.close() L = np.min(ntimes) #Set the time axis to be the time axis of the shortest control rin imin = np.argmin(ntimes) fshortest = model_names[imin] f = cdms.open(fshortest) tax = f(self.variable).getTime() tax.id = 'time' tax.designateTime() f.close() #Load data #SingleMember=np.ma.zeros((len(model_names),L))+1.e20 SingleMember = np.ma.zeros((nmod, L)) + 1.e20 i = 0 for model in models: I = np.where([x.split(".")[2] == model for x in fnames])[0] if len(I) > 0: first_member = I[0] fname = fnames[first_member] f = cdms.open(fname) vdata = f(self.variable) SingleMember[i] = vdata[:L] i += 1 else: if self.verbose: print("No piControl data for " + model + " " + self.variable) f.close() #Historical units are already converted; need to convert piControl from #kg m-2 s-1 to mm day-1 #if self.variable in ["pr","evspsbl","prsn","mrros","mrro"]: # SingleMember = SingleMember*86400. SingleMember = MV.masked_where( np.abs(SingleMember) > 1.e10, SingleMember) SingleMember = MV.array(SingleMember) SingleMember.setAxis(1, tax) SingleMember.setAxis(0, cmip5.make_model_axis(models)) ###KLUDGE: FIRST YEAR IS ZERO- FIX THIS IN DOWNLOADER SingleMember = MV.masked_where(SingleMember == 0, SingleMember) # if self.variable in ["mrsos","mrso"]: # if not raw: # SingleMember=self.standardize_zscore(SingleMember) # else: # if not raw: # SingleMember=self.convert_to_percentage(SingleMember) if season is None: return SingleMember cdutil.setTimeBoundsMonthly(SingleMember) seasonal = getattr(cdutil, season).departures(SingleMember) return DA_tools.concatenate_this(seasonal, compressed=compressed)
def __init__(self, experiment): self.experiment = experiment f = cdms.open("DATA/cmip5.sahel_precip." + experiment + ".nc") west = f("pr_W") east = f("pr_CE") self.data = {} self.data["west"] = west self.data["east"] = east west_a = cdutil.ANNUALCYCLE.departures(west) east_a = cdutil.ANNUALCYCLE.departures(east) if experiment != "piControl": nmod, nmon = west_a.shape west_rs = west_a.reshape((nmod, nmon / 12, 12)) west_rs.setAxis(0, west.getAxis(0)) tax = cdms.createAxis(west.getTime()[6::12]) tax.id = 'time' tax.designateTime() tax.units = west.getTime().units west_rs.setAxis(1, tax) westsolver = Eof(MV.average(west_rs, axis=0)) else: west_a = da.concatenate_this(west_a, compressed=True) nmon, = west_a.shape tax = cdms.createAxis(np.arange(west_a.shape[0])) tax.units = 'months since 0001-1-15' tax.id = 'time' tax.designateTime() taxC = tax.asComponentTime() test = [x.torel('days since 0001-1-15').value for x in taxC] tax_days = cdms.createAxis(test) tax_days.designateTime() tax_days.id = 'time' tax_days.units = 'days since 0001-1-15' west_a.setAxis(0, tax_days) taxmonthly = cdms.createAxis(west_a.getTime()[6::12]) taxmonthly.units = west_a.getTime().units taxmonthly.designateTime() taxmonthly.id = 'time' west_rs = west_a.reshape((nmon / 12, 12)) west_rs.setAxis(0, taxmonthly) westsolver = Eof(west_rs) if experiment != "piControl": nmod, nmon = east_a.shape east_rs = east_a.reshape((nmod, nmon / 12, 12)) east_rs.setAxis(0, east.getAxis(0)) east_rs.setAxis(1, tax) eastsolver = Eof(MV.average(east_rs, axis=0)) else: east_a = da.concatenate_this(east_a, compressed=True) east_a.setAxis(0, tax_days) nmon, = east_a.shape east_rs = east_a.reshape((nmon / 12, 12)) east_rs.setAxis(0, taxmonthly) eastsolver = Eof(east_rs) facwest = da.get_orientation(westsolver) faceast = da.get_orientation(eastsolver) self.solvers = {} self.solvers["east"] = eastsolver self.solvers["west"] = westsolver self.reshaped = {} self.reshaped["east"] = east_rs self.reshaped["west"] = west_rs if len(self.reshaped["west"].shape) > 2: data = [ MV.average(cmip5.ensemble2multimodel(self.reshaped["west"]), axis=0), MV.average(cmip5.ensemble2multimodel(self.reshaped["east"]), axis=0) ] else: data = [self.reshaped["west"], self.reshaped["east"]] msolver = MultivariateEof(data) self.solvers["multi"] = msolver self.anomalies = {} self.anomalies["east"] = east_a self.anomalies["west"] = west_a