{ "metadata": { "name": "", "signature": "sha256:3f28a84fd60926eecfe4af2e2afed0c1d0b7203c32431e0b6e90253ea046975f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "pyModis notebook" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Download data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we have to import pymodis library and set all the needed variables" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "import glob\n", "from IPython.core.display import Image\n", "from pymodis import downmodis" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# destination foldert\n", "dest = \"/tmp\"\n", "# tiles to download\n", "tiles = \"h18v04,h19v04\"\n", "# starting day\n", "day = \"2014-08-14\"\n", "# number of day to download\n", "delta = 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point we can initialize the downmodis object. We are going to download MODIS LST product, in the \"/tmp\" directory" ] }, { "cell_type": "code", "collapsed": false, "input": [ "modisDown = downmodis.downModis(destinationFolder=dest, tiles=tiles, today=day, delta=delta)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "modisDown.connect()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point we start the download of files." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Warning
\n", "The next operation could take some minutes depending on your internet speed
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "modisDown.downloadsAllDay()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# create the list of files to use\n", "files = glob.glob(os.path.join(dest, 'MOD11A1.A2014*.hdf'))\n", "print files" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from topng import hdf2png\n", "pngfile = hdf2png(files[0])\n", "Image(filename=pngfile)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Parse the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We import the needed modules and after we obtain a list with the full path to the downloaded MODIS files" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from pymodis import parsemodis" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Parse single data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point we can parse one file (in this case the first one), creating a parseModis object" ] }, { "cell_type": "code", "collapsed": false, "input": [ "modisParse = parsemodis.parseModis(files[0])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you can obtain several parameters from the MODIS metadata, we query only some values to show you:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# bounding box of the tiles\n", "modisParse.retBoundary()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# quality statistics\n", "modisParse.retMeasure()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "modisParse.retRangeTime()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check parseModis class documentation for more info" ] }, { "cell_type": "code", "collapsed": false, "input": [ "parsemodis.parseModis?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Parse multiple data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After the test with only a MODIS file, we are going to test the parsing for multiple files" ] }, { "cell_type": "code", "collapsed": false, "input": [ "modisMultiParse = parsemodis.parseModisMulti(files)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you can obtain the value of boundary" ] }, { "cell_type": "code", "collapsed": false, "input": [ "modisMultiParse.valBound()\n", "modisMultiParse.boundary" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or write a xml for a MODIS mosaic file" ] }, { "cell_type": "code", "collapsed": false, "input": [ "modisMultiParse.writexml(os.path.join(dest, 'modismultiparse.xml'))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "at the end you can read the created file and print the lines" ] }, { "cell_type": "code", "collapsed": false, "input": [ "f = open(os.path.join(dest, 'modismultiparse.xml'))\n", "lines = f.readlines()\n", "p = [l.strip() for l in lines]\n", "f.close()\n", "print \"\\n\".join(p)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Create a mosaic file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to use the GDAL library to create the mosaic. As subset we are going to use daily and nightly temperature and it's quality layer" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from pymodis.convertmodis_gdal import createMosaicGDAL\n", "# [daily temp, quality for daily, not used, not used, nightly temp, quality for nightly]\n", "subset = [1,1,0,0,1,1]\n", "output_pref = os.path.join(dest, 'MOD11A1.A2014226.mosaic')\n", "output_tif = os.path.join(dest, 'MOD11A1.A2014226.mosaic.tif')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we'll initialize the mosaic object" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# the first parameter is a list with the original tiles,\n", "# the second one is a list with the the subset to process,\n", "# the last is the output format, in this case GeoTiff\n", "mosaic = createMosaicGDAL(files, subset, 'GTiff')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point we create the xml file with the information of input data, and a multilayr GeoTiff file containing the mosaic of the choosen band" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mosaic.run(output_tif)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally we can also create a GDAL VRT file, a XML file containing the information about input data. This is really powerfull if you want convert it in different format or projection system" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mosaic.write_vrt(output_pref)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Convert data" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Single HDF file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this first example we are going to convert an original MODIS HDF file using GDAL library" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from pymodis.convertmodis_gdal import convertModisGDAL\n", "output_pref = os.path.join(dest, 'MOD11A1.A2014226.h18v04')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to convert a single tile, with the 'subset' already used for mosaicking, the output resolution will be 1000 meters and the projection system will be EU-LAEA" ] }, { "cell_type": "code", "collapsed": false, "input": [ "convertsingle = convertModisGDAL(hdfname=files[0], prefix=output_pref, subset=subset, res=1000, epsg=3035)\n", "convertsingle.run()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "VRT mosaic file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We created separated VRT file, one for each choosen subset. So first we collect the name of VRT files" ] }, { "cell_type": "code", "collapsed": false, "input": [ "vrtfiles = glob.glob(os.path.join(dest, 'MOD11A1.A2014*.vrt'))\n", "print vrtfiles" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can convert all the VRT files in a *``for``* loop " ] }, { "cell_type": "code", "collapsed": false, "input": [ "for f in vrtfiles:\n", " base = os.path.basename(f).replace('.vrt', '_vrt')\n", " output = os.path.join(dest, base)\n", " convertsingle = convertModisGDAL(hdfname=f, prefix=output, subset= [1,1,1,1], res=1000, epsg=3035, vrt=True)\n", " convertsingle.run_vrt_separated()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It will create 4 GeoTIFF files ready to be processed." ] }, { "cell_type": "code", "collapsed": false, "input": [ "vrttiffiles = glob.glob(os.path.join(dest, 'MOD11A1.A2014*_vrt.tif'))\n", "print vrttiffiles" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from topng import tif2png\n", "pngfile = tif2png(vrttiffiles[1])\n", "Image(filename=pngfile)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "The tutorial is finished, you can find more info at pyModis website" ] } ], "metadata": {} } ] }