=====================================
Time Series And Compiled Time Series
=====================================

Sen2chain latest functionality is Time Series Extraction. It allows to spatially extract masked radiometric indices from a spatial layer, like geojson or shapefile, and produce spatial statistics  of these radiometric indices on the time period of your preferences. 
Make sure the radiometric indices were produced for the right cloudmask version, otherwise an empty file will be generated.
Multiprocessing is available to speed up calculations. Finally, output data is exported to csv format.

.. note:: 

    Two functions are available to extract time series. The new compiled version is recommended if extractions are to be carried out on a recurring basis, for more efficient use. 

Time Series Extraction
----------------------

.. code-block:: python

    from sen2chain import TimeSeries
    ts=TimeSeries(vectors_file='/path/to/shapefile.shp',date_min=date_min,date_max=date_max,indices=['NDVI','NBR','BIGR'],cm_version='CM004',iterations =1)
    ....
    ....
    ts.to_csv(out_path='/path/out_path')

With this command, the data is not reprojected. Vector data must therefore be in WGS 84 (EPSG: 4326) for extraction to work properly.
    
    
Time Series Compiled Extraction
-------------------------------

This lastest version generates a new file tree in the Sen2chain_data/Extraction folder. 
The folder is structured into three folders: "raw_data", which contains the raw extraction for each image, the "shp_files" folder, which stores the shp file for recognition if reused, and finally the "compiled_data" folder, which will contain the various compilations reaquested by the user.
The user can choose the compilation to be "yearly", "monthly", "weekly" or "alldata". 


.. code-block:: python

    from sen2chain import TimeSeriesCompil
    ts=TimeSeriesCompil(vectors_file="/path/to/shp", 
        indices = ["NDVI", "NDWIGAO"], 
        date_min = "2021-01-01", date_max="2022-01-01", 
        cm_version = "CM004", multiproc=4) #initiate class 
    
    ts.extract_data()  #extract data to create raw files
    ts.compile_data(alldata = True, monthly=True)  #if you want your data to be compiled for all the images on the time period + monthly


Recurring Time Series Extraction : JobExtraction
------------------------------------------------

Extractions can be scheduled automatically by creating programmed jobs in Cron. 

.. code-block:: python

    from sen2chain import JobExtraction
    jb = JobExtraction('JobName') #iniate the template
    jb.save() #create the job file in sen2chain_data/config/jobs_extr
    
You can now open the file sen2chain_data/config/jobs_extr/job_JobName.cfg and choose the parameters that you want for your extraction. 
You can also add a last parameter which allows you to send your result on another server if you want : *user@server:path/to/destination*.
Once the cfg file is filled you can enable your cron job. 

.. code-block:: python

    j.cron_enable() #enable your job in your crontab 
    
    j.run() #run job immediatly (Job need to be saved first)
    
    

