#NEWLINE# def download( dbname , dts , bbox ) : #NEWLINE# #INDENT# 'Downloads the PRISM data products for a set of\n    dates *dt* and imports them into the PostGIS database *dbname*.' #NEWLINE# url = 'jsimpson.pps.eosdis.nasa.gov' #NEWLINE# ftp = FTP ( url ) #NEWLINE# ftp . login ( 'kandread@jpl.nasa.gov' , 'kandread@jpl.nasa.gov' ) #NEWLINE# ftp . cwd ( 'data/imerg/gis' ) #NEWLINE# outpath = tempfile . mkdtemp ( ) #NEWLINE# for dt in [ ( dts [ 0 ] + timedelta ( t ) ) for t in range ( ( ( dts [ 1 ] - dts [ 0 ] ) . days + 1 ) ) ] : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ftp . cwd ( '/data/imerg/gis/{0}/{1:02d}' . format ( dt . year , dt . month ) ) #NEWLINE# filenames = [ f for f in ftp . nlst ( ) if ( re . match ( '3B.*{0}.*S000000.*1day\\.tif.*' . format ( dt . strftime ( '%Y%m%d' ) ) , f ) is not None ) ] #NEWLINE# if ( len ( filenames ) > 0 ) : #NEWLINE# #INDENT# fname = filenames [ 0 ] #NEWLINE# with open ( '{0}/{1}' . format ( outpath , fname ) , 'wb' ) as f : #NEWLINE# #INDENT# ftp . retrbinary ( 'RETR {0}' . format ( fname ) , f . write ) #NEWLINE# #UNINDENT# with open ( '{0}/{1}' . format ( outpath , fname . replace ( 'tif' , 'tfw' ) ) , 'wb' ) as f : #NEWLINE# #INDENT# ftp . retrbinary ( 'RETR {0}' . format ( fname . replace ( 'tif' , 'tfw' ) ) , f . write ) #NEWLINE# #UNINDENT# tfname = fname . replace ( 'tif' , 'tfw' ) #NEWLINE# fname = datasets . uncompress ( fname , outpath ) #NEWLINE# datasets . uncompress ( tfname , outpath ) #NEWLINE# subprocess . call ( [ 'gdalwarp' , '-t_srs' , 'epsg:4326' , '{0}/{1}' . format ( outpath , fname ) , '{0}/prec.tif' . format ( outpath ) ] ) #NEWLINE# if ( bbox is not None ) : #NEWLINE# #INDENT# subprocess . call ( [ 'gdal_translate' , '-a_srs' , 'epsg:4326' , '-projwin' , '{0}' . format ( bbox [ 0 ] ) , '{0}' . format ( bbox [ 3 ] ) , '{0}' . format ( bbox [ 2 ] ) , '{0}' . format ( bbox [ 1 ] ) , '{0}/prec.tif' . format ( outpath ) , '{0}/prec1.tif' . format ( outpath ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# subprocess . call ( [ 'gdal_translate' , '-a_srs' , 'epsg:4326' , '{0}/prec.tif' . format ( outpath ) , '{0}/prec1.tif' . format ( outpath ) ] ) #NEWLINE# #UNINDENT# cmd = ' ' . join ( [ 'gdal_calc.py' , '-A' , '{0}/prec1.tif' . format ( outpath ) , '--outfile={0}/prec2.tif' . format ( outpath ) , '--calc="0.1*A"' ] ) #NEWLINE# subprocess . call ( cmd , shell = True ) #NEWLINE# dbio . ingest ( dbname , '{0}/prec2.tif' . format ( outpath ) , dt , table , False ) #NEWLINE# #UNINDENT# #UNINDENT# except : #NEWLINE# #INDENT# print ( 'WARNING! No data were available to import into {0} for {1}.' . format ( table , dt . strftime ( '%Y-%m-%d' ) ) )