Пример #1
0
def HabMapItemCSV(species, publicationDate, csvName):
    '''
    (list, integer, string) -> pandas DataFrame and saved CSV file.
    
    Creates a dataframe and csv file with rows for species in your list and columns for
        each of the pieces of information needed when updating metadata records on 
        ScienceBase for habitat maps.
    
    Notes:
    The "end_date" column pulls from tblUpdateDateTime, but it's unclear how this table
        is and was populated.  Many entries are blank, so this function uses 2013 if the
        model editing field is blank.
    
    Arguments:
    species -- Python list of GAP species codes to process
    publicationDate -- A year to use as the publication date
    csvName -- Path and name of where to save csv file
    
    Example:
    >>>DF = HabMapItemCSV(["aAMBUx", "bCOHAx", "bAMROx", "bCOMEx"], 
                               publicationDate = 2017, csvName="T:/temp/SBTable.csv")    
    '''
    import pandas as pd, gapdb, gapmodeling
    # Intialize a dataframe
    DF0 = pd.DataFrame()
    DF0.index.name = "GAP_code"

    abstract_text = """This dataset represents a species habitat distribution model for {0}.  These habitat maps are created by applying a <a href="https://www.sciencebase.gov/catalog/item/527d0a83e4b0850ea0518326">deductive habitat model</a> to remotely-sensed data layers within a species' range."""

    citation_text = """U.S. Geological Survey - Gap Analysis Program, {2}, {0} ({1}) Habitat Map, U.S. Geological Survey data release, {3}."""

    # Fill out desired columns
    for sp in species:
        print sp
        nameCom = gapdb.NameCommon(sp)
        nameSci = gapdb.NameSci(sp)
        DF0.loc[sp, "common_name"] = nameCom
        DF0.loc[sp, "scientific_name"] = nameSci
        DF0.loc[sp, "start_date"] = 2008
        try:
            DF0.loc[sp, "end_date"] = gapdb.ProcessDate(sp).year
        except:
            DF0.loc[sp, "end_date"] = 2013
        DF0.loc[sp, "publication_date"] = publicationDate
        DF0.loc[sp, "citation"] = citation_text.format(nameCom, nameSci,
                                                       publicationDate,
                                                       GetHabMapDOI(sp))
        DF0.loc[sp, "place_keywords"] = "United States"
        DF0.loc[sp, "theme_keywords"] = "{1}, {0}".format(nameCom, nameSci)
        DF0.loc[sp, "editor"] = gapdb.Who(sp, "edited_model")
        DF0.loc[sp, "reviewer"] = gapdb.Who(sp, "reviewed_model")
        DF0.loc[sp, "NatureServe_element_code"] = gapdb.Crosswalk(sp)[1]
        DF0.loc[sp, "TSN_code"] = gapdb.Crosswalk(sp)[2]
        DF0.loc[sp, "Global_SEQ_ID"] = gapdb.Crosswalk(sp)[3]
        DF0.loc[sp, "input_data"] = str(gapmodeling.SpeciesInputs(sp))
        DF0.loc[sp, "IPDS"] = "IP-082267"
        DF0.loc[sp, "abstract"] = abstract_text.format(nameCom)
        DF0.loc[sp, "doi"] = GetHabMapDOI(sp)
        DF0.loc[sp, "ScienceBase_url"] = GetHabMapURL(sp)

    DF0.to_csv(csvName)
    return DF0
