<?xml version="1.0" encoding="ISO-8859-1"?>

<menu name="TOPAS_Durham_Local">
		

   <menu name="Simple ed Rietveld">
		<item name="Select Data File" type="macro">
			TAnewINP(v)
			{
				String w_ext = jEdit.getProperty("ta.last_xdd_type");
				if (w_ext == null) w_ext = "*.raw";
				jEdit.setProperty("vfs.browser.last-filter", w_ext);
			
				String w_path = jEdit.getProperty("ta.last_new_inp_dir");
				if (w_path == null) {
					w_path = jEdit.getProperty("ta.main.dir") +"test_examples\\";
				}
				jEdit.setProperty("vfs.browser.last-path", w_path);
			
				VFSFileChooserDialog chooser = new VFSFileChooserDialog(v, w_path, VFSBrowser.OPEN_DIALOG, true);
				String[] files = chooser.getSelectedFiles();
				if (files != null) {
					String w_dir = MiscUtilities.getParentOfPath(files[0]);
					String w_fn_no_ext = MiscUtilities.getFileNameNoExtension(files[0]);
					jEdit.setProperty("ta.last_new_inp_dir", w_dir);
					
					String w_path_inp = w_dir + w_fn_no_ext + "_riet_01.inp";
					b = jEdit.openFile(v, w_path_inp);
					if (b != null) {
						if (b.isNewFile()) {
							t = v.getTextArea();
							int i = 0;
							while(true) {
								t.goToEndOfLine(false);
								String w_path = MiscUtilities.getFileName(files[i]);
								if (w_path.indexOf(' ') != -1) {
									w_path = "\"" + w_path + "\"";
								}
								b.insert(t.getCaretPosition(), "\n'--------------------------------------------------------------"); 
								b.insert(t.getCaretPosition(), "\n'Input dile for simple 16.4 energy dispersive Rietveld refinement"); 
								b.insert(t.getCaretPosition(), "\n'Use save/set current button then run with F6 in topas"); 
								b.insert(t.getCaretPosition(), "\n'Replace $ and # symbols with text/numbers as needed"); 
								b.insert(t.getCaretPosition(), "\n'Gaussian peaks may help your background, try before str section:"); 
								b.insert(t.getCaretPosition(), "\n'xo_Is xo @ 50 min = 30; max = 80; peak_type pv pv_lor !lor 0 pv_fwhm @  10 min 5 max 30 I @  3000"); 
								b.insert(t.getCaretPosition(), "\n'Scale factor off by default"); 
								b.insert(t.getCaretPosition(), "\n'This is very much caveat emptor!"); 
								b.insert(t.getCaretPosition(), "\n'--------------------------------------------------------------"); 
								b.insert(t.getCaretPosition(), "\n\nr_wp 0 r_exp 0 r_p 0 r_wp_dash 0 r_p_dash 0 r_exp_dash 0 weighted_Durbin_Watson 0 gof 0"); 
								b.insert(t.getCaretPosition(), "\n\n'--------------------------------------------------------------"); 
								b.insert(t.getCaretPosition(), "\n'General information about refinement here\n'Remove comments as required"); 
								b.insert(t.getCaretPosition(), "\n'--------------------------------------------------------------"); 
								b.insert(t.getCaretPosition(), "\n\niters 100000"); 
								b.insert(t.getCaretPosition(), "\nchi2_convergence_criteria 0.001"); 
								b.insert(t.getCaretPosition(), "\n'do_errors"); 
								b.insert(t.getCaretPosition(), "\n\n'--------------------------------------------------------------"); 
								b.insert(t.getCaretPosition(), "\n'Information on datafile etc here"); 
								b.insert(t.getCaretPosition(), "\n'--------------------------------------------------------------"); 
								b.insert(t.getCaretPosition(), "\nxdd " + w_path); 
								b.insert(t.getCaretPosition(), "\n\tx_calculation_step = Yobs_dx_at(Xo); convolution_step 4");
								b.insert(t.getCaretPosition(), "\n\tbkg @ 0 0 0 "); 
			
								i++;
								if (i == files.length) break;
							}
							jEdit.setProperty("ta.last_xdd_type", "*" + MiscUtilities.getFileExtension(files[0]));
						} else {
							String wmes = "INP file " + w_path_inp + " already exists";
							Macros.message(v, wmes);
						}
					}
				}
			}
			TAnewINP(view);
		</item>

		<item name="Start_X" type="xinsert_script">{%starte "Enter start energy:"}{@	
			textArea.goToEndOfLine(false)
			}\n\tprm !start_val   {$$starte}
   start_X = start_val;
         </item>      
		<item name="End_X" type="xinsert_script">{%ende "Enter end energy:"}{@	
			textArea.goToEndOfLine(false)
			}\n\tprm !finish_val   {$$ende}
   finish_X = finish_val;
         </item>      
      
		<item name="Set Detector Angle" type="xinsert_script">{%detang "Enter Detector Angle (2th) in degrees:"}{@	
			textArea.goToEndOfLine(false)
			}\n\tprm !detector_angle_in_radians = {$$detang} Deg_on_2; 
   prm wavelength = 2 D_spacing Sin(detector_angle_in_radians);
   prm energy_in_eV = 10^5 /  (8.065541 wavelength);
   NoThDependence(0.001)
   d_spacing_to_energy_in_eV_for_f1_f11 = energy_in_eV;
   pk_xo = 10^-3 energy_in_eV + zero;
