Skip to content

query.py

Class to perform queries on the VAST observational data.

Attributes:

Name Type Description
HOST_NCPU int

The number of CPU found on the host using 'cpu_count()'.

FieldQuery

This is a class representation of a query of the VAST Pilot survey fields, returning basic information such as observation dates and psf information.

Attributes:

Name Type Description
field str

Name of requested field.

valid bool

Confirm the requested field exists.

pilot_info DataFrame

Dataframe describing the pilot survey.

field_info DataFrame

Dataframe describing properties of the field.

epochs DataFrame

Dataframe containing epochs this field was observed in.

Source code in vasttools/query.py
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
class FieldQuery:
    """
    This is a class representation of a query of the VAST Pilot survey
    fields, returning basic information such as observation dates and psf
    information.

    Attributes:
        field (str): Name of requested field.
        valid (bool): Confirm the requested field exists.
        pilot_info (pandas.core.frame.DataFrame):
            Dataframe describing the pilot survey.
        field_info (pandas.core.frame.DataFrame):
            Dataframe describing properties of the field.
        epochs (pandas.core.frame.DataFrame):
            Dataframe containing epochs this field was observed in.
    """

    def __init__(self, field: str) -> None:
        """Constructor method

        Args:
            field: Name of requested field.

        Returns:
            None
        """
        self.logger = logging.getLogger('vasttools.query.FieldQuery')

        self.field = field
        self.valid = self._check_field()

    def _check_field(self) -> bool:
        """
        Check that the field is a valid pilot survey field.

        We check against epochs 1 and 18 which are the first complete
        low- and mid-band epochs respectively.

        Returns:
            Bool representing if field is valid.
        """

        epoch_01 = load_fields_file("1")
        epoch_18 = load_fields_file("18")
        base_fields = pd.concat(epoch_01, epoch_18)
        self.logger.debug("Field name: {}".format(self.field))
        result = base_fields['FIELD_NAME'].str.contains(
            re.escape(self.field)
        ).any()
        self.logger.debug("Field found: {}".format(result))
        if result is False:
            self.logger.error(
                "Field {} is not a valid field name!".format(self.field)
            )
        del epoch_01, epoch_18, base_fields
        return result

    def _get_beams(self) -> Dict[str, Beams]:
        """
        Processes all the beams of a field per epoch and initialises
        radio_beam.Beams objects.

        Returns:
            Dictionary of 'radio_beam.Beams' objects.
        """
        epoch_beams = {}
        for e in self.settings['epochs']:
            epoch_cut = self.field_info[self.field_info.EPOCH == e]
            epoch_beams[e] = Beams(
                epoch_cut.BMAJ.values * u.arcsec,
                epoch_cut.BMIN.values * u.arcsec,
                epoch_cut.BPA.values * u.deg
            )
        return epoch_beams

    def run_query(
            self,
            psf: bool = False,
            largest_psf: bool = False,
            common_psf: bool = False,
            all_psf: bool = False,
            save: bool = False,
            _pilot_info: Optional[pd.DataFrame] = None
    ) -> None:
        """Running the field query.

        Args:
            largest_psf: If true the largest psf is calculated
                of the field per epoch. Defaults to False.
            common_psf: If true the common psf is calculated
                of the field per epoch. Defaults to False.
            all_psf: If true the common psf is calculated of the field
                per epoch and all the beam information of
                the field is shown. Defaults to False.
            save: Save the output tables to a csv file. Defaults to False.
            _pilot_info: Allows for the pilot info to be provided
                rather than the function building it locally. If not provided
                then the dataframe is built. Defaults to None.

        Returns:
            None
        """
        if not self.valid:
            self.logger.error("Field doesn't exist.")
            return

        if _pilot_info is not None:
            self.pilot_info = _pilot_info
        else:
            self.logger.debug("Building pilot info file.")
            epochs_dict = RELEASED_EPOCHS.copy()
            epochs_dict.update(OBSERVED_EPOCHS)
            for i, val in enumerate(sorted(epochs_dict)):
                if i == 0:
                    self.pilot_info = load_fields_file(val)
                    self.pilot_info["EPOCH"] = epochs_dict[val]
                else:
                    to_append = load_fields_file(val)
                    to_append["EPOCH"] = epochs_dict[val]
                    self.pilot_info = pd.concat(
                        [self.pilot_info, to_append],
                        sort=False
                    )

        self.field_info = self.pilot_info[
            self.pilot_info.FIELD_NAME == self.field
        ]

        self.field_info.reset_index(drop=True, inplace=True)

        self.field_info = self.field_info.filter([
            "EPOCH",
            "FIELD_NAME",
            "SBID",
            "OBS_FREQ",
            "BEAM",
            "RA_HMS",
            "DEC_DMS",
            "DATEOBS",
            "DATEEND",
            "BMAJ",
            "BMIN",
            "BPA"
        ])

        self.field_info.sort_values(by=["EPOCH", "BEAM"], inplace=True)

        self.epochs = self.field_info.EPOCH.unique()

        if psf or largest_psf or common_psf or all_psf:
            self.logger.info("Getting psf information.")
            epoch_beams = self._get_beams()

        if all_psf:
            common_beams = {}
            self.logger.info("Calculating common psfs...")
            for i in sorted(epoch_beams):
                common_beams[i] = epoch_beams[i].common_beam()

            self.logger.info("{} information:".format(self.field))

            print(tabulate(
                self.field_info,
                headers=self.field_info.columns,
                showindex=False
            ))

            table = []

            for i in sorted(epoch_beams):
                table.append([
                    self.field,
                    i,
                    common_beams[i].major.to(u.arcsec).value,
                    common_beams[i].minor.to(u.arcsec).value,
                    common_beams[i].pa.to(u.deg).value
                ])

            self.logger.info("Common psf for {}".format(self.field))

            print(tabulate(table, headers=[
                "FIELD",
                "EPOCH",
                "BMAJ (arcsec)",
                "BMIN (arcsec)",
                "BPA (degree)"
            ]))

            if save:
                common_df = pd.DataFrame(table, columns=[
                    "FIELD",
                    "EPOCH",
                    "BMAJ (arcsec)",
                    "BMIN (arcsec)",
                    "BPA (degree)"
                ])
                savename = "{}_field_info_common_psf.csv".format(self.field)
                common_df.to_csv(savename, index=False)
                self.logger.info("Saved common psf output to {}.".format(
                    savename
                ))

        else:
            self.field_info = self.field_info.filter([
                "EPOCH",
                "FIELD_NAME",
                "SBID",
                "RA_HMS",
                "DEC_DMS",
                "DATEOBS",
                "DATEEND",
            ])

            self.field_info.rename(columns={
                "RA_HMS": "RA_HMS (Beam 0)",
                "DEC_DMS": "DEC_DMS (Beam 0)",
            }, inplace=True)

            self.field_info.drop_duplicates("EPOCH", inplace=True)
            if psf:
                beams_zero = []
                for i in sorted(epoch_beams):
                    beams_zero.append(epoch_beams[i][0])

                self.field_info["BMAJ (arcsec)"] = [
                    b.major.value for b in beams_zero
                ]
                self.field_info["BMIN (arcsec)"] = [
                    b.minor.value for b in beams_zero
                ]
                self.field_info["BPA (deg)"] = [
                    b.pa.value for b in beams_zero
                ]
            if largest_psf:
                largest_beams = []
                for i in sorted(epoch_beams):
                    largest_beams.append(epoch_beams[i].largest_beam())

                self.field_info["L_BMAJ (arcsec)"] = [
                    b.major.value for b in largest_beams
                ]
                self.field_info["L_BMIN (arcsec)"] = [
                    b.minor.value for b in largest_beams
                ]
                self.field_info["L_BPA (deg)"] = [
                    b.pa.value for b in largest_beams
                ]

            elif common_psf:
                common_beams = []
                self.logger.info("Calculating common psfs...")
                for i in sorted(epoch_beams):
                    common_beams.append(epoch_beams[i].common_beam())

                self.field_info["C_BMAJ (arcsec)"] = [
                    b.major.to(u.arcsec).value for b in common_beams
                ]
                self.field_info["C_BMIN (arcsec)"] = [
                    b.minor.to(u.arcsec).value for b in common_beams
                ]
                self.field_info["C_BPA (deg)"] = [
                    b.pa.to(u.deg).value for b in common_beams
                ]

            self.logger.info("{} information:".format(self.field))

            print(tabulate(
                self.field_info,
                headers=self.field_info.columns,
                showindex=False
            ))

        if save:
            savename = "{}_field_info.csv".format(self.field)
            self.field_info.to_csv(savename, index=False)
            self.logger.info("Saved output to {}.".format(savename))

__init__(field)

Constructor method

Parameters:

Name Type Description Default
field str

Name of requested field.

required

Returns:

Type Description
None

None

Source code in vasttools/query.py
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
def __init__(self, field: str) -> None:
    """Constructor method

    Args:
        field: Name of requested field.

    Returns:
        None
    """
    self.logger = logging.getLogger('vasttools.query.FieldQuery')

    self.field = field
    self.valid = self._check_field()

run_query(psf=False, largest_psf=False, common_psf=False, all_psf=False, save=False, _pilot_info=None)

Running the field query.

Parameters:

Name Type Description Default
largest_psf bool

If true the largest psf is calculated of the field per epoch. Defaults to False.

False
common_psf bool

If true the common psf is calculated of the field per epoch. Defaults to False.

False
all_psf bool

If true the common psf is calculated of the field per epoch and all the beam information of the field is shown. Defaults to False.

False
save bool

Save the output tables to a csv file. Defaults to False.

False
_pilot_info Optional[DataFrame]

Allows for the pilot info to be provided rather than the function building it locally. If not provided then the dataframe is built. Defaults to None.

None

Returns:

Type Description
None

None

Source code in vasttools/query.py
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
def run_query(
        self,
        psf: bool = False,
        largest_psf: bool = False,
        common_psf: bool = False,
        all_psf: bool = False,
        save: bool = False,
        _pilot_info: Optional[pd.DataFrame] = None
) -> None:
    """Running the field query.

    Args:
        largest_psf: If true the largest psf is calculated
            of the field per epoch. Defaults to False.
        common_psf: If true the common psf is calculated
            of the field per epoch. Defaults to False.
        all_psf: If true the common psf is calculated of the field
            per epoch and all the beam information of
            the field is shown. Defaults to False.
        save: Save the output tables to a csv file. Defaults to False.
        _pilot_info: Allows for the pilot info to be provided
            rather than the function building it locally. If not provided
            then the dataframe is built. Defaults to None.

    Returns:
        None
    """
    if not self.valid:
        self.logger.error("Field doesn't exist.")
        return

    if _pilot_info is not None:
        self.pilot_info = _pilot_info
    else:
        self.logger.debug("Building pilot info file.")
        epochs_dict = RELEASED_EPOCHS.copy()
        epochs_dict.update(OBSERVED_EPOCHS)
        for i, val in enumerate(sorted(epochs_dict)):
            if i == 0:
                self.pilot_info = load_fields_file(val)
                self.pilot_info["EPOCH"] = epochs_dict[val]
            else:
                to_append = load_fields_file(val)
                to_append["EPOCH"] = epochs_dict[val]
                self.pilot_info = pd.concat(
                    [self.pilot_info, to_append],
                    sort=False
                )

    self.field_info = self.pilot_info[
        self.pilot_info.FIELD_NAME == self.field
    ]

    self.field_info.reset_index(drop=True, inplace=True)

    self.field_info = self.field_info.filter([
        "EPOCH",
        "FIELD_NAME",
        "SBID",
        "OBS_FREQ",
        "BEAM",
        "RA_HMS",
        "DEC_DMS",
        "DATEOBS",
        "DATEEND",
        "BMAJ",
        "BMIN",
        "BPA"
    ])

    self.field_info.sort_values(by=["EPOCH", "BEAM"], inplace=True)

    self.epochs = self.field_info.EPOCH.unique()

    if psf or largest_psf or common_psf or all_psf:
        self.logger.info("Getting psf information.")
        epoch_beams = self._get_beams()

    if all_psf:
        common_beams = {}
        self.logger.info("Calculating common psfs...")
        for i in sorted(epoch_beams):
            common_beams[i] = epoch_beams[i].common_beam()

        self.logger.info("{} information:".format(self.field))

        print(tabulate(
            self.field_info,
            headers=self.field_info.columns,
            showindex=False
        ))

        table = []

        for i in sorted(epoch_beams):
            table.append([
                self.field,
                i,
                common_beams[i].major.to(u.arcsec).value,
                common_beams[i].minor.to(u.arcsec).value,
                common_beams[i].pa.to(u.deg).value
            ])

        self.logger.info("Common psf for {}".format(self.field))

        print(tabulate(table, headers=[
            "FIELD",
            "EPOCH",
            "BMAJ (arcsec)",
            "BMIN (arcsec)",
            "BPA (degree)"
        ]))

        if save:
            common_df = pd.DataFrame(table, columns=[
                "FIELD",
                "EPOCH",
                "BMAJ (arcsec)",
                "BMIN (arcsec)",
                "BPA (degree)"
            ])
            savename = "{}_field_info_common_psf.csv".format(self.field)
            common_df.to_csv(savename, index=False)
            self.logger.info("Saved common psf output to {}.".format(
                savename
            ))

    else:
        self.field_info = self.field_info.filter([
            "EPOCH",
            "FIELD_NAME",
            "SBID",
            "RA_HMS",
            "DEC_DMS",
            "DATEOBS",
            "DATEEND",
        ])

        self.field_info.rename(columns={
            "RA_HMS": "RA_HMS (Beam 0)",
            "DEC_DMS": "DEC_DMS (Beam 0)",
        }, inplace=True)

        self.field_info.drop_duplicates("EPOCH", inplace=True)
        if psf:
            beams_zero = []
            for i in sorted(epoch_beams):
                beams_zero.append(epoch_beams[i][0])

            self.field_info["BMAJ (arcsec)"] = [
                b.major.value for b in beams_zero
            ]
            self.field_info["BMIN (arcsec)"] = [
                b.minor.value for b in beams_zero
            ]
            self.field_info["BPA (deg)"] = [
                b.pa.value for b in beams_zero
            ]
        if largest_psf:
            largest_beams = []
            for i in sorted(epoch_beams):
                largest_beams.append(epoch_beams[i].largest_beam())

            self.field_info["L_BMAJ (arcsec)"] = [
                b.major.value for b in largest_beams
            ]
            self.field_info["L_BMIN (arcsec)"] = [
                b.minor.value for b in largest_beams
            ]
            self.field_info["L_BPA (deg)"] = [
                b.pa.value for b in largest_beams
            ]

        elif common_psf:
            common_beams = []
            self.logger.info("Calculating common psfs...")
            for i in sorted(epoch_beams):
                common_beams.append(epoch_beams[i].common_beam())

            self.field_info["C_BMAJ (arcsec)"] = [
                b.major.to(u.arcsec).value for b in common_beams
            ]
            self.field_info["C_BMIN (arcsec)"] = [
                b.minor.to(u.arcsec).value for b in common_beams
            ]
            self.field_info["C_BPA (deg)"] = [
                b.pa.to(u.deg).value for b in common_beams
            ]

        self.logger.info("{} information:".format(self.field))

        print(tabulate(
            self.field_info,
            headers=self.field_info.columns,
            showindex=False
        ))

    if save:
        savename = "{}_field_info.csv".format(self.field)
        self.field_info.to_csv(savename, index=False)
        self.logger.info("Saved output to {}.".format(savename))

Query