Пример #2
0
def SaveModelJSON(species, saveDir, year=2001, version=1):
    '''
    (string, string, integer, integer) -> dictionary
    
    Builds a species or subspecies-level dictionary of all the relevant 
        information for a habitat modeling.  The dictionary format is for
        ScienceBase.  Returns the dictionary and saves a copy as a json file
        in the directory you provide.
    
    Arguments:
    species -- gap species code
    saveDir -- a directory to save json files in
    year -- the year of the data used in modeling (2001 or 2011)
    version -- the version number of the model (at the species level)
    
    Example:
    >>test = SaveModelJSON("bAMROx", saveDIR="C:/Temp", year=2001, version=1)
    '''
    import gapdb, gapmodeling, json

    fileName = "{0}_CONUS_HabModel_{1}v{2}.json"
    try:
        # Make an empty dictionary to collect model dictionaries
        if year == 2001:
            speciesDict = {"input_layers": gapmodeling.layers_2001}
        elif year == 2011:
            speciesDict = {"input_layers": gapmodeling.layers_2011}

        # List taxanomic information
        taxonomic = {
            "common_name": gapdb.NameCommon(species),
            "scientific_name": gapdb.NameSci(species),
            "gap_code": gapdb.Crosswalk(species)[0],
            "ELCode": gapdb.Crosswalk(species)[1],
            "ITIS_TSN": gapdb.Crosswalk(species)[2],
            "Global_SEQ_ID": gapdb.Crosswalk(species)[3]
        }
        speciesDict["taxonomic"] = taxonomic

        # Add DOI
        speciesDict["doi"] = GetHabMapDOI(species)

        # Add ID
        speciesDict["sb_url"] = GetHabMapURL(species)

        # Add the species' habitat description
        description = gapmodeling.getHabitatDescription(species)
        speciesDict["habitat_description"] = description

        # Add species' references/citations
        referencesDF = gapmodeling.SpReferences(species)
        # This returns a dictionary where the key is reference code
        # and the value is the reference text
        references = dict(referencesDF["memCitation"])
        # Get just the reference text string and not the reference code
        refText = references.values()
        speciesDict["references"] = refText

        # Establish a file name for speciesDict
        fileName = str(saveDir + fileName.format(species, year, version))

        # Get python tuple (like a list) of regional model codes
        modelCodes = [
            x for x in gapmodeling.ModelCodes(
                species, publishedOnly=True, conusOnly=True, migratory=False)
            if int(x[-1:]) in [1, 2, 3, 4, 5, 6]
        ]

        # Get the models as dictionaries, add to a dictionary of models, then add that
        # to the species-level dictionary
        models = {}
        for model in modelCodes:
            modelDict = gapmodeling.ModelAsDictionary(model, ecolSystem="both")
            models[model] = modelDict
        speciesDict["models"] = models

        # Save species model dictionary as json object
        with open(fileName, "w") as outfile:
            json.dump(speciesDict, outfile)
        return speciesDict, fileName

    except Exception as e:
        print(e)
        return False
