def extract(iter, kper):

    if iter != None:
        bak_prefix = seawat.ref_dir + 'bak\\' + str(iter) + '_'

    conc_file = 'MT3D001.UCN'
    concObj = mfb.MT3D_Concentration(seawat.nlay, seawat.nrow, seawat.ncol,
                                     conc_file)
    ctimes = concObj.get_time_list()
    init_time = ctimes[np.where(ctimes[:, 2] == kper)][0]
    init_seekpoint = long(init_time[-1])
    totim, kstp, kper, c, success = concObj.get_array(init_seekpoint)
    for k, lay in enumerate(seawat.layer_botm_names):
        aname = seawat.ref_dir + 'mod\\sconc_1_' + str(k + 1) + '.ref'
        if iter != None:
            bakname = bak_prefix + 'sconc_1_' + str(k + 1) + '.ref'
            aname2 = seawat.ref_dir + 'sconc_1_' + str(k + 1) + '.ref'
            shutil.copy(aname, bakname)
            shutil.copy(aname, aname2)
        print 'writing ', aname
        f = open(aname, 'w', 0)
        f.write(toString(c[k, :, :]))
        f.close()
        print 'head', totim
        if success == False:
            break
        fig = pylab.figure()
        ax = pylab.subplot(111, aspect='equal')
        p = ax.pcolor(np.flipud(hd[:, xsec_row, :]),
                      cmap=cmap_heads,
                      vmin=hd.min(),
                      vmax=hd.max())
        ax.set_yticklabels([])
        ax.set_xticklabels([])
        cax = pylab.colorbar(p, orientation='horizontal')
        cax.ax.set_title('heads ' + str(totim))
        axes.append(ax)
if 'c' in plt:
    conc_handle = mfb.MT3D_Concentration(nlay, nrow, ncol, 'MT3D001.UCN')
    while True:
        totim_c, kstp_c, kper_c, c, success = conc_handle.next()
        print 'conc', totim_c
        if success == False:
            break
        fig = pylab.figure()
        ax = pylab.subplot(111, aspect='equal')
        p = ax.pcolor(np.flipud(c[:, xsec_row, :]),
                      cmap=cmap_conc,
                      vmin=c.min(),
                      vmax=c.max())
        ax.set_yticklabels([])
        ax.set_xticklabels([])
        cax = pylab.colorbar(p, orientation='horizontal')
        cax.ax.set_title('conc ' + str(totim_c))
Пример #3
0
def apply():

    #--calibration
    keyfile = 'misc\\bro.03_calibration_seawatlist.key'
    key_well, key_wfield = {}, {}
    wconc, wfconc, wfflux, wfmass = {}, {}, {}, {}
    f = open(keyfile, 'r')
    for line in f:
        if 'wel' in line:
            raw = line.strip().split()
            kij = (int(raw[1]) - 1, int(raw[2]) - 1, int(raw[3]) - 1)
            if raw[0] in key_wfield.keys():
                if kij not in key_wfield[raw[0]]:
                    key_wfield[raw[0]].append(kij)
            else:
                key_wfield[raw[0]] = [kij]
                wfconc[raw[0]] = []
                wfflux[raw[0]] = []
                wfmass[raw[0]] = []

            if raw[4] in key_well.keys():
                key_well[raw[4]].append(kij)
            else:
                key_well[raw[4]] = [kij]
                wconc[raw[4]] = []

    path = 'bro.03\\calibration\\'
    cbc_file = path + cal.root + '_wel.cbc'
    cbc_obj = mfb.MODFLOW_CBB(cal.nlay, cal.nrow, cal.ncol, cbc_file)
    flux_type = '           WELLS'
    conc_file = path + 'MT3D001.UCN'
    conc_obj = mfb.MT3D_Concentration(cal.nlay, cal.nrow, cal.ncol, conc_file)
    dts = []
    for itime, end in enumerate(cal.sp_end):
        print end, '\r',
        ctotim, conc, kstp, kper, csuccess = conc_obj.next()
        flux, ftotim, fsuccess = cbc_obj.read_next_fluxtype(flux_type)

        if not csuccess:
            #raise Exception('Error reading binary file: '+str(conc_file))
            break
        if not fsuccess:
            #raise Exception('Error reading binary file: '+str(cbc_file))
            break
        if kper != itime + 1:
            #raise Exception('cbc kper not the same as loop kper')
            break
        if ctotim != ftotim:
            #raise Exception('cbc totim not the same as conc totim')
            break
        dts.append(end)
        #--individual wells - max concetration at any node for active wells
        for wname, kijs in key_well.iteritems():
            max_conc, max_flux = 0.0, 0.0
            for (k, i, j) in kijs:
                cn = conc[k, i, j]
                fx = np.abs(flux[k, i, j])
                if fx > 0.0 and cn > max_conc:
                    max_conc = cn
                    max_flux = fx
            if max_flux != 0.0:
                wconc[wname].append(max_conc)
            else:
                wconc[wname].append(np.NaN)

        #-- well fields
        for wname, kijs in key_wfield.iteritems():
            tot_mass, tot_fx = 0.0, 0.0
            for (k, i, j) in kijs:
                cn = conc[k, i, j]
                fx = np.abs(flux[k, i, j])
                tot_mass += (cn * fx)
                tot_fx += fx
            if tot_fx > 0.0:
                avg_conc = tot_mass / tot_fx

            else:
                avg_conc = np.NaN
                tot_mass = np.NaN

            wfconc[wname].append(avg_conc)
            wfflux[wname].append(tot_fx)
            wfmass[wname].append(tot_mass)

    df_wconc = pandas.DataFrame(wconc, index=dts)
    df_wconc.to_csv('calibration_wconc.csv')

    df_wfconc = pandas.DataFrame(wfconc, index=dts)
    df_wfconc.to_csv('calibration_wfconc.csv')
    df_wfmass = pandas.DataFrame(wfmass, index=dts)
    df_wfmass.to_csv('calibration_wfmass.csv')
