<jupyter_start><jupyter_text>ABSTRACT  All CMEMS in situ data products can be found and downloaded after [registration](http://marine.copernicus.eu/services-portfolio/register-now/) via [CMEMS catalogue] (http://marine.copernicus.eu/services-portfolio/access-to-products/).Such channel is advisable just for sporadic netCDF donwloading because when operational, interaction with the web user interface is not practical. In this context though, the use of scripts for ftp file transference is is a much more advisable approach.As long as every line of such files contains information about the netCDFs contained within the different directories [see at tips why](https://github.com/CopernicusMarineInsitu/INSTACTraining/blob/master/tips/README.md), it is posible for users to loop over its lines to download only those that matches a number of specifications such as spatial coverage, time coverage, provider, data_mode, parameters or file_name related (region, data type, TS or PF, platform code, or/and platform category, timestamp). PREREQUISITES - [credentias](http://marine.copernicus.eu/services-portfolio/register-now/)- aimed [in situ product name](http://cmems-resources.cls.fr/documents/PUM/CMEMS-INS-PUM-013.pdf)- aimed [hosting distribution unit](https://github.com/CopernicusMarineInsitu/INSTACTraining/blob/master/tips/README.md)- aimed [index file](https://github.com/CopernicusMarineInsitu/INSTACTraining/blob/master/tips/README.md)i.e:<jupyter_code>user = '' #type CMEMS user name within colons
password = ''  #type CMEMS password within colons
product_name = 'INSITU_BAL_NRT_OBSERVATIONS_<PASSWORD>' #type aimed CMEMS in situ product 
distribution_unit = 'cmems.smhi.se' #type aimed hosting institution
index_file = 'index_history.txt' #type aimed index file name

#remember! platform category only for history and monthly directories<jupyter_output><empty_output><jupyter_text>DOWNLOAD 1. Index file download<jupyter_code>import ftplib 
ftp=ftplib.FTP(distribution_unit,user,password) 
ftp.cwd("Core")
ftp.cwd(product_name) 
local_file = open(index_file, 'wb')
ftp.retrbinary('RETR ' + index_file, local_file.write)
local_file.close()
ftp.quit()
#ready when 221 Goodbye.!<jupyter_output><empty_output><jupyter_text>QUICK VIEW<jupyter_code>import numpy as np
import pandas as pd
from random import randint
index = np.genfromtxt(index_file, skip_header=6, unpack=False, delimiter=',', dtype=None,
           names=['catalog_id', 'file_name', 'geospatial_lat_min', 'geospatial_lat_max',
                     'geospatial_lon_min', 'geospatial_lon_max',
                     'time_coverage_start', 'time_coverage_end', 
                     'provider', 'date_update', 'data_mode', 'parameters'])
dataset = randint(0,len(index)) #ramdom line of the index file
values = [index[dataset]['catalog_id'], '<a href='+index[dataset]['file_name']+'>'+index[dataset]['file_name']+'</a>', index[dataset]['geospatial_lat_min'], index[dataset]['geospatial_lat_max'],
                 index[dataset]['geospatial_lon_min'], index[dataset]['geospatial_lon_max'], index[dataset]['time_coverage_start'],
                 index[dataset]['time_coverage_end'], index[dataset]['provider'], index[dataset]['date_update'], index[dataset]['data_mode'],
                 index[dataset]['parameters']]
headers = ['catalog_id', 'file_name', 'geospatial_lat_min', 'geospatial_lat_max',
                     'geospatial_lon_min', 'geospatial_lon_max',
                     'time_coverage_start', 'time_coverage_end', 
                     'provider', 'date_update', 'data_mode', 'parameters']
df = pd.DataFrame(values, index=headers, columns=[dataset])
df.style<jupyter_output><empty_output><jupyter_text>FILTERING CRITERIA Regarding the above glimpse, it is posible to filter by 12 criteria. As example we will setup next a filter to only download those files that contains data within a defined boundingbox.     1. Aimed category<jupyter_code>targeted_category = 'drifter'<jupyter_output><empty_output><jupyter_text>2. netCDF filtering/selection<jupyter_code>selected_netCDFs = [];

for netCDF in index:    
    file_name = netCDF['file_name']
    
    folders = file_name.split('/')[3:len(file_name.split('/'))-1]
    category = file_name.split('/')[3:len(file_name.split('/'))-1][len(file_name.split('/')[3:len(file_name.split('/'))-1])-1]
    
    if (category == targeted_category):
        selected_netCDFs.append(file_name)
            
print("total: " +str(len(selected_netCDFs)))<jupyter_output>total: 11<jupyter_text>SELECTION DOWNLOAD<jupyter_code>for nc in selected_netCDFs:

    last_idx_slash = nc.rfind('/')
    ncdf_file_name = nc[last_idx_slash+1:]
    folders = nc.split('/')[3:len(nc.split('/'))-1]
    host = nc.split('/')[2] #or distribution unit
    
    ftp=ftplib.FTP(host,user,password) 
    for folder in folders:
        ftp.cwd(folder)
                                   
    local_file = open(ncdf_file_name, 'wb')
    ftp.retrbinary('RETR '+ncdf_file_name, local_file.write)
    local_file.close()                             

    ftp.quit()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12426268187180496, 'fraction_numerical': 0.005112072355485647, 'mean_word_length': 4.722159730033746, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '10120', 'license': 'MIT', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1604, 'n_words': 353, 'path': 'PythonNotebooks/indexFileNavigation/.ipynb_checkpoints/index_file_navigation_platformCategory-checkpoint.ipynb', 'repo_name': 'CopernicusMarineInsitu/INSTACTraining', 'size': '5083'}