Skip to content

Splusdata

connect

Class that logs in into splus.cloud and perform all types of download operation.

Source code in splusdata/connect.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
class connect:
    """Class that logs in into splus.cloud and perform all types of download operation. 
    """    
    def __init__(self, username=None, password=None):
        """Class may be initialized with splus.cloud user and password. If left in blanck, user and password will be asked on the runtime. 
        Once logged in you may use all main tools from this class.

        Examples:
            >>> conn = splusdata.connect('spcloud_user', 'password')
            You're connected.
            >>> conn.get_cut(ra=0.1, dec=0.1, radius=100, band='R')
            [<astropy.io.fits.hdu.image.PrimaryHDU object at 0x7f1bd283fe80>, <astropy.io.fits.hdu.compressed.CompImageHDU object at 0x7f1bd283fca0>]

        Args:
            username (str, optional): splus.cloud username
            password (str, optional): splus.cloud pasword
        """        

        if not username or not password:
            username = input("splus.cloud username: ")
            password = getpass("splus.cloud password: ")

        data = {'username':username, 'password':password}
        res = requests.post("https://splus.cloud/api/auth/login", data = data)
        usr = json.loads(res.content)
        self.token = usr['token']
        self.headers = {'Authorization': 'Token ' + self.token}

        res = requests.post("https://splus.cloud/api/auth/collab", headers = self.headers)
        collab = json.loads(res.content)

        if collab['collab'] == 'yes':
            self.collab = True
            print('You have access to internal data')
        else:
            self.collab = False
            pass

        self.lastres = ''


    def twelve_band_img(self, ra, dec, radius, noise=0.15, saturation=0.15, R="R,I,F861,Z", G="G,F515,F660", B="U,F378,F395,F410,F430", option=1):        
        """Function to get twelve band composed images. 

        Args:
            ra (float): RA coordinate in degrees. 
            dec (float): DEC coordinate in degrees.
            radius (int): Image size in pixels. Final size will be (radius X radius)
            noise (float, optional): Image noise value. Defaults to 0.15.
            saturation (float, optional): Image saturation value. Defaults to 0.15.    
            R (str, optional): Combinations of bands to compose red. Defaults to "R,I,F861,Z".
            G (str, optional): Combinations of bands to compose green. Defaults to "G,F515,F660".
            B (str, optional): Combinations of bands to compose blue. Defaults to "U,F378,F395,F410,F430".
            option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
        Returns:
            PIL.Image: Image requested
        """             


        res = requests.get("https://splus.cloud/api/get_image/" + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + R + "-" + G + "-" + B + "/" + str(noise) + "/" + str(saturation) + "/" + str(option), headers=self.headers)
        source = json.loads(res.content)
        source['filename']
        res = requests.get("https://splus.cloud" + source['filename'], headers=self.headers)
        image = Image.open(io.BytesIO(res.content))

        self.lastcontent = image
        self.lastres = '12img'
        return image

    def get_img(self, ra, dec, radius, R="I", G="R", B="G", stretch=3, Q=8, option=1):
        """Function to get three band composed images made by lupton. 

        Args:
            ra (float): RA coordinate in degrees. 
            dec (float): DEC coordinate in degrees.
            radius (int): Image size in pixels. Final size will be (radius X radius)
            R (str, optional): Band to compose red. Defaults to "I".
            G (str, optional): Band to compose green. Defaults to "R".
            B (str, optional): Band to compose blue. Defaults to "G".
            stretch (int, optional): Stretch of image, same of make_lupton_rgb from astropy. Defaults to 3.
            Q (int, optional): Q of image, same of make_lupton_rgb from astropy. Defaults to 8.
            option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
        Returns:
            PIL.Image: Image requested
        """        
        res = requests.get("https://splus.cloud/api/get_lupton_image/" + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + str(R) + "/" + str(G) + "/" + str(B) + "/" + str(stretch) + "/" + str(Q) + "/" + str(option), headers=self.headers)
        source = json.loads(res.content)
        source['filename']
        res = requests.get("https://splus.cloud" + source['filename'], headers=self.headers)
        image = Image.open(io.BytesIO(res.content))

        self.lastcontent = image
        self.lastres = 'get_cut'
        return image

    def get_band_img(self, ra, dec, radius, band='R', mode='linear', option=1):
        """Get image composed with one band.

        Args:
            ra (float): RA coordinate in degrees. 
            dec (float): DEC coordinate in degrees.
            radius (int): Image size in pixels. Final size will be (radius X radius)
            band (str, optional): Band to compose image. Defaults to 'R'.
            mode (str, optional): Mode. Defaults to 'linear'.
            option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
        Returns:
            PIL.Image: Image requested
        """        
        res = requests.get("https://splus.cloud/api/get_band_image/" + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + str(band) + "/" + str(mode) + "/" + str(option), headers=self.headers)
        source = json.loads(res.content)
        source['filename']
        res = requests.get("https://splus.cloud" + source['filename'], headers=self.headers)
        image = Image.open(io.BytesIO(res.content))

        self.lastcontent = image
        self.lastres = 'get_band_image'
        return image


    def get_cut(self, ra, dec, radius, band, filepath=None, option=1):
        """Get fits cut.

        Args:
            ra (float): RA coordinate in degrees. 
            dec (float): DEC coordinate in degrees.
            radius (int): Image size in pixels. Final size will be (radius X radius)
            band (str): Band requested
            filepath (str, optional): file path to save result. Defaults to None.
            option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
        Returns:
            astropy.io.fits: Fits file with image. 
        """        
        if band.upper() == 'ALL':
            if filepath == None:
                return 'You must save the file while getting "all" bands'

            elif filepath != None:  
                filepath = filepath + ".tar.gz"
                res = requests.get("https://splus.cloud/api/get_direct_cut/"  + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + "ALL", headers=self.headers)

                with open(filepath, 'wb') as f:
                    f.write(res.content)
                    f.close()

                return 'File saved to ' + filepath

        res = requests.get("https://splus.cloud/api/get_direct_cut/"  + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + str(band) + "/" + str(option), headers=self.headers)
        hdu = fits.open(io.BytesIO(res.content))
        if filepath != None:
            hdu.writeto(filepath + ".fz")

        self.lastcontent = hdu
        self.lastres = 'cut'
        return hdu

    def get_cut_weight(self, ra, dec, radius, band, filepath=None, option=1):
        """Get weight image fits cut. 

        Args:
            ra (float): RA coordinate in degrees. 
            dec (float): DEC coordinate in degrees.
            radius (int): Image size in pixels. Final size will be (radius X radius)
            band (str): Band requested
            filepath (str, optional): file path to save result. Defaults to None.
            option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
        Returns:
            astropy.io.fits: Fits file with image. 
        """        
        if band.upper() == 'ALL':
            if filepath == None:
                return 'You must save the file while getting "all" bands'

            elif filepath != None:  
                filepath = filepath + ".tar.gz"
                res = requests.get("https://splus.cloud/api/get_direct_cut_weight/"  + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + "ALL" + "/" + str(option), headers=self.headers)

                with open(filepath, 'wb') as f:
                    f.write(res.content)
                    f.close()

                return 'File saved to ' + filepath

        res = requests.get("https://splus.cloud/api/get_direct_cut_weight/"  + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + str(band) + "/" + str(option), headers=self.headers)
        hdu = fits.open(io.BytesIO(res.content))
        if filepath != None:
            hdu.writeto(filepath + ".fz")

        self.lastcontent = hdu
        self.lastres = 'cut'
        return hdu

    def get_field(self, field, band):
        """Get whole 11k field fits.

        Args:
            field (str): field name. 
            band (str): Band.

        Returns:
            astropy.io.fits: Fits file with image. 
        """        
        res = requests.get("https://splus.cloud/api/get_direct_field/" + str(field) + "/" + str(band) , headers=self.headers)
        hdu = fits.open(io.BytesIO(res.content))

        self.lastres = 'field'
        self.lastcontent = hdu
        return hdu  

    def get_field_weight(self, field, band):
        """Get whole 11k weight field fits.

        Args:
            field (str): field name. 
            band (str): Band.

        Returns:
            astropy.io.fits: Fits file with image. 
        """        
        res = requests.get("https://splus.cloud/api/get_direct_field_weight/" + str(field) + "/" + str(band) , headers=self.headers)
        hdu = fits.open(io.BytesIO(res.content))

        self.lastres = 'field'
        self.lastcontent = hdu
        return hdu  

    def get_tap_tables(self):
        """Get info about available tables.
        """        

        print("Tables and columns available at https://splus.cloud/query/")

    def query(self, query, table_upload=None, publicdata=None):
        """Perform async queries on splus cloud TAP service. 

        Args:
            query (str): query itself.
            table_upload (pandas.DataFrame, optional): table to upload. Defaults to None.
            publicdata (bool, optional): If internal wants to access public data. Defaults to None.

        Returns:
            astropy.table.Table: result table.
        """        
        if self.collab:
            baselink = "https://splus.cloud/tap/tap/async/"
        else:
            baselink = "https://splus.cloud/public-TAP/tap/async/"


        if publicdata and self.collab:
            baselink = "https://splus.cloud/public-TAP/tap/async/"

        data = {
            "request": 'doQuery',
            "version": '1.0',
            "lang": 'ADQL',
            "phase": 'run',
            "query": query,
            "format": 'fits'
        }


        if str(type(table_upload)) != "<class 'NoneType'>":
            if 'astropy.table' in str(type(table_upload)):
                if len(table_upload) > 6000:
                    print('Cutting to the first 6000 objects!')
                    table_upload = table_upload[0:6000]
                    table_upload = from_table(table_upload)

                    IObytes = io.BytesIO()
                    writeto(table_upload, IObytes)

                    IObytes.seek(0)
                else:
                    table_upload = from_table(table_upload)

                    IObytes = io.BytesIO()
                    writeto(table_upload, IObytes)

                    IObytes.seek(0)

            elif 'astropy.io.votable' in str(type(table_upload)):
                if table_upload.get_first_table().nrows > 6000:
                    return 'votable bigger than 6000'
                else:
                    IObytes = io.BytesIO()
                    writeto(table_upload, IObytes)
                    IObytes.seek(0)

            elif 'DataFrame' in str(type(table_upload)):
                if len(table_upload) > 6000:
                    print('Cutting to the first 6000 objects!')
                    table_upload = table_upload[0:6000]
                    table_upload = Table.from_pandas(table_upload)
                    table_upload = from_table(table_upload)
                    IObytes = io.BytesIO()
                    writeto(table_upload, IObytes)
                    IObytes.seek(0)
                else:
                    table_upload = Table.from_pandas(table_upload)
                    table_upload = from_table(table_upload)
                    IObytes = io.BytesIO()
                    writeto(table_upload, IObytes)
                    IObytes.seek(0)


            else:
                return 'Table type not supported'

            data['upload'] = 'upload,param:uplTable'
            res = requests.post(baselink , data = data, headers=self.headers, files={'uplTable': IObytes.read()})

        if not table_upload:
            res = requests.post(baselink , data = data, headers=self.headers)

        xmldoc = minidom.parse(io.BytesIO(res.content))

        try:
            item = xmldoc.getElementsByTagName('phase')[0]
            process = item.firstChild.data

            item = xmldoc.getElementsByTagName('jobId')[0]
            jobID = item.firstChild.data

            while process == 'EXECUTING':
                res = requests.get(baselink + jobID, headers=self.headers)
                xmldoc = minidom.parse(io.BytesIO(res.content))

                item = xmldoc.getElementsByTagName('phase')[0]
                process = item.firstChild.data
                time.sleep(5)

            if process == 'COMPLETED':
                item = xmldoc.getElementsByTagName('result')[0]
                link = item.attributes['xlink:href'].value

                link = link.replace("http://192.168.10.23:8080", "https://splus.cloud").replace("http://10.180.0.209:8080", "https://splus.cloud").replace("http://10.180.0.207:8080", "https://splus.cloud").replace("http://10.180.0.219:8080", "https://splus.cloud")
                res = requests.get(link, headers=self.headers)

                self.lastres = 'query'
                self.lastcontent = Table.read(io.BytesIO(res.content))
                print('finished')

                return self.lastcontent

            if process == 'ERROR':
                item = xmldoc.getElementsByTagName('message')[0]
                message = item.firstChild.data

                print("Error: ", message)

        except:
            item = xmldoc.getElementsByTagName('INFO')
            print(item[0].attributes['value'].value, ": ", item[0].firstChild.data)


    def checkcoords(self, ra, dec):
        """Check if coords are in footprint

        Args:
            ra (float): RA coordinate in degrees. 
            dec (float): DEC coordinate in degrees.
        Returns:
            dict: result.
        """        
        res = requests.get("https://splus.cloud/api/whichdr/" + str(ra) + "/" + str(dec) , headers=self.headers)
        res = res.content.decode("utf-8")

        res = ast.literal_eval(res)
        return res


    def get_last_result(self):
        """If you missed to save your last query or image into a variable, you may get it here.

        Returns:
            _type_: Last result, may be image or query. 
        """        
        return self.lastcontent