This is a class representation of various information about a particular query including the catalogue of target sources, the Stokes parameter, crossmatch radius and output parameters.

Attributes:

Name Type Description
coords SkyCoord

The sky coordinates to be queried.

source_names List[str]

The names of the sources (coordinates) being queried.

ncpu int

The number of cpus available.

planets bool

Set to 'True' when planets are to be queried.

settings Dict

Dictionary that contains the various settings of the query. TODO: This dictionary typing needs better defining.

base_folder str

The base folder of the VAST data.

fields_found bool

Set to 'True' once 'find_fields' has been run on the query.

racs bool

Set to 'True' if RACS (Epoch 00) is included in the query.

query_df DataFrame

The dataframe that is constructed to perform the query.

sources_df DataFrame

The dataframe that contains the found sources when 'find_sources' is run.

results Series

Series that contains each result in the form of a vasttools.source.Source object, with the source name as the index.

Source code in vasttools/query.py
  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
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
class Query:
    """
    This is a class representation of various information about a particular
    query including the catalogue of target sources, the Stokes parameter,
    crossmatch radius and output parameters.

    Attributes:
        coords (astropy.coordinates.sky_coordinate.SkyCoord):
            The sky coordinates to be queried.
        source_names (List[str]):
            The names of the sources (coordinates) being queried.
        ncpu (int): The number of cpus available.
        planets (bool): Set to 'True' when planets are to be queried.
        settings (Dict):
            Dictionary that contains the various settings of the query.
            TODO: This dictionary typing needs better defining.
        base_folder (str): The base folder of the VAST data.
        fields_found (bool): Set to 'True' once 'find_fields' has been
            run on the query.
        racs (bool): Set to 'True' if RACS (Epoch 00) is included in
            the query.
        query_df (pandas.core.frame.DataFrame):
            The dataframe that is constructed to perform the query.
        sources_df (pandas.core.frame.DataFrame):
            The dataframe that contains the found sources when 'find_sources'
            is run.
        results (pandas.core.frame.Series):
            Series that contains each result in the form of a
            vasttools.source.Source object, with the source name
            as the index.
    """

    def __init__(
        self,
        coords: Optional[SkyCoord] = None,
        source_names: Optional[List[str]] = None,
        epochs: Union[str, List[str], List[int]] = "1",
        stokes: str = "I",
        crossmatch_radius: float = 5.0,
        max_sep: float = 1.5,
        use_tiles: bool = True,
        use_islands: bool = False,
        base_folder: Optional[str] = None,
        matches_only: bool = False,
        no_rms: bool = False,
        search_around_coordinates: bool = False,
        output_dir: str = ".",
        planets: Optional[List[str]] = None,
        ncpu: int = 2,
        sort_output: bool = False,
        forced_fits: bool = False,
        forced_cluster_threshold: float = 1.5,
        forced_allow_nan: bool = False,
        incl_observed: bool = False,
        corrected_data: bool = False,
        post_processed_data: bool = True,
        search_all_fields: bool = False,
        scheduler: str = 'processes',
    ) -> None:
        """
        Constructor method.

        Args:
            coords: List of coordinates to query, defaults to None.
            source_names: List of source names, defaults to None.
            epochs: Epochs to query. Can be specified with either a list
                or a comma-separated string. All available epochs can be
                queried by passing "all", and all available VAST epochs can be
                queried by passing "all-vast". Defaults to "1".
            stokes: Stokes parameter to query.
            crossmatch_radius: Crossmatch radius in arcsec, defaults to 5.0.
            max_sep: Maximum separation of source from beam centre
                in degrees, defaults to 1.5.
            use_tiles: Query tiles rather than combined mosaics,
                defaults to `False`.
            use_islands: Use selavy islands rather than components,
                defaults to `False`.
            base_folder: Path to base folder if using default directory
                structure, defaults to 'None'.
            matches_only: Only produce data products for sources with a
                selavy match, defaults to `False`.
            no_rms: When set to `True` the estimate of the background RMS
                around each source, will not be performed,
                defaults to `False`.
            search_around_coordinates: When set to `True`, all matches to a
                searched coordinate are returned, instead of only the closest
                match.
            output_dir: Output directory to place all results in,
                defaults to ".".
            planets: List of planets to search for, defaults to None.
            ncpu: Number of CPUs to use, defaults to 2.
            sort_output: Sorts the output into individual source
                directories, defaults to `False`.
            forced_fits: Turns on the option to perform forced fits
                on the locations queried, defaults to `False`.
            forced_cluster_threshold: The cluster_threshold value passed to
                the forced photometry. Beam width distance limit for which a
                cluster is formed for extraction, defaults to 3.0.
            forced_allow_nan: `allow_nan` value passed to the
                forced photometry. If False then any cluster containing a
                NaN is ignored. Defaults to False.
            incl_observed: Include epochs that have been observed, but not
                released, in the query. This should only be used when finding
                fields, not querying data. Defaults to False.
            corrected_data: Access the corrected data. Only relevant if
                `tiles` is `True`. Defaults to `False`.
            post_processed_data: Access the post-processed data. Only relevant
                if `tiles` is `True`. Defaults to `True`.
            search_all_fields: If `True`, return all data at the requested
                positions regardless of field. If `False`, only return data
                from the best (closest) field in each epoch.
            scheduler: Dask scheduling option to use. Options are "processes"
                (parallel processing) or "single-threaded". Defaults to
                "processes".

        Returns:
            None

        Raises:
            ValueError: If the number of CPUs requested exceeds the total
                available.
            QueryInitError: No coordinates or source names have been provided.
            QueryInitError: Forced fits and search around coordinates options
                have both been selected.
            QueryInitError: Number of source names provided does not match the
                number of coordinates.
            ValueError: Planet provided is not a valid planet.
            QueryInitError: Base folder could not be determined.
            QueryInitError: SIMBAD search failed.
            QueryInitError: Base folder cannot be found.
            QueryInitError: Base folder cannot be found.
            QueryInitError: Problems found in query settings.
            QueryInitError: Invalid scheduler option requested.
        """
        self.logger = logging.getLogger('vasttools.find_sources.Query')

        install_mp_handler(logger=self.logger)

        if source_names is None:
            source_names = []
        if planets is None:
            planets = []

        self.source_names = np.array(source_names)
        self.simbad_names = None

        self.corrected_data = corrected_data
        self.post_processed_data = post_processed_data

        if coords is None:
            self.coords = coords
        elif coords.isscalar:
            self.coords = SkyCoord([coords.ra], [coords.dec])
        else:
            self.coords = coords

        if self.coords is None:
            len_coords = 0
        else:
            len_coords = self.coords.shape[0]

        if ncpu > HOST_NCPU:
            raise ValueError(
                "Number of CPUs requested ({}) "
                "exceeds number available ({})".format(
                    ncpu,
                    HOST_NCPU
                )
            )
        self.ncpu = ncpu
        self.logger.debug(f"Using {self.ncpu} CPUs")

        if coords is None and len(source_names) == 0 and len(planets) == 0:
            raise QueryInitError(
                "No coordinates or source names have been provided!"
                " Check inputs and try again!"
            )

        if forced_fits and search_around_coordinates:
            raise QueryInitError(
                "Forced fits and search around coordinates mode cannot be"
                " used together! Check inputs and try again."
            )

        if (
            self.coords is not None and
            len(self.source_names) > 0 and
            len(self.source_names) != len_coords
        ):
            raise QueryInitError(
                "The number of entered source names ({}) does not match the"
                " number of coordinates ({})!".format(
                    len(self.source_names),
                    len_coords
                )
            )

        if self.coords is not None and len(self.source_names) == 0:
            source_names = [
                'source_' + i.to_string(
                    'hmsdms', sep="", precision=1
                ).replace(" ", "") for i in self.coords
            ]
            self.source_names = np.array(source_names)

        if self.coords is None:
            if len(source_names) != 0:
                num_sources = len(source_names)
                self.coords, self.simbad_names = simbad_search(
                    source_names, logger=self.logger
                )
                num_simbad = len(list(filter(None, self.simbad_names)))
                if self.coords is not None:
                    simbad_msg = "SIMBAD search found {}/{} source(s):".format(
                        num_simbad,
                        num_sources
                    )
                    self.logger.info(simbad_msg)
                    names = zip(self.simbad_names, self.source_names)
                    for simbad_name, query_name in names:
                        if simbad_name:
                            self.logger.info(
                                '{}: {}'.format(query_name, simbad_name)
                            )
                        else:
                            self.logger.info(
                                '{}: No match.'.format(query_name)
                            )
                    if self.logger is None:
                        warnings.warn(simbad_msg)
                else:
                    self.logger.error(
                        "SIMBAD search failed!"
                    )
                    raise QueryInitError(
                        "SIMBAD search failed!"
                    )

        planets = [i.lower() for i in planets]
        valid_planets = sum([i in ALLOWED_PLANETS for i in planets])

        if valid_planets != len(planets):
            self.logger.error(
                "Invalid planet object provided!"
            )
            raise ValueError(
                "Invalid planet object provided!"
            )

        self.planets = planets

        self.settings = {}

        if base_folder is None:
            the_base_folder = os.getenv('VAST_DATA_DIR')
            if the_base_folder is None:
                raise QueryInitError(
                    "The base folder directory could not be determined!"
                    " Either the system environment 'VAST_DATA_DIR' must be"
                    " defined or the 'base_folder' argument defined when"
                    " initialising the query."
                )
        else:
            the_base_folder = os.path.abspath(str(base_folder))

        if not os.path.isdir(the_base_folder):
            raise QueryInitError(
                "Base folder {} not found!".format(
                    the_base_folder
                )
            )

        self.base_folder = the_base_folder

        self.settings['incl_observed'] = incl_observed
        self.settings['epochs'] = self._get_epochs(epochs)
        self.settings['stokes'] = self._get_stokes(stokes)

        self.settings['crossmatch_radius'] = Angle(
            crossmatch_radius, unit=u.arcsec
        )
        self.settings['max_sep'] = max_sep

        self.settings['islands'] = use_islands
        self.settings['tiles'] = use_tiles
        self.settings['no_rms'] = no_rms
        self.settings['matches_only'] = matches_only
        self.settings['search_around'] = search_around_coordinates
        self.settings['sort_output'] = sort_output
        self.settings['forced_fits'] = forced_fits
        self.settings[
            'forced_cluster_threshold'
        ] = forced_cluster_threshold
        self.settings['forced_allow_nan'] = forced_allow_nan

        self.settings['output_dir'] = output_dir
        self.settings['search_all_fields'] = search_all_fields

        scheduler_options = ['processes', 'single-threaded']
        if scheduler not in scheduler_options:
            raise QueryInitError(
                f"{scheduler} is not a suitable scheduler option. Please "
                f"select from {scheduler_options}"
            )
        self.settings['scheduler'] = scheduler

        # Going to need this so load it now
        self._epoch_fields = get_fields_per_epoch_info()

        if not os.path.isdir(self.base_folder):
            self.logger.critical(
                "The base directory {} does not exist!".format(
                    self.base_folder
                )
            )
            raise QueryInitError(
                "The base directory {} does not exist!".format(
                    self.base_folder
                )
            )

        settings_ok = self._validate_settings()

        if not settings_ok:
            self.logger.critical("Problems found in query settings!")
            self.logger.critical("Please address and try again.")
            raise QueryInitError((
                "Problems found in query settings!"
                "\nPlease address and try again."
            ))

        all_data_available = self._check_data_availability()
        if all_data_available:
            self.logger.info("All data available!")
        else:
            self.logger.warning(
                "Not all requested data is available! See above for details."
            )
            self.logger.warning(
                "Query will continue run, but proceed with caution."
            )

        if self.coords is not None:
            self.query_df = self._build_catalog()
            if self.query_df.empty:
                raise QueryInitError(
                    'No sources remaining. None of the entered coordinates'
                    ' are found in the VAST Pilot survey footprint!'
                )
        else:
            self.query_df = None

        self.fields_found = False

    def _validate_settings(self) -> bool:
        """
        Used to check that the settings are valid.

        Returns:
            `True` if settings are acceptable, `False` otherwise.
        """

        self.logger.debug("Using settings: ")
        self.logger.debug(self.settings)

        if self.settings['tiles']:
            if self.post_processed_data:
                self.logger.debug("Using post-processed TILES data...")
            elif self.corrected_data:
                self.logger.warning(
                    "Using corrected TILES data - this should only be "
                    "selected with good reason! Otherwise, use the default!"
                )
            else:
                self.logger.warning(
                    "Using raw TILES data - this should only be "
                    "selected with good reason! Otherwise, use the default!"
                )

        else:
            self.logger.debug("Using COMBINED data")
        if self.settings['tiles'] and self.settings['stokes'].lower() != "i":
            if self.vast_full:
                self.logger.warning("Stokes V tiles are only available for the"
                                    " full VAST survey. Proceed with caution!"
                                    )
            else:
                self.logger.critical("Stokes V tiles are only available for "
                                     "the full VAST survey."
                                     )
                return False

        if self.settings['tiles'] and self.settings['islands']:
            if self.vast_p1 or self.vast_p2 or self.racs:
                self.logger.critical(
                    "Only component catalogues are supported with tiles "
                    "for the selected epochs."
                )
                return False

        if self.settings['islands']:
            self.logger.warning(
                "Image RMS and peak flux error are not available with islands."
                "Using background_noise as a placeholder for both."
            )

        if self.vast_full and not self.settings['tiles']:
            self.logger.critical("COMBINED images are not available for "
                                 "the full VAST survey. Query will continue "
                                 "to run, but proceed with caution."
                                 )

        if self.settings['tiles'] and self.corrected_data and self.vast_full:
            self.logger.critical(
                "Corrected data does not exist for the full VAST survey."
                "Pass corrected_data=False and post_processed_data=True "
                "to access full survey data. Query will continue to run, "
                "but proceed with caution."
            )

        # TO DO: Maybe add some setting validation for self.post_processed_data
        if self.corrected_data and self.post_processed_data:
            self.logger.critical(
                "Only one of corrected_data and post-processed data can be "
                "selected."
            )
            return False

        return True

    def _check_data_availability(self) -> bool:
        """
        Used to check that the requested data is available.

        Returns:
            `True` if all data is available, `False` otherwise.
        """

        all_available = True

        base_dir = Path(self.base_folder)

        data_type = "COMBINED"
        corrected_str = ""

        if self.settings['tiles']:
            data_type = "TILES"
            if self.corrected_data:
                corrected_str = "_CORRECTED"
            if self.post_processed_data:
                corrected_str = "_PROCESSED"

        stokes = self.settings['stokes']

        self.logger.info("Checking data availability...")

        for epoch in self.settings['epochs']:
            epoch_dir = base_dir / "EPOCH{}".format(RELEASED_EPOCHS[epoch])
            if not epoch_dir.is_dir():
                self.logger.critical(f"Epoch {epoch} is unavailable.")
                self.logger.debug(f"{epoch_dir} does not exist.")
                all_available = False
                continue

            data_dir = epoch_dir / data_type
            if not data_dir.is_dir():
                self.logger.critical(
                    f"{data_type} data unavailable for epoch {epoch}"
                )
                self.logger.debug(f"{data_dir} does not exist.")
                all_available = False
                continue

            image_dir = data_dir / f"STOKES{stokes}_IMAGES{corrected_str}"
            if not image_dir.is_dir():
                self.logger.critical(
                    f"Stokes {stokes} images unavailable for epoch {epoch}"
                )
                self.logger.debug(f"{image_dir} does not exist.")
                all_available = False

            selavy_dir = data_dir / f"STOKES{stokes}_SELAVY{corrected_str}"
            if not selavy_dir.is_dir():
                self.logger.critical(
                    f"Stokes {stokes} catalogues unavailable for epoch {epoch}"
                )
                self.logger.debug(f"{selavy_dir} does not exist.")
                all_available = False

            rms_dir = data_dir / f"STOKES{stokes}_RMSMAPS{corrected_str}"
            if not rms_dir.is_dir() and not self.settings["no_rms"]:
                self.logger.critical(
                    f"Stokes {stokes} RMS maps unavailable for epoch {epoch}"
                )
                self.logger.debug(f"{rms_dir} does not exist.")
                all_available = False

        if all_available:
            self.logger.info("All requested data is available!")

        return all_available

    def _get_all_cutout_data(self,
                             imsize: Angle,
                             img: bool = True,
                             rms: bool = False,
                             bkg: bool = False
                             ) -> pd.DataFrame:
        """
        Get cutout data and selavy components for all sources.

        Args:
            imsize: Size of the requested cutout.
            img: Fetch image data, defaults to `True`.
            rms: Fetch rms data, defaults to `False`.
            bkg: Fetch bkg data, defaults to `False`.

        Returns:
            Dataframe containing the cutout data of all measurements in
            the query. Cutout data specifically is the image data, header,
            wcs, and selavy sources present in the cutout.

        Raises:
            Exception: Function cannot be run when 'search_around_coordinates'
                mode has been selected.
        """
        # first get cutout data and selavy sources per image
        # group by image to do this

        if self.settings['search_around']:
            raise Exception(
                'Getting cutout data cannot be run when'
                ' search around coordinates mode has been'
                ' used.'
            )

        meta = {
            'data': 'O',
            'wcs': 'O',
            'header': 'O',
            'selavy_overlay': 'O',
            'beam': 'O',
            'rms_data': 'O',
            'rms_wcs': 'O',
            'rms_header': 'O',
            'bkg_data': 'O',
            'bkg_wcs': 'O',
            'bkg_header': 'O',
            'name': 'U',
            'dateobs': 'datetime64[ns]',
        }

        cutouts = (
            dd.from_pandas(self.sources_df, self.ncpu)
            .groupby('image')
            .apply(
                self._grouped_fetch_cutouts,
                imsize=imsize,
                meta=meta,
                img=img,
                rms=rms,
                bkg=bkg,
            ).compute(num_workers=self.ncpu,
                      scheduler=self.settings['scheduler']
                      )
        )

        if not cutouts.empty:
            if isinstance(cutouts.index, pd.MultiIndex):
                cutouts.index = cutouts.index.droplevel()

        return cutouts

    def _gen_all_source_products(
        self,
        fits: bool = True,
        rms: bool = False,
        bkg: bool = False,
        png: bool = False,
        ann: bool = False,
        reg: bool = False,
        lightcurve: bool = False,
        measurements: bool = False,
        fits_outfile: Optional[str] = None,
        png_selavy: bool = True,
        png_percentile: float = 99.9,
        png_zscale: bool = False,
        png_contrast: float = 0.2,
        png_no_islands: bool = True,
        png_no_colorbar: bool = False,
        png_crossmatch_overlay: bool = False,
        png_hide_beam: bool = False,
        png_disable_autoscaling: bool = False,
        png_offset_axes: bool = True,
        ann_crossmatch_overlay: bool = False,
        reg_crossmatch_overlay: bool = False,
        lc_sigma_thresh: int = 5,
        lc_figsize: Tuple[int, int] = (8, 4),
        lc_min_points: int = 2,
        lc_min_detections: int = 1,
        lc_mjd: bool = False,
        lc_start_date: Optional[pd.Timestamp] = None,
        lc_grid: bool = False,
        lc_yaxis_start: str = "auto",
        lc_peak_flux: bool = True,
        lc_use_forced_for_limits: bool = False,
        lc_use_forced_for_all: bool = False,
        lc_hide_legend: bool = False,
        measurements_simple: bool = False,
        imsize: Angle = Angle(5. * u.arcmin),
        plot_dpi: int = 150
    ) -> None:
        """
        Generate products for all sources.
        This function is not intended to be used interactively - script only.

        Args:
            fits: Create and save fits cutouts, defaults to `True`.
            rms: Create and save fits cutouts of the rms images,
                defaults to `True`.
            bkg: Create and save fits cutouts of the bkg images,
                defaults to `True`.
            png: Create and save png postagestamps, defaults to `False`.
            ann: Create and save kvis annotation files for all components,
                defaults to `False`
            reg: Create and save DS9 annotation files for all components,
                defaults to `False`
            lightcurve: Create and save lightcurves for all sources,
                defaults to `False`
            measurements: Create and save measurements for all sources,
                defaults to `False`
            fits_outfile: File to save fits cutout to, defaults to None.
            png_selavy: Overlay selavy components onto png postagestamp,
                defaults to `True`
            png_percentile: Percentile level for the png normalisation,
                defaults to 99.9.
            png_zscale: Use z-scale normalisation rather than linear,
                defaults to `False`.
            png_contrast: Z-scale constrast, defaults to 0.2.
            png_no_islands: Don't overlay selavy islands on png
                postagestamps, defaults to `True`.
            png_no_colorbar: Don't include colourbar on png output,
                defaults to `False`
            png_crossmatch_overlay: Overlay the crossmatch radius on png
                postagestamps, defaults to `False`.
            png_hide_beam: Do not show the beam shape on png postagestamps,
                defaults to `False`.
            png_disable_autoscaling: Disable autoscaling.
            png_offset_axes: Use offset, rather than absolute, axis labels.
            ann_crossmatch_overlay: Include crossmatch radius in ann,
                defaults to `False`.
            reg_crossmatch_overlay: Include crossmatch radius in reg,
                defaults to `False`.
            lc_sigma_thresh: Detection threshold (in sigma) for
                lightcurves, defaults to 5.
            lc_figsize: Size of lightcurve figure, defaults to (8, 4).
            lc_min_points: Minimum number of source observations required
                for a lightcurve to be generated, defaults to 2.
            lc_min_detections: Minimum number of source detections required
                for a lightcurve to be generated, defaults to 1.
            lc_mjd: Use MJD for lightcurve x-axis, defaults to `False`.
            lc_start_date: Plot lightcurve in days from start date,
                defaults to None.
            lc_grid: Include grid on lightcurve plot, defaults to `False`.
            lc_yaxis_start: Start the lightcurve y-axis at 0 ('0') or use
                the matpotlib default ('auto'). Defaults to 'auto'.
            lc_peak_flux: Generate lightcurve using peak flux density
                rather than integrated flux density, defaults to `True`.
            measurements_simple: Use simple schema for measurement output,
                defaults to `False`.
            imsize: Size of the requested cutout.
            plot_dpi: Specify the DPI of saved figures, defaults to 150.

        Returns:
            None

        Raises:
            Exception: Function cannot be run when 'search_around_coordinates'
                option has been selected.
        """
        if self.settings['search_around']:
            raise Exception(
                'Getting source products cannot be run when'
                ' search around coordinates mode has been'
                ' used.'
            )

        if sum([fits, rms, bkg, png, ann, reg]) > 0:
            self.logger.info(
                "Fetching cutout data for sources..."
            )
            cutouts_df = self._get_all_cutout_data(imsize,
                                                   img=fits,
                                                   rms=rms,
                                                   bkg=bkg,
                                                   )
            self.logger.info('Done.')
            if cutouts_df.empty:
                fits = False
                png = False
                self.logger.warning(
                    'Cutout data could not be fetched, turning off fits and'
                    ' png production.'
                )
                to_process = [(s, None) for s in self.results.values]
                cutouts_df = None
            else:
                to_process = [(s, cutouts_df.loc[
                    cutouts_df['name'] == s.name
                ].sort_values(
                    by='dateobs'
                ).reset_index()) for s in self.results.values]

                del cutouts_df
                gc.collect()
        else:
            to_process = [(s, None) for s in self.results.values]
            cutouts_df = None

        self.logger.info(
            'Saving source products, please be paitent for large queries...'
        )

        produce_source_products_multi = partial(
            self._produce_source_products,
            fits=fits,
            rms=rms,
            bkg=bkg,
            png=png,
            ann=ann,
            reg=reg,
            lightcurve=lightcurve,
            measurements=measurements,
            png_selavy=png_selavy,
            png_percentile=png_percentile,
            png_zscale=png_zscale,
            png_contrast=png_contrast,
            png_no_islands=png_no_islands,
            png_no_colorbar=png_no_colorbar,
            png_crossmatch_overlay=png_crossmatch_overlay,
            png_hide_beam=png_hide_beam,
            png_disable_autoscaling=png_disable_autoscaling,
            png_offset_axes=png_offset_axes,
            ann_crossmatch_overlay=ann_crossmatch_overlay,
            reg_crossmatch_overlay=reg_crossmatch_overlay,
            lc_sigma_thresh=lc_sigma_thresh,
            lc_figsize=lc_figsize,
            lc_min_points=lc_min_points,
            lc_min_detections=lc_min_detections,
            lc_mjd=lc_mjd,
            lc_start_date=lc_start_date,
            lc_grid=lc_grid,
            lc_yaxis_start=lc_yaxis_start,
            lc_peak_flux=lc_peak_flux,
            lc_use_forced_for_limits=lc_use_forced_for_limits,
            lc_use_forced_for_all=lc_use_forced_for_all,
            lc_hide_legend=lc_hide_legend,
            measurements_simple=measurements_simple,
            calc_script_norms=~(png_disable_autoscaling),
            plot_dpi=plot_dpi
        )

        if self.settings['scheduler'] == 'processes':
            original_sigint_handler = signal.signal(
                signal.SIGINT, signal.SIG_IGN
            )
            signal.signal(signal.SIGINT, original_sigint_handler)
            workers = Pool(processes=self.ncpu, maxtasksperchild=100)

            try:
                workers.map(
                    produce_source_products_multi,
                    to_process,
                )
            except KeyboardInterrupt:
                self.logger.error(
                    "Caught KeyboardInterrupt, terminating workers."
                )
                workers.terminate()
                sys.exit()
            except Exception as e:
                self.logger.error(
                    "Encountered error!."
                )
                self.logger.error(
                    e
                )
                workers.terminate()
                sys.exit()
            finally:
                self.logger.debug("Closing workers.")
                # Using terminate below as close was prone to hanging
                # when join is called. I believe the hang comes from
                # a child processes not getting returned properly
                # because of the large number of file I/O.
                workers.terminate()
                workers.join()
        elif self.settings['scheduler'] == 'single-threaded' or self.ncpu == 1:
            for result in map(produce_source_products_multi, to_process):
                pass

    def _produce_source_products(
        self,
        i: Tuple[Source, pd.DataFrame],
        fits: bool = True,
        rms: bool = False,
        bkg: bool = False,
        png: bool = False,
        ann: bool = False,
        reg: bool = False,
        lightcurve: bool = False,
        measurements: bool = False,
        png_selavy: bool = True,
        png_percentile: float = 99.9,
        png_zscale: bool = False,
        png_contrast: float = 0.2,
        png_no_islands: bool = True,
        png_no_colorbar: bool = False,
        png_crossmatch_overlay: bool = False,
        png_hide_beam: bool = False,
        png_disable_autoscaling: bool = False,
        png_offset_axes: bool = True,
        ann_crossmatch_overlay: bool = False,
        reg_crossmatch_overlay: bool = False,
        lc_sigma_thresh: int = 5,
        lc_figsize: Tuple[int, int] = (8, 4),
        lc_min_points: int = 2,
        lc_min_detections: int = 1,
        lc_mjd: bool = False,
        lc_start_date: Optional[pd.Timestamp] = None,
        lc_grid: bool = False,
        lc_yaxis_start: str = "auto",
        lc_peak_flux: bool = True,
        lc_use_forced_for_limits: bool = False,
        lc_use_forced_for_all: bool = False,
        lc_hide_legend: bool = False,
        measurements_simple: bool = False,
        calc_script_norms: bool = False,
        plot_dpi: int = 150
    ) -> None:
        """
        Produce source products for one source.

        Args:
            i: Tuple containing source and cutout data.
            fits: Create and save fits cutouts, defaults to `True`.
            rms: Create and save fits cutouts of the rms images,
                defaults to `True`.
            bkg: Create and save fits cutouts of the bkg images,
                defaults to `True`.
            png: Create and save png postagestamps, defaults to `False`.
            ann: Create and save kvis annotation files for all components,
                defaults to `False`.
            reg: Create and save DS9 annotation files for all components,
                defaults to `False`.
            lightcurve: Create and save lightcurves for all sources,
                defaults to `False`.
            measurements: Create and save measurements for all sources,
                defaults to `False`.
            png_selavy: Overlay selavy components onto png postagestamp,
                defaults to `True`.
            png_percentile: Percentile level for the png normalisation,
                defaults to 99.9.
            png_zscale: Use z-scale normalisation rather than linear,
                defaults to `False`.
            png_contrast: Z-scale constrast, defaults to 0.2.
            png_no_islands: Don't overlay selavy islands on png
                postagestamps, defaults to `True`.
            png_no_colorbar: Don't include colourbar on png output,
                defaults to `False`.
            png_crossmatch_overlay: Overlay the crossmatch radius on png
                postagestamps, defaults to `False`.
            png_hide_beam: Do not show the beam shape on png postagestamps,\
                defaults to `False`.
            png_disable_autoscaling: Disable autoscaling.
            png_offset_axes: Use offset, rather than absolute, axis labels.
            ann_crossmatch_overlay: Include crossmatch radius in ann,
                defaults to `False`.
            reg_crossmatch_overlay: Include crossmatch radius in reg,
                defaults to `False`.
            lc_sigma_thresh: Detection threshold (in sigma) for
                lightcurves, defaults to 5.
            lc_figsize: Size of lightcurve figure, defaults to (8, 4).
            lc_min_points: Minimum number of source observations required
                for a lightcurve to be generated, defaults to 2.
            lc_min_detections: Minimum number of source detections required
                for a lightcurve to be generated, defaults to 1.
            lc_mjd: Use MJD for lightcurve x-axis, defaults to `False`.
            lc_start_date: Plot lightcurve in days from start date,
                defaults to None.
            lc_grid: Include grid on lightcurve plot, defaults to `False`.
            lc_yaxis_start: Start the lightcurve y-axis at 0 ('0') or use
                the matpotlib default ('auto'). Defaults to 'auto'.
            lc_peak_flux: Generate lightcurve using peak flux density
                rather than integrated flux density, defaults to `True`.
            lc_use_forced_for_limits: Generate lightcurves using forced
                photometry for non-detections only.
            lc_use_forced_for_all: Generate lightcurves using forced
                photometry for all measurements.
            measurements_simple: Use simple schema for measurement output,
                defaults to `False`.
            calc_script_norms: Calculate the png normalisation if it
                hasn't been already.
            plot_dpi: Specify the DPI of saved figures, defaults to 150.

        Returns:
            None
        """

        source, cutout_data = i

        self.logger.debug(f"Producing source products for {source.name}")

        if fits:
            source.save_all_fits_cutouts(cutout_data=cutout_data)
        if sum([rms, bkg]) > 1:
            source._save_all_noisemap_cutouts(cutout_data, rms=rms, bkg=bkg)

        if png:
            source.save_all_png_cutouts(
                selavy=png_selavy,
                percentile=png_percentile,
                zscale=png_zscale,
                contrast=png_contrast,
                no_islands=png_no_islands,
                no_colorbar=png_no_colorbar,
                crossmatch_overlay=png_crossmatch_overlay,
                hide_beam=png_hide_beam,
                disable_autoscaling=png_disable_autoscaling,
                cutout_data=cutout_data,
                calc_script_norms=calc_script_norms,
                plot_dpi=plot_dpi,
                offset_axes=png_offset_axes
            )

        if ann:
            source.save_all_ann(
                crossmatch_overlay=ann_crossmatch_overlay,
                cutout_data=cutout_data
            )

        if reg:
            source.save_all_reg(
                crossmatch_overlay=reg_crossmatch_overlay,
                cutout_data=cutout_data
            )

        if lightcurve:
            source.plot_lightcurve(
                sigma_thresh=lc_sigma_thresh,
                figsize=lc_figsize,
                min_points=lc_min_points,
                min_detections=lc_min_detections,
                mjd=lc_mjd,
                start_date=lc_start_date,
                grid=lc_grid,
                yaxis_start=lc_yaxis_start,
                peak_flux=lc_peak_flux,
                save=True,
                outfile=None,
                use_forced_for_limits=lc_use_forced_for_limits,
                use_forced_for_all=lc_use_forced_for_all,
                hide_legend=lc_hide_legend,
                plot_dpi=plot_dpi
            )

        if measurements:
            source.write_measurements(simple=measurements_simple)

        # attempt to avoid join hangs
        time.sleep(0.2)

        return

    def _summary_log(self) -> None:
        """
        Prints a summary log.

        Returns:
            None
        """
        self.logger.info("-------------------------")
        self.logger.info("Summary:")
        self.logger.info("-------------------------")
        self.logger.info(
            "Number of sources within footprint: %i",
            self.num_sources_searched
        )
        try:
            self.logger.info(
                "Number of sources with detections: %i",
                self.num_sources_detected
            )
        except Exception as e:
            # Means that find sources has not been run
            pass
        self.logger.info("-------------------------")

    def _add_source_cutout_data(self, s: Source) -> Source:
        """
        Add cutout data to the source of interest.

        Args:
            s: Source of interest.

        Returns:
            Updated source of interest containing the cutout data.
        """
        s_name = s.name
        s_cutout = self.sources_df[[
            'data',
            'wcs',
            'header',
            'selavy_overlay',
            'beam'
        ]][self.sources_df.name == s_name].reset_index(drop=True)

        s.cutout_df = s_cutout
        s._cutouts_got = True

        del s_cutout

        return s

    def _grouped_fetch_cutouts(
        self,
        group: pd.DataFrame,
        imsize: Angle,
        img: bool = True,
        rms: bool = False,
        bkg: bool = False
    ) -> pd.DataFrame:
        """
        Function that handles fetching the cutout data per
        group object, where the requested sources have been
        grouped by image.

        Args:
            group: Catalogue of sources grouped by field.
            imsize: Size of the requested cutout.
            img: Fetch image data, defaults to `True`.
            rms: Fetch rms data, defaults to `False`.
            bkg: Fetch bkg data, defaults to `False`.

        Returns:
            Dataframe containing the cutout data for the group.
        """
        image_file = group.iloc[0]['image']
        self.logger.debug(f"Fetching cutouts from {image_file}")

        try:
            image = Image(
                group.iloc[0].field,
                group.iloc[0].epoch,
                self.settings['stokes'],
                self.base_folder,
                sbid=group.iloc[0].sbid,
                tiles=self.settings['tiles'],
                corrected_data=self.corrected_data,
                post_processed_data=self.post_processed_data,
            )

            if img:
                image.get_img_data()
                img_cutout_data = group.apply(
                    self._get_cutout,
                    args=(image, imsize),
                    axis=1,
                    result_type='expand'
                ).rename(columns={
                    0: "data",
                    1: "wcs",
                    2: "header",
                    3: "selavy_overlay",
                    4: "beam"
                })
            else:
                img_cutout_data = pd.DataFrame([[None] * 5] * len(group),
                                               columns=[
                    'data',
                    'wcs',
                    'header',
                    'selavy_overlay',
                    'beam',
                ]
                )
            if rms:
                image.get_rms_img()
                rms_cutout_data = group.apply(
                    self._get_cutout,
                    args=(image, imsize),
                    img=False,
                    rms=True,
                    axis=1,
                    result_type='expand'
                ).rename(columns={
                    0: "rms_data",
                    1: "rms_wcs",
                    2: "rms_header",
                }).drop(columns=[3, 4])
            else:
                rms_cutout_data = pd.DataFrame([[None] * 3] * len(group),
                                               columns=[
                    'rms_data',
                    'rms_wcs',
                    'rms_header',
                ]
                )

            if bkg:
                image.get_bkg_img()
                bkg_cutout_data = group.apply(
                    self._get_cutout,
                    args=(image, imsize),
                    img=False,
                    bkg=True,
                    axis=1,
                    result_type='expand'
                ).rename(columns={
                    0: "bkg_data",
                    1: "bkg_wcs",
                    2: "bkg_header",
                }).drop(columns=[3, 4])
            else:
                bkg_cutout_data = pd.DataFrame([[None] * 3] * len(group),
                                               columns=[
                    'bkg_data',
                    'bkg_wcs',
                    'bkg_header',
                ]
                )

            self.logger.debug("Generated all cutout data")

            to_concat = [img_cutout_data, rms_cutout_data, bkg_cutout_data]
            cutout_data = pd.concat(to_concat, axis=1).dropna(how='all')

            self.logger.debug("Concatenated into cutout_data")

            if bkg or rms:
                bkg_values = bkg_cutout_data['bkg_data'].values
                rms_values = rms_cutout_data['rms_data'].values
                if bkg_values == rms_values:
                    self.logger.warning("Bkg and RMS data are identical!")

            self.logger.debug(cutout_data.columns)
            self.logger.debug(len(cutout_data))
            self.logger.debug(group['name'].values)

            cutout_data['name'] = group['name'].values
            self.logger.debug(cutout_data['name'])
            cutout_data['dateobs'] = group['dateobs'].values
            self.logger.debug(cutout_data['dateobs'])

            del image
        except Exception as e:
            self.logger.warning(
                "Caught exception inside _grouped_fetch_cutouts")
            self.logger.warning(e)
            cutout_data = pd.DataFrame(columns=[
                'data',
                'wcs',
                'header',
                'selavy_overlay',
                'beam',
                'name',
                'dateobs',
                'rms_data',
                'rms_wcs',
                'rms_header',
                'bkg_data',
                'bkg_wcs',
                'bkg_header',
            ])

        return cutout_data

    def _get_cutout(
        self,
        row: pd.Series,
        image: Image,
        size: Angle = Angle(5. * u.arcmin),
        img: bool = True,
        rms: bool = False,
        bkg: bool = False
    ) -> Tuple[pd.DataFrame, WCS, fits.Header, pd.DataFrame, Beam]:
        """
        Create cutout centered on a source location

        Args:
            row: Row of query catalogue corresponding to the source of
                interest
            image: Image to create cutout from.
            size: Size of the cutout, defaults to Angle(5.*u.arcmin).
            img: Make a cutout from the image data, defaults to `True`.
            rms: Make a cutout from the rms data, defaults to `False`.
            bkg: Make a cutout from the bkg data, defaults to `False`.

        Returns:
            Tuple containing cutout data, WCS, image header, associated
            selavy components and beam information.

        Raises:
            ValueError: Exactly one of img, rms or bkg must be `True`
        """

        if sum([img, rms, bkg]) != 1:
            raise ValueError("Exactly one of img, rms or bkg must be True")

        if img:
            thedata = image.data
            thewcs = image.wcs
            theheader = image.header.copy()
            thepath = image.imgpath
        elif rms:
            thedata = image.rms_data
            thewcs = image.rms_wcs
            theheader = image.rms_header.copy()
            thepath = image.rmspath
        elif bkg:
            thedata = image.bkg_data
            thewcs = image.bkg_wcs
            theheader = image.bkg_header.copy()
            thepath = image.bkgpath

        self.logger.debug(f"Using data from {thepath}")

        try:
            cutout = Cutout2D(
                thedata,
                position=row.skycoord,
                size=size,
                wcs=thewcs
            )
        except NoOverlapError:
            self.logger.warning(f"Unable to create cutout for {row['name']}.")
            self.logger.warning(f"Image path: {thepath}")
            self.logger.warning(f"Coordinate: {row.skycoord.to_string()}")
            return (None, None, None, None, None)

        if img:
            selavy_components = read_selavy(row.selavy, cols=[
                'island_id',
                'ra_deg_cont',
                'dec_deg_cont',
                'maj_axis',
                'min_axis',
                'pos_ang'
            ])

            selavy_coords = SkyCoord(
                selavy_components.ra_deg_cont.values,
                selavy_components.dec_deg_cont.values,
                unit=(u.deg, u.deg)
            )

            selavy_components = filter_selavy_components(
                selavy_components,
                selavy_coords,
                size,
                row.skycoord
            )

            del selavy_coords

            beam = image.beam
        else:
            beam = None
            selavy_components = None

        theheader.update(cutout.wcs.to_header())

        return (
            cutout.data, cutout.wcs, theheader, selavy_components, beam
        )

    def find_sources(self) -> None:
        """
        Run source search. Results are stored in attributes.

        Steps:
        1. Run find_fields if not already run.
        2. Add the file paths to each measurement point.
        3. Obtain forced fits if requested.
        4. Run selavy matching and upper limit fetching.
        5. Package up results into vasttools.source.Source objects.

        Returns:
            None

        Raises:
            Exception: find_sources cannot be run with the incl_observed option
        """

        if self.settings['incl_observed']:
            raise Exception(
                'find_sources cannot be run with the incl_observed option'
            )

        self.logger.debug('Running find_sources...')

        if self.fields_found is False:
            self.find_fields()

        self.logger.info("Finding sources in VAST data...")

        self.sources_df = self.fields_df.sort_values(
            by=['name', 'dateobs']
        ).reset_index(drop=True)

        self.logger.debug("Adding files...")
        self.sources_df[
            ['selavy', 'image', 'rms']
        ] = self.sources_df[['epoch', 'field', 'sbid']].apply(
            self._add_files,
            axis=1,
            result_type='expand'
        )

        self._validate_files()

        if self.settings['forced_fits']:
            self.logger.info("Obtaining forced fits...")
            meta = {
                'f_island_id': 'U',
                'f_component_id': 'U',
                'f_ra_deg_cont': 'f',
                'f_dec_deg_cont': 'f',
                'f_flux_peak': 'f',
                'f_flux_peak_err': 'f',
                'f_flux_int': 'f',
                'f_flux_int_err': 'f',
                'f_chi_squared_fit': 'f',
                'f_rms_image': 'f',
                'f_maj_axis': 'f',
                'f_min_axis': 'f',
                'f_pos_ang': 'f',
            }

            f_results = (
                dd.from_pandas(self.sources_df, self.ncpu)
                .groupby('image')
                .apply(
                    self._get_forced_fits,
                    cluster_threshold=(
                        self.settings['forced_cluster_threshold']
                    ),
                    allow_nan=self.settings['forced_allow_nan'],
                    meta=meta,
                ).compute(num_workers=self.ncpu,
                          scheduler=self.settings['scheduler']
                          )
            )

            if not f_results.empty:
                if isinstance(f_results.index, pd.MultiIndex):
                    f_results.index = f_results.index.droplevel()
            else:
                self.settings['forced_fits'] = False

            self.logger.info("Forced fitting finished.")

        self.logger.debug("Getting components...")
        results = (
            dd.from_pandas(self.sources_df, self.ncpu)
            .groupby('selavy')
            .apply(
                self._get_components,
                meta=self._get_selavy_meta(),
            ).compute(num_workers=self.ncpu,
                      scheduler=self.settings['scheduler']
                      )
        )

        self.logger.debug("Selavy components succesfully added.")
        self.logger.debug(results)

        if self.settings['islands']:
            results['rms_image'] = results['background_noise']
            results['flux_peak_err'] = results['background_noise']

        if not results.empty:
            if isinstance(results.index, pd.MultiIndex):
                results.index = results.index.droplevel()

        if self.settings['search_around']:
            results = results.set_index('index')

        if self.settings['forced_fits']:
            results = results.merge(
                f_results, left_index=True, right_index=True
            )

            del f_results

        if self.settings['search_around']:
            how = 'inner'
        else:
            how = 'left'

        self.crossmatch_results = self.sources_df.merge(
            results, how=how, left_index=True, right_index=True
        )
        self.logger.debug("Crossmatch results:")
        self.logger.debug(self.crossmatch_results)

        meta = {'name': 'O'}

        self.num_sources_detected = (
            self.crossmatch_results.groupby('name').agg({
                'detection': any
            }).sum()
        )
        self.logger.debug(f"{self.num_sources_detected} sources detected:")

        if self.settings['search_around']:
            self.results = self.crossmatch_results.rename(
                columns={'#': 'distance'}
            )
        else:
            npart = min(self.ncpu, self.crossmatch_results.name.nunique())
            self.results = (
                dd.from_pandas(self.crossmatch_results, npart)
                .groupby('name')
                .apply(
                    self._init_sources,
                    meta=meta,
                ).compute(num_workers=npart,
                          scheduler=self.settings['scheduler']
                          )
            )
            self.results = self.results.dropna()

        self.logger.info("Source finding complete!")

    def save_search_around_results(self, sort_output: bool = False) -> None:
        """
        Save results from cone search.

        Args:
            sort_output: Whether to sort the output, defaults to `False`.

        Returns:
            None
        """
        meta = {}
        # also have the sort output setting as a function
        # input in case of interactive use.
        if self.settings['sort_output']:
            sort_output = True
        result = (
            dd.from_pandas(
                self.results.drop([
                    'fields',
                    'stokes',
                    'skycoord',
                    'selavy',
                    'image',
                    'rms',
                ], axis=1), self.ncpu)
            .groupby('name')
            .apply(
                self._write_search_around_results,
                sort_output=sort_output,
                meta=meta,
            ).compute(num_workers=self.ncpu,
                      scheduler=self.settings['scheduler']
                      )
        )

    def _write_search_around_results(
        self, group: pd.DataFrame, sort_output: bool
    ) -> None:
        """
        Write cone search results to file

        Args:
            group: The group from the pandas groupby function,
                which in this case is grouped by image.
            sort_output: Whether to sort the output.

        Returns:
            None
        """
        source_name = group['name'].iloc[0].replace(
            " ", "_"
        ).replace("/", "_")

        group = group.sort_values(by=['dateobs', 'component_id'])

        outname = "{}_matches_around.csv".format(
            source_name
        )

        if sort_output:
            base = os.path.join(
                self.settings['output_dir'],
                source_name
            )
        else:
            base = self.settings['output_dir']

        outname = os.path.join(
            base,
            outname
        )

        group.to_csv(outname, index=False)

        time.sleep(0.1)

    def _check_for_duplicate_epochs(self, epochs: pd.Series) -> pd.Series:
        """
        Checks whether a source has been detected in an epoch twice, which
        usually affects planets.

        If a duplicate is detected it adds `-N` to the epoch where N is the
        ith occurance of the epoch. E.g. 0, 0 is converted to 0-1, 0-2.

        Args:
            epochs: The epochs of the source.

        Returns:
            Corrected epochs.
        """
        dup_mask = epochs.duplicated(keep=False)
        if dup_mask.any():
            epochs.loc[dup_mask] = (
                epochs.loc[dup_mask]
                + "-"
                + (epochs[dup_mask].groupby(
                    epochs[dup_mask]
                ).cumcount() + 1).astype(str)
            )

        return epochs

    def _init_sources(self, group: pd.DataFrame) -> Source:
        """
        Initialises the vasttools.source.Source objects
        which are returned by find_sources.

        Args:
            group: The grouped measurements to initialise a source object.

        Returns:
            Source of interest.
        """
        group = group.sort_values(by='dateobs')

        if group.empty:
            return

        m = group.iloc[0]

        if self.settings['matches_only']:
            if group['detection'].sum() == 0:
                self.logger.warning(
                    f"'{m['name']}' has no detections and 'matches only' "
                    "has been selected. This source will not be in the "
                    "results."
                )
                return
        if m['planet']:
            source_coord = group.skycoord
            source_primary_field = group.primary_field
            group['epoch'] = self._check_for_duplicate_epochs(
                group['epoch']
            )
        else:
            source_coord = m.skycoord
            source_primary_field = m.primary_field
        source_name = m['name']
        source_epochs = group['epoch'].to_list()
        source_fields = group['field'].to_list()
        source_stokes = self.settings['stokes']
        source_base_folder = self.base_folder
        source_crossmatch_radius = self.settings['crossmatch_radius']
        source_outdir = self.settings['output_dir']
        if self.settings['sort_output']:
            source_outdir = os.path.join(
                source_outdir,
                source_name.replace(" ", "_").replace("/", "_")
            )
        if self.settings['tiles']:
            source_image_type = "TILES"
        else:
            source_image_type = "COMBINED"
        source_islands = self.settings['islands']

        if '#' in group.columns:
            source_df = group.drop('#', axis=1)
        else:
            source_df = group

        source_df = source_df.sort_values('dateobs').reset_index(drop=True)

        self.logger.debug("Initialising Source with base folder:")
        self.logger.debug(source_base_folder)
        thesource = Source(
            source_coord,
            source_name,
            source_epochs,
            source_fields,
            source_stokes,
            source_primary_field,
            source_crossmatch_radius,
            source_df,
            source_base_folder,
            source_image_type,
            islands=source_islands,
            forced_fits=self.settings['forced_fits'],
            outdir=source_outdir,
            corrected_data=self.corrected_data,
            post_processed_data=self.post_processed_data,
        )

        return thesource

    def _forcedphot_preload(self,
                            image: str,
                            background: str,
                            noise: str,
                            memmap: Optional[bool] = False
                            ):
        """
        Load the relevant image, background and noisemap files.
        Args:
            image: a string with the path of the image file
            background: a string with the path of the background map
            noise: a string with the path of the noise map
        Returns:
            A tuple containing the HDU lists
        """

        image_hdul = open_fits(image, memmap=memmap)
        background_hdul = open_fits(background, memmap=memmap)
        noise_hdul = open_fits(noise, memmap=memmap)

        return image_hdul, background_hdul, noise_hdul


    def _get_forced_fits(
        self, group: pd.DataFrame,
        cluster_threshold: float = 1.5, allow_nan: bool = False
    ) -> pd.DataFrame:
        """
        Perform the forced fits on an image, on the coordinates
        supplied by the group.

        Args:
            group: A dataframe of sources/positions which have been
                supplied by grouping the queried sources by image.
            cluster_threshold: The cluster_threshold value passed to
                the forced photometry. Beam width distance limit for which a
                cluster is formed for extraction, defaults to 3.0.
            allow_nan: `allow_nan` value passed to the forced photometry.
                If False then any cluster containing a NaN is ignored.
                Defaults to False.

        Returns:
            Dataframe containing the forced fit measurements for each source.
        """
        image = group.name
        if image is None:
            return

        group = group.sort_values(by='dateobs')

        m = group.iloc[0]
        image_name = image.split("/")[-1]
        rms = m['rms']
        bkg = rms.replace('noiseMap', 'meanMap')

        field = m['field']
        epoch = m['epoch']
        stokes = m['stokes']
        self.logger.debug("Getting Image for forced fits")
        try:
            img_beam = Image(
                field,
                epoch,
                stokes,
                self.base_folder,
                tiles=self.settings["tiles"],
                path=image,
                rmspath=rms,
                corrected_data=self.corrected_data,
                post_processed_data=self.post_processed_data,
            )
            img_beam.get_img_data()
            img_beam = img_beam.beam
        except Exception as e:
            return pd.DataFrame(columns=[
                'f_island_id',
                'f_component_id',
                'f_ra_deg_cont',
                'f_dec_deg_cont',
                'f_flux_peak',
                'f_flux_peak_err',
                'f_flux_int',
                'f_flux_int_err',
                'f_chi_squared_fit',
                'f_rms_image',
                'f_maj_axis',
                'f_min_axis',
                'f_pos_ang',
            ])

        major = img_beam.major.to(u.arcsec).value
        minor = img_beam.minor.to(u.arcsec).value
        pa = img_beam.pa.to(u.deg).value

        to_fit = SkyCoord(
            group.ra, group.dec, unit=(u.deg, u.deg)
        )

        # make the Forced Photometry object
        forcedphot_input = self._forcedphot_preload(image,
                                                    bkg,
                                                    rms,
                                                    memmap=False
                                                    )
        FP = ForcedPhot(*forcedphot_input)

        # run the forced photometry
        (
            flux_islands, flux_err_islands,
            chisq_islands, DOF_islands, iscluster
        ) = FP.measure(
            to_fit,
            [major for i in range(to_fit.shape[0])] * u.arcsec,
            [minor for i in range(to_fit.shape[0])] * u.arcsec,
            [pa for i in range(to_fit.shape[0])] * u.deg,
            cluster_threshold=cluster_threshold,
            allow_nan=allow_nan
        )

        flux_islands *= 1.e3
        flux_err_islands *= 1.e3

        source_names = [
            "{}_{:04d}".format(
                image_name, i
            ) for i in range(len(flux_islands))
        ]

        data = {
            'f_island_id': source_names,
            'f_component_id': source_names,
            'f_ra_deg_cont': group.ra,
            'f_dec_deg_cont': group.dec,
            'f_flux_peak': flux_islands,
            'f_flux_peak_err': flux_err_islands,
            'f_flux_int': flux_islands,
            'f_flux_int_err': flux_err_islands,
            'f_chi_squared_fit': chisq_islands,
            'f_rms_image': flux_err_islands,
        }

        df = pd.DataFrame(data)

        df['f_maj_axis'] = major
        df['f_min_axis'] = minor
        df['f_pos_ang'] = pa

        df.index = group.index.values

        return df

    def _get_selavy_meta(self) -> dict:
        """
        Obtains the correct metadata dictionary for use with
        Query._get_components

        Args:
            None
        Returns:
            The metadata dictionary
        """

        if self.settings["islands"]:
            meta = {
                'island_id': 'U',
                'island_name': 'U',
                'n_components': 'f',
                'ra_hms_cont': 'U',
                'dec_dms_cont': 'U',
                'ra_deg_cont': 'f',
                'dec_deg_cont': 'f',
                'freq': 'f',
                'maj_axis': 'f',
                'min_axis': 'f',
                'pos_ang': 'f',
                'flux_int': 'f',
                'flux_int_err': 'f',
                'flux_peak': 'f',
                'mean_background': 'f',
                'background_noise': 'f',
                'max_residual': 'f',
                'min_residual': 'f',
                'mean_residual': 'f',
                'rms_residual': 'f',
                'stdev_residual': 'f',
                'x_min': 'i',
                'x_max': 'i',
                'y_min': 'i',
                'y_max': 'i',
                'n_pix': 'i',
                'solid_angle': 'f',
                'beam_area': 'f',
                'x_ave': 'f',
                'y_ave': 'f',
                'x_cen': 'f',
                'y_cen': 'f',
                'x_peak': 'i',
                'y_peak': 'i',
                'flag_i1': 'i',
                'flag_i2': 'i',
                'flag_i3': 'i',
                'flag_i4': 'i',
                'comment': 'U',
                'detection': '?'
            }
        else:
            meta = {
                'island_id': 'U',
                'component_id': 'U',
                'component_name': 'U',
                'ra_hms_cont': 'U',
                'dec_dms_cont': 'U',
                'ra_deg_cont': 'f',
                'dec_deg_cont': 'f',
                'ra_err': 'f',
                'dec_err': 'f',
                'freq': 'f',
                'flux_peak': 'f',
                'flux_peak_err': 'f',
                'flux_int': 'f',
                'flux_int_err': 'f',
                'maj_axis': 'f',
                'min_axis': 'f',
                'pos_ang': 'f',
                'maj_axis_err': 'f',
                'min_axis_err': 'f',
                'pos_ang_err': 'f',
                'maj_axis_deconv': 'f',
                'min_axis_deconv': 'f',
                'pos_ang_deconv': 'f',
                'maj_axis_deconv_err': 'f',
                'min_axis_deconv_err': 'f',
                'pos_ang_deconv_err': 'f',
                'chi_squared_fit': 'f',
                'rms_fit_gauss': 'f',
                'spectral_index': 'f',
                'spectral_curvature': 'f',
                'spectral_index_err': 'f',
                'spectral_curvature_err': 'f',
                'rms_image': 'f',
                'has_siblings': 'f',
                'fit_is_estimate': 'f',
                'spectral_index_from_TT': 'f',
                'flag_c4': 'f',
                'comment': 'f',
                'detection': '?',
            }

        if self.settings['search_around']:
            meta['#'] = 'f'
            meta['index'] = 'i'

        return meta

    def _get_components(self, group: pd.DataFrame) -> pd.DataFrame:
        """
        Obtains the matches from the selavy catalogue for each coordinate
        in the group. The group is the queried sources grouped by image
        (the result from find_fields). If no component is found then the
        rms is measured at the source location.

        Args:
            group: The grouped coordinates to search in the image.

        Returns:
            The selavy matched component and/or upper limits for the queried
            coordinates.
        """
        selavy_file = str(group.name)

        if selavy_file is None:
            self.logger.warning("Selavy file is None. Returning None.")
            return

        master = pd.DataFrame()

        selavy_df = read_selavy(selavy_file)
        self.logger.debug(f"Selavy df head: {selavy_df.head()}")

        if self.settings['stokes'] != "I":
            head, tail = os.path.split(selavy_file)
            nselavy_file = os.path.join(head, 'n{}'.format(tail))
            nselavy_df = read_selavy(nselavy_file)

            nselavy_df[["flux_peak", "flux_int"]] *= -1.0

            selavy_df = pd.concat(
                [selavy_df, nselavy_df],
                ignore_index=True,
                sort=False
            )

        selavy_coords = SkyCoord(
            selavy_df.ra_deg_cont,
            selavy_df.dec_deg_cont,
            unit=(u.deg, u.deg)
        )

        group_coords = SkyCoord(
            group.ra,
            group.dec,
            unit=(u.deg, u.deg)
        )

        if self.settings['search_around']:
            idxselavy, idxc, d2d, _ = group_coords.search_around_sky(
                selavy_coords, self.settings['crossmatch_radius']
            )
            if idxc.shape[0] == 0:
                return
            copy = selavy_df.iloc[idxselavy].reset_index(drop=True)
            copy['detection'] = True
            copy['#'] = d2d.arcsec
            copy.index = group.iloc[idxc].index.values
            master = pd.concat([master, copy], sort=False)
            # reset index and move previous index to the end to match the meta
            master_cols = master.columns.to_list()
            master = master.reset_index()[master_cols + ['index']]
        else:
            idx, d2d, _ = group_coords.match_to_catalog_sky(selavy_coords)
            mask = d2d < self.settings['crossmatch_radius']
            idx_matches = idx[mask]

            copy = selavy_df.iloc[idx_matches].reset_index(drop=True)
            copy['detection'] = True
            copy.index = group[mask].index.values

            master = pd.concat([master, copy], sort=False)

            missing = group_coords[~mask]
            if missing.shape[0] > 0:
                if not self.settings['no_rms']:
                    try:
                        self.logger.debug(
                            "Initialising Image for components RMS estimates")
                        self.logger.debug(self.base_folder)
                        image = Image(
                            group.iloc[0].field,
                            group.iloc[0].epoch,
                            self.settings['stokes'],
                            self.base_folder,
                            sbid=group.iloc[0].sbid,
                            tiles=self.settings['tiles'],
                            corrected_data=self.corrected_data,
                            post_processed_data=self.post_processed_data
                        )
                        image.get_img_data()
                        rms_values = image.measure_coord_pixel_values(
                            missing, rms=True
                        )
                        rms_df = pd.DataFrame(
                            rms_values, columns=['rms_image'])

                        # to mJy
                        rms_df['rms_image'] = rms_df['rms_image'] * 1.e3
                    except Exception as e:
                        rms_df = pd.DataFrame(
                            [-99 for i in range(missing.shape[0])],
                            columns=['rms_image']
                        )
                else:
                    rms_df = pd.DataFrame(
                        [-99 for i in range(missing.shape[0])],
                        columns=['rms_image']
                    )

                rms_df['detection'] = False

                rms_df.index = group[~mask].index.values

                master = pd.concat([master, rms_df], sort=False)

        return master

    def _get_selavy_path(self, epoch_string: str, row: pd.Series) -> str:
        """
        Get the path to the selavy file for a specific row of the dataframe.
        Args:
            epoch_string: The name of the epoch in the form of 'EPOCHXX'.
            row: row: The input row of the dataframe.
        Returns:
            The path to the selavy file of interest
        """

        field = row.field.replace('RACS', 'VAST')

        if self.settings['islands']:
            cat_type = 'islands'
        else:
            cat_type = 'components'

        if self.settings['tiles']:
            dir_name = "TILES"

            data_folder = f"STOKES{self.settings['stokes']}_SELAVY"
            if self.corrected_data:
                data_folder += "_CORRECTED"
            if self.post_processed_data:
                data_folder += "_PROCESSED"

            selavy_folder = Path(
                self.base_folder,
                epoch_string,
                dir_name,
                data_folder
            )

            selavy_file_fmt = (
                "selavy-image.{}.{}.SB{}.cont."
                "taylor.0.restored.conv.{}.xml".format(
                    self.settings['stokes'].lower(), field, row.sbid, cat_type
                )
            )

            if self.corrected_data:
                selavy_file_fmt = selavy_file_fmt.replace(".xml",
                                                          ".corrected.xml"
                                                          )
            if self.post_processed_data:
                selavy_file_fmt = selavy_file_fmt.replace(".xml",
                                                          ".processed.xml"
                                                          )

            selavy_path = selavy_folder / selavy_file_fmt

            # Some epochs don't have .conv.
            if not selavy_path.is_file():
                self.logger.debug(f"{selavy_path} is not a file...")
                self.logger.debug(f"Removing '.conv' from filename")
                selavy_path = Path(str(selavy_path).replace('.conv', ''))

        else:
            dir_name = "COMBINED"
            selavy_folder = Path(
                self.base_folder,
                epoch_string,
                dir_name,
                f"STOKES{self.settings['stokes']}_SELAVY"
            )

            selavy_file_fmt = "selavy-{}.EPOCH{}.{}.conv.{}.xml".format(
                field,
                RELEASED_EPOCHS[row.epoch],
                self.settings['stokes'],
                cat_type
            )

            selavy_path = selavy_folder / selavy_file_fmt

        return str(selavy_path)

    def _add_files(self, row: pd.Series) -> Tuple[str, str, str]:
        """
        Adds the file paths for the image, selavy catalogues and
        rms images for each source to be queried.
        Args:
            row: The input row of the dataframe (this function is called with
                a .apply())
        Returns:
            The paths of the image, selavy catalogue and rms image.
        """
        epoch_string = "EPOCH{}".format(
            RELEASED_EPOCHS[row.epoch]
        )

        img_dir = "STOKES{}_IMAGES".format(self.settings['stokes'])
        rms_dir = "STOKES{}_RMSMAPS".format(self.settings['stokes'])
        field = row.field.replace('RACS', 'VAST')

        if self.settings['tiles']:
            dir_name = "TILES"

            image_file_fmt = (
                "image.{}.{}.SB{}.cont"
                ".taylor.0.restored.fits".format(
                    self.settings['stokes'].lower(), field, row.sbid
                )
            )

            if self.corrected_data:
                img_dir += "_CORRECTED"
                rms_dir += "_CORRECTED"
                image_file_fmt = image_file_fmt.replace(".fits",
                                                        ".corrected.fits"
                                                        )
            if self.post_processed_data:
                img_dir += "_PROCESSED"
                rms_dir += "_PROCESSED"
                image_file_fmt = image_file_fmt.replace(".fits",
                                                        ".processed.fits"
                                                        )
            rms_file_fmt = f"noiseMap.{image_file_fmt}"

        else:
            dir_name = "COMBINED"

            image_file_fmt = "{}.EPOCH{}.{}.conv.fits".format(
                field,
                RELEASED_EPOCHS[row.epoch],
                self.settings['stokes'],
            )

            rms_file_fmt = "noiseMap.{}.EPOCH{}.{}.conv.fits".format(
                field,
                RELEASED_EPOCHS[row.epoch],
                self.settings['stokes'],
            )

        selavy_file = self._get_selavy_path(epoch_string, row)

        image_file = Path(os.path.join(
            self.base_folder,
            epoch_string,
            dir_name,
            img_dir,
            image_file_fmt
        ))

        rms_file = Path(os.path.join(
            self.base_folder,
            epoch_string,
            dir_name,
            rms_dir,
            rms_file_fmt
        ))

        if not image_file.is_file():
            conv_image_file = Path(str(image_file).replace('.restored',
                                                           '.restored.conv')
                                   )
            if conv_image_file.is_file():
                image_file = conv_image_file
                rms_file = Path(str(rms_file).replace('.restored',
                                                      '.restored.conv')
                                )

        return selavy_file, str(image_file), str(rms_file)

    def _validate_files(self) -> None:
        """
        Check whether files in sources_df exist, and if not, remove them.

        Returns:
            None
        """

        missing_df = pd.DataFrame()
        missing_df['selavy'] = ~self.sources_df['selavy'].map(os.path.exists)
        missing_df['image'] = ~self.sources_df['image'].map(os.path.exists)
        missing_df['rms'] = ~self.sources_df['rms'].map(os.path.exists)

        missing_df['any'] = missing_df.any(axis=1)

        self.logger.debug(missing_df)

        for i, row in missing_df[missing_df['any']].iterrows():
            sources_row = self.sources_df.iloc[i]

            self.logger.warning(f"Removing {sources_row['name']}: Epoch "
                                f"{sources_row.epoch} due to missing files")
            if row.selavy:
                self.logger.debug(f"{sources_row.selavy} does not exist!")
            if row.image:
                self.logger.debug(f"{sources_row.image} does not exist!")
            if row.rms:
                self.logger.debug(f"{sources_row.rms} does not exist!")

        self.sources_df = self.sources_df[~missing_df['any']]

    def write_find_fields(self, outname: Optional[str] = None) -> None:
        """
        Write the results of a field search to file.

        Args:
            outname: Name of file to write output to, defaults to None, which
                will name the file 'find_fields_results.csv'.

        Returns:
            None
        """
        if self.fields_found is False:
            self.find_fields()

        if outname is None:
            name = 'find_fields_result.csv'
        else:
            name = outname + '.pkl'

        outdir = self.settings['output_dir']
        if outdir is not None:
            name = os.path.join(outdir, name)

        self.fields_df[[
            'name',
            'ra',
            'dec',
            'field',
            'epoch',
            'sbid',
            'dateobs',
            'primary_field'
        ]].to_csv(name, index=False)

        self.logger.info('Find fields output saved as {}.'.format(
            name
        ))

    def find_fields(self) -> None:
        """
        Find the corresponding field for each source.

        Planet fields are also found here if any are selected.

        Returns:
            None

        Raises:
            Exception: No sources are found within the requested footprint.
        """

        if self.racs:
            base_fc = 'RACS'
        else:
            base_fc = 'VAST'

        self.logger.info(
            f"Matching queried sources to {base_fc} fields..."
        )

        base_epoch = BASE_EPOCHS[base_fc]

        fields = Fields(base_epoch)
        field_centres = load_field_centres()

        field_centres = field_centres.loc[
            field_centres['field'].str.contains(base_fc)
        ].reset_index()

        field_centres_sc = SkyCoord(
            field_centres["centre-ra"],
            field_centres["centre-dec"],
            unit=(u.deg, u.deg)
        )

        # if RACS is being use we convert all the names to 'VAST'
        # to match the VAST field names, makes matching easier.
        if base_fc != 'VAST':
            field_centres['field'] = [
                f.replace("RACS", "VAST") for f in field_centres.field
            ]

        field_centre_names = field_centres.field

        if self.query_df is not None:
            self.fields_df = self.query_df.copy()

            # _field_matching returns 7 arguments. This dict specifies types,
            # O for object (in this case, lists) and U for unicode string.
            meta = {
                0: 'O',
                1: 'U',
                2: 'O',
                3: 'O',
                4: 'O',
                5: 'O',
                6: 'O',
            }
            self.logger.debug("Running field matching...")

            self.fields_df[[
                'fields',
                'primary_field',
                'epochs',
                'field_per_epoch',
                'sbids',
                'dates',
                'freqs'
            ]] = (
                dd.from_pandas(self.fields_df, self.ncpu)
                .apply(
                    self._field_matching,
                    args=(
                        fields.direction,
                        fields.fields.FIELD_NAME,
                        field_centres_sc,
                        field_centre_names
                    ),
                    meta=meta,
                    axis=1,
                    result_type='expand'
                ).compute(num_workers=self.ncpu,
                          scheduler=self.settings['scheduler']
                          )
            )

            self.logger.debug("Finished field matching.")
            self.fields_df = self.fields_df.dropna()

            if self.fields_df.empty:
                raise Exception(
                    "No requested sources are within the requested footprint!")

            self.fields_df = self.fields_df.explode(
                'field_per_epoch'
            ).reset_index(drop=True)

            field_per_epoch = self.fields_df['field_per_epoch'].tolist()

            self.fields_df[
                ['epoch', 'field', 'sbid', 'dateobs', 'frequency']
            ] = pd.DataFrame(
                field_per_epoch,
                index=self.fields_df.index
            )

            to_drop = [
                'field_per_epoch',
                'epochs',
                'sbids',
                'dates',
                'freqs'
            ]
            self.logger.debug(self.fields_df['name'])
            self.logger.debug(self.fields_df['dateobs'])
            self.fields_df = self.fields_df.drop(
                labels=to_drop, axis=1
            ).sort_values(
                by=['name', 'dateobs']
            ).reset_index(drop=True)

            self.fields_df['planet'] = False
        else:
            self.fields_df = None

        # Handle Planets
        if len(self.planets) > 0:
            self.logger.debug(f"Searching for planets: {self.planets}")
            planet_fields = self._search_planets()

            if self.fields_df is None:
                self.fields_df = planet_fields
            else:
                self.fields_df = pd.concat(
                    [self.fields_df, planet_fields]
                ).reset_index(drop=True)

        self.logger.debug(self.fields_df)

        if self.query_df is None:
            prev_num = 0
        else:
            prev_num = self.query_df.shape[0]

        if self.planets is not None:
            prev_num += len(self.planets)

        self.num_sources_searched = self.fields_df.name.unique().shape[0]

        if self.racs:
            self.logger.info(
                f"{self.num_sources_searched}/{prev_num} "
                "sources in RACS & VAST footprint."
            )
        else:
            self.logger.info(
                f"{self.num_sources_searched}/{prev_num} "
                "sources in VAST footprint."
            )

        self.fields_df['dateobs'] = pd.to_datetime(
            self.fields_df['dateobs']
        )

        # All field names should start with VAST, not RACS
        self.fields_df['field'] = self.fields_df['field'].str.replace("RACS",
                                                                      "VAST"
                                                                      )

        self.logger.info("Done.")
        self.fields_found = True

    def _field_matching(
        self,
        row: pd.Series,
        fields_coords: SkyCoord,
        fields_names: pd.Series,
        field_centres: SkyCoord,
        field_centre_names: List[str]
    ) -> Tuple[
        str, str, List[str], List[str], List[str], List[datetime.datetime]
    ]:
        """
        This function does the actual field matching for each queried
        coordinate, which is a 'row' here in the function.

        Args:
            row: The row from the query_df, i.e. the coordinates to match
                to a field.
            fields_coords: SkyCoord object representing the beam
                centres of the VAST or RACS survey.
            fields_names: Field names to match with the SkyCoord object.
            field_centres: SkyCoord object representing the field centres
            field_centre_names: Field names matching the field centre
                SkyCoord.

        Returns:
            Tuple containing the field information.
        """

        self.logger.debug("Running field matching with following row info:")
        self.logger.debug(row)
        self.logger.debug("Field names:")
        self.logger.debug(fields_names)

        seps = row.skycoord.separation(fields_coords)
        accept = seps.deg < self.settings['max_sep']
        fields = np.unique(fields_names[accept])

        if self.racs or self.vast_full:
            vast_fields = np.array(
                [f.replace("RACS", "VAST") for f in fields]
            )

        if fields.shape[0] == 0:
            self.logger.info(
                f"Source '{row['name']}' not in the requested epoch footprint."
            )
            return_vals = [np.nan] * 7  # Return nans in all 7 columns
            self.logger.debug(return_vals)
            return return_vals

        centre_seps = row.skycoord.separation(field_centres)
        primary_field = field_centre_names.iloc[np.argmin(centre_seps.deg)]
        self.logger.debug(f"Primary field: {primary_field}")
        epochs = []
        field_per_epochs = []
        sbids = []
        dateobs = []
        freqs = []

        for i in self.settings['epochs']:
            self.logger.debug(f"Epoch {i}")
            if i not in RACS_EPOCHS and self.racs:
                the_fields = vast_fields
            elif i not in RACS_EPOCHS and self.vast_full:
                the_fields = vast_fields
            else:
                the_fields = fields

            epoch_fields_names = self._epoch_fields.loc[i].index
            stripped = False
            if epoch_fields_names[0].endswith('A'):
                self.logger.debug("Using stripped field names")
                stripped = True
                epoch_fields_names = strip_fieldnames(epoch_fields_names)
            the_fields = list(set([f.rstrip('A') for f in the_fields]))

            self.logger.debug("Fields in epoch: ")
            self.logger.debug(epoch_fields_names)

            self.logger.debug("The fields: ")
            self.logger.debug(the_fields)

            available_fields = [
                f for f in the_fields if f in epoch_fields_names.to_list()
            ]
            self.logger.debug("Available fields:")
            self.logger.debug(available_fields)

            if i in RACS_EPOCHS:
                available_fields = [
                    j.replace("RACS", "VAST") for j in available_fields
                ]

            if len(available_fields) == 0:
                self.logger.debug("No fields available")
                continue

            if self.settings['search_all_fields']:
                selected_fields = available_fields

            elif primary_field in available_fields:
                selected_fields = [primary_field]
                self.logger.debug("Selecting primary field")

            elif len(available_fields) == 1:
                selected_fields = [available_fields[0]]
                self.logger.debug("Selecting only available field")

            else:
                field_indexes = [
                    field_centre_names[
                        field_centre_names == f.rstrip('A')
                    ].index[0] for f in available_fields
                ]
                min_field_index = np.argmin(
                    centre_seps[field_indexes].deg
                )

                selected_fields = [available_fields[min_field_index]]
                self.logger.debug("Selecting closest field")

            self.logger.debug(f"Selected fields: {selected_fields}")

            # Change VAST back to RACS
            if i in RACS_EPOCHS:
                selected_fields = [f.replace("VAST", "RACS")
                                   for f in selected_fields
                                   ]
            for field in selected_fields:
                if stripped:
                    field = f"{field}A"
                sbid_vals = self._epoch_fields.loc[i, field]["SBID"]
                date_vals = self._epoch_fields.loc[i, field]["DATEOBS"]
                freq_vals = self._epoch_fields.loc[i, field]["OBS_FREQ"]

                for sbid, date, freq in zip(sbid_vals, date_vals, freq_vals):
                    sbids.append(sbid)
                    dateobs.append(date)
                    freqs.append(freq)
                    epochs.append(i)
                    field_per_epochs.append([i, field, sbid, date, freq])

        return_vals = (fields,
                       primary_field,
                       epochs,
                       field_per_epochs,
                       sbids,
                       dateobs,
                       freqs
                       )
        # If len(available_fields) == 0 for all epochs need to return nan
        if len(epochs) == 0:
            return [np.nan] * 7  # Return nans in all 7 columns

        return return_vals

    def _get_planets_epoch_df_template(self) -> pd.DataFrame:
        """
        Generate template df for fields containing planets in all epochs

        Returns:
            Dataframe containing fields and epoch info.
        """
        epochs = self.settings['epochs']
        field_centres = load_field_centres()

        planet_epoch_fields = self._epoch_fields.loc[epochs].reset_index()
        stripped_field_names = planet_epoch_fields.FIELD_NAME.str.rstrip('A')
        planet_epoch_fields['STRIPPED_FIELD_NAME'] = stripped_field_names

        planet_epoch_fields = planet_epoch_fields.merge(
            field_centres, left_on='STRIPPED_FIELD_NAME',
            right_on='field', how='left'
        ).drop(['field', 'OBS_FREQ', 'STRIPPED_FIELD_NAME'], axis=1).rename(
            columns={'EPOCH': 'epoch'}
        )

        return planet_epoch_fields

    def _search_planets(self) -> pd.DataFrame:
        """
        Search for planets in all requested epochs

        Returns:
            Dataframe containing search results
        """
        template = self._get_planets_epoch_df_template()

        template['planet'] = [self.planets for i in range(template.shape[0])]

        template = template.explode('planet')
        template['planet'] = template['planet'].str.capitalize()

        meta = {
            'epoch': 'U',
            'FIELD_NAME': 'U',
            'SBID': 'i',
            'DATEOBS': 'datetime64[ns]',
            'centre-ra': 'f',
            'centre-dec': 'f',
            'planet': 'U',
            'ra': 'f',
            'dec': 'f',
            'sep': 'f'
        }

        results = (
            dd.from_pandas(template, self.ncpu)
            .groupby('planet')
            .apply(
                match_planet_to_field,
                meta=meta,
            ).compute(num_workers=self.ncpu,
                      scheduler=self.settings['scheduler']
                      )
        )

        results = results.reset_index(drop=True).drop(
            ['centre-ra', 'centre-dec', 'sep'], axis=1
        ).rename(columns={
            'planet': 'name',
            'FIELD_NAME': 'field',
            'DATEOBS': 'dateobs',
            'SBID': 'sbid',
        }).sort_values(by=['name', 'dateobs'])

        results['stokes'] = self.settings['stokes'].upper()
        results['primary_field'] = results['field']
        results['skycoord'] = SkyCoord(
            results['ra'], results['dec'], unit=(u.deg, u.deg)
        )
        results['fields'] = [[i] for i in results['field']]
        results['planet'] = True

        return results

    def _build_catalog(self) -> pd.DataFrame:
        """
        Generate source catalogue from requested coordinates,
        removing those outside of the VAST pilot fields.

        Returns:
            Catalogue of source positions.
        """
        self.logger.debug("Building catalogue")

        cols = ['ra', 'dec', 'name', 'skycoord', 'stokes']

        if self.racs:
            self.logger.debug("Using RACS footprint for masking")
            mask = self.coords.dec.deg > 50

            if mask.any():
                self.logger.warning(
                    "Removing %i sources outside the RACS area", sum(mask)
                )
                self.coords = self.coords[~mask]
                self.source_names = self.source_names[~mask]
        else:
            mocs = VASTMOCS()

            pilot = self.vast_p1 or self.vast_p2

            if pilot:
                self.logger.debug("Using VAST pilot footprint for masking")
                footprint_moc = mocs.load_survey_footprint('pilot')

            if self.vast_full:
                self.logger.debug("Using full VAST footprint for masking")
                full_moc = mocs.load_survey_footprint('full')
                if pilot:
                    footprint_moc = footprint_moc.union(full_moc)
                else:
                    footprint_moc = full_moc

            self.logger.debug("Masking sources outside footprint")
            mask = footprint_moc.contains(
                self.coords.ra, self.coords.dec, keep_inside=False
            )
            if mask.any():
                self.logger.warning(
                    f"Removing {sum(mask)} sources outside the requested "
                    f"survey footprint."
                )
                self.coords = self.coords[~mask]
                self.source_names = self.source_names[~mask]

        self.logger.debug("Generating catalog dataframe")
        if self.coords.shape == ():
            catalog = pd.DataFrame(
                [[
                    self.coords.ra.deg,
                    self.coords.dec.deg,
                    self.source_names[0],
                    self.coords,
                    self.settings['stokes']
                ]], columns=cols
            )
        else:
            catalog = pd.DataFrame(
                self.source_names,
                columns=['name']
            )
            catalog['ra'] = self.coords.ra.deg
            catalog['dec'] = self.coords.dec.deg
            catalog['skycoord'] = self.coords
            catalog['stokes'] = self.settings['stokes']

        if self.simbad_names is not None:
            self.logger.debug("Handling SIMBAD naming")
            self.simbad_names = self.simbad_names[~mask]
            catalog['simbad_name'] = self.simbad_names

        return catalog

    def _get_epochs(self,
                    req_epochs: Union[str, List[str], List[int]]
                    ) -> List[str]:
        """
        Parse the list of epochs to query.

        Args:
            req_epochs: Requested epochs to query.

        Returns:
            Epochs to query, as a list of strings.

        Raises:
            QueryInitError: None of the requested epochs are available
        """

        epoch_dict = RELEASED_EPOCHS.copy()

        if self.settings['incl_observed']:
            epoch_dict.update(OBSERVED_EPOCHS)
        available_epochs = sorted(epoch_dict, key=epoch_dict.get)
        self.logger.debug("Available epochs: " + str(available_epochs))

        if req_epochs == 'all':
            epochs = available_epochs
        elif req_epochs == 'all-vast':
            epochs = available_epochs
            for racs_epoch in RACS_EPOCHS:
                if racs_epoch in epochs:
                    epochs.remove(racs_epoch)
        else:
            epochs = []
            if isinstance(req_epochs, list):
                epoch_iter = req_epochs
            elif isinstance(req_epochs, int):
                epoch_iter = [req_epochs]
            else:
                epoch_iter = req_epochs.split(',')

            for epoch in epoch_iter:
                if isinstance(epoch, int):
                    epoch = str(epoch)
                if epoch in available_epochs:
                    epochs.append(epoch)
                else:
                    epoch_x = f"{epoch}x"
                    self.logger.debug(
                        f"Epoch {epoch} is not available. Trying {epoch_x}"
                    )
                    if epoch_x in available_epochs:
                        epochs.append(epoch_x)
                        self.logger.debug(f"Epoch {epoch_x} available.")
                    else:
                        self.logger.info(
                            f"Epoch {epoch_x} is not available."
                        )

        # survey check
        self._check_survey(epochs)

        if self.racs:
            self.logger.warning('RACS data selected!')
            self.logger.warning(
                'Remember RACS data supplied by VAST is not final '
                'and results may vary.'
            )

        if len(epochs) == 0:
            raise QueryInitError(
                "None of the requested epochs are available"
            )

        return epochs

    def _check_survey(self, epochs: list) -> None:
        """
        Check which surveys are being queried (e.g. RACS, pilot/full VAST).

        Args:
            epochs: Requested epochs to query
        """

        self.racs = False
        self.vast_p1 = False
        self.vast_p2 = False
        self.vast_full = False

        non_full_epochs = RACS_EPOCHS + P1_EPOCHS + P2_EPOCHS
        all_epochs = RELEASED_EPOCHS.keys()
        full_epochs = set(all_epochs) - set(non_full_epochs)

        epochs_set = set(epochs)
        if len(epochs_set & set(RACS_EPOCHS)) > 0:
            self.racs = True
        if len(epochs_set & set(P1_EPOCHS)) > 0:
            self.vast_p1 = True
        if len(epochs_set & set(P2_EPOCHS)) > 0:
            self.vast_p2 = True
        if len(epochs_set & set(full_epochs)) > 0:
            self.vast_full = True

        self.logger.debug(f"self.racs: {self.racs}")
        self.logger.debug(f"self.vast_p1: {self.vast_p1}")
        self.logger.debug(f"self.vast_p2: {self.vast_p2}")
        self.logger.debug(f"self.vast_full: {self.vast_full}")

    def _get_stokes(self, req_stokes: str) -> str:
        """
        Set the stokes Parameter

        Args:
            req_stokes: Requested stokes parameter to check.

        Returns:
            Valid stokes parameter.

        Raises:
            ValueError: Entered Stokes parameter is not valid.
            ValueError: Stokes V is not supported with RACS.
        """

        valid = ["I", "Q", "U", "V"]

        if req_stokes.upper() not in valid:
            raise ValueError(
                "Stokes {} is not valid!".format(req_stokes.upper())
            )
        elif self.racs and req_stokes.upper() == 'V':
            raise ValueError(
                "Stokes V is not supported with RACS!"
            )
        else:
            return req_stokes.upper()

