<jupyter_start><jupyter_text>Reading a remote file using OPeNDAP Now we will read a remote file using the [OPeNDAP protocol](http://www.opendap.org/). The advantage is that the file has not to be downloaded on your computer, while you can access the variables you want using it as it were on your computer. Furthermore, if the file is automatically updated on the remote server, you can run the same script without having to bother downloading it everytime it is updaded.We first import the modules needed to load and plot the data.<jupyter_code>%matplotlib inline
import netCDF4
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>We will use data from a mooring located in the Ibiza Channel.To get the file name, browse the directories in the thredds daa server until you find the file you want (http://thredds.socib.es/thredds/dodsC/mooring/conductivity_and_temperature_recorder/buoy_canaldeibiza-scb_sbe37006/L1/dep0003_buoy-canaldeibiza_scb-sbe37006_L1_latest.nc.html). Then copy the field called **Data URL**.<jupyter_code>dataurl = "http://thredds.socib.es/thredds/dodsC/mooring/conductivity_and_temperature_recorder/buoy_canaldeibiza-scb_sbe37006/L1/dep0003_buoy-canaldeibiza_scb-sbe37006_L1_latest.nc"<jupyter_output><empty_output><jupyter_text>Read the file The method is exactly the same as the previous example, except the file has not been downloaded on your machine. In the present case, the file is updated several times a day, meaning that running the same code at different time of the day could produce different results, as more data will be included.To be sure the file is closed, we will use the [context manager](https://en.wikibooks.org/wiki/Python_Programming/Context_Managers) *with ... as ...*.<jupyter_code>with netCDF4.Dataset(dataurl) as ds:
    temperature_values = ds.variables['WTR_TEM_SBE37'][:]
    time_values = ds.variables['time'][:]
    temperature_units = ds.variables['WTR_TEM_SBE37'].units
    time_units = ds.variables['time'].units<jupyter_output><empty_output><jupyter_text>The variable storing the temperature is not called *temp* or *TEMP*, but *WTR_TEM_SBE37*. The variable names can be obtained by checking the [link on the thredds server](http://thredds.socib.es/thredds/dodsC/mooring/conductivity_and_temperature_recorder/buoy_canaldeibiza-scb_sbe37006/L1/dep0003_buoy-canaldeibiza_scb-sbe37006_L1_latest.nc.html) or by running:<jupyter_code>with netCDF4.Dataset(dataurl) as ds:
    print ds.variables.keys()<jupyter_output>[u'station_name', u'time', u'LAT', u'LON', u'DEPTH', u'WTR_TEM_SBE37', u'QC_WTR_TEM_SBE37', u'COND_SBE37', u'QC_COND_SBE37', u'SALT_SBE37', u'QC_SALT_SBE37']<jupyter_text>In another example we will see how to do it in a more clever way.  A simple plot<jupyter_code>plt.plot(time_values, temperature_values)
plt.xlabel(time_units, fontsize=20)
plt.ylabel(temperature_units, fontsize=20)<jupyter_output><empty_output><jupyter_text>With the time units in seconds since 1st January 1970, it is not easy to see which period the curve represents. The solution is to change the time format using the *units* attribute of the variable and the *num2date* function.<jupyter_code>time2 = netCDF4.num2date(time_values, time_units)
print time2[0:5]
plt.plot(time2, temperature_values)
plt.ylabel(temperature_units, fontsize=20)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08681384248210024, 'fraction_numerical': 0.026252983293556086, 'mean_word_length': 6.418141592920354, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '222842', 'license': 'MIT', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1124, 'n_words': 327, 'path': 'PythonNotebooks/PlatformPlots/Read_TimeSeries_2.ipynb', 'repo_name': 'CopernicusMarineInsitu/INSTACTraining', 'size': '3352'}