__init__(username=None, password=None)

Class may be initialized with splus.cloud user and password. If left in blanck, user and password will be asked on the runtime. Once logged in you may use all main tools from this class.

Examples:

>>> conn = splusdata.connect('spcloud_user', 'password')
You're connected.
>>> conn.get_cut(ra=0.1, dec=0.1, radius=100, band='R')
[<astropy.io.fits.hdu.image.PrimaryHDU object at 0x7f1bd283fe80>, <astropy.io.fits.hdu.compressed.CompImageHDU object at 0x7f1bd283fca0>]

Parameters:

Name Type Description Default
username str

splus.cloud username

None
password str

splus.cloud pasword

None
Source code in splusdata/connect.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def __init__(self, username=None, password=None):
    """Class may be initialized with splus.cloud user and password. If left in blanck, user and password will be asked on the runtime. 
    Once logged in you may use all main tools from this class.

    Examples:
        >>> conn = splusdata.connect('spcloud_user', 'password')
        You're connected.
        >>> conn.get_cut(ra=0.1, dec=0.1, radius=100, band='R')
        [<astropy.io.fits.hdu.image.PrimaryHDU object at 0x7f1bd283fe80>, <astropy.io.fits.hdu.compressed.CompImageHDU object at 0x7f1bd283fca0>]

    Args:
        username (str, optional): splus.cloud username
        password (str, optional): splus.cloud pasword
    """        

    if not username or not password:
        username = input("splus.cloud username: ")
        password = getpass("splus.cloud password: ")

    data = {'username':username, 'password':password}
    res = requests.post("https://splus.cloud/api/auth/login", data = data)
    usr = json.loads(res.content)
    self.token = usr['token']
    self.headers = {'Authorization': 'Token ' + self.token}

    res = requests.post("https://splus.cloud/api/auth/collab", headers = self.headers)
    collab = json.loads(res.content)

    if collab['collab'] == 'yes':
        self.collab = True
        print('You have access to internal data')
    else:
        self.collab = False
        pass

    self.lastres = ''