__init__(coords=None, source_names=None, epochs='1', stokes='I', crossmatch_radius=5.0, max_sep=1.5, use_tiles=True, use_islands=False, base_folder=None, matches_only=False, no_rms=False, search_around_coordinates=False, output_dir='.', planets=None, ncpu=2, sort_output=False, forced_fits=False, forced_cluster_threshold=1.5, forced_allow_nan=False, incl_observed=False, corrected_data=False, post_processed_data=True, search_all_fields=False, scheduler='processes')

Constructor method.

Parameters:

Name Type Description Default
coords Optional[SkyCoord]

List of coordinates to query, defaults to None.

None
source_names Optional[List[str]]

List of source names, defaults to None.

None
epochs Union[str, List[str], List[int]]

Epochs to query. Can be specified with either a list or a comma-separated string. All available epochs can be queried by passing "all", and all available VAST epochs can be queried by passing "all-vast". Defaults to "1".

'1'
stokes str

Stokes parameter to query.

'I'
crossmatch_radius float

Crossmatch radius in arcsec, defaults to 5.0.

5.0
max_sep float

Maximum separation of source from beam centre in degrees, defaults to 1.5.

1.5
use_tiles bool

Query tiles rather than combined mosaics, defaults to False.

True
use_islands bool

Use selavy islands rather than components, defaults to False.