'lognormal fit a exp[-0.5(ln(x/xo)/b)^2]
   prm !sa  1.0000 min 0.00001 max 5.0
   prm  sb  0.2000 min 0.00001 max 5.0  'describes width, typically around 0.2 on 16.4
   prm  s0  60.000 min 1.00000 max 100  'Start this at middle of intensity range
   scale_pks = sa*Exp(-0.5*(Ln(Xo/s0)/sb)^2); 'seems to do well
         \n\tprm !zero 0
         </item>	
         
				<item name="Read a CIF file" type="macro">
			do_cif(v) {
				t = v.getTextArea();
				b = t.getBuffer();

				t.goToEndOfLine(false);					
				b.insert(t.getCaretPosition(), "\n\n'--------------------------------------------------------------"); 
				b.insert(t.getCaretPosition(), "\n'Information on structure");
				b.insert(t.getCaretPosition(), "\n'Type in phase/space group/cell etc");
				b.insert(t.getCaretPosition(), "\n'Comment in/out sections as needed");
				b.insert(t.getCaretPosition(), "\n'--------------------------------------------------------------"); 

				jEdit.setProperty("vfs.browser.last-filter", "*.cif");
				w_path = jEdit.getProperty("ta.last_cif_path");
				if (w_path == null) {
					w_path = jEdit.getProperty("ta.main.dir") +"cif\\";
				}
				jEdit.setProperty("vfs.browser.last-path", w_path);
				waitForConsole(v);								
				VFSFileChooserDialog chooser = new VFSFileChooserDialog(v, w_path, VFSBrowser.OPEN_DIALOG, true);
				String[] files = chooser.getSelectedFiles();
				if (files != null) {					
					int i = 0;
					b.beginCompoundEdit();
					while(true) {
						runInSystemShell(v, jEdit.getProperty("ta.main.dir"));			
						waitForConsole(v);								
						runInSystemShell(v, "cif1 " + "\"" + files[i] + "\" cif1.tmp");
						waitForConsole(v);								
						t.goToEndOfLine(false);					
						b.insert(t.getCaretPosition(), "\n");
						b.insertFile(v, jEdit.getProperty("ta.main.dir") + "cif1.tmp");
						VFSManager.waitForRequests();
						i++;
						if (i == files.length) break;
						b.insert(t.getCaretPosition(), "\n");
					}
					b.endCompoundEdit();
					jEdit.setProperty("ta.last_cif_path", files[0]);
				}
				t.goToEndOfLine(false);
				b.insert(t.getCaretPosition(), "\n\t\tscale @  0.0001"); 
				b.insert(t.getCaretPosition(), "\n\t\tr_bragg 0"); 
				b.insert(t.getCaretPosition(), "\n\t\tprm fwhm  0.5 min 0.0001 max 1"); 
				b.insert(t.getCaretPosition(), "\n\t\tprm !slope  0.00 min -1 max 1"); 
				b.insert(t.getCaretPosition(), "\n\t\tprm fwhm_tot = fwhm + slope * D_spacing;"); 
				b.insert(t.getCaretPosition(), "\n\t\tpeak_type pv pv_lor lor1_0000  0.647614497` pv_fwhm  = fwhm_tot;"); 
			}
			do_cif(view);
			</item>


		<item name="view structure" type="macro">
			textArea.goToEndOfLine(false);
			buffer.insert(textArea.getCaretPosition(), "\n\t\tview_structure"); 
		</item>
		

 </menu>   
 

   <menu name="Miscellaneous Local Commands">
		<item name="Cu Kb and W La contamination" type="macro">
			textArea.goToEndOfLine(false);
			buffer.insert(textArea.getCaretPosition(), "\n\tla @ 0.0010 lo 1.39222   lh 0.5      'CU KB"); 
			buffer.insert(textArea.getCaretPosition(), "\n\tla @ 0.0010 lo 1.47639   lh 0.5      'W LA"); 
		</item>
		<item name="CSIRO str database" type="macro">
			Macros.getMacro("TAInsertSTR").invoke(view);
		</item>
      <item name="CSIRO Inel Detector" type="xinsert_script">{%alphaval "Enter Incident Beam Angle in degrees:"}{@	
			textArea.goToEndOfLine(false)
			}\n\tFIXEDTHETA(alpha, {$$alphaval})\n\tSDINEL(!delta,0,!alpha,{$$alphaval})
      </item>
         </menu>	
	</menu>


	
