Example #1
0
def RetrieveData(Date, args):
    """
    This function retrieves MOD13 NDVI data for a given date from the
    http://e4ftl01.cr.usgs.gov/ server.

    Keyword arguments:
    Date -- 'yyyy-mm-dd'
    args -- A list of parameters defined in the DownloadData function.
    """

    # WAPOR modules
    import pyWAPOR.Functions.Processing_Functions as PF

    # Argument
    [
        output_folder, TilesVertical, TilesHorizontal, latlim, lonlim,
        username, password, hdf_library
    ] = args

    # Define output file
    NDVIfileName = os.path.join(
        output_folder, 'NDVI_MYD13Q1_-_16-daily_' + Date.strftime('%Y') + '.' +
        Date.strftime('%m') + '.' + Date.strftime('%d') + '.tif')

    if not os.path.exists(NDVIfileName):

        # Collect the data from the MODIS webpage and returns the data and lat and long in meters of those tiles
        try:
            Collect_data(TilesHorizontal, TilesVertical, Date, username,
                         password, output_folder, hdf_library)

            # Define the output name of the collect data function
            name_collect = os.path.join(output_folder, 'Merged.tif')

            # Reproject the MODIS product to epsg_to
            epsg_to = '4326'
            name_reprojected = PF.reproject_MODIS(name_collect, epsg_to)

            # Clip the data to the users extend
            data, geo = PF.clip_data(name_reprojected, latlim, lonlim)

            # Save results as Gtiff
            PF.Save_as_tiff(name=NDVIfileName,
                            data=data,
                            geo=geo,
                            projection='WGS84')

            # remove the side products
            os.remove(name_collect)
            os.remove(name_reprojected)

        except:
            print("Was not able to download the file")

    return True
