8
8
from . import conf
9
9
10
10
import os
11
+ from ast import literal_eval
12
+ from copy import copy
13
+
11
14
from astropy import units as u
12
15
from astropy .table import Table
13
- from astropy .table import MaskedColumn
14
- from copy import copy
15
16
16
17
try :
17
18
from mocpy import MOC
@@ -34,15 +35,15 @@ class MOCServerClass(BaseQuery):
34
35
Query the `CDS MOCServer <http://alasky.unistra.fr/MocServer/query>`_
35
36
36
37
The `CDS MOCServer <http://alasky.unistra.fr/MocServer/query>`_ allows the user to retrieve all the data sets (with
37
- their meta-datas ) having sources in a specific region. This region can be a `regions.CircleSkyRegion`, a
38
+ their meta-data ) having sources in a specific region. This region can be a `regions.CircleSkyRegion`, a
38
39
`regions.PolygonSkyRegion` or a `mocpy.MOC` object.
39
40
40
41
This package implements two methods:
41
42
42
43
* :meth:`~astroquery.mocserver.MOCServerClass.query_region` retrieving data-sets
43
- (their associated MOCs and meta-datas ) having sources in a given region.
44
+ (their associated MOCs and meta-data ) having sources in a given region.
44
45
* :meth:`~astroquery.mocserver.MOCServerClass.find_datasets` retrieving data-sets
45
- (their associated MOCs and meta-datas ) based on the values of their meta-datas .
46
+ (their associated MOCs and meta-data ) based on the values of their meta-data .
46
47
47
48
"""
48
49
URL = conf .server
@@ -86,18 +87,18 @@ def query_region(self, *, region=None, get_query_payload=False, verbose=False, *
86
87
max_norder : int, optional
87
88
Has sense only if ``return_moc`` is set to True. Specifies the maximum precision order of the returned MOC.
88
89
fields : [str], optional
89
- Has sense only if ``return_moc`` is set to False. Specifies which meta datas to retrieve. The returned
90
+ Has sense only if ``return_moc`` is set to False. Specifies which meta data to retrieve. The returned
90
91
`astropy.table.Table` table will only contain the column names given in ``fields``.
91
92
92
93
Specifying the fields we want to retrieve allows the request to be faster because of the reduced chunk of
93
94
data moving from the MOCServer to the client.
94
95
95
- Some meta-datas as ``obs_collection`` or ``data_ucd`` do not keep a constant type throughout all the
96
+ Some meta-data as ``obs_collection`` or ``data_ucd`` do not keep a constant type throughout all the
96
97
MOCServer's data-sets and this lead to problems because `astropy.table.Table` supposes values in a column
97
98
to have an unique type. When we encounter this problem for a specific meta-data, we remove its corresponding
98
99
column from the returned astropy table.
99
100
meta_data : str, optional
100
- Algebraic expression on meta-datas for filtering the data-sets at the server side.
101
+ Algebraic expression on meta-data for filtering the data-sets at the server side.
101
102
Examples of meta data expressions:
102
103
103
104
* Retrieve all the Hubble surveys: "ID=*HST*"
@@ -132,7 +133,7 @@ def find_datasets(self, meta_data, *, get_query_payload=False, verbose=False, **
132
133
Parameters
133
134
----------
134
135
meta_data : str
135
- Algebraic expression on meta-datas for filtering the data-sets at the server side.
136
+ Algebraic expression on meta-data for filtering the data-sets at the server side.
136
137
Examples of meta data expressions:
137
138
138
139
* Retrieve all the Hubble surveys: "ID=*HST*"
@@ -142,16 +143,16 @@ def find_datasets(self, meta_data, *, get_query_payload=False, verbose=False, **
142
143
More example of expressions can be found following this `link
143
144
<http://alasky.unistra.fr/MocServer/example>`_ (especially see the urls).
144
145
fields : [str], optional
145
- Has sense only if ``return_moc`` is set to False. Specifies which meta datas to retrieve. The returned
146
+ Has sense only if ``return_moc`` is set to False. Specifies which meta data to retrieve. The returned
146
147
`astropy.table.Table` table will only contain the column names given in ``fields``.
147
148
148
149
Specifying the fields we want to retrieve allows the request to be faster because of the reduced chunk of
149
150
data moving from the MOCServer to the client.
150
151
151
- Some meta-datas such as ``obs_collection`` or ``data_ucd`` do not keep a constant type throughout all the
152
+ Some meta-data such as ``obs_collection`` or ``data_ucd`` do not keep a constant type throughout all the
152
153
MOCServer's data-sets and this lead to problems because `astropy.table.Table` supposes values in a column
153
154
to have an unique type. This case is not common: it is mainly linked to a typing error in the text files
154
- describing the meta-datas of the data-sets. When we encounter this for a specific meta-data, we link the
155
+ describing the meta-data of the data-sets. When we encounter this for a specific meta-data, we link the
155
156
generic type ``object`` to the column. Therefore, keep in mind that ``object`` typed columns can contain
156
157
values of different types (e.g. lists and singletons or string and floats).
157
158
max_rec : int, optional
@@ -329,87 +330,30 @@ def _parse_result(self, response, *, verbose=False):
329
330
result = response .json ()
330
331
331
332
if not self .return_moc :
332
- """
333
- The user will get `astropy.table.Table` object whose columns refer to the returned data-set meta-datas.
334
- """
335
- # cast the data-sets meta-datas values to their correct Python type.
336
- typed_result = []
337
- for d in result :
338
- typed_d = {k : self ._cast_to_float (v ) for k , v in d .items ()}
339
- typed_result .append (typed_d )
340
-
341
- # looping over all the record's keys to find all the existing keys
342
- column_names_l = []
343
- for d in typed_result :
344
- column_names_l .extend (d .keys ())
345
-
346
- # remove all the doubles
347
- column_names_l = list (set (column_names_l ))
348
- # init a dict mapping all the meta-data's name to an empty list
349
- table_d = {key : [] for key in column_names_l }
350
- type_d = {key : None for key in column_names_l }
351
-
352
- masked_array_d = {key : [] for key in column_names_l }
353
- # fill the dict with the value of each returned data-set one by one.
354
- for d in typed_result :
355
- row_table_d = {key : None for key in column_names_l }
356
- row_table_d .update (d )
357
-
358
- for k , mask_l in masked_array_d .items ():
359
- entry_masked = False if k in d .keys () else True
360
- mask_l .append (entry_masked )
361
-
362
- for k , v in row_table_d .items ():
363
- if v :
364
- type_d [k ] = type (v )
365
-
366
- table_d [k ].append (v )
367
-
368
- # define all the columns using astropy.table.MaskedColumn objects
369
- columns_l = []
370
- for k , v in table_d .items ():
371
- try :
372
- if k != '#' :
373
- columns_l .append (MaskedColumn (v , name = k , mask = masked_array_d [k ], dtype = type_d [k ]))
374
- except ValueError :
375
- # some metadata can be of multiple types when looking on all the datasets.
376
- # this can be due to internal typing errors of the metadatas.
377
- columns_l .append (MaskedColumn (v , name = k , mask = masked_array_d [k ], dtype = object ))
378
-
379
- # return an `astropy.table.Table` object created from columns_l
380
- return Table (columns_l )
381
-
382
- """
383
- The user will get `mocpy.MOC` object.
384
- """
385
- # remove
386
- empty_order_removed_d = {}
387
- for order , ipix_l in result .items ():
388
- if len (ipix_l ) > 0 :
389
- empty_order_removed_d .update ({order : ipix_l })
333
+ # return a table with the meta-data, we cast the string values for convenience
334
+ result = [{key : _cast_to_float (value ) for key , value in row .items ()} for row in result ]
335
+ return Table (rows = result )
390
336
391
337
# return a `mocpy.MOC` object. See https://github.com/cds-astro/mocpy and the MOCPy's doc
392
- return MOC .from_json (empty_order_removed_d )
393
-
394
- @staticmethod
395
- def _cast_to_float (value ):
396
- """
397
- Cast ``value`` to a float if possible.
398
-
399
- Parameters
400
- ----------
401
- value : str
402
- string to cast
338
+ return MOC .from_json (result )
339
+
340
+ def _cast_to_float (value ):
341
+ """
342
+ Cast ``value`` to a float if possible.
403
343
404
- Returns
405
- -------
406
- value : float or str
407
- A float if it can be casted so otherwise the initial string.
408
- """
409
- try :
410
- return float (value )
411
- except (ValueError , TypeError ):
412
- return value
344
+ Parameters
345
+ ----------
346
+ value : str
347
+ string to cast
413
348
349
+ Returns
350
+ -------
351
+ value : float or str
352
+ A float if it can be casted so otherwise the initial string.
353
+ """
354
+ try :
355
+ return float (value )
356
+ except (ValueError , TypeError ):
357
+ return value
414
358
415
359
MOCServer = MOCServerClass ()
0 commit comments