def fit_to_weibull(sim_file,beta_0,tau_0,event_range=None,magnitude_filter=None,section_filter=None): from scipy import optimize from pyvc import VCEvents,VCSimData fitfunc = lambda p,x: weibull_one(x,p[0],p[1]) errfunc = lambda p,x,y: fitfunc(p,x) - y p0 = [beta_0,tau_0] with VCSimData() as sim_data: # open the simulation data file sim_data.open_file(sim_file) # instantiate the vc classes passing in an instance of the VCSimData # class events = VCEvents(sim_data) event_data = events.get_event_data(['event_number', 'event_year', 'event_magnitude', 'event_range_duration'], event_range=event_range, magnitude_filter=magnitude_filter, section_filter=section_filter) intervals = np.array([ x - event_data['event_year'][n-1] for n,x in enumerate(event_data['event_year']) if n != 0 ]) cumulative = {} cumulative['x'] = np.sort(intervals) cumulative['y'] = np.arange(float(intervals.size))/float(intervals.size) p1,success = optimize.leastsq(errfunc,p0[:],args=(cumulative['x'],cumulative['y'])) print "\nBETA: {}".format(p1[0]) print "TAU : {}\n".format(p1[1])
def plot_number_area_data(sim_file, output_file=None, event_range=None): from pyvc import * from pyvc import vcplotutils #Can't handle plot label if no event_range given with VCSimData() as sim_data: # open the simulation data file sim_data.open_file(sim_file) # instantiate the vc events class passing in an instance of the # VCSimData class events = VCEvents(sim_data) # get the data event_data = events.get_event_data(['event_number','event_area'], event_range=event_range) start,end = event_range['filter'] duration = round(end-start) #--------------------------------------------------------------------------- # Prepare the plot and do it. #--------------------------------------------------------------------------- # initilize a dict to store the event counts and get the total number # of events. cum_num = {} total_events = len(event_data['event_number']) for num in range(total_events): area = float(sorted(event_data['event_area'])[num])*float(pow(10,-6)) cum_num[area] = total_events - (num + 1) sys.stdout.write("\nnumber of events : {}\n".format(total_events)) # dump the counts into x and y arrays for plotting. also, divide the count # by the number of years so we get count per year. x = [] y = [] for area in sorted(iter(cum_num)): x.append(float(area)) y.append(float(cum_num[area])) # create the line for b = 1 x_b1 = np.linspace(min(x),max(x),num=1000) y_b1 = y[0]*x_b1[0]*(np.array(x_b1)**-1) #sys.stdout.write(str(y_b1)) plt.title('Duration: {} years Total events: {}'.format(duration,total_events)) plt.plot(x_b1,y_b1,label='b=1',ls='-',lw=2,c='r') plt.plot(x,y,'.',c='k') plt.legend(loc='upper right') plt.ylabel(r'$N (\geq A_r)$') plt.xlabel(r'$A_r [km^2]$') plt.xlim(-500,5500) plt.ylim(-20,750) plt.savefig(output_file,dpi=200) # do the standard plot """vcplotutils.standard_plot(output_file, x, y,
def get_field(evnum,sim_file,field_type='gravity',mag=False,avg_slip=False, num_secs=False,num_elem=False): from pyvc import * from operator import itemgetter from pyvc import vcplots import sys output_directory = 'animation_test_g/' field_values_directory = '{}field_values/'.format(output_directory) padding = 0.01 cutoff = None with VCSimData() as sim_data: sim_data.open_file(sim_file) events = VCEvents(sim_data) geometry = VCGeometry(sim_data) min_lat = geometry.min_lat max_lat = geometry.max_lat min_lon = geometry.min_lon max_lon = geometry.max_lon base_lat = geometry.base_lat base_lon = geometry.base_lon event_data = events[evnum] event_element_slips = events.get_event_element_slips(evnum) event_sections = geometry.sections_with_elements(event_element_slips.keys()) if field_type=='gravity': EF = vcplots.VCGravityField(min_lat, max_lat, min_lon, max_lon, base_lat, base_lon, padding=padding) else: EF = vcplots.VCDisplacementField(min_lat, max_lat, min_lon, max_lon, base_lat, base_lon, padding=padding) field_values_loaded = EF.load_field_values('{}{}_'.format(field_values_directory, evnum)) if field_values_loaded: sys.stdout.write('loaded'.format(evnum)) # If they havent been saved then we need to calculate them elif not field_values_loaded: sys.stdout.write('processing '.format(evnum)) sys.stdout.flush() ele_getter = itemgetter(*event_element_slips.keys()) event_element_data = ele_getter(geometry) if len(event_element_slips) == 1: event_element_data = [event_element_data] sys.stdout.write('{} elements :: '.format(len(event_element_slips))) sys.stdout.flush() EF.calculate_field_values( event_element_data, event_element_slips, cutoff=cutoff, save_file_prefix='{}{}_'.format(field_values_directory, evnum) ) '{} elements in {} sections : '.format(len(event_element_slips), len(event_sections)) if mag or avg_slip or num_secs or num_elem: returning = [] returning.append(EF) if mag: returning.append(event_data[3]) if avg_slip: returning.append(event_data[13]) if num_secs: returning.append(len(event_element_slips)) if num_elem: returning.append(len(event_sections)) return returning else: return EF