checkcoords(ra, dec)

Check if coords are in footprint

Parameters:

Name Type Description Default
ra float

RA coordinate in degrees.

required
dec float

DEC coordinate in degrees.

required

Returns:

Name Type Description
dict

result.

Source code in splusdata/connect.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
def checkcoords(self, ra, dec):
    """Check if coords are in footprint

    Args:
        ra (float): RA coordinate in degrees. 
        dec (float): DEC coordinate in degrees.
    Returns:
        dict: result.
    """        
    res = requests.get("https://splus.cloud/api/whichdr/" + str(ra) + "/" + str(dec) , headers=self.headers)
    res = res.content.decode("utf-8")

    res = ast.literal_eval(res)
    return res

get_band_img(ra, dec, radius, band='R', mode='linear', option=1)

Get image composed with one band.

Parameters:

Name Type Description Default
ra float

RA coordinate in degrees.

required
dec float

DEC coordinate in degrees.

required
radius int

Image size in pixels. Final size will be (radius X radius)

required
band str

Band to compose image. Defaults to 'R'.

'R'
mode str

Mode. Defaults to 'linear'.

'linear'
option str

in case a coordinate overlap over two fields, use option = 2 if you want the second field.

1

Returns:

Type Description

PIL.Image: Image requested

Source code in splusdata/connect.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def get_band_img(self, ra, dec, radius, band='R', mode='linear', option=1):
    """Get image composed with one band.

    Args:
        ra (float): RA coordinate in degrees. 
        dec (float): DEC coordinate in degrees.
        radius (int): Image size in pixels. Final size will be (radius X radius)
        band (str, optional): Band to compose image. Defaults to 'R'.
        mode (str, optional): Mode. Defaults to 'linear'.
        option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
    Returns:
        PIL.Image: Image requested
    """        
    res = requests.get("https://splus.cloud/api/get_band_image/" + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + str(band) + "/" + str(mode) + "/" + str(option), headers=self.headers)
    source = json.loads(res.content)
    source['filename']
    res = requests.get("https://splus.cloud" + source['filename'], headers=self.headers)
    image = Image.open(io.BytesIO(res.content))

    self.lastcontent = image
    self.lastres = 'get_band_image'
    return image

