fmt = raster_example.format print 'Pixel type: ' + px_type print 'Spatial Reference: ' + sref.name print 'Raster format: ' + fmt # Sort the rasters so we can use 'em ## Loop through points in 'sites' shapefile ## with arcpy.da.SearchCursor(out_shp, ['BasinID', 'date_y1', 'date_y2', 'id']) as cur: i = 0 for row in cur: try: # See if the site has 2 valid dates sdate, edate = mcmtools.dates_from_site(row[1], row[2]) basin_id = int(row[0]) site_id = row[3] except: # No dates? No bueno. Go to next site. continue print 'Processing site ' + site_id # Let's crunch the numbers # Return little dict of site's results dct_site, fields = calc_precip(sdate, edate, basin_id, site_id) dct_results.update(dct_site) # Append calculation results to big dict i += 1 if i == 3: pass # in case you wanna check on some stuff
## Loop through points in 'sites' shapefile ## with open(csv_path, 'w+b') as f: writer = csv.writer(f) writer.writerow(['id', 'runoff', 'baseflow', 'soil_moisture', 'lai']) with open(csv_path, 'a') as f: writer = csv.writer(f) with arcpy.da.SearchCursor( out_shp, ['id', 'date_y1', 'date_y2', 'SHAPE@XY']) as cur: i = 0 for row in cur: site_id = row[0] try: # See if the site has 2 valid dates sdate, edate = mcmtools.dates_from_site( row[1], row[2] ) # one month before to 12 months after starting date except: print "No date or dates not valid" writer.writerow([site_id, '[]', '[]', '[]', '[]']) continue print 'Processing site ' + site_id # Let's crunch the numbers print sdate print edate runoff_timeseries = sample_NLDAS_timeseries( sdate, edate, row[3], 12) # kg/m2 baseflow_timeseries = sample_NLDAS_timeseries( sdate, edate, row[3], 13) # kg/m2 soil_moisture_timeseries = sample_NLDAS_timeseries(
sites_shp = ogr.Open(sites_shp_path) sites_lyr = sites_shp.GetLayer() sites_featList = range(sites_lyr.GetFeatureCount()) for sFID in sites_featList: site = sites_lyr.GetFeature(sFID) basinid = int(site.GetField('BasinID')) site_id = site.GetField('id') d1 = site.GetField('date_y1') d2 = site.GetField('date_y2') if d1 == 'NaT': continue sdate, edate = mcmtools.dates_from_site( d1, d2) # one month before to 12 months after starting date # find feature from Basin shapefile for bFID in basins_featList: basin = basins_lyr.GetFeature(bFID) thisid = int(basin.GetField('BasinID')) if thisid is basinid: break print basinid [runoff_timeseries, dates] = zonal_stats_NLDAS_timeseries(sdate, edate, basin, 12) # kg/m2 break ## [baseflow_timeseries, dates] = sample_NLDAS_timeseries(sdate,edate,basin,13) # kg/m2 ## [soil_moisture_timeseries, dates] = sample_NLDAS_timeseries(sdate,edate,basin,25) # kg/m2 ## [lai_timeseries, dates] = sample_NLDAS_timeseries(sdate,edate,basin,50) # 0-9
dct_site = headers.fromkeys(headers, 'NaN') # Numpy object array to hold results. First row is headers results = numpy.zeros((nrows + 1, ncols), dtype=numpy.object) results[0, :] = numpy.array([ 'id', 'BasinID', 'lat', 'P_monthly', 'Tmean_monthly', 'Tmax_monthly', 'Tmin_monthly', 'PET_monthly', 'R_monthly', 'f_monthly', 'alpha_monthly' ], dtype=numpy.object) for i, row in enumerate(cur, start=1): # Site must have 5 attributes, otherwise something won't work try: sdate, sdate12 = mcmtools.dates_from_site( row[2]) # to 12 months after starting date edate, edate12 = mcmtools.dates_from_site( row[5]) # edate is exclusive basin_id = int(row[0]) site_id = row[1] lat = row[3] lon = row[4] except: continue print '\n---------------------' print ' Processing ' + site_id print '-----------------------' # Return a numpy vector of the site results
## with open(csv_path, 'w+b') as f: writer = csv.writer(f) writer.writerow([ 'id', 'BasinID', 'dates', 'runoff', 'baseflow', 'soil_moisture', 'lai' ]) with open(csv_path, 'a') as f: writer = csv.writer(f) with arcpy.da.SearchCursor( in_shp, ['id', 'date_y1', 'date_y2', 'date_y3', 'BasinID']) as cur: i = 0 for row in cur: site_id = row[0] try: # See if the site has 2 valid dates sdate, sdate12 = mcmtools.dates_from_site( row[1]) # to 12 months after starting date if row[3] == 'NaT': sdate = datetime.date(2016, 5, 1) edate = datetime.date(2016, 6, 1) else: continue edate, edate12 = mcmtools.dates_from_site(row[3]) except: print "No date or dates not valid" writer.writerow([site_id, int(row[4]), '[]', '[]', '[]', '[]']) continue print 'Processing site ' + site_id # Let's crunch the numbers print sdate print edate