Example #2
0
def DownloadData(output_folder, latlim, lonlim):
    """
    This function downloads DEM data from SRTM

    Keyword arguments:
    output_folder -- directory of the result
	latlim -- [ymin, ymax] (values must be between -60 and 60)
    lonlim -- [xmin, xmax] (values must be between -180 and 180)
    """

    # WA+ modules
    import pyWAPOR.Functions.Processing_Functions as PF

    # Check the latitude and longitude and otherwise set lat or lon on greatest extent
    if latlim[0] < -60 or latlim[1] > 60:
        print(
            'Latitude above 60N or below 60S is not possible. Value set to maximum'
        )
        latlim[0] = np.max(latlim[0], -60)
        latlim[1] = np.min(latlim[1], 60)
    if lonlim[0] < -180 or lonlim[1] > 180:
        print(
            'Longitude must be between 180E and 180W. Now value is set to maximum'
        )
        lonlim[0] = np.max(lonlim[0], -180)
        lonlim[1] = np.min(lonlim[1], 180)

    # converts the latlim and lonlim into names of the tiles which must be
    # downloaded
    name, rangeLon, rangeLat = Find_Document_Names(latlim, lonlim)

    # Memory for the map x and y shape (starts with zero)
    size_X_tot = 0
    size_Y_tot = 0

    nameResults = []

    # Create a temporary folder for processing
    output_folder_trash = os.path.join(output_folder, "Temp")
    if not os.path.exists(output_folder_trash):
        os.makedirs(output_folder_trash)

    # Download, extract, and converts all the files to tiff files
    for nameFile in name:

        try:
            # Download the data from
            # http://earlywarning.usgs.gov/hydrodata/
            output_file, file_name = Download_Data(nameFile,
                                                   output_folder_trash)

            # extract zip data
            PF.Extract_Data(output_file, output_folder_trash)

            # The input is the file name and in which directory the data must be stored
            file_name_tiff = file_name.replace(".zip", ".tif")
            output_tiff = os.path.join(output_folder_trash, file_name_tiff)

            # convert data from adf to a tiff file
            dest_SRTM = gdal.Open(output_tiff)
            geo_out = dest_SRTM.GetGeoTransform()
            size_X = dest_SRTM.RasterXSize
            size_Y = dest_SRTM.RasterYSize

            if (int(size_X) != int(6001) or int(size_Y) != int(6001)):
                data = np.ones((6001, 6001)) * -9999

                # Create the latitude bound
                Vfile = nameFile.split("_")[2][0:2]
                Bound2 = 60 - 5 * (int(Vfile) - 1)

                # Create the longitude bound
                Hfile = nameFile.split("_")[1]
                Bound1 = -180 + 5 * (int(Hfile) - 1)

                Expected_X_min = Bound1
                Expected_Y_max = Bound2

                Xid_start = int(
                    np.round((geo_out[0] - Expected_X_min) / geo_out[1]))
                Xid_end = int(
                    np.round(
                        ((geo_out[0] + size_X * geo_out[1]) - Expected_X_min) /
                        geo_out[1]))
                Yid_start = int(
                    np.round((Expected_Y_max - geo_out[3]) / (-geo_out[5])))
                Yid_end = int(
                    np.round((Expected_Y_max - (geo_out[3] +
                                                (size_Y * geo_out[5]))) /
                             (-geo_out[5])))

                data_SRTM = dest_SRTM.GetRasterBand(1).ReadAsArray()

                data[Yid_start:Yid_end, Xid_start:Xid_end] = data_SRTM
                if np.max(data) == 255:
                    data[data == 255] = -9999
                data[data < -9999] = -9999

                geo_in = [
                    Bound1 - 0.5 * 0.00083333333333333, 0.00083333333333333,
                    0.0, Bound2 + 0.5 * 0.00083333333333333, 0.0,
                    -0.0008333333333333333333
                ]

                # save chunk as tiff file
                PF.Save_as_tiff(name=output_tiff,
                                data=data,
                                geo=geo_in,
                                projection="WGS84")

                dest_SRTM = None

        except:

            # If tile not exist create a replacing zero tile (sea tiles)
            file_name_tiff = file_name.replace(".zip", ".tif")
            output_tiff = os.path.join(output_folder_trash, file_name_tiff)
            file_name = nameFile
            data = np.ones((6001, 6001)) * -9999
            data = data.astype(np.float32)

            # Create the latitude bound
            Vfile = nameFile.split("_")[2][0:2]
            Bound2 = 60 - 5 * (int(Vfile) - 1)

            # Create the longitude bound
            Hfile = nameFile.split("_")[1]
            Bound1 = -180 + 5 * (int(Hfile) - 1)

            # Geospatial data for the tile
            geo_in = [
                Bound1 - 0.5 * 0.00083333333333333, 0.00083333333333333, 0.0,
                Bound2 + 0.5 * 0.00083333333333333, 0.0,
                -0.0008333333333333333333
            ]

            # save chunk as tiff file
            PF.Save_as_tiff(name=output_tiff,
                            data=data,
                            geo=geo_in,
                            projection="WGS84")

        # clip data
        Data, Geo_data = PF.clip_data(output_tiff, latlim, lonlim)
        size_Y_out = int(np.shape(Data)[0])
        size_X_out = int(np.shape(Data)[1])

        # Total size of the product so far
        size_Y_tot = int(size_Y_tot + size_Y_out)
        size_X_tot = int(size_X_tot + size_X_out)

        if nameFile is name[0]:
            Geo_x_end = Geo_data[0]
            Geo_y_end = Geo_data[3]
        else:
            Geo_x_end = np.min([Geo_x_end, Geo_data[0]])
            Geo_y_end = np.max([Geo_y_end, Geo_data[3]])

        # create name for chunk
        FileNameEnd = "%s_temporary.tif" % (file_name)
        nameForEnd = os.path.join(output_folder_trash, FileNameEnd)
        nameResults.append(str(nameForEnd))

        # save chunk as tiff file
        PF.Save_as_tiff(name=nameForEnd,
                        data=Data,
                        geo=Geo_data,
                        projection="WGS84")

    size_X_end = int(size_X_tot / len(rangeLat)) + 1  #!
    size_Y_end = int(size_Y_tot / len(rangeLon)) + 1  #!

    # Define the georeference of the end matrix
    geo_out = [Geo_x_end, Geo_data[1], 0, Geo_y_end, 0, Geo_data[5]]

    latlim_out = [geo_out[3] + geo_out[5] * size_Y_end, geo_out[3]]
    lonlim_out = [geo_out[0], geo_out[0] + geo_out[1] * size_X_end]

    # merge chunk together resulting in 1 tiff map
    datasetTot = Merge_DEM(latlim_out, lonlim_out, nameResults, size_Y_end,
                           size_X_end)

    datasetTot[datasetTot < -9999] = -9999

    # name of the end result
    output_DEM_name = "DEM_SRTM_m_3s.tif"

    Save_name = os.path.join(output_folder, output_DEM_name)

    # Make geotiff file
    PF.Save_as_tiff(name=Save_name,
                    data=datasetTot,
                    geo=geo_out,
                    projection="WGS84")
    os.chdir(output_folder)

    # Delete the temporary folder
    try:
        shutil.rmtree(output_folder_trash)
    except:
        pass

    return ()