False
base_folder Optional[str]

Path to base folder if using default directory structure, defaults to 'None'.

None
matches_only bool

Only produce data products for sources with a selavy match, defaults to False.

False
no_rms bool

When set to True the estimate of the background RMS around each source, will not be performed, defaults to False.

False
search_around_coordinates bool

When set to True, all matches to a searched coordinate are returned, instead of only the closest match.

False
output_dir str

Output directory to place all results in, defaults to ".".

'.'
planets Optional[List[str]]

List of planets to search for, defaults to None.

None
ncpu int

Number of CPUs to use, defaults to 2.

2
sort_output bool

Sorts the output into individual source directories, defaults to False.

False
forced_fits bool

Turns on the option to perform forced fits on the locations queried, defaults to False.

False
forced_cluster_threshold float

The cluster_threshold value passed to the forced photometry. Beam width distance limit for which a cluster is formed for extraction, defaults to 3.0.

1.5
forced_allow_nan bool

allow_nan value passed to the forced photometry. If False then any cluster containing a NaN is ignored. Defaults to False.

False
incl_observed bool

Include epochs that have been observed, but not released, in the query. This should only be used when finding fields, not querying data. Defaults to False.

False
corrected_data bool

Access the corrected data. Only relevant if tiles is True. Defaults to False.

