Ejemplo n.º 1
0
def getIntFcn(weatherObj, itype='wet', interpType='scipy'):
    '''
    Function to create and return an Interpolator object
    '''
    import interpolator as intprn

    ifFun = intprn.Interpolator()
    ifFun.setPoints(*weatherObj.getPoints())
    ifFun.setProjection(weatherObj.getProjection())

    if itype == 'wet':
        ifFun.getInterpFcns(weatherObj.getWetRefractivity(),
                            interpType=interpType)
    elif itype == 'hydro':
        ifFun.getInterpFcns(weatherObj.getHydroRefractivity(),
                            interpType=interpType)
    return ifFun
Ejemplo n.º 2
0
def main(path='./'):
    
    interp = interpolator.Interpolator()
    
    NbPoints = 10000
    xmin = -5.
    xmax =  5.
    Lmax = np.abs(xmax - xmin)
    dx = Lmax/NbPoints
    print("dx = %f" %dx)
    
    x = interp.Getx(NbPoints, xmin, xmax)
    
    x0 = 0.    # center of the spline function
    dx_L0 = 1.
    
    # Arbitrary order
    order=3
    
    print("order = %d"%order)
    
    shape = interp.getShape(order, x, x0, dx_L0)
    
    # Compute the area using the composite Simpson's rule.
    area_ref = scint.simps(shape, dx=dx)
    print("Area ref=", area_ref)
    
    nbpts = (order+1)
    print("nbpts = (order+1)")
    print("nbr of points = %d"%nbpts)
    
    ik_l = np.arange(nbpts)
    print(ik_l)
    
    bmin = x0 - (order+2)*0.5*dx_L0
    bmax = x0 + (order+2)*0.5*dx_L0
    plt.plot(x,shape)
    
    plt.xlim(bmin, bmax)
    
    plt.xlabel("x",size=20)  
    plt.ylabel("S(x)",size=20) 
Ejemplo n.º 3
0
def main(path='./'):

    if len(sys.argv) == 2:
        path = sys.argv[1]
    # ---------------------- INITIALIZATION  -----------------------------------
    nbrParts = 2
    nbrOrder = 4
    nbrTestCases = nbrParts * nbrOrder

    nbrCellX_l = [40] * nbrTestCases
    nbrCellY_l = [0] * nbrTestCases
    nbrCellZ_l = [0] * nbrTestCases
    nbrCells = {'X': nbrCellX_l, 'Y': nbrCellY_l, 'Z': nbrCellZ_l}

    dx_l = [0.1] * nbrTestCases
    dy_l = [0.] * nbrTestCases
    dz_l = [0.] * nbrTestCases
    meshSize = {'X': dx_l, 'Y': dy_l, 'Z': dz_l}

    interpOrder_l = [1, 2, 3, 4] * nbrParts

    # Hybrid quantities
    field_l = ['rho'] * nbrTestCases

    patchMinX_l = [0.] * nbrTestCases

    particlePosX_l = [1.01] * nbrOrder + [1.09] * nbrOrder

    gl = gridlayout.GridLayout()

    interp = interpolator.Interpolator()

    Direction_l = gl.Direction_l
    Qty_l = gl.Qty_l

    print(nbrTestCases)

    icase_l = np.arange(nbrTestCases)
    print(icase_l)

    # -------- Let us define a function on Bx with Yee lattice --------
    f = open(os.path.join(path, "indexes_summary.txt"), "w")

    # the number of test cases
    f.write("%d \n" % len(icase_l))

    for icase in icase_l:
        order = interpOrder_l[icase]
        nbrX = nbrCellX_l[icase]
        dx = dx_l[icase]
        field = field_l[icase]
        xmin = patchMinX_l[icase]
        xpart = particlePosX_l[icase]

        f.write(
            ("%d %d %5.4f %s %f %f") % (order, nbrX, dx, field, xmin, xpart))

        f.write("\n")

    f.close()

    for icase in icase_l:
        f = open( os.path.join(path,("indexes_testCase%d.txt") % \
        (icase_l[icase])), "w")

        order = interpOrder_l[icase]
        nbrX = nbrCellX_l[icase]
        dx = dx_l[icase]
        field = field_l[icase]
        xmin = patchMinX_l[icase]
        xpart = particlePosX_l[icase]

        reduced = interp.reducedCoord(field, xmin, xpart, dx, gl)

        indexes = interp.indexList(order, reduced)
        print(indexes)
        size = len(indexes)
        print("len(indexes) : %d" % size)

        for index in indexes:
            f.write("%d " % index)

        f.close()