import numpy as np
import MFBinaryClass as mfb
from bro_pred import seawat
'''get initial conditions for seawat model predictive run - heads and zetas
'''

hds_file = '..\\..\\_model\\bro.02\\seawat.hds'
conc_file = '..\\..\\_model\\bro.02\\MT3D001.UCN'

hds_obj = mfb.MODFLOW_Head(seawat.nlay, seawat.nrow, seawat.ncol, hds_file)
conc_obj = mfb.MT3D_Concentration(seawat.nlay, seawat.nrow, seawat.ncol,
                                  conc_file)

htimes = hds_obj.get_time_list()
ctimes = conc_obj.get_time_list()

hseekpoint = long(htimes[-1, 3])
cseekpoint = long(ctimes[-1, 3])

hds_save = seawat.ref_dir + 'strt_'
conc_save = seawat.ref_dir + 'sconc_1_'

htotim, kstp, kper, h, hsuccess = hds_obj.get_array(hseekpoint)
ctotim, ckstp, ckper, c, csuccess = conc_obj.get_array(cseekpoint)

if not hsuccess:
    raise Exception('could not extract heads')

if not csuccess:
    raise Exception('could not extract zetas')
def plot_worker(jobq, pid, conc_file):

    concObj = mfb.MT3D_Concentration(seawat.nlay, seawat.nrow, seawat.ncol,
                                     conc_file)
    ctimes = concObj.get_time_list()

    while True:
        args = jobq.get()
        if args == None:
            break
        '''args[0] = plot name           
           args[1] = conc seekpoint         
           args[2] = conc layer to plot 
           args[3] = active reaches
           args[4] = active wells                    
           args[5] = fig_title                 

        '''
        fig_name = args[0]
        conc_seekpoint = args[1]
        lay_idxs = args[2]
        lines = args[3]
        wells = args[4]
        fig_title = args[5]

        totim, kstp, kper, c, success = concObj.get_array(conc_seekpoint)
        c = np.ma.masked_where(c <= 0.014, c)

        fig = pylab.figure(figsize=(8, 8))

        ax1 = pylab.axes((0.05, 0.525, 0.45, 0.45))
        ax2 = pylab.axes((0.05, 0.055, 0.45, 0.45))
        ax3 = pylab.axes((0.525, 0.525, 0.45, 0.45))
        ax4 = pylab.axes((0.525, 0.055, 0.45, 0.45))

        cax1 = pylab.axes((0.05, 0.53, 0.45, 0.015))
        cax2 = pylab.axes((0.05, 0.05, 0.45, 0.015))
        cax3 = pylab.axes((0.525, 0.53, 0.45, 0.015))
        cax4 = pylab.axes((0.525, 0.05, 0.45, 0.015))

        print np.max(c[lay_idxs[1], :, :])
        print np.min(c[lay_idxs[1], :, :])

        p1 = ax1.imshow(c[lay_idxs[0], :, :],
                        extent=imshow_extent,
                        cmap=cmap,
                        interpolation='none',
                        vmax=1.0,
                        vmin=0.0)
        p2 = ax2.imshow(c[lay_idxs[1], :, :],
                        extent=imshow_extent,
                        cmap=cmap,
                        interpolation='none',
                        vmax=1.0,
                        vmin=0.0)
        p3 = ax3.imshow(c[lay_idxs[2], :, :],
                        extent=imshow_extent,
                        cmap=cmap,
                        interpolation='none',
                        vmax=1.0,
                        vmin=0.0)
        p4 = ax4.imshow(c[lay_idxs[3], :, :],
                        extent=imshow_extent,
                        cmap=cmap,
                        interpolation='none',
                        vmax=1.0,
                        vmin=0.0)

        cb1 = pylab.colorbar(p1, cax=cax1, orientation='horizontal')
        cb2 = pylab.colorbar(p2, cax=cax2, orientation='horizontal')
        cb3 = pylab.colorbar(p3, cax=cax3, orientation='horizontal')
        cb4 = pylab.colorbar(p4, cax=cax4, orientation='horizontal')

        cb1.set_label('Concentration ' + seawat.layer_botm_names[lay_idxs[0]])
        cb2.set_label('Concentration ' + seawat.layer_botm_names[lay_idxs[1]])
        cb3.set_label('Concentration ' + seawat.layer_botm_names[lay_idxs[2]])
        cb4.set_label('Concentration ' + seawat.layer_botm_names[lay_idxs[3]])

        ax1.set_ylim(flow.plt_y)
        ax1.set_xlim(flow.plt_x)
        ax2.set_ylim(flow.plt_y)
        ax2.set_xlim(flow.plt_x)
        ax3.set_ylim(flow.plt_y)
        ax3.set_xlim(flow.plt_x)
        ax4.set_ylim(flow.plt_y)
        ax4.set_xlim(flow.plt_x)

        ax1.set_xticklabels([])
        ax3.set_xticklabels([])
        ax3.set_yticklabels([])
        ax4.set_yticklabels([])

        #-- plot active reaches
        for line in lines:
            ax1.plot(line[0, :], line[1, :], 'k-', lw=0.25)
            ax2.plot(line[0, :], line[1, :], 'k-', lw=0.25)
            ax3.plot(line[0, :], line[1, :], 'k-', lw=0.25)
            ax4.plot(line[0, :], line[1, :], 'k-', lw=0.25)

        #-- plot active wells
        #for wpoint in wells:
        #    color=salt_well_color
        #    if hds_name:
        #        ax1.plot(wpoint[0],wpoint[1],'.',mfc=color,mec='none',ms=4)
        #        ax2.plot(wpoint[0],wpoint[1],'.',mfc=color,mec='none',ms=4)
        #    if zta_name:
        #        ax3.plot(wpoint[0],wpoint[1],'.',mfc=color,mec='none',ms=4)
        #        ax4.plot(wpoint[0],wpoint[1],'.',mfc=color,mec='none',ms=4)
        for wpoint in wells:
            color = 'k'
            ax1.plot(wpoint[0], wpoint[1], '.', mfc=color, mec='none', ms=4)
            ax2.plot(wpoint[0], wpoint[1], '.', mfc=color, mec='none', ms=4)
            ax3.plot(wpoint[0], wpoint[1], '.', mfc=color, mec='none', ms=4)
            ax4.plot(wpoint[0], wpoint[1], '.', mfc=color, mec='none', ms=4)

        fig.text(0.5, 0.965, fig_title, ha='center')
        pylab.savefig(fig_name, dpi=300, format='png', bbox_inches='tight')
        pylab.close('all')
        print pid, '-- done--', fig_title
        jobq.task_done()
    jobq.task_done()
    return