get_cut(ra, dec, radius, band, filepath=None, option=1)

Get fits cut.

Parameters:

Name Type Description Default
ra float

RA coordinate in degrees.

required
dec float

DEC coordinate in degrees.

required
radius int

Image size in pixels. Final size will be (radius X radius)

required
band str

Band requested

required
filepath str

file path to save result. Defaults to None.

None
option str

in case a coordinate overlap over two fields, use option = 2 if you want the second field.

1

Returns:

Type Description

astropy.io.fits: Fits file with image.

Source code in splusdata/connect.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def get_cut(self, ra, dec, radius, band, filepath=None, option=1):
    """Get fits cut.

    Args:
        ra (float): RA coordinate in degrees. 
        dec (float): DEC coordinate in degrees.
        radius (int): Image size in pixels. Final size will be (radius X radius)
        band (str): Band requested
        filepath (str, optional): file path to save result. Defaults to None.
        option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
    Returns:
        astropy.io.fits: Fits file with image. 
    """        
    if band.upper() == 'ALL':
        if filepath == None:
            return 'You must save the file while getting "all" bands'

        elif filepath != None:  
            filepath = filepath + ".tar.gz"
            res = requests.get("https://splus.cloud/api/get_direct_cut/"  + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + "ALL", headers=self.headers)

            with open(filepath, 'wb') as f:
                f.write(res.content)
                f.close()

            return 'File saved to ' + filepath

    res = requests.get("https://splus.cloud/api/get_direct_cut/"  + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + str(band) + "/" + str(option), headers=self.headers)
    hdu = fits.open(io.BytesIO(res.content))
    if filepath != None:
        hdu.writeto(filepath + ".fz")

    self.lastcontent = hdu
    self.lastres = 'cut'
    return hdu

