Sie sind auf Seite 1von 2

#-------------------------------------------------#Script: 1_DownloadTiles.

py
#Author: Stefanie Bohms, SGT Inc. (USGS/EROS)
#Use: Download Daymet tiles (.nc files) from http://daymet.ornl.gov/thredds serv
er by weather parameter
#Note: need wget installed on system to be able to retrieve files (http://gnuwin
32.sourceforge.net/packages/wget.htm)
#import modules
import sys, os, traceback, datetime, time, string, glob, urllib2, arcpy
try:
#Output directory for data downloaded - enter path when prompted by script
filedir = raw_input('Enter Output path (e.g. D:\daymet): ')
#change to ouput directory
os.chdir(filedir)
#year for which data will be downloaded - enter year when prompted by script
year = raw_input('Enter year to be downloaded(yyyy): ') # 2008
# enter txt file with tile IDs, see tiles.txt for example - enter path when
prompted by script
tiletxtdir = raw_input('Enter path and tile list (e.g. D:\daymet\tiles.txt):
')
#access txt file
tiles = open(tiletxtdir, 'r')
print year
#create a folder with the year in output folder
yeardir = filedir + os.sep + year
if not os.path.exists(yeardir):
os.mkdir(yeardir)
#enter weather parameter e.g. tmin - enter parameter when prompted by scrip
t
type = raw_input('Enter type: ')
# loop through txt file and download the tiles for the tiles ID given
s = 1
for tile in tiles:
print tile
intile = tile[0:5]
tiledir = yeardir + os.sep + str(intile)
if not os.path.exists(tiledir):
os.mkdir(tiledir)
os.chdir(tiledir)
netdcffile = 'http://daymet.ornl.gov/thredds/fileServer/allcf/'+ year +
'/' + str(intile) +'_' + year + '/' + type + '.nc'
response = urllib2.urlopen(netdcffile)
binContents = response.read()
response.close()
outf = open(tiledir + "\\" + type + '.nc' , "wb")
outf.write(binContents)
outf.close()
#loops through txt file and creates daily rasters for each layer in the .nc
file
print "Start converting .nc file bands to tif for tile ID " +tile
intile = tile[0:5]
tiledir = yeardir + os.sep + str(intile)
if not os.path.exists(tiledir):
os.mkdir(tiledir)
#define input .nc file
netfile = tiledir + os.sep + type + '.nc'
print netfile

RasterLayer = tiledir + os.sep + str(type) +'_layer' + str(s)


print RasterLayer
#define output netcdf raster name
Output_Raster = tiledir + os.sep + 'netcdf_raster.tif'
# Execute MakeNetCDFRasterLayer
arcpy.MakeNetCDFRasterLayer_md(netfile, type, 'x', 'y', RasterLayer, '',
'', '')
resultValue = 366
count = 0
#create a folder with the parameter type entered
out1 = tiledir + os.sep + type
if not os.path.exists(out1):
os.mkdir(out1)
#Loop through the bands in the netcdf file and copy bands as a seperate
TIF file. Exporting individual bands from Output_Raster.
while count <= int(resultValue):
print "created tif file for band at index (day) " + str(count)
# Execute SelectByDimension tool
valueSelect = ["time", count]
net_layer = arcpy.SelectByDimension_md(RasterLayer, [valueSelect], "
BY_INDEX")
#define output tif name
output = out1 + os.sep + type +'_' + str(count) +".tif"
arcpy.CopyRaster_management(net_layer, output)
arcpy.AddMessage(output + " " "exported" + " " + "successfully")
count += 1
del count
del resultValue
s += 1
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n
+ str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
arcpy.AddError(pymsg)
print pymsg

"

Das könnte Ihnen auch gefallen