def basinPrcpEns(Dir, File, step, FT, startDate, endDate, ens): date = startDate while date <= endDate: year = date.year mon = date.month day = date.day hour = date.hour dPath = os.path.join(Dir % (hour, year, mon), File % (ens, year, mon, day, hour)) if matOrder == "SN": data = gtopen(dPath).vars["PRCP"][:][:, 0, ::-1, :] elif matOrder == "NS": data = gtopen(dPath).vars["PRCP"][:][:, 0, ::-1, :] else: raise KeyError if date == startDate: prcp = data else: prcp = np.concatenate((prcp, data)) if prcp.shape[0] > FT: prcp = prcp[0:FT] break date = date + dt.timedelta(hours=step) mPrcp = prcp * prcpC return mPrcp
def encodeGtool(self,binData,var): ### print "this is on developing code. Check how to append Z axis and endian" ### varOut = self.varOutDict[var] varName = self.varNameDict[var] gtUnit = self.varUnitDict[varName] os.environ['F_UFMTENDIAN'] = 'big' #not need? outPath = os.path.join(self.outDir,'%s_%04d%02d%02d%02d_e%02d.gt'%(varOut,self.year,self.mon,self.day,self.hour,self.eNum)) DTime = [datetime.datetime(self.year,self.mon,self.day,self.hour)+datetime.timedelta(seconds=self.dT)*i for i in range(int(binData.shape[0]))] TStamp = [dtime.strftime('%Y%m%d %H%M%S ') for dtime in DTime] gtOut = gtopen(outPath,mode='w+') print "="*80 print "encoding data..." [gtOut.append( d ) for d in binData] print "finished encoding." print "="*80 gtOut = gtopen(outPath,mode='r+') gtVar = gtOut.vars[''] gtVar.header['ITEM'] = varName gtVar.header['TITL1'] = varName gtVar.header['DSET'] = varName gtVar.header['DATE'] = TStamp gtVar.header['UNIT'] = gtUnit gtVar.header['AEND1'] = binData.shape[3] gtVar.header['AEND2'] = binData.shape[2] gtVar.header['AEND3'] = binData.shape[1] gtVar.header['AITM1'] = "GLON%iM"%(binData.shape[3]) gtVar.header['AITM2'] = "GLAT%iIM"%(binData.shape[2]) gtVar.header['AITM3'] = self.GATM gtVar.header['MSIGN'] = getpass.getuser() print "outPath: ",outPath print gtVar.header
def readGtool(self,gtPath,VAR,z,Coef): gt = gtopen(gtPath).vars[VAR][:][:,z,:,:] product = gt*Coef product[np.where(product>1e+5)] = 0 #tmp print "output shape: ",product.shape return product
def drawImage(self, i, dtype="gtool", varname="VARIABLE_NAME"): sns.set_style("white") west = self.wests[i] east = self.easts[i] south = self.souths[i] north = self.norths[i] res = self.res[i] norml = self.norm[i] Norml = colors.BoundaryNorm(norml, 256) dPath = self.dPath[i] nsConv = self.nsConv[i] imgDir = self.imgDirs[i] imgName = self.imgNames[i] coef = self.coefs[i] nx = self.nx[i] ny = self.ny[i] varname = self.varnames[i] print dPath if dtype == "binary": print "Read as Binary" values = np.fromfile(dPath, dtype=np.float32).reshape(-1, ny[i], nx[i]) elif dtype == "gtool": print "Read as Gtool" values = gtopen(dPath).vars[varname][:][:, 0, :, :] else: sys.stderr.write("Unrecongizable argument for dtype: %s" % dtype) raise IOError if nsConv: values = values[:, ::-1, :] val = values.sum(axis=0) * coef mycm = self.createCmap() h = float(ny) / float(nx) v = 1 plt.figure(figsize=(v * 10, h * 10)) #m = Basemap(urcrnrlat=north,urcrnrlon=east,llcrnrlat=south,llcrnrlon=west,projection="merc") #m.imshow(np.ma.masked_less(val,norml[0]),alpha=0.5,cmap=mycm,norm=Norml) plt.imshow(np.ma.masked_less(val, norml[0]), alpha=0.5, cmap=mycm, norm=Norml) plt.tick_params(labelbottom=False, labelleft=False) ax = plt.gca() ax.spines["right"].set_color("none") ax.spines["left"].set_color("none") ax.spines["top"].set_color("none") ax.spines["bottom"].set_color("none") oName = os.path.join(os.path.join(imgDir, "prcp"), imgName) plt.savefig(oName, dpi=300, transparent=True, bbox_inches="tight", pad_inches=0.) return oName
def basinPrcpEns(Dir, File, step, FT, startDate, endDate, ens, mask): # print "read precipitation." date = startDate while date <= endDate: year = date.year mon = date.month day = date.day hour = date.hour dPath = os.path.join(Dir % (hour, year, mon), File % (ens, year, mon, day, hour)) # print dPath if matOrder == "SN": data = gtopen(dPath).vars["PRCP"][:][:, 0, ::-1, :] elif matOrder == "NS": data = gtopen(dPath).vars["PRCP"][:][:, 0, ::-1, :] else: raise KeyError # print data.shape if date == startDate: prcp = data else: prcp = np.concatenate((prcp, data)) if prcp.shape[0] > FT: prcp = prcp[0:FT] # print "formatted.", prcp.shape break date = date + dt.timedelta(hours=step) mPrcp = prcp * mask * prcpC mPrcpAvg = np.nanmean(mPrcp, axis=(1, 2)) tindx = [] for t in range(0, FT): date = startDate + dt.timedelta(hours=t * tRes) tindx.append(date) mPrcpAvgDf = pd.DataFrame(mPrcpAvg, index=tindx, columns=[ens]) return mPrcpAvgDf
def main(config, year, mon, day, hour): #Configuration initFile = ConfigParser.SafeConfigParser() initFile.read(config) ### gtRoot = initFile.get("Forcing", "gtRoot") #gtool data root cRes = initFile.get("Forcing", "cRes") year = int(year) mon = int(mon) day = int(day) hour = int(hour) gtPath = os.path.join(gtRoot, cRes, "%04d/%02d/%02d/%02d" % (year, mon, day, hour), "PRCP_%04d%02d%02d%02d.gt" % (year, mon, day, hour)) gt = gtopen(gtPath).vars["PRCP"][:] tShape = gt.shape[0] subprocess.call("echo tShape=%s >> ./Modelinfo.txt" % tShape, shell=True) return tShape
if var in ["PSurf"]: return 100.0 else: return 1.0 for Year in range(iYear,eYear+1): for var in lvar: ibaseDir = "/data1/hjkim/ELSE/JRA55/in/L1_01u" iDir = ibaseDir + "/%s"%(var) iPath = iDir + "/%s_%04d.gt"%(var,Year) obaseDir = "/tank/utsumi/data/H08/ELSE.JRA55.L1_01u" oDir = obaseDir + "/%s"%(var) UtilTLSM.mk_dir(oDir) coef = ret_coef(var) gt = gtopen(iPath) a2dat = zeros([ny,nx],float32) for i,chunk in enumerate(gt): DATE = chunk.header["DATE"] DTime= datetime(int(DATE[:4]), int(DATE[4:6]), int(DATE[6:8]),int(DATE[9:11])) a2dat = a2dat + chunk.data[0] if DTime.hour ==21: a2dat = flipud(c_[a2dat[:,180:], a2dat[:,:180]])/8.0 * coef oPath = oDir + "/%s_%04d%02d%02d.%s"%(var,DTime.year,DTime.month,DTime.day,Suf) #---------------- if endian=="little": a2dat.tofile(oPath) elif endian=="big": a2dat.byteswap().tofile(oPath) else: print "check endian:",endian