get_cut_weight(ra, dec, radius, band, filepath=None, option=1)

Get weight image fits cut.

Parameters:

Name Type Description Default
ra float

RA coordinate in degrees.

required
dec float

DEC coordinate in degrees.

required
radius int

Image size in pixels. Final size will be (radius X radius)

required
band str

Band requested

required
filepath str

file path to save result. Defaults to None.

None
option str

in case a coordinate overlap over two fields, use option = 2 if you want the second field.

1

Returns:

Type Description

astropy.io.fits: Fits file with image.

Source code in splusdata/connect.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def get_cut_weight(self, ra, dec, radius, band, filepath=None, option=1):
    """Get weight image fits cut. 

    Args:
        ra (float): RA coordinate in degrees. 
        dec (float): DEC coordinate in degrees.
        radius (int): Image size in pixels. Final size will be (radius X radius)
        band (str): Band requested
        filepath (str, optional): file path to save result. Defaults to None.
        option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
    Returns:
        astropy.io.fits: Fits file with image. 
    """        
    if band.upper() == 'ALL':
        if filepath == None:
            return 'You must save the file while getting "all" bands'

        elif filepath != None:  
            filepath = filepath + ".tar.gz"
            res = requests.get("https://splus.cloud/api/get_direct_cut_weight/"  + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + "ALL" + "/" + str(option), headers=self.headers)

            with open(filepath, 'wb') as f:
                f.write(res.content)
                f.close()

            return 'File saved to ' + filepath

    res = requests.get("https://splus.cloud/api/get_direct_cut_weight/"  + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + str(band) + "/" + str(option), headers=self.headers)
    hdu = fits.open(io.BytesIO(res.content))
    if filepath != None:
        hdu.writeto(filepath + ".fz")

    self.lastcontent = hdu
    self.lastres = 'cut'
    return hdu

