def activation_view(request): #redirect_url = request.GET.get('next') # Form handling logic #[...] print 'reached' #if redirect_url is not None: # activation_form.helper.form_action = reverse('submit_calculation') + '?next=' + redirectUrl if request.method == 'POST': print 'posting' activation_form = ActivationForm(request.POST) if activation_form.is_valid(): cleaned_data=activation_form.cleaned_data print 'valid' #activation.load_table() fast_ratio=50 Cd_ratio=70 fluence=float(cleaned_data['flux']) mass=cleaned_data['mass'] chemical_formula=cleaned_data['chemical_formula'] exposure=float(cleaned_data['time_on']) rest_times=[0,1,24,360,float(cleaned_data['time_off'])] env = activation.ActivationEnvironment(fluence=fluence,Cd_ratio=Cd_ratio, fast_ratio=fast_ratio,location="BT-2") sample = activation.Sample(chemical_formula, mass) sample.calculate_activation(env, exposure=exposure, rest_times=rest_times) sample.show_table() total = [0]*len(sample.rest_times) rows = [] for el,activity_el in activation._sorted_activity(sample.activity.items()): total = [t+a for t,a in zip(total,activity_el)] rows.append([el.isotope,el.daughter,el.reaction,el.Thalf_str]+activity_el) result = { 'success': True, 'sample': chemical_formula, 'mass': mass, 'flux': fluence, 'fast': fast_ratio, 'Cd': Cd_ratio, 'exposure': exposure, 'rest': rest_times, 'activity': rows, 'total': total, } return HttpResponse(simplejson.dumps(result)) #return HttpResponseRedirect("/results/") else: activation_form=ActivationForm() return render_to_response("activation.html", {'activation_form': activation_form}, context_instance=RequestContext(request))
def cgi_call(): form = cgi.FieldStorage() #print >>sys.stderr, form #print >>sys.stderr, "sample",form.getfirst('sample') # Parse inputs errors = {}; try: chem = formula(form.getfirst('sample')) except: errors['sample'] = error() try: fluence = float(form.getfirst('flux',100000)) except: errors['flux'] = error() try: fast_ratio = float(form.getfirst('fast','0')) except: errors['fast'] = error() try: Cd_ratio = float(form.getfirst('Cd','0')) except: errors['Cd'] = error() try: exposure = parse_hours(form.getfirst('exposure','1')) except: errors['exposure'] = error() try: mass = float(form.getfirst('mass','1')) except: errors['mass'] = error() try: density = parse_density(form.getfirst('density','0')) except: errors['density'] = error() try: #print >>sys.stderr,form.getlist('rest[]') rest_times = [parse_hours(v) for v in form.getlist('rest[]')] if not rest_times: rest_times = [0,1,24,360] except: errors['rest'] = error() try: decay_level = float(form.getfirst('activity','0.001')) except: errors['activity'] = error() try: thickness = float(form.getfirst('thickness', '1')) except: errors['thickness'] = error() try: wavelength_str = form.getfirst('wavelength','1').strip() if wavelength_str.endswith('meV'): wavelength = nsf.neutron_wavelength(float(wavelength_str[:-3])) elif wavelength_str.endswith('m/s'): wavelength = nsf.neutron_wavelength_from_velocity(float(wavelength_str[:-3])) elif wavelength_str.endswith('Ang'): wavelength = float(wavelength_str[:-3]) else: wavelength = float(wavelength_str) #print >>sys.stderr,wavelength_str except: errors['wavelength'] = error() try: xray_source = form.getfirst('xray','Cu') try: xray_wavelength = elements.symbol(xray_source).K_alpha except ValueError: xray_wavelength = float(xray_source) except: errors['xray'] = error() try: abundance_source = form.getfirst('abundance','NIST') if abundance_source == "NIST": abundance = activation.NIST2001_isotopic_abundance elif abundance_source == "IAEA": abundance = activation.IAEA1987_isotopic_abundance else: raise ValueError("abundance should be NIST or IAEA") except: errors['abundance'] = error() if errors: return {'success':False, 'error':'invalid request', 'detail':errors} # Fill in defaults if density == 0: # default to a density of 1 if chem.density is None: chem.density = 1 elif type(density) is dict: chem.density = chem.molecular_mass/density['volume'] else: # if density is given, assume it is for natural abundance chem.natural_density = density result = {'success': True} result['sample'] = { 'formula': str(chem), 'mass': mass, 'density': chem.density, 'thickness': thickness, 'natural_density': chem.natural_density, } # Run calculations try: env = activation.ActivationEnvironment(fluence=fluence,fast_ratio=fast_ratio, Cd_ratio=Cd_ratio) sample = activation.Sample(chem, mass=mass) sample.calculate_activation(env,exposure=exposure,rest_times=rest_times,abundance=abundance) decay_time = sample.decay_time(decay_level) total = [0]*len(sample.rest_times) rows = [] for el,activity_el in activation._sorted_activity(sample.activity.items()): total = [t+a for t,a in zip(total,activity_el)] rows.append([el.isotope,el.reaction,el.daughter,el.Thalf_str]+activity_el) result['activation'] = { 'flux': fluence, 'fast': fast_ratio, 'Cd': Cd_ratio, 'exposure': exposure, 'rest': rest_times, 'activity': rows, 'total': total, 'decay_level': decay_level, 'decay_time': decay_time, } except: result['activation'] = {"error": error()} #nsf_sears.replace_neutron_data() try: sld,xs,penetration = neutron_scattering(chem, wavelength=wavelength) result['scattering'] = { 'neutron': { 'wavelength': wavelength, 'energy': nsf.neutron_energy(wavelength), 'velocity': nsf.VELOCITY_FACTOR/wavelength, }, 'xs': {'coh': xs[0], 'abs': xs[1], 'incoh': xs[2]}, 'sld': {'real': sld[0], 'imag': sld[1], 'incoh': sld[2]}, 'penetration': penetration, 'transmission': 100*exp(-thickness/penetration), } except: missing = [str(el) for el in chem.atoms if not el.neutron.has_sld()] if any(missing): msg = "missing neutron cross sections for "+", ".join(missing) else: msg = error() result['scattering'] = {'error': msg } try: xsld = xray_sld(chem, wavelength=wavelength) result['xray_scattering'] = { 'wavelength': xray_wavelength, 'sld': {'real': xsld[0], 'imag': xsld[1]}, }, except: result['xrayscattering'] = {'error': error()} return result