nasa公共元数据存储库(cmr)api的python包装器。
python-cmr的Python项目详细描述
python cmr
python cmr是nasa eosdis的一个易于使用的包装器 Common Metadata Repository API。这个包裹的目的是 通过提供抢先检查的方法来直观地查询api并且不易出错 对于无效输入并处理cmr api所需的url编码。
访问nasa的地球科学元数据非常简单:
>>> from cmr import CollectionQuery, GranuleQuery >>> api = CollectionQuery() >>> collections = api.archive_center("LP DAAC").keyword("AST_L1*").get(5) >>> for collection in collections: >>> print(collection["short_name"]) AST_L1A AST_L1AE AST_L1T >>> api = GranuleQuery() >>> granules = api.short_name("AST_L1T").point(-112.73, 42.5).get(3) >>> for granule in granules: >>> print(granule["title"]) SC:AST_L1T.003:2149105822 SC:AST_L1T.003:2149105820 SC:AST_L1T.003:2149155037
安装
从pypi安装:
$ pip install python-cmr
要从github安装,可以尝试dev分支:
$ git clone https://github.com/jddeal/python-cmr $ cd python-cmr $ pip install .
示例
这个库分为两类,collectionquery和granulequery。每一个 类提供了一组用于为cmr生成查询的方法。未提供所有参数 cmr的api包含在这个版本的python cmr中。
collection和granular查询都可以使用以下方法:
# search for granules matching a specific product/short_name >>> api.short_name("AST_L1T") # search for granules matching a specific version >>> api.version("006") # search for granules at a specific longitude and latitude >>> api.point(-112.73, 42.5) # search for granules in an area bound by a box (lower left lon/lat, upper right lon/lat) >>> api.bounding_box(-112.70, 42.5, -110, 44.5) # search for granules in a polygon (these need to be in counter clockwise order and the # last coordinate must match the first in order to close the polygon) >>> api.polygon([(-100, 40), (-110, 40), (-105, 38), (-100, 40)]) # search for granules in a line >>> api.line([(-100, 40), (-90, 40), (-95, 38)]) # search for granules in an open or closed date range >>> api.temporal("2016-10-10T01:02:00Z", "2016-10-12T00:00:30Z") >>> api.temporal("2016-10-10T01:02:00Z", None) >>> api.temporal(datetime(2016, 10, 10, 1, 2, 0), datetime.now()) # only include granules available for download >>> api.downloadable() # only include granules that are unavailable for download >>> api.online_only() # search for collections/granules associated with or identified by concept IDs # note: often the ECHO collection ID can be used here as well # note: when using CollectionQuery, only collection concept IDs can be passed # note: when uses GranuleQuery, passing a collection's concept ID will filter by granules associated # with that particular collection. >>> api.concept_id("C1299783579-LPDAAC_ECS") >>> api.concept_id(["G1327299284-LPDAAC_ECS", "G1326330014-LPDAAC_ECS"])
颗粒搜索支持这些方法(除了上面的共享方法之外):
# search for a granule by its unique ID >>> api.granule_ur("SC:AST_L1T.003:2150315169") # search for granules from a specific orbit >>> api.orbit_number(5000) # filter by the day/night flag >>> api.day_night_flag("day") # filter by cloud cover percentage range >>> api.cloud_cover(25, 75) # filter by specific instrument or platform >>> api.instrument("MODIS") >>> api.platform("Terra")
集合搜索支持这些方法(除了上面的共享方法之外):
# search for collections from a specific archive center >>> api.archive_center("LP DAAC") # case insensitive, wildcard enabled text search through most collection fields >>> api.keyword("M*D09")
作为将方法链接在一起以设置查询参数的替代方法,可以使用 方法允许您将参数作为关键字参数传递:
# search for AST_L1T version 003 granules at latitude 42, longitude -100 >>> api.parameters( short_name="AST_L1T", version="003", point=(-100, 42) )
注意:Kwarg键应该与上述示例中方法的名称和值相匹配 如果是需要多个值的参数,则应为元组。
要检查和检索api的结果,可以使用以下方法:
# inspect the number of results the query will return without downloading the results >>> print(api.hits()) # retrieve 100 granules >>> granules = api.get(100) # retrieve 25,000 granules >>> granules = api.get(25000) # retrieve all the granules possible for the query >>> granules = api.get_all() # this is a shortcut for api.get(api.hits())
默认情况下,响应将以json形式返回,并可作为python字典列表访问。 在发出请求之前,可以指定其他格式:
>>> granules = api.format("echo10").get(100)
颗粒查询和集合查询都支持以下格式:
- json(默认)
- XML
- 回声10
- ISO
- ISO 19115
- CSV
- 原子
- kml
- 本地的
集合查询还支持以下格式:
- dif
- DIF10
- 打开数据
- umm_json
- umm_json_vx_y(例如:umm_json_v1_9)