예제 #1
0
파일: main.py 프로젝트: NicWayand/WEM
    def copy_files(self, tofolder):
        """ 
        Move wrfout* files to folder.
        Create folder if it doesn't exist

        Move *.TS files if they exist
        Copy namelist.input to that folder.
        Copy rsl.error.0000 to the folder.
        
        Input(s):
        args = names of folder tree, in order of depth.
        """
        root = self.C.path_to_storage
        topath = os.path.join(root, tofolder)

        utils.trycreate(topath)

        files = {"wrfout_d0*": "mv", "namelist.input": "cp", "rsl.error.0000": "cp"}

        if len(glob.glob("*.TS")):
            # hi-res time series files
            files["*.TS"] = "mv"
            files["tslist"] = "cp"

        for f, transfer in files.iteritems():
            fs = os.path.join(self.C.path_to_WRF, f)
            command = "%s %s %s" % (transfer, fs, topath)
            os.system(command)
            del command

        # Finally the namelist.wps.
        path_to_namelistwps = os.path.join(self.C.path_to_WPS, "namelist.wps")
        "cp %s %s" % (path_to_namelistwps, topath)
예제 #2
0
파일: main.py 프로젝트: nishadhka/WEM
    def save_data(self, data, folder, fname, format='pickle'):
        """
        Save array to file.
        Needed by subclasses?
        """

        # Strip file extension given
        fname_base = os.path.splitext(fname)[0]
        # Check for folder, create if necessary
        utils.trycreate(folder)
        # Create absolute path
        fpath = os.path.join(folder, fname_base)

        if format == 'pickle':
            with open(fpath + '.pickle', 'wb') as f:
                pickle.dump(data, f)
        elif format == 'numpy':
            N.save(fpath, data)
        elif format == 'json':
            j = json.dumps(data)
            with open(fpath + '.json', 'w') as f:
                print >> f, j
        else:
            print("Give suitable saving format.")
            raise Exception

        print("Saved file {0} to {1}.".format(fname, folder))
예제 #3
0
파일: figure.py 프로젝트: qingu/WEM
    def save(self,outpath,fname):
        # fig.tight_layout()
        if fname[-4:] == '.png':
            pass
        else:
            fname = fname + '.png'

        utils.trycreate(outpath)
        fpath = os.path.join(outpath,fname)
        #self.fig.savefig(fpath)
        self.fig.savefig(fpath,bbox_inches='tight')
        print("Saving figure {0}".format(fpath))
        plt.close(self.fig)
예제 #4
0
파일: figure.py 프로젝트: nishadhka/WEM
    def save(self, outpath, fname):
        # fig.tight_layout()
        if fname[-4:] == '.png':
            pass
        else:
            fname = fname + '.png'

        utils.trycreate(outpath)
        fpath = os.path.join(outpath, fname)
        #self.fig.savefig(fpath)
        self.fig.savefig(fpath, bbox_inches='tight')
        print("Saving figure {0}".format(fpath))
        plt.close(self.fig)
예제 #5
0
    def plot(self,va,lv,times,**kwargs):
        for t in times:

            fig = plt.figure()
            data = self.get(va,lv,t)
            m, x, y = self.basemap_setup()

            if 'scale' in kwargs:   
                S = kwargs['scale']
                f1 = m.contour(x,y,data,S,colors='k')
            else:
                f1 = m.contour(x,y,data,colors='k')

            if self.C.plot_titles:
                title = utils.string_from_time('title',t)
                plt.title(title)

            if 'wind_overlay' in kwargs:
                jet = kwargs['wind_overlay']
                wind = self.get('wind',lv,t)
                windplot = m.contourf(x,y,wind,jet,alpha=0.6)
                plt.colorbar(windplot)

            elif 'W_overlay' in kwargs:
                Wscale = kwargs['W_overlay']
                W = self.get('W',lv,t)
                windplot = m.contourf(x,y,W,alpha=0.6)
                plt.colorbar(windplot)


            # if self.C.colorbar:
                # plt.colorbar(orientation='horizontal')

            datestr = utils.string_from_time('output',t)
            fname = '_'.join(('ECMWF',va,str(lv),datestr)) + '.png'

            print(("Plotting {0} at {1} for {2}".format(
                        va,lv,datestr)))

            plt.clabel(f1, inline=1, fmt='%4u', fontsize=12, colors='k')

            utils.trycreate(self.C.output_root)
            plt.savefig(os.path.join(self.C.output_root,fname))
            plt.clf()
            plt.close()
