示例#1
0
    def query(self,
              start_date: str,
              end_date: str,
              product: str,
              provider: str = 'LPDAAC_ECS',
              bbox: List = []) -> List[Dict]:
        """ 
        Search CMR database for spectral MODIS tiles matching a temporal range,
        defined by a start date and end date. Returns metadata containing the URL of
        each image.

        Parameters
        ----------
        start_date: string
            Start date yyyy-mm-dd
        end_date: string
            End date yyyy-mm-dd
        product: string
            Product name 
        provider: string
            Provider (default is 'LPDAAC_ECS')
        bbox: List[float]
            Bounding box [lower_left_lon, lower_left_lat, upper_right_lon, upper_right_lat]

        Returns
        ----------
        List[Dict]
            List of granules
        """
        q = GranuleQuery()
        prod, ver = product['products'][0].split('.')
        q.short_name(prod).version(ver)
        q.temporal(f"{start_date}T00:00:00Z", f"{end_date}T23:59:59Z")
        if (len(bbox) >= 4):
            q.bounding_box(*bbox[:4])
        _granules = q.get_all()

        # filter dates
        if 'day_offset' in product.keys():
            day_offset = products[product]['day_offset']
        else:
            day_offset = 0
        granules = []
        for gran in _granules:
            # CMR uses day 1 of window - correct this to be middle of window
            date = (dateparser(gran['time_start'].split('T')[0]) +
                    datetime.timedelta(days=day_offset)).date()
            if (dateparser(start_date).date() <= date <=
                    dateparser(end_date).date()):
                granules.append(gran)
        logger.info("%s granules found within %s - %s" %
                    (len(granules), start_date, end_date))
        return granules
示例#2
0
def query(start_date, end_date, product='MCD43A4.006', provider='LPDAAC_ECS'):
    """ Search CMR database for spectral MODIS tiles matching a temporal range,
    defined by a start date and end date. Returns metadata containing the URL of
    each image.
    """
    q = GranuleQuery()
    prod, ver = product.split('.')
    q.short_name(prod).version(ver)
    q.temporal('%sT00:00:00Z' % str(start_date),
               '%sT23:59:00Z' % str(end_date))
    _granules = q.get_all()

    # filter dates
    day_offset = products[product]['day_offset']
    granules = []
    for gran in _granules:
        # CMR uses day 1 of window - correct this to be middle of window
        date = (dateparser(gran['time_start'].split('T')[0]) +
                datetime.timedelta(days=day_offset)).date()
        if (start_date <= date <= end_date):
            granules.append(gran)
    logger.info("%s granules found within %s - %s" %
                (len(granules), start_date, end_date))
    return granules
示例#3
0
def get_modis(destination, date, search_window, footprint, choose, clip,
              reproject):
    """
    Retrieves MODIS scenes.

    Scenes must entirely contain <footprint> within date +/- search_window.
    """
    workdir = path.join(destination, "modis")
    makedirs(workdir, exist_ok=True)

    footprint_path = path.join(destination, footprint)
    searchFootprint = shape(fiona.open(footprint_path)[0]["geometry"])

    date_window = timedelta(days=search_window)
    date_range = (date - date_window, date + date_window)

    cmrAPI = GranuleQuery()

    results = (cmrAPI.short_name(MODIS_SNOW_CMR_SHORT_NAME).bounding_box(
        *searchFootprint.bounds).temporal(*date_range).get())

    results = json_normalize(results)
    print(results["time_start"].iloc[0])
    print(date)
    results["time_start"] = to_datetime(
        results["time_start"]).dt.tz_localize(None)

    results["timedeltas"] = (date - results.time_start).abs()
    results = results.sort_values(by="timedeltas", ascending=True)
    results["browse"] = [[
        _i["href"] for _i in image_links if _i.get("title", "") == "(BROWSE)"
    ][0] for image_links in results.links]
    print(results.iloc[0].links)

    image_iloc = 0
    if choose:
        print(
            tabulate(
                results[[
                    "cloud_cover", "day_night_flag", "timedeltas", "browse"
                ]].reset_index(drop=True),
                headers="keys",
            ))
        image_iloc = int(
            input("Choose image ID [0-{}]: ".format(len(results) - 1)))

    image_url = results.iloc[image_iloc].links[0]["href"]
    MODIS_DL_COMMAND = ("wget "
                        "--http-user=$EARTHDATA_USERNAME "
                        "--http-password=$EARTHDATA_PASSWORD "
                        "--no-check-certificate --auth-no-challenge "
                        '-r --reject "index.html*" -np -e robots=off -nH -nd '
                        "--directory-prefix={destination} "
                        "{image_url}".format(destination=workdir,
                                             image_url=image_url))

    _sh = Popen(MODIS_DL_COMMAND, shell=True).communicate()

    ## have to do some gdal magic with MODIS data.
    ## 1) turn it into a TIFF with the right projection.
    MODISfile = glob(path.join(workdir, "MOD10A1*.hdf*"))[0]
    output_file = path.join(workdir, "MODIS_reproj.tif")

    MODIS_CONVERT_COMMAND = (
        "gdalwarp "
        "HDF4_EOS:EOS_GRID:{path}:MOD_Grid_Snow_500m:NDSI_Snow_Cover "
        "-cutline {footprint} -crop_to_cutline -dstnodata 9999 "
        "-t_srs {projection} -r cubic "
        "-s_srs '+proj=sinu +R=6371007.181 +nadgrids=@null +wktext' "
        "{output_tif} ".format(
            path=MODISfile,
            footprint=footprint_path,
            projection=reproject,
            output_tif=output_file,
        ))

    _sh = Popen(MODIS_CONVERT_COMMAND, shell=True).communicate()

    print(MODISfile, output_file)