== Using the software
WARNING: This software requires Python 3.12 or later. +
The module *venv* and the package manager *pip* are also required.

=== Downloading the source code
IMPORTANT: Currently ({revdate}) the source code is hosted on a private Gitea instance, owned by {author}. +
If the site is down for maintenance or temporarily unavailable please contact the webmaster via mailto:{email}[e-mail].

// TO-DO: add link to direct download of package
The source code can be acquired directly via *git*, or downloaded from the official repository on link:{repo-url}[Gitea D'Amico^].

[source,bash,subs="verbatim,attributes"]
----
git clone {repo-url}.git {software-name}
cd {software-name} # enter directory
ls
  LICENSE    docs/     output/           src/
  README.md  glossary  requirements.txt  tests/
----

Optionally, you can access the code in the development branch by executing:
[source,bash]
----
git checkout dev
----

=== Preparing the environment
Before starting {software-name} {revnumber} requires a total of 6 modules to be installed, which are listed link:{repo-url}/src/branch/main/requirements.txt[here^]. Since installing a Python module system-wide is almost never a good idea, start by creating and activating a virtual environment.

In the software folder, run:

[source,bash]
----
# Calls venv module to create new Python virtual environment in .venv:
python3 -m venv .venv
# If command is successful, running ls should show a new .venv folder:
ls -d .*
  .venv
# Activate venv:
source .venv/bin/activate
----

.Most shells like Bash show very clearly when you're working inside a virtual environment.
[#usage-venv]
image::usage-venv.png[]

At this point you're free to install the requirements through *pip*:
[source,bash]
----
# Install from list in requirements.txt:
pip install -r requirements.txt
----

Most of the warnings displayed by pip are safe and generally it's not dangerous to ignore them. +
Unless pip exits abruptly returning an error, you environment is ready to work.

=== Configuration through .env file
// foggetaboutit
Much like the previous step, configuring the software with your settings (API key, eLabFTW URL...) is something you do _una tantum_ and then usually forget about it.

Inside the {software-name} folder there's a file called `.env.example`. Rename it removing ".example", then open it with your editor of choice. This is your *.env* (or *dotenv*) file.

[source,bash]
----
mv .env.example .env
vim .env
# The file presents itself like this:
 1 | api_key=""
 2 | elabid=""
 3 | ELABFTW_API_URL="https://elabftw.fisica.unina.it/api/v2"
 4 | operative_unit="cnr-spin.na"
----

* *api_key* is your own personal eLabFTW API key. Generating one is an easy task explained in full detail below.
* *elabid* is the elabid of the resource you'd like to select (your starting sample); this field can (and probably should) be left blank - in which case the application prompts you for an elabid on runtime, and your answer will not be stored meaning you can easily rerun the program with a different target.
* *ELABFTW_API_URL* is the URL of your eLabFTW instance; if you're running this from the laboratories in Monte S. Angelo, Naples, you're probably leaving this field as it is.
* *operative_unit* is the operative unit you ran your experiments from. It's only needed to compose the filename of the NeXus, which can be easily modified anytime later, and it's not necessary for creating the file itself.

None of these fields are required, meaning you can technically skip this entire section. If any of the first three keys are blank or missing you will be prompted to provide the necessary info at runtime, and your answers will not be memorized - meaning e.g. you will have to provide your API key every time you run the program.

NOTE: Do [.underline]#NOT# confuse .env with .venv: the first is a [.underline]#file# containing all the environmental variables you need to run {software-name} properly, the latter is a [.underline]#directory# containing your virtual environment with all the required modules.

==== Generating an eLabFTW API key
eLabFTW has its own link:{elabftw-apikey-docs}[API documentation^] on which you can rely. A new API key can be generated in the Settings → API Keys page by giving it a name and an access level:

.Screenshot from our eLabFTW. The key must have a name and permissions. Naturally, the key you see here in clear has been invalidated.
[#api-gen]
image::usage-apigen.png[align=center,width=75%]

The *name* of the key is a descriptor for you to remember why you created it in the first place - something like "parser_key01". The *permissions* can either be "_Read/Write_" or "_Read-Only_": in the first scenario the key may also be used to edit or create entries you own on eLabFTW, while read-only key only allow GET requests. {software-name} doesn't require writing permissions, so both options will do.

[WARNING]
.A few warnings.
====
* The key eLabFTW generates is [.underline]#only shown once#, then stored encrypted in the database. This means that after closing or refreshing the page the key [.underline]#is lost forever# if not saved on an external support. Which brings us to the second warning.
* Store and protect your API key like you would your password, as [.underline]#it gives full/limited access to your account# exactly like your password, but without the protection given by 2FA/MFA. For this purpose there are many offline (like link:{keepass-site}[KeePass^]) or online (like link:{bitwarden-site}[BitWarden^]) **password managers**.
* Your .env file is [.underline]#NOT# a safe place to _store_ your API key. Once pasted there be very careful who you share your files with, and be careful not to expose your key when sending your NeXus files to other computers. If you don't trust your awareness leave the api_key field blank and just paste your API key in the terminal every time you run {software-name}.
====

=== Running the program
Open a terminal into the project folder. Before attempting to run the program:

* Make sure your virtual environment is active, or if it isn't run: +
`source .venv/bin/activate`
* Make sure the required modules are installed, or if they aren't run:
`pip install -r requirements.txt`
* Make sure your .env file is properly set, or if it isn't make sure you know how to paste into the terminal the API key, the elabid of the required source and the URL of your eLabFTW instance (ending in `/api/v2`).

When you're ready, run:
[source,bash]
====
python3 src/main.py
====

If your .env file is completely filled out with valid values the only output you may read on the terminal are warnings or worst-case-scenario errors. Next chapter will cover all such cases. If your .env file lacks one or more values you will be asked to input the missing info at runtime.

==== Entering missing values if prompted
If you decide to run without a valid .env file (again, worst-case-scenario) you will be prompted to enter the required information directly into the terminal.

.The difference between running {software-name} with no .env, and with a properly filled out .env. Same parameters, same output.
[#usage-difference-dotenv]
image::usage-difference-dotenv.png[]

First and foremost you will be prompted for a valid API key. To paste your key in the terminal either right-click (_PowerShell_ and other terminal emulators), right-click > _Paste_, Ctrl + Shift + V (on most terminal emulators) or middle-click (Linux).

Then you will be prompted for an elabid - which is a positive integer number. You can find your sample's elabid on eLabFTW, above the sample's name and before the sample's label and status. See xref:usage-elabid[xrefstyle="short"].

Last but not least you will be prompted for a valid eLabFTW API endpoint URL. Such URL is composed by the base URL of your eLabFTW instance, closing with `/api/v2`. For instance: _++https://elabftw.fisica.unina.it/api/v2++_. +
{software-name} {revnumber} will not validate such URL or return some very specific error.

WARNING: Make sure the URL you paste doesn't end with a trailing slash. +
++https://elabftw.fisica.unina.it/api/v2++ ✓ +
++https://elabftw.fisica.unina.it/api/v2/++ ✗

You won't be prompted for the operative unit, so that will require either setting up a .env or manually editing your NeXus files' names. The list of officially approved acronyms for the operative units can be consulted on NFFA-DI's link:{nffa-di-uo-acronyms}[official website^].

.Where to find the elabid of a sample.
[#usage-elabid]
image::usage-elabid.png[]

==== Retrieving and verifying your file
By default the NeXus file will be saved in the `output/` folder. Currently ({revdate}) the software will also save a JSON dictionary with the full chain of all metadata collected on the sample. There is also an `attachments/` folder containing all the attachments downloaded during execution, which will be removed later on.

The file will be recognizable by its name, which should already be in compliance with the following NFFA-DI naming guidelines:

> «Each file generated in the context of a Proposal stored on OFED must use the following naming convention: ++nffa-di_[proposal_id]_[UO]_[UO_internal_id]++» - where _proposal_id_ is the approved ID of the research proposal, _UO_ is the link:{nffa-di-uo-acronyms}[official code^] of the operative unit, and «_UO_internal_id_ is a combination of the technique/instrument acronym and an Experiment ID freely decided». +
> «Each file generated in the context of an In-house Research Project stored on OFED must use the following naming convention: nffa-di_[UO]_[project_id]_key, where the first part of the name adheres to the name of the bucket, while key is arbitrary.»
>
> Source: link:{nffa-di-rdp}[NFFA-DI Research Data Policy^]

This means that the accepted filename for a NeXus file of a PLD, where proposal_id is _EXMPL01_, the operative unit is CNR-SPIN Naples and the sample's internal ID is _Na-26-012_ the filename will be:

image::usage-name.png[]

A NeXus file can be verified through one of the readers listed in xref:reading-nxs[xrefstyle="short"]. Pay attention to the following aspects:

* Do I visualize the file correctly?
* Does the file respect the fabrication method's schema?
* Is every required field present? Do I read the same values on eLabFTW and in the NeXus file? Are the units of measurement present?
* Can I visualize heatmaps and N-axis graphs correctly?

If the answer to all previous questions is "Yes", then the output file is NFFA-DI compliant.

////
collect nxs file
filename is: [paste link of guidelines here]
output folder is: output/
attachments will be in: output/attachments - to be removed
???
profit
////