get_field(field, band)

Get whole 11k field fits.

Parameters:

Name Type Description Default
field str

field name.

required
band str

Band.

required

Returns:

Type Description

astropy.io.fits: Fits file with image.

Source code in splusdata/connect.py
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def get_field(self, field, band):
    """Get whole 11k field fits.

    Args:
        field (str): field name. 
        band (str): Band.

    Returns:
        astropy.io.fits: Fits file with image. 
    """        
    res = requests.get("https://splus.cloud/api/get_direct_field/" + str(field) + "/" + str(band) , headers=self.headers)
    hdu = fits.open(io.BytesIO(res.content))

    self.lastres = 'field'
    self.lastcontent = hdu
    return hdu  

get_field_weight(field, band)

Get whole 11k weight field fits.

Parameters:

Name Type Description Default
field str

field name.

required
band str

Band.

required

Returns:

Type Description

astropy.io.fits: Fits file with image.

Source code in splusdata/connect.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def get_field_weight(self, field, band):
    """Get whole 11k weight field fits.

    Args:
        field (str): field name. 
        band (str): Band.

    Returns:
        astropy.io.fits: Fits file with image. 
    """        
    res = requests.get("https://splus.cloud/api/get_direct_field_weight/" + str(field) + "/" + str(band) , headers=self.headers)
    hdu = fits.open(io.BytesIO(res.content))

    self.lastres = 'field'
    self.lastcontent = hdu
    return hdu  

get_img(ra, dec, radius, R='I', G='R', B='G', stretch=3, Q=8, option=1)

Function to get three band composed images made by lupton.

Parameters:

Name Type Description Default
ra float

RA coordinate in degrees.

required
dec float

DEC coordinate in degrees.

required
radius int

Image size in pixels. Final size will be (radius X radius)

required
R str

Band to compose red. Defaults to "I".

'I'
G str

Band to compose green. Defaults to "R".

'R'
B str

Band to compose blue. Defaults to "G".

'G'
stretch int

Stretch of image, same of make_lupton_rgb from astropy. Defaults to 3.

3
Q int

Q of image, same of make_lupton_rgb from astropy. Defaults to 8.

8
option str

in case a coordinate overlap over two fields, use option = 2 if you want the second field.

1

Returns:

Type Description

PIL.Image: Image requested

Source code in splusdata/connect.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def get_img(self, ra, dec, radius, R="I", G="R", B="G", stretch=3, Q=8, option=1):
    """Function to get three band composed images made by lupton. 

    Args:
        ra (float): RA coordinate in degrees. 
        dec (float): DEC coordinate in degrees.
        radius (int): Image size in pixels. Final size will be (radius X radius)
        R (str, optional): Band to compose red. Defaults to "I".
        G (str, optional): Band to compose green. Defaults to "R".
        B (str, optional): Band to compose blue. Defaults to "G".
        stretch (int, optional): Stretch of image, same of make_lupton_rgb from astropy. Defaults to 3.
        Q (int, optional): Q of image, same of make_lupton_rgb from astropy. Defaults to 8.
        option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
    Returns:
        PIL.Image: Image requested
    """        
    res = requests.get("https://splus.cloud/api/get_lupton_image/" + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + str(R) + "/" + str(G) + "/" + str(B) + "/" + str(stretch) + "/" + str(Q) + "/" + str(option), headers=self.headers)
    source = json.loads(res.content)
    source['filename']
    res = requests.get("https://splus.cloud" + source['filename'], headers=self.headers)
    image = Image.open(io.BytesIO(res.content))

    self.lastcontent = image
    self.lastres = 'get_cut'
    return image