예제 #6
0
    def plot(self, va, lv, times, **kwargs):
        for t in times:

            fig = plt.figure()
            data = self.get(va, lv, t)
            m, x, y = self.basemap_setup()

            if 'scale' in kwargs:
                S = kwargs['scale']
                f1 = m.contour(x, y, data, S, colors='k')
            else:
                f1 = m.contour(x, y, data, colors='k')

            if self.C.plot_titles:
                title = utils.string_from_time('title', t)
                plt.title(title)

            if 'wind_overlay' in kwargs:
                jet = kwargs['wind_overlay']
                wind = self.get('wind', lv, t)
                windplot = m.contourf(x, y, wind, jet, alpha=0.6)
                plt.colorbar(windplot)

            elif 'W_overlay' in kwargs:
                Wscale = kwargs['W_overlay']
                W = self.get('W', lv, t)
                windplot = m.contourf(x, y, W, alpha=0.6)
                plt.colorbar(windplot)

            # if self.C.colorbar:
            # plt.colorbar(orientation='horizontal')

            datestr = utils.string_from_time('output', t)
            fname = '_'.join(('ECMWF', va, str(lv), datestr)) + '.png'

            print(("Plotting {0} at {1} for {2}".format(va, lv, datestr)))

            plt.clabel(f1, inline=1, fmt='%4u', fontsize=12, colors='k')

            utils.trycreate(self.C.output_root)
            plt.savefig(os.path.join(self.C.output_root, fname))
            plt.clf()
            plt.close()
예제 #7
0
    def copy_files(self, fromfolder, tofolder, matchfs=False):
        """Move wrfout files to folder.
        Create folder if it doesn't exist

        Move .TS files if they exist
        Copy namelist.input to that folder.
        Copy rsl.error.0000 to the folder.
        
        Input(s):
        args = names of folder tree, in order of depth.

        """
        root = self.path_to_storage
        # topath = os.path.join(root,tofolder)

        utils.trycreate(tofolder)

        if not matchfs:
            files = {
                'wrfout_d0*': 'mv',
                'namelist.input': 'cp',
                'rsl.error.0000': 'cp'
            }
        else:
            files = matchfs

        # if len(glob.glob('*.TS')):
        # files['*.TS'] = 'mv'
        # files['tslist'] = 'cp'

        for f, transfer in files.items():
            fs = os.path.join(fromfolder, f)
            fout = os.path.join(tofolder, f)
            command = '{0} {1} {2}'.format(transfer, fs, fout)
            os.system(command)
            del command

        if self.doWPS:
            # Finally the namelist.wps.
            path_to_namelistwps = os.path.join(self.path_to_WPS,
                                               'namelist.wps')
            fout = os.path.join(tofolder, 'namelist.wps')
            'cp {0} {1}'.format(path_to_namelistwps, fout)
예제 #8
0
    def copy_files(self,fromfolder,tofolder,matchfs=False):
        """Move wrfout files to folder.
        Create folder if it doesn't exist

        Move .TS files if they exist
        Copy namelist.input to that folder.
        Copy rsl.error.0000 to the folder.
        
        Input(s):
        args = names of folder tree, in order of depth.

        """
        root = self.path_to_storage
        # topath = os.path.join(root,tofolder)
        
        utils.trycreate(tofolder)
        
        if not matchfs:
            files = {'wrfout_d0*':'mv','namelist.input':'cp',
                    'rsl.error.0000':'cp'}
        else:
            files = matchfs

        # if len(glob.glob('*.TS')):
            # files['*.TS'] = 'mv'
            # files['tslist'] = 'cp'
        
        for f,transfer in files.items():
            fs = os.path.join(fromfolder,f)
            fout = os.path.join(tofolder,f)
            command = '{0} {1} {2}'.format(transfer,fs,fout)
            os.system(command)
            del command

        if self.doWPS:
            # Finally the namelist.wps.
            path_to_namelistwps = os.path.join(self.path_to_WPS,'namelist.wps')
            fout = os.path.join(tofolder,'namelist.wps')
            'cp {0} {1}'.format(path_to_namelistwps,fout)
