<jupyter_start><jupyter_text>Convert AlborEx positions data to JSON file easily displayed by  [Leaflet](https://leafletjs.com/).    The functions defined is this notebook will be added as methods for the different classes of `alborexdata.py`.<jupyter_code>import alborexdata
import os
import geojson
import numpy as np
import netCDF4
import datetime
import json
with open('alborexconfig.json') as json_data_file:
    config = json.load(json_data_file)
outputdir = config["datadirleaflet"]<jupyter_output><empty_output><jupyter_text>CTD<jupyter_code>lonCTD1, latCTD1 = alborexdata.load_lonloat_ctdleg(config["datafiles"]["ctdlegs"][0])
lonCTD2, latCTD2 = alborexdata.load_lonloat_ctdleg(config["datafiles"]["ctdlegs"][1])
CTDlist = [[lat, lon] for lat, lon in zip(latCTD1, lonCTD1)]
with open(os.path.join(outputdir, 'CTD1.js'), 'w') as f:
    f.write("var CTD1list = " + str(CTDlist))
CTDlist = [[lat, lon] for lat, lon in zip(latCTD2, lonCTD2)]
with open(os.path.join(outputdir, 'CTD2.js'), 'w') as f:
    f.write("var CTD2list = " + str(CTDlist))<jupyter_output><empty_output><jupyter_text>Glider<jupyter_code>reload(alborexdata)
glider1 = alborexdata.Glider() 
glider2 = alborexdata.Glider() 
glider1.get_coords(config["datafiles"]["gliders"][0])
glider2.get_coords(config["datafiles"]["gliders"][1])
makeGeoJson(lon1, lat1, os.path.join(outputdir, 'Glider1.js'), 'glider1', NN=100)
makeGeoJson(lon2, lat2, os.path.join(outputdir, 'Glider2.js'), 'glider2', NN=100)<jupyter_output><empty_output><jupyter_text>Drifters We've created a list of deployments (text file) using [get_deployments_alborex](get_deployments_alborex.ipynb).<jupyter_code>drifterfilelist = './deploymentList.txt'<jupyter_output><empty_output><jupyter_text>Dates of interest for the extraction<jupyter_code>dateinit = datetime.datetime(2014, 5, 25)
dateend = datetime.datetime(2014, 6, 15)
timeinit = (dateinit-datetime.datetime(1970,1,1)).total_seconds()
timeend = (dateend-datetime.datetime(1970,1,1)).total_seconds()
lonall, latall = [], []
with open(drifterfilelist, 'r') as f:
    for drifterfile in f:
        with netCDF4.Dataset(drifterfile) as nc:
            time = nc.variables['time'][:]
            timeunits = nc.variables['time'].units
            goodtime = np.where(np.logical_and((time > timeinit),(time <= timeend)))[0]
            
            # Only take values in the considered period
            lon = nc.variables['LON'][goodtime]
            lat = nc.variables['LAT'][goodtime]
            QClon = nc.variables['QC_LON'][goodtime]
            QClat = nc.variables['QC_LAT'][goodtime]
            # Only take good coordinates
            goodcoords = np.where(np.logical_and((QClon==1),(QClat==1)))[0]
            lon = lon[goodcoords]
            lat = lat[goodcoords]
            
            lonall.append(lon.tolist())
            latall.append(lat.tolist())
            #print(lon.min(), lon.max(), lat.min(), lat.max())
# Create list of tuples
DrifterList = [[(lon, lat) for lon, lat in zip(londep, latdep)] for londep, latdep in zip(lonall, latall)]
DrifterGeojson = geojson.MultiLineString(DrifterList)  
with open(os.path.join(outputdir, 'drifters.js'), 'w') as f:
    f.write("var drifters = ")
    geojson.dump(DrifterGeojson, f)<jupyter_output><empty_output><jupyter_text>Profilers Same as drifters.<jupyter_code>profilerfilelist = './alborexProfilerList.txt'
lonall, latall = [], []
with open(profilerfilelist, 'r') as f:
    for profilerfile in f:
        with netCDF4.Dataset(profilerfile) as nc:
            time = nc.variables['time'][:]
            timeunits = nc.variables['time'].units
            goodtime = np.where(np.logical_and((time > timeinit),(time <= timeend)))[0]
            
            # Only take values in the considered period
            lon = nc.variables['LON'][goodtime[1:]]
            lat = nc.variables['LAT'][goodtime[1:]]
            """
            QClon = nc.variables['QC_LON'][goodtime]
            QClat = nc.variables['QC_LAT'][goodtime]
            # Only take good coordinates
            goodcoords = np.where(np.logical_and((QClon==1),(QClat==1)))[0]
            lon = lon[goodcoords]
            lat = lat[goodcoords]
            """
            lonall.append(lon.tolist())
            latall.append(lat.tolist())
            #print(lon.min(), lon.max(), lat.min(), lat.max())
# Create list of tuples
ProfilerList = [[(lon, lat) for lon, lat in zip(londep, latdep)] for londep, latdep in zip(lonall, latall)]
ProfilerGeojson = geojson.MultiLineString(ProfilerList)  
with open(os.path.join(outputdir, 'profilers.js'), 'w') as f:
    f.write("var profilers = ")
    geojson.dump(ProfilerGeojson, f)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14594017094017095, 'fraction_numerical': 0.015598290598290598, 'mean_word_length': 3.8760416666666666, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '371689', 'license': 'MIT', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1689, 'n_words': 311, 'path': 'python/netCDF2json.ipynb', 'repo_name': 'ctroupin/AlborEx-Data', 'size': '4680'}