get_last_result()

If you missed to save your last query or image into a variable, you may get it here.

Returns:

Name Type Description
_type_

Last result, may be image or query.

Source code in splusdata/connect.py
387
388
389
390
391
392
393
def get_last_result(self):
    """If you missed to save your last query or image into a variable, you may get it here.

    Returns:
        _type_: Last result, may be image or query. 
    """        
    return self.lastcontent

get_tap_tables()

Get info about available tables.

Source code in splusdata/connect.py
241
242
243
244
245
def get_tap_tables(self):
    """Get info about available tables.
    """        

    print("Tables and columns available at https://splus.cloud/query/")

query(query, table_upload=None, publicdata=None)

Perform async queries on splus cloud TAP service.

Parameters:

Name Type Description Default
query str

query itself.

required
table_upload pandas.DataFrame

table to upload. Defaults to None.

None
publicdata bool

If internal wants to access public data. Defaults to None.

None

Returns:

Type Description

astropy.table.Table: result table.

Source code in splusdata/connect.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
def query(self, query, table_upload=None, publicdata=None):
    """Perform async queries on splus cloud TAP service. 

    Args:
        query (str): query itself.
        table_upload (pandas.DataFrame, optional): table to upload. Defaults to None.
        publicdata (bool, optional): If internal wants to access public data. Defaults to None.

    Returns:
        astropy.table.Table: result table.
    """        
    if self.collab:
        baselink = "https://splus.cloud/tap/tap/async/"
    else:
        baselink = "https://splus.cloud/public-TAP/tap/async/"


    if publicdata and self.collab:
        baselink = "https://splus.cloud/public-TAP/tap/async/"

    data = {
        "request": 'doQuery',
        "version": '1.0',
        "lang": 'ADQL',
        "phase": 'run',
        "query": query,
        "format": 'fits'
    }


    if str(type(table_upload)) != "<class 'NoneType'>":
        if 'astropy.table' in str(type(table_upload)):
            if len(table_upload) > 6000:
                print('Cutting to the first 6000 objects!')
                table_upload = table_upload[0:6000]
                table_upload = from_table(table_upload)

                IObytes = io.BytesIO()
                writeto(table_upload, IObytes)

                IObytes.seek(0)
            else:
                table_upload = from_table(table_upload)

                IObytes = io.BytesIO()
                writeto(table_upload, IObytes)

                IObytes.seek(0)

        elif 'astropy.io.votable' in str(type(table_upload)):
            if table_upload.get_first_table().nrows > 6000:
                return 'votable bigger than 6000'
            else:
                IObytes = io.BytesIO()
                writeto(table_upload, IObytes)
                IObytes.seek(0)

        elif 'DataFrame' in str(type(table_upload)):
            if len(table_upload) > 6000:
                print('Cutting to the first 6000 objects!')
                table_upload = table_upload[0:6000]
                table_upload = Table.from_pandas(table_upload)
                table_upload = from_table(table_upload)
                IObytes = io.BytesIO()
                writeto(table_upload, IObytes)
                IObytes.seek(0)
            else:
                table_upload = Table.from_pandas(table_upload)
                table_upload = from_table(table_upload)
                IObytes = io.BytesIO()
                writeto(table_upload, IObytes)
                IObytes.seek(0)


        else:
            return 'Table type not supported'

        data['upload'] = 'upload,param:uplTable'
        res = requests.post(baselink , data = data, headers=self.headers, files={'uplTable': IObytes.read()})

    if not table_upload:
        res = requests.post(baselink , data = data, headers=self.headers)

    xmldoc = minidom.parse(io.BytesIO(res.content))

    try:
        item = xmldoc.getElementsByTagName('phase')[0]
        process = item.firstChild.data

        item = xmldoc.getElementsByTagName('jobId')[0]
        jobID = item.firstChild.data

        while process == 'EXECUTING':
            res = requests.get(baselink + jobID, headers=self.headers)
            xmldoc = minidom.parse(io.BytesIO(res.content))

            item = xmldoc.getElementsByTagName('phase')[0]
            process = item.firstChild.data
            time.sleep(5)

        if process == 'COMPLETED':
            item = xmldoc.getElementsByTagName('result')[0]
            link = item.attributes['xlink:href'].value

            link = link.replace("http://192.168.10.23:8080", "https://splus.cloud").replace("http://10.180.0.209:8080", "https://splus.cloud").replace("http://10.180.0.207:8080", "https://splus.cloud").replace("http://10.180.0.219:8080", "https://splus.cloud")
            res = requests.get(link, headers=self.headers)

            self.lastres = 'query'
            self.lastcontent = Table.read(io.BytesIO(res.content))
            print('finished')

            return self.lastcontent

        if process == 'ERROR':
            item = xmldoc.getElementsByTagName('message')[0]
            message = item.firstChild.data

            print("Error: ", message)

    except:
        item = xmldoc.getElementsByTagName('INFO')
        print(item[0].attributes['value'].value, ": ", item[0].firstChild.data)