False
post_processed_data bool

Access the post-processed data. Only relevant if tiles is True. Defaults to True.

True
search_all_fields bool

If True, return all data at the requested positions regardless of field. If False, only return data from the best (closest) field in each epoch.

False
scheduler str

Dask scheduling option to use. Options are "processes" (parallel processing) or "single-threaded". Defaults to "processes".

'processes'

Returns:

Type Description
None

None

Raises:

Type Description
ValueError

If the number of CPUs requested exceeds the total available.

QueryInitError

No coordinates or source names have been provided.

QueryInitError

Forced fits and search around coordinates options have both been selected.

QueryInitError

Number of source names provided does not match the number of coordinates.

ValueError

Planet provided is not a valid planet.

QueryInitError

Base folder could not be determined.

QueryInitError

SIMBAD search failed.

QueryInitError

Base folder cannot be found.

QueryInitError

Base folder cannot be found.

QueryInitError

Problems found in query settings.

QueryInitError

Invalid scheduler option requested.

Source code in vasttools/query.py
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
def __init__(
    self,
    coords: Optional[SkyCoord] = None,
    source_names: Optional[List[str]] = None,
    epochs: Union[str, List[str], List[int]] = "1",
    stokes: str = "I",
    crossmatch_radius: float = 5.0,
    max_sep: float = 1.5,
    use_tiles: bool = True,
    use_islands: bool = False,
    base_folder: Optional[str] = None,
    matches_only: bool = False,
    no_rms: bool = False,
    search_around_coordinates: bool = False,
    output_dir: str = ".",
    planets: Optional[List[str]] = None,
    ncpu: int = 2,
    sort_output: bool = False,
    forced_fits: bool = False,
    forced_cluster_threshold: float = 1.5,
    forced_allow_nan: bool = False,
    incl_observed: bool = False,
    corrected_data: bool = False,
    post_processed_data: bool = True,
    search_all_fields: bool = False,
    scheduler: str = 'processes',
) -> None:
    """
    Constructor method.

    Args:
        coords: List of coordinates to query, defaults to None.
        source_names: List of source names, defaults to None.
        epochs: Epochs to query. Can be specified with either a list
            or a comma-separated string. All available epochs can be
            queried by passing "all", and all available VAST epochs can be
            queried by passing "all-vast". Defaults to "1".
        stokes: Stokes parameter to query.
        crossmatch_radius: Crossmatch radius in arcsec, defaults to 5.0.
        max_sep: Maximum separation of source from beam centre
            in degrees, defaults to 1.5.
        use_tiles: Query tiles rather than combined mosaics,
            defaults to `False`.
        use_islands: Use selavy islands rather than components,
            defaults to `False`.
        base_folder: Path to base folder if using default directory
            structure, defaults to 'None'.
        matches_only: Only produce data products for sources with a
            selavy match, defaults to `False`.
        no_rms: When set to `True` the estimate of the background RMS
            around each source, will not be performed,
            defaults to `False`.
        search_around_coordinates: When set to `True`, all matches to a
            searched coordinate are returned, instead of only the closest
            match.
        output_dir: Output directory to place all results in,
            defaults to ".".
        planets: List of planets to search for, defaults to None.
        ncpu: Number of CPUs to use, defaults to 2.
        sort_output: Sorts the output into individual source
            directories, defaults to `False`.
        forced_fits: Turns on the option to perform forced fits
            on the locations queried, defaults to `False`.
        forced_cluster_threshold: The cluster_threshold value passed to
            the forced photometry. Beam width distance limit for which a
            cluster is formed for extraction, defaults to 3.0.
        forced_allow_nan: `allow_nan` value passed to the
            forced photometry. If False then any cluster containing a
            NaN is ignored. Defaults to False.
        incl_observed: Include epochs that have been observed, but not
            released, in the query. This should only be used when finding
            fields, not querying data. Defaults to False.
        corrected_data: Access the corrected data. Only relevant if
            `tiles` is `True`. Defaults to `False`.
        post_processed_data: Access the post-processed data. Only relevant
            if `tiles` is `True`. Defaults to `True`.
        search_all_fields: If `True`, return all data at the requested
            positions regardless of field. If `False`, only return data
            from the best (closest) field in each epoch.
        scheduler: Dask scheduling option to use. Options are "processes"
            (parallel processing) or "single-threaded". Defaults to
            "processes".

    Returns:
        None

    Raises:
        ValueError: If the number of CPUs requested exceeds the total
            available.
        QueryInitError: No coordinates or source names have been provided.
        QueryInitError: Forced fits and search around coordinates options
            have both been selected.
        QueryInitError: Number of source names provided does not match the
            number of coordinates.
        ValueError: Planet provided is not a valid planet.
        QueryInitError: Base folder could not be determined.
        QueryInitError: SIMBAD search failed.
        QueryInitError: Base folder cannot be found.
        QueryInitError: Base folder cannot be found.
        QueryInitError: Problems found in query settings.
        QueryInitError: Invalid scheduler option requested.
    """
    self.logger = logging.getLogger('vasttools.find_sources.Query')

    install_mp_handler(logger=self.logger)

    if source_names is None:
        source_names = []
    if planets is None:
        planets = []

    self.source_names = np.array(source_names)
    self.simbad_names = None

    self.corrected_data = corrected_data
    self.post_processed_data = post_processed_data

    if coords is None:
        self.coords = coords
    elif coords.isscalar:
        self.coords = SkyCoord([coords.ra], [coords.dec])
    else:
        self.coords = coords

    if self.coords is None:
        len_coords = 0
    else:
        len_coords = self.coords.shape[0]

    if ncpu > HOST_NCPU:
        raise ValueError(
            "Number of CPUs requested ({}) "
            "exceeds number available ({})".format(
                ncpu,
                HOST_NCPU
            )
        )
    self.ncpu = ncpu
    self.logger.debug(f"Using {self.ncpu} CPUs")

    if coords is None and len(source_names) == 0 and len(planets) == 0:
        raise QueryInitError(
            "No coordinates or source names have been provided!"
            " Check inputs and try again!"
        )

    if forced_fits and search_around_coordinates:
        raise QueryInitError(
            "Forced fits and search around coordinates mode cannot be"
            " used together! Check inputs and try again."
        )

    if (
        self.coords is not None and
        len(self.source_names) > 0 and
        len(self.source_names) != len_coords
    ):
        raise QueryInitError(
            "The number of entered source names ({}) does not match the"
            " number of coordinates ({})!".format(
                len(self.source_names),
                len_coords
            )
        )

    if self.coords is not None and len(self.source_names) == 0:
        source_names = [
            'source_' + i.to_string(
                'hmsdms', sep="", precision=1
            ).replace(" ", "") for i in self.coords
        ]
        self.source_names = np.array(source_names)

    if self.coords is None:
        if len(source_names) != 0:
            num_sources = len(source_names)
            self.coords, self.simbad_names = simbad_search(
                source_names, logger=self.logger
            )
            num_simbad = len(list(filter(None, self.simbad_names)))
            if self.coords is not None:
                simbad_msg = "SIMBAD search found {}/{} source(s):".format(
                    num_simbad,
                    num_sources
                )
                self.logger.info(simbad_msg)
                names = zip(self.simbad_names, self.source_names)
                for simbad_name, query_name in names:
                    if simbad_name:
                        self.logger.info(
                            '{}: {}'.format(query_name, simbad_name)
                        )
                    else:
                        self.logger.info(
                            '{}: No match.'.format(query_name)
                        )
                if self.logger is None:
                    warnings.warn(simbad_msg)
            else:
                self.logger.error(
                    "SIMBAD search failed!"
                )
                raise QueryInitError(
                    "SIMBAD search failed!"
                )

    planets = [i.lower() for i in planets]
    valid_planets = sum([i in ALLOWED_PLANETS for i in planets])

    if valid_planets != len(planets):
        self.logger.error(
            "Invalid planet object provided!"
        )
        raise ValueError(
            "Invalid planet object provided!"
        )

    self.planets = planets

    self.settings = {}

    if base_folder is None:
        the_base_folder = os.getenv('VAST_DATA_DIR')
        if the_base_folder is None:
            raise QueryInitError(
                "The base folder directory could not be determined!"
                " Either the system environment 'VAST_DATA_DIR' must be"
                " defined or the 'base_folder' argument defined when"
                " initialising the query."
            )
    else:
        the_base_folder = os.path.abspath(str(base_folder))

    if not os.path.isdir(the_base_folder):
        raise QueryInitError(
            "Base folder {} not found!".format(
                the_base_folder
            )
        )

    self.base_folder = the_base_folder

    self.settings['incl_observed'] = incl_observed
    self.settings['epochs'] = self._get_epochs(epochs)
    self.settings['stokes'] = self._get_stokes(stokes)

    self.settings['crossmatch_radius'] = Angle(
        crossmatch_radius, unit=u.arcsec
    )
    self.settings['max_sep'] = max_sep

    self.settings['islands'] = use_islands
    self.settings['tiles'] = use_tiles
    self.settings['no_rms'] = no_rms
    self.settings['matches_only'] = matches_only
    self.settings['search_around'] = search_around_coordinates
    self.settings['sort_output'] = sort_output
    self.settings['forced_fits'] = forced_fits
    self.settings[
        'forced_cluster_threshold'
    ] = forced_cluster_threshold
    self.settings['forced_allow_nan'] = forced_allow_nan

    self.settings['output_dir'] = output_dir
    self.settings['search_all_fields'] = search_all_fields

    scheduler_options = ['processes', 'single-threaded']
    if scheduler not in scheduler_options:
        raise QueryInitError(
            f"{scheduler} is not a suitable scheduler option. Please "
            f"select from {scheduler_options}"
        )
    self.settings['scheduler'] = scheduler

    # Going to need this so load it now
    self._epoch_fields = get_fields_per_epoch_info()

    if not os.path.isdir(self.base_folder):
        self.logger.critical(
            "The base directory {} does not exist!".format(
                self.base_folder
            )
        )
        raise QueryInitError(
            "The base directory {} does not exist!".format(
                self.base_folder
            )
        )

    settings_ok = self._validate_settings()

    if not settings_ok:
        self.logger.critical("Problems found in query settings!")
        self.logger.critical("Please address and try again.")
        raise QueryInitError((
            "Problems found in query settings!"
            "\nPlease address and try again."
        ))

    all_data_available = self._check_data_availability()
    if all_data_available:
        self.logger.info("All data available!")
    else:
        self.logger.warning(
            "Not all requested data is available! See above for details."
        )
        self.logger.warning(
            "Query will continue run, but proceed with caution."
        )

    if self.coords is not None:
        self.query_df = self._build_catalog()
        if self.query_df.empty:
            raise QueryInitError(
                'No sources remaining. None of the entered coordinates'
                ' are found in the VAST Pilot survey footprint!'
            )
    else:
        self.query_df = None

    self.fields_found = False

