コード例 #1
0
ファイル: burned_area.py プロジェクト: firelab/met_utils
def ba_multiyear_ratios(years,
                        ba_template,
                        ind_template,
                        ind_names,
                        histo_outfile=None,
                        ratio_outfile=None,
                        bins=10,
                        minmaxyears=None):
    """computes multiyear histograms and stores in a netcdf file."""

    # open netcdf files
    bafiles = []
    indfiles = []
    for y in years:
        bafiles.append(nc.Dataset(ba_template % y))
        indfiles.append(nc.Dataset(ind_template % y))

    # compute min/max
    if minmaxyears is None:
        minmax = oi.multifile_minmax(indfiles, ind_names)
    else:
        minmax = oi.multifile_minmax(ind_template,
                                     ind_names,
                                     years=minmaxyears)

    if not ('__iter__' in dir(bins)):
        bins = [bins] * len(ind_names)
    minmax = zip(minmax[0], minmax[1], bins)

    # compute histogram
    ratios, halfdeg_counts, histo = ba_ratio_histograms(
        bafiles, indfiles, ind_names, minmax)

    # write output
    if histo_outfile is not None:
        ah.save_sparse_histos(histo, histo_outfile)
    if ratio_outfile is not None:
        write_raw_ratio_file(ratio_outfile, ratios, halfdeg_counts, ind_names,
                             minmax)

    # close netcdf files
    for i_files in range(len(years)):
        bafiles[i_files].close()
        indfiles[i_files].close()

    return ratios, histo
コード例 #2
0
ファイル: burned_area.py プロジェクト: firelab/met_utils
def ba_multiyear_ratios(years, ba_template, ind_template, ind_names, 
                histo_outfile=None, ratio_outfile=None, bins=10, minmaxyears=None) :
    """computes multiyear histograms and stores in a netcdf file."""

    # open netcdf files
    bafiles = [ ]
    indfiles = [ ] 
    for y in years : 
        bafiles.append(nc.Dataset(ba_template % y))
        indfiles.append(nc.Dataset(ind_template % y))

    # compute min/max
    if minmaxyears is None :
        minmax = oi.multifile_minmax(indfiles, ind_names)
    else :
        minmax = oi.multifile_minmax(ind_template, ind_names, years=minmaxyears)
        
    if not ('__iter__' in dir(bins)) : 
        bins = [ bins ] * len(ind_names)
    minmax = zip(minmax[0], minmax[1], bins)

    # compute histogram
    ratios, halfdeg_counts, histo = ba_ratio_histograms(bafiles, indfiles, ind_names, minmax)
    
    # write output
    if histo_outfile is not None : 
        ah.save_sparse_histos(histo, histo_outfile)
    if ratio_outfile is not None: 
        write_raw_ratio_file(ratio_outfile, ratios, halfdeg_counts, ind_names, minmax)

    # close netcdf files
    for i_files in range(len(years)) : 
        bafiles[i_files].close()
        indfiles[i_files].close()

    return ratios, histo
