%%bash
signac --help

"""
Explanation: 1.5 Command Line Interface (CLI)
The following section demonstrates how to use the signac command line interface (CLI).
The CLI allows you to interact with your data space without python, which may be advantageous in various situations, e.g., for scripting or data exploration.
You will find that for many of the functions introduced earlier there is an equivalent CLI command.
The CLI is accessed via the top-level signac command.
You can get help about the various functions with the -h or --help argument.
End of explanation
"""


% rm -rf projects/tutorial/cli
% mkdir -p projects/tutorial/cli

"""
Explanation: To interact with a project on the command line, the current working directory needs to be within or below the project's root directory.
Let's start by reseting the designated project root directory for this section of the tutorial.
End of explanation
"""


% cd projects/tutorial/cli

"""
Explanation: Next we switch the current working directory to the project root directory.
End of explanation
"""


%%bash
signac init TutorialCLIProject

"""
Explanation: Then we initialize the project.
End of explanation
"""


%%bash
signac project
signac project --workspace

"""
Explanation: We can verify the project configuration using the signac project command.
End of explanation
"""


%%bash
signac job '{"kT": 1.0, "p": 1.0, "N": 1000}'

"""
Explanation: We access a job by providing the state point on the command line in JSON format ).
) The JSON format requires double quotes for keys.
End of explanation
"""


%%bash
signac job '{"kT": 1.0, "p": 1.0, "N": 1000}' --workspace

"""
Explanation: By default this will print the associated job id to STDOUT.
Instead of the job id, we can also get the path to the job's workspace.
End of explanation
"""


%%bash
signac job '{"kT": 1.0, "p": 1.0, "N": 1000}' --create

"""
Explanation: Please note that obtaining the path in this way does not necessarily mean that the path exists.
However, we can initialize the job and create the workspace using the -c or --create argument.
End of explanation
"""


%%bash
signac statepoint ee617ad585a90809947709a7a45dda9a

"""
Explanation: We can use the signac statepoint command to get the statepoint associated with the initialized job.
End of explanation
"""


%%bash
echo '{"kT": 1.0, "p": 0.1, "N": 1000}' > statepoint.txt
cat statepoint.txt

"""
Explanation: Usually we will not provide statepoints on the command line, but read them from a file.
Let's create a statepoint file with one statepoint:
End of explanation
"""


%%bash
cat statepoint.txt | signac job

"""
Explanation: We can pipe the content of this file into the signac CLI to get the corresponding job id.
End of explanation
"""


import signac

def V_idg(N, p, kT):
    return N * kT / p

project = signac.get_project()
for p in 0.1, 1.0, 10.0:
    sp = {'p': p, 'kT': 1.0, 'N': 1000}
    job = project.open_job(sp)
    job.document['V'] = V_idg(**sp)

"""
Explanation: We will reproduce the ideal gas project from section 1.1 to generate some data for the following examples.
End of explanation
"""


%%bash
signac find

"""
Explanation: We can use the signac find command to find all jobs within our project's workspace.
End of explanation
"""


%%bash
signac find '{"p": 0.1}'

"""
Explanation: Just like with project.find_jobs() we can provide a filter argument to find a subset of jobs matching the given filter.
Here we get all jobs with a pressure of 0.1:
End of explanation
"""


%%bash
signac find --doc-filter '{"V": 1000.0}'

"""
Explanation: In this example, that is of course only one job.
Similarly, we can also filter based on information in the job document. Here, we find all jobs that have a volume corresponding to a pressure of 1 (volume = 1000*1/1 = 1000).
End of explanation
"""


%%bash
signac find | xargs signac statepoint

"""
Explanation: Once again, this only returns one job in this case.
We can pipe signac find results into signac statepoint with xargs to resolve the statepoints.
End of explanation
"""


%%bash
signac project --index > index.txt

"""
Explanation: You will have noticed that each time we execute a find operation the data space is indexed anew.
This is no problem for small data spaces, however especially for larger data spaces, where the indexing process may be substantially expensive it's advantageous to cache the index in a file.
End of explanation
"""


%%bash
signac find -i index.txt '{"p": 0.1}'

"""
Explanation: This index file can be used in conjunction with all functions that require a data space index, for example signac find:
End of explanation
"""


%%bash
signac view -i index.txt ./view

"""
Explanation: Or for instance when creating a linked view.
End of explanation
"""


%ls view

"""
Explanation: The signac view command works exactly like project.create_linked_view such that the ./view directory now contains a linked view to the job workspaces.
End of explanation
"""


