Skip to content

Commit 6445242

Browse files
author
Favio Medrano
committed
refactor name
1 parent 72158b3 commit 6445242

37 files changed

+238
-165
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## About
44

5-
pyerddap is a API implementation for the ERDDAP server.
5+
erddap-python is a python API implementation for the ERDDAP server.
66

77
ERDDAP is a data server that gives you a simple, consistent way to download subsets of gridded and tabular scientific datasets in common file formats and make graphs and maps.
88

@@ -12,13 +12,13 @@ ERDDAP is a data server that gives you a simple, consistent way to download subs
1212
Using pip:
1313

1414
```
15-
$ pip install pyerddap
15+
$ pip install erddap-python
1616
```
1717

1818
## Usage
1919

2020
```
21-
from pyerddap import ERDDAP_Tabledap
21+
from erddapClient import ERDDAP_Tabledap
2222
2323
url = 'https://coastwatch.pfeg.noaa.gov/erddap'
2424
datasetid = 'cwwcNDBCMet'

erddapClient/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from erddapClient.erddap_server import ERDDAP
2+
from erddapClient.erddap_dataset import ERDDAP_Dataset
3+
from erddapClient.erddap_tabledap import ERDDAP_Tabledap
4+
from erddapClient.erddap_griddap import ERDDAP_Griddap
5+
6+
__all__ = ["ERRDAP", "ERDDAP_Dataset", "ERDDAP_Tabledap", "ERDDAP_Griddap"]
475 Bytes
Binary file not shown.
454 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

pyerddap/erddap_dataset.py renamed to erddapClient/erddap_dataset.py

+11-105
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import os
2-
from pyerddap import url_operations
3-
from pyerddap.remote_requests import urlread
4-
from pyerddap.parse_utils import parseDictMetadata, parseConstraintValue
5-
from pyerddap.formatting import dataset_repr, simple_dataset_repr
2+
from erddapClient import url_operations
3+
from erddapClient.remote_requests import urlread
4+
from erddapClient.parse_utils import parseDictMetadata, parseConstraintValue
5+
from erddapClient.formatting import dataset_repr, simple_dataset_repr
66
import datetime as dt
77

88

99
class ERDDAP_Dataset:
1010

1111
DEFAULT_FILETYPE = 'csvp'
12-
BINARY_FILETYPES = [ 'dods', 'mat', 'nc', 'ncCF', 'ncCFMA', 'wav' ]
12+
BINARY_FILETYPES = [ 'dods', 'mat', 'nc', 'ncCF', 'ncCFMA', 'wav',
13+
'smallPdf', 'pdf', 'largePdf',
14+
'smallPng', 'png', 'largePng', 'transparentPng']
1315

1416
def __init__(self, erddapurl, datasetid, protocol='tabledap', auth=None, lazyload=True):
1517
self.erddapurl = erddapurl
@@ -118,7 +120,7 @@ def getBaseURL(self, filetype=DEFAULT_FILETYPE):
118120

119121
def getAttribute(self, attribute, variableName='NC_GLOBAL'):
120122
self.loadMetadata()
121-
for rowAttribute in self.metadata:
123+
for rowAttribute in self.__info:
122124
if rowAttribute['Variable Name'] == variableName and rowAttribute['Attribute Name'] == attribute:
123125
return rowAttribute['Value']
124126

@@ -127,11 +129,11 @@ def loadMetadata(self):
127129
if self.metadata is None:
128130
rawRequest = urlread(self.getMetadataURL(), auth=self.erddapauth)
129131
rawRequestJson = rawRequest.json()
130-
self.metadata, self.variables = parseDictMetadata(rawRequestJson)
132+
self.__info, self.variables, self.metadata = parseDictMetadata(rawRequestJson)
131133

132134

133-
def getMetadataURL(self, request_format='json'):
134-
return os.path.join(self.erddapurl, "info", self.datasetid , "index." + request_format )
135+
def getMetadataURL(self, filetype='json'):
136+
return os.path.join(self.erddapurl, "info", self.datasetid , "index." + filetype )
135137

136138

137139
def clearConstraints(self):
@@ -160,99 +162,3 @@ def getDataRequest(self, filetype=DEFAULT_FILETYPE, request_kwargs={}):
160162
return rawRequest.text
161163

162164

163-
# Server side functions wrappers
164-
165-
def addVariablesWhere(self, attributeName, attributeValue):
166-
'''
167-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#addVariablesWhere
168-
'''
169-
self.serverSideFunctions.append(
170-
'addVariablesWhere("{}","{}")'.format(attributeName, attributeValue)
171-
)
172-
return self
173-
174-
def distinct(self):
175-
'''
176-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#distinct
177-
'''
178-
self.serverSideFunctions.append( 'distinct()' )
179-
return self
180-
181-
def units(self, value):
182-
'''
183-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#units
184-
'''
185-
self.serverSideFunctions.append( 'units({})'.format(value) )
186-
187-
def orderBy(self, variables):
188-
'''
189-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderBy
190-
'''
191-
self.addServerSideFunction('orderBy', variables)
192-
return self
193-
194-
def orderByClosest(self, variables):
195-
'''
196-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByClosest
197-
'''
198-
self.addServerSideFunction('orderByClosest', variables)
199-
return self
200-
201-
def orderByCount(self, variables):
202-
'''
203-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByCount
204-
'''
205-
self.addServerSideFunction('orderByCount', variables)
206-
return self
207-
208-
def orderByLimit(self, variables):
209-
'''
210-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByLimit
211-
'''
212-
self.addServerSideFunction('orderByLimit', variables)
213-
return self
214-
215-
def orderByMax(self, variables):
216-
'''
217-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByMax
218-
'''
219-
self.addServerSideFunction('orderByMax', variables)
220-
return self
221-
222-
def orderByMin(self, variables):
223-
'''
224-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByMin
225-
'''
226-
self.addServerSideFunction('orderByMin', variables)
227-
return self
228-
229-
def orderByMinMax(self, variables):
230-
'''
231-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByMinMax
232-
'''
233-
self.addServerSideFunction('orderByMinMax', variables)
234-
return self
235-
236-
def orderByMean(self, variables):
237-
'''
238-
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByMean
239-
'''
240-
self.addServerSideFunction('orderByMean', variables)
241-
return self
242-
243-
def addServerSideFunction(self, functionName, arguments):
244-
self.serverSideFunctions.append(
245-
"{}(\"{}\")".format(
246-
functionName, self.parseListOrStrToCommaSeparatedString(arguments)
247-
)
248-
)
249-
250-
251-
def parseListOrStrToCommaSeparatedString(self, listorstring):
252-
if type(listorstring) is list:
253-
return ','.join(listorstring)
254-
else:
255-
return listorstring
256-
257-
258-

erddapClient/erddap_griddap.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from erddapClient.erddap_dataset import ERDDAP_Dataset
2+
import xarray as xr
3+
import requests
4+
5+
class ERDDAP_Griddap(ERDDAP_Dataset):
6+
7+
DEFAULT_FILETYPE = 'nc'
8+
9+
def __init__(self, url, datasetid, auth=None, lazyload=True):
10+
super().__init__(url, datasetid, 'griddap', auth, lazyload=lazyload)
11+
12+
13+
@property
14+
def xarray(self):
15+
if not hasattr(self,'__xarray'):
16+
if self.erddapauth:
17+
session = requests.Session()
18+
session.auth = self.erddapauth
19+
store = xr.backends.PydapDataStore.open(self.getBaseURL('opendap'),
20+
session=session)
21+
self.__xarray = xr.open_dataset(store)
22+
else:
23+
self.__xarray = xr.open_dataset(self.getBaseURL('opendap'))
24+
return self.__xarray

pyerddap/erddap_server.py renamed to erddapClient/erddap_server.py

+40-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import os
22
from urllib.parse import quote_plus
3-
from pyerddap import url_operations
4-
from pyerddap.formatting import erddap_search_results_repr
5-
from pyerddap.parse_utils import parseConstraintValue, parseConstraintDateTime, parseSearchResults
6-
from pyerddap.remote_requests import urlread
7-
from pyerddap.erddap_dataset import ERDDAP_Dataset
8-
from pyerddap.erddap_tabledap import ERDDAP_Tabledap
9-
from pyerddap.erddap_griddap import ERDDAP_Griddap
3+
from erddapClient import url_operations
4+
from erddapClient.formatting import erddap_search_results_repr, erddap_server_repr
5+
from erddapClient.parse_utils import parseConstraintValue, parseConstraintDateTime, parseSearchResults
6+
from erddapClient.remote_requests import urlread
7+
from erddapClient.erddap_dataset import ERDDAP_Dataset
8+
from erddapClient.erddap_tabledap import ERDDAP_Tabledap
9+
from erddapClient.erddap_griddap import ERDDAP_Griddap
1010

1111

1212
class ERDDAP_SearchResults(list):
@@ -20,15 +20,47 @@ def results(self):
2020

2121
class ERDDAP:
2222

23-
ALLDATASETS_VARIABLES = ['datasetID','accessible','institution','dataStructure','cdm_data_type','class','title','minLongitude','maxLongitude','longitudeSpacing','minLatitude','maxLatitude','latitudeSpacing','minAltitude','maxAltitude','minTime','maxTime','timeSpacing','griddap','subset','tabledap','MakeAGraph','sos','wcs','wms','files','fgdc','iso19115','metadata','sourceUrl','infoUrl','rss','email','testOutOfDate','outOfDate','summary']
23+
ALLDATASETS_VARIABLES = [ 'datasetID','accessible','institution','dataStructure',
24+
'cdm_data_type','class','title','minLongitude','maxLongitude',
25+
'longitudeSpacing','minLatitude','maxLatitude','latitudeSpacing',
26+
'minAltitude','maxAltitude','minTime','maxTime','timeSpacing',
27+
'griddap','subset','tabledap','MakeAGraph','sos','wcs','wms',
28+
'files','fgdc','iso19115','metadata','sourceUrl','infoUrl',
29+
'rss','email','testOutOfDate','outOfDate','summary' ]
2430

2531
def __init__(self, url, auth=None, lazyload=True):
2632
self.serverURL = url
2733
self.auth = auth
2834
self.tabledapAllDatasets = ERDDAP_Dataset(self.serverURL, 'allDatasets', auth=auth)
2935

36+
def __repr__(self):
37+
return erddap_server_repr(self)
38+
39+
@property
40+
def version(self):
41+
if not hasattr(self,'__version'):
42+
try:
43+
req = urlread( os.path.join(self.serverURL, 'version'), self.auth)
44+
__version = req.text
45+
__version = __version.replace("\n", "")
46+
except:
47+
__version = 'ERDDAP_version=<1.22'
48+
return __version
49+
50+
@property
51+
def version_string(self):
52+
if not hasattr(self,'__version_string'):
53+
try:
54+
req = urlread( os.path.join(self.serverURL, 'version_string'), self.auth)
55+
__version_string = req.text
56+
__version_string = __version_string.replace("\n", "")
57+
except:
58+
__version_string = 'ERDDAP_version_string=<1.80'
59+
return __version_string
3060

3161
def advancedSearch(self, **filters):
62+
"""
63+
"""
3264

3365
searchURL = self.getSearchURL( **filters)
3466
rawSearchResults = urlread(searchURL, self.auth)

erddapClient/erddap_tabledap.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
from erddapClient.erddap_dataset import ERDDAP_Dataset
2+
from erddapClient.formatting import dataset_repr
3+
from io import StringIO
4+
import pandas as pd
5+
6+
7+
class ERDDAP_Tabledap(ERDDAP_Dataset):
8+
9+
DEFAULT_FILETYPE = 'csvp'
10+
11+
def __init__(self, url, datasetid, auth=None, lazyload=True):
12+
super().__init__(url, datasetid, 'tabledap', auth, lazyload=lazyload)
13+
14+
15+
def getDataFrame(self, request_kwargs={}, **kwargs):
16+
csvpdata = self.getDataRequest('csvp', **request_kwargs)
17+
return pd.read_csv(StringIO(csvpdata), **kwargs)
18+
19+
#
20+
# Tabledap server side functions wrappers
21+
#
22+
def addVariablesWhere(self, attributeName, attributeValue):
23+
'''
24+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#addVariablesWhere
25+
'''
26+
self.serverSideFunctions.append(
27+
'addVariablesWhere("{}","{}")'.format(attributeName, attributeValue)
28+
)
29+
return self
30+
31+
def distinct(self):
32+
'''
33+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#distinct
34+
'''
35+
self.serverSideFunctions.append( 'distinct()' )
36+
return self
37+
38+
def units(self, value):
39+
'''
40+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#units
41+
'''
42+
self.serverSideFunctions.append( 'units({})'.format(value) )
43+
44+
def orderBy(self, variables):
45+
'''
46+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderBy
47+
'''
48+
self.addServerSideFunction('orderBy', variables)
49+
return self
50+
51+
def orderByClosest(self, variables):
52+
'''
53+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByClosest
54+
'''
55+
self.addServerSideFunction('orderByClosest', variables)
56+
return self
57+
58+
def orderByCount(self, variables):
59+
'''
60+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByCount
61+
'''
62+
self.addServerSideFunction('orderByCount', variables)
63+
return self
64+
65+
def orderByLimit(self, variables):
66+
'''
67+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByLimit
68+
'''
69+
self.addServerSideFunction('orderByLimit', variables)
70+
return self
71+
72+
def orderByMax(self, variables):
73+
'''
74+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByMax
75+
'''
76+
self.addServerSideFunction('orderByMax', variables)
77+
return self
78+
79+
def orderByMin(self, variables):
80+
'''
81+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByMin
82+
'''
83+
self.addServerSideFunction('orderByMin', variables)
84+
return self
85+
86+
def orderByMinMax(self, variables):
87+
'''
88+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByMinMax
89+
'''
90+
self.addServerSideFunction('orderByMinMax', variables)
91+
return self
92+
93+
def orderByMean(self, variables):
94+
'''
95+
https://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html#orderByMean
96+
'''
97+
self.addServerSideFunction('orderByMean', variables)
98+
return self
99+
100+
def addServerSideFunction(self, functionName, arguments):
101+
self.serverSideFunctions.append(
102+
"{}(\"{}\")".format(
103+
functionName, self.parseListOrStrToCommaSeparatedString(arguments)
104+
)
105+
)
106+
107+
108+
def parseListOrStrToCommaSeparatedString(self, listorstring):
109+
if type(listorstring) is list:
110+
return ','.join(listorstring)
111+
else:
112+
return listorstring
113+
114+

0 commit comments

Comments
 (0)