コード例 #3
0
ファイル: burned_area.py プロジェクト: firelab/met_utils
def sparse_multiyear_histogram(years, csv_template, bahistfile, 
                            count_threshold=50, bins=25, out_template=None) : 
    """computes and optionally saves sparse histograms of MODIS BA counts"""
    # open the ba histogram file
    bahist = nc.Dataset(bahistfile)
    counts = bahist.variables['burned_total']
    
    # read all csv files and concatenate
    file_list = []
    for y in years :  
        file_list.append(pd.read_csv(csv_template % y))
    compare = pd.concat(file_list)
    compare = compare[ np.logical_and(compare.icol(0)>=10,compare.icol(0)<364) ] 
    
    # get min/max/bin from multiyear histogram file
    mmb, binsizes = read_multiyear_minmax(bahist,counts.dimensions)
        
    # create an indexer
    index = ah.init_indexers(mmb) 
    
    # strip out geometry
    dim_bins = [m[2] for m in mmb]   
    
    # create sparse histograms
    shisto_forest = ah.SparseKeyedHistogram(minmax=mmb, threshold=count_threshold,
                           bins=bins, default_minmax=(1,count_threshold,count_threshold-1))
    shisto_not_forest = ah.SparseKeyedHistogram(minmax=mmb, threshold=count_threshold,
                           bins=bins, default_minmax=(1,count_threshold,count_threshold-1))
    shisto_total = ah.SparseKeyedHistogram(minmax=mmb, threshold=count_threshold,
                           bins=bins, default_minmax=(1,count_threshold,count_threshold-1))

                           

    # loop through all bins with nonzero data
    i_nonzero = np.where( counts[:]>0 )
    for i_bin in zip(*i_nonzero) : 
        total = select_data(compare, counts.dimensions, i_bin, index, dim_bins)
        forest = total[ total.ix[:,1].isin(FOREST_LC) ]
        not_forest = total [ total.ix[:,1].isin(NONFOREST_LC) ]

        shisto_forest.put_combo(i_bin, forest['BA Count'], units=False)
        shisto_not_forest.put_combo(i_bin, not_forest["BA Count"], units=False)
        shisto_total.put_combo(i_bin, total['BA Count'], units=False)
        
    # save file if filename template specified
    if out_template is not None : 
        ah.save_sparse_histos(shisto_total, out_template%'total')
        ah.save_sparse_histos(shisto_forest, out_template%'forest')
        ah.save_sparse_histos(shisto_not_forest, out_template%'not_forest')
        
    bahist.close()
    
    return (shisto_total, shisto_forest, shisto_not_forest)
コード例 #4
0
ファイル: burned_area.py プロジェクト: firelab/met_utils
def sparse_multiyear_histogram(years,
                               csv_template,
                               bahistfile,
                               count_threshold=50,
                               bins=25,
                               out_template=None):
    """computes and optionally saves sparse histograms of MODIS BA counts"""
    # open the ba histogram file
    bahist = nc.Dataset(bahistfile)
    counts = bahist.variables['burned_total']

    # read all csv files and concatenate
    file_list = []
    for y in years:
        file_list.append(pd.read_csv(csv_template % y))
    compare = pd.concat(file_list)
    compare = compare[np.logical_and(
        compare.icol(0) >= 10,
        compare.icol(0) < 364)]

    # get min/max/bin from multiyear histogram file
    mmb, binsizes = read_multiyear_minmax(bahist, counts.dimensions)

    # create an indexer
    index = ah.init_indexers(mmb)

    # strip out geometry
    dim_bins = [m[2] for m in mmb]

    # create sparse histograms
    shisto_forest = ah.SparseKeyedHistogram(
        minmax=mmb,
        threshold=count_threshold,
        bins=bins,
        default_minmax=(1, count_threshold, count_threshold - 1))
    shisto_not_forest = ah.SparseKeyedHistogram(
        minmax=mmb,
        threshold=count_threshold,
        bins=bins,
        default_minmax=(1, count_threshold, count_threshold - 1))
    shisto_total = ah.SparseKeyedHistogram(
        minmax=mmb,
        threshold=count_threshold,
        bins=bins,
        default_minmax=(1, count_threshold, count_threshold - 1))

    # loop through all bins with nonzero data
    i_nonzero = np.where(counts[:] > 0)
    for i_bin in zip(*i_nonzero):
        total = select_data(compare, counts.dimensions, i_bin, index, dim_bins)
        forest = total[total.ix[:, 1].isin(FOREST_LC)]
        not_forest = total[total.ix[:, 1].isin(NONFOREST_LC)]

        shisto_forest.put_combo(i_bin, forest['BA Count'], units=False)
        shisto_not_forest.put_combo(i_bin, not_forest["BA Count"], units=False)
        shisto_total.put_combo(i_bin, total['BA Count'], units=False)

    # save file if filename template specified
    if out_template is not None:
        ah.save_sparse_histos(shisto_total, out_template % 'total')
        ah.save_sparse_histos(shisto_forest, out_template % 'forest')
        ah.save_sparse_histos(shisto_not_forest, out_template % 'not_forest')

    bahist.close()

    return (shisto_total, shisto_forest, shisto_not_forest)