Ejemplo n.º 4
0
    def process_points(self, duration_hrs, map_area='texas', export_maps=True, show_maps=True, create_raster=True):
        # attempt to download the shapefiles: if they are already downloaded, they will not be downloaded again
        str_year = self.str_year
        str_month = self.str_month
        str_day = self.str_day
        duration_hrs = int(duration_hrs)

        if len(str_month) == 1:
            str_month = '0' + str_month
        if len(str_day) == 1:
            str_day = '0' + str_day
        download_status = wgetpy.download(str_year, str_month, str_day, overwrite=False)
        print('Download status: ' + download_status)

        if not PrecipProcessor.process_all_durations or (PrecipProcessor.process_all_durations and duration_hrs == 1):
            # clip shapefiles will only be run once if processing all durations
            self.clip_shapefiles()
            # feature class to feature class will only be run once if processing all durations
            # self.fc_to_fc()
        self.update_all_pts()
        df = self.get_dataframe()

        # create dictionary mapping duration_hrs to all_pts and ddf field names
        dict_dur = {1: ['Dur_1hr', 'd_1hr'], 3: ['Dur_3hr', 'd_3hr'], 6: ['Dur_6h', 'd_6hr'],
                    12: ['Dur_12hr', 'd_12hr'], 24: ['Dur_24hr', 'd_24hr']}
        if duration_hrs in dict_dur:
            ddf_field = dict_dur[duration_hrs][1]
        else:
            # need to add error handling for a value error
            print("Value Error")

        print('make ddf dataframe...')
        sqlitewjt = sqlite_wjt.SQLiteWJT(self.dictSettings['precip_db'])
        sql = "select cast(hrapx as string) || '_' || cast(hrapy as string) as hrap_id, prob, " + ddf_field + \
              " from ddf"
        df_ddf = sqlitewjt.get_dataframe(sql)
        df_ddf = df_ddf.set_index('hrap_id')
        sqlitewjt.close_connection()

        # create new duration columns
        print('make new columns...')
        new_cols = []
        num_new_columns = 24 - duration_hrs + 1
        for i in range(num_new_columns):
            new_cols.append('win_' + str(i + 1))

        # add the new columns to the df and populate
        for i, c in enumerate(new_cols):
            # create and initialize the new column
            df[c] = 0
            # populate new column by adding up hourly values
            for j in range(duration_hrs):
                df[c] += df.ix[:, i + j + 3]

        # create a new column containing the max value from the 'win_' columns,
        # and a column to hold the interpolated probs
        df['max_win_pt'] = df[new_cols].max(axis=1)
        df['return_period'] = 0

        # aggregate on county to get the mean rainfall
        print('export stats to csv...')
        # create a pivot table dataframe: get statistics by county and moving window columns
        # 'aggfunc' can take a list of statistical functions
        df_stats = pd.pivot_table(df, index=['county'], values=new_cols, aggfunc=[np.mean])
        df_stats.to_csv('output\\df_stats_interim.csv')
        # add a new column containing the name of the column with the max value for each county
        # problematic--->df_stats['Max'] = df_stats.ix[:, 3:num_new_columns + 3].idxmax(axis=1)
        df_stats['Max'] = df_stats.ix[:, 0:num_new_columns].idxmax(axis=1)
        # get a series of tuples containing the statistic name and the name of the column containing the max value,
        # e.g. ('mean', 'win_2')
        series_maxcols = df_stats[:]['Max']

        # iterate over the df and find the probability for each point
        print('interpolating...')
        # either get max by point or max average in county
        by_pt_or_area = 'by_point'

        interp = interpolator.Interpolator()
        sqlwjt = sql_wjt.SqlWjt(self.sql_cnn_string)
        # 'max_win_pt' contains the highest window value for the individual point
        max_win_pt_index = df.columns.get_loc('max_win_pt')
        i = 0
        for row in df.iterrows():
            hrap_id = str(row[0])
            hrapx = str(row[1][0])
            hrapy = str(row[1][1])
            county = str(row[1][2])

            # 'series_maxcols' contains, for each county, a tuple ('mean', 'column name'),
            # get the column name for the county's tuple
            if county in series_maxcols.index:
                max_col_name = series_maxcols[county][1]
            else:
                # need to catch an error here
                print('error')
            # get the column index corresponding to the column containing the maximum means for the current county
            max_col_index = df.columns.get_loc(max_col_name)
            # get the depth value in the column
            if by_pt_or_area == 'by_point':
                max_win = row[1][max_win_pt_index]
            else:
                max_win = row[1][max_col_index]
            rp = 0.0
            prob = 1.0
            # be sure the point has ddf values, and has rain; if so, interpolate for probability
            if max_win > 0 and hrap_id in df_ddf.index:
                i += 1
                # filter ddf values into a new df
                dfiter = df_ddf.ix[str(row[0])]
                # check is less than minimum, or gt maximum; if not, interpolate
                if max_win < dfiter.ix[0, ddf_field]:
                    # some rainfall, but less than a 2-yr
                    rp = 1
                elif max_win > dfiter.ix[len(dfiter) - 1, ddf_field]:
                    prob = dfiter.ix[len(dfiter) - 1, 'prob']
                    # add 1 to return period to indicate that depth is beyond the maximum (e.g. 500 yr)
                    rp = 1. / prob + 1
                else:
                    for r in range(len(dfiter) - 1):
                        if dfiter.ix[r, ddf_field] <= max_win < dfiter.ix[r + 1, ddf_field]:
                            prob = interp.interpolate(max_win, dfiter.ix[r, ddf_field], dfiter.ix[r + 1, ddf_field],
                                                      dfiter.ix[r, 'prob'], dfiter.ix[r + 1, 'prob'])
                            rp = 1. / prob
                # update the primary dataframe
                df.ix[hrap_id, 'return_period'] = rp
                # insert results into a table with only 3 fields.
                # combined with a table join at the end, this is ~10X faster than updating all_pts directly
                sql = 'insert into ' + self.hrap_val_table + ' values (' + hrapx + ',' + hrapy + ',' + str(rp) + ')'
                sqlwjt.update_table(sql, commit=False)
                if i % 100 == 0:
                    sqlwjt.commit_update()

        # update sql server fc
        sql = 'update a set a.globvalue = b.val from ' + self.update_table + ' a inner join ' + \
              self.hrap_val_table + ' b ' \
              'on a.hrapx = b.hrapx and a.hrapy = b.hrapy'
        sqlwjt.update_table(sql, commit=True)
        sqlwjt.close_cnn()

        # create new df w/ subset of columns and write df to csv
        dfsubset = df[['hrapx', 'hrapy', 'county', 'max_win_pt', 'return_period']]
        dfsubset.to_csv('output\\' + str_year + str_month + str_day + '_' + str(duration_hrs) + '.csv')
        # determine max return period, and send text if > 100 yr
        max_return_period = dfsubset.sort_values('return_period', ascending=False).iloc[0]['return_period']
        max_return_period_county = dfsubset.sort_values('return_period', ascending=False).iloc[0]['county']
        if max_return_period >= 100 and max_return_period > self.max_return_period:
            self.max_return_period = max_return_period
            self.max_return_period_county = max_return_period_county

        if create_raster:
            # create raster
            print('creating raster...')
            self.create_raster('allpts_raster')
            out_raster = 'raster_' + str_year + str_month + str_day + '_' + str(duration_hrs) + 'hr'
            self.create_raster(out_raster)

        # export pdf
        if export_maps:
            print('exporting pdf...')
            pdf = PdfCreator()
            pdf_name = str_year + str_month + str_day + '_' + str(duration_hrs) + 'hr'
            status = pdf.export_pdf(map_area, pdf_name=pdf_name,
                                    legend_title=str(self.precip_date) + ': ' + str(duration_hrs) + ' hr')
            print(status)
            pdf_file = os.path.join(os.getcwd(), 'output', pdf_name + '.pdf')
            if show_maps:
                os.startfile(pdf_file)