Пример #3
0
def SppInAOI(AOIShp, hucShp, workDir, origin, season, reproduction,
                 presence):
    '''
    (string, string, string, string, list, list, list, list) -> list
    
    Returns a list of species occurring within the provided polygon.  Runtime
    is about 3-5 minutes.
    
    Arguments:
    AOIShp -- A shapefile polygon (dissolved) to investigate.  Should have 
        the same coordinate systems as the huc shapefile.
    hucShp -- A 12 digit huc shapefile that matches the GAP species database hucs.
    workDir -- Where to work and save output.
    origin -- Origin codes to include.
    season -- Season codes to include.
    reproduction -- Reproduction codes to include.
    presence -- Presence codes to include.
    
    Example:
    >>> sppList = SppInPolygon(AOIShp = "T:/Temp/BlueMountains2.shp",
                               hucShp = config.hucs,
                               workDir = "T:/Temp/",
                               origin = [1],
                               season = [1, 3, 4],
                               reproduction = [1, 2, 3],
                               presence = [1, 2, 3])
    '''    
    import arcpy
    arcpy.ResetEnvironments()
    arcpy.env.overwriteOutput=True
    arcpy.env.workspace = workDir
    import pandas as pd
    
    ##############################################  Get list of hucs within polygon
    ###############################################################################
    print("\nSelecting HUCs completely within the AOI shapefile\n")
    arcpy.management.MakeFeatureLayer(hucShp, 'HUCs_lyr')
    arcpy.management.MakeFeatureLayer(AOIShp, 'shp_lyr')
    arcpy.management.SelectLayerByLocation('HUCs_lyr', 'INTERSECT', 'shp_lyr')
    
    # Make an empty list to append
    selHUCsList = []
    # Get the fields from the input selected HUCs layer
    fields = arcpy.ListFields('HUCs_lyr')
    # Create a fieldinfo object
    fieldinfo = arcpy.FieldInfo()
    # Use only the HUC12RNG field and set it to fieldinfo
    for field in fields:
        if field.name == "HUC12RNG":
            fieldinfo.addField(field.name, field.name, "VISIBLE", "")
    # The selected HUCs layer will have fields as set in fieldinfo object
    arcpy.MakeTableView_management("HUCs_lyr", "selHUCsTV", "", "", fieldinfo)
    # Loop through the selected HUCs and add them to a list
    for row in sorted(arcpy.da.SearchCursor('selHUCsTV', ['HUC12RNG'])):
        selHUCsList.append(row[0])
    # Make the selected HUCs list a set for comparing with species range HUCs
    selHUCsSet = set(selHUCsList)
    
    #################################################  Get a species list to assess
    ###############################################################################  
    print("Comparing species ranges to selected HUCs\n")
    ## Make WHRdb and Species databse connections
    whrCursor, whrConn = gapdb.ConnectWHR()
    sppCursor, sppConn = gapdb.ConnectSppDB()
    
    # Build and SQL statement that returns CONUS
    # full species codes and names that are in the modeled list
    sql = """SELECT t.strUC, t.strCommonName, t.strScientificName,
                    t.strsubSciNameText, t.ysnInclude, intRegionCode               
                    FROM dbo.tblAllSpecies as t
                    WHERE (t.ysnInclude = 'True') AND t.intRegionCode < 7"""
    
    # Pull into a dataframe
    dfAllSpp = pd.read_sql(sql, whrConn)
     # Drop the region code and include fields
    dfAllSpp = dfAllSpp.drop(['intRegionCode','ysnInclude'], axis=1)
    # Drop duplicates to get unique species codes
    dfUnique = dfAllSpp.drop_duplicates(subset='strUC', keep='first')
    
    ################################  Asses each species' occurence in polygon hucs
    ###############################################################################  
    # List to collect species in AOI
    masterList = []
    for SC in list(dfUnique.strUC):
        taxa = dictionaries.taxaDict[SC[0]]
        
        # What hucs are species' in?
        sql = """SELECT t.strHUC12RNG, t.strUC, t.intGapOrigin, t.intGapPres, 
                    t.intGapRepro, t.intGapSeas 
                    FROM dbo.tblRanges_""" + taxa + """ as t
                    WHERE (t.strUC = '""" + str(SC) + """') 
                    AND t.strHUC12RNG < '190000000000'"""
        dfRngHUCs = pd.read_sql(sql, sppConn)
        
        # Which hucs have acceptable attributes?
        select={'intGapPres':presence, 'intGapSeas':season, 
                'intGapOrigin':origin, 'intGapRepro':reproduction}
        dfS1 = dfRngHUCs[dfRngHUCs[select.keys()].isin(select).all(axis=1)]   
        
        # Get the strHUC12RNG column into a set
        SpeciesSet = set(dfS1[dfS1.columns[0]].tolist())
        
        # Compare the species and AOI huc sets to see if there's any overlap.
        if len(selHUCsSet & SpeciesSet) > 0:
            print(gapdb.NameCommon(SC))
            masterList.append(SC)
        else:
            pass 
    
    if len(masterList) == 0:
        print "!!!!  There was some sort of problem  !!!!\n"
    else:
        # Delete cursors and close db connections
        sppConn.close()
        whrConn.close()
        del sppCursor, sppConn
        del whrCursor, whrConn
        
        return masterList