예제 #9
0
    def copy_files(self, tofolder):
        """ 
        Move wrfout* files to folder.
        Create folder if it doesn't exist

        Move *.TS files if they exist
        Copy namelist.input to that folder.
        Copy rsl.error.0000 to the folder.
        
        Input(s):
        args = names of folder tree, in order of depth.
        """
        root = self.C.path_to_storage
        topath = os.path.join(root, tofolder)

        utils.trycreate(topath)

        files = {
            'wrfout_d0*': 'mv',
            'namelist.input': 'cp',
            'rsl.error.0000': 'cp'
        }

        if len(glob.glob('*.TS')):
            # hi-res time series files
            files['*.TS'] = 'mv'
            files['tslist'] = 'cp'

        for f, transfer in files.iteritems():
            fs = os.path.join(self.C.path_to_WRF, f)
            command = '%s %s %s' % (transfer, fs, topath)
            os.system(command)
            del command

        # Finally the namelist.wps.
        path_to_namelistwps = os.path.join(self.C.path_to_WPS, 'namelist.wps')
        'cp %s %s' % (path_to_namelistwps, topath)
예제 #10
0
        elif (p == 2) & (ex != "ICBC"):
            img = M.image.imread(fpath)
            ax.imshow(img)
        else:
            try:
                img = M.image.imread(fpath)
            except:
                print "Skipping ", panel
            else:
                print "Plotting ", panel
                ax.imshow(img)
        ax.axis("off")
        # import pdb; pdb.set_trace()
        plt.title(panel)

    # pdb.set_trace()
    if ex == "STCH":
        outfiledir = os.path.join(config.output_root, case_str, IC, ens, MP)
    elif ex == "ICBC":
        outfiledir = os.path.join(config.output_root, case_str, IC)
    else:
        outfiledir = os.path.join(config.output_root, case_str, IC, ens)

    outfilename = "_".join(("postage", ex, va_str, lv_str, time_str)) + ".png"
    outfilepath = os.path.join(outfiledir, outfilename)
    print outfilepath
    utils.trycreate(outfiledir)
    # plt.tight_layout()
    fig.savefig(outfilepath)
    fig.clf()
예제 #11
0
    clvs = N.arange(5,32.5,2.5)
    cb = F.bmap.contourf(F.x,F.y,windmag,alpha=0.5,cmap=M.cm.copper_r,levels=clvs,extend='max')

    # F.bmap.plot((lonA,lonB),(latA,latB),latlon=True,color='k')

    # fig.subplots_adjust(bottom=0.12,right=0.9)
    # cbar_ax = fig.add_axes([0.15,0.087,0.7,0.025])
    # cbar_ax = False
    # cbx = plt.colorbar(cb,ticks=False,cax=cbar_ax,orientation='horizontal')#,extend='both')
    cbx = plt.colorbar(cb,orientation='horizontal',shrink=0.5)
    cbx.set_label('Wind difference (m/s)')
    fig.tight_layout()

    fname = 'diff_wind.png'
    fpath = os.path.join(outdir,fname)
    utils.trycreate(outdir)
    fig.savefig(fpath)
    print(("Saved at {0}.".format(fpath)))
    plt.close(fig)

# Two together
for nest in ('SINGLE','NESTED'):
    fig,ax = plt.subplots(1,figsize=(4,4))
    F = BirdsEye(ax=ax,fig=fig)
    F.bmap,F.x,F.y = F.basemap_setup(lats=lats,lons=lons,)
    xpts, ypts = F.bmap([lonB,lonA],[latB,latA])
    F.bmap.plot(xpts,ypts,latlon=False,color='k')
    n = 3
    U = DATA[nest]['U']
    U2 = U[::n,::n]
    V = DATA[nest]['V']
예제 #12
0
U_diff = DATA["SINGLE"]["U"] - DATA["NESTED"]["U"]
V_diff = DATA["SINGLE"]["V"] - DATA["NESTED"]["V"]

F = BirdsEye(ax=ax, fig=fig)
F.bmap, F.x, F.y = F.basemap_setup(lats=lats, lons=lons)
n = 3
U_diff2 = -1 * U_diff[::n, ::n]
V_diff2 = -1 * V_diff[::n, ::n]
x = F.x[::n, ::n]
y = F.y[::n, ::n]
F.bmap.quiver(x, y, U_diff2, V_diff2, scale=500)
windmag = N.sqrt(U_diff ** 2 + V_diff ** 2)
clvs = N.arange(5, 32.5, 2.5)
cb = F.bmap.contourf(F.x, F.y, windmag, alpha=0.5, cmap=M.cm.copper_r, levels=clvs, extend="max")


# fig.subplots_adjust(bottom=0.12,right=0.9)
# cbar_ax = fig.add_axes([0.15,0.087,0.7,0.025])
# cbar_ax = False
# cbx = plt.colorbar(cb,ticks=False,cax=cbar_ax,orientation='horizontal')#,extend='both')
cbx = plt.colorbar(cb, orientation="horizontal", shrink=0.5)
cbx.set_label("Wind difference (m/s)")
fig.tight_layout()