Ejemplo n.º 5
0
def main(path='./'):

    if len(sys.argv) == 2:
        path = sys.argv[1]
    # ---------------------- INITIALIZATION  -----------------------------------
    nbrParts = 2
    nbrOrder = 4
    nbrTestCases = nbrParts * nbrOrder

    nbrCellX_l = [40] * nbrTestCases
    #    nbrCellY_l=[ 0]*nbrTestCases
    #    nbrCellZ_l=[ 0]*nbrTestCases

    dx_l = [0.1] * nbrTestCases
    #    dy_l=[0. ]*nbrTestCases
    #    dz_l=[0. ]*nbrTestCases

    interpOrder_l = [1, 2, 3, 4] * nbrParts

    # Hybrid quantities
    field_l = ['rho'] * nbrTestCases

    patchMinX_l = [0.] * nbrTestCases

    particlePosX_l = [1.01] * nbrOrder + [1.09] * nbrOrder

    gl = gridlayout.GridLayout()

    interp = interpolator.Interpolator()

    print(nbrTestCases)

    icase_l = np.arange(nbrTestCases)
    print(icase_l)

    # -------- Let us define a function on Bx with Yee lattice --------
    f = open(os.path.join(path, "weights_summary.txt"), "w")

    # the number of test cases
    f.write("%d \n" % len(icase_l))

    for icase in icase_l:
        order = interpOrder_l[icase]
        nbrCellX = nbrCellX_l[icase]
        dx = dx_l[icase]
        field = field_l[icase]
        xmin = patchMinX_l[icase]
        xpart = particlePosX_l[icase]

        f.write(("%d %d %5.4f %s %f %f") %
                (order, nbrCellX, dx, field, xmin, xpart))

        f.write("\n")

    f.close()

    for icase in icase_l:
        f = open( os.path.join(path,("weights_testCase%d.txt") % \
        (icase_l[icase])), "w")

        order = interpOrder_l[icase]
        nbrCellX = nbrCellX_l[icase]
        dx = dx_l[icase]
        field = field_l[icase]
        xmin = patchMinX_l[icase]
        xpart = particlePosX_l[icase]

        reduced = interp.reducedCoord(field, xmin, xpart, dx, gl)

        indexes = interp.indexList(order, reduced)
        #        print(indexes)

        #        print("xpart = ", xpart)
        x_ticks = indexes * dx + xmin
        #        print("x_ticks[] = ", x_ticks)
        shape = interp.getShape(order, x_ticks, xpart, dx)
        #        print("shape[] = ", shape)
        #        print("Sum(shape[]) = %f" % sum(shape))

        for weight in shape:
            f.write("%.16f " % weight)

        f.close()