twelve_band_img(ra, dec, radius, noise=0.15, saturation=0.15, R='R,I,F861,Z', G='G,F515,F660', B='U,F378,F395,F410,F430', option=1)

Function to get twelve band composed images.

Parameters:

Name Type Description Default
ra float

RA coordinate in degrees.

required
dec float

DEC coordinate in degrees.

required
radius int

Image size in pixels. Final size will be (radius X radius)

required
noise float

Image noise value. Defaults to 0.15.

0.15
saturation float

Image saturation value. Defaults to 0.15.

0.15
R str

Combinations of bands to compose red. Defaults to "R,I,F861,Z".

'R,I,F861,Z'
G str

Combinations of bands to compose green. Defaults to "G,F515,F660".

'G,F515,F660'
B str

Combinations of bands to compose blue. Defaults to "U,F378,F395,F410,F430".

'U,F378,F395,F410,F430'
option str

in case a coordinate overlap over two fields, use option = 2 if you want the second field.

1

Returns:

Type Description

PIL.Image: Image requested

Source code in splusdata/connect.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def twelve_band_img(self, ra, dec, radius, noise=0.15, saturation=0.15, R="R,I,F861,Z", G="G,F515,F660", B="U,F378,F395,F410,F430", option=1):        
    """Function to get twelve band composed images. 

    Args:
        ra (float): RA coordinate in degrees. 
        dec (float): DEC coordinate in degrees.
        radius (int): Image size in pixels. Final size will be (radius X radius)
        noise (float, optional): Image noise value. Defaults to 0.15.
        saturation (float, optional): Image saturation value. Defaults to 0.15.    
        R (str, optional): Combinations of bands to compose red. Defaults to "R,I,F861,Z".
        G (str, optional): Combinations of bands to compose green. Defaults to "G,F515,F660".
        B (str, optional): Combinations of bands to compose blue. Defaults to "U,F378,F395,F410,F430".
        option (str, optional): in case a coordinate overlap over two fields, use option = 2 if you want the second field.
    Returns:
        PIL.Image: Image requested
    """             


    res = requests.get("https://splus.cloud/api/get_image/" + str(ra) + "/" + str(dec) + "/" + str(radius) + "/" + R + "-" + G + "-" + B + "/" + str(noise) + "/" + str(saturation) + "/" + str(option), headers=self.headers)
    source = json.loads(res.content)
    source['filename']
    res = requests.get("https://splus.cloud" + source['filename'], headers=self.headers)
    image = Image.open(io.BytesIO(res.content))

    self.lastcontent = image
    self.lastres = '12img'
    return image