fname = "diff_wind.png"
fpath = os.path.join(outdir, fname)
utils.trycreate(outdir)
fig.savefig(fpath)
print("Saved at {0}.".format(fpath))
plt.close(fig)
예제 #13
0
            axtitle = 'Control'
        elif plot=='RUC':
            axtitle = ''
        else:
            axtitle = plot.replace('_',' ')
        ax.set_title(axtitle)
    if len(plotlist)==13:
        axes.flat[-2].axis('off')
    
    axes.flat[-1].axis('off')
    if vrbl=='cref' or 'strongestwind':
        axes.flat[1].axis('off')

    # axes.flat[-1].colorbar(cb)
    # plt.colorbar(cb,cax=cax)
    # plt.colorbar(cb,cax=axes.flat[-1],use_gridspec=True)
    fig.tight_layout(h_pad=0.01)
    fig.subplots_adjust(wspace = 0.1, hspace = 0.1)

    if vrbl is not 'sstrongestwind':
        cbar_ax = fig.add_axes([0.81,0.12,0.16,0.023])
        cb1 = plt.colorbar(cb,cax=cbar_ax,orientation='horizontal')#,extend='both')
        cb1.set_label('Comp. Reflectivity (dBZ)')

    sp_fname = p.create_fname(vrbl,utc=tstr,level=lv,f_prefix=IC,f_suffix=enstype)
    sp_fpath = os.path.join(sp_outdir,sp_fname)
    utils.trycreate(sp_outdir)
    fig.savefig(sp_fpath)
    plt.close(fig)
    print(("Saving figure to {0}".format(sp_fpath)))
예제 #14
0
        ax.set_title(axtitle)
    if len(plotlist) == 13:
        axes.flat[-2].axis('off')

    axes.flat[-1].axis('off')
    if vrbl == 'cref' or 'strongestwind':
        axes.flat[1].axis('off')

    # axes.flat[-1].colorbar(cb)
    # plt.colorbar(cb,cax=cax)
    # plt.colorbar(cb,cax=axes.flat[-1],use_gridspec=True)
    fig.tight_layout(h_pad=0.01)
    fig.subplots_adjust(wspace=0.1, hspace=0.1)

    if vrbl is not 'sstrongestwind':
        cbar_ax = fig.add_axes([0.81, 0.12, 0.16, 0.023])
        cb1 = plt.colorbar(cb, cax=cbar_ax,
                           orientation='horizontal')  #,extend='both')
        cb1.set_label('Comp. Reflectivity (dBZ)')

    sp_fname = p.create_fname(vrbl,
                              utc=tstr,
                              level=lv,
                              f_prefix=IC,
                              f_suffix=enstype)
    sp_fpath = os.path.join(sp_outdir, sp_fname)
    utils.trycreate(sp_outdir)
    fig.savefig(sp_fpath)
    plt.close(fig)
    print(("Saving figure to {0}".format(sp_fpath)))