def main(num_plots):

    #--load well locations and pandas dataframe
    well_shapename = '..\\..\\_gis\\shapes\\pws_combine'
    well_points = sf.load_shape_list(well_shapename)
    #shp = sf.reader(well_shapename)
    #print sf.get_fieldnames(well_shapename)
    records = sf.load_as_dict(well_shapename, loadShapes=False)
    well_names = records['DPEP_NAME']
    well_zbots = records['zbot']
    float_zbots = []
    for i, wb in enumerate(well_zbots):
        float_zbots.append(float(wb))
    well_zbots = np.array(float_zbots)
    well_rows, well_cols = records['row'], records['column']
    pump = pandas.read_csv(
        '..\\..\\_pumpage\\dataframes\\pws_filled_zeros.csv',
        index_col=0,
        parse_dates=True)

    #--load lines and active dates
    line_shapename = '..\\..\\_gis\shapes\sw_reaches'
    lines = sf.load_shape_list(line_shapename)
    shp = sf.Reader(line_shapename)
    fnames = sf.get_fieldnames(line_shapename, ignorecase=True)
    #for i,fn in enumerate(fnames):
    #    print i,fn
    a_idx = fnames.index('ACTIVE_ST')
    line_active = []
    for i in range(shp.numRecords):
        rec = shp.record(i)
        year = int(rec[a_idx])
        if year < flow.start.year:
            year = flow.start.year
        dt = datetime(year=year, month=1, day=1)
        line_active.append(dt)

    #--head stuff
    #--use bot of Q5 to check for dry cells
    #hds_elev = np.loadtxt(flow.ref_dir+'Q5_bot.ref')
    #hds_layer_idx = 0
    #head_file = flow.root+'.hds'
    #headObj = mfb.MODFLOW_Head(flow.nlay,flow.nrow,flow.ncol,head_file)
    #htimes = headObj.get_time_list()

    #--conc stuff
    conc_lay_idxs = [0, 5, 9, 11]
    conc_file = 'MT3D001.UCN'
    concObj = mfb.MT3D_Concentration(seawat.nlay, seawat.nrow, seawat.ncol,
                                     conc_file)
    ctimes = concObj.get_time_list()

    #--zeta stuff
    #zta_layer_idx = 0
    #zta_elev = np.loadtxt(flow.ref_dir+'Q1_bot.ref')
    #zeta_file = flow.root+'.zta'
    #zetaObj = mfb.MODFLOW_CBB(flow.nlay,flow.nrow,flow.ncol,zeta_file)
    #zta_text = '    ZETAPLANE  1'
    #z1times = zetaObj.get_time_list(zta_text)
    #zeta_file = None

    #-- stress period step
    sp_step = 1
    plt_dir = 'png\\results\\seawat\\'

    #--for ffmpeg - sequentially numbered
    plt_num = 1
    istart = 0
    q_args = []
    for i, [start, end] in enumerate(zip(seawat.sp_start, seawat.sp_end)):
        if i >= istart and i % sp_step == 0:
            print 'building args list for stress period ending on ', end
            #--find the conc output nearest the end of the stress period

            try:
                kper_seekpoints = ctimes[np.where(ctimes[:, 2] == i + 1), -1]
                c_seekpoint = long(kper_seekpoints[0][-1])

            except:
                break

            act_lines = []
            for ldt, line in zip(line_active, lines):
                if ldt <= start:
                    act_lines.append(line)

            act_wells = []
            if i == 0:
                plt_start = start
            else:
                plt_start = seawat.sp_start[i - sp_step]
            plt_end = seawat.sp_end[i]
            pump_plt = pump[plt_start:plt_end]
            pump_plt_sum = pump_plt.sum()
            for wname, wpoint, wrow, wcol, wzbot in zip(
                    well_names, well_points, well_rows, well_cols, well_zbots):
                if wname in pump_plt.keys() and pump_plt_sum[wname] != 0:
                    act_wells.append(wpoint)

            fig_name = plt_dir + 'sp{0:03.0f}_conc.png'.format(plt_num)
            fig_title = 'stress period ' + str(
                i + 1) + ' start date ' + start.strftime('%d/%m/%Y')
            args = [
                fig_name, c_seekpoint, conc_lay_idxs, act_lines, act_wells,
                fig_title
            ]
            q_args.append(args)
            plt_num += 1
            if num_plots != None and i > num_plots:
                break

    jobq = mp.JoinableQueue()

    #--for testing
    if num_plots != None:
        jobq.put_nowait(q_args[0])
        jobq.put_nowait(None)
        plot_worker(jobq, 1, conc_file)
        return

    procs = []
    num_procs = 3

    for i in range(num_procs):
        #--pass the woker function jobq and a PID
        p = mp.Process(target=plot_worker, args=(jobq, i, conc_file))
        p.daemon = True
        print 'starting process', p.name
        p.start()
        procs.append(p)

    for q in q_args:
        jobq.put(q)

    for p in procs:
        jobq.put(None)

    #--block until all finish
    for p in procs:
        p.join()
        print p.name, 'Finished'

    anim_name = 'png\\demo_conc.avi'
    if os.path.exists(anim_name):
        os.remove(anim_name)
    cmd_line = 'ffmpeg.exe -i png\\results\\seawat\\sp%03d_conc.png -r 24 ' + anim_name + ' -y'
    os.system(cmd_line)
    return