find_fields()

Find the corresponding field for each source.

Planet fields are also found here if any are selected.

Returns:

Type Description
None

None

Raises:

Type Description
Exception

No sources are found within the requested footprint.

Source code in vasttools/query.py
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
def find_fields(self) -> None:
    """
    Find the corresponding field for each source.

    Planet fields are also found here if any are selected.

    Returns:
        None

    Raises:
        Exception: No sources are found within the requested footprint.
    """

    if self.racs:
        base_fc = 'RACS'
    else:
        base_fc = 'VAST'

    self.logger.info(
        f"Matching queried sources to {base_fc} fields..."
    )

    base_epoch = BASE_EPOCHS[base_fc]

    fields = Fields(base_epoch)
    field_centres = load_field_centres()

    field_centres = field_centres.loc[
        field_centres['field'].str.contains(base_fc)
    ].reset_index()

    field_centres_sc = SkyCoord(
        field_centres["centre-ra"],
        field_centres["centre-dec"],
        unit=(u.deg, u.deg)
    )

    # if RACS is being use we convert all the names to 'VAST'
    # to match the VAST field names, makes matching easier.
    if base_fc != 'VAST':
        field_centres['field'] = [
            f.replace("RACS", "VAST") for f in field_centres.field
        ]

    field_centre_names = field_centres.field

    if self.query_df is not None:
        self.fields_df = self.query_df.copy()

        # _field_matching returns 7 arguments. This dict specifies types,
        # O for object (in this case, lists) and U for unicode string.
        meta = {
            0: 'O',
            1: 'U',
            2: 'O',
            3: 'O',
            4: 'O',
            5: 'O',
            6: 'O',
        }
        self.logger.debug("Running field matching...")

        self.fields_df[[
            'fields',
            'primary_field',
            'epochs',
            'field_per_epoch',
            'sbids',
            'dates',
            'freqs'
        ]] = (
            dd.from_pandas(self.fields_df, self.ncpu)
            .apply(
                self._field_matching,
                args=(
                    fields.direction,
                    fields.fields.FIELD_NAME,
                    field_centres_sc,
                    field_centre_names
                ),
                meta=meta,
                axis=1,
                result_type='expand'
            ).compute(num_workers=self.ncpu,
                      scheduler=self.settings['scheduler']
                      )
        )

        self.logger.debug("Finished field matching.")
        self.fields_df = self.fields_df.dropna()

        if self.fields_df.empty:
            raise Exception(
                "No requested sources are within the requested footprint!")

        self.fields_df = self.fields_df.explode(
            'field_per_epoch'
        ).reset_index(drop=True)

        field_per_epoch = self.fields_df['field_per_epoch'].tolist()

        self.fields_df[
            ['epoch', 'field', 'sbid', 'dateobs', 'frequency']
        ] = pd.DataFrame(
            field_per_epoch,
            index=self.fields_df.index
        )

        to_drop = [
            'field_per_epoch',
            'epochs',
            'sbids',
            'dates',
            'freqs'
        ]
        self.logger.debug(self.fields_df['name'])
        self.logger.debug(self.fields_df['dateobs'])
        self.fields_df = self.fields_df.drop(
            labels=to_drop, axis=1
        ).sort_values(
            by=['name', 'dateobs']
        ).reset_index(drop=True)

        self.fields_df['planet'] = False
    else:
        self.fields_df = None

    # Handle Planets
    if len(self.planets) > 0:
        self.logger.debug(f"Searching for planets: {self.planets}")
        planet_fields = self._search_planets()

        if self.fields_df is None:
            self.fields_df = planet_fields
        else:
            self.fields_df = pd.concat(
                [self.fields_df, planet_fields]
            ).reset_index(drop=True)

    self.logger.debug(self.fields_df)

    if self.query_df is None:
        prev_num = 0
    else:
        prev_num = self.query_df.shape[0]

    if self.planets is not None:
        prev_num += len(self.planets)

    self.num_sources_searched = self.fields_df.name.unique().shape[0]

    if self.racs:
        self.logger.info(
            f"{self.num_sources_searched}/{prev_num} "
            "sources in RACS & VAST footprint."
        )
    else:
        self.logger.info(
            f"{self.num_sources_searched}/{prev_num} "
            "sources in VAST footprint."
        )

    self.fields_df['dateobs'] = pd.to_datetime(
        self.fields_df['dateobs']
    )

    # All field names should start with VAST, not RACS
    self.fields_df['field'] = self.fields_df['field'].str.replace("RACS",
                                                                  "VAST"
                                                                  )

    self.logger.info("Done.")
    self.fields_found = True