Example #3
0
def RetrieveData(Date, args):
    """
    This function retrieves MYD11 LST data for a given date from the
    https://e4ftl01.cr.usgs.gov/ server.

    Keyword arguments:
    Date -- 'yyyy-mm-dd'
    args -- A list of parameters defined in the DownloadData function.
    """
    
    # WAPOR modules
    import pyWAPOR.Functions.Processing_Functions as PF
    
    # Argument
    [output_folder, TilesVertical, TilesHorizontal,lonlim, latlim, username, password, hdf_library] = args
    
    # Define output names
    LSTfileName = os.path.join(output_folder, 'LST_MYD11A1_K_daily_' + Date.strftime('%Y') + '.' + Date.strftime('%m') + '.' + Date.strftime('%d') + '.tif')
    TimefileName = os.path.join(output_folder, 'Time_MYD11A1_hour_daily_' + Date.strftime('%Y') + '.' + Date.strftime('%m') + '.' + Date.strftime('%d') + '.tif')    
    OnsangfileName = os.path.join(output_folder, 'Angle_MYD11A1_degrees_daily_' + Date.strftime('%Y') + '.' + Date.strftime('%m') + '.' + Date.strftime('%d') + '.tif')    

    if not (os.path.exists(LSTfileName) and os.path.exists(TimefileName)):

        # Collect the data from the MODIS webpage and returns the data and lat and long in meters of those tiles
        try:
            Collect_data(TilesHorizontal, TilesVertical, Date, username, password, output_folder, hdf_library)
        
            # Define the output name of the collect data function
            name_collect = os.path.join(output_folder, 'Merged.tif')
            name_collect_time = os.path.join(output_folder, 'Merged_Time.tif')
            name_collect_obsang = os.path.join(output_folder, 'Merged_Obsang.tif')
            
            # Reproject the MODIS product to epsg_to
            epsg_to ='4326'
            name_reprojected = PF.reproject_MODIS(name_collect, epsg_to)
            name_reprojected_time = PF.reproject_MODIS(name_collect_time, epsg_to)
            name_reprojected_obsang = PF.reproject_MODIS(name_collect_obsang, epsg_to)
            
            # Clip the data to the users extend
            data, geo = PF.clip_data(name_reprojected, latlim, lonlim)
            data_time, geo = PF.clip_data(name_reprojected_time, latlim, lonlim)
            data_obsang, geo = PF.clip_data(name_reprojected_obsang, latlim, lonlim)
            
            # remove wrong values
            data[data==0.] = -9999
            data_time[data_time==25.5] = -9999
           
            # Save results as Gtiff 
            PF.Save_as_tiff(name=LSTfileName, data=data, geo=geo, projection='WGS84')
            PF.Save_as_tiff(name=TimefileName, data=data_time, geo=geo, projection='WGS84')
            PF.Save_as_tiff(name=OnsangfileName, data=data_obsang, geo=geo, projection='WGS84')
             
            # remove the side products
            os.remove(name_collect)
            os.remove(name_reprojected)
            os.remove(name_collect_time)
            os.remove(name_reprojected_time) 
            os.remove(name_collect_obsang)
            os.remove(name_reprojected_obsang)             
        except:
            print("Was not able to download the file")

    return True