def process_combined_years_and_data(monthly_dates_and_data, month_list, calculation, start_year, end_year, start_month, end_month, model_name):
    date_list = [i[0] for i in monthly_dates_and_data]
    data = [i[1] for i in monthly_dates_and_data]
    #print date_list

    # Process for results
    results = process_gcm_data(custom_span=len(month_list), sample_method=calculation, date_list=date_list, data=data, start_year=start_year, end_year=end_year, start_month=month_list[0]+1, end_month=month_list[-1]+1)

    # Drop nans
    results = results.dropna()
    #print results, len(results), "results",
    #print "Average for all specified years: ", np.mean(results)
    #print results
    return model_name, np.mean(results)
Exemple #2
0
def process_data(sw_lat, sw_lon, ne_lon, ne_lat, month_list, start_year, end_year, end_month, start_month, variable, data_path, calculation, model_name):

	# File handles
	pathname = data_path
	filehandle=Dataset(pathname,'r',format="NETCDF4")
	#print filehandle.variables
	try:
	    lathandle = filehandle.variables['lat']
	except:
	    lathandle = filehandle.variables['Latitude']
	try:
	    lonhandle=filehandle.variables['lon']
	except:
	    lonhandle=filehandle.variables['Longitude']
	try:
	    timehandle=filehandle.variables['time']
	except:
	    try:
	        timehandle=filehandle.variables['Time']
	    except:
	        timehandle=filehandle.variables['day']

	# NetCDF variable 
	datahandle=filehandle.variables[variable]
	time_num=len(timehandle)
	timeindex=range(time_num)  #python starts arrays at 0
	time=timehandle[timeindex]
	lat_array = lathandle[:]
	lon_array = lonhandle[:]

	positive_east_longitude = "True"

	# Lons from 0-360 converted to -180 to 180
	if positive_east_longitude == "True":
		lon_array = [x - 360 for x in lon_array[:]] 

	# Bounding box indices
	sw_lat = index_from_numpy_array(np.array(lat_array), sw_lat)
	#print SWLat, lat_array[SWLat]

	sw_lon = index_from_numpy_array(np.array(lon_array), sw_lon)
	#print SWLong, lon_array[SWLong]

	ne_lat = index_from_numpy_array(np.array(lat_array), ne_lat)
	#print NELat, lat_array[NELat]

	ne_lon = index_from_numpy_array(np.array(lon_array), ne_lon)
	#print NELong, lon_array[NELong]


	closestLat = slice(sw_lat, ne_lat)
	closestLon = slice(sw_lon, ne_lon)

	# Which months to get data for
	#month_list = [11,0,1]

	def get_monthly_dates_and_data(month):
		""" Function that get dates and data for a single month from a NetCDF File"""
		# Month to get data for
		request_month = month

		# Dimensions
		variable_dimensions = datahandle.dimensions

		# Dictionary to map dimension with index value
		variable_index_dictionary = {}

		# Slice out month data for all years
		timeindex = slice(request_month,len(timehandle),12)

		#print timeindex
		for var in variable_dimensions:
		    if var == "time" or var == "Time" or var == "day":
		        variable_index_dictionary[var] = timeindex
		    if var == "lat" or var == "Latitude":
		        variable_index_dictionary[var] = closestLat
		    if var == "lon" or var == "Longitude":
		        variable_index_dictionary[var] = closestLon

		# Dictionary to map the order of the dimensions
		variable_dimensions_dictionary = {}
		variable_dimensions_dictionary[0] = datahandle.dimensions[0]
		variable_dimensions_dictionary[1] = datahandle.dimensions[1]
		variable_dimensions_dictionary[2] = datahandle.dimensions[2]
		#print variable_dimensions_dictionary

		# Get the dates using calculation for now leap years
		dates = [get_date_no_leap_year(x) for x in timehandle[timeindex]]
		#print dates, len(dates)

		# Get the data
		data = datahandle[variable_index_dictionary[variable_dimensions_dictionary[0]], variable_index_dictionary[variable_dimensions_dictionary[1]], variable_index_dictionary[variable_dimensions_dictionary[2]]]
		#print  "Temporal resolution from NetCDF= ", len(data[:])

		# Average or sum over spatial subset region
		data_spatial_analysis = spatial_subset(data, method="mean")
		#print "Spatial subset temporal resolution = ", len(data_spatial_analysis)

		dataset = []
		for i in range(len(dates)):
			dataset.append([dates[i], data_spatial_analysis[i]])
		return dataset

	# Get and sort monthly dates and data
	monthly_dates_and_data = []
	for x in month_list:
		#print x
		monthly_dates_and_data += get_monthly_dates_and_data(x)
	monthly_dates_and_data.sort()

	#print monthly_dates_and_data

	date_list = [i[0] for i in monthly_dates_and_data]
	data = [i[1] for i in monthly_dates_and_data]
	#print date_list

	# Process for results
	results = process_gcm_data(custom_span=len(month_list), sample_method=calculation, date_list=date_list, data=data, start_year=start_year, end_year=end_year, start_month=month_list[0]+1, end_month=month_list[-1]+1)

	# Drop nans
	results = results.dropna()
	#print results, len(results), "results",
	#print "Average for all specified years: ", np.mean(results)
	return model_name, np.mean(results)