Пример #7
0
def main(num_plots):

    #--load well locations and pandas dataframe
    well_shapename = '..\\..\\_gis\\shapes\\pws_combine'
    well_points = sf.load_shape_list(well_shapename)
    #shp = sf.reader(well_shapename)
    #print sf.get_fieldnames(well_shapename)
    records = sf.load_as_dict(well_shapename, loadShapes=False)
    well_names = records['DPEP_NAME']
    well_aban = records['ABAN_YEAR']
    act_wells = []
    for pt, ab in zip(well_points, well_aban):
        if ab == '':
            act_wells.append(pt)

    #--load lines and active dates
    line_shapename = '..\\..\\_gis\shapes\sw_reaches'
    lines = sf.load_shape_list(line_shapename)

    #--head stuff
    #--use bot of Q5 to check for dry cells
    #hds_elev = np.loadtxt(flow.ref_dir+'Q5_bot.ref')
    #hds_layer_idx = 0
    #head_file = flow.root+'.hds'
    #headObj = mfb.MODFLOW_Head(flow.nlay,flow.nrow,flow.ncol,head_file)
    #htimes = headObj.get_time_list()

    #--conc stuff
    conc_lay_idxs = [0, 2, 3, 4, 5]
    conc_file = 'MT3D001.UCN'
    concObj = mfb.MT3D_Concentration(seawat.nlay, seawat.nrow, seawat.ncol,
                                     conc_file)
    ctimes = concObj.get_time_list()

    #-- stress period step
    sp_step = 1
    plt_dir = 'png\\results\\seawat\\'

    #--for ffmpeg - sequentially numbered
    plt_num = 1
    istart = 0
    q_args = []
    for i, [start, end] in enumerate(zip(seawat.sp_start, seawat.sp_end)):
        if i >= istart and i % sp_step == 0:
            print 'building args list for stress period ending on ', end
            #--find the conc output nearest the end of the stress period

            try:
                kper_seekpoints = ctimes[np.where(ctimes[:, 2] == i + 1), -1]
                c_seekpoint = long(kper_seekpoints[0][-1])

            except:
                break

            fig_name = plt_dir + 'sp{0:03.0f}_conc.png'.format(plt_num)
            fig_title = 'stress period ' + str(
                i + 1) + ' start date ' + start.strftime('%d/%m/%Y')
            args = [
                fig_name, c_seekpoint, conc_lay_idxs, lines, act_wells,
                fig_title
            ]
            q_args.append(args)
            plt_num += 1
            if num_plots != None and i > num_plots:
                break

    jobq = mp.JoinableQueue()

    #--for testing
    #if num_plots != None:
    #    jobq.put_nowait(q_args[0])
    #    jobq.put_nowait(None)
    #    plot_worker(jobq,1,conc_file)
    #    return

    procs = []
    num_procs = 4

    for i in range(num_procs):
        #--pass the woker function jobq and a PID
        p = mp.Process(target=plot_worker, args=(jobq, i, conc_file))
        p.daemon = True
        print 'starting process', p.name
        p.start()
        procs.append(p)

    for q in q_args:
        jobq.put(q)

    for p in procs:
        jobq.put(None)

    #--block until all finish
    for p in procs:
        p.join()
        print p.name, 'Finished'

    anim_name = 'png\\demo_conc.avi'
    if os.path.exists(anim_name):
        os.remove(anim_name)
    cmd_line = 'ffmpeg.exe -i png\\results\\seawat\\sp%03d_conc.png -r 24 ' + anim_name + ' -y'
    os.system(cmd_line)
    return
Пример #8
0
import numpy as np
from numpy import ma
import pylab
import MFBinaryClass as mfb
import shapefile
import arrayUtil as au

import bro_info as bi

#nrow,ncol,nlay = 411,501,6
#delr,delc = 500,500
#offset = [728600.0,577350.0,0.0]
#results = 'results\\'
#nreach = 10810

day_2_sec = 1.0 / 86400.0

hds_handle = mfb.MODFLOW_Head(bi.nlay, bi.nrow, bi.ncol,
                              results + 'bro_6lay.hds')
totim, kstp, kper, h, success = hds_handle.get_record()

conc_handle = mfb.MT3D_Concentration(bi.nlay, bi.nrow, bi.ncol, 'MT3D001.UCN')
totim_c, kstp_c, kper_c, c, success = conc_handle.get_record()
ibound = np.loadtxt('ref\\ibound.ref')

for l in range(bi.nlay):
    print 'max conc in layer ' + str(l + 1) + ' :' + str(
        c[l, :, :].max()), c[l, :, :].shape
    np.savetxt('init_conc_' + str(l + 1) + '.ref', c[l, :, :], fmt='%15.6e')
    np.savetxt('init_heads_' + str(l + 1) + '.ref', h[l, :, :], fmt='%15.6e')