---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
    jupytext_version: 1.11.5
kernelspec:
  display_name: Python 3
  language: python
  name: python3
---

# The tutorial 1st 

Demonstrates the basic operations involved in refinement.

## coding

> **1. Save your diffraction data to the root directory and rename the file to `intensity.csv`.**



```{code-cell}
# import PyXplore package
from PyXplore import WPEM
import pandas as pd
```

> **2. Parse your diffraction data (`2θ`, intensity) and perform background processing.**

```{code-cell}
intensity_csv = pd.read_csv(r'intensity.csv',header=None )
var = WPEM.BackgroundFit(intensity_csv,lowAngleRange=17,poly_n=13,bac_split=16,bac_num=300)
```
> **3. After running the code, a new folder named `ConvertedDocuments` will be created in the root directory. This folder contains the background information.**

> **Copy the two important files — `bac.csv` and `no_bac_intensity.csv` — from `ConvertedDocuments` into the root directory, as they are required for the next steps.**


> **4. After background subtraction, the next step is to parse the reference structure.**
>
> Save the reference `.cif` file in the root directory. For example, if the structure is Mn₂O₃, place a file named `Mn2O3.cif` in the root directory as the reference phase.
>
> If you are unsure of the reference phase, you must perform phase identification first. Please visit our website for assistance: [https://xqueryer.caobin.asia/](https://xqueryer.caobin.asia/)


```{code-cell}
latt, AtomCoordinates,des = WPEM.CIFpreprocess(filepath='Mn2O3.cif',two_theta_range=(15,75))
```



> **5. After running the code, a new folder named `output_xrd` will be created.**
>
> Inside this folder, locate the file named `xxxHKL.csv`, copy it to the root directory, and rename it to `peak0.csv`. This file will be used in the refinement step.


```{code-cell}
# The wavelength is set according to the actual light source
wavelength = [1.540593, 1.544414]
# The file name of non-background data (2theta-intensity data)
no_bac_intensity_file = "no_bac_intensity.csv" 
# The file name of raw/original data (2theta-intensity data)
original_file = "intensity.csv"  
# The file name of background data (2theta-intensity data)
bacground_file = "bac.csv"  


# Input the initial lattice constants {a, b, c, α, β, γ}, whose values need to be assumed at initialization.
Lattice_constants = [latt,]

# Execute the model

WPEM.XRDfit(
    wavelength, var, Lattice_constants,no_bac_intensity_file, original_file, bacground_file, 
    subset_number=11,low_bound=20,up_bound=70,bta = 0.85,iter_max = 5, asy_C = 0,InitializationEpoch=0, 
    )
```


> **6. The refinement results will be saved in the folder `WPEMFittingResults`.**
>
> For automated refinement and system integration, please refer to my repository: [Ares](https://github.com/Bin-Cao/Ares).
>



```{seealso}
Details on the parameters and output files can be found in the documentation booklet for **PyXplore**. For more information, see the introduction in the `PyWPEM` repository: [https://github.com/Bin-Cao/PyWPEM](https://github.com/Bin-Cao/PyWPEM). 
```