find_sources()

Run source search. Results are stored in attributes.

Steps: 1. Run find_fields if not already run. 2. Add the file paths to each measurement point. 3. Obtain forced fits if requested. 4. Run selavy matching and upper limit fetching. 5. Package up results into vasttools.source.Source objects.

Returns:

Type Description
None

None

Raises:

Type Description
Exception

find_sources cannot be run with the incl_observed option

Source code in vasttools/query.py
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
def find_sources(self) -> None:
    """
    Run source search. Results are stored in attributes.

    Steps:
    1. Run find_fields if not already run.
    2. Add the file paths to each measurement point.
    3. Obtain forced fits if requested.
    4. Run selavy matching and upper limit fetching.
    5. Package up results into vasttools.source.Source objects.

    Returns:
        None

    Raises:
        Exception: find_sources cannot be run with the incl_observed option
    """

    if self.settings['incl_observed']:
        raise Exception(
            'find_sources cannot be run with the incl_observed option'
        )

    self.logger.debug('Running find_sources...')

    if self.fields_found is False:
        self.find_fields()

    self.logger.info("Finding sources in VAST data...")

    self.sources_df = self.fields_df.sort_values(
        by=['name', 'dateobs']
    ).reset_index(drop=True)

    self.logger.debug("Adding files...")
    self.sources_df[
        ['selavy', 'image', 'rms']
    ] = self.sources_df[['epoch', 'field', 'sbid']].apply(
        self._add_files,
        axis=1,
        result_type='expand'
    )

    self._validate_files()

    if self.settings['forced_fits']:
        self.logger.info("Obtaining forced fits...")
        meta = {
            'f_island_id': 'U',
            'f_component_id': 'U',
            'f_ra_deg_cont': 'f',
            'f_dec_deg_cont': 'f',
            'f_flux_peak': 'f',
            'f_flux_peak_err': 'f',
            'f_flux_int': 'f',
            'f_flux_int_err': 'f',
            'f_chi_squared_fit': 'f',
            'f_rms_image': 'f',
            'f_maj_axis': 'f',
            'f_min_axis': 'f',
            'f_pos_ang': 'f',
        }

        f_results = (
            dd.from_pandas(self.sources_df, self.ncpu)
            .groupby('image')
            .apply(
                self._get_forced_fits,
                cluster_threshold=(
                    self.settings['forced_cluster_threshold']
                ),
                allow_nan=self.settings['forced_allow_nan'],
                meta=meta,
            ).compute(num_workers=self.ncpu,
                      scheduler=self.settings['scheduler']
                      )
        )

        if not f_results.empty:
            if isinstance(f_results.index, pd.MultiIndex):
                f_results.index = f_results.index.droplevel()
        else:
            self.settings['forced_fits'] = False

        self.logger.info("Forced fitting finished.")

    self.logger.debug("Getting components...")
    results = (
        dd.from_pandas(self.sources_df, self.ncpu)
        .groupby('selavy')
        .apply(
            self._get_components,
            meta=self._get_selavy_meta(),
        ).compute(num_workers=self.ncpu,
                  scheduler=self.settings['scheduler']
                  )
    )

    self.logger.debug("Selavy components succesfully added.")
    self.logger.debug(results)

    if self.settings['islands']:
        results['rms_image'] = results['background_noise']
        results['flux_peak_err'] = results['background_noise']

    if not results.empty:
        if isinstance(results.index, pd.MultiIndex):
            results.index = results.index.droplevel()

    if self.settings['search_around']:
        results = results.set_index('index')

    if self.settings['forced_fits']:
        results = results.merge(
            f_results, left_index=True, right_index=True
        )

        del f_results

    if self.settings['search_around']:
        how = 'inner'
    else:
        how = 'left'

    self.crossmatch_results = self.sources_df.merge(
        results, how=how, left_index=True, right_index=True
    )
    self.logger.debug("Crossmatch results:")
    self.logger.debug(self.crossmatch_results)

    meta = {'name': 'O'}

    self.num_sources_detected = (
        self.crossmatch_results.groupby('name').agg({
            'detection': any
        }).sum()
    )
    self.logger.debug(f"{self.num_sources_detected} sources detected:")

    if self.settings['search_around']:
        self.results = self.crossmatch_results.rename(
            columns={'#': 'distance'}
        )
    else:
        npart = min(self.ncpu, self.crossmatch_results.name.nunique())
        self.results = (
            dd.from_pandas(self.crossmatch_results, npart)
            .groupby('name')
            .apply(
                self._init_sources,
                meta=meta,
            ).compute(num_workers=npart,
                      scheduler=self.settings['scheduler']
                      )
        )
        self.results = self.results.dropna()

    self.logger.info("Source finding complete!")