예제 #15
0
    def plot_skewT(self,plot_time,plot_latlon,dom,save_output,save_plot=1):
        

        # Defines the ranges of the plot, do not confuse with self.P_bot and self.P_top
        self.barb_increments = {'half': 2.5,'full':5.0,'flag':25.0}
        self.skewness = 37.5
        self.P_bot = 100000.
        self.P_top = 10000.
        self.dp = 100.
        self.plevs = N.arange(self.P_bot,self.P_top-1,-self.dp)

        # pdb.set_trace()
        prof_lat, prof_lon = plot_latlon
        datestr = utils.string_from_time('output',plot_time)
        t_idx = self.W.get_time_idx(plot_time,tuple_format=1)
        y,x, exact_lat, exact_lon = gridded_data.getXY(self.W.lats1D,self.W.lons1D,prof_lat,prof_lon)


        # Create figure
        if save_plot:
            height, width = (10,10)

            fig = plt.figure(figsize=(width,height))
            self.isotherms()
            self.isobars()
            self.dry_adiabats()
            self.moist_adiabats()


            P_slices = {'t': t_idx, 'la': y, 'lo': x}
            H_slices = {'t':t_idx, 'lv':0, 'la':y, 'lo':x}
            # pdb.set_trace()
            P = self.W.get('pressure',P_slices)[0,:,0,0]

            elev = self.W.get('HGT',H_slices)

            thin_locs = gridded_data.thinned_barbs(P)

            self.windbarbs(self.W.nc,t_idx,y,x,P,thin_locs,n=45,color='blue')
            self.temperature(self.W.nc,t_idx,y,x,P,linestyle='solid',color='blue')
            self.dewpoint(self.W.nc,t_idx,y,x,P,linestyle='dashed',color='blue')

            xticks = N.arange(-20,51,5)
            yticks = N.arange(100000.0,self.P_top-1,-10**4)
            ytix = ["%4u" %(p/100.0) for p in yticks]
            plt.xticks(xticks,['' if tick%10!=0 else str(tick) for tick in xticks])
            plt.yticks(yticks,ytix)

            plt.axis([-20,50,105000.0,20000.0])
            plt.xlabel(r'Temperature ($^{\circ}$C) at 1000 hPa')
            plt.xticks(xticks,['' if tick%10!=0 else str(tick) for tick in xticks])
            plt.ylabel('Pressure (hPa)')
            plt.yticks(yticks,ytix)
            #yticks = N.arange(self.P_bot,P_t-1,-10**4)
            #plt.yticks(yticks,yticks/100)

            fname = '_'.join(('skewT',datestr,'{0:03d}'.format(x),'{0:03d}'.format(y))) + '.png'
            utils.trycreate(self.path_to_output)

            fpath = os.path.join(self.path_to_output,fname)
            plt.savefig(fpath)
            plt.close()

        # For saving Skew T data
        if save_output:

            # Pickle files are saved here:
            pickle_fname = '_'.join(('WRFsounding',datestr,'{0:03d}'.format(x),
                            '{0:03d}'.format(y)+'.p'))
            pickle_path = os.path.join(self.C.pickledir,pickle_fname)
            u,v = self.return_data('wind',self.W.nc,t_idx,y,x,thin_locs)
            T = self.return_data('temp',self.W.nc,t_idx,y,x,thin_locs,P=P)
            Td = self.return_data('dwpt',self.W.nc,t_idx,y,x,thin_locs,P=P)
            
            data_dict = {'u':u,'v':v,'T':T,'Td':Td,'P':P}
            with open(pickle_path,'wb') as p:
                pickle.dump(data_dict,p)
            print("Saving data to {0}".format(pickle_path))

        return
예제 #16
0
        elif (p == 2) & (ex != 'ICBC'):
            img = M.image.imread(fpath)
            ax.imshow(img)
        else:
            try:
                img = M.image.imread(fpath)
            except:
                print("Skipping ", panel)
            else:
                print("Plotting ", panel)
                ax.imshow(img)
        ax.axis('off')
        # import pdb; pdb.set_trace()
        plt.title(panel)

    # pdb.set_trace()
    if ex == 'STCH':
        outfiledir = os.path.join(config.output_root, case_str, IC, ens, MP)
    elif ex == 'ICBC':
        outfiledir = os.path.join(config.output_root, case_str, IC)
    else:
        outfiledir = os.path.join(config.output_root, case_str, IC, ens)

    outfilename = '_'.join(('postage', ex, va_str, lv_str, time_str)) + '.png'
    outfilepath = os.path.join(outfiledir, outfilename)
    print(outfilepath)
    utils.trycreate(outfiledir)
    #plt.tight_layout()
    fig.savefig(outfilepath)
    fig.clf()
예제 #17
0
    # Write back
    assert N.all(newdata > 0.0)
    # pdb.set_trace()
    nc.variables['SMOIS'][:,:,:,:] = newdata
    nc.close()
    return f_out

#### PROCEDURE ####

for case,it in CASES.items:
    for enstype in enstypes:
        # Initial settings
        cstr = "{:04d}{:02d}{:02d}".format(case.year,case.month,case.day)
        istr = "{:02d}{:02d}".format(it.hour,it.minute)
        pathtoWRFout_ci = os.path.join(pathtoWRFout,cstr,istr)
        utils.trycreate(pathtoWRFout_ci)
        namelistdir = os.path.join(pathtoWRF,'nls_{}'.format(enstype))
        dirstr = "eachmember_{:04d}{:02d}{:02d}{:02d}{:02d}".format(
                    it.year,it.month,it.day,it.hour,it.minute)
        # ICs file that needs renaming. Specific to member and time.
        wrfstr = "wrfout_".format()
        pathtoICs = "/work/nyussouf/WoF/{cstr}/NSSL-2M/EXP/{dirstr}/{wrfstr}".format(
                            cstr=cstr,dirstr=dirstr,wrfstr=wrfstr)
        pathtoLBCs = 
        

        # Copy wrfout ICs and rename to wrfinput, make sure python namelist generator knows

        # Run namelist generator - make sure it modifies the right wrfinputs
        # Needs to edit time/date/grid lat/lon?
        # Do 6 hour runs