# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.6
#     language: python
#     name: python3.6
# ---

import os
from os.path import exists
####################you will need to change some paths here!#####################
#output files
filename_out_nc='F:/data/cruise_data/saildrone/baja-2018/daily_files/sd-1002/saildrone-gen_4-baja_2018-EP-sd1002-ALL-1_min-v1.nc'
#F:/data/cruise_data/saildrone/baja-2018/data_so_far.kml'
#################################################################################
import xarray as xr
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
import requests
def listFD(url, ext=''):
    page = requests.get(url).text
    #print(page)
    soup = BeautifulSoup(page, 'html.parser')
    return [url + node.get('href') for node in soup.find_all('a') if (node.get('href').endswith(ext) and node.get('href').startswith('2'))]



drop_list=['quality_level','or_number_of_pixels','dt_analysis','satellite_zenith_angle','sses_bias'  ,
           'sses_standard_deviation','wind_speed','sst_dtime','l2p_flags']
for incr_day in range(17,18): #61):
    d = datetime(2018,4,11) + timedelta(days=incr_day)
    day_of_year = d.timetuple().tm_yday
    url = 'https://opendap.jpl.nasa.gov/opendap/OceanTemperature/ghrsst/data/GDS2/L3U/VIIRS_NPP/OSPO/v2.41/' \
            + str(d.year) + '/' + str(day_of_year).zfill(3) + '/'
    ext = 'nc'
    filenames=listFD(url, ext)
    ilen=len(filenames)
    ds_new=[]
    for ic in range(0,ilen):
        file = filenames[ic]
        print(file)
        ds=xr.open_dataset(file,drop_variables=drop_list)
        ds2=ds.sel(dict(lon=slice(-130,-110),lat=slice(42,23)))
        if ds2.sea_surface_temperature.max()>10:
            ds_new.append(ds2)
        ds.close()
    combined = xr.concat(ds_new, dim='time')
    mn=combined.sea_surface_temperature.mean(dim='time')
    mn.to_netcdf('./data/sst'+str(day_of_year)+'.nc')
    combined.to_netcdf('./data/all_sst'+str(day_of_year)+'.nc')

ds=xr.open_dataset(file)

ds.close()

# +

incr_day
# -



StarcoderdataJupyterScriptsDedupFiltered