save_search_around_results(sort_output=False)

Save results from cone search.

Parameters:

Name Type Description Default
sort_output bool

Whether to sort the output, defaults to False.

False

Returns:

Type Description
None

None

Source code in vasttools/query.py
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
def save_search_around_results(self, sort_output: bool = False) -> None:
    """
    Save results from cone search.

    Args:
        sort_output: Whether to sort the output, defaults to `False`.

    Returns:
        None
    """
    meta = {}
    # also have the sort output setting as a function
    # input in case of interactive use.
    if self.settings['sort_output']:
        sort_output = True
    result = (
        dd.from_pandas(
            self.results.drop([
                'fields',
                'stokes',
                'skycoord',
                'selavy',
                'image',
                'rms',
            ], axis=1), self.ncpu)
        .groupby('name')
        .apply(
            self._write_search_around_results,
            sort_output=sort_output,
            meta=meta,
        ).compute(num_workers=self.ncpu,
                  scheduler=self.settings['scheduler']
                  )
    )

write_find_fields(outname=None)

Write the results of a field search to file.

Parameters:

Name Type Description Default
outname Optional[str]

Name of file to write output to, defaults to None, which will name the file 'find_fields_results.csv'.

None

Returns:

Type Description
None

None

Source code in vasttools/query.py
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
def write_find_fields(self, outname: Optional[str] = None) -> None:
    """
    Write the results of a field search to file.

    Args:
        outname: Name of file to write output to, defaults to None, which
            will name the file 'find_fields_results.csv'.

    Returns:
        None
    """
    if self.fields_found is False:
        self.find_fields()

    if outname is None:
        name = 'find_fields_result.csv'
    else:
        name = outname + '.pkl'

    outdir = self.settings['output_dir']
    if outdir is not None:
        name = os.path.join(outdir, name)

    self.fields_df[[
        'name',
        'ra',
        'dec',
        'field',
        'epoch',
        'sbid',
        'dateobs',
        'primary_field'
    ]].to_csv(name, index=False)

    self.logger.info('Find fields output saved as {}.'.format(
        name
    ))

QueryInitError

Bases: Exception

A defined error for a problem encountered in the initialisation.

Source code in vasttools/query.py
71
72
73
74
75
class QueryInitError(Exception):
    """
    A defined error for a problem encountered in the initialisation.
    """
    pass