[1846] | 1 | """This module defines the scantable class."""
|
---|
| 2 |
|
---|
[1697] | 3 | import os
|
---|
[2751] | 4 | import re
|
---|
[2315] | 5 | import tempfile
|
---|
[1948] | 6 | import numpy
|
---|
[1691] | 7 | try:
|
---|
| 8 | from functools import wraps as wraps_dec
|
---|
| 9 | except ImportError:
|
---|
| 10 | from asap.compatibility import wraps as wraps_dec
|
---|
| 11 |
|
---|
[1824] | 12 | from asap.env import is_casapy
|
---|
[876] | 13 | from asap._asap import Scantable
|
---|
[2004] | 14 | from asap._asap import filler, msfiller
|
---|
[1824] | 15 | from asap.parameters import rcParams
|
---|
[1862] | 16 | from asap.logging import asaplog, asaplog_post_dec
|
---|
[1824] | 17 | from asap.selector import selector
|
---|
| 18 | from asap.linecatalog import linecatalog
|
---|
[1600] | 19 | from asap.coordinate import coordinate
|
---|
[1859] | 20 | from asap.utils import _n_bools, mask_not, mask_and, mask_or, page
|
---|
[1907] | 21 | from asap.asapfitter import fitter
|
---|
[102] | 22 |
|
---|
[1689] | 23 | def preserve_selection(func):
|
---|
[1691] | 24 | @wraps_dec(func)
|
---|
[1689] | 25 | def wrap(obj, *args, **kw):
|
---|
| 26 | basesel = obj.get_selection()
|
---|
[1857] | 27 | try:
|
---|
| 28 | val = func(obj, *args, **kw)
|
---|
| 29 | finally:
|
---|
| 30 | obj.set_selection(basesel)
|
---|
[1689] | 31 | return val
|
---|
| 32 | return wrap
|
---|
| 33 |
|
---|
[1846] | 34 | def is_scantable(filename):
|
---|
| 35 | """Is the given file a scantable?
|
---|
[1689] | 36 |
|
---|
[1846] | 37 | Parameters:
|
---|
| 38 |
|
---|
| 39 | filename: the name of the file/directory to test
|
---|
| 40 |
|
---|
| 41 | """
|
---|
[1883] | 42 | if ( os.path.isdir(filename)
|
---|
| 43 | and os.path.exists(filename+'/table.info')
|
---|
| 44 | and os.path.exists(filename+'/table.dat') ):
|
---|
| 45 | f=open(filename+'/table.info')
|
---|
| 46 | l=f.readline()
|
---|
| 47 | f.close()
|
---|
[2753] | 48 | match_pattern = '^Type = (Scantable)? *$'
|
---|
[2751] | 49 | if re.match(match_pattern,l):
|
---|
[1883] | 50 | return True
|
---|
| 51 | else:
|
---|
| 52 | return False
|
---|
| 53 | else:
|
---|
| 54 | return False
|
---|
| 55 | ## return (os.path.isdir(filename)
|
---|
| 56 | ## and not os.path.exists(filename+'/table.f1')
|
---|
| 57 | ## and os.path.exists(filename+'/table.info'))
|
---|
[1697] | 58 |
|
---|
[1883] | 59 | def is_ms(filename):
|
---|
| 60 | """Is the given file a MeasurementSet?
|
---|
[1697] | 61 |
|
---|
[1883] | 62 | Parameters:
|
---|
| 63 |
|
---|
| 64 | filename: the name of the file/directory to test
|
---|
| 65 |
|
---|
| 66 | """
|
---|
| 67 | if ( os.path.isdir(filename)
|
---|
| 68 | and os.path.exists(filename+'/table.info')
|
---|
| 69 | and os.path.exists(filename+'/table.dat') ):
|
---|
| 70 | f=open(filename+'/table.info')
|
---|
| 71 | l=f.readline()
|
---|
| 72 | f.close()
|
---|
| 73 | if ( l.find('Measurement Set') != -1 ):
|
---|
| 74 | return True
|
---|
| 75 | else:
|
---|
| 76 | return False
|
---|
| 77 | else:
|
---|
| 78 | return False
|
---|
[2186] | 79 |
|
---|
| 80 | def normalise_edge_param(edge):
|
---|
| 81 | """\
|
---|
| 82 | Convert a given edge value to a one-dimensional array that can be
|
---|
| 83 | given to baseline-fitting/subtraction functions.
|
---|
| 84 | The length of the output value will be an even because values for
|
---|
| 85 | the both sides of spectra are to be contained for each IF. When
|
---|
| 86 | the length is 2, the values will be applied to all IFs. If the length
|
---|
| 87 | is larger than 2, it will be 2*ifnos().
|
---|
| 88 | Accepted format of edge include:
|
---|
| 89 | * an integer - will be used for both sides of spectra of all IFs.
|
---|
| 90 | e.g. 10 is converted to [10,10]
|
---|
[2277] | 91 | * an empty list/tuple [] - converted to [0, 0] and used for all IFs.
|
---|
[2186] | 92 | * a list/tuple containing an integer - same as the above case.
|
---|
| 93 | e.g. [10] is converted to [10,10]
|
---|
| 94 | * a list/tuple containing two integers - will be used for all IFs.
|
---|
| 95 | e.g. [5,10] is output as it is. no need to convert.
|
---|
| 96 | * a list/tuple of lists/tuples containing TWO integers -
|
---|
| 97 | each element of edge will be used for each IF.
|
---|
[2277] | 98 | e.g. [[5,10],[15,20]] - [5,10] for IF[0] and [15,20] for IF[1].
|
---|
| 99 |
|
---|
| 100 | If an element contains the same integer values, the input 'edge'
|
---|
| 101 | parameter can be given in a simpler shape in the following cases:
|
---|
[2186] | 102 | ** when len(edge)!=2
|
---|
[2277] | 103 | any elements containing the same values can be replaced
|
---|
| 104 | to single integers.
|
---|
| 105 | e.g. [[15,15]] can be simplified to [15] (or [15,15] or 15 also).
|
---|
| 106 | e.g. [[1,1],[2,2],[3,3]] can be simplified to [1,2,3].
|
---|
[2186] | 107 | ** when len(edge)=2
|
---|
| 108 | care is needed for this case: ONLY ONE of the
|
---|
| 109 | elements can be a single integer,
|
---|
| 110 | e.g. [[5,5],[10,10]] can be simplified to [5,[10,10]]
|
---|
[2277] | 111 | or [[5,5],10], but can NOT be simplified to [5,10].
|
---|
[2186] | 112 | when [5,10] given, it is interpreted as
|
---|
[2277] | 113 | [[5,10],[5,10],[5,10],...] instead, as shown before.
|
---|
[2186] | 114 | """
|
---|
| 115 | from asap import _is_sequence_or_number as _is_valid
|
---|
| 116 | if isinstance(edge, list) or isinstance(edge, tuple):
|
---|
| 117 | for edgepar in edge:
|
---|
| 118 | if not _is_valid(edgepar, int):
|
---|
| 119 | raise ValueError, "Each element of the 'edge' tuple has \
|
---|
| 120 | to be a pair of integers or an integer."
|
---|
| 121 | if isinstance(edgepar, list) or isinstance(edgepar, tuple):
|
---|
| 122 | if len(edgepar) != 2:
|
---|
| 123 | raise ValueError, "Each element of the 'edge' tuple has \
|
---|
| 124 | to be a pair of integers or an integer."
|
---|
| 125 | else:
|
---|
| 126 | if not _is_valid(edge, int):
|
---|
| 127 | raise ValueError, "Parameter 'edge' has to be an integer or a \
|
---|
| 128 | pair of integers specified as a tuple. \
|
---|
| 129 | Nested tuples are allowed \
|
---|
| 130 | to make individual selection for different IFs."
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | if isinstance(edge, int):
|
---|
| 134 | edge = [ edge, edge ] # e.g. 3 => [3,3]
|
---|
| 135 | elif isinstance(edge, list) or isinstance(edge, tuple):
|
---|
| 136 | if len(edge) == 0:
|
---|
| 137 | edge = [0, 0] # e.g. [] => [0,0]
|
---|
| 138 | elif len(edge) == 1:
|
---|
| 139 | if isinstance(edge[0], int):
|
---|
| 140 | edge = [ edge[0], edge[0] ] # e.g. [1] => [1,1]
|
---|
| 141 |
|
---|
| 142 | commonedge = True
|
---|
| 143 | if len(edge) > 2: commonedge = False
|
---|
| 144 | else:
|
---|
| 145 | for edgepar in edge:
|
---|
| 146 | if isinstance(edgepar, list) or isinstance(edgepar, tuple):
|
---|
| 147 | commonedge = False
|
---|
| 148 | break
|
---|
| 149 |
|
---|
| 150 | if commonedge:
|
---|
| 151 | if len(edge) > 1:
|
---|
| 152 | norm_edge = edge
|
---|
| 153 | else:
|
---|
| 154 | norm_edge = edge + edge
|
---|
| 155 | else:
|
---|
| 156 | norm_edge = []
|
---|
| 157 | for edgepar in edge:
|
---|
| 158 | if isinstance(edgepar, int):
|
---|
| 159 | norm_edge += [edgepar, edgepar]
|
---|
| 160 | else:
|
---|
| 161 | norm_edge += edgepar
|
---|
| 162 |
|
---|
| 163 | return norm_edge
|
---|
| 164 |
|
---|
| 165 | def raise_fitting_failure_exception(e):
|
---|
| 166 | msg = "The fit failed, possibly because it didn't converge."
|
---|
| 167 | if rcParams["verbose"]:
|
---|
| 168 | asaplog.push(str(e))
|
---|
| 169 | asaplog.push(str(msg))
|
---|
| 170 | else:
|
---|
| 171 | raise RuntimeError(str(e)+'\n'+msg)
|
---|
| 172 |
|
---|
[2189] | 173 | def pack_progress_params(showprogress, minnrow):
|
---|
| 174 | return str(showprogress).lower() + ',' + str(minnrow)
|
---|
| 175 |
|
---|
[2767] | 176 | def pack_blinfo(blinfo, maxirow):
|
---|
| 177 | """\
|
---|
| 178 | convert a dictionary or a list of dictionaries of baseline info
|
---|
| 179 | into a list of comma-separated strings.
|
---|
| 180 | """
|
---|
| 181 | if isinstance(blinfo, dict):
|
---|
| 182 | res = do_pack_blinfo(blinfo, maxirow)
|
---|
| 183 | return [res] if res != '' else []
|
---|
| 184 | elif isinstance(blinfo, list) or isinstance(blinfo, tuple):
|
---|
| 185 | res = []
|
---|
| 186 | for i in xrange(len(blinfo)):
|
---|
| 187 | resi = do_pack_blinfo(blinfo[i], maxirow)
|
---|
| 188 | if resi != '':
|
---|
| 189 | res.append(resi)
|
---|
| 190 | return res
|
---|
| 191 |
|
---|
| 192 | def do_pack_blinfo(blinfo, maxirow):
|
---|
| 193 | """\
|
---|
| 194 | convert a dictionary of baseline info for a spectrum into
|
---|
| 195 | a comma-separated string.
|
---|
| 196 | """
|
---|
| 197 | dinfo = {}
|
---|
| 198 | for key in ['row', 'blfunc', 'masklist']:
|
---|
| 199 | if blinfo.has_key(key):
|
---|
| 200 | val = blinfo[key]
|
---|
| 201 | if key == 'row':
|
---|
| 202 | irow = val
|
---|
| 203 | if isinstance(val, list) or isinstance(val, tuple):
|
---|
| 204 | slval = []
|
---|
| 205 | for i in xrange(len(val)):
|
---|
| 206 | if isinstance(val[i], list) or isinstance(val[i], tuple):
|
---|
| 207 | for j in xrange(len(val[i])):
|
---|
| 208 | slval.append(str(val[i][j]))
|
---|
| 209 | else:
|
---|
| 210 | slval.append(str(val[i]))
|
---|
| 211 | sval = ",".join(slval)
|
---|
| 212 | else:
|
---|
| 213 | sval = str(val)
|
---|
| 214 |
|
---|
| 215 | dinfo[key] = sval
|
---|
| 216 | else:
|
---|
| 217 | raise ValueError("'"+key+"' is missing in blinfo.")
|
---|
| 218 |
|
---|
| 219 | if irow >= maxirow: return ''
|
---|
| 220 |
|
---|
| 221 | for key in ['order', 'npiece', 'nwave']:
|
---|
| 222 | if blinfo.has_key(key):
|
---|
| 223 | val = blinfo[key]
|
---|
| 224 | if isinstance(val, list) or isinstance(val, tuple):
|
---|
| 225 | slval = []
|
---|
| 226 | for i in xrange(len(val)):
|
---|
| 227 | slval.append(str(val[i]))
|
---|
| 228 | sval = ",".join(slval)
|
---|
| 229 | else:
|
---|
| 230 | sval = str(val)
|
---|
| 231 | dinfo[key] = sval
|
---|
| 232 |
|
---|
| 233 | fspec_keys = {'poly': 'order', 'chebyshev': 'order', 'cspline': 'npiece', 'sinusoid': 'nwave'}
|
---|
| 234 |
|
---|
[2984] | 235 | fspec_key = fspec_keys[dinfo['blfunc']]
|
---|
[2767] | 236 | if not blinfo.has_key(fspec_key):
|
---|
| 237 | raise ValueError("'"+fspec_key+"' is missing in blinfo.")
|
---|
| 238 |
|
---|
| 239 | clip_params_n = 0
|
---|
| 240 | for key in ['clipthresh', 'clipniter']:
|
---|
| 241 | if blinfo.has_key(key):
|
---|
| 242 | clip_params_n += 1
|
---|
| 243 | dinfo[key] = str(blinfo[key])
|
---|
| 244 |
|
---|
| 245 | if clip_params_n == 0:
|
---|
| 246 | dinfo['clipthresh'] = '0.0'
|
---|
| 247 | dinfo['clipniter'] = '0'
|
---|
| 248 | elif clip_params_n != 2:
|
---|
| 249 | raise ValueError("both 'clipthresh' and 'clipniter' must be given for n-sigma clipping.")
|
---|
| 250 |
|
---|
| 251 | lf_params_n = 0
|
---|
| 252 | for key in ['thresh', 'edge', 'chan_avg_limit']:
|
---|
| 253 | if blinfo.has_key(key):
|
---|
| 254 | lf_params_n += 1
|
---|
| 255 | val = blinfo[key]
|
---|
| 256 | if isinstance(val, list) or isinstance(val, tuple):
|
---|
| 257 | slval = []
|
---|
| 258 | for i in xrange(len(val)):
|
---|
| 259 | slval.append(str(val[i]))
|
---|
| 260 | sval = ",".join(slval)
|
---|
| 261 | else:
|
---|
| 262 | sval = str(val)
|
---|
| 263 | dinfo[key] = sval
|
---|
| 264 |
|
---|
| 265 | if lf_params_n == 3:
|
---|
| 266 | dinfo['use_linefinder'] = 'true'
|
---|
[2810] | 267 | elif lf_params_n == 0:
|
---|
[2767] | 268 | dinfo['use_linefinder'] = 'false'
|
---|
| 269 | dinfo['thresh'] = ''
|
---|
| 270 | dinfo['edge'] = ''
|
---|
| 271 | dinfo['chan_avg_limit'] = ''
|
---|
| 272 | else:
|
---|
| 273 | raise ValueError("all of 'thresh', 'edge' and 'chan_avg_limit' must be given to use linefinder.")
|
---|
| 274 |
|
---|
[2984] | 275 | slblinfo = [dinfo['row'], dinfo['blfunc'], dinfo[fspec_key], dinfo['masklist'], \
|
---|
[2767] | 276 | dinfo['clipthresh'], dinfo['clipniter'], \
|
---|
| 277 | dinfo['use_linefinder'], dinfo['thresh'], dinfo['edge'], dinfo['chan_avg_limit']]
|
---|
| 278 |
|
---|
| 279 | return ":".join(slblinfo)
|
---|
| 280 |
|
---|
| 281 | def parse_fitresult(sres):
|
---|
| 282 | """\
|
---|
| 283 | Parse the returned value of apply_bltable() or sub_baseline() and
|
---|
| 284 | extract row number, the best-fit coefficients and rms, then pack
|
---|
| 285 | them into a dictionary.
|
---|
| 286 | The input value is generated by Scantable::packFittingResults() and
|
---|
| 287 | formatted as 'row:coeff[0],coeff[1],..,coeff[n-1]:rms'.
|
---|
| 288 | """
|
---|
| 289 | res = []
|
---|
| 290 | for i in xrange(len(sres)):
|
---|
| 291 | (srow, scoeff, srms) = sres[i].split(":")
|
---|
| 292 | row = int(srow)
|
---|
| 293 | rms = float(srms)
|
---|
| 294 | lscoeff = scoeff.split(",")
|
---|
| 295 | coeff = []
|
---|
| 296 | for j in xrange(len(lscoeff)):
|
---|
| 297 | coeff.append(float(lscoeff[j]))
|
---|
| 298 | res.append({'row': row, 'coeff': coeff, 'rms': rms})
|
---|
| 299 |
|
---|
| 300 | return res
|
---|
| 301 |
|
---|
[2882] | 302 | def is_number(s):
|
---|
| 303 | s = s.strip()
|
---|
| 304 | res = True
|
---|
| 305 | try:
|
---|
| 306 | a = float(s)
|
---|
| 307 | res = True
|
---|
| 308 | except:
|
---|
| 309 | res = False
|
---|
| 310 | finally:
|
---|
| 311 | return res
|
---|
| 312 |
|
---|
| 313 | def is_frequency(s):
|
---|
| 314 | s = s.strip()
|
---|
| 315 | return (s[-2:].lower() == "hz")
|
---|
| 316 |
|
---|
[2884] | 317 | def get_freq_by_string(s1, s2):
|
---|
| 318 | if not (is_number(s1) and is_frequency(s2)):
|
---|
[2882] | 319 | raise RuntimeError("Invalid input string.")
|
---|
| 320 |
|
---|
| 321 | prefix_list = ["a", "f", "p", "n", "u", "m", ".", "k", "M", "G", "T", "P", "E"]
|
---|
| 322 | factor_list = [1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1.0, 1e+3, 1e+6, 1e+9, 1e+12, 1e+15, 1e+18]
|
---|
| 323 |
|
---|
[2884] | 324 | s1 = s1.strip()
|
---|
| 325 | s2 = s2.strip()
|
---|
[2882] | 326 |
|
---|
[2884] | 327 | prefix = s2[-3:-2]
|
---|
[2882] | 328 | if is_number(prefix):
|
---|
[2884] | 329 | res1 = float(s1)
|
---|
| 330 | res2 = float(s2[:-2])
|
---|
[2882] | 331 | else:
|
---|
[2884] | 332 | factor = factor_list[prefix_list.index(prefix)]
|
---|
| 333 | res1 = float(s1) * factor
|
---|
| 334 | res2 = float(s2[:-3]) * factor
|
---|
[2882] | 335 |
|
---|
[2884] | 336 | return (res1, res2)
|
---|
[2882] | 337 |
|
---|
| 338 | def is_velocity(s):
|
---|
| 339 | s = s.strip()
|
---|
| 340 | return (s[-3:].lower() == "m/s")
|
---|
| 341 |
|
---|
[2884] | 342 | def get_velocity_by_string(s1, s2):
|
---|
| 343 | if not (is_number(s1) and is_velocity(s2)):
|
---|
[2882] | 344 | raise RuntimeError("Invalid input string.")
|
---|
| 345 |
|
---|
[2884] | 346 | # note that the default velocity unit is km/s
|
---|
[2882] | 347 | prefix_list = [".", "k"]
|
---|
| 348 | factor_list = [1e-3, 1.0]
|
---|
| 349 |
|
---|
[2884] | 350 | s1 = s1.strip()
|
---|
| 351 | s2 = s2.strip()
|
---|
[2882] | 352 |
|
---|
[2884] | 353 | prefix = s2[-4:-3]
|
---|
| 354 | if is_number(prefix): # in case velocity unit m/s
|
---|
| 355 | res1 = float(s1) * 1e-3
|
---|
| 356 | res2 = float(s2[:-3]) * 1e-3
|
---|
[2882] | 357 | else:
|
---|
[2884] | 358 | factor = factor_list[prefix_list.index(prefix)]
|
---|
| 359 | res1 = float(s1) * factor
|
---|
| 360 | res2 = float(s2[:-4]) * factor
|
---|
[2882] | 361 |
|
---|
[2884] | 362 | return (res1, res2)
|
---|
[2882] | 363 |
|
---|
[2889] | 364 | def get_frequency_by_velocity(restfreq, vel, doppler):
|
---|
[2882] | 365 | # vel is in unit of km/s
|
---|
| 366 |
|
---|
| 367 | # speed of light
|
---|
| 368 | vel_c = 299792.458
|
---|
| 369 |
|
---|
| 370 | import math
|
---|
| 371 | r = vel / vel_c
|
---|
| 372 |
|
---|
[2889] | 373 | if doppler.lower() == 'radio':
|
---|
| 374 | return restfreq * (1.0 - r)
|
---|
| 375 | if doppler.lower() == 'optical':
|
---|
| 376 | return restfreq / (1.0 + r)
|
---|
| 377 | else:
|
---|
| 378 | return restfreq * math.sqrt((1.0 - r) / (1.0 + r))
|
---|
[2882] | 379 |
|
---|
[2891] | 380 | def get_restfreq_in_Hz(s_restfreq):
|
---|
| 381 | value = 0.0
|
---|
| 382 | unit = ""
|
---|
| 383 | s = s_restfreq.replace(" ","")
|
---|
[2889] | 384 |
|
---|
[2891] | 385 | for i in range(len(s))[::-1]:
|
---|
| 386 | if s[i].isalpha():
|
---|
| 387 | unit = s[i] + unit
|
---|
| 388 | else:
|
---|
| 389 | value = float(s[0:i+1])
|
---|
| 390 | break
|
---|
| 391 |
|
---|
| 392 | if (unit == "") or (unit.lower() == "hz"):
|
---|
| 393 | return value
|
---|
| 394 | elif (len(unit) == 3) and (unit[1:3].lower() == "hz"):
|
---|
| 395 | unitprefix = unit[0]
|
---|
| 396 | factor = 1.0
|
---|
| 397 |
|
---|
| 398 | prefix_list = ["a", "f", "p", "n", "u", "m", ".", "k", "M", "G", "T", "P", "E"]
|
---|
| 399 | factor_list = [1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1.0, 1e+3, 1e+6, 1e+9, 1e+12, 1e+15, 1e+18]
|
---|
| 400 | factor = factor_list[prefix_list.index(unitprefix)]
|
---|
| 401 | """
|
---|
| 402 | if (unitprefix == 'a'):
|
---|
| 403 | factor = 1.0e-18
|
---|
| 404 | elif (unitprefix == 'f'):
|
---|
| 405 | factor = 1.0e-15
|
---|
| 406 | elif (unitprefix == 'p'):
|
---|
| 407 | factor = 1.0e-12
|
---|
| 408 | elif (unitprefix == 'n'):
|
---|
| 409 | factor = 1.0e-9
|
---|
| 410 | elif (unitprefix == 'u'):
|
---|
| 411 | factor = 1.0e-6
|
---|
| 412 | elif (unitprefix == 'm'):
|
---|
| 413 | factor = 1.0e-3
|
---|
| 414 | elif (unitprefix == 'k'):
|
---|
| 415 | factor = 1.0e+3
|
---|
| 416 | elif (unitprefix == 'M'):
|
---|
| 417 | factor = 1.0e+6
|
---|
| 418 | elif (unitprefix == 'G'):
|
---|
| 419 | factor = 1.0e+9
|
---|
| 420 | elif (unitprefix == 'T'):
|
---|
| 421 | factor = 1.0e+12
|
---|
| 422 | elif (unitprefix == 'P'):
|
---|
| 423 | factor = 1.0e+15
|
---|
| 424 | elif (unitprefix == 'E'):
|
---|
| 425 | factor = 1.0e+18
|
---|
| 426 | """
|
---|
| 427 | return value*factor
|
---|
| 428 | else:
|
---|
| 429 | mesg = "wrong unit of restfreq."
|
---|
| 430 | raise Exception, mesg
|
---|
| 431 |
|
---|
| 432 | def normalise_restfreq(in_restfreq):
|
---|
| 433 | if isinstance(in_restfreq, float):
|
---|
| 434 | return in_restfreq
|
---|
| 435 | elif isinstance(in_restfreq, int) or isinstance(in_restfreq, long):
|
---|
| 436 | return float(in_restfreq)
|
---|
| 437 | elif isinstance(in_restfreq, str):
|
---|
| 438 | return get_restfreq_in_Hz(in_restfreq)
|
---|
| 439 | elif isinstance(in_restfreq, list) or isinstance(in_restfreq, numpy.ndarray):
|
---|
| 440 | if isinstance(in_restfreq, numpy.ndarray):
|
---|
| 441 | if len(in_restfreq.shape) > 1:
|
---|
| 442 | mesg = "given in numpy.ndarray, in_restfreq must be 1-D."
|
---|
| 443 | raise Exception, mesg
|
---|
| 444 |
|
---|
| 445 | res = []
|
---|
| 446 | for i in xrange(len(in_restfreq)):
|
---|
| 447 | elem = in_restfreq[i]
|
---|
| 448 | if isinstance(elem, float):
|
---|
| 449 | res.append(elem)
|
---|
| 450 | elif isinstance(elem, int) or isinstance(elem, long):
|
---|
| 451 | res.append(float(elem))
|
---|
| 452 | elif isinstance(elem, str):
|
---|
| 453 | res.append(get_restfreq_in_Hz(elem))
|
---|
| 454 | elif isinstance(elem, dict):
|
---|
| 455 | if isinstance(elem["value"], float):
|
---|
| 456 | res.append(elem)
|
---|
| 457 | elif isinstance(elem["value"], int):
|
---|
| 458 | dictelem = {}
|
---|
| 459 | dictelem["name"] = elem["name"]
|
---|
| 460 | dictelem["value"] = float(elem["value"])
|
---|
| 461 | res.append(dictelem)
|
---|
| 462 | elif isinstance(elem["value"], str):
|
---|
| 463 | dictelem = {}
|
---|
| 464 | dictelem["name"] = elem["name"]
|
---|
| 465 | dictelem["value"] = get_restfreq_in_Hz(elem["value"])
|
---|
| 466 | res.append(dictelem)
|
---|
| 467 | else:
|
---|
| 468 | mesg = "restfreq elements must be float, int, or string."
|
---|
| 469 | raise Exception, mesg
|
---|
| 470 | return res
|
---|
| 471 | else:
|
---|
| 472 | mesg = "wrong type of restfreq given."
|
---|
| 473 | raise Exception, mesg
|
---|
| 474 |
|
---|
| 475 | def set_restfreq(s, restfreq):
|
---|
| 476 | rfset = (restfreq != '') and (restfreq != [])
|
---|
| 477 | if rfset:
|
---|
| 478 | s.set_restfreqs(normalise_restfreq(restfreq))
|
---|
| 479 |
|
---|
[876] | 480 | class scantable(Scantable):
|
---|
[1846] | 481 | """\
|
---|
| 482 | The ASAP container for scans (single-dish data).
|
---|
[102] | 483 | """
|
---|
[1819] | 484 |
|
---|
[1862] | 485 | @asaplog_post_dec
|
---|
[2315] | 486 | def __init__(self, filename, average=None, unit=None, parallactify=None,
|
---|
| 487 | **args):
|
---|
[1846] | 488 | """\
|
---|
[102] | 489 | Create a scantable from a saved one or make a reference
|
---|
[1846] | 490 |
|
---|
[102] | 491 | Parameters:
|
---|
[1846] | 492 |
|
---|
| 493 | filename: the name of an asap table on disk
|
---|
| 494 | or
|
---|
| 495 | the name of a rpfits/sdfits/ms file
|
---|
| 496 | (integrations within scans are auto averaged
|
---|
| 497 | and the whole file is read) or
|
---|
| 498 | [advanced] a reference to an existing scantable
|
---|
| 499 |
|
---|
| 500 | average: average all integrations withinb a scan on read.
|
---|
| 501 | The default (True) is taken from .asaprc.
|
---|
| 502 |
|
---|
[484] | 503 | unit: brightness unit; must be consistent with K or Jy.
|
---|
[1846] | 504 | Over-rides the default selected by the filler
|
---|
| 505 | (input rpfits/sdfits/ms) or replaces the value
|
---|
| 506 | in existing scantables
|
---|
| 507 |
|
---|
[1920] | 508 | antenna: for MeasurementSet input data only:
|
---|
[2349] | 509 | Antenna selection. integer (id) or string
|
---|
| 510 | (name or id).
|
---|
[1846] | 511 |
|
---|
[2349] | 512 | parallactify: Indicate that the data had been parallactified.
|
---|
| 513 | Default (false) is taken from rc file.
|
---|
[1846] | 514 |
|
---|
[2754] | 515 | getpt: Whether to import direction from MS/POINTING
|
---|
| 516 | table properly or not.
|
---|
| 517 | This is effective only when filename is MS.
|
---|
| 518 | The default (True) is to import direction
|
---|
| 519 | from MS/POINTING.
|
---|
[710] | 520 | """
|
---|
[976] | 521 | if average is None:
|
---|
[710] | 522 | average = rcParams['scantable.autoaverage']
|
---|
[1593] | 523 | parallactify = parallactify or rcParams['scantable.parallactify']
|
---|
[1259] | 524 | varlist = vars()
|
---|
[876] | 525 | from asap._asap import stmath
|
---|
[1819] | 526 | self._math = stmath( rcParams['insitu'] )
|
---|
[876] | 527 | if isinstance(filename, Scantable):
|
---|
| 528 | Scantable.__init__(self, filename)
|
---|
[181] | 529 | else:
|
---|
[1697] | 530 | if isinstance(filename, str):
|
---|
[976] | 531 | filename = os.path.expandvars(filename)
|
---|
| 532 | filename = os.path.expanduser(filename)
|
---|
| 533 | if not os.path.exists(filename):
|
---|
| 534 | s = "File '%s' not found." % (filename)
|
---|
| 535 | raise IOError(s)
|
---|
[1697] | 536 | if is_scantable(filename):
|
---|
| 537 | ondisk = rcParams['scantable.storage'] == 'disk'
|
---|
| 538 | Scantable.__init__(self, filename, ondisk)
|
---|
| 539 | if unit is not None:
|
---|
| 540 | self.set_fluxunit(unit)
|
---|
[2008] | 541 | if average:
|
---|
| 542 | self._assign( self.average_time( scanav=True ) )
|
---|
[1819] | 543 | # do not reset to the default freqframe
|
---|
| 544 | #self.set_freqframe(rcParams['scantable.freqframe'])
|
---|
[1883] | 545 | elif is_ms(filename):
|
---|
[1916] | 546 | # Measurement Set
|
---|
| 547 | opts={'ms': {}}
|
---|
[2844] | 548 | mskeys=['getpt','antenna']
|
---|
[1916] | 549 | for key in mskeys:
|
---|
| 550 | if key in args.keys():
|
---|
| 551 | opts['ms'][key] = args[key]
|
---|
| 552 | self._fill([filename], unit, average, opts)
|
---|
[1893] | 553 | elif os.path.isfile(filename):
|
---|
[2761] | 554 | opts={'nro': {}}
|
---|
| 555 | nrokeys=['freqref']
|
---|
| 556 | for key in nrokeys:
|
---|
| 557 | if key in args.keys():
|
---|
| 558 | opts['nro'][key] = args[key]
|
---|
| 559 | self._fill([filename], unit, average, opts)
|
---|
[2350] | 560 | # only apply to new data not "copy constructor"
|
---|
| 561 | self.parallactify(parallactify)
|
---|
[1883] | 562 | else:
|
---|
[1819] | 563 | msg = "The given file '%s'is not a valid " \
|
---|
| 564 | "asap table." % (filename)
|
---|
[1859] | 565 | raise IOError(msg)
|
---|
[1118] | 566 | elif (isinstance(filename, list) or isinstance(filename, tuple)) \
|
---|
[976] | 567 | and isinstance(filename[-1], str):
|
---|
[1916] | 568 | self._fill(filename, unit, average)
|
---|
[1586] | 569 | self.parallactify(parallactify)
|
---|
[1259] | 570 | self._add_history("scantable", varlist)
|
---|
[102] | 571 |
|
---|
[1862] | 572 | @asaplog_post_dec
|
---|
[876] | 573 | def save(self, name=None, format=None, overwrite=False):
|
---|
[1846] | 574 | """\
|
---|
[1280] | 575 | Store the scantable on disk. This can be an asap (aips++) Table,
|
---|
| 576 | SDFITS or MS2 format.
|
---|
[1846] | 577 |
|
---|
[116] | 578 | Parameters:
|
---|
[1846] | 579 |
|
---|
[2431] | 580 | name: the name of the outputfile. For format 'ASCII'
|
---|
[1093] | 581 | this is the root file name (data in 'name'.txt
|
---|
[497] | 582 | and header in 'name'_header.txt)
|
---|
[1855] | 583 |
|
---|
[116] | 584 | format: an optional file format. Default is ASAP.
|
---|
[1855] | 585 | Allowed are:
|
---|
| 586 |
|
---|
| 587 | * 'ASAP' (save as ASAP [aips++] Table),
|
---|
| 588 | * 'SDFITS' (save as SDFITS file)
|
---|
| 589 | * 'ASCII' (saves as ascii text file)
|
---|
| 590 | * 'MS2' (saves as an casacore MeasurementSet V2)
|
---|
[2315] | 591 | * 'FITS' (save as image FITS - not readable by
|
---|
| 592 | class)
|
---|
[1855] | 593 | * 'CLASS' (save as FITS readable by CLASS)
|
---|
| 594 |
|
---|
[411] | 595 | overwrite: If the file should be overwritten if it exists.
|
---|
[256] | 596 | The default False is to return with warning
|
---|
[411] | 597 | without writing the output. USE WITH CARE.
|
---|
[1855] | 598 |
|
---|
[1846] | 599 | Example::
|
---|
| 600 |
|
---|
[116] | 601 | scan.save('myscan.asap')
|
---|
[1118] | 602 | scan.save('myscan.sdfits', 'SDFITS')
|
---|
[1846] | 603 |
|
---|
[116] | 604 | """
|
---|
[411] | 605 | from os import path
|
---|
[1593] | 606 | format = format or rcParams['scantable.save']
|
---|
[256] | 607 | suffix = '.'+format.lower()
|
---|
[1118] | 608 | if name is None or name == "":
|
---|
[256] | 609 | name = 'scantable'+suffix
|
---|
[718] | 610 | msg = "No filename given. Using default name %s..." % name
|
---|
| 611 | asaplog.push(msg)
|
---|
[411] | 612 | name = path.expandvars(name)
|
---|
[256] | 613 | if path.isfile(name) or path.isdir(name):
|
---|
| 614 | if not overwrite:
|
---|
[718] | 615 | msg = "File %s exists." % name
|
---|
[1859] | 616 | raise IOError(msg)
|
---|
[451] | 617 | format2 = format.upper()
|
---|
| 618 | if format2 == 'ASAP':
|
---|
[116] | 619 | self._save(name)
|
---|
[2029] | 620 | elif format2 == 'MS2':
|
---|
| 621 | msopt = {'ms': {'overwrite': overwrite } }
|
---|
| 622 | from asap._asap import mswriter
|
---|
| 623 | writer = mswriter( self )
|
---|
| 624 | writer.write( name, msopt )
|
---|
[116] | 625 | else:
|
---|
[989] | 626 | from asap._asap import stwriter as stw
|
---|
[1118] | 627 | writer = stw(format2)
|
---|
| 628 | writer.write(self, name)
|
---|
[116] | 629 | return
|
---|
| 630 |
|
---|
[102] | 631 | def copy(self):
|
---|
[1846] | 632 | """Return a copy of this scantable.
|
---|
| 633 |
|
---|
| 634 | *Note*:
|
---|
| 635 |
|
---|
[1348] | 636 | This makes a full (deep) copy. scan2 = scan1 makes a reference.
|
---|
[1846] | 637 |
|
---|
| 638 | Example::
|
---|
| 639 |
|
---|
[102] | 640 | copiedscan = scan.copy()
|
---|
[1846] | 641 |
|
---|
[102] | 642 | """
|
---|
[876] | 643 | sd = scantable(Scantable._copy(self))
|
---|
[113] | 644 | return sd
|
---|
| 645 |
|
---|
[1093] | 646 | def drop_scan(self, scanid=None):
|
---|
[1846] | 647 | """\
|
---|
[1093] | 648 | Return a new scantable where the specified scan number(s) has(have)
|
---|
| 649 | been dropped.
|
---|
[1846] | 650 |
|
---|
[1093] | 651 | Parameters:
|
---|
[1846] | 652 |
|
---|
[1093] | 653 | scanid: a (list of) scan number(s)
|
---|
[1846] | 654 |
|
---|
[1093] | 655 | """
|
---|
| 656 | from asap import _is_sequence_or_number as _is_valid
|
---|
| 657 | from asap import _to_list
|
---|
| 658 | from asap import unique
|
---|
| 659 | if not _is_valid(scanid):
|
---|
[2315] | 660 | raise RuntimeError( 'Please specify a scanno to drop from the'
|
---|
| 661 | ' scantable' )
|
---|
[1859] | 662 | scanid = _to_list(scanid)
|
---|
| 663 | allscans = unique([ self.getscan(i) for i in range(self.nrow())])
|
---|
| 664 | for sid in scanid: allscans.remove(sid)
|
---|
| 665 | if len(allscans) == 0:
|
---|
| 666 | raise ValueError("Can't remove all scans")
|
---|
| 667 | sel = selector(scans=allscans)
|
---|
| 668 | return self._select_copy(sel)
|
---|
[1093] | 669 |
|
---|
[1594] | 670 | def _select_copy(self, selection):
|
---|
| 671 | orig = self.get_selection()
|
---|
| 672 | self.set_selection(orig+selection)
|
---|
| 673 | cp = self.copy()
|
---|
| 674 | self.set_selection(orig)
|
---|
| 675 | return cp
|
---|
| 676 |
|
---|
[102] | 677 | def get_scan(self, scanid=None):
|
---|
[1855] | 678 | """\
|
---|
[102] | 679 | Return a specific scan (by scanno) or collection of scans (by
|
---|
| 680 | source name) in a new scantable.
|
---|
[1846] | 681 |
|
---|
| 682 | *Note*:
|
---|
| 683 |
|
---|
[1348] | 684 | See scantable.drop_scan() for the inverse operation.
|
---|
[1846] | 685 |
|
---|
[102] | 686 | Parameters:
|
---|
[1846] | 687 |
|
---|
[513] | 688 | scanid: a (list of) scanno or a source name, unix-style
|
---|
| 689 | patterns are accepted for source name matching, e.g.
|
---|
| 690 | '*_R' gets all 'ref scans
|
---|
[1846] | 691 |
|
---|
| 692 | Example::
|
---|
| 693 |
|
---|
[513] | 694 | # get all scans containing the source '323p459'
|
---|
| 695 | newscan = scan.get_scan('323p459')
|
---|
| 696 | # get all 'off' scans
|
---|
| 697 | refscans = scan.get_scan('*_R')
|
---|
| 698 | # get a susbset of scans by scanno (as listed in scan.summary())
|
---|
[1118] | 699 | newscan = scan.get_scan([0, 2, 7, 10])
|
---|
[1846] | 700 |
|
---|
[102] | 701 | """
|
---|
| 702 | if scanid is None:
|
---|
[1859] | 703 | raise RuntimeError( 'Please specify a scan no or name to '
|
---|
| 704 | 'retrieve from the scantable' )
|
---|
[102] | 705 | try:
|
---|
[946] | 706 | bsel = self.get_selection()
|
---|
| 707 | sel = selector()
|
---|
[102] | 708 | if type(scanid) is str:
|
---|
[946] | 709 | sel.set_name(scanid)
|
---|
[1594] | 710 | return self._select_copy(sel)
|
---|
[102] | 711 | elif type(scanid) is int:
|
---|
[946] | 712 | sel.set_scans([scanid])
|
---|
[1594] | 713 | return self._select_copy(sel)
|
---|
[381] | 714 | elif type(scanid) is list:
|
---|
[946] | 715 | sel.set_scans(scanid)
|
---|
[1594] | 716 | return self._select_copy(sel)
|
---|
[381] | 717 | else:
|
---|
[718] | 718 | msg = "Illegal scanid type, use 'int' or 'list' if ints."
|
---|
[1859] | 719 | raise TypeError(msg)
|
---|
[102] | 720 | except RuntimeError:
|
---|
[1859] | 721 | raise
|
---|
[102] | 722 |
|
---|
| 723 | def __str__(self):
|
---|
[2315] | 724 | tempFile = tempfile.NamedTemporaryFile()
|
---|
| 725 | Scantable._summary(self, tempFile.name)
|
---|
| 726 | tempFile.seek(0)
|
---|
| 727 | asaplog.clear()
|
---|
| 728 | return tempFile.file.read()
|
---|
[102] | 729 |
|
---|
[2315] | 730 | @asaplog_post_dec
|
---|
[976] | 731 | def summary(self, filename=None):
|
---|
[1846] | 732 | """\
|
---|
[102] | 733 | Print a summary of the contents of this scantable.
|
---|
[1846] | 734 |
|
---|
[102] | 735 | Parameters:
|
---|
[1846] | 736 |
|
---|
[1931] | 737 | filename: the name of a file to write the putput to
|
---|
[102] | 738 | Default - no file output
|
---|
[1846] | 739 |
|
---|
[102] | 740 | """
|
---|
| 741 | if filename is not None:
|
---|
[256] | 742 | if filename is "":
|
---|
| 743 | filename = 'scantable_summary.txt'
|
---|
[415] | 744 | from os.path import expandvars, isdir
|
---|
[411] | 745 | filename = expandvars(filename)
|
---|
[2286] | 746 | if isdir(filename):
|
---|
[718] | 747 | msg = "Illegal file name '%s'." % (filename)
|
---|
[1859] | 748 | raise IOError(msg)
|
---|
[2286] | 749 | else:
|
---|
| 750 | filename = ""
|
---|
| 751 | Scantable._summary(self, filename)
|
---|
[710] | 752 |
|
---|
[1512] | 753 | def get_spectrum(self, rowno):
|
---|
[1471] | 754 | """Return the spectrum for the current row in the scantable as a list.
|
---|
[1846] | 755 |
|
---|
[1471] | 756 | Parameters:
|
---|
[1846] | 757 |
|
---|
[1573] | 758 | rowno: the row number to retrieve the spectrum from
|
---|
[1846] | 759 |
|
---|
[1471] | 760 | """
|
---|
| 761 | return self._getspectrum(rowno)
|
---|
[946] | 762 |
|
---|
[1471] | 763 | def get_mask(self, rowno):
|
---|
| 764 | """Return the mask for the current row in the scantable as a list.
|
---|
[1846] | 765 |
|
---|
[1471] | 766 | Parameters:
|
---|
[1846] | 767 |
|
---|
[1573] | 768 | rowno: the row number to retrieve the mask from
|
---|
[1846] | 769 |
|
---|
[1471] | 770 | """
|
---|
| 771 | return self._getmask(rowno)
|
---|
| 772 |
|
---|
| 773 | def set_spectrum(self, spec, rowno):
|
---|
[1938] | 774 | """Set the spectrum for the current row in the scantable.
|
---|
[1846] | 775 |
|
---|
[1471] | 776 | Parameters:
|
---|
[1846] | 777 |
|
---|
[1855] | 778 | spec: the new spectrum
|
---|
[1846] | 779 |
|
---|
[1855] | 780 | rowno: the row number to set the spectrum for
|
---|
| 781 |
|
---|
[1471] | 782 | """
|
---|
[2348] | 783 | assert(len(spec) == self.nchan(self.getif(rowno)))
|
---|
[1471] | 784 | return self._setspectrum(spec, rowno)
|
---|
| 785 |
|
---|
[1600] | 786 | def get_coordinate(self, rowno):
|
---|
| 787 | """Return the (spectral) coordinate for a a given 'rowno'.
|
---|
[1846] | 788 |
|
---|
| 789 | *Note*:
|
---|
| 790 |
|
---|
[1600] | 791 | * This coordinate is only valid until a scantable method modifies
|
---|
| 792 | the frequency axis.
|
---|
| 793 | * This coordinate does contain the original frequency set-up
|
---|
| 794 | NOT the new frame. The conversions however are done using the user
|
---|
| 795 | specified frame (e.g. LSRK/TOPO). To get the 'real' coordinate,
|
---|
| 796 | use scantable.freq_align first. Without it there is no closure,
|
---|
[1846] | 797 | i.e.::
|
---|
[1600] | 798 |
|
---|
[1846] | 799 | c = myscan.get_coordinate(0)
|
---|
| 800 | c.to_frequency(c.get_reference_pixel()) != c.get_reference_value()
|
---|
| 801 |
|
---|
[1600] | 802 | Parameters:
|
---|
[1846] | 803 |
|
---|
[1600] | 804 | rowno: the row number for the spectral coordinate
|
---|
| 805 |
|
---|
| 806 | """
|
---|
| 807 | return coordinate(Scantable.get_coordinate(self, rowno))
|
---|
| 808 |
|
---|
[946] | 809 | def get_selection(self):
|
---|
[1846] | 810 | """\
|
---|
[1005] | 811 | Get the selection object currently set on this scantable.
|
---|
[1846] | 812 |
|
---|
| 813 | Example::
|
---|
| 814 |
|
---|
[1005] | 815 | sel = scan.get_selection()
|
---|
| 816 | sel.set_ifs(0) # select IF 0
|
---|
| 817 | scan.set_selection(sel) # apply modified selection
|
---|
[1846] | 818 |
|
---|
[946] | 819 | """
|
---|
| 820 | return selector(self._getselection())
|
---|
| 821 |
|
---|
[1576] | 822 | def set_selection(self, selection=None, **kw):
|
---|
[1846] | 823 | """\
|
---|
[1005] | 824 | Select a subset of the data. All following operations on this scantable
|
---|
| 825 | are only applied to thi selection.
|
---|
[1846] | 826 |
|
---|
[1005] | 827 | Parameters:
|
---|
[1697] | 828 |
|
---|
[1846] | 829 | selection: a selector object (default unset the selection), or
|
---|
[2431] | 830 | any combination of 'pols', 'ifs', 'beams', 'scans',
|
---|
| 831 | 'cycles', 'name', 'query'
|
---|
[1697] | 832 |
|
---|
[1846] | 833 | Examples::
|
---|
[1697] | 834 |
|
---|
[1005] | 835 | sel = selector() # create a selection object
|
---|
[1118] | 836 | self.set_scans([0, 3]) # select SCANNO 0 and 3
|
---|
[1005] | 837 | scan.set_selection(sel) # set the selection
|
---|
| 838 | scan.summary() # will only print summary of scanno 0 an 3
|
---|
| 839 | scan.set_selection() # unset the selection
|
---|
[1697] | 840 | # or the equivalent
|
---|
| 841 | scan.set_selection(scans=[0,3])
|
---|
| 842 | scan.summary() # will only print summary of scanno 0 an 3
|
---|
| 843 | scan.set_selection() # unset the selection
|
---|
[1846] | 844 |
|
---|
[946] | 845 | """
|
---|
[1576] | 846 | if selection is None:
|
---|
| 847 | # reset
|
---|
| 848 | if len(kw) == 0:
|
---|
| 849 | selection = selector()
|
---|
| 850 | else:
|
---|
| 851 | # try keywords
|
---|
| 852 | for k in kw:
|
---|
| 853 | if k not in selector.fields:
|
---|
[2320] | 854 | raise KeyError("Invalid selection key '%s', "
|
---|
| 855 | "valid keys are %s" % (k,
|
---|
| 856 | selector.fields))
|
---|
[1576] | 857 | selection = selector(**kw)
|
---|
[946] | 858 | self._setselection(selection)
|
---|
| 859 |
|
---|
[1819] | 860 | def get_row(self, row=0, insitu=None):
|
---|
[1846] | 861 | """\
|
---|
[1819] | 862 | Select a row in the scantable.
|
---|
| 863 | Return a scantable with single row.
|
---|
[1846] | 864 |
|
---|
[1819] | 865 | Parameters:
|
---|
[1846] | 866 |
|
---|
| 867 | row: row no of integration, default is 0.
|
---|
| 868 | insitu: if False a new scantable is returned. Otherwise, the
|
---|
| 869 | scaling is done in-situ. The default is taken from .asaprc
|
---|
| 870 | (False)
|
---|
| 871 |
|
---|
[1819] | 872 | """
|
---|
[2349] | 873 | if insitu is None:
|
---|
| 874 | insitu = rcParams['insitu']
|
---|
[1819] | 875 | if not insitu:
|
---|
| 876 | workscan = self.copy()
|
---|
| 877 | else:
|
---|
| 878 | workscan = self
|
---|
| 879 | # Select a row
|
---|
[2349] | 880 | sel = selector()
|
---|
[1992] | 881 | sel.set_rows([row])
|
---|
[1819] | 882 | workscan.set_selection(sel)
|
---|
| 883 | if not workscan.nrow() == 1:
|
---|
[2349] | 884 | msg = "Could not identify single row. %d rows selected." \
|
---|
| 885 | % (workscan.nrow())
|
---|
[1819] | 886 | raise RuntimeError(msg)
|
---|
| 887 | if insitu:
|
---|
| 888 | self._assign(workscan)
|
---|
| 889 | else:
|
---|
| 890 | return workscan
|
---|
| 891 |
|
---|
[1862] | 892 | @asaplog_post_dec
|
---|
[2957] | 893 | def stats(self, stat='stddev', mask=None, form='3.3f', row=None, skip_flaggedrow=False):
|
---|
[1846] | 894 | """\
|
---|
[135] | 895 | Determine the specified statistic of the current beam/if/pol
|
---|
[102] | 896 | Takes a 'mask' as an optional parameter to specify which
|
---|
| 897 | channels should be excluded.
|
---|
[1846] | 898 |
|
---|
[102] | 899 | Parameters:
|
---|
[1846] | 900 |
|
---|
[1819] | 901 | stat: 'min', 'max', 'min_abc', 'max_abc', 'sumsq', 'sum',
|
---|
| 902 | 'mean', 'var', 'stddev', 'avdev', 'rms', 'median'
|
---|
[1855] | 903 |
|
---|
[135] | 904 | mask: an optional mask specifying where the statistic
|
---|
[102] | 905 | should be determined.
|
---|
[1855] | 906 |
|
---|
[1819] | 907 | form: format string to print statistic values
|
---|
[1846] | 908 |
|
---|
[1907] | 909 | row: row number of spectrum to process.
|
---|
| 910 | (default is None: for all rows)
|
---|
[1846] | 911 |
|
---|
[2957] | 912 | skip_flaggedrow: if True, skip outputting text for flagged
|
---|
| 913 | spectra. default is False.
|
---|
| 914 |
|
---|
[1907] | 915 | Example:
|
---|
[113] | 916 | scan.set_unit('channel')
|
---|
[1118] | 917 | msk = scan.create_mask([100, 200], [500, 600])
|
---|
[135] | 918 | scan.stats(stat='mean', mask=m)
|
---|
[1846] | 919 |
|
---|
[102] | 920 | """
|
---|
[1593] | 921 | mask = mask or []
|
---|
[876] | 922 | if not self._check_ifs():
|
---|
[1118] | 923 | raise ValueError("Cannot apply mask as the IFs have different "
|
---|
| 924 | "number of channels. Please use setselection() "
|
---|
| 925 | "to select individual IFs")
|
---|
[1819] | 926 | getchan = False
|
---|
| 927 | if stat.lower().startswith('min') or stat.lower().startswith('max'):
|
---|
| 928 | chan = self._math._minmaxchan(self, mask, stat)
|
---|
| 929 | getchan = True
|
---|
| 930 | statvals = []
|
---|
[2957] | 931 |
|
---|
| 932 | rtnabc = False
|
---|
| 933 | if stat.lower().endswith('_abc'):
|
---|
| 934 | rtnabc = True
|
---|
| 935 | else:
|
---|
[1907] | 936 | if row == None:
|
---|
| 937 | statvals = self._math._stats(self, mask, stat)
|
---|
| 938 | else:
|
---|
| 939 | statvals = self._math._statsrow(self, mask, stat, int(row))
|
---|
[256] | 940 |
|
---|
[1819] | 941 | #def cb(i):
|
---|
| 942 | # return statvals[i]
|
---|
[256] | 943 |
|
---|
[1819] | 944 | #return self._row_callback(cb, stat)
|
---|
[102] | 945 |
|
---|
[1819] | 946 | label=stat
|
---|
| 947 | #callback=cb
|
---|
| 948 | out = ""
|
---|
| 949 | #outvec = []
|
---|
| 950 | sep = '-'*50
|
---|
[1907] | 951 |
|
---|
| 952 | if row == None:
|
---|
| 953 | rows = xrange(self.nrow())
|
---|
| 954 | elif isinstance(row, int):
|
---|
| 955 | rows = [ row ]
|
---|
| 956 |
|
---|
| 957 | for i in rows:
|
---|
[1819] | 958 | refstr = ''
|
---|
| 959 | statunit= ''
|
---|
| 960 | if getchan:
|
---|
| 961 | qx, qy = self.chan2data(rowno=i, chan=chan[i])
|
---|
| 962 | if rtnabc:
|
---|
| 963 | statvals.append(qx['value'])
|
---|
| 964 | refstr = ('(value: %'+form) % (qy['value'])+' ['+qy['unit']+'])'
|
---|
| 965 | statunit= '['+qx['unit']+']'
|
---|
| 966 | else:
|
---|
| 967 | refstr = ('(@ %'+form) % (qx['value'])+' ['+qx['unit']+'])'
|
---|
| 968 |
|
---|
[2973] | 969 | if skip_flaggedrow and self._getflagrow(i):
|
---|
| 970 | statvals[i] = None
|
---|
| 971 | continue
|
---|
[2957] | 972 |
|
---|
[1819] | 973 | tm = self._gettime(i)
|
---|
| 974 | src = self._getsourcename(i)
|
---|
| 975 | out += 'Scan[%d] (%s) ' % (self.getscan(i), src)
|
---|
| 976 | out += 'Time[%s]:\n' % (tm)
|
---|
[1907] | 977 | if self.nbeam(-1) > 1: out += ' Beam[%d] ' % (self.getbeam(i))
|
---|
| 978 | if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i))
|
---|
| 979 | if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i))
|
---|
[1819] | 980 | #outvec.append(callback(i))
|
---|
[1907] | 981 | if len(rows) > 1:
|
---|
| 982 | # out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n'
|
---|
| 983 | out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n'
|
---|
| 984 | else:
|
---|
| 985 | # out += ('= %'+form) % (outvec[0]) +' '+refstr+'\n'
|
---|
| 986 | out += ('= %'+form) % (statvals[0]) +' '+refstr+'\n'
|
---|
[1819] | 987 | out += sep+"\n"
|
---|
| 988 |
|
---|
[1859] | 989 | import os
|
---|
| 990 | if os.environ.has_key( 'USER' ):
|
---|
| 991 | usr = os.environ['USER']
|
---|
| 992 | else:
|
---|
| 993 | import commands
|
---|
| 994 | usr = commands.getoutput( 'whoami' )
|
---|
| 995 | tmpfile = '/tmp/tmp_'+usr+'_casapy_asap_scantable_stats'
|
---|
| 996 | f = open(tmpfile,'w')
|
---|
| 997 | print >> f, sep
|
---|
| 998 | print >> f, ' %s %s' % (label, statunit)
|
---|
| 999 | print >> f, sep
|
---|
| 1000 | print >> f, out
|
---|
| 1001 | f.close()
|
---|
| 1002 | f = open(tmpfile,'r')
|
---|
| 1003 | x = f.readlines()
|
---|
| 1004 | f.close()
|
---|
| 1005 | asaplog.push(''.join(x), False)
|
---|
| 1006 |
|
---|
[1819] | 1007 | return statvals
|
---|
| 1008 |
|
---|
| 1009 | def chan2data(self, rowno=0, chan=0):
|
---|
[1846] | 1010 | """\
|
---|
[1819] | 1011 | Returns channel/frequency/velocity and spectral value
|
---|
| 1012 | at an arbitrary row and channel in the scantable.
|
---|
[1846] | 1013 |
|
---|
[1819] | 1014 | Parameters:
|
---|
[1846] | 1015 |
|
---|
[1819] | 1016 | rowno: a row number in the scantable. Default is the
|
---|
| 1017 | first row, i.e. rowno=0
|
---|
[1855] | 1018 |
|
---|
[1819] | 1019 | chan: a channel in the scantable. Default is the first
|
---|
| 1020 | channel, i.e. pos=0
|
---|
[1846] | 1021 |
|
---|
[1819] | 1022 | """
|
---|
| 1023 | if isinstance(rowno, int) and isinstance(chan, int):
|
---|
| 1024 | qx = {'unit': self.get_unit(),
|
---|
| 1025 | 'value': self._getabcissa(rowno)[chan]}
|
---|
| 1026 | qy = {'unit': self.get_fluxunit(),
|
---|
| 1027 | 'value': self._getspectrum(rowno)[chan]}
|
---|
| 1028 | return qx, qy
|
---|
| 1029 |
|
---|
[1118] | 1030 | def stddev(self, mask=None):
|
---|
[1846] | 1031 | """\
|
---|
[135] | 1032 | Determine the standard deviation of the current beam/if/pol
|
---|
| 1033 | Takes a 'mask' as an optional parameter to specify which
|
---|
| 1034 | channels should be excluded.
|
---|
[1846] | 1035 |
|
---|
[135] | 1036 | Parameters:
|
---|
[1846] | 1037 |
|
---|
[135] | 1038 | mask: an optional mask specifying where the standard
|
---|
| 1039 | deviation should be determined.
|
---|
| 1040 |
|
---|
[1846] | 1041 | Example::
|
---|
| 1042 |
|
---|
[135] | 1043 | scan.set_unit('channel')
|
---|
[1118] | 1044 | msk = scan.create_mask([100, 200], [500, 600])
|
---|
[135] | 1045 | scan.stddev(mask=m)
|
---|
[1846] | 1046 |
|
---|
[135] | 1047 | """
|
---|
[1118] | 1048 | return self.stats(stat='stddev', mask=mask);
|
---|
[135] | 1049 |
|
---|
[1003] | 1050 |
|
---|
[1259] | 1051 | def get_column_names(self):
|
---|
[1846] | 1052 | """\
|
---|
[1003] | 1053 | Return a list of column names, which can be used for selection.
|
---|
| 1054 | """
|
---|
[1259] | 1055 | return list(Scantable.get_column_names(self))
|
---|
[1003] | 1056 |
|
---|
[1730] | 1057 | def get_tsys(self, row=-1):
|
---|
[1846] | 1058 | """\
|
---|
[113] | 1059 | Return the System temperatures.
|
---|
[1846] | 1060 |
|
---|
| 1061 | Parameters:
|
---|
| 1062 |
|
---|
| 1063 | row: the rowno to get the information for. (default all rows)
|
---|
| 1064 |
|
---|
[113] | 1065 | Returns:
|
---|
[1846] | 1066 |
|
---|
[876] | 1067 | a list of Tsys values for the current selection
|
---|
[1846] | 1068 |
|
---|
[113] | 1069 | """
|
---|
[1730] | 1070 | if row > -1:
|
---|
| 1071 | return self._get_column(self._gettsys, row)
|
---|
[876] | 1072 | return self._row_callback(self._gettsys, "Tsys")
|
---|
[256] | 1073 |
|
---|
[2406] | 1074 | def get_tsysspectrum(self, row=-1):
|
---|
| 1075 | """\
|
---|
| 1076 | Return the channel dependent system temperatures.
|
---|
[1730] | 1077 |
|
---|
[2406] | 1078 | Parameters:
|
---|
| 1079 |
|
---|
| 1080 | row: the rowno to get the information for. (default all rows)
|
---|
| 1081 |
|
---|
| 1082 | Returns:
|
---|
| 1083 |
|
---|
| 1084 | a list of Tsys values for the current selection
|
---|
| 1085 |
|
---|
| 1086 | """
|
---|
| 1087 | return self._get_column( self._gettsysspectrum, row )
|
---|
| 1088 |
|
---|
[2791] | 1089 | def set_tsys(self, values, row=-1):
|
---|
| 1090 | """\
|
---|
| 1091 | Set the Tsys value(s) of the given 'row' or the whole scantable
|
---|
| 1092 | (selection).
|
---|
| 1093 |
|
---|
| 1094 | Parameters:
|
---|
| 1095 |
|
---|
| 1096 | values: a scalar or list (if Tsys is a vector) of Tsys value(s)
|
---|
| 1097 | row: the row number to apply Tsys values to.
|
---|
| 1098 | (default all rows)
|
---|
| 1099 |
|
---|
| 1100 | """
|
---|
| 1101 |
|
---|
| 1102 | if not hasattr(values, "__len__"):
|
---|
| 1103 | values = [values]
|
---|
| 1104 | self._settsys(values, row)
|
---|
| 1105 |
|
---|
[1730] | 1106 | def get_weather(self, row=-1):
|
---|
[1846] | 1107 | """\
|
---|
[2930] | 1108 | Return the weather information.
|
---|
[1846] | 1109 |
|
---|
| 1110 | Parameters:
|
---|
| 1111 |
|
---|
| 1112 | row: the rowno to get the information for. (default all rows)
|
---|
| 1113 |
|
---|
| 1114 | Returns:
|
---|
| 1115 |
|
---|
| 1116 | a dict or list of of dicts of values for the current selection
|
---|
| 1117 |
|
---|
| 1118 | """
|
---|
[2930] | 1119 | if row >= len(self):
|
---|
| 1120 | raise IndexError("row out of range")
|
---|
[1730] | 1121 | values = self._get_column(self._get_weather, row)
|
---|
| 1122 | if row > -1:
|
---|
| 1123 | return {'temperature': values[0],
|
---|
| 1124 | 'pressure': values[1], 'humidity' : values[2],
|
---|
| 1125 | 'windspeed' : values[3], 'windaz' : values[4]
|
---|
| 1126 | }
|
---|
| 1127 | else:
|
---|
| 1128 | out = []
|
---|
| 1129 | for r in values:
|
---|
| 1130 | out.append({'temperature': r[0],
|
---|
| 1131 | 'pressure': r[1], 'humidity' : r[2],
|
---|
| 1132 | 'windspeed' : r[3], 'windaz' : r[4]
|
---|
| 1133 | })
|
---|
| 1134 | return out
|
---|
| 1135 |
|
---|
[876] | 1136 | def _row_callback(self, callback, label):
|
---|
| 1137 | out = ""
|
---|
[1118] | 1138 | outvec = []
|
---|
[1590] | 1139 | sep = '-'*50
|
---|
[876] | 1140 | for i in range(self.nrow()):
|
---|
| 1141 | tm = self._gettime(i)
|
---|
| 1142 | src = self._getsourcename(i)
|
---|
[1590] | 1143 | out += 'Scan[%d] (%s) ' % (self.getscan(i), src)
|
---|
[876] | 1144 | out += 'Time[%s]:\n' % (tm)
|
---|
[1590] | 1145 | if self.nbeam(-1) > 1:
|
---|
| 1146 | out += ' Beam[%d] ' % (self.getbeam(i))
|
---|
| 1147 | if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i))
|
---|
| 1148 | if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i))
|
---|
[876] | 1149 | outvec.append(callback(i))
|
---|
| 1150 | out += '= %3.3f\n' % (outvec[i])
|
---|
[1590] | 1151 | out += sep+'\n'
|
---|
[1859] | 1152 |
|
---|
| 1153 | asaplog.push(sep)
|
---|
| 1154 | asaplog.push(" %s" % (label))
|
---|
| 1155 | asaplog.push(sep)
|
---|
| 1156 | asaplog.push(out)
|
---|
[1861] | 1157 | asaplog.post()
|
---|
[1175] | 1158 | return outvec
|
---|
[256] | 1159 |
|
---|
[1947] | 1160 | def _get_column(self, callback, row=-1, *args):
|
---|
[1070] | 1161 | """
|
---|
| 1162 | """
|
---|
| 1163 | if row == -1:
|
---|
[1947] | 1164 | return [callback(i, *args) for i in range(self.nrow())]
|
---|
[1070] | 1165 | else:
|
---|
[1819] | 1166 | if 0 <= row < self.nrow():
|
---|
[1947] | 1167 | return callback(row, *args)
|
---|
[256] | 1168 |
|
---|
[1070] | 1169 |
|
---|
[1948] | 1170 | def get_time(self, row=-1, asdatetime=False, prec=-1):
|
---|
[1846] | 1171 | """\
|
---|
[113] | 1172 | Get a list of time stamps for the observations.
|
---|
[1938] | 1173 | Return a datetime object or a string (default) for each
|
---|
| 1174 | integration time stamp in the scantable.
|
---|
[1846] | 1175 |
|
---|
[113] | 1176 | Parameters:
|
---|
[1846] | 1177 |
|
---|
[1348] | 1178 | row: row no of integration. Default -1 return all rows
|
---|
[1855] | 1179 |
|
---|
[1348] | 1180 | asdatetime: return values as datetime objects rather than strings
|
---|
[1846] | 1181 |
|
---|
[2349] | 1182 | prec: number of digits shown. Default -1 to automatic
|
---|
| 1183 | calculation.
|
---|
[1948] | 1184 | Note this number is equals to the digits of MVTime,
|
---|
| 1185 | i.e., 0<prec<3: dates with hh:: only,
|
---|
| 1186 | <5: with hh:mm:, <7 or 0: with hh:mm:ss,
|
---|
[1947] | 1187 | and 6> : with hh:mm:ss.tt... (prec-6 t's added)
|
---|
| 1188 |
|
---|
[113] | 1189 | """
|
---|
[1175] | 1190 | from datetime import datetime
|
---|
[1948] | 1191 | if prec < 0:
|
---|
| 1192 | # automagically set necessary precision +1
|
---|
[2349] | 1193 | prec = 7 - \
|
---|
| 1194 | numpy.floor(numpy.log10(numpy.min(self.get_inttime(row))))
|
---|
[1948] | 1195 | prec = max(6, int(prec))
|
---|
| 1196 | else:
|
---|
| 1197 | prec = max(0, prec)
|
---|
| 1198 | if asdatetime:
|
---|
| 1199 | #precision can be 1 millisecond at max
|
---|
| 1200 | prec = min(12, prec)
|
---|
| 1201 |
|
---|
[1947] | 1202 | times = self._get_column(self._gettime, row, prec)
|
---|
[1348] | 1203 | if not asdatetime:
|
---|
[1392] | 1204 | return times
|
---|
[1947] | 1205 | format = "%Y/%m/%d/%H:%M:%S.%f"
|
---|
| 1206 | if prec < 7:
|
---|
| 1207 | nsub = 1 + (((6-prec)/2) % 3)
|
---|
| 1208 | substr = [".%f","%S","%M"]
|
---|
| 1209 | for i in range(nsub):
|
---|
| 1210 | format = format.replace(substr[i],"")
|
---|
[1175] | 1211 | if isinstance(times, list):
|
---|
[1947] | 1212 | return [datetime.strptime(i, format) for i in times]
|
---|
[1175] | 1213 | else:
|
---|
[1947] | 1214 | return datetime.strptime(times, format)
|
---|
[102] | 1215 |
|
---|
[1348] | 1216 |
|
---|
| 1217 | def get_inttime(self, row=-1):
|
---|
[1846] | 1218 | """\
|
---|
[1348] | 1219 | Get a list of integration times for the observations.
|
---|
| 1220 | Return a time in seconds for each integration in the scantable.
|
---|
[1846] | 1221 |
|
---|
[1348] | 1222 | Parameters:
|
---|
[1846] | 1223 |
|
---|
[1348] | 1224 | row: row no of integration. Default -1 return all rows.
|
---|
[1846] | 1225 |
|
---|
[1348] | 1226 | """
|
---|
[1573] | 1227 | return self._get_column(self._getinttime, row)
|
---|
[1348] | 1228 |
|
---|
[1573] | 1229 |
|
---|
[714] | 1230 | def get_sourcename(self, row=-1):
|
---|
[1846] | 1231 | """\
|
---|
[794] | 1232 | Get a list source names for the observations.
|
---|
[714] | 1233 | Return a string for each integration in the scantable.
|
---|
| 1234 | Parameters:
|
---|
[1846] | 1235 |
|
---|
[1348] | 1236 | row: row no of integration. Default -1 return all rows.
|
---|
[1846] | 1237 |
|
---|
[714] | 1238 | """
|
---|
[1070] | 1239 | return self._get_column(self._getsourcename, row)
|
---|
[714] | 1240 |
|
---|
[794] | 1241 | def get_elevation(self, row=-1):
|
---|
[1846] | 1242 | """\
|
---|
[794] | 1243 | Get a list of elevations for the observations.
|
---|
| 1244 | Return a float for each integration in the scantable.
|
---|
[1846] | 1245 |
|
---|
[794] | 1246 | Parameters:
|
---|
[1846] | 1247 |
|
---|
[1348] | 1248 | row: row no of integration. Default -1 return all rows.
|
---|
[1846] | 1249 |
|
---|
[794] | 1250 | """
|
---|
[1070] | 1251 | return self._get_column(self._getelevation, row)
|
---|
[794] | 1252 |
|
---|
| 1253 | def get_azimuth(self, row=-1):
|
---|
[1846] | 1254 | """\
|
---|
[794] | 1255 | Get a list of azimuths for the observations.
|
---|
| 1256 | Return a float for each integration in the scantable.
|
---|
[1846] | 1257 |
|
---|
[794] | 1258 | Parameters:
|
---|
[1348] | 1259 | row: row no of integration. Default -1 return all rows.
|
---|
[1846] | 1260 |
|
---|
[794] | 1261 | """
|
---|
[1070] | 1262 | return self._get_column(self._getazimuth, row)
|
---|
[794] | 1263 |
|
---|
| 1264 | def get_parangle(self, row=-1):
|
---|
[1846] | 1265 | """\
|
---|
[794] | 1266 | Get a list of parallactic angles for the observations.
|
---|
| 1267 | Return a float for each integration in the scantable.
|
---|
[1846] | 1268 |
|
---|
[794] | 1269 | Parameters:
|
---|
[1846] | 1270 |
|
---|
[1348] | 1271 | row: row no of integration. Default -1 return all rows.
|
---|
[1846] | 1272 |
|
---|
[794] | 1273 | """
|
---|
[1070] | 1274 | return self._get_column(self._getparangle, row)
|
---|
[794] | 1275 |
|
---|
[1070] | 1276 | def get_direction(self, row=-1):
|
---|
| 1277 | """
|
---|
| 1278 | Get a list of Positions on the sky (direction) for the observations.
|
---|
[1594] | 1279 | Return a string for each integration in the scantable.
|
---|
[1855] | 1280 |
|
---|
[1070] | 1281 | Parameters:
|
---|
[1855] | 1282 |
|
---|
[1070] | 1283 | row: row no of integration. Default -1 return all rows
|
---|
[1855] | 1284 |
|
---|
[1070] | 1285 | """
|
---|
| 1286 | return self._get_column(self._getdirection, row)
|
---|
| 1287 |
|
---|
[1391] | 1288 | def get_directionval(self, row=-1):
|
---|
[1846] | 1289 | """\
|
---|
[1391] | 1290 | Get a list of Positions on the sky (direction) for the observations.
|
---|
| 1291 | Return a float for each integration in the scantable.
|
---|
[1846] | 1292 |
|
---|
[1391] | 1293 | Parameters:
|
---|
[1846] | 1294 |
|
---|
[1391] | 1295 | row: row no of integration. Default -1 return all rows
|
---|
[1846] | 1296 |
|
---|
[1391] | 1297 | """
|
---|
| 1298 | return self._get_column(self._getdirectionvec, row)
|
---|
| 1299 |
|
---|
[1862] | 1300 | @asaplog_post_dec
|
---|
[102] | 1301 | def set_unit(self, unit='channel'):
|
---|
[1846] | 1302 | """\
|
---|
[102] | 1303 | Set the unit for all following operations on this scantable
|
---|
[1846] | 1304 |
|
---|
[102] | 1305 | Parameters:
|
---|
[1846] | 1306 |
|
---|
| 1307 | unit: optional unit, default is 'channel'. Use one of '*Hz',
|
---|
| 1308 | 'km/s', 'channel' or equivalent ''
|
---|
| 1309 |
|
---|
[102] | 1310 | """
|
---|
[484] | 1311 | varlist = vars()
|
---|
[1118] | 1312 | if unit in ['', 'pixel', 'channel']:
|
---|
[113] | 1313 | unit = ''
|
---|
| 1314 | inf = list(self._getcoordinfo())
|
---|
| 1315 | inf[0] = unit
|
---|
| 1316 | self._setcoordinfo(inf)
|
---|
[1118] | 1317 | self._add_history("set_unit", varlist)
|
---|
[113] | 1318 |
|
---|
[1862] | 1319 | @asaplog_post_dec
|
---|
[484] | 1320 | def set_instrument(self, instr):
|
---|
[1846] | 1321 | """\
|
---|
[1348] | 1322 | Set the instrument for subsequent processing.
|
---|
[1846] | 1323 |
|
---|
[358] | 1324 | Parameters:
|
---|
[1846] | 1325 |
|
---|
[710] | 1326 | instr: Select from 'ATPKSMB', 'ATPKSHOH', 'ATMOPRA',
|
---|
[407] | 1327 | 'DSS-43' (Tid), 'CEDUNA', and 'HOBART'
|
---|
[1846] | 1328 |
|
---|
[358] | 1329 | """
|
---|
| 1330 | self._setInstrument(instr)
|
---|
[1118] | 1331 | self._add_history("set_instument", vars())
|
---|
[358] | 1332 |
|
---|
[1862] | 1333 | @asaplog_post_dec
|
---|
[1190] | 1334 | def set_feedtype(self, feedtype):
|
---|
[1846] | 1335 | """\
|
---|
[1190] | 1336 | Overwrite the feed type, which might not be set correctly.
|
---|
[1846] | 1337 |
|
---|
[1190] | 1338 | Parameters:
|
---|
[1846] | 1339 |
|
---|
[1190] | 1340 | feedtype: 'linear' or 'circular'
|
---|
[1846] | 1341 |
|
---|
[1190] | 1342 | """
|
---|
| 1343 | self._setfeedtype(feedtype)
|
---|
| 1344 | self._add_history("set_feedtype", vars())
|
---|
| 1345 |
|
---|
[1862] | 1346 | @asaplog_post_dec
|
---|
[2897] | 1347 | def get_doppler(self):
|
---|
| 1348 | """\
|
---|
| 1349 | Get the doppler.
|
---|
| 1350 | """
|
---|
| 1351 | return self._getcoordinfo()[2]
|
---|
| 1352 |
|
---|
| 1353 | @asaplog_post_dec
|
---|
[276] | 1354 | def set_doppler(self, doppler='RADIO'):
|
---|
[1846] | 1355 | """\
|
---|
[276] | 1356 | Set the doppler for all following operations on this scantable.
|
---|
[1846] | 1357 |
|
---|
[276] | 1358 | Parameters:
|
---|
[1846] | 1359 |
|
---|
[276] | 1360 | doppler: One of 'RADIO', 'OPTICAL', 'Z', 'BETA', 'GAMMA'
|
---|
[1846] | 1361 |
|
---|
[276] | 1362 | """
|
---|
[484] | 1363 | varlist = vars()
|
---|
[276] | 1364 | inf = list(self._getcoordinfo())
|
---|
| 1365 | inf[2] = doppler
|
---|
| 1366 | self._setcoordinfo(inf)
|
---|
[1118] | 1367 | self._add_history("set_doppler", vars())
|
---|
[710] | 1368 |
|
---|
[1862] | 1369 | @asaplog_post_dec
|
---|
[226] | 1370 | def set_freqframe(self, frame=None):
|
---|
[1846] | 1371 | """\
|
---|
[113] | 1372 | Set the frame type of the Spectral Axis.
|
---|
[1846] | 1373 |
|
---|
[113] | 1374 | Parameters:
|
---|
[1846] | 1375 |
|
---|
[591] | 1376 | frame: an optional frame type, default 'LSRK'. Valid frames are:
|
---|
[1819] | 1377 | 'TOPO', 'LSRD', 'LSRK', 'BARY',
|
---|
[1118] | 1378 | 'GEO', 'GALACTO', 'LGROUP', 'CMB'
|
---|
[1846] | 1379 |
|
---|
| 1380 | Example::
|
---|
| 1381 |
|
---|
[113] | 1382 | scan.set_freqframe('BARY')
|
---|
[1846] | 1383 |
|
---|
[113] | 1384 | """
|
---|
[1593] | 1385 | frame = frame or rcParams['scantable.freqframe']
|
---|
[484] | 1386 | varlist = vars()
|
---|
[1819] | 1387 | # "REST" is not implemented in casacore
|
---|
| 1388 | #valid = ['REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', \
|
---|
| 1389 | # 'GEO', 'GALACTO', 'LGROUP', 'CMB']
|
---|
| 1390 | valid = ['TOPO', 'LSRD', 'LSRK', 'BARY', \
|
---|
[1118] | 1391 | 'GEO', 'GALACTO', 'LGROUP', 'CMB']
|
---|
[591] | 1392 |
|
---|
[989] | 1393 | if frame in valid:
|
---|
[113] | 1394 | inf = list(self._getcoordinfo())
|
---|
| 1395 | inf[1] = frame
|
---|
| 1396 | self._setcoordinfo(inf)
|
---|
[1118] | 1397 | self._add_history("set_freqframe", varlist)
|
---|
[102] | 1398 | else:
|
---|
[1118] | 1399 | msg = "Please specify a valid freq type. Valid types are:\n", valid
|
---|
[1859] | 1400 | raise TypeError(msg)
|
---|
[710] | 1401 |
|
---|
[1862] | 1402 | @asaplog_post_dec
|
---|
[989] | 1403 | def set_dirframe(self, frame=""):
|
---|
[1846] | 1404 | """\
|
---|
[989] | 1405 | Set the frame type of the Direction on the sky.
|
---|
[1846] | 1406 |
|
---|
[989] | 1407 | Parameters:
|
---|
[1846] | 1408 |
|
---|
[989] | 1409 | frame: an optional frame type, default ''. Valid frames are:
|
---|
| 1410 | 'J2000', 'B1950', 'GALACTIC'
|
---|
[1846] | 1411 |
|
---|
| 1412 | Example:
|
---|
| 1413 |
|
---|
[989] | 1414 | scan.set_dirframe('GALACTIC')
|
---|
[1846] | 1415 |
|
---|
[989] | 1416 | """
|
---|
| 1417 | varlist = vars()
|
---|
[1859] | 1418 | Scantable.set_dirframe(self, frame)
|
---|
[1118] | 1419 | self._add_history("set_dirframe", varlist)
|
---|
[989] | 1420 |
|
---|
[113] | 1421 | def get_unit(self):
|
---|
[1846] | 1422 | """\
|
---|
[113] | 1423 | Get the default unit set in this scantable
|
---|
[1846] | 1424 |
|
---|
[113] | 1425 | Returns:
|
---|
[1846] | 1426 |
|
---|
[113] | 1427 | A unit string
|
---|
[1846] | 1428 |
|
---|
[113] | 1429 | """
|
---|
| 1430 | inf = self._getcoordinfo()
|
---|
| 1431 | unit = inf[0]
|
---|
| 1432 | if unit == '': unit = 'channel'
|
---|
| 1433 | return unit
|
---|
[102] | 1434 |
|
---|
[1862] | 1435 | @asaplog_post_dec
|
---|
[158] | 1436 | def get_abcissa(self, rowno=0):
|
---|
[1846] | 1437 | """\
|
---|
[158] | 1438 | Get the abcissa in the current coordinate setup for the currently
|
---|
[113] | 1439 | selected Beam/IF/Pol
|
---|
[1846] | 1440 |
|
---|
[113] | 1441 | Parameters:
|
---|
[1846] | 1442 |
|
---|
[226] | 1443 | rowno: an optional row number in the scantable. Default is the
|
---|
| 1444 | first row, i.e. rowno=0
|
---|
[1846] | 1445 |
|
---|
[113] | 1446 | Returns:
|
---|
[1846] | 1447 |
|
---|
[1348] | 1448 | The abcissa values and the format string (as a dictionary)
|
---|
[1846] | 1449 |
|
---|
[113] | 1450 | """
|
---|
[256] | 1451 | abc = self._getabcissa(rowno)
|
---|
[710] | 1452 | lbl = self._getabcissalabel(rowno)
|
---|
[158] | 1453 | return abc, lbl
|
---|
[113] | 1454 |
|
---|
[1862] | 1455 | @asaplog_post_dec
|
---|
[2322] | 1456 | def flag(self, mask=None, unflag=False, row=-1):
|
---|
[1846] | 1457 | """\
|
---|
[1001] | 1458 | Flag the selected data using an optional channel mask.
|
---|
[1846] | 1459 |
|
---|
[1001] | 1460 | Parameters:
|
---|
[1846] | 1461 |
|
---|
[1001] | 1462 | mask: an optional channel mask, created with create_mask. Default
|
---|
| 1463 | (no mask) is all channels.
|
---|
[1855] | 1464 |
|
---|
[1819] | 1465 | unflag: if True, unflag the data
|
---|
[1846] | 1466 |
|
---|
[2322] | 1467 | row: an optional row number in the scantable.
|
---|
| 1468 | Default -1 flags all rows
|
---|
| 1469 |
|
---|
[1001] | 1470 | """
|
---|
| 1471 | varlist = vars()
|
---|
[1593] | 1472 | mask = mask or []
|
---|
[1994] | 1473 | self._flag(row, mask, unflag)
|
---|
[1001] | 1474 | self._add_history("flag", varlist)
|
---|
| 1475 |
|
---|
[1862] | 1476 | @asaplog_post_dec
|
---|
[2322] | 1477 | def flag_row(self, rows=None, unflag=False):
|
---|
[1846] | 1478 | """\
|
---|
[1819] | 1479 | Flag the selected data in row-based manner.
|
---|
[1846] | 1480 |
|
---|
[1819] | 1481 | Parameters:
|
---|
[1846] | 1482 |
|
---|
[1843] | 1483 | rows: list of row numbers to be flagged. Default is no row
|
---|
[2322] | 1484 | (must be explicitly specified to execute row-based
|
---|
| 1485 | flagging).
|
---|
[1855] | 1486 |
|
---|
[1819] | 1487 | unflag: if True, unflag the data.
|
---|
[1846] | 1488 |
|
---|
[1819] | 1489 | """
|
---|
| 1490 | varlist = vars()
|
---|
[2322] | 1491 | if rows is None:
|
---|
| 1492 | rows = []
|
---|
[1859] | 1493 | self._flag_row(rows, unflag)
|
---|
[1819] | 1494 | self._add_history("flag_row", varlist)
|
---|
| 1495 |
|
---|
[1862] | 1496 | @asaplog_post_dec
|
---|
[1819] | 1497 | def clip(self, uthres=None, dthres=None, clipoutside=True, unflag=False):
|
---|
[1846] | 1498 | """\
|
---|
[1819] | 1499 | Flag the selected data outside a specified range (in channel-base)
|
---|
[1846] | 1500 |
|
---|
[1819] | 1501 | Parameters:
|
---|
[1846] | 1502 |
|
---|
[1819] | 1503 | uthres: upper threshold.
|
---|
[1855] | 1504 |
|
---|
[1819] | 1505 | dthres: lower threshold
|
---|
[1846] | 1506 |
|
---|
[2322] | 1507 | clipoutside: True for flagging data outside the range
|
---|
| 1508 | [dthres:uthres].
|
---|
[1928] | 1509 | False for flagging data inside the range.
|
---|
[1855] | 1510 |
|
---|
[1846] | 1511 | unflag: if True, unflag the data.
|
---|
| 1512 |
|
---|
[1819] | 1513 | """
|
---|
| 1514 | varlist = vars()
|
---|
[1859] | 1515 | self._clip(uthres, dthres, clipoutside, unflag)
|
---|
[1819] | 1516 | self._add_history("clip", varlist)
|
---|
| 1517 |
|
---|
[1862] | 1518 | @asaplog_post_dec
|
---|
[1584] | 1519 | def lag_flag(self, start, end, unit="MHz", insitu=None):
|
---|
[1846] | 1520 | """\
|
---|
[1192] | 1521 | Flag the data in 'lag' space by providing a frequency to remove.
|
---|
[2177] | 1522 | Flagged data in the scantable get interpolated over the region.
|
---|
[1192] | 1523 | No taper is applied.
|
---|
[1846] | 1524 |
|
---|
[1192] | 1525 | Parameters:
|
---|
[1846] | 1526 |
|
---|
[1579] | 1527 | start: the start frequency (really a period within the
|
---|
| 1528 | bandwidth) or period to remove
|
---|
[1855] | 1529 |
|
---|
[1579] | 1530 | end: the end frequency or period to remove
|
---|
[1855] | 1531 |
|
---|
[2431] | 1532 | unit: the frequency unit (default 'MHz') or '' for
|
---|
[1579] | 1533 | explicit lag channels
|
---|
[1846] | 1534 |
|
---|
| 1535 | *Notes*:
|
---|
| 1536 |
|
---|
[1579] | 1537 | It is recommended to flag edges of the band or strong
|
---|
[1348] | 1538 | signals beforehand.
|
---|
[1846] | 1539 |
|
---|
[1192] | 1540 | """
|
---|
| 1541 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 1542 | self._math._setinsitu(insitu)
|
---|
| 1543 | varlist = vars()
|
---|
[1579] | 1544 | base = { "GHz": 1000000000., "MHz": 1000000., "kHz": 1000., "Hz": 1.}
|
---|
| 1545 | if not (unit == "" or base.has_key(unit)):
|
---|
[1192] | 1546 | raise ValueError("%s is not a valid unit." % unit)
|
---|
[1859] | 1547 | if unit == "":
|
---|
| 1548 | s = scantable(self._math._lag_flag(self, start, end, "lags"))
|
---|
| 1549 | else:
|
---|
| 1550 | s = scantable(self._math._lag_flag(self, start*base[unit],
|
---|
| 1551 | end*base[unit], "frequency"))
|
---|
[1192] | 1552 | s._add_history("lag_flag", varlist)
|
---|
| 1553 | if insitu:
|
---|
| 1554 | self._assign(s)
|
---|
| 1555 | else:
|
---|
| 1556 | return s
|
---|
[1001] | 1557 |
|
---|
[1862] | 1558 | @asaplog_post_dec
|
---|
[2349] | 1559 | def fft(self, rowno=None, mask=None, getrealimag=False):
|
---|
[2177] | 1560 | """\
|
---|
| 1561 | Apply FFT to the spectra.
|
---|
| 1562 | Flagged data in the scantable get interpolated over the region.
|
---|
| 1563 |
|
---|
| 1564 | Parameters:
|
---|
[2186] | 1565 |
|
---|
| 1566 | rowno: The row number(s) to be processed. int, list
|
---|
[2349] | 1567 | and tuple are accepted. By default (None), FFT
|
---|
[2186] | 1568 | is applied to the whole data.
|
---|
| 1569 |
|
---|
| 1570 | mask: Auxiliary channel mask(s). Given as a boolean
|
---|
| 1571 | list, it is applied to all specified rows.
|
---|
| 1572 | A list of boolean lists can also be used to
|
---|
| 1573 | apply different masks. In the latter case, the
|
---|
| 1574 | length of 'mask' must be the same as that of
|
---|
[2349] | 1575 | 'rowno'. The default is None.
|
---|
[2177] | 1576 |
|
---|
| 1577 | getrealimag: If True, returns the real and imaginary part
|
---|
| 1578 | values of the complex results.
|
---|
| 1579 | If False (the default), returns the amplitude
|
---|
| 1580 | (absolute value) normalised with Ndata/2 and
|
---|
| 1581 | phase (argument, in unit of radian).
|
---|
| 1582 |
|
---|
| 1583 | Returns:
|
---|
| 1584 |
|
---|
[2186] | 1585 | A list of dictionaries containing the results for each spectrum.
|
---|
| 1586 | Each dictionary contains two values, the real and the imaginary
|
---|
| 1587 | parts when getrealimag = True, or the amplitude(absolute value)
|
---|
| 1588 | and the phase(argument) when getrealimag = False. The key for
|
---|
| 1589 | these values are 'real' and 'imag', or 'ampl' and 'phase',
|
---|
[2177] | 1590 | respectively.
|
---|
| 1591 | """
|
---|
[2349] | 1592 | if rowno is None:
|
---|
| 1593 | rowno = []
|
---|
[2177] | 1594 | if isinstance(rowno, int):
|
---|
| 1595 | rowno = [rowno]
|
---|
| 1596 | elif not (isinstance(rowno, list) or isinstance(rowno, tuple)):
|
---|
[2186] | 1597 | raise TypeError("The row number(s) must be int, list or tuple.")
|
---|
| 1598 | if len(rowno) == 0: rowno = [i for i in xrange(self.nrow())]
|
---|
| 1599 |
|
---|
[2411] | 1600 | usecommonmask = True
|
---|
| 1601 |
|
---|
| 1602 | if mask is None:
|
---|
| 1603 | mask = []
|
---|
| 1604 | if isinstance(mask, list) or isinstance(mask, tuple):
|
---|
| 1605 | if len(mask) == 0:
|
---|
| 1606 | mask = [[]]
|
---|
| 1607 | else:
|
---|
| 1608 | if isinstance(mask[0], bool):
|
---|
| 1609 | if len(mask) != self.nchan(self.getif(rowno[0])):
|
---|
| 1610 | raise ValueError("The spectra and the mask have "
|
---|
| 1611 | "different length.")
|
---|
| 1612 | mask = [mask]
|
---|
| 1613 | elif isinstance(mask[0], list) or isinstance(mask[0], tuple):
|
---|
| 1614 | usecommonmask = False
|
---|
| 1615 | if len(mask) != len(rowno):
|
---|
| 1616 | raise ValueError("When specifying masks for each "
|
---|
| 1617 | "spectrum, the numbers of them "
|
---|
| 1618 | "must be identical.")
|
---|
| 1619 | for i in xrange(mask):
|
---|
| 1620 | if len(mask[i]) != self.nchan(self.getif(rowno[i])):
|
---|
| 1621 | raise ValueError("The spectra and the mask have "
|
---|
| 1622 | "different length.")
|
---|
| 1623 | else:
|
---|
| 1624 | raise TypeError("The mask must be a boolean list or "
|
---|
| 1625 | "a list of boolean list.")
|
---|
| 1626 | else:
|
---|
[2349] | 1627 | raise TypeError("The mask must be a boolean list or a list of "
|
---|
| 1628 | "boolean list.")
|
---|
[2186] | 1629 |
|
---|
| 1630 | res = []
|
---|
| 1631 |
|
---|
| 1632 | imask = 0
|
---|
| 1633 | for whichrow in rowno:
|
---|
| 1634 | fspec = self._fft(whichrow, mask[imask], getrealimag)
|
---|
| 1635 | nspec = len(fspec)
|
---|
[2177] | 1636 |
|
---|
[2349] | 1637 | i = 0
|
---|
| 1638 | v1 = []
|
---|
| 1639 | v2 = []
|
---|
| 1640 | reselem = {"real":[],"imag":[]} if getrealimag \
|
---|
| 1641 | else {"ampl":[],"phase":[]}
|
---|
[2177] | 1642 |
|
---|
[2186] | 1643 | while (i < nspec):
|
---|
| 1644 | v1.append(fspec[i])
|
---|
| 1645 | v2.append(fspec[i+1])
|
---|
[2349] | 1646 | i += 2
|
---|
[2186] | 1647 |
|
---|
[2177] | 1648 | if getrealimag:
|
---|
[2186] | 1649 | reselem["real"] += v1
|
---|
| 1650 | reselem["imag"] += v2
|
---|
[2177] | 1651 | else:
|
---|
[2186] | 1652 | reselem["ampl"] += v1
|
---|
| 1653 | reselem["phase"] += v2
|
---|
[2177] | 1654 |
|
---|
[2186] | 1655 | res.append(reselem)
|
---|
| 1656 |
|
---|
[2349] | 1657 | if not usecommonmask:
|
---|
| 1658 | imask += 1
|
---|
[2186] | 1659 |
|
---|
[2177] | 1660 | return res
|
---|
| 1661 |
|
---|
| 1662 | @asaplog_post_dec
|
---|
[113] | 1663 | def create_mask(self, *args, **kwargs):
|
---|
[1846] | 1664 | """\
|
---|
[1118] | 1665 | Compute and return a mask based on [min, max] windows.
|
---|
[189] | 1666 | The specified windows are to be INCLUDED, when the mask is
|
---|
[113] | 1667 | applied.
|
---|
[1846] | 1668 |
|
---|
[102] | 1669 | Parameters:
|
---|
[1846] | 1670 |
|
---|
[1118] | 1671 | [min, max], [min2, max2], ...
|
---|
[1024] | 1672 | Pairs of start/end points (inclusive)specifying the regions
|
---|
[102] | 1673 | to be masked
|
---|
[1855] | 1674 |
|
---|
[189] | 1675 | invert: optional argument. If specified as True,
|
---|
| 1676 | return an inverted mask, i.e. the regions
|
---|
| 1677 | specified are EXCLUDED
|
---|
[1855] | 1678 |
|
---|
[513] | 1679 | row: create the mask using the specified row for
|
---|
| 1680 | unit conversions, default is row=0
|
---|
| 1681 | only necessary if frequency varies over rows.
|
---|
[1846] | 1682 |
|
---|
| 1683 | Examples::
|
---|
| 1684 |
|
---|
[113] | 1685 | scan.set_unit('channel')
|
---|
[1846] | 1686 | # a)
|
---|
[1118] | 1687 | msk = scan.create_mask([400, 500], [800, 900])
|
---|
[189] | 1688 | # masks everything outside 400 and 500
|
---|
[113] | 1689 | # and 800 and 900 in the unit 'channel'
|
---|
| 1690 |
|
---|
[1846] | 1691 | # b)
|
---|
[1118] | 1692 | msk = scan.create_mask([400, 500], [800, 900], invert=True)
|
---|
[189] | 1693 | # masks the regions between 400 and 500
|
---|
[113] | 1694 | # and 800 and 900 in the unit 'channel'
|
---|
[1846] | 1695 |
|
---|
| 1696 | # c)
|
---|
| 1697 | #mask only channel 400
|
---|
[1554] | 1698 | msk = scan.create_mask([400])
|
---|
[1846] | 1699 |
|
---|
[102] | 1700 | """
|
---|
[1554] | 1701 | row = kwargs.get("row", 0)
|
---|
[513] | 1702 | data = self._getabcissa(row)
|
---|
[113] | 1703 | u = self._getcoordinfo()[0]
|
---|
[1859] | 1704 | if u == "":
|
---|
| 1705 | u = "channel"
|
---|
| 1706 | msg = "The current mask window unit is %s" % u
|
---|
| 1707 | i = self._check_ifs()
|
---|
| 1708 | if not i:
|
---|
| 1709 | msg += "\nThis mask is only valid for IF=%d" % (self.getif(i))
|
---|
| 1710 | asaplog.push(msg)
|
---|
[2348] | 1711 | n = len(data)
|
---|
[1295] | 1712 | msk = _n_bools(n, False)
|
---|
[710] | 1713 | # test if args is a 'list' or a 'normal *args - UGLY!!!
|
---|
| 1714 |
|
---|
[2349] | 1715 | ws = (isinstance(args[-1][-1], int)
|
---|
| 1716 | or isinstance(args[-1][-1], float)) and args or args[0]
|
---|
[710] | 1717 | for window in ws:
|
---|
[1554] | 1718 | if len(window) == 1:
|
---|
| 1719 | window = [window[0], window[0]]
|
---|
| 1720 | if len(window) == 0 or len(window) > 2:
|
---|
[2349] | 1721 | raise ValueError("A window needs to be defined as "
|
---|
| 1722 | "[start(, end)]")
|
---|
[1545] | 1723 | if window[0] > window[1]:
|
---|
| 1724 | tmp = window[0]
|
---|
| 1725 | window[0] = window[1]
|
---|
| 1726 | window[1] = tmp
|
---|
[102] | 1727 | for i in range(n):
|
---|
[1024] | 1728 | if data[i] >= window[0] and data[i] <= window[1]:
|
---|
[1295] | 1729 | msk[i] = True
|
---|
[113] | 1730 | if kwargs.has_key('invert'):
|
---|
| 1731 | if kwargs.get('invert'):
|
---|
[1295] | 1732 | msk = mask_not(msk)
|
---|
[102] | 1733 | return msk
|
---|
[710] | 1734 |
|
---|
[1931] | 1735 | def get_masklist(self, mask=None, row=0, silent=False):
|
---|
[1846] | 1736 | """\
|
---|
[1819] | 1737 | Compute and return a list of mask windows, [min, max].
|
---|
[1846] | 1738 |
|
---|
[1819] | 1739 | Parameters:
|
---|
[1846] | 1740 |
|
---|
[1819] | 1741 | mask: channel mask, created with create_mask.
|
---|
[1855] | 1742 |
|
---|
[1819] | 1743 | row: calcutate the masklist using the specified row
|
---|
| 1744 | for unit conversions, default is row=0
|
---|
| 1745 | only necessary if frequency varies over rows.
|
---|
[1846] | 1746 |
|
---|
[1819] | 1747 | Returns:
|
---|
[1846] | 1748 |
|
---|
[1819] | 1749 | [min, max], [min2, max2], ...
|
---|
| 1750 | Pairs of start/end points (inclusive)specifying
|
---|
| 1751 | the masked regions
|
---|
[1846] | 1752 |
|
---|
[1819] | 1753 | """
|
---|
| 1754 | if not (isinstance(mask,list) or isinstance(mask, tuple)):
|
---|
| 1755 | raise TypeError("The mask should be list or tuple.")
|
---|
[2427] | 1756 | if len(mask) <= 0:
|
---|
| 1757 | raise TypeError("The mask elements should be > 0")
|
---|
[2348] | 1758 | data = self._getabcissa(row)
|
---|
| 1759 | if len(data) != len(mask):
|
---|
[1819] | 1760 | msg = "Number of channels in scantable != number of mask elements"
|
---|
| 1761 | raise TypeError(msg)
|
---|
| 1762 | u = self._getcoordinfo()[0]
|
---|
[1859] | 1763 | if u == "":
|
---|
| 1764 | u = "channel"
|
---|
| 1765 | msg = "The current mask window unit is %s" % u
|
---|
| 1766 | i = self._check_ifs()
|
---|
| 1767 | if not i:
|
---|
| 1768 | msg += "\nThis mask is only valid for IF=%d" % (self.getif(i))
|
---|
[1931] | 1769 | if not silent:
|
---|
| 1770 | asaplog.push(msg)
|
---|
[2349] | 1771 | masklist = []
|
---|
[1819] | 1772 | ist, ien = None, None
|
---|
| 1773 | ist, ien=self.get_mask_indices(mask)
|
---|
| 1774 | if ist is not None and ien is not None:
|
---|
| 1775 | for i in xrange(len(ist)):
|
---|
| 1776 | range=[data[ist[i]],data[ien[i]]]
|
---|
| 1777 | range.sort()
|
---|
| 1778 | masklist.append([range[0],range[1]])
|
---|
| 1779 | return masklist
|
---|
| 1780 |
|
---|
| 1781 | def get_mask_indices(self, mask=None):
|
---|
[1846] | 1782 | """\
|
---|
[1819] | 1783 | Compute and Return lists of mask start indices and mask end indices.
|
---|
[1855] | 1784 |
|
---|
| 1785 | Parameters:
|
---|
| 1786 |
|
---|
[1819] | 1787 | mask: channel mask, created with create_mask.
|
---|
[1846] | 1788 |
|
---|
[1819] | 1789 | Returns:
|
---|
[1846] | 1790 |
|
---|
[1819] | 1791 | List of mask start indices and that of mask end indices,
|
---|
| 1792 | i.e., [istart1,istart2,....], [iend1,iend2,....].
|
---|
[1846] | 1793 |
|
---|
[1819] | 1794 | """
|
---|
| 1795 | if not (isinstance(mask,list) or isinstance(mask, tuple)):
|
---|
| 1796 | raise TypeError("The mask should be list or tuple.")
|
---|
[2427] | 1797 | if len(mask) <= 0:
|
---|
| 1798 | raise TypeError("The mask elements should be > 0")
|
---|
[2349] | 1799 | istart = []
|
---|
| 1800 | iend = []
|
---|
| 1801 | if mask[0]:
|
---|
| 1802 | istart.append(0)
|
---|
[1819] | 1803 | for i in range(len(mask)-1):
|
---|
| 1804 | if not mask[i] and mask[i+1]:
|
---|
| 1805 | istart.append(i+1)
|
---|
| 1806 | elif mask[i] and not mask[i+1]:
|
---|
| 1807 | iend.append(i)
|
---|
[2349] | 1808 | if mask[len(mask)-1]:
|
---|
| 1809 | iend.append(len(mask)-1)
|
---|
[1819] | 1810 | if len(istart) != len(iend):
|
---|
| 1811 | raise RuntimeError("Numbers of mask start != mask end.")
|
---|
| 1812 | for i in range(len(istart)):
|
---|
| 1813 | if istart[i] > iend[i]:
|
---|
| 1814 | raise RuntimeError("Mask start index > mask end index")
|
---|
| 1815 | break
|
---|
| 1816 | return istart,iend
|
---|
| 1817 |
|
---|
[2013] | 1818 | @asaplog_post_dec
|
---|
[2882] | 1819 | def parse_spw_selection(self, selectstring, restfreq=None, frame=None, doppler=None):
|
---|
| 1820 | """
|
---|
| 1821 | Parse MS type spw/channel selection syntax.
|
---|
| 1822 |
|
---|
| 1823 | Parameters:
|
---|
| 1824 | selectstring : A string expression of spw and channel selection.
|
---|
| 1825 | Comma-separated expressions mean different spw -
|
---|
| 1826 | channel combinations. Spws and channel selections
|
---|
| 1827 | are partitioned by a colon ':'. In a single
|
---|
| 1828 | selection expression, you can put multiple values
|
---|
| 1829 | separated by semicolons ';'. Both for spw and
|
---|
| 1830 | channel selection, allowed cases include single
|
---|
| 1831 | value, blank('') or asterisk('*') to specify all
|
---|
| 1832 | available values, two values connected with a
|
---|
| 1833 | tilde ('~') to specify an inclusive range. Unit
|
---|
| 1834 | strings for frequency or velocity can be added to
|
---|
| 1835 | the tilde-connected values. For channel selection
|
---|
| 1836 | expression, placing a '<' or a '>' is possible to
|
---|
| 1837 | specify a semi-infinite interval as well.
|
---|
| 1838 |
|
---|
| 1839 | examples:
|
---|
| 1840 | '' or '*' = all spws (all channels)
|
---|
| 1841 | '<2,4~6,9' = Spws 0,1,4,5,6,9 (all channels)
|
---|
| 1842 | '3:3~45;60' = channels 3 to 45 and 60 in spw 3
|
---|
| 1843 | '0~1:2~6,8' = channels 2 to 6 in spws 0,1, and
|
---|
| 1844 | all channels in spw8
|
---|
[2936] | 1845 | '1.3~1.5GHz' = all spws whose central frequency
|
---|
| 1846 | falls in frequency range between
|
---|
| 1847 | 1.3GHz and 1.5GHz.
|
---|
| 1848 | '1.3~1.5GHz:1.3~1.5GHz' = channels which fall
|
---|
[2884] | 1849 | between the specified
|
---|
| 1850 | frequency range in spws
|
---|
[2936] | 1851 | whose central frequency
|
---|
| 1852 | falls in the specified
|
---|
| 1853 | frequency range.
|
---|
[2884] | 1854 | '1:-200~250km/s' = channels that fall between the
|
---|
| 1855 | specified velocity range in
|
---|
| 1856 | spw 1.
|
---|
[2897] | 1857 | restfreq: the rest frequency.
|
---|
| 1858 | examples: '115.2712GHz', 115271201800.0
|
---|
| 1859 | frame: an optional frame type, default 'LSRK'. Valid frames are:
|
---|
| 1860 | 'TOPO', 'LSRD', 'LSRK', 'BARY',
|
---|
| 1861 | 'GEO', 'GALACTO', 'LGROUP', 'CMB'
|
---|
| 1862 | doppler: one of 'RADIO', 'OPTICAL', 'Z', 'BETA', 'GAMMA'
|
---|
[2882] | 1863 | Returns:
|
---|
| 1864 | A dictionary of selected (valid) spw and masklist pairs,
|
---|
| 1865 | e.g. {'0': [[50,250],[350,462]], '2': [[100,400],[550,974]]}
|
---|
| 1866 | """
|
---|
| 1867 | if not isinstance(selectstring, str):
|
---|
| 1868 | asaplog.post()
|
---|
| 1869 | asaplog.push("Expression of spw/channel selection must be a string.")
|
---|
| 1870 | asaplog.post("ERROR")
|
---|
| 1871 |
|
---|
| 1872 | orig_unit = self.get_unit()
|
---|
| 1873 | self.set_unit('channel')
|
---|
| 1874 |
|
---|
[2891] | 1875 | if restfreq is not None:
|
---|
[2892] | 1876 | orig_molids = self._getmolidcol_list()
|
---|
| 1877 | set_restfreq(self, restfreq)
|
---|
[2882] | 1878 |
|
---|
[2897] | 1879 | orig_coord = self._getcoordinfo()
|
---|
[2892] | 1880 |
|
---|
| 1881 | if frame is not None:
|
---|
| 1882 | orig_frame = orig_coord[1]
|
---|
| 1883 | self.set_freqframe(frame)
|
---|
| 1884 |
|
---|
| 1885 | if doppler is not None:
|
---|
| 1886 | orig_doppler = orig_coord[2]
|
---|
| 1887 | self.set_doppler(doppler)
|
---|
[2882] | 1888 |
|
---|
| 1889 | valid_ifs = self.getifnos()
|
---|
| 1890 |
|
---|
| 1891 | comma_sep = selectstring.split(",")
|
---|
| 1892 | res = {}
|
---|
| 1893 |
|
---|
| 1894 | for cms_elem in comma_sep:
|
---|
| 1895 | colon_sep = cms_elem.split(":")
|
---|
| 1896 |
|
---|
| 1897 | if (len(colon_sep) > 2):
|
---|
| 1898 | raise RuntimeError("Invalid selection expression: more than two colons!")
|
---|
| 1899 |
|
---|
| 1900 | # parse spw expression and store result in spw_list.
|
---|
| 1901 | # allowed cases include '', '*', 'a', '<a', '>a', 'a~b',
|
---|
[2884] | 1902 | # 'a~b*Hz' (where * can be '', 'k', 'M', 'G' etc.),
|
---|
| 1903 | # 'a~b*m/s' (where * can be '' or 'k') and also
|
---|
[2882] | 1904 | # several of the above expressions connected with ';'.
|
---|
| 1905 |
|
---|
| 1906 | spw_list = []
|
---|
| 1907 |
|
---|
| 1908 | semicolon_sep = colon_sep[0].split(";")
|
---|
| 1909 |
|
---|
| 1910 | for scs_elem in semicolon_sep:
|
---|
| 1911 | scs_elem = scs_elem.strip()
|
---|
| 1912 |
|
---|
| 1913 | lt_sep = scs_elem.split("<")
|
---|
| 1914 | gt_sep = scs_elem.split(">")
|
---|
| 1915 | ti_sep = scs_elem.split("~")
|
---|
| 1916 |
|
---|
| 1917 | lt_sep_length = len(lt_sep)
|
---|
| 1918 | gt_sep_length = len(gt_sep)
|
---|
| 1919 | ti_sep_length = len(ti_sep)
|
---|
| 1920 |
|
---|
| 1921 | len_product = lt_sep_length * gt_sep_length * ti_sep_length
|
---|
| 1922 |
|
---|
| 1923 | if (len_product > 2):
|
---|
| 1924 | # '<', '>' and '~' must not coexist in a single spw expression
|
---|
| 1925 |
|
---|
| 1926 | raise RuntimeError("Invalid spw selection.")
|
---|
| 1927 |
|
---|
| 1928 | elif (len_product == 1):
|
---|
| 1929 | # '', '*', or single spw number.
|
---|
| 1930 |
|
---|
| 1931 | if (scs_elem == "") or (scs_elem == "*"):
|
---|
| 1932 | spw_list = valid_ifs[:] # deep copy
|
---|
| 1933 |
|
---|
| 1934 | else: # single number
|
---|
[2887] | 1935 | expr = int(scs_elem)
|
---|
| 1936 | spw_list.append(expr)
|
---|
| 1937 | if expr not in valid_ifs:
|
---|
| 1938 | asaplog.push("Invalid spw given. Ignored.")
|
---|
| 1939 |
|
---|
[2882] | 1940 | else: # (len_product == 2)
|
---|
[2887] | 1941 | # namely, one of '<', '>' or '~' appears just once.
|
---|
[2882] | 1942 |
|
---|
| 1943 | if (lt_sep_length == 2): # '<a'
|
---|
| 1944 | if is_number(lt_sep[1]):
|
---|
[2886] | 1945 | no_valid_spw = True
|
---|
[2882] | 1946 | for i in valid_ifs:
|
---|
| 1947 | if (i < float(lt_sep[1])):
|
---|
| 1948 | spw_list.append(i)
|
---|
[2886] | 1949 | no_valid_spw = False
|
---|
| 1950 |
|
---|
| 1951 | if no_valid_spw:
|
---|
| 1952 | raise ValueError("Invalid spw selection ('<" + str(lt_sep[1]) + "').")
|
---|
[2882] | 1953 |
|
---|
| 1954 | else:
|
---|
[2886] | 1955 | raise RuntimeError("Invalid spw selection.")
|
---|
[2882] | 1956 |
|
---|
| 1957 | elif (gt_sep_length == 2): # '>a'
|
---|
| 1958 | if is_number(gt_sep[1]):
|
---|
[2886] | 1959 | no_valid_spw = True
|
---|
[2882] | 1960 | for i in valid_ifs:
|
---|
| 1961 | if (i > float(gt_sep[1])):
|
---|
| 1962 | spw_list.append(i)
|
---|
[2886] | 1963 | no_valid_spw = False
|
---|
| 1964 |
|
---|
| 1965 | if no_valid_spw:
|
---|
| 1966 | raise ValueError("Invalid spw selection ('>" + str(gt_sep[1]) + "').")
|
---|
[2882] | 1967 |
|
---|
| 1968 | else:
|
---|
[2886] | 1969 | raise RuntimeError("Invalid spw selection.")
|
---|
[2882] | 1970 |
|
---|
| 1971 | else: # (ti_sep_length == 2) where both boundaries inclusive
|
---|
| 1972 | expr0 = ti_sep[0].strip()
|
---|
| 1973 | expr1 = ti_sep[1].strip()
|
---|
| 1974 |
|
---|
| 1975 | if is_number(expr0) and is_number(expr1):
|
---|
| 1976 | # 'a~b'
|
---|
| 1977 | expr_pmin = min(float(expr0), float(expr1))
|
---|
| 1978 | expr_pmax = max(float(expr0), float(expr1))
|
---|
[2887] | 1979 | has_invalid_spw = False
|
---|
[2886] | 1980 | no_valid_spw = True
|
---|
| 1981 |
|
---|
[2882] | 1982 | for i in valid_ifs:
|
---|
| 1983 | if (expr_pmin <= i) and (i <= expr_pmax):
|
---|
| 1984 | spw_list.append(i)
|
---|
[2886] | 1985 | no_valid_spw = False
|
---|
[2887] | 1986 | else:
|
---|
| 1987 | has_invalid_spw = True
|
---|
[2886] | 1988 |
|
---|
[2887] | 1989 | if has_invalid_spw:
|
---|
| 1990 | msg = "Invalid spw is given. Ignored."
|
---|
| 1991 | asaplog.push(msg)
|
---|
| 1992 | asaplog.post()
|
---|
| 1993 |
|
---|
[2886] | 1994 | if no_valid_spw:
|
---|
| 1995 | raise ValueError("No valid spw in range ('" + str(expr_pmin) + "~" + str(expr_pmax) + "').")
|
---|
[2887] | 1996 |
|
---|
[2884] | 1997 | elif is_number(expr0) and is_frequency(expr1):
|
---|
| 1998 | # 'a~b*Hz'
|
---|
| 1999 | (expr_f0, expr_f1) = get_freq_by_string(expr0, expr1)
|
---|
[2936] | 2000 | expr_fmin = min(expr_f0, expr_f1)
|
---|
| 2001 | expr_fmax = max(expr_f0, expr_f1)
|
---|
[2886] | 2002 | no_valid_spw = True
|
---|
| 2003 |
|
---|
[2882] | 2004 | for coord in self._get_coordinate_list():
|
---|
[2936] | 2005 | spw = coord['if']
|
---|
| 2006 |
|
---|
| 2007 | """
|
---|
[2882] | 2008 | expr_p0 = coord['coord'].to_pixel(expr_f0)
|
---|
| 2009 | expr_p1 = coord['coord'].to_pixel(expr_f1)
|
---|
| 2010 | expr_pmin = min(expr_p0, expr_p1)
|
---|
| 2011 | expr_pmax = max(expr_p0, expr_p1)
|
---|
| 2012 |
|
---|
| 2013 | pmin = 0.0
|
---|
| 2014 | pmax = float(self.nchan(spw) - 1)
|
---|
[2936] | 2015 |
|
---|
[2882] | 2016 | if ((expr_pmax - pmin)*(expr_pmin - pmax) <= 0.0):
|
---|
| 2017 | spw_list.append(spw)
|
---|
[2886] | 2018 | no_valid_spw = False
|
---|
[2936] | 2019 | """
|
---|
[2886] | 2020 |
|
---|
[2937] | 2021 | crd = coord['coord']
|
---|
| 2022 | fhead = crd.to_frequency(0)
|
---|
| 2023 | ftail = crd.to_frequency(self.nchan(spw) - 1)
|
---|
[2936] | 2024 | fcen = (fhead + ftail) / 2.0
|
---|
| 2025 |
|
---|
| 2026 | if ((expr_fmin <= fcen) and (fcen <= expr_fmax)):
|
---|
| 2027 | spw_list.append(spw)
|
---|
| 2028 | no_valid_spw = False
|
---|
| 2029 |
|
---|
[2886] | 2030 | if no_valid_spw:
|
---|
| 2031 | raise ValueError("No valid spw in range ('" + str(expr0) + "~" + str(expr1) + "').")
|
---|
[2882] | 2032 |
|
---|
[2884] | 2033 | elif is_number(expr0) and is_velocity(expr1):
|
---|
| 2034 | # 'a~b*m/s'
|
---|
| 2035 | (expr_v0, expr_v1) = get_velocity_by_string(expr0, expr1)
|
---|
[2882] | 2036 | expr_vmin = min(expr_v0, expr_v1)
|
---|
| 2037 | expr_vmax = max(expr_v0, expr_v1)
|
---|
[2886] | 2038 | no_valid_spw = True
|
---|
| 2039 |
|
---|
[2882] | 2040 | for coord in self._get_coordinate_list():
|
---|
| 2041 | spw = coord['if']
|
---|
[2936] | 2042 |
|
---|
| 2043 | """
|
---|
[2882] | 2044 | pmin = 0.0
|
---|
| 2045 | pmax = float(self.nchan(spw) - 1)
|
---|
| 2046 |
|
---|
| 2047 | vel0 = coord['coord'].to_velocity(pmin)
|
---|
| 2048 | vel1 = coord['coord'].to_velocity(pmax)
|
---|
| 2049 |
|
---|
| 2050 | vmin = min(vel0, vel1)
|
---|
| 2051 | vmax = max(vel0, vel1)
|
---|
| 2052 |
|
---|
| 2053 | if ((expr_vmax - vmin)*(expr_vmin - vmax) <= 0.0):
|
---|
| 2054 | spw_list.append(spw)
|
---|
[2886] | 2055 | no_valid_spw = False
|
---|
[2936] | 2056 | """
|
---|
[2886] | 2057 |
|
---|
[2937] | 2058 | crd = coord['coord']
|
---|
| 2059 | fhead = crd.to_frequency(0)
|
---|
| 2060 | ftail = crd.to_frequency(self.nchan(spw) - 1)
|
---|
| 2061 | fcen = (fhead + ftail) / 2.0
|
---|
| 2062 | vcen = crd.to_velocity(crd.to_pixel(fcen))
|
---|
[2936] | 2063 |
|
---|
| 2064 | if ((expr_vmin <= vcen) and (vcen <= expr_vmax)):
|
---|
| 2065 | spw_list.append(spw)
|
---|
| 2066 | no_valid_spw = False
|
---|
| 2067 |
|
---|
[2886] | 2068 | if no_valid_spw:
|
---|
| 2069 | raise ValueError("No valid spw in range ('" + str(expr0) + "~" + str(expr1) + "').")
|
---|
[2882] | 2070 |
|
---|
| 2071 | else:
|
---|
| 2072 | # cases such as 'aGHz~bkm/s' are not allowed now
|
---|
| 2073 | raise RuntimeError("Invalid spw selection.")
|
---|
| 2074 |
|
---|
[2887] | 2075 | # check spw list and remove invalid ones.
|
---|
| 2076 | # if no valid spw left, emit ValueError.
|
---|
| 2077 | if len(spw_list) == 0:
|
---|
| 2078 | raise ValueError("No valid spw in given range.")
|
---|
| 2079 |
|
---|
[2882] | 2080 | # parse channel expression and store the result in crange_list.
|
---|
| 2081 | # allowed cases include '', 'a~b', 'a*Hz~b*Hz' (where * can be
|
---|
| 2082 | # '', 'k', 'M', 'G' etc.), 'a*m/s~b*m/s' (where * can be '' or 'k')
|
---|
| 2083 | # and also several of the above expressions connected with ';'.
|
---|
| 2084 |
|
---|
| 2085 | for spw in spw_list:
|
---|
| 2086 | pmin = 0.0
|
---|
| 2087 | pmax = float(self.nchan(spw) - 1)
|
---|
[2909] | 2088 |
|
---|
| 2089 | molid = self._getmolidcol_list()[self.get_first_rowno_by_if(spw)]
|
---|
[2882] | 2090 |
|
---|
| 2091 | if (len(colon_sep) == 1):
|
---|
| 2092 | # no expression for channel selection,
|
---|
| 2093 | # which means all channels are to be selected.
|
---|
| 2094 | crange_list = [[pmin, pmax]]
|
---|
| 2095 |
|
---|
| 2096 | else: # (len(colon_sep) == 2)
|
---|
| 2097 | crange_list = []
|
---|
| 2098 |
|
---|
| 2099 | found = False
|
---|
| 2100 | for i in self._get_coordinate_list():
|
---|
| 2101 | if (i['if'] == spw):
|
---|
| 2102 | coord = i['coord']
|
---|
| 2103 | found = True
|
---|
| 2104 | break
|
---|
| 2105 |
|
---|
[2887] | 2106 | if found:
|
---|
| 2107 | semicolon_sep = colon_sep[1].split(";")
|
---|
| 2108 | for scs_elem in semicolon_sep:
|
---|
| 2109 | scs_elem = scs_elem.strip()
|
---|
[2882] | 2110 |
|
---|
[2887] | 2111 | ti_sep = scs_elem.split("~")
|
---|
| 2112 | ti_sep_length = len(ti_sep)
|
---|
[2882] | 2113 |
|
---|
[2887] | 2114 | if (ti_sep_length > 2):
|
---|
| 2115 | raise RuntimeError("Invalid channel selection.")
|
---|
[2882] | 2116 |
|
---|
[2887] | 2117 | elif (ti_sep_length == 1):
|
---|
| 2118 | if (scs_elem == "") or (scs_elem == "*"):
|
---|
| 2119 | # '' and '*' for all channels
|
---|
| 2120 | crange_list = [[pmin, pmax]]
|
---|
| 2121 | break
|
---|
| 2122 | elif (is_number(scs_elem)):
|
---|
| 2123 | # single channel given
|
---|
| 2124 | crange_list.append([float(scs_elem), float(scs_elem)])
|
---|
| 2125 | else:
|
---|
| 2126 | raise RuntimeError("Invalid channel selection.")
|
---|
[2882] | 2127 |
|
---|
[2887] | 2128 | else: #(ti_sep_length == 2)
|
---|
| 2129 | expr0 = ti_sep[0].strip()
|
---|
| 2130 | expr1 = ti_sep[1].strip()
|
---|
[2882] | 2131 |
|
---|
[2887] | 2132 | if is_number(expr0) and is_number(expr1):
|
---|
| 2133 | # 'a~b'
|
---|
| 2134 | expr_pmin = min(float(expr0), float(expr1))
|
---|
| 2135 | expr_pmax = max(float(expr0), float(expr1))
|
---|
[2882] | 2136 |
|
---|
[2887] | 2137 | elif is_number(expr0) and is_frequency(expr1):
|
---|
| 2138 | # 'a~b*Hz'
|
---|
| 2139 | (expr_f0, expr_f1) = get_freq_by_string(expr0, expr1)
|
---|
| 2140 | expr_p0 = coord.to_pixel(expr_f0)
|
---|
| 2141 | expr_p1 = coord.to_pixel(expr_f1)
|
---|
| 2142 | expr_pmin = min(expr_p0, expr_p1)
|
---|
| 2143 | expr_pmax = max(expr_p0, expr_p1)
|
---|
[2882] | 2144 |
|
---|
[2887] | 2145 | elif is_number(expr0) and is_velocity(expr1):
|
---|
| 2146 | # 'a~b*m/s'
|
---|
[2909] | 2147 | restf = self.get_restfreqs()[molid][0]
|
---|
[2887] | 2148 | (expr_v0, expr_v1) = get_velocity_by_string(expr0, expr1)
|
---|
[2897] | 2149 | dppl = self.get_doppler()
|
---|
| 2150 | expr_f0 = get_frequency_by_velocity(restf, expr_v0, dppl)
|
---|
| 2151 | expr_f1 = get_frequency_by_velocity(restf, expr_v1, dppl)
|
---|
[2887] | 2152 | expr_p0 = coord.to_pixel(expr_f0)
|
---|
| 2153 | expr_p1 = coord.to_pixel(expr_f1)
|
---|
| 2154 | expr_pmin = min(expr_p0, expr_p1)
|
---|
| 2155 | expr_pmax = max(expr_p0, expr_p1)
|
---|
[2882] | 2156 |
|
---|
[2887] | 2157 | else:
|
---|
| 2158 | # cases such as 'aGHz~bkm/s' are not allowed now
|
---|
| 2159 | raise RuntimeError("Invalid channel selection.")
|
---|
[2882] | 2160 |
|
---|
[2887] | 2161 | cmin = max(pmin, expr_pmin)
|
---|
| 2162 | cmax = min(pmax, expr_pmax)
|
---|
| 2163 | # if the given range of channel selection has overwrap with
|
---|
| 2164 | # that of current spw, output the overwrap area.
|
---|
| 2165 | if (cmin <= cmax):
|
---|
| 2166 | cmin = float(int(cmin + 0.5))
|
---|
| 2167 | cmax = float(int(cmax + 0.5))
|
---|
| 2168 | crange_list.append([cmin, cmax])
|
---|
[2882] | 2169 |
|
---|
| 2170 | if (len(crange_list) == 0):
|
---|
| 2171 | crange_list.append([])
|
---|
| 2172 |
|
---|
[2910] | 2173 | if (len(crange_list[0]) > 0):
|
---|
| 2174 | if res.has_key(spw):
|
---|
| 2175 | res[spw].extend(crange_list)
|
---|
| 2176 | else:
|
---|
| 2177 | res[spw] = crange_list
|
---|
[2882] | 2178 |
|
---|
[2887] | 2179 | for spw in res.keys():
|
---|
[2932] | 2180 | if spw in valid_ifs:
|
---|
[2933] | 2181 | # remove duplicated channel ranges
|
---|
[2932] | 2182 | for i in reversed(xrange(len(res[spw]))):
|
---|
| 2183 | for j in xrange(i):
|
---|
[2933] | 2184 | if ((res[spw][i][0]-res[spw][j][1])*(res[spw][i][1]-res[spw][j][0]) <= 0) or \
|
---|
| 2185 | (min(abs(res[spw][i][0]-res[spw][j][1]),abs(res[spw][j][0]-res[spw][i][1])) == 1):
|
---|
[2935] | 2186 | asaplog.post()
|
---|
| 2187 | merge_warn_mesg = "Spw " + str(spw) + ": overwrapping channel ranges are merged."
|
---|
| 2188 | asaplog.push(merge_warn_mesg)
|
---|
| 2189 | asaplog.post('WARN')
|
---|
[2932] | 2190 | res[spw][j][0] = min(res[spw][i][0], res[spw][j][0])
|
---|
| 2191 | res[spw][j][1] = max(res[spw][i][1], res[spw][j][1])
|
---|
| 2192 | res[spw].pop(i)
|
---|
| 2193 | break
|
---|
| 2194 | else:
|
---|
[2887] | 2195 | del res[spw]
|
---|
| 2196 |
|
---|
| 2197 | if len(res) == 0:
|
---|
| 2198 | raise RuntimeError("No valid spw.")
|
---|
| 2199 |
|
---|
[2882] | 2200 | # restore original values
|
---|
[2892] | 2201 | self.set_unit(orig_unit)
|
---|
[2891] | 2202 | if restfreq is not None:
|
---|
[2892] | 2203 | self._setmolidcol_list(orig_molids)
|
---|
| 2204 | if frame is not None:
|
---|
| 2205 | self.set_freqframe(orig_frame)
|
---|
| 2206 | if doppler is not None:
|
---|
| 2207 | self.set_doppler(orig_doppler)
|
---|
[2882] | 2208 |
|
---|
| 2209 | return res
|
---|
[2890] | 2210 |
|
---|
[2882] | 2211 | @asaplog_post_dec
|
---|
| 2212 | def get_first_rowno_by_if(self, ifno):
|
---|
| 2213 | found = False
|
---|
| 2214 | for irow in xrange(self.nrow()):
|
---|
| 2215 | if (self.getif(irow) == ifno):
|
---|
| 2216 | res = irow
|
---|
| 2217 | found = True
|
---|
| 2218 | break
|
---|
| 2219 |
|
---|
[2926] | 2220 | if not found: raise RuntimeError("No valid spw.")
|
---|
[2882] | 2221 |
|
---|
| 2222 | return res
|
---|
| 2223 |
|
---|
| 2224 | @asaplog_post_dec
|
---|
| 2225 | def _get_coordinate_list(self):
|
---|
| 2226 | res = []
|
---|
| 2227 | spws = self.getifnos()
|
---|
| 2228 | for spw in spws:
|
---|
| 2229 | elem = {}
|
---|
| 2230 | elem['if'] = spw
|
---|
| 2231 | elem['coord'] = self.get_coordinate(self.get_first_rowno_by_if(spw))
|
---|
| 2232 | res.append(elem)
|
---|
| 2233 |
|
---|
| 2234 | return res
|
---|
| 2235 |
|
---|
| 2236 | @asaplog_post_dec
|
---|
[2349] | 2237 | def parse_maskexpr(self, maskstring):
|
---|
[2013] | 2238 | """
|
---|
| 2239 | Parse CASA type mask selection syntax (IF dependent).
|
---|
| 2240 |
|
---|
| 2241 | Parameters:
|
---|
| 2242 | maskstring : A string mask selection expression.
|
---|
| 2243 | A comma separated selections mean different IF -
|
---|
| 2244 | channel combinations. IFs and channel selections
|
---|
| 2245 | are partitioned by a colon, ':'.
|
---|
| 2246 | examples:
|
---|
[2015] | 2247 | '' = all IFs (all channels)
|
---|
[2013] | 2248 | '<2,4~6,9' = IFs 0,1,4,5,6,9 (all channels)
|
---|
| 2249 | '3:3~45;60' = channels 3 to 45 and 60 in IF 3
|
---|
| 2250 | '0~1:2~6,8' = channels 2 to 6 in IFs 0,1, and
|
---|
| 2251 | all channels in IF8
|
---|
| 2252 | Returns:
|
---|
| 2253 | A dictionary of selected (valid) IF and masklist pairs,
|
---|
| 2254 | e.g. {'0': [[50,250],[350,462]], '2': [[100,400],[550,974]]}
|
---|
| 2255 | """
|
---|
| 2256 | if not isinstance(maskstring,str):
|
---|
| 2257 | asaplog.post()
|
---|
[2611] | 2258 | asaplog.push("Mask expression should be a string.")
|
---|
[2013] | 2259 | asaplog.post("ERROR")
|
---|
| 2260 |
|
---|
| 2261 | valid_ifs = self.getifnos()
|
---|
| 2262 | frequnit = self.get_unit()
|
---|
| 2263 | seldict = {}
|
---|
[2015] | 2264 | if maskstring == "":
|
---|
| 2265 | maskstring = str(valid_ifs)[1:-1]
|
---|
[2611] | 2266 | ## split each selection "IF range[:CHAN range]"
|
---|
[2867] | 2267 | # split maskstring by "<spaces>,<spaces>"
|
---|
| 2268 | comma_sep = re.compile('\s*,\s*')
|
---|
| 2269 | sellist = comma_sep.split(maskstring)
|
---|
| 2270 | # separator by "<spaces>:<spaces>"
|
---|
| 2271 | collon_sep = re.compile('\s*:\s*')
|
---|
[2013] | 2272 | for currselstr in sellist:
|
---|
[2867] | 2273 | selset = collon_sep.split(currselstr)
|
---|
[2013] | 2274 | # spw and mask string (may include ~, < or >)
|
---|
[2349] | 2275 | spwmasklist = self._parse_selection(selset[0], typestr='integer',
|
---|
[2611] | 2276 | minval=min(valid_ifs),
|
---|
[2349] | 2277 | maxval=max(valid_ifs))
|
---|
[2013] | 2278 | for spwlist in spwmasklist:
|
---|
| 2279 | selspws = []
|
---|
| 2280 | for ispw in range(spwlist[0],spwlist[1]+1):
|
---|
| 2281 | # Put into the list only if ispw exists
|
---|
| 2282 | if valid_ifs.count(ispw):
|
---|
| 2283 | selspws.append(ispw)
|
---|
| 2284 | del spwmasklist, spwlist
|
---|
| 2285 |
|
---|
| 2286 | # parse frequency mask list
|
---|
| 2287 | if len(selset) > 1:
|
---|
[2349] | 2288 | freqmasklist = self._parse_selection(selset[1], typestr='float',
|
---|
| 2289 | offset=0.)
|
---|
[2013] | 2290 | else:
|
---|
| 2291 | # want to select the whole spectrum
|
---|
| 2292 | freqmasklist = [None]
|
---|
| 2293 |
|
---|
| 2294 | ## define a dictionary of spw - masklist combination
|
---|
| 2295 | for ispw in selspws:
|
---|
| 2296 | #print "working on", ispw
|
---|
| 2297 | spwstr = str(ispw)
|
---|
| 2298 | if len(selspws) == 0:
|
---|
| 2299 | # empty spw
|
---|
| 2300 | continue
|
---|
| 2301 | else:
|
---|
| 2302 | ## want to get min and max of the spw and
|
---|
| 2303 | ## offset to set for '<' and '>'
|
---|
| 2304 | if frequnit == 'channel':
|
---|
| 2305 | minfreq = 0
|
---|
| 2306 | maxfreq = self.nchan(ifno=ispw)
|
---|
| 2307 | offset = 0.5
|
---|
| 2308 | else:
|
---|
| 2309 | ## This is ugly part. need improvement
|
---|
| 2310 | for ifrow in xrange(self.nrow()):
|
---|
| 2311 | if self.getif(ifrow) == ispw:
|
---|
| 2312 | #print "IF",ispw,"found in row =",ifrow
|
---|
| 2313 | break
|
---|
| 2314 | freqcoord = self.get_coordinate(ifrow)
|
---|
| 2315 | freqs = self._getabcissa(ifrow)
|
---|
| 2316 | minfreq = min(freqs)
|
---|
| 2317 | maxfreq = max(freqs)
|
---|
| 2318 | if len(freqs) == 1:
|
---|
| 2319 | offset = 0.5
|
---|
| 2320 | elif frequnit.find('Hz') > 0:
|
---|
[2349] | 2321 | offset = abs(freqcoord.to_frequency(1,
|
---|
| 2322 | unit=frequnit)
|
---|
| 2323 | -freqcoord.to_frequency(0,
|
---|
| 2324 | unit=frequnit)
|
---|
| 2325 | )*0.5
|
---|
[2013] | 2326 | elif frequnit.find('m/s') > 0:
|
---|
[2349] | 2327 | offset = abs(freqcoord.to_velocity(1,
|
---|
| 2328 | unit=frequnit)
|
---|
| 2329 | -freqcoord.to_velocity(0,
|
---|
| 2330 | unit=frequnit)
|
---|
| 2331 | )*0.5
|
---|
[2013] | 2332 | else:
|
---|
| 2333 | asaplog.post()
|
---|
| 2334 | asaplog.push("Invalid frequency unit")
|
---|
| 2335 | asaplog.post("ERROR")
|
---|
| 2336 | del freqs, freqcoord, ifrow
|
---|
| 2337 | for freq in freqmasklist:
|
---|
| 2338 | selmask = freq or [minfreq, maxfreq]
|
---|
| 2339 | if selmask[0] == None:
|
---|
| 2340 | ## selection was "<freq[1]".
|
---|
| 2341 | if selmask[1] < minfreq:
|
---|
| 2342 | ## avoid adding region selection
|
---|
| 2343 | selmask = None
|
---|
| 2344 | else:
|
---|
| 2345 | selmask = [minfreq,selmask[1]-offset]
|
---|
| 2346 | elif selmask[1] == None:
|
---|
| 2347 | ## selection was ">freq[0]"
|
---|
| 2348 | if selmask[0] > maxfreq:
|
---|
| 2349 | ## avoid adding region selection
|
---|
| 2350 | selmask = None
|
---|
| 2351 | else:
|
---|
| 2352 | selmask = [selmask[0]+offset,maxfreq]
|
---|
| 2353 | if selmask:
|
---|
| 2354 | if not seldict.has_key(spwstr):
|
---|
| 2355 | # new spw selection
|
---|
| 2356 | seldict[spwstr] = []
|
---|
| 2357 | seldict[spwstr] += [selmask]
|
---|
| 2358 | del minfreq,maxfreq,offset,freq,selmask
|
---|
| 2359 | del spwstr
|
---|
| 2360 | del freqmasklist
|
---|
| 2361 | del valid_ifs
|
---|
| 2362 | if len(seldict) == 0:
|
---|
| 2363 | asaplog.post()
|
---|
[2349] | 2364 | asaplog.push("No valid selection in the mask expression: "
|
---|
| 2365 | +maskstring)
|
---|
[2013] | 2366 | asaplog.post("WARN")
|
---|
| 2367 | return None
|
---|
| 2368 | msg = "Selected masklist:\n"
|
---|
| 2369 | for sif, lmask in seldict.iteritems():
|
---|
| 2370 | msg += " IF"+sif+" - "+str(lmask)+"\n"
|
---|
| 2371 | asaplog.push(msg)
|
---|
| 2372 | return seldict
|
---|
| 2373 |
|
---|
[2611] | 2374 | @asaplog_post_dec
|
---|
| 2375 | def parse_idx_selection(self, mode, selexpr):
|
---|
| 2376 | """
|
---|
| 2377 | Parse CASA type mask selection syntax of SCANNO, IFNO, POLNO,
|
---|
| 2378 | BEAMNO, and row number
|
---|
| 2379 |
|
---|
| 2380 | Parameters:
|
---|
| 2381 | mode : which column to select.
|
---|
| 2382 | ['scan',|'if'|'pol'|'beam'|'row']
|
---|
| 2383 | selexpr : A comma separated selection expression.
|
---|
| 2384 | examples:
|
---|
| 2385 | '' = all (returns [])
|
---|
| 2386 | '<2,4~6,9' = indices less than 2, 4 to 6 and 9
|
---|
| 2387 | (returns [0,1,4,5,6,9])
|
---|
| 2388 | Returns:
|
---|
| 2389 | A List of selected indices
|
---|
| 2390 | """
|
---|
| 2391 | if selexpr == "":
|
---|
| 2392 | return []
|
---|
| 2393 | valid_modes = {'s': 'scan', 'i': 'if', 'p': 'pol',
|
---|
| 2394 | 'b': 'beam', 'r': 'row'}
|
---|
| 2395 | smode = mode.lower()[0]
|
---|
| 2396 | if not (smode in valid_modes.keys()):
|
---|
| 2397 | msg = "Invalid mode '%s'. Valid modes are %s" %\
|
---|
| 2398 | (mode, str(valid_modes.values()))
|
---|
| 2399 | asaplog.post()
|
---|
| 2400 | asaplog.push(msg)
|
---|
| 2401 | asaplog.post("ERROR")
|
---|
| 2402 | mode = valid_modes[smode]
|
---|
| 2403 | minidx = None
|
---|
| 2404 | maxidx = None
|
---|
| 2405 | if smode == 'r':
|
---|
| 2406 | minidx = 0
|
---|
[2987] | 2407 | maxidx = self.nrow()-1
|
---|
[2611] | 2408 | else:
|
---|
| 2409 | idx = getattr(self,"get"+mode+"nos")()
|
---|
| 2410 | minidx = min(idx)
|
---|
| 2411 | maxidx = max(idx)
|
---|
| 2412 | del idx
|
---|
[2867] | 2413 | # split selexpr by "<spaces>,<spaces>"
|
---|
| 2414 | comma_sep = re.compile('\s*,\s*')
|
---|
| 2415 | sellist = comma_sep.split(selexpr)
|
---|
[2611] | 2416 | idxlist = []
|
---|
| 2417 | for currselstr in sellist:
|
---|
| 2418 | # single range (may include ~, < or >)
|
---|
| 2419 | currlist = self._parse_selection(currselstr, typestr='integer',
|
---|
| 2420 | minval=minidx,maxval=maxidx)
|
---|
| 2421 | for thelist in currlist:
|
---|
| 2422 | idxlist += range(thelist[0],thelist[1]+1)
|
---|
[2932] | 2423 | # remove duplicated elements after first ones
|
---|
| 2424 | for i in reversed(xrange(len(idxlist))):
|
---|
| 2425 | if idxlist.index(idxlist[i]) < i:
|
---|
| 2426 | idxlist.pop(i)
|
---|
[2987] | 2427 |
|
---|
| 2428 | # remove elements outside range [minidx, maxidx] for smode='r'
|
---|
| 2429 | if smode == 'r':
|
---|
| 2430 | for i in reversed(xrange(len(idxlist))):
|
---|
| 2431 | if (idxlist[i] < minidx) or (idxlist[i] > maxidx):
|
---|
| 2432 | idxlist.pop(i)
|
---|
| 2433 |
|
---|
[2611] | 2434 | msg = "Selected %s: %s" % (mode.upper()+"NO", str(idxlist))
|
---|
| 2435 | asaplog.push(msg)
|
---|
| 2436 | return idxlist
|
---|
| 2437 |
|
---|
[2349] | 2438 | def _parse_selection(self, selstr, typestr='float', offset=0.,
|
---|
[2351] | 2439 | minval=None, maxval=None):
|
---|
[2013] | 2440 | """
|
---|
| 2441 | Parameters:
|
---|
| 2442 | selstr : The Selection string, e.g., '<3;5~7;100~103;9'
|
---|
| 2443 | typestr : The type of the values in returned list
|
---|
| 2444 | ('integer' or 'float')
|
---|
| 2445 | offset : The offset value to subtract from or add to
|
---|
| 2446 | the boundary value if the selection string
|
---|
[2611] | 2447 | includes '<' or '>' [Valid only for typestr='float']
|
---|
[2013] | 2448 | minval, maxval : The minimum/maximum values to set if the
|
---|
| 2449 | selection string includes '<' or '>'.
|
---|
| 2450 | The list element is filled with None by default.
|
---|
| 2451 | Returns:
|
---|
| 2452 | A list of min/max pair of selections.
|
---|
| 2453 | Example:
|
---|
[2611] | 2454 | _parse_selection('<3;5~7;9',typestr='int',minval=0)
|
---|
| 2455 | --> returns [[0,2],[5,7],[9,9]]
|
---|
| 2456 | _parse_selection('<3;5~7;9',typestr='float',offset=0.5,minval=0)
|
---|
| 2457 | --> returns [[0.,2.5],[5.0,7.0],[9.,9.]]
|
---|
[2013] | 2458 | """
|
---|
[2867] | 2459 | # split selstr by '<spaces>;<spaces>'
|
---|
| 2460 | semi_sep = re.compile('\s*;\s*')
|
---|
| 2461 | selgroups = semi_sep.split(selstr)
|
---|
[2013] | 2462 | sellists = []
|
---|
| 2463 | if typestr.lower().startswith('int'):
|
---|
| 2464 | formatfunc = int
|
---|
[2611] | 2465 | offset = 1
|
---|
[2013] | 2466 | else:
|
---|
| 2467 | formatfunc = float
|
---|
| 2468 |
|
---|
| 2469 | for currsel in selgroups:
|
---|
[2867] | 2470 | if currsel.strip() == '*' or len(currsel.strip()) == 0:
|
---|
| 2471 | minsel = minval
|
---|
| 2472 | maxsel = maxval
|
---|
[2013] | 2473 | if currsel.find('~') > 0:
|
---|
[2611] | 2474 | # val0 <= x <= val1
|
---|
[2013] | 2475 | minsel = formatfunc(currsel.split('~')[0].strip())
|
---|
[2867] | 2476 | maxsel = formatfunc(currsel.split('~')[1].strip())
|
---|
[2611] | 2477 | elif currsel.strip().find('<=') > -1:
|
---|
| 2478 | bound = currsel.split('<=')
|
---|
| 2479 | try: # try "x <= val"
|
---|
| 2480 | minsel = minval
|
---|
| 2481 | maxsel = formatfunc(bound[1].strip())
|
---|
| 2482 | except ValueError: # now "val <= x"
|
---|
| 2483 | minsel = formatfunc(bound[0].strip())
|
---|
| 2484 | maxsel = maxval
|
---|
| 2485 | elif currsel.strip().find('>=') > -1:
|
---|
| 2486 | bound = currsel.split('>=')
|
---|
| 2487 | try: # try "x >= val"
|
---|
| 2488 | minsel = formatfunc(bound[1].strip())
|
---|
| 2489 | maxsel = maxval
|
---|
| 2490 | except ValueError: # now "val >= x"
|
---|
| 2491 | minsel = minval
|
---|
| 2492 | maxsel = formatfunc(bound[0].strip())
|
---|
| 2493 | elif currsel.strip().find('<') > -1:
|
---|
| 2494 | bound = currsel.split('<')
|
---|
| 2495 | try: # try "x < val"
|
---|
| 2496 | minsel = minval
|
---|
| 2497 | maxsel = formatfunc(bound[1].strip()) \
|
---|
| 2498 | - formatfunc(offset)
|
---|
| 2499 | except ValueError: # now "val < x"
|
---|
| 2500 | minsel = formatfunc(bound[0].strip()) \
|
---|
[2013] | 2501 | + formatfunc(offset)
|
---|
[2611] | 2502 | maxsel = maxval
|
---|
| 2503 | elif currsel.strip().find('>') > -1:
|
---|
| 2504 | bound = currsel.split('>')
|
---|
| 2505 | try: # try "x > val"
|
---|
| 2506 | minsel = formatfunc(bound[1].strip()) \
|
---|
| 2507 | + formatfunc(offset)
|
---|
| 2508 | maxsel = maxval
|
---|
| 2509 | except ValueError: # now "val > x"
|
---|
| 2510 | minsel = minval
|
---|
| 2511 | maxsel = formatfunc(bound[0].strip()) \
|
---|
| 2512 | - formatfunc(offset)
|
---|
[2013] | 2513 | else:
|
---|
| 2514 | minsel = formatfunc(currsel)
|
---|
| 2515 | maxsel = formatfunc(currsel)
|
---|
| 2516 | sellists.append([minsel,maxsel])
|
---|
| 2517 | return sellists
|
---|
| 2518 |
|
---|
[1819] | 2519 | # def get_restfreqs(self):
|
---|
| 2520 | # """
|
---|
| 2521 | # Get the restfrequency(s) stored in this scantable.
|
---|
| 2522 | # The return value(s) are always of unit 'Hz'
|
---|
| 2523 | # Parameters:
|
---|
| 2524 | # none
|
---|
| 2525 | # Returns:
|
---|
| 2526 | # a list of doubles
|
---|
| 2527 | # """
|
---|
| 2528 | # return list(self._getrestfreqs())
|
---|
| 2529 |
|
---|
| 2530 | def get_restfreqs(self, ids=None):
|
---|
[1846] | 2531 | """\
|
---|
[256] | 2532 | Get the restfrequency(s) stored in this scantable.
|
---|
| 2533 | The return value(s) are always of unit 'Hz'
|
---|
[1846] | 2534 |
|
---|
[256] | 2535 | Parameters:
|
---|
[1846] | 2536 |
|
---|
[1819] | 2537 | ids: (optional) a list of MOLECULE_ID for that restfrequency(s) to
|
---|
| 2538 | be retrieved
|
---|
[1846] | 2539 |
|
---|
[256] | 2540 | Returns:
|
---|
[1846] | 2541 |
|
---|
[1819] | 2542 | dictionary containing ids and a list of doubles for each id
|
---|
[1846] | 2543 |
|
---|
[256] | 2544 | """
|
---|
[1819] | 2545 | if ids is None:
|
---|
[2349] | 2546 | rfreqs = {}
|
---|
[1819] | 2547 | idlist = self.getmolnos()
|
---|
| 2548 | for i in idlist:
|
---|
[2349] | 2549 | rfreqs[i] = list(self._getrestfreqs(i))
|
---|
[1819] | 2550 | return rfreqs
|
---|
| 2551 | else:
|
---|
[2349] | 2552 | if type(ids) == list or type(ids) == tuple:
|
---|
| 2553 | rfreqs = {}
|
---|
[1819] | 2554 | for i in ids:
|
---|
[2349] | 2555 | rfreqs[i] = list(self._getrestfreqs(i))
|
---|
[1819] | 2556 | return rfreqs
|
---|
| 2557 | else:
|
---|
| 2558 | return list(self._getrestfreqs(ids))
|
---|
[102] | 2559 |
|
---|
[2349] | 2560 | @asaplog_post_dec
|
---|
[931] | 2561 | def set_restfreqs(self, freqs=None, unit='Hz'):
|
---|
[1846] | 2562 | """\
|
---|
[931] | 2563 | Set or replace the restfrequency specified and
|
---|
[1938] | 2564 | if the 'freqs' argument holds a scalar,
|
---|
[931] | 2565 | then that rest frequency will be applied to all the selected
|
---|
| 2566 | data. If the 'freqs' argument holds
|
---|
| 2567 | a vector, then it MUST be of equal or smaller length than
|
---|
| 2568 | the number of IFs (and the available restfrequencies will be
|
---|
| 2569 | replaced by this vector). In this case, *all* data have
|
---|
| 2570 | the restfrequency set per IF according
|
---|
| 2571 | to the corresponding value you give in the 'freqs' vector.
|
---|
[1118] | 2572 | E.g. 'freqs=[1e9, 2e9]' would mean IF 0 gets restfreq 1e9 and
|
---|
[931] | 2573 | IF 1 gets restfreq 2e9.
|
---|
[1846] | 2574 |
|
---|
[1395] | 2575 | You can also specify the frequencies via a linecatalog.
|
---|
[1153] | 2576 |
|
---|
[931] | 2577 | Parameters:
|
---|
[1846] | 2578 |
|
---|
[931] | 2579 | freqs: list of rest frequency values or string idenitfiers
|
---|
[1855] | 2580 |
|
---|
[931] | 2581 | unit: unit for rest frequency (default 'Hz')
|
---|
[402] | 2582 |
|
---|
[1846] | 2583 |
|
---|
| 2584 | Example::
|
---|
| 2585 |
|
---|
[1819] | 2586 | # set the given restfrequency for the all currently selected IFs
|
---|
[931] | 2587 | scan.set_restfreqs(freqs=1.4e9)
|
---|
[1845] | 2588 | # set restfrequencies for the n IFs (n > 1) in the order of the
|
---|
| 2589 | # list, i.e
|
---|
| 2590 | # IF0 -> 1.4e9, IF1 -> 1.41e9, IF3 -> 1.42e9
|
---|
| 2591 | # len(list_of_restfreqs) == nIF
|
---|
| 2592 | # for nIF == 1 the following will set multiple restfrequency for
|
---|
| 2593 | # that IF
|
---|
[1819] | 2594 | scan.set_restfreqs(freqs=[1.4e9, 1.41e9, 1.42e9])
|
---|
[1845] | 2595 | # set multiple restfrequencies per IF. as a list of lists where
|
---|
| 2596 | # the outer list has nIF elements, the inner s arbitrary
|
---|
| 2597 | scan.set_restfreqs(freqs=[[1.4e9, 1.41e9], [1.67e9]])
|
---|
[391] | 2598 |
|
---|
[1846] | 2599 | *Note*:
|
---|
[1845] | 2600 |
|
---|
[931] | 2601 | To do more sophisticate Restfrequency setting, e.g. on a
|
---|
| 2602 | source and IF basis, use scantable.set_selection() before using
|
---|
[1846] | 2603 | this function::
|
---|
[931] | 2604 |
|
---|
[1846] | 2605 | # provided your scantable is called scan
|
---|
| 2606 | selection = selector()
|
---|
[2431] | 2607 | selection.set_name('ORION*')
|
---|
[1846] | 2608 | selection.set_ifs([1])
|
---|
| 2609 | scan.set_selection(selection)
|
---|
| 2610 | scan.set_restfreqs(freqs=86.6e9)
|
---|
| 2611 |
|
---|
[931] | 2612 | """
|
---|
| 2613 | varlist = vars()
|
---|
[1157] | 2614 | from asap import linecatalog
|
---|
| 2615 | # simple value
|
---|
[1118] | 2616 | if isinstance(freqs, int) or isinstance(freqs, float):
|
---|
[1845] | 2617 | self._setrestfreqs([freqs], [""], unit)
|
---|
[1157] | 2618 | # list of values
|
---|
[1118] | 2619 | elif isinstance(freqs, list) or isinstance(freqs, tuple):
|
---|
[1157] | 2620 | # list values are scalars
|
---|
[1118] | 2621 | if isinstance(freqs[-1], int) or isinstance(freqs[-1], float):
|
---|
[1845] | 2622 | if len(freqs) == 1:
|
---|
| 2623 | self._setrestfreqs(freqs, [""], unit)
|
---|
| 2624 | else:
|
---|
| 2625 | # allow the 'old' mode of setting mulitple IFs
|
---|
| 2626 | savesel = self._getselection()
|
---|
[2599] | 2627 | sel = self.get_selection()
|
---|
[1845] | 2628 | iflist = self.getifnos()
|
---|
| 2629 | if len(freqs)>len(iflist):
|
---|
| 2630 | raise ValueError("number of elements in list of list "
|
---|
| 2631 | "exeeds the current IF selections")
|
---|
| 2632 | iflist = self.getifnos()
|
---|
| 2633 | for i, fval in enumerate(freqs):
|
---|
| 2634 | sel.set_ifs(iflist[i])
|
---|
| 2635 | self._setselection(sel)
|
---|
| 2636 | self._setrestfreqs([fval], [""], unit)
|
---|
| 2637 | self._setselection(savesel)
|
---|
| 2638 |
|
---|
| 2639 | # list values are dict, {'value'=, 'name'=)
|
---|
[1157] | 2640 | elif isinstance(freqs[-1], dict):
|
---|
[1845] | 2641 | values = []
|
---|
| 2642 | names = []
|
---|
| 2643 | for d in freqs:
|
---|
| 2644 | values.append(d["value"])
|
---|
| 2645 | names.append(d["name"])
|
---|
| 2646 | self._setrestfreqs(values, names, unit)
|
---|
[1819] | 2647 | elif isinstance(freqs[-1], list) or isinstance(freqs[-1], tuple):
|
---|
[1157] | 2648 | savesel = self._getselection()
|
---|
[2599] | 2649 | sel = self.get_selection()
|
---|
[1322] | 2650 | iflist = self.getifnos()
|
---|
[1819] | 2651 | if len(freqs)>len(iflist):
|
---|
[1845] | 2652 | raise ValueError("number of elements in list of list exeeds"
|
---|
| 2653 | " the current IF selections")
|
---|
| 2654 | for i, fval in enumerate(freqs):
|
---|
[1322] | 2655 | sel.set_ifs(iflist[i])
|
---|
[1259] | 2656 | self._setselection(sel)
|
---|
[1845] | 2657 | self._setrestfreqs(fval, [""], unit)
|
---|
[1157] | 2658 | self._setselection(savesel)
|
---|
| 2659 | # freqs are to be taken from a linecatalog
|
---|
[1153] | 2660 | elif isinstance(freqs, linecatalog):
|
---|
| 2661 | savesel = self._getselection()
|
---|
[2599] | 2662 | sel = self.get_selection()
|
---|
[1153] | 2663 | for i in xrange(freqs.nrow()):
|
---|
[1322] | 2664 | sel.set_ifs(iflist[i])
|
---|
[1153] | 2665 | self._setselection(sel)
|
---|
[1845] | 2666 | self._setrestfreqs([freqs.get_frequency(i)],
|
---|
| 2667 | [freqs.get_name(i)], "MHz")
|
---|
[1153] | 2668 | # ensure that we are not iterating past nIF
|
---|
| 2669 | if i == self.nif()-1: break
|
---|
| 2670 | self._setselection(savesel)
|
---|
[931] | 2671 | else:
|
---|
| 2672 | return
|
---|
| 2673 | self._add_history("set_restfreqs", varlist)
|
---|
| 2674 |
|
---|
[2349] | 2675 | @asaplog_post_dec
|
---|
[1360] | 2676 | def shift_refpix(self, delta):
|
---|
[1846] | 2677 | """\
|
---|
[1589] | 2678 | Shift the reference pixel of the Spectra Coordinate by an
|
---|
| 2679 | integer amount.
|
---|
[1846] | 2680 |
|
---|
[1589] | 2681 | Parameters:
|
---|
[1846] | 2682 |
|
---|
[1589] | 2683 | delta: the amount to shift by
|
---|
[1846] | 2684 |
|
---|
| 2685 | *Note*:
|
---|
| 2686 |
|
---|
[1589] | 2687 | Be careful using this with broadband data.
|
---|
[1846] | 2688 |
|
---|
[1360] | 2689 | """
|
---|
[2349] | 2690 | varlist = vars()
|
---|
[1731] | 2691 | Scantable.shift_refpix(self, delta)
|
---|
[2349] | 2692 | s._add_history("shift_refpix", varlist)
|
---|
[931] | 2693 |
|
---|
[1862] | 2694 | @asaplog_post_dec
|
---|
[2820] | 2695 | def history(self, filename=None, nrows=-1, start=0):
|
---|
[1846] | 2696 | """\
|
---|
[1259] | 2697 | Print the history. Optionally to a file.
|
---|
[1846] | 2698 |
|
---|
[1348] | 2699 | Parameters:
|
---|
[1846] | 2700 |
|
---|
[1928] | 2701 | filename: The name of the file to save the history to.
|
---|
[1846] | 2702 |
|
---|
[1259] | 2703 | """
|
---|
[2820] | 2704 | n = self._historylength()
|
---|
| 2705 | if nrows == -1:
|
---|
| 2706 | nrows = n
|
---|
| 2707 | if start+nrows > n:
|
---|
| 2708 | nrows = nrows-start
|
---|
| 2709 | if n > 1000 and nrows == n:
|
---|
| 2710 | nrows = 1000
|
---|
| 2711 | start = n-1000
|
---|
| 2712 | asaplog.push("Warning: History has {0} entries. Displaying last "
|
---|
| 2713 | "1000".format(n))
|
---|
| 2714 | hist = list(self._gethistory(nrows, start))
|
---|
[794] | 2715 | out = "-"*80
|
---|
[484] | 2716 | for h in hist:
|
---|
[2820] | 2717 | if not h.strip():
|
---|
| 2718 | continue
|
---|
| 2719 | if h.find("---") >-1:
|
---|
| 2720 | continue
|
---|
[489] | 2721 | else:
|
---|
| 2722 | items = h.split("##")
|
---|
| 2723 | date = items[0]
|
---|
| 2724 | func = items[1]
|
---|
| 2725 | items = items[2:]
|
---|
[794] | 2726 | out += "\n"+date+"\n"
|
---|
| 2727 | out += "Function: %s\n Parameters:" % (func)
|
---|
[489] | 2728 | for i in items:
|
---|
[1938] | 2729 | if i == '':
|
---|
| 2730 | continue
|
---|
[489] | 2731 | s = i.split("=")
|
---|
[1118] | 2732 | out += "\n %s = %s" % (s[0], s[1])
|
---|
[2820] | 2733 | out = "\n".join([out, "*"*80])
|
---|
[1259] | 2734 | if filename is not None:
|
---|
| 2735 | if filename is "":
|
---|
| 2736 | filename = 'scantable_history.txt'
|
---|
| 2737 | filename = os.path.expandvars(os.path.expanduser(filename))
|
---|
| 2738 | if not os.path.isdir(filename):
|
---|
| 2739 | data = open(filename, 'w')
|
---|
| 2740 | data.write(out)
|
---|
| 2741 | data.close()
|
---|
| 2742 | else:
|
---|
| 2743 | msg = "Illegal file name '%s'." % (filename)
|
---|
[1859] | 2744 | raise IOError(msg)
|
---|
| 2745 | return page(out)
|
---|
[2349] | 2746 |
|
---|
[513] | 2747 | #
|
---|
| 2748 | # Maths business
|
---|
| 2749 | #
|
---|
[1862] | 2750 | @asaplog_post_dec
|
---|
[2818] | 2751 | def average_time(self, mask=None, scanav=False, weight='tint', align=False,
|
---|
| 2752 | avmode="NONE"):
|
---|
[1846] | 2753 | """\
|
---|
[2349] | 2754 | Return the (time) weighted average of a scan. Scans will be averaged
|
---|
| 2755 | only if the source direction (RA/DEC) is within 1' otherwise
|
---|
[1846] | 2756 |
|
---|
| 2757 | *Note*:
|
---|
| 2758 |
|
---|
[1070] | 2759 | in channels only - align if necessary
|
---|
[1846] | 2760 |
|
---|
[513] | 2761 | Parameters:
|
---|
[1846] | 2762 |
|
---|
[513] | 2763 | mask: an optional mask (only used for 'var' and 'tsys'
|
---|
| 2764 | weighting)
|
---|
[1855] | 2765 |
|
---|
[558] | 2766 | scanav: True averages each scan separately
|
---|
| 2767 | False (default) averages all scans together,
|
---|
[1855] | 2768 |
|
---|
[1099] | 2769 | weight: Weighting scheme.
|
---|
| 2770 | 'none' (mean no weight)
|
---|
| 2771 | 'var' (1/var(spec) weighted)
|
---|
| 2772 | 'tsys' (1/Tsys**2 weighted)
|
---|
| 2773 | 'tint' (integration time weighted)
|
---|
| 2774 | 'tintsys' (Tint/Tsys**2)
|
---|
| 2775 | 'median' ( median averaging)
|
---|
[535] | 2776 | The default is 'tint'
|
---|
[1855] | 2777 |
|
---|
[931] | 2778 | align: align the spectra in velocity before averaging. It takes
|
---|
| 2779 | the time of the first spectrum as reference time.
|
---|
[2818] | 2780 | avmode: 'SOURCE' - also select by source name - or
|
---|
| 2781 | 'NONE' (default). Not applicable for scanav=True or
|
---|
| 2782 | weight=median
|
---|
[1846] | 2783 |
|
---|
| 2784 | Example::
|
---|
| 2785 |
|
---|
[513] | 2786 | # time average the scantable without using a mask
|
---|
[710] | 2787 | newscan = scan.average_time()
|
---|
[1846] | 2788 |
|
---|
[513] | 2789 | """
|
---|
| 2790 | varlist = vars()
|
---|
[1593] | 2791 | weight = weight or 'TINT'
|
---|
| 2792 | mask = mask or ()
|
---|
[2818] | 2793 | scanav = (scanav and 'SCAN') or avmode.upper()
|
---|
[1118] | 2794 | scan = (self, )
|
---|
[1859] | 2795 |
|
---|
| 2796 | if align:
|
---|
| 2797 | scan = (self.freq_align(insitu=False), )
|
---|
[2818] | 2798 | asaplog.push("Note: Alignment is don on a source-by-source basis")
|
---|
| 2799 | asaplog.push("Note: Averaging (by default) is not")
|
---|
| 2800 | # we need to set it to SOURCE averaging here
|
---|
[1859] | 2801 | s = None
|
---|
| 2802 | if weight.upper() == 'MEDIAN':
|
---|
| 2803 | s = scantable(self._math._averagechannel(scan[0], 'MEDIAN',
|
---|
| 2804 | scanav))
|
---|
| 2805 | else:
|
---|
| 2806 | s = scantable(self._math._average(scan, mask, weight.upper(),
|
---|
| 2807 | scanav))
|
---|
[1099] | 2808 | s._add_history("average_time", varlist)
|
---|
[513] | 2809 | return s
|
---|
[710] | 2810 |
|
---|
[1862] | 2811 | @asaplog_post_dec
|
---|
[876] | 2812 | def convert_flux(self, jyperk=None, eta=None, d=None, insitu=None):
|
---|
[1846] | 2813 | """\
|
---|
[513] | 2814 | Return a scan where all spectra are converted to either
|
---|
| 2815 | Jansky or Kelvin depending upon the flux units of the scan table.
|
---|
| 2816 | By default the function tries to look the values up internally.
|
---|
| 2817 | If it can't find them (or if you want to over-ride), you must
|
---|
| 2818 | specify EITHER jyperk OR eta (and D which it will try to look up
|
---|
| 2819 | also if you don't set it). jyperk takes precedence if you set both.
|
---|
[1846] | 2820 |
|
---|
[513] | 2821 | Parameters:
|
---|
[1846] | 2822 |
|
---|
[513] | 2823 | jyperk: the Jy / K conversion factor
|
---|
[1855] | 2824 |
|
---|
[513] | 2825 | eta: the aperture efficiency
|
---|
[1855] | 2826 |
|
---|
[1928] | 2827 | d: the geometric diameter (metres)
|
---|
[1855] | 2828 |
|
---|
[513] | 2829 | insitu: if False a new scantable is returned.
|
---|
| 2830 | Otherwise, the scaling is done in-situ
|
---|
| 2831 | The default is taken from .asaprc (False)
|
---|
[1846] | 2832 |
|
---|
[513] | 2833 | """
|
---|
| 2834 | if insitu is None: insitu = rcParams['insitu']
|
---|
[876] | 2835 | self._math._setinsitu(insitu)
|
---|
[513] | 2836 | varlist = vars()
|
---|
[1593] | 2837 | jyperk = jyperk or -1.0
|
---|
| 2838 | d = d or -1.0
|
---|
| 2839 | eta = eta or -1.0
|
---|
[876] | 2840 | s = scantable(self._math._convertflux(self, d, eta, jyperk))
|
---|
| 2841 | s._add_history("convert_flux", varlist)
|
---|
| 2842 | if insitu: self._assign(s)
|
---|
| 2843 | else: return s
|
---|
[513] | 2844 |
|
---|
[1862] | 2845 | @asaplog_post_dec
|
---|
[876] | 2846 | def gain_el(self, poly=None, filename="", method="linear", insitu=None):
|
---|
[1846] | 2847 | """\
|
---|
[513] | 2848 | Return a scan after applying a gain-elevation correction.
|
---|
| 2849 | The correction can be made via either a polynomial or a
|
---|
| 2850 | table-based interpolation (and extrapolation if necessary).
|
---|
| 2851 | You specify polynomial coefficients, an ascii table or neither.
|
---|
| 2852 | If you specify neither, then a polynomial correction will be made
|
---|
| 2853 | with built in coefficients known for certain telescopes (an error
|
---|
| 2854 | will occur if the instrument is not known).
|
---|
| 2855 | The data and Tsys are *divided* by the scaling factors.
|
---|
[1846] | 2856 |
|
---|
[513] | 2857 | Parameters:
|
---|
[1846] | 2858 |
|
---|
[513] | 2859 | poly: Polynomial coefficients (default None) to compute a
|
---|
| 2860 | gain-elevation correction as a function of
|
---|
| 2861 | elevation (in degrees).
|
---|
[1855] | 2862 |
|
---|
[513] | 2863 | filename: The name of an ascii file holding correction factors.
|
---|
| 2864 | The first row of the ascii file must give the column
|
---|
| 2865 | names and these MUST include columns
|
---|
[2431] | 2866 | 'ELEVATION' (degrees) and 'FACTOR' (multiply data
|
---|
[513] | 2867 | by this) somewhere.
|
---|
| 2868 | The second row must give the data type of the
|
---|
| 2869 | column. Use 'R' for Real and 'I' for Integer.
|
---|
| 2870 | An example file would be
|
---|
| 2871 | (actual factors are arbitrary) :
|
---|
| 2872 |
|
---|
| 2873 | TIME ELEVATION FACTOR
|
---|
| 2874 | R R R
|
---|
| 2875 | 0.1 0 0.8
|
---|
| 2876 | 0.2 20 0.85
|
---|
| 2877 | 0.3 40 0.9
|
---|
| 2878 | 0.4 60 0.85
|
---|
| 2879 | 0.5 80 0.8
|
---|
| 2880 | 0.6 90 0.75
|
---|
[1855] | 2881 |
|
---|
[513] | 2882 | method: Interpolation method when correcting from a table.
|
---|
[2431] | 2883 | Values are 'nearest', 'linear' (default), 'cubic'
|
---|
| 2884 | and 'spline'
|
---|
[1855] | 2885 |
|
---|
[513] | 2886 | insitu: if False a new scantable is returned.
|
---|
| 2887 | Otherwise, the scaling is done in-situ
|
---|
| 2888 | The default is taken from .asaprc (False)
|
---|
[1846] | 2889 |
|
---|
[513] | 2890 | """
|
---|
| 2891 |
|
---|
| 2892 | if insitu is None: insitu = rcParams['insitu']
|
---|
[876] | 2893 | self._math._setinsitu(insitu)
|
---|
[513] | 2894 | varlist = vars()
|
---|
[1593] | 2895 | poly = poly or ()
|
---|
[513] | 2896 | from os.path import expandvars
|
---|
| 2897 | filename = expandvars(filename)
|
---|
[876] | 2898 | s = scantable(self._math._gainel(self, poly, filename, method))
|
---|
| 2899 | s._add_history("gain_el", varlist)
|
---|
[1593] | 2900 | if insitu:
|
---|
| 2901 | self._assign(s)
|
---|
| 2902 | else:
|
---|
| 2903 | return s
|
---|
[710] | 2904 |
|
---|
[1862] | 2905 | @asaplog_post_dec
|
---|
[931] | 2906 | def freq_align(self, reftime=None, method='cubic', insitu=None):
|
---|
[1846] | 2907 | """\
|
---|
[513] | 2908 | Return a scan where all rows have been aligned in frequency/velocity.
|
---|
| 2909 | The alignment frequency frame (e.g. LSRK) is that set by function
|
---|
| 2910 | set_freqframe.
|
---|
[1846] | 2911 |
|
---|
[513] | 2912 | Parameters:
|
---|
[1855] | 2913 |
|
---|
[513] | 2914 | reftime: reference time to align at. By default, the time of
|
---|
| 2915 | the first row of data is used.
|
---|
[1855] | 2916 |
|
---|
[513] | 2917 | method: Interpolation method for regridding the spectra.
|
---|
[2431] | 2918 | Choose from 'nearest', 'linear', 'cubic' (default)
|
---|
| 2919 | and 'spline'
|
---|
[1855] | 2920 |
|
---|
[513] | 2921 | insitu: if False a new scantable is returned.
|
---|
| 2922 | Otherwise, the scaling is done in-situ
|
---|
| 2923 | The default is taken from .asaprc (False)
|
---|
[1846] | 2924 |
|
---|
[513] | 2925 | """
|
---|
[931] | 2926 | if insitu is None: insitu = rcParams["insitu"]
|
---|
[2429] | 2927 | oldInsitu = self._math._insitu()
|
---|
[876] | 2928 | self._math._setinsitu(insitu)
|
---|
[513] | 2929 | varlist = vars()
|
---|
[1593] | 2930 | reftime = reftime or ""
|
---|
[931] | 2931 | s = scantable(self._math._freq_align(self, reftime, method))
|
---|
[876] | 2932 | s._add_history("freq_align", varlist)
|
---|
[2429] | 2933 | self._math._setinsitu(oldInsitu)
|
---|
[2349] | 2934 | if insitu:
|
---|
| 2935 | self._assign(s)
|
---|
| 2936 | else:
|
---|
| 2937 | return s
|
---|
[513] | 2938 |
|
---|
[1862] | 2939 | @asaplog_post_dec
|
---|
[1725] | 2940 | def opacity(self, tau=None, insitu=None):
|
---|
[1846] | 2941 | """\
|
---|
[513] | 2942 | Apply an opacity correction. The data
|
---|
| 2943 | and Tsys are multiplied by the correction factor.
|
---|
[1846] | 2944 |
|
---|
[513] | 2945 | Parameters:
|
---|
[1855] | 2946 |
|
---|
[1689] | 2947 | tau: (list of) opacity from which the correction factor is
|
---|
[513] | 2948 | exp(tau*ZD)
|
---|
[1689] | 2949 | where ZD is the zenith-distance.
|
---|
| 2950 | If a list is provided, it has to be of length nIF,
|
---|
| 2951 | nIF*nPol or 1 and in order of IF/POL, e.g.
|
---|
| 2952 | [opif0pol0, opif0pol1, opif1pol0 ...]
|
---|
[1725] | 2953 | if tau is `None` the opacities are determined from a
|
---|
| 2954 | model.
|
---|
[1855] | 2955 |
|
---|
[513] | 2956 | insitu: if False a new scantable is returned.
|
---|
| 2957 | Otherwise, the scaling is done in-situ
|
---|
| 2958 | The default is taken from .asaprc (False)
|
---|
[1846] | 2959 |
|
---|
[513] | 2960 | """
|
---|
[2349] | 2961 | if insitu is None:
|
---|
| 2962 | insitu = rcParams['insitu']
|
---|
[876] | 2963 | self._math._setinsitu(insitu)
|
---|
[513] | 2964 | varlist = vars()
|
---|
[1689] | 2965 | if not hasattr(tau, "__len__"):
|
---|
| 2966 | tau = [tau]
|
---|
[876] | 2967 | s = scantable(self._math._opacity(self, tau))
|
---|
| 2968 | s._add_history("opacity", varlist)
|
---|
[2349] | 2969 | if insitu:
|
---|
| 2970 | self._assign(s)
|
---|
| 2971 | else:
|
---|
| 2972 | return s
|
---|
[513] | 2973 |
|
---|
[1862] | 2974 | @asaplog_post_dec
|
---|
[513] | 2975 | def bin(self, width=5, insitu=None):
|
---|
[1846] | 2976 | """\
|
---|
[513] | 2977 | Return a scan where all spectra have been binned up.
|
---|
[1846] | 2978 |
|
---|
[1348] | 2979 | Parameters:
|
---|
[1846] | 2980 |
|
---|
[513] | 2981 | width: The bin width (default=5) in pixels
|
---|
[1855] | 2982 |
|
---|
[513] | 2983 | insitu: if False a new scantable is returned.
|
---|
| 2984 | Otherwise, the scaling is done in-situ
|
---|
| 2985 | The default is taken from .asaprc (False)
|
---|
[1846] | 2986 |
|
---|
[513] | 2987 | """
|
---|
[2349] | 2988 | if insitu is None:
|
---|
| 2989 | insitu = rcParams['insitu']
|
---|
[876] | 2990 | self._math._setinsitu(insitu)
|
---|
[513] | 2991 | varlist = vars()
|
---|
[876] | 2992 | s = scantable(self._math._bin(self, width))
|
---|
[1118] | 2993 | s._add_history("bin", varlist)
|
---|
[1589] | 2994 | if insitu:
|
---|
| 2995 | self._assign(s)
|
---|
| 2996 | else:
|
---|
| 2997 | return s
|
---|
[513] | 2998 |
|
---|
[1862] | 2999 | @asaplog_post_dec
|
---|
[2672] | 3000 | def reshape(self, first, last, insitu=None):
|
---|
| 3001 | """Resize the band by providing first and last channel.
|
---|
| 3002 | This will cut off all channels outside [first, last].
|
---|
| 3003 | """
|
---|
| 3004 | if insitu is None:
|
---|
| 3005 | insitu = rcParams['insitu']
|
---|
| 3006 | varlist = vars()
|
---|
| 3007 | if last < 0:
|
---|
| 3008 | last = self.nchan()-1 + last
|
---|
| 3009 | s = None
|
---|
| 3010 | if insitu:
|
---|
| 3011 | s = self
|
---|
| 3012 | else:
|
---|
| 3013 | s = self.copy()
|
---|
| 3014 | s._reshape(first,last)
|
---|
| 3015 | s._add_history("reshape", varlist)
|
---|
| 3016 | if not insitu:
|
---|
| 3017 | return s
|
---|
| 3018 |
|
---|
| 3019 | @asaplog_post_dec
|
---|
[513] | 3020 | def resample(self, width=5, method='cubic', insitu=None):
|
---|
[1846] | 3021 | """\
|
---|
[1348] | 3022 | Return a scan where all spectra have been binned up.
|
---|
[1573] | 3023 |
|
---|
[1348] | 3024 | Parameters:
|
---|
[1846] | 3025 |
|
---|
[513] | 3026 | width: The bin width (default=5) in pixels
|
---|
[1855] | 3027 |
|
---|
[513] | 3028 | method: Interpolation method when correcting from a table.
|
---|
[2431] | 3029 | Values are 'nearest', 'linear', 'cubic' (default)
|
---|
| 3030 | and 'spline'
|
---|
[1855] | 3031 |
|
---|
[513] | 3032 | insitu: if False a new scantable is returned.
|
---|
| 3033 | Otherwise, the scaling is done in-situ
|
---|
| 3034 | The default is taken from .asaprc (False)
|
---|
[1846] | 3035 |
|
---|
[513] | 3036 | """
|
---|
[2349] | 3037 | if insitu is None:
|
---|
| 3038 | insitu = rcParams['insitu']
|
---|
[876] | 3039 | self._math._setinsitu(insitu)
|
---|
[513] | 3040 | varlist = vars()
|
---|
[876] | 3041 | s = scantable(self._math._resample(self, method, width))
|
---|
[1118] | 3042 | s._add_history("resample", varlist)
|
---|
[2349] | 3043 | if insitu:
|
---|
| 3044 | self._assign(s)
|
---|
| 3045 | else:
|
---|
| 3046 | return s
|
---|
[513] | 3047 |
|
---|
[1862] | 3048 | @asaplog_post_dec
|
---|
[946] | 3049 | def average_pol(self, mask=None, weight='none'):
|
---|
[1846] | 3050 | """\
|
---|
[946] | 3051 | Average the Polarisations together.
|
---|
[1846] | 3052 |
|
---|
[946] | 3053 | Parameters:
|
---|
[1846] | 3054 |
|
---|
[946] | 3055 | mask: An optional mask defining the region, where the
|
---|
| 3056 | averaging will be applied. The output will have all
|
---|
| 3057 | specified points masked.
|
---|
[1855] | 3058 |
|
---|
[946] | 3059 | weight: Weighting scheme. 'none' (default), 'var' (1/var(spec)
|
---|
| 3060 | weighted), or 'tsys' (1/Tsys**2 weighted)
|
---|
[1846] | 3061 |
|
---|
[946] | 3062 | """
|
---|
| 3063 | varlist = vars()
|
---|
[1593] | 3064 | mask = mask or ()
|
---|
[1010] | 3065 | s = scantable(self._math._averagepol(self, mask, weight.upper()))
|
---|
[1118] | 3066 | s._add_history("average_pol", varlist)
|
---|
[992] | 3067 | return s
|
---|
[513] | 3068 |
|
---|
[1862] | 3069 | @asaplog_post_dec
|
---|
[1145] | 3070 | def average_beam(self, mask=None, weight='none'):
|
---|
[1846] | 3071 | """\
|
---|
[1145] | 3072 | Average the Beams together.
|
---|
[1846] | 3073 |
|
---|
[1145] | 3074 | Parameters:
|
---|
| 3075 | mask: An optional mask defining the region, where the
|
---|
| 3076 | averaging will be applied. The output will have all
|
---|
| 3077 | specified points masked.
|
---|
[1855] | 3078 |
|
---|
[1145] | 3079 | weight: Weighting scheme. 'none' (default), 'var' (1/var(spec)
|
---|
| 3080 | weighted), or 'tsys' (1/Tsys**2 weighted)
|
---|
[1846] | 3081 |
|
---|
[1145] | 3082 | """
|
---|
| 3083 | varlist = vars()
|
---|
[1593] | 3084 | mask = mask or ()
|
---|
[1145] | 3085 | s = scantable(self._math._averagebeams(self, mask, weight.upper()))
|
---|
| 3086 | s._add_history("average_beam", varlist)
|
---|
| 3087 | return s
|
---|
| 3088 |
|
---|
[1586] | 3089 | def parallactify(self, pflag):
|
---|
[1846] | 3090 | """\
|
---|
[1843] | 3091 | Set a flag to indicate whether this data should be treated as having
|
---|
[1617] | 3092 | been 'parallactified' (total phase == 0.0)
|
---|
[1846] | 3093 |
|
---|
[1617] | 3094 | Parameters:
|
---|
[1855] | 3095 |
|
---|
[1843] | 3096 | pflag: Bool indicating whether to turn this on (True) or
|
---|
[1617] | 3097 | off (False)
|
---|
[1846] | 3098 |
|
---|
[1617] | 3099 | """
|
---|
[1586] | 3100 | varlist = vars()
|
---|
| 3101 | self._parallactify(pflag)
|
---|
| 3102 | self._add_history("parallactify", varlist)
|
---|
| 3103 |
|
---|
[1862] | 3104 | @asaplog_post_dec
|
---|
[992] | 3105 | def convert_pol(self, poltype=None):
|
---|
[1846] | 3106 | """\
|
---|
[992] | 3107 | Convert the data to a different polarisation type.
|
---|
[1565] | 3108 | Note that you will need cross-polarisation terms for most conversions.
|
---|
[1846] | 3109 |
|
---|
[992] | 3110 | Parameters:
|
---|
[1855] | 3111 |
|
---|
[992] | 3112 | poltype: The new polarisation type. Valid types are:
|
---|
[2431] | 3113 | 'linear', 'circular', 'stokes' and 'linpol'
|
---|
[1846] | 3114 |
|
---|
[992] | 3115 | """
|
---|
| 3116 | varlist = vars()
|
---|
[1859] | 3117 | s = scantable(self._math._convertpol(self, poltype))
|
---|
[1118] | 3118 | s._add_history("convert_pol", varlist)
|
---|
[992] | 3119 | return s
|
---|
| 3120 |
|
---|
[1862] | 3121 | @asaplog_post_dec
|
---|
[2269] | 3122 | def smooth(self, kernel="hanning", width=5.0, order=2, plot=False,
|
---|
| 3123 | insitu=None):
|
---|
[1846] | 3124 | """\
|
---|
[513] | 3125 | Smooth the spectrum by the specified kernel (conserving flux).
|
---|
[1846] | 3126 |
|
---|
[513] | 3127 | Parameters:
|
---|
[1846] | 3128 |
|
---|
[513] | 3129 | kernel: The type of smoothing kernel. Select from
|
---|
[1574] | 3130 | 'hanning' (default), 'gaussian', 'boxcar', 'rmedian'
|
---|
| 3131 | or 'poly'
|
---|
[1855] | 3132 |
|
---|
[513] | 3133 | width: The width of the kernel in pixels. For hanning this is
|
---|
| 3134 | ignored otherwise it defauls to 5 pixels.
|
---|
| 3135 | For 'gaussian' it is the Full Width Half
|
---|
| 3136 | Maximum. For 'boxcar' it is the full width.
|
---|
[1574] | 3137 | For 'rmedian' and 'poly' it is the half width.
|
---|
[1855] | 3138 |
|
---|
[1574] | 3139 | order: Optional parameter for 'poly' kernel (default is 2), to
|
---|
| 3140 | specify the order of the polnomial. Ignored by all other
|
---|
| 3141 | kernels.
|
---|
[1855] | 3142 |
|
---|
[1819] | 3143 | plot: plot the original and the smoothed spectra.
|
---|
| 3144 | In this each indivual fit has to be approved, by
|
---|
| 3145 | typing 'y' or 'n'
|
---|
[1855] | 3146 |
|
---|
[513] | 3147 | insitu: if False a new scantable is returned.
|
---|
| 3148 | Otherwise, the scaling is done in-situ
|
---|
| 3149 | The default is taken from .asaprc (False)
|
---|
[1846] | 3150 |
|
---|
[513] | 3151 | """
|
---|
| 3152 | if insitu is None: insitu = rcParams['insitu']
|
---|
[876] | 3153 | self._math._setinsitu(insitu)
|
---|
[513] | 3154 | varlist = vars()
|
---|
[1819] | 3155 |
|
---|
| 3156 | if plot: orgscan = self.copy()
|
---|
| 3157 |
|
---|
[1574] | 3158 | s = scantable(self._math._smooth(self, kernel.lower(), width, order))
|
---|
[876] | 3159 | s._add_history("smooth", varlist)
|
---|
[1819] | 3160 |
|
---|
[2610] | 3161 | action = 'H'
|
---|
[1819] | 3162 | if plot:
|
---|
[2150] | 3163 | from asap.asapplotter import new_asaplot
|
---|
| 3164 | theplot = new_asaplot(rcParams['plotter.gui'])
|
---|
[2535] | 3165 | from matplotlib import rc as rcp
|
---|
| 3166 | rcp('lines', linewidth=1)
|
---|
[2150] | 3167 | theplot.set_panels()
|
---|
[1819] | 3168 | ylab=s._get_ordinate_label()
|
---|
[2150] | 3169 | #theplot.palette(0,["#777777","red"])
|
---|
[1819] | 3170 | for r in xrange(s.nrow()):
|
---|
| 3171 | xsm=s._getabcissa(r)
|
---|
| 3172 | ysm=s._getspectrum(r)
|
---|
| 3173 | xorg=orgscan._getabcissa(r)
|
---|
| 3174 | yorg=orgscan._getspectrum(r)
|
---|
[2610] | 3175 | if action != "N": #skip plotting if rejecting all
|
---|
| 3176 | theplot.clear()
|
---|
| 3177 | theplot.hold()
|
---|
| 3178 | theplot.set_axes('ylabel',ylab)
|
---|
| 3179 | theplot.set_axes('xlabel',s._getabcissalabel(r))
|
---|
| 3180 | theplot.set_axes('title',s._getsourcename(r))
|
---|
| 3181 | theplot.set_line(label='Original',color="#777777")
|
---|
| 3182 | theplot.plot(xorg,yorg)
|
---|
| 3183 | theplot.set_line(label='Smoothed',color="red")
|
---|
| 3184 | theplot.plot(xsm,ysm)
|
---|
| 3185 | ### Ugly part for legend
|
---|
| 3186 | for i in [0,1]:
|
---|
| 3187 | theplot.subplots[0]['lines'].append(
|
---|
| 3188 | [theplot.subplots[0]['axes'].lines[i]]
|
---|
| 3189 | )
|
---|
| 3190 | theplot.release()
|
---|
| 3191 | ### Ugly part for legend
|
---|
| 3192 | theplot.subplots[0]['lines']=[]
|
---|
| 3193 | res = self._get_verify_action("Accept smoothing?",action)
|
---|
| 3194 | #print "IF%d, POL%d: got result = %s" %(s.getif(r),s.getpol(r),res)
|
---|
| 3195 | if r == 0: action = None
|
---|
| 3196 | #res = raw_input("Accept smoothing ([y]/n): ")
|
---|
[1819] | 3197 | if res.upper() == 'N':
|
---|
[2610] | 3198 | # reject for the current rows
|
---|
[1819] | 3199 | s._setspectrum(yorg, r)
|
---|
[2610] | 3200 | elif res.upper() == 'R':
|
---|
| 3201 | # reject all the following rows
|
---|
| 3202 | action = "N"
|
---|
| 3203 | s._setspectrum(yorg, r)
|
---|
| 3204 | elif res.upper() == 'A':
|
---|
| 3205 | # accept all the following rows
|
---|
| 3206 | break
|
---|
[2150] | 3207 | theplot.quit()
|
---|
| 3208 | del theplot
|
---|
[1819] | 3209 | del orgscan
|
---|
| 3210 |
|
---|
[876] | 3211 | if insitu: self._assign(s)
|
---|
| 3212 | else: return s
|
---|
[513] | 3213 |
|
---|
[2186] | 3214 | @asaplog_post_dec
|
---|
[2435] | 3215 | def regrid_channel(self, width=5, plot=False, insitu=None):
|
---|
| 3216 | """\
|
---|
| 3217 | Regrid the spectra by the specified channel width
|
---|
| 3218 |
|
---|
| 3219 | Parameters:
|
---|
| 3220 |
|
---|
| 3221 | width: The channel width (float) of regridded spectra
|
---|
| 3222 | in the current spectral unit.
|
---|
| 3223 |
|
---|
| 3224 | plot: [NOT IMPLEMENTED YET]
|
---|
| 3225 | plot the original and the regridded spectra.
|
---|
| 3226 | In this each indivual fit has to be approved, by
|
---|
| 3227 | typing 'y' or 'n'
|
---|
| 3228 |
|
---|
| 3229 | insitu: if False a new scantable is returned.
|
---|
| 3230 | Otherwise, the scaling is done in-situ
|
---|
| 3231 | The default is taken from .asaprc (False)
|
---|
| 3232 |
|
---|
| 3233 | """
|
---|
| 3234 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 3235 | varlist = vars()
|
---|
| 3236 |
|
---|
| 3237 | if plot:
|
---|
| 3238 | asaplog.post()
|
---|
| 3239 | asaplog.push("Verification plot is not implemtnetd yet.")
|
---|
| 3240 | asaplog.post("WARN")
|
---|
| 3241 |
|
---|
| 3242 | s = self.copy()
|
---|
| 3243 | s._regrid_specchan(width)
|
---|
| 3244 |
|
---|
| 3245 | s._add_history("regrid_channel", varlist)
|
---|
| 3246 |
|
---|
| 3247 | # if plot:
|
---|
| 3248 | # from asap.asapplotter import new_asaplot
|
---|
| 3249 | # theplot = new_asaplot(rcParams['plotter.gui'])
|
---|
[2535] | 3250 | # from matplotlib import rc as rcp
|
---|
| 3251 | # rcp('lines', linewidth=1)
|
---|
[2435] | 3252 | # theplot.set_panels()
|
---|
| 3253 | # ylab=s._get_ordinate_label()
|
---|
| 3254 | # #theplot.palette(0,["#777777","red"])
|
---|
| 3255 | # for r in xrange(s.nrow()):
|
---|
| 3256 | # xsm=s._getabcissa(r)
|
---|
| 3257 | # ysm=s._getspectrum(r)
|
---|
| 3258 | # xorg=orgscan._getabcissa(r)
|
---|
| 3259 | # yorg=orgscan._getspectrum(r)
|
---|
| 3260 | # theplot.clear()
|
---|
| 3261 | # theplot.hold()
|
---|
| 3262 | # theplot.set_axes('ylabel',ylab)
|
---|
| 3263 | # theplot.set_axes('xlabel',s._getabcissalabel(r))
|
---|
| 3264 | # theplot.set_axes('title',s._getsourcename(r))
|
---|
| 3265 | # theplot.set_line(label='Original',color="#777777")
|
---|
| 3266 | # theplot.plot(xorg,yorg)
|
---|
| 3267 | # theplot.set_line(label='Smoothed',color="red")
|
---|
| 3268 | # theplot.plot(xsm,ysm)
|
---|
| 3269 | # ### Ugly part for legend
|
---|
| 3270 | # for i in [0,1]:
|
---|
| 3271 | # theplot.subplots[0]['lines'].append(
|
---|
| 3272 | # [theplot.subplots[0]['axes'].lines[i]]
|
---|
| 3273 | # )
|
---|
| 3274 | # theplot.release()
|
---|
| 3275 | # ### Ugly part for legend
|
---|
| 3276 | # theplot.subplots[0]['lines']=[]
|
---|
| 3277 | # res = raw_input("Accept smoothing ([y]/n): ")
|
---|
| 3278 | # if res.upper() == 'N':
|
---|
| 3279 | # s._setspectrum(yorg, r)
|
---|
| 3280 | # theplot.quit()
|
---|
| 3281 | # del theplot
|
---|
| 3282 | # del orgscan
|
---|
| 3283 |
|
---|
| 3284 | if insitu: self._assign(s)
|
---|
| 3285 | else: return s
|
---|
| 3286 |
|
---|
| 3287 | @asaplog_post_dec
|
---|
[2186] | 3288 | def _parse_wn(self, wn):
|
---|
| 3289 | if isinstance(wn, list) or isinstance(wn, tuple):
|
---|
| 3290 | return wn
|
---|
| 3291 | elif isinstance(wn, int):
|
---|
| 3292 | return [ wn ]
|
---|
| 3293 | elif isinstance(wn, str):
|
---|
[2277] | 3294 | if '-' in wn: # case 'a-b' : return [a,a+1,...,b-1,b]
|
---|
[2186] | 3295 | val = wn.split('-')
|
---|
| 3296 | val = [int(val[0]), int(val[1])]
|
---|
| 3297 | val.sort()
|
---|
| 3298 | res = [i for i in xrange(val[0], val[1]+1)]
|
---|
[2277] | 3299 | elif wn[:2] == '<=' or wn[:2] == '=<': # cases '<=a','=<a' : return [0,1,...,a-1,a]
|
---|
[2186] | 3300 | val = int(wn[2:])+1
|
---|
| 3301 | res = [i for i in xrange(val)]
|
---|
[2277] | 3302 | elif wn[-2:] == '>=' or wn[-2:] == '=>': # cases 'a>=','a=>' : return [0,1,...,a-1,a]
|
---|
[2186] | 3303 | val = int(wn[:-2])+1
|
---|
| 3304 | res = [i for i in xrange(val)]
|
---|
[2277] | 3305 | elif wn[0] == '<': # case '<a' : return [0,1,...,a-2,a-1]
|
---|
[2186] | 3306 | val = int(wn[1:])
|
---|
| 3307 | res = [i for i in xrange(val)]
|
---|
[2277] | 3308 | elif wn[-1] == '>': # case 'a>' : return [0,1,...,a-2,a-1]
|
---|
[2186] | 3309 | val = int(wn[:-1])
|
---|
| 3310 | res = [i for i in xrange(val)]
|
---|
[2411] | 3311 | elif wn[:2] == '>=' or wn[:2] == '=>': # cases '>=a','=>a' : return [a,-999], which is
|
---|
| 3312 | # then interpreted in C++
|
---|
| 3313 | # side as [a,a+1,...,a_nyq]
|
---|
| 3314 | # (CAS-3759)
|
---|
[2186] | 3315 | val = int(wn[2:])
|
---|
[2411] | 3316 | res = [val, -999]
|
---|
| 3317 | #res = [i for i in xrange(val, self.nchan()/2+1)]
|
---|
| 3318 | elif wn[-2:] == '<=' or wn[-2:] == '=<': # cases 'a<=','a=<' : return [a,-999], which is
|
---|
| 3319 | # then interpreted in C++
|
---|
| 3320 | # side as [a,a+1,...,a_nyq]
|
---|
| 3321 | # (CAS-3759)
|
---|
[2186] | 3322 | val = int(wn[:-2])
|
---|
[2411] | 3323 | res = [val, -999]
|
---|
| 3324 | #res = [i for i in xrange(val, self.nchan()/2+1)]
|
---|
| 3325 | elif wn[0] == '>': # case '>a' : return [a+1,-999], which is
|
---|
| 3326 | # then interpreted in C++
|
---|
| 3327 | # side as [a+1,a+2,...,a_nyq]
|
---|
| 3328 | # (CAS-3759)
|
---|
[2186] | 3329 | val = int(wn[1:])+1
|
---|
[2411] | 3330 | res = [val, -999]
|
---|
| 3331 | #res = [i for i in xrange(val, self.nchan()/2+1)]
|
---|
| 3332 | elif wn[-1] == '<': # case 'a<' : return [a+1,-999], which is
|
---|
| 3333 | # then interpreted in C++
|
---|
| 3334 | # side as [a+1,a+2,...,a_nyq]
|
---|
| 3335 | # (CAS-3759)
|
---|
[2186] | 3336 | val = int(wn[:-1])+1
|
---|
[2411] | 3337 | res = [val, -999]
|
---|
| 3338 | #res = [i for i in xrange(val, self.nchan()/2+1)]
|
---|
[2012] | 3339 |
|
---|
[2186] | 3340 | return res
|
---|
| 3341 | else:
|
---|
| 3342 | msg = 'wrong value given for addwn/rejwn'
|
---|
| 3343 | raise RuntimeError(msg)
|
---|
| 3344 |
|
---|
[2713] | 3345 | @asaplog_post_dec
|
---|
[2810] | 3346 | def apply_bltable(self, insitu=None, retfitres=None, inbltable=None, outbltable=None, overwrite=None):
|
---|
[2767] | 3347 | """\
|
---|
| 3348 | Subtract baseline based on parameters written in Baseline Table.
|
---|
| 3349 |
|
---|
| 3350 | Parameters:
|
---|
[2809] | 3351 | insitu: if True, baseline fitting/subtraction is done
|
---|
[2810] | 3352 | in-situ. If False, a new scantable with
|
---|
| 3353 | baseline subtracted is returned. Actually,
|
---|
| 3354 | format of the returned value depends on both
|
---|
| 3355 | insitu and retfitres (see below).
|
---|
[2767] | 3356 | The default is taken from .asaprc (False)
|
---|
[2810] | 3357 | retfitres: if True, the results of baseline fitting (i.e.,
|
---|
| 3358 | coefficients and rms) are returned.
|
---|
| 3359 | default is False.
|
---|
| 3360 | The format of the returned value of this
|
---|
| 3361 | function varies as follows:
|
---|
| 3362 | (1) in case insitu=True and retfitres=True:
|
---|
| 3363 | fitting result.
|
---|
| 3364 | (2) in case insitu=True and retfitres=False:
|
---|
| 3365 | None.
|
---|
| 3366 | (3) in case insitu=False and retfitres=True:
|
---|
| 3367 | a dictionary containing a new scantable
|
---|
| 3368 | (with baseline subtracted) and the fitting
|
---|
| 3369 | results.
|
---|
| 3370 | (4) in case insitu=False and retfitres=False:
|
---|
| 3371 | a new scantable (with baseline subtracted).
|
---|
[2767] | 3372 | inbltable: name of input baseline table. The row number of
|
---|
| 3373 | scantable and that of inbltable must be
|
---|
| 3374 | identical.
|
---|
| 3375 | outbltable: name of output baseline table where baseline
|
---|
| 3376 | parameters and fitting results recorded.
|
---|
| 3377 | default is ''(no output).
|
---|
[2809] | 3378 | overwrite: if True when an existing baseline table is
|
---|
| 3379 | specified for outbltable, overwrites it.
|
---|
| 3380 | Otherwise there is no harm.
|
---|
[2767] | 3381 | default is False.
|
---|
| 3382 | """
|
---|
| 3383 |
|
---|
| 3384 | try:
|
---|
| 3385 | varlist = vars()
|
---|
[2810] | 3386 | if retfitres is None: retfitres = False
|
---|
[2767] | 3387 | if inbltable is None: raise ValueError("bltable missing.")
|
---|
| 3388 | if outbltable is None: outbltable = ''
|
---|
| 3389 | if overwrite is None: overwrite = False
|
---|
| 3390 |
|
---|
| 3391 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 3392 | if insitu:
|
---|
| 3393 | workscan = self
|
---|
| 3394 | else:
|
---|
| 3395 | workscan = self.copy()
|
---|
| 3396 |
|
---|
| 3397 | sres = workscan._apply_bltable(inbltable,
|
---|
[2810] | 3398 | retfitres,
|
---|
[2767] | 3399 | outbltable,
|
---|
| 3400 | os.path.exists(outbltable),
|
---|
| 3401 | overwrite)
|
---|
[2810] | 3402 | if retfitres: res = parse_fitresult(sres)
|
---|
[2767] | 3403 |
|
---|
| 3404 | workscan._add_history('apply_bltable', varlist)
|
---|
| 3405 |
|
---|
| 3406 | if insitu:
|
---|
| 3407 | self._assign(workscan)
|
---|
[2810] | 3408 | if retfitres:
|
---|
| 3409 | return res
|
---|
| 3410 | else:
|
---|
| 3411 | return None
|
---|
[2767] | 3412 | else:
|
---|
[2810] | 3413 | if retfitres:
|
---|
| 3414 | return {'scantable': workscan, 'fitresults': res}
|
---|
| 3415 | else:
|
---|
| 3416 | return workscan
|
---|
[2767] | 3417 |
|
---|
| 3418 | except RuntimeError, e:
|
---|
| 3419 | raise_fitting_failure_exception(e)
|
---|
| 3420 |
|
---|
| 3421 | @asaplog_post_dec
|
---|
[2810] | 3422 | def sub_baseline(self, insitu=None, retfitres=None, blinfo=None, bltable=None, overwrite=None):
|
---|
[2767] | 3423 | """\
|
---|
| 3424 | Subtract baseline based on parameters written in the input list.
|
---|
| 3425 |
|
---|
| 3426 | Parameters:
|
---|
[2809] | 3427 | insitu: if True, baseline fitting/subtraction is done
|
---|
[2810] | 3428 | in-situ. If False, a new scantable with
|
---|
| 3429 | baseline subtracted is returned. Actually,
|
---|
| 3430 | format of the returned value depends on both
|
---|
| 3431 | insitu and retfitres (see below).
|
---|
[2767] | 3432 | The default is taken from .asaprc (False)
|
---|
[2810] | 3433 | retfitres: if True, the results of baseline fitting (i.e.,
|
---|
| 3434 | coefficients and rms) are returned.
|
---|
| 3435 | default is False.
|
---|
| 3436 | The format of the returned value of this
|
---|
| 3437 | function varies as follows:
|
---|
| 3438 | (1) in case insitu=True and retfitres=True:
|
---|
| 3439 | fitting result.
|
---|
| 3440 | (2) in case insitu=True and retfitres=False:
|
---|
| 3441 | None.
|
---|
| 3442 | (3) in case insitu=False and retfitres=True:
|
---|
| 3443 | a dictionary containing a new scantable
|
---|
| 3444 | (with baseline subtracted) and the fitting
|
---|
| 3445 | results.
|
---|
| 3446 | (4) in case insitu=False and retfitres=False:
|
---|
| 3447 | a new scantable (with baseline subtracted).
|
---|
[2767] | 3448 | blinfo: baseline parameter set stored in a dictionary
|
---|
| 3449 | or a list of dictionary. Each dictionary
|
---|
| 3450 | corresponds to each spectrum and must contain
|
---|
| 3451 | the following keys and values:
|
---|
| 3452 | 'row': row number,
|
---|
| 3453 | 'blfunc': function name. available ones include
|
---|
| 3454 | 'poly', 'chebyshev', 'cspline' and
|
---|
| 3455 | 'sinusoid',
|
---|
| 3456 | 'order': maximum order of polynomial. needed
|
---|
| 3457 | if blfunc='poly' or 'chebyshev',
|
---|
| 3458 | 'npiece': number or piecewise polynomial.
|
---|
| 3459 | needed if blfunc='cspline',
|
---|
| 3460 | 'nwave': a list of sinusoidal wave numbers.
|
---|
| 3461 | needed if blfunc='sinusoid', and
|
---|
| 3462 | 'masklist': min-max windows for channel mask.
|
---|
| 3463 | the specified ranges will be used
|
---|
| 3464 | for fitting.
|
---|
| 3465 | bltable: name of output baseline table where baseline
|
---|
| 3466 | parameters and fitting results recorded.
|
---|
| 3467 | default is ''(no output).
|
---|
[2809] | 3468 | overwrite: if True when an existing baseline table is
|
---|
| 3469 | specified for bltable, overwrites it.
|
---|
| 3470 | Otherwise there is no harm.
|
---|
[2767] | 3471 | default is False.
|
---|
| 3472 |
|
---|
| 3473 | Example:
|
---|
| 3474 | sub_baseline(blinfo=[{'row':0, 'blfunc':'poly', 'order':5,
|
---|
| 3475 | 'masklist':[[10,350],[352,510]]},
|
---|
| 3476 | {'row':1, 'blfunc':'cspline', 'npiece':3,
|
---|
| 3477 | 'masklist':[[3,16],[19,404],[407,511]]}
|
---|
| 3478 | ])
|
---|
| 3479 |
|
---|
| 3480 | the first spectrum (row=0) will be fitted with polynomial
|
---|
| 3481 | of order=5 and the next one (row=1) will be fitted with cubic
|
---|
| 3482 | spline consisting of 3 pieces.
|
---|
| 3483 | """
|
---|
| 3484 |
|
---|
| 3485 | try:
|
---|
| 3486 | varlist = vars()
|
---|
[2810] | 3487 | if retfitres is None: retfitres = False
|
---|
[2767] | 3488 | if blinfo is None: blinfo = []
|
---|
| 3489 | if bltable is None: bltable = ''
|
---|
| 3490 | if overwrite is None: overwrite = False
|
---|
| 3491 |
|
---|
| 3492 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 3493 | if insitu:
|
---|
| 3494 | workscan = self
|
---|
| 3495 | else:
|
---|
| 3496 | workscan = self.copy()
|
---|
| 3497 |
|
---|
| 3498 | nrow = workscan.nrow()
|
---|
| 3499 |
|
---|
| 3500 | in_blinfo = pack_blinfo(blinfo=blinfo, maxirow=nrow)
|
---|
| 3501 |
|
---|
| 3502 | sres = workscan._sub_baseline(in_blinfo,
|
---|
[2810] | 3503 | retfitres,
|
---|
[2767] | 3504 | bltable,
|
---|
| 3505 | os.path.exists(bltable),
|
---|
| 3506 | overwrite)
|
---|
[2810] | 3507 | if retfitres: res = parse_fitresult(sres)
|
---|
[2767] | 3508 |
|
---|
| 3509 | workscan._add_history('sub_baseline', varlist)
|
---|
| 3510 |
|
---|
| 3511 | if insitu:
|
---|
| 3512 | self._assign(workscan)
|
---|
[2810] | 3513 | if retfitres:
|
---|
| 3514 | return res
|
---|
| 3515 | else:
|
---|
| 3516 | return None
|
---|
[2767] | 3517 | else:
|
---|
[2810] | 3518 | if retfitres:
|
---|
| 3519 | return {'scantable': workscan, 'fitresults': res}
|
---|
| 3520 | else:
|
---|
| 3521 | return workscan
|
---|
[2767] | 3522 |
|
---|
| 3523 | except RuntimeError, e:
|
---|
| 3524 | raise_fitting_failure_exception(e)
|
---|
| 3525 |
|
---|
| 3526 | @asaplog_post_dec
|
---|
[2713] | 3527 | def calc_aic(self, value=None, blfunc=None, order=None, mask=None,
|
---|
| 3528 | whichrow=None, uselinefinder=None, edge=None,
|
---|
| 3529 | threshold=None, chan_avg_limit=None):
|
---|
| 3530 | """\
|
---|
| 3531 | Calculates and returns model selection criteria for a specified
|
---|
| 3532 | baseline model and a given spectrum data.
|
---|
| 3533 | Available values include Akaike Information Criterion (AIC), the
|
---|
| 3534 | corrected Akaike Information Criterion (AICc) by Sugiura(1978),
|
---|
| 3535 | Bayesian Information Criterion (BIC) and the Generalised Cross
|
---|
| 3536 | Validation (GCV).
|
---|
[2186] | 3537 |
|
---|
[2713] | 3538 | Parameters:
|
---|
| 3539 | value: name of model selection criteria to calculate.
|
---|
| 3540 | available ones include 'aic', 'aicc', 'bic' and
|
---|
| 3541 | 'gcv'. default is 'aicc'.
|
---|
| 3542 | blfunc: baseline function name. available ones include
|
---|
| 3543 | 'chebyshev', 'cspline' and 'sinusoid'.
|
---|
| 3544 | default is 'chebyshev'.
|
---|
| 3545 | order: parameter for basline function. actually stands for
|
---|
| 3546 | order of polynomial (order) for 'chebyshev',
|
---|
| 3547 | number of spline pieces (npiece) for 'cspline' and
|
---|
| 3548 | maximum wave number for 'sinusoid', respectively.
|
---|
| 3549 | default is 5 (which is also the default order value
|
---|
| 3550 | for [auto_]chebyshev_baseline()).
|
---|
| 3551 | mask: an optional mask. default is [].
|
---|
| 3552 | whichrow: row number. default is 0 (the first row)
|
---|
| 3553 | uselinefinder: use sd.linefinder() to flag out line regions
|
---|
| 3554 | default is True.
|
---|
| 3555 | edge: an optional number of channel to drop at
|
---|
| 3556 | the edge of spectrum. If only one value is
|
---|
| 3557 | specified, the same number will be dropped
|
---|
| 3558 | from both sides of the spectrum. Default
|
---|
| 3559 | is to keep all channels. Nested tuples
|
---|
| 3560 | represent individual edge selection for
|
---|
| 3561 | different IFs (a number of spectral channels
|
---|
| 3562 | can be different)
|
---|
| 3563 | default is (0, 0).
|
---|
| 3564 | threshold: the threshold used by line finder. It is
|
---|
| 3565 | better to keep it large as only strong lines
|
---|
| 3566 | affect the baseline solution.
|
---|
| 3567 | default is 3.
|
---|
| 3568 | chan_avg_limit: a maximum number of consequtive spectral
|
---|
| 3569 | channels to average during the search of
|
---|
| 3570 | weak and broad lines. The default is no
|
---|
| 3571 | averaging (and no search for weak lines).
|
---|
| 3572 | If such lines can affect the fitted baseline
|
---|
| 3573 | (e.g. a high order polynomial is fitted),
|
---|
| 3574 | increase this parameter (usually values up
|
---|
| 3575 | to 8 are reasonable). Most users of this
|
---|
| 3576 | method should find the default value sufficient.
|
---|
| 3577 | default is 1.
|
---|
| 3578 |
|
---|
| 3579 | Example:
|
---|
| 3580 | aic = scan.calc_aic(blfunc='chebyshev', order=5, whichrow=0)
|
---|
| 3581 | """
|
---|
| 3582 |
|
---|
| 3583 | try:
|
---|
| 3584 | varlist = vars()
|
---|
| 3585 |
|
---|
| 3586 | if value is None: value = 'aicc'
|
---|
| 3587 | if blfunc is None: blfunc = 'chebyshev'
|
---|
| 3588 | if order is None: order = 5
|
---|
| 3589 | if mask is None: mask = []
|
---|
| 3590 | if whichrow is None: whichrow = 0
|
---|
| 3591 | if uselinefinder is None: uselinefinder = True
|
---|
| 3592 | if edge is None: edge = (0, 0)
|
---|
| 3593 | if threshold is None: threshold = 3
|
---|
| 3594 | if chan_avg_limit is None: chan_avg_limit = 1
|
---|
| 3595 |
|
---|
| 3596 | return self._calc_aic(value, blfunc, order, mask,
|
---|
| 3597 | whichrow, uselinefinder, edge,
|
---|
| 3598 | threshold, chan_avg_limit)
|
---|
| 3599 |
|
---|
| 3600 | except RuntimeError, e:
|
---|
| 3601 | raise_fitting_failure_exception(e)
|
---|
| 3602 |
|
---|
[1862] | 3603 | @asaplog_post_dec
|
---|
[2771] | 3604 | def sinusoid_baseline(self, mask=None, applyfft=None,
|
---|
[2269] | 3605 | fftmethod=None, fftthresh=None,
|
---|
[2771] | 3606 | addwn=None, rejwn=None,
|
---|
| 3607 | insitu=None,
|
---|
| 3608 | clipthresh=None, clipniter=None,
|
---|
| 3609 | plot=None,
|
---|
| 3610 | getresidual=None,
|
---|
| 3611 | showprogress=None, minnrow=None,
|
---|
| 3612 | outlog=None,
|
---|
[2767] | 3613 | blfile=None, csvformat=None,
|
---|
| 3614 | bltable=None):
|
---|
[2047] | 3615 | """\
|
---|
[2349] | 3616 | Return a scan which has been baselined (all rows) with sinusoidal
|
---|
| 3617 | functions.
|
---|
| 3618 |
|
---|
[2047] | 3619 | Parameters:
|
---|
[2186] | 3620 | mask: an optional mask
|
---|
| 3621 | applyfft: if True use some method, such as FFT, to find
|
---|
| 3622 | strongest sinusoidal components in the wavenumber
|
---|
| 3623 | domain to be used for baseline fitting.
|
---|
| 3624 | default is True.
|
---|
| 3625 | fftmethod: method to find the strong sinusoidal components.
|
---|
| 3626 | now only 'fft' is available and it is the default.
|
---|
| 3627 | fftthresh: the threshold to select wave numbers to be used for
|
---|
| 3628 | fitting from the distribution of amplitudes in the
|
---|
| 3629 | wavenumber domain.
|
---|
| 3630 | both float and string values accepted.
|
---|
| 3631 | given a float value, the unit is set to sigma.
|
---|
| 3632 | for string values, allowed formats include:
|
---|
[2349] | 3633 | 'xsigma' or 'x' (= x-sigma level. e.g.,
|
---|
| 3634 | '3sigma'), or
|
---|
[2186] | 3635 | 'topx' (= the x strongest ones, e.g. 'top5').
|
---|
| 3636 | default is 3.0 (unit: sigma).
|
---|
| 3637 | addwn: the additional wave numbers to be used for fitting.
|
---|
| 3638 | list or integer value is accepted to specify every
|
---|
| 3639 | wave numbers. also string value can be used in case
|
---|
| 3640 | you need to specify wave numbers in a certain range,
|
---|
| 3641 | e.g., 'a-b' (= a, a+1, a+2, ..., b-1, b),
|
---|
| 3642 | '<a' (= 0,1,...,a-2,a-1),
|
---|
| 3643 | '>=a' (= a, a+1, ... up to the maximum wave
|
---|
| 3644 | number corresponding to the Nyquist
|
---|
| 3645 | frequency for the case of FFT).
|
---|
[2411] | 3646 | default is [0].
|
---|
[2186] | 3647 | rejwn: the wave numbers NOT to be used for fitting.
|
---|
| 3648 | can be set just as addwn but has higher priority:
|
---|
| 3649 | wave numbers which are specified both in addwn
|
---|
| 3650 | and rejwn will NOT be used. default is [].
|
---|
[2771] | 3651 | insitu: if False a new scantable is returned.
|
---|
| 3652 | Otherwise, the scaling is done in-situ
|
---|
| 3653 | The default is taken from .asaprc (False)
|
---|
[2081] | 3654 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
|
---|
[2349] | 3655 | clipniter: maximum number of iteration of 'clipthresh'-sigma
|
---|
| 3656 | clipping (default is 0)
|
---|
[2081] | 3657 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
|
---|
| 3658 | plot the fit and the residual. In this each
|
---|
| 3659 | indivual fit has to be approved, by typing 'y'
|
---|
| 3660 | or 'n'
|
---|
| 3661 | getresidual: if False, returns best-fit values instead of
|
---|
| 3662 | residual. (default is True)
|
---|
[2189] | 3663 | showprogress: show progress status for large data.
|
---|
| 3664 | default is True.
|
---|
| 3665 | minnrow: minimum number of input spectra to show.
|
---|
| 3666 | default is 1000.
|
---|
[2081] | 3667 | outlog: Output the coefficients of the best-fit
|
---|
| 3668 | function to logger (default is False)
|
---|
| 3669 | blfile: Name of a text file in which the best-fit
|
---|
| 3670 | parameter values to be written
|
---|
[2186] | 3671 | (default is '': no file/logger output)
|
---|
[2641] | 3672 | csvformat: if True blfile is csv-formatted, default is False.
|
---|
[2767] | 3673 | bltable: name of a baseline table where fitting results
|
---|
| 3674 | (coefficients, rms, etc.) are to be written.
|
---|
| 3675 | if given, fitting results will NOT be output to
|
---|
| 3676 | scantable (insitu=True) or None will be
|
---|
| 3677 | returned (insitu=False).
|
---|
| 3678 | (default is "": no table output)
|
---|
[2047] | 3679 |
|
---|
| 3680 | Example:
|
---|
[2349] | 3681 | # return a scan baselined by a combination of sinusoidal curves
|
---|
| 3682 | # having wave numbers in spectral window up to 10,
|
---|
[2047] | 3683 | # also with 3-sigma clipping, iteration up to 4 times
|
---|
[2186] | 3684 | bscan = scan.sinusoid_baseline(addwn='<=10',clipthresh=3.0,clipniter=4)
|
---|
[2081] | 3685 |
|
---|
| 3686 | Note:
|
---|
| 3687 | The best-fit parameter values output in logger and/or blfile are now
|
---|
| 3688 | based on specunit of 'channel'.
|
---|
[2047] | 3689 | """
|
---|
| 3690 |
|
---|
[2186] | 3691 | try:
|
---|
| 3692 | varlist = vars()
|
---|
[2047] | 3693 |
|
---|
[2186] | 3694 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 3695 | if insitu:
|
---|
| 3696 | workscan = self
|
---|
| 3697 | else:
|
---|
| 3698 | workscan = self.copy()
|
---|
| 3699 |
|
---|
[2410] | 3700 | if mask is None: mask = []
|
---|
[2186] | 3701 | if applyfft is None: applyfft = True
|
---|
| 3702 | if fftmethod is None: fftmethod = 'fft'
|
---|
| 3703 | if fftthresh is None: fftthresh = 3.0
|
---|
[2411] | 3704 | if addwn is None: addwn = [0]
|
---|
[2186] | 3705 | if rejwn is None: rejwn = []
|
---|
| 3706 | if clipthresh is None: clipthresh = 3.0
|
---|
| 3707 | if clipniter is None: clipniter = 0
|
---|
| 3708 | if plot is None: plot = False
|
---|
| 3709 | if getresidual is None: getresidual = True
|
---|
[2189] | 3710 | if showprogress is None: showprogress = True
|
---|
| 3711 | if minnrow is None: minnrow = 1000
|
---|
[2186] | 3712 | if outlog is None: outlog = False
|
---|
| 3713 | if blfile is None: blfile = ''
|
---|
[2641] | 3714 | if csvformat is None: csvformat = False
|
---|
[2767] | 3715 | if bltable is None: bltable = ''
|
---|
[2047] | 3716 |
|
---|
[2767] | 3717 | sapplyfft = 'true' if applyfft else 'false'
|
---|
| 3718 | fftinfo = ','.join([sapplyfft, fftmethod.lower(), str(fftthresh).lower()])
|
---|
[2641] | 3719 |
|
---|
[2767] | 3720 | scsvformat = 'T' if csvformat else 'F'
|
---|
| 3721 |
|
---|
[2081] | 3722 | #CURRENTLY, PLOT=true is UNAVAILABLE UNTIL sinusoidal fitting is implemented as a fitter method.
|
---|
[2767] | 3723 | workscan._sinusoid_baseline(mask,
|
---|
| 3724 | fftinfo,
|
---|
| 3725 | #applyfft, fftmethod.lower(),
|
---|
| 3726 | #str(fftthresh).lower(),
|
---|
[2349] | 3727 | workscan._parse_wn(addwn),
|
---|
[2643] | 3728 | workscan._parse_wn(rejwn),
|
---|
| 3729 | clipthresh, clipniter,
|
---|
| 3730 | getresidual,
|
---|
[2349] | 3731 | pack_progress_params(showprogress,
|
---|
[2641] | 3732 | minnrow),
|
---|
[2767] | 3733 | outlog, scsvformat+blfile,
|
---|
| 3734 | bltable)
|
---|
[2186] | 3735 | workscan._add_history('sinusoid_baseline', varlist)
|
---|
[2767] | 3736 |
|
---|
| 3737 | if bltable == '':
|
---|
| 3738 | if insitu:
|
---|
| 3739 | self._assign(workscan)
|
---|
| 3740 | else:
|
---|
| 3741 | return workscan
|
---|
[2047] | 3742 | else:
|
---|
[2767] | 3743 | if not insitu:
|
---|
| 3744 | return None
|
---|
[2047] | 3745 |
|
---|
| 3746 | except RuntimeError, e:
|
---|
[2186] | 3747 | raise_fitting_failure_exception(e)
|
---|
[2047] | 3748 |
|
---|
| 3749 |
|
---|
[2186] | 3750 | @asaplog_post_dec
|
---|
[2771] | 3751 | def auto_sinusoid_baseline(self, mask=None, applyfft=None,
|
---|
[2349] | 3752 | fftmethod=None, fftthresh=None,
|
---|
[2771] | 3753 | addwn=None, rejwn=None,
|
---|
| 3754 | insitu=None,
|
---|
| 3755 | clipthresh=None, clipniter=None,
|
---|
| 3756 | edge=None, threshold=None, chan_avg_limit=None,
|
---|
| 3757 | plot=None,
|
---|
| 3758 | getresidual=None,
|
---|
| 3759 | showprogress=None, minnrow=None,
|
---|
| 3760 | outlog=None,
|
---|
[2767] | 3761 | blfile=None, csvformat=None,
|
---|
| 3762 | bltable=None):
|
---|
[2047] | 3763 | """\
|
---|
[2349] | 3764 | Return a scan which has been baselined (all rows) with sinusoidal
|
---|
| 3765 | functions.
|
---|
[2047] | 3766 | Spectral lines are detected first using linefinder and masked out
|
---|
| 3767 | to avoid them affecting the baseline solution.
|
---|
| 3768 |
|
---|
| 3769 | Parameters:
|
---|
[2189] | 3770 | mask: an optional mask retreived from scantable
|
---|
| 3771 | applyfft: if True use some method, such as FFT, to find
|
---|
| 3772 | strongest sinusoidal components in the wavenumber
|
---|
| 3773 | domain to be used for baseline fitting.
|
---|
| 3774 | default is True.
|
---|
| 3775 | fftmethod: method to find the strong sinusoidal components.
|
---|
| 3776 | now only 'fft' is available and it is the default.
|
---|
| 3777 | fftthresh: the threshold to select wave numbers to be used for
|
---|
| 3778 | fitting from the distribution of amplitudes in the
|
---|
| 3779 | wavenumber domain.
|
---|
| 3780 | both float and string values accepted.
|
---|
| 3781 | given a float value, the unit is set to sigma.
|
---|
| 3782 | for string values, allowed formats include:
|
---|
[2349] | 3783 | 'xsigma' or 'x' (= x-sigma level. e.g.,
|
---|
| 3784 | '3sigma'), or
|
---|
[2189] | 3785 | 'topx' (= the x strongest ones, e.g. 'top5').
|
---|
| 3786 | default is 3.0 (unit: sigma).
|
---|
| 3787 | addwn: the additional wave numbers to be used for fitting.
|
---|
| 3788 | list or integer value is accepted to specify every
|
---|
| 3789 | wave numbers. also string value can be used in case
|
---|
| 3790 | you need to specify wave numbers in a certain range,
|
---|
| 3791 | e.g., 'a-b' (= a, a+1, a+2, ..., b-1, b),
|
---|
| 3792 | '<a' (= 0,1,...,a-2,a-1),
|
---|
| 3793 | '>=a' (= a, a+1, ... up to the maximum wave
|
---|
| 3794 | number corresponding to the Nyquist
|
---|
| 3795 | frequency for the case of FFT).
|
---|
[2411] | 3796 | default is [0].
|
---|
[2189] | 3797 | rejwn: the wave numbers NOT to be used for fitting.
|
---|
| 3798 | can be set just as addwn but has higher priority:
|
---|
| 3799 | wave numbers which are specified both in addwn
|
---|
| 3800 | and rejwn will NOT be used. default is [].
|
---|
[2771] | 3801 | insitu: if False a new scantable is returned.
|
---|
| 3802 | Otherwise, the scaling is done in-situ
|
---|
| 3803 | The default is taken from .asaprc (False)
|
---|
[2189] | 3804 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
|
---|
[2349] | 3805 | clipniter: maximum number of iteration of 'clipthresh'-sigma
|
---|
| 3806 | clipping (default is 0)
|
---|
[2189] | 3807 | edge: an optional number of channel to drop at
|
---|
| 3808 | the edge of spectrum. If only one value is
|
---|
| 3809 | specified, the same number will be dropped
|
---|
| 3810 | from both sides of the spectrum. Default
|
---|
| 3811 | is to keep all channels. Nested tuples
|
---|
| 3812 | represent individual edge selection for
|
---|
| 3813 | different IFs (a number of spectral channels
|
---|
| 3814 | can be different)
|
---|
| 3815 | threshold: the threshold used by line finder. It is
|
---|
| 3816 | better to keep it large as only strong lines
|
---|
| 3817 | affect the baseline solution.
|
---|
| 3818 | chan_avg_limit: a maximum number of consequtive spectral
|
---|
| 3819 | channels to average during the search of
|
---|
| 3820 | weak and broad lines. The default is no
|
---|
| 3821 | averaging (and no search for weak lines).
|
---|
| 3822 | If such lines can affect the fitted baseline
|
---|
| 3823 | (e.g. a high order polynomial is fitted),
|
---|
| 3824 | increase this parameter (usually values up
|
---|
| 3825 | to 8 are reasonable). Most users of this
|
---|
| 3826 | method should find the default value sufficient.
|
---|
| 3827 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
|
---|
| 3828 | plot the fit and the residual. In this each
|
---|
| 3829 | indivual fit has to be approved, by typing 'y'
|
---|
| 3830 | or 'n'
|
---|
| 3831 | getresidual: if False, returns best-fit values instead of
|
---|
| 3832 | residual. (default is True)
|
---|
| 3833 | showprogress: show progress status for large data.
|
---|
| 3834 | default is True.
|
---|
| 3835 | minnrow: minimum number of input spectra to show.
|
---|
| 3836 | default is 1000.
|
---|
| 3837 | outlog: Output the coefficients of the best-fit
|
---|
| 3838 | function to logger (default is False)
|
---|
| 3839 | blfile: Name of a text file in which the best-fit
|
---|
| 3840 | parameter values to be written
|
---|
| 3841 | (default is "": no file/logger output)
|
---|
[2641] | 3842 | csvformat: if True blfile is csv-formatted, default is False.
|
---|
[2767] | 3843 | bltable: name of a baseline table where fitting results
|
---|
| 3844 | (coefficients, rms, etc.) are to be written.
|
---|
| 3845 | if given, fitting results will NOT be output to
|
---|
| 3846 | scantable (insitu=True) or None will be
|
---|
| 3847 | returned (insitu=False).
|
---|
| 3848 | (default is "": no table output)
|
---|
[2047] | 3849 |
|
---|
| 3850 | Example:
|
---|
[2186] | 3851 | bscan = scan.auto_sinusoid_baseline(addwn='<=10', insitu=False)
|
---|
[2081] | 3852 |
|
---|
| 3853 | Note:
|
---|
| 3854 | The best-fit parameter values output in logger and/or blfile are now
|
---|
| 3855 | based on specunit of 'channel'.
|
---|
[2047] | 3856 | """
|
---|
| 3857 |
|
---|
[2186] | 3858 | try:
|
---|
| 3859 | varlist = vars()
|
---|
[2047] | 3860 |
|
---|
[2186] | 3861 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 3862 | if insitu:
|
---|
| 3863 | workscan = self
|
---|
[2047] | 3864 | else:
|
---|
[2186] | 3865 | workscan = self.copy()
|
---|
| 3866 |
|
---|
[2410] | 3867 | if mask is None: mask = []
|
---|
[2186] | 3868 | if applyfft is None: applyfft = True
|
---|
| 3869 | if fftmethod is None: fftmethod = 'fft'
|
---|
| 3870 | if fftthresh is None: fftthresh = 3.0
|
---|
[2411] | 3871 | if addwn is None: addwn = [0]
|
---|
[2186] | 3872 | if rejwn is None: rejwn = []
|
---|
| 3873 | if clipthresh is None: clipthresh = 3.0
|
---|
| 3874 | if clipniter is None: clipniter = 0
|
---|
| 3875 | if edge is None: edge = (0,0)
|
---|
| 3876 | if threshold is None: threshold = 3
|
---|
| 3877 | if chan_avg_limit is None: chan_avg_limit = 1
|
---|
| 3878 | if plot is None: plot = False
|
---|
| 3879 | if getresidual is None: getresidual = True
|
---|
[2189] | 3880 | if showprogress is None: showprogress = True
|
---|
| 3881 | if minnrow is None: minnrow = 1000
|
---|
[2186] | 3882 | if outlog is None: outlog = False
|
---|
| 3883 | if blfile is None: blfile = ''
|
---|
[2641] | 3884 | if csvformat is None: csvformat = False
|
---|
[2767] | 3885 | if bltable is None: bltable = ''
|
---|
[2047] | 3886 |
|
---|
[2767] | 3887 | sapplyfft = 'true' if applyfft else 'false'
|
---|
| 3888 | fftinfo = ','.join([sapplyfft, fftmethod.lower(), str(fftthresh).lower()])
|
---|
[2641] | 3889 |
|
---|
[2767] | 3890 | scsvformat = 'T' if csvformat else 'F'
|
---|
| 3891 |
|
---|
[2277] | 3892 | #CURRENTLY, PLOT=true is UNAVAILABLE UNTIL sinusoidal fitting is implemented as a fitter method.
|
---|
[2767] | 3893 | workscan._auto_sinusoid_baseline(mask,
|
---|
| 3894 | fftinfo,
|
---|
[2349] | 3895 | workscan._parse_wn(addwn),
|
---|
| 3896 | workscan._parse_wn(rejwn),
|
---|
| 3897 | clipthresh, clipniter,
|
---|
| 3898 | normalise_edge_param(edge),
|
---|
| 3899 | threshold, chan_avg_limit,
|
---|
| 3900 | getresidual,
|
---|
| 3901 | pack_progress_params(showprogress,
|
---|
| 3902 | minnrow),
|
---|
[2767] | 3903 | outlog, scsvformat+blfile, bltable)
|
---|
[2047] | 3904 | workscan._add_history("auto_sinusoid_baseline", varlist)
|
---|
[2767] | 3905 |
|
---|
| 3906 | if bltable == '':
|
---|
| 3907 | if insitu:
|
---|
| 3908 | self._assign(workscan)
|
---|
| 3909 | else:
|
---|
| 3910 | return workscan
|
---|
[2047] | 3911 | else:
|
---|
[2767] | 3912 | if not insitu:
|
---|
| 3913 | return None
|
---|
[2047] | 3914 |
|
---|
| 3915 | except RuntimeError, e:
|
---|
[2186] | 3916 | raise_fitting_failure_exception(e)
|
---|
[2047] | 3917 |
|
---|
| 3918 | @asaplog_post_dec
|
---|
[2771] | 3919 | def cspline_baseline(self, mask=None, npiece=None, insitu=None,
|
---|
[2349] | 3920 | clipthresh=None, clipniter=None, plot=None,
|
---|
| 3921 | getresidual=None, showprogress=None, minnrow=None,
|
---|
[2767] | 3922 | outlog=None, blfile=None, csvformat=None,
|
---|
| 3923 | bltable=None):
|
---|
[1846] | 3924 | """\
|
---|
[2349] | 3925 | Return a scan which has been baselined (all rows) by cubic spline
|
---|
| 3926 | function (piecewise cubic polynomial).
|
---|
| 3927 |
|
---|
[513] | 3928 | Parameters:
|
---|
[2771] | 3929 | mask: An optional mask
|
---|
| 3930 | npiece: Number of pieces. (default is 2)
|
---|
[2189] | 3931 | insitu: If False a new scantable is returned.
|
---|
| 3932 | Otherwise, the scaling is done in-situ
|
---|
| 3933 | The default is taken from .asaprc (False)
|
---|
| 3934 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
|
---|
[2349] | 3935 | clipniter: maximum number of iteration of 'clipthresh'-sigma
|
---|
| 3936 | clipping (default is 0)
|
---|
[2189] | 3937 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
|
---|
| 3938 | plot the fit and the residual. In this each
|
---|
| 3939 | indivual fit has to be approved, by typing 'y'
|
---|
| 3940 | or 'n'
|
---|
| 3941 | getresidual: if False, returns best-fit values instead of
|
---|
| 3942 | residual. (default is True)
|
---|
| 3943 | showprogress: show progress status for large data.
|
---|
| 3944 | default is True.
|
---|
| 3945 | minnrow: minimum number of input spectra to show.
|
---|
| 3946 | default is 1000.
|
---|
| 3947 | outlog: Output the coefficients of the best-fit
|
---|
| 3948 | function to logger (default is False)
|
---|
| 3949 | blfile: Name of a text file in which the best-fit
|
---|
| 3950 | parameter values to be written
|
---|
| 3951 | (default is "": no file/logger output)
|
---|
[2641] | 3952 | csvformat: if True blfile is csv-formatted, default is False.
|
---|
[2767] | 3953 | bltable: name of a baseline table where fitting results
|
---|
| 3954 | (coefficients, rms, etc.) are to be written.
|
---|
| 3955 | if given, fitting results will NOT be output to
|
---|
| 3956 | scantable (insitu=True) or None will be
|
---|
| 3957 | returned (insitu=False).
|
---|
| 3958 | (default is "": no table output)
|
---|
[1846] | 3959 |
|
---|
[2012] | 3960 | Example:
|
---|
[2349] | 3961 | # return a scan baselined by a cubic spline consisting of 2 pieces
|
---|
| 3962 | # (i.e., 1 internal knot),
|
---|
[2012] | 3963 | # also with 3-sigma clipping, iteration up to 4 times
|
---|
| 3964 | bscan = scan.cspline_baseline(npiece=2,clipthresh=3.0,clipniter=4)
|
---|
[2081] | 3965 |
|
---|
| 3966 | Note:
|
---|
| 3967 | The best-fit parameter values output in logger and/or blfile are now
|
---|
| 3968 | based on specunit of 'channel'.
|
---|
[2012] | 3969 | """
|
---|
| 3970 |
|
---|
[2186] | 3971 | try:
|
---|
| 3972 | varlist = vars()
|
---|
| 3973 |
|
---|
| 3974 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 3975 | if insitu:
|
---|
| 3976 | workscan = self
|
---|
| 3977 | else:
|
---|
| 3978 | workscan = self.copy()
|
---|
[1855] | 3979 |
|
---|
[2410] | 3980 | if mask is None: mask = []
|
---|
[2189] | 3981 | if npiece is None: npiece = 2
|
---|
| 3982 | if clipthresh is None: clipthresh = 3.0
|
---|
| 3983 | if clipniter is None: clipniter = 0
|
---|
| 3984 | if plot is None: plot = False
|
---|
| 3985 | if getresidual is None: getresidual = True
|
---|
| 3986 | if showprogress is None: showprogress = True
|
---|
| 3987 | if minnrow is None: minnrow = 1000
|
---|
| 3988 | if outlog is None: outlog = False
|
---|
| 3989 | if blfile is None: blfile = ''
|
---|
[2767] | 3990 | if csvformat is None: csvformat = False
|
---|
| 3991 | if bltable is None: bltable = ''
|
---|
[1855] | 3992 |
|
---|
[2767] | 3993 | scsvformat = 'T' if csvformat else 'F'
|
---|
[2641] | 3994 |
|
---|
[2012] | 3995 | #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
|
---|
[2767] | 3996 | workscan._cspline_baseline(mask, npiece,
|
---|
| 3997 | clipthresh, clipniter,
|
---|
[2349] | 3998 | getresidual,
|
---|
| 3999 | pack_progress_params(showprogress,
|
---|
[2641] | 4000 | minnrow),
|
---|
[2767] | 4001 | outlog, scsvformat+blfile,
|
---|
| 4002 | bltable)
|
---|
[2012] | 4003 | workscan._add_history("cspline_baseline", varlist)
|
---|
[2767] | 4004 |
|
---|
| 4005 | if bltable == '':
|
---|
| 4006 | if insitu:
|
---|
| 4007 | self._assign(workscan)
|
---|
| 4008 | else:
|
---|
| 4009 | return workscan
|
---|
[2012] | 4010 | else:
|
---|
[2767] | 4011 | if not insitu:
|
---|
| 4012 | return None
|
---|
[2012] | 4013 |
|
---|
| 4014 | except RuntimeError, e:
|
---|
[2186] | 4015 | raise_fitting_failure_exception(e)
|
---|
[1855] | 4016 |
|
---|
[2186] | 4017 | @asaplog_post_dec
|
---|
[2771] | 4018 | def auto_cspline_baseline(self, mask=None, npiece=None, insitu=None,
|
---|
[2349] | 4019 | clipthresh=None, clipniter=None,
|
---|
| 4020 | edge=None, threshold=None, chan_avg_limit=None,
|
---|
| 4021 | getresidual=None, plot=None,
|
---|
| 4022 | showprogress=None, minnrow=None, outlog=None,
|
---|
[2767] | 4023 | blfile=None, csvformat=None, bltable=None):
|
---|
[2012] | 4024 | """\
|
---|
| 4025 | Return a scan which has been baselined (all rows) by cubic spline
|
---|
| 4026 | function (piecewise cubic polynomial).
|
---|
| 4027 | Spectral lines are detected first using linefinder and masked out
|
---|
| 4028 | to avoid them affecting the baseline solution.
|
---|
| 4029 |
|
---|
| 4030 | Parameters:
|
---|
[2771] | 4031 | mask: an optional mask retreived from scantable
|
---|
| 4032 | npiece: Number of pieces. (default is 2)
|
---|
[2189] | 4033 | insitu: if False a new scantable is returned.
|
---|
| 4034 | Otherwise, the scaling is done in-situ
|
---|
| 4035 | The default is taken from .asaprc (False)
|
---|
| 4036 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
|
---|
[2349] | 4037 | clipniter: maximum number of iteration of 'clipthresh'-sigma
|
---|
| 4038 | clipping (default is 0)
|
---|
[2189] | 4039 | edge: an optional number of channel to drop at
|
---|
| 4040 | the edge of spectrum. If only one value is
|
---|
| 4041 | specified, the same number will be dropped
|
---|
| 4042 | from both sides of the spectrum. Default
|
---|
| 4043 | is to keep all channels. Nested tuples
|
---|
| 4044 | represent individual edge selection for
|
---|
| 4045 | different IFs (a number of spectral channels
|
---|
| 4046 | can be different)
|
---|
| 4047 | threshold: the threshold used by line finder. It is
|
---|
| 4048 | better to keep it large as only strong lines
|
---|
| 4049 | affect the baseline solution.
|
---|
| 4050 | chan_avg_limit: a maximum number of consequtive spectral
|
---|
| 4051 | channels to average during the search of
|
---|
| 4052 | weak and broad lines. The default is no
|
---|
| 4053 | averaging (and no search for weak lines).
|
---|
| 4054 | If such lines can affect the fitted baseline
|
---|
| 4055 | (e.g. a high order polynomial is fitted),
|
---|
| 4056 | increase this parameter (usually values up
|
---|
| 4057 | to 8 are reasonable). Most users of this
|
---|
| 4058 | method should find the default value sufficient.
|
---|
| 4059 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
|
---|
| 4060 | plot the fit and the residual. In this each
|
---|
| 4061 | indivual fit has to be approved, by typing 'y'
|
---|
| 4062 | or 'n'
|
---|
| 4063 | getresidual: if False, returns best-fit values instead of
|
---|
| 4064 | residual. (default is True)
|
---|
| 4065 | showprogress: show progress status for large data.
|
---|
| 4066 | default is True.
|
---|
| 4067 | minnrow: minimum number of input spectra to show.
|
---|
| 4068 | default is 1000.
|
---|
| 4069 | outlog: Output the coefficients of the best-fit
|
---|
| 4070 | function to logger (default is False)
|
---|
| 4071 | blfile: Name of a text file in which the best-fit
|
---|
| 4072 | parameter values to be written
|
---|
| 4073 | (default is "": no file/logger output)
|
---|
[2641] | 4074 | csvformat: if True blfile is csv-formatted, default is False.
|
---|
[2767] | 4075 | bltable: name of a baseline table where fitting results
|
---|
| 4076 | (coefficients, rms, etc.) are to be written.
|
---|
| 4077 | if given, fitting results will NOT be output to
|
---|
| 4078 | scantable (insitu=True) or None will be
|
---|
| 4079 | returned (insitu=False).
|
---|
| 4080 | (default is "": no table output)
|
---|
[1846] | 4081 |
|
---|
[1907] | 4082 | Example:
|
---|
[2012] | 4083 | bscan = scan.auto_cspline_baseline(npiece=3, insitu=False)
|
---|
[2081] | 4084 |
|
---|
| 4085 | Note:
|
---|
| 4086 | The best-fit parameter values output in logger and/or blfile are now
|
---|
| 4087 | based on specunit of 'channel'.
|
---|
[2012] | 4088 | """
|
---|
[1846] | 4089 |
|
---|
[2186] | 4090 | try:
|
---|
| 4091 | varlist = vars()
|
---|
[2012] | 4092 |
|
---|
[2186] | 4093 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 4094 | if insitu:
|
---|
| 4095 | workscan = self
|
---|
[1391] | 4096 | else:
|
---|
[2186] | 4097 | workscan = self.copy()
|
---|
| 4098 |
|
---|
[2410] | 4099 | #if mask is None: mask = [True for i in xrange(workscan.nchan())]
|
---|
| 4100 | if mask is None: mask = []
|
---|
[2186] | 4101 | if npiece is None: npiece = 2
|
---|
| 4102 | if clipthresh is None: clipthresh = 3.0
|
---|
| 4103 | if clipniter is None: clipniter = 0
|
---|
| 4104 | if edge is None: edge = (0, 0)
|
---|
| 4105 | if threshold is None: threshold = 3
|
---|
| 4106 | if chan_avg_limit is None: chan_avg_limit = 1
|
---|
| 4107 | if plot is None: plot = False
|
---|
| 4108 | if getresidual is None: getresidual = True
|
---|
[2189] | 4109 | if showprogress is None: showprogress = True
|
---|
| 4110 | if minnrow is None: minnrow = 1000
|
---|
[2186] | 4111 | if outlog is None: outlog = False
|
---|
| 4112 | if blfile is None: blfile = ''
|
---|
[2641] | 4113 | if csvformat is None: csvformat = False
|
---|
[2767] | 4114 | if bltable is None: bltable = ''
|
---|
[1819] | 4115 |
|
---|
[2767] | 4116 | scsvformat = 'T' if csvformat else 'F'
|
---|
[2641] | 4117 |
|
---|
[2277] | 4118 | #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
|
---|
[2767] | 4119 | workscan._auto_cspline_baseline(mask, npiece,
|
---|
| 4120 | clipthresh, clipniter,
|
---|
[2269] | 4121 | normalise_edge_param(edge),
|
---|
| 4122 | threshold,
|
---|
| 4123 | chan_avg_limit, getresidual,
|
---|
| 4124 | pack_progress_params(showprogress,
|
---|
| 4125 | minnrow),
|
---|
[2767] | 4126 | outlog,
|
---|
| 4127 | scsvformat+blfile,
|
---|
| 4128 | bltable)
|
---|
[2012] | 4129 | workscan._add_history("auto_cspline_baseline", varlist)
|
---|
[2767] | 4130 |
|
---|
| 4131 | if bltable == '':
|
---|
| 4132 | if insitu:
|
---|
| 4133 | self._assign(workscan)
|
---|
| 4134 | else:
|
---|
| 4135 | return workscan
|
---|
[1856] | 4136 | else:
|
---|
[2767] | 4137 | if not insitu:
|
---|
| 4138 | return None
|
---|
[2012] | 4139 |
|
---|
| 4140 | except RuntimeError, e:
|
---|
[2186] | 4141 | raise_fitting_failure_exception(e)
|
---|
[513] | 4142 |
|
---|
[1931] | 4143 | @asaplog_post_dec
|
---|
[2771] | 4144 | def chebyshev_baseline(self, mask=None, order=None, insitu=None,
|
---|
[2645] | 4145 | clipthresh=None, clipniter=None, plot=None,
|
---|
| 4146 | getresidual=None, showprogress=None, minnrow=None,
|
---|
[2767] | 4147 | outlog=None, blfile=None, csvformat=None,
|
---|
| 4148 | bltable=None):
|
---|
[2645] | 4149 | """\
|
---|
| 4150 | Return a scan which has been baselined (all rows) by Chebyshev polynomials.
|
---|
| 4151 |
|
---|
| 4152 | Parameters:
|
---|
[2771] | 4153 | mask: An optional mask
|
---|
| 4154 | order: the maximum order of Chebyshev polynomial (default is 5)
|
---|
[2645] | 4155 | insitu: If False a new scantable is returned.
|
---|
| 4156 | Otherwise, the scaling is done in-situ
|
---|
| 4157 | The default is taken from .asaprc (False)
|
---|
| 4158 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
|
---|
| 4159 | clipniter: maximum number of iteration of 'clipthresh'-sigma
|
---|
| 4160 | clipping (default is 0)
|
---|
| 4161 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
|
---|
| 4162 | plot the fit and the residual. In this each
|
---|
| 4163 | indivual fit has to be approved, by typing 'y'
|
---|
| 4164 | or 'n'
|
---|
| 4165 | getresidual: if False, returns best-fit values instead of
|
---|
| 4166 | residual. (default is True)
|
---|
| 4167 | showprogress: show progress status for large data.
|
---|
| 4168 | default is True.
|
---|
| 4169 | minnrow: minimum number of input spectra to show.
|
---|
| 4170 | default is 1000.
|
---|
| 4171 | outlog: Output the coefficients of the best-fit
|
---|
| 4172 | function to logger (default is False)
|
---|
| 4173 | blfile: Name of a text file in which the best-fit
|
---|
| 4174 | parameter values to be written
|
---|
| 4175 | (default is "": no file/logger output)
|
---|
| 4176 | csvformat: if True blfile is csv-formatted, default is False.
|
---|
[2767] | 4177 | bltable: name of a baseline table where fitting results
|
---|
| 4178 | (coefficients, rms, etc.) are to be written.
|
---|
| 4179 | if given, fitting results will NOT be output to
|
---|
| 4180 | scantable (insitu=True) or None will be
|
---|
| 4181 | returned (insitu=False).
|
---|
| 4182 | (default is "": no table output)
|
---|
[2645] | 4183 |
|
---|
| 4184 | Example:
|
---|
| 4185 | # return a scan baselined by a cubic spline consisting of 2 pieces
|
---|
| 4186 | # (i.e., 1 internal knot),
|
---|
| 4187 | # also with 3-sigma clipping, iteration up to 4 times
|
---|
| 4188 | bscan = scan.cspline_baseline(npiece=2,clipthresh=3.0,clipniter=4)
|
---|
| 4189 |
|
---|
| 4190 | Note:
|
---|
| 4191 | The best-fit parameter values output in logger and/or blfile are now
|
---|
| 4192 | based on specunit of 'channel'.
|
---|
| 4193 | """
|
---|
| 4194 |
|
---|
| 4195 | try:
|
---|
| 4196 | varlist = vars()
|
---|
| 4197 |
|
---|
| 4198 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 4199 | if insitu:
|
---|
| 4200 | workscan = self
|
---|
| 4201 | else:
|
---|
| 4202 | workscan = self.copy()
|
---|
| 4203 |
|
---|
| 4204 | if mask is None: mask = []
|
---|
| 4205 | if order is None: order = 5
|
---|
| 4206 | if clipthresh is None: clipthresh = 3.0
|
---|
| 4207 | if clipniter is None: clipniter = 0
|
---|
| 4208 | if plot is None: plot = False
|
---|
| 4209 | if getresidual is None: getresidual = True
|
---|
| 4210 | if showprogress is None: showprogress = True
|
---|
| 4211 | if minnrow is None: minnrow = 1000
|
---|
| 4212 | if outlog is None: outlog = False
|
---|
| 4213 | if blfile is None: blfile = ''
|
---|
[2767] | 4214 | if csvformat is None: csvformat = False
|
---|
| 4215 | if bltable is None: bltable = ''
|
---|
[2645] | 4216 |
|
---|
[2767] | 4217 | scsvformat = 'T' if csvformat else 'F'
|
---|
[2645] | 4218 |
|
---|
| 4219 | #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
|
---|
[2767] | 4220 | workscan._chebyshev_baseline(mask, order,
|
---|
| 4221 | clipthresh, clipniter,
|
---|
[2645] | 4222 | getresidual,
|
---|
| 4223 | pack_progress_params(showprogress,
|
---|
| 4224 | minnrow),
|
---|
[2767] | 4225 | outlog, scsvformat+blfile,
|
---|
| 4226 | bltable)
|
---|
[2645] | 4227 | workscan._add_history("chebyshev_baseline", varlist)
|
---|
[2767] | 4228 |
|
---|
| 4229 | if bltable == '':
|
---|
| 4230 | if insitu:
|
---|
| 4231 | self._assign(workscan)
|
---|
| 4232 | else:
|
---|
| 4233 | return workscan
|
---|
[2645] | 4234 | else:
|
---|
[2767] | 4235 | if not insitu:
|
---|
| 4236 | return None
|
---|
[2645] | 4237 |
|
---|
| 4238 | except RuntimeError, e:
|
---|
| 4239 | raise_fitting_failure_exception(e)
|
---|
| 4240 |
|
---|
| 4241 | @asaplog_post_dec
|
---|
[2771] | 4242 | def auto_chebyshev_baseline(self, mask=None, order=None, insitu=None,
|
---|
[2645] | 4243 | clipthresh=None, clipniter=None,
|
---|
| 4244 | edge=None, threshold=None, chan_avg_limit=None,
|
---|
| 4245 | getresidual=None, plot=None,
|
---|
| 4246 | showprogress=None, minnrow=None, outlog=None,
|
---|
[2767] | 4247 | blfile=None, csvformat=None, bltable=None):
|
---|
[2645] | 4248 | """\
|
---|
| 4249 | Return a scan which has been baselined (all rows) by Chebyshev polynomials.
|
---|
| 4250 | Spectral lines are detected first using linefinder and masked out
|
---|
| 4251 | to avoid them affecting the baseline solution.
|
---|
| 4252 |
|
---|
| 4253 | Parameters:
|
---|
[2771] | 4254 | mask: an optional mask retreived from scantable
|
---|
| 4255 | order: the maximum order of Chebyshev polynomial (default is 5)
|
---|
[2645] | 4256 | insitu: if False a new scantable is returned.
|
---|
| 4257 | Otherwise, the scaling is done in-situ
|
---|
| 4258 | The default is taken from .asaprc (False)
|
---|
| 4259 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
|
---|
| 4260 | clipniter: maximum number of iteration of 'clipthresh'-sigma
|
---|
| 4261 | clipping (default is 0)
|
---|
| 4262 | edge: an optional number of channel to drop at
|
---|
| 4263 | the edge of spectrum. If only one value is
|
---|
| 4264 | specified, the same number will be dropped
|
---|
| 4265 | from both sides of the spectrum. Default
|
---|
| 4266 | is to keep all channels. Nested tuples
|
---|
| 4267 | represent individual edge selection for
|
---|
| 4268 | different IFs (a number of spectral channels
|
---|
| 4269 | can be different)
|
---|
| 4270 | threshold: the threshold used by line finder. It is
|
---|
| 4271 | better to keep it large as only strong lines
|
---|
| 4272 | affect the baseline solution.
|
---|
| 4273 | chan_avg_limit: a maximum number of consequtive spectral
|
---|
| 4274 | channels to average during the search of
|
---|
| 4275 | weak and broad lines. The default is no
|
---|
| 4276 | averaging (and no search for weak lines).
|
---|
| 4277 | If such lines can affect the fitted baseline
|
---|
| 4278 | (e.g. a high order polynomial is fitted),
|
---|
| 4279 | increase this parameter (usually values up
|
---|
| 4280 | to 8 are reasonable). Most users of this
|
---|
| 4281 | method should find the default value sufficient.
|
---|
| 4282 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
|
---|
| 4283 | plot the fit and the residual. In this each
|
---|
| 4284 | indivual fit has to be approved, by typing 'y'
|
---|
| 4285 | or 'n'
|
---|
| 4286 | getresidual: if False, returns best-fit values instead of
|
---|
| 4287 | residual. (default is True)
|
---|
| 4288 | showprogress: show progress status for large data.
|
---|
| 4289 | default is True.
|
---|
| 4290 | minnrow: minimum number of input spectra to show.
|
---|
| 4291 | default is 1000.
|
---|
| 4292 | outlog: Output the coefficients of the best-fit
|
---|
| 4293 | function to logger (default is False)
|
---|
| 4294 | blfile: Name of a text file in which the best-fit
|
---|
| 4295 | parameter values to be written
|
---|
| 4296 | (default is "": no file/logger output)
|
---|
| 4297 | csvformat: if True blfile is csv-formatted, default is False.
|
---|
[2767] | 4298 | bltable: name of a baseline table where fitting results
|
---|
| 4299 | (coefficients, rms, etc.) are to be written.
|
---|
| 4300 | if given, fitting results will NOT be output to
|
---|
| 4301 | scantable (insitu=True) or None will be
|
---|
| 4302 | returned (insitu=False).
|
---|
| 4303 | (default is "": no table output)
|
---|
[2645] | 4304 |
|
---|
| 4305 | Example:
|
---|
| 4306 | bscan = scan.auto_cspline_baseline(npiece=3, insitu=False)
|
---|
| 4307 |
|
---|
| 4308 | Note:
|
---|
| 4309 | The best-fit parameter values output in logger and/or blfile are now
|
---|
| 4310 | based on specunit of 'channel'.
|
---|
| 4311 | """
|
---|
| 4312 |
|
---|
| 4313 | try:
|
---|
| 4314 | varlist = vars()
|
---|
| 4315 |
|
---|
| 4316 | if insitu is None: insitu = rcParams['insitu']
|
---|
| 4317 | if insitu:
|
---|
| 4318 | workscan = self
|
---|
| 4319 | else:
|
---|
| 4320 | workscan = self.copy()
|
---|
| 4321 |
|
---|
| 4322 | if mask is None: mask = []
|
---|
| 4323 | if order is None: order = 5
|
---|
| 4324 | if clipthresh is None: clipthresh = 3.0
|
---|
| 4325 | if clipniter is None: clipniter = 0
|
---|
| 4326 | if edge is None: edge = (0, 0)
|
---|
| 4327 | if threshold is None: threshold = 3
|
---|
| 4328 | if chan_avg_limit is None: chan_avg_limit = 1
|
---|
| 4329 | if plot is None: plot = False
|
---|
| 4330 | if getresidual is None: getresidual = True
|
---|
| 4331 | if showprogress is None: showprogress = True
|
---|
| 4332 | if minnrow is None: minnrow = 1000
|
---|
| 4333 | if outlog is None: outlog = False
|
---|
| 4334 | if blfile is None: blfile = ''
|
---|
| 4335 | if csvformat is None: csvformat = False
|
---|
[2767] | 4336 | if bltable is None: bltable = ''
|
---|
[2645] | 4337 |
|
---|
[2767] | 4338 | scsvformat = 'T' if csvformat else 'F'
|
---|
[2645] | 4339 |
|
---|
| 4340 | #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
|
---|
[2767] | 4341 | workscan._auto_chebyshev_baseline(mask, order,
|
---|
| 4342 | clipthresh, clipniter,
|
---|
[2645] | 4343 | normalise_edge_param(edge),
|
---|
| 4344 | threshold,
|
---|
| 4345 | chan_avg_limit, getresidual,
|
---|
| 4346 | pack_progress_params(showprogress,
|
---|
| 4347 | minnrow),
|
---|
[2767] | 4348 | outlog, scsvformat+blfile,
|
---|
| 4349 | bltable)
|
---|
[2645] | 4350 | workscan._add_history("auto_chebyshev_baseline", varlist)
|
---|
[2767] | 4351 |
|
---|
| 4352 | if bltable == '':
|
---|
| 4353 | if insitu:
|
---|
| 4354 | self._assign(workscan)
|
---|
| 4355 | else:
|
---|
| 4356 | return workscan
|
---|
[2645] | 4357 | else:
|
---|
[2767] | 4358 | if not insitu:
|
---|
| 4359 | return None
|
---|
[2645] | 4360 |
|
---|
| 4361 | except RuntimeError, e:
|
---|
| 4362 | raise_fitting_failure_exception(e)
|
---|
| 4363 |
|
---|
| 4364 | @asaplog_post_dec
|
---|
[2771] | 4365 | def poly_baseline(self, mask=None, order=None, insitu=None,
|
---|
[2767] | 4366 | clipthresh=None, clipniter=None, plot=None,
|
---|
[2269] | 4367 | getresidual=None, showprogress=None, minnrow=None,
|
---|
[2767] | 4368 | outlog=None, blfile=None, csvformat=None,
|
---|
| 4369 | bltable=None):
|
---|
[1907] | 4370 | """\
|
---|
| 4371 | Return a scan which has been baselined (all rows) by a polynomial.
|
---|
| 4372 | Parameters:
|
---|
[2771] | 4373 | mask: an optional mask
|
---|
| 4374 | order: the order of the polynomial (default is 0)
|
---|
[2189] | 4375 | insitu: if False a new scantable is returned.
|
---|
| 4376 | Otherwise, the scaling is done in-situ
|
---|
| 4377 | The default is taken from .asaprc (False)
|
---|
[2767] | 4378 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
|
---|
| 4379 | clipniter: maximum number of iteration of 'clipthresh'-sigma
|
---|
| 4380 | clipping (default is 0)
|
---|
[2189] | 4381 | plot: plot the fit and the residual. In this each
|
---|
| 4382 | indivual fit has to be approved, by typing 'y'
|
---|
| 4383 | or 'n'
|
---|
| 4384 | getresidual: if False, returns best-fit values instead of
|
---|
| 4385 | residual. (default is True)
|
---|
| 4386 | showprogress: show progress status for large data.
|
---|
| 4387 | default is True.
|
---|
| 4388 | minnrow: minimum number of input spectra to show.
|
---|
| 4389 | default is 1000.
|
---|
| 4390 | outlog: Output the coefficients of the best-fit
|
---|
| 4391 | function to logger (default is False)
|
---|
| 4392 | blfile: Name of a text file in which the best-fit
|
---|
| 4393 | parameter values to be written
|
---|
| 4394 | (default is "": no file/logger output)
|
---|
[2641] | 4395 | csvformat: if True blfile is csv-formatted, default is False.
|
---|
[2767] | 4396 | bltable: name of a baseline table where fitting results
|
---|
| 4397 | (coefficients, rms, etc.) are to be written.
|
---|
| 4398 | if given, fitting results will NOT be output to
|
---|
| 4399 | scantable (insitu=True) or None will be
|
---|
| 4400 | returned (insitu=False).
|
---|
| 4401 | (default is "": no table output)
|
---|
[2012] | 4402 |
|
---|
[1907] | 4403 | Example:
|
---|
| 4404 | # return a scan baselined by a third order polynomial,
|
---|
| 4405 | # not using a mask
|
---|
| 4406 | bscan = scan.poly_baseline(order=3)
|
---|
| 4407 | """
|
---|
[1931] | 4408 |
|
---|
[2186] | 4409 | try:
|
---|
| 4410 | varlist = vars()
|
---|
[1931] | 4411 |
|
---|
[2269] | 4412 | if insitu is None:
|
---|
| 4413 | insitu = rcParams["insitu"]
|
---|
[2186] | 4414 | if insitu:
|
---|
| 4415 | workscan = self
|
---|
| 4416 | else:
|
---|
| 4417 | workscan = self.copy()
|
---|
[1907] | 4418 |
|
---|
[2410] | 4419 | if mask is None: mask = []
|
---|
[2189] | 4420 | if order is None: order = 0
|
---|
[2767] | 4421 | if clipthresh is None: clipthresh = 3.0
|
---|
| 4422 | if clipniter is None: clipniter = 0
|
---|
[2189] | 4423 | if plot is None: plot = False
|
---|
| 4424 | if getresidual is None: getresidual = True
|
---|
| 4425 | if showprogress is None: showprogress = True
|
---|
| 4426 | if minnrow is None: minnrow = 1000
|
---|
| 4427 | if outlog is None: outlog = False
|
---|
[2767] | 4428 | if blfile is None: blfile = ''
|
---|
[2641] | 4429 | if csvformat is None: csvformat = False
|
---|
[2767] | 4430 | if bltable is None: bltable = ''
|
---|
[1907] | 4431 |
|
---|
[2767] | 4432 | scsvformat = 'T' if csvformat else 'F'
|
---|
[2641] | 4433 |
|
---|
[2012] | 4434 | if plot:
|
---|
[2269] | 4435 | outblfile = (blfile != "") and \
|
---|
[2349] | 4436 | os.path.exists(os.path.expanduser(
|
---|
| 4437 | os.path.expandvars(blfile))
|
---|
| 4438 | )
|
---|
[2269] | 4439 | if outblfile:
|
---|
| 4440 | blf = open(blfile, "a")
|
---|
[2012] | 4441 |
|
---|
[1907] | 4442 | f = fitter()
|
---|
| 4443 | f.set_function(lpoly=order)
|
---|
[2186] | 4444 |
|
---|
| 4445 | rows = xrange(workscan.nrow())
|
---|
| 4446 | #if len(rows) > 0: workscan._init_blinfo()
|
---|
[2610] | 4447 |
|
---|
| 4448 | action = "H"
|
---|
[1907] | 4449 | for r in rows:
|
---|
| 4450 | f.x = workscan._getabcissa(r)
|
---|
| 4451 | f.y = workscan._getspectrum(r)
|
---|
[2541] | 4452 | if mask:
|
---|
| 4453 | f.mask = mask_and(mask, workscan._getmask(r)) # (CAS-1434)
|
---|
| 4454 | else: # mask=None
|
---|
| 4455 | f.mask = workscan._getmask(r)
|
---|
| 4456 |
|
---|
[1907] | 4457 | f.data = None
|
---|
| 4458 | f.fit()
|
---|
[2541] | 4459 |
|
---|
[2610] | 4460 | if action != "Y": # skip plotting when accepting all
|
---|
| 4461 | f.plot(residual=True)
|
---|
| 4462 | #accept_fit = raw_input("Accept fit ( [y]/n ): ")
|
---|
| 4463 | #if accept_fit.upper() == "N":
|
---|
| 4464 | # #workscan._append_blinfo(None, None, None)
|
---|
| 4465 | # continue
|
---|
| 4466 | accept_fit = self._get_verify_action("Accept fit?",action)
|
---|
| 4467 | if r == 0: action = None
|
---|
[1907] | 4468 | if accept_fit.upper() == "N":
|
---|
| 4469 | continue
|
---|
[2610] | 4470 | elif accept_fit.upper() == "R":
|
---|
| 4471 | break
|
---|
| 4472 | elif accept_fit.upper() == "A":
|
---|
| 4473 | action = "Y"
|
---|
[2012] | 4474 |
|
---|
| 4475 | blpars = f.get_parameters()
|
---|
| 4476 | masklist = workscan.get_masklist(f.mask, row=r, silent=True)
|
---|
| 4477 | #workscan._append_blinfo(blpars, masklist, f.mask)
|
---|
[2269] | 4478 | workscan._setspectrum((f.fitter.getresidual()
|
---|
| 4479 | if getresidual else f.fitter.getfit()), r)
|
---|
[1907] | 4480 |
|
---|
[2012] | 4481 | if outblfile:
|
---|
| 4482 | rms = workscan.get_rms(f.mask, r)
|
---|
[2269] | 4483 | dataout = \
|
---|
| 4484 | workscan.format_blparams_row(blpars["params"],
|
---|
| 4485 | blpars["fixed"],
|
---|
| 4486 | rms, str(masklist),
|
---|
[2641] | 4487 | r, True, csvformat)
|
---|
[2012] | 4488 | blf.write(dataout)
|
---|
| 4489 |
|
---|
[1907] | 4490 | f._p.unmap()
|
---|
| 4491 | f._p = None
|
---|
[2012] | 4492 |
|
---|
[2349] | 4493 | if outblfile:
|
---|
| 4494 | blf.close()
|
---|
[1907] | 4495 | else:
|
---|
[2767] | 4496 | workscan._poly_baseline(mask, order,
|
---|
| 4497 | clipthresh, clipniter, #
|
---|
| 4498 | getresidual,
|
---|
[2269] | 4499 | pack_progress_params(showprogress,
|
---|
| 4500 | minnrow),
|
---|
[2767] | 4501 | outlog, scsvformat+blfile,
|
---|
| 4502 | bltable) #
|
---|
[1907] | 4503 |
|
---|
| 4504 | workscan._add_history("poly_baseline", varlist)
|
---|
| 4505 |
|
---|
| 4506 | if insitu:
|
---|
| 4507 | self._assign(workscan)
|
---|
| 4508 | else:
|
---|
| 4509 | return workscan
|
---|
| 4510 |
|
---|
[1919] | 4511 | except RuntimeError, e:
|
---|
[2186] | 4512 | raise_fitting_failure_exception(e)
|
---|
[1907] | 4513 |
|
---|
[2186] | 4514 | @asaplog_post_dec
|
---|
[2771] | 4515 | def auto_poly_baseline(self, mask=None, order=None, insitu=None,
|
---|
[2767] | 4516 | clipthresh=None, clipniter=None,
|
---|
| 4517 | edge=None, threshold=None, chan_avg_limit=None,
|
---|
| 4518 | getresidual=None, plot=None,
|
---|
| 4519 | showprogress=None, minnrow=None, outlog=None,
|
---|
| 4520 | blfile=None, csvformat=None, bltable=None):
|
---|
[1846] | 4521 | """\
|
---|
[1931] | 4522 | Return a scan which has been baselined (all rows) by a polynomial.
|
---|
[880] | 4523 | Spectral lines are detected first using linefinder and masked out
|
---|
| 4524 | to avoid them affecting the baseline solution.
|
---|
| 4525 |
|
---|
| 4526 | Parameters:
|
---|
[2771] | 4527 | mask: an optional mask retreived from scantable
|
---|
| 4528 | order: the order of the polynomial (default is 0)
|
---|
[2189] | 4529 | insitu: if False a new scantable is returned.
|
---|
| 4530 | Otherwise, the scaling is done in-situ
|
---|
| 4531 | The default is taken from .asaprc (False)
|
---|
[2767] | 4532 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
|
---|
| 4533 | clipniter: maximum number of iteration of 'clipthresh'-sigma
|
---|
| 4534 | clipping (default is 0)
|
---|
[2189] | 4535 | edge: an optional number of channel to drop at
|
---|
| 4536 | the edge of spectrum. If only one value is
|
---|
| 4537 | specified, the same number will be dropped
|
---|
| 4538 | from both sides of the spectrum. Default
|
---|
| 4539 | is to keep all channels. Nested tuples
|
---|
| 4540 | represent individual edge selection for
|
---|
| 4541 | different IFs (a number of spectral channels
|
---|
| 4542 | can be different)
|
---|
| 4543 | threshold: the threshold used by line finder. It is
|
---|
| 4544 | better to keep it large as only strong lines
|
---|
| 4545 | affect the baseline solution.
|
---|
| 4546 | chan_avg_limit: a maximum number of consequtive spectral
|
---|
| 4547 | channels to average during the search of
|
---|
| 4548 | weak and broad lines. The default is no
|
---|
| 4549 | averaging (and no search for weak lines).
|
---|
| 4550 | If such lines can affect the fitted baseline
|
---|
| 4551 | (e.g. a high order polynomial is fitted),
|
---|
| 4552 | increase this parameter (usually values up
|
---|
| 4553 | to 8 are reasonable). Most users of this
|
---|
| 4554 | method should find the default value sufficient.
|
---|
| 4555 | plot: plot the fit and the residual. In this each
|
---|
| 4556 | indivual fit has to be approved, by typing 'y'
|
---|
| 4557 | or 'n'
|
---|
| 4558 | getresidual: if False, returns best-fit values instead of
|
---|
| 4559 | residual. (default is True)
|
---|
| 4560 | showprogress: show progress status for large data.
|
---|
| 4561 | default is True.
|
---|
| 4562 | minnrow: minimum number of input spectra to show.
|
---|
| 4563 | default is 1000.
|
---|
| 4564 | outlog: Output the coefficients of the best-fit
|
---|
| 4565 | function to logger (default is False)
|
---|
| 4566 | blfile: Name of a text file in which the best-fit
|
---|
| 4567 | parameter values to be written
|
---|
| 4568 | (default is "": no file/logger output)
|
---|
[2641] | 4569 | csvformat: if True blfile is csv-formatted, default is False.
|
---|
[2767] | 4570 | bltable: name of a baseline table where fitting results
|
---|
| 4571 | (coefficients, rms, etc.) are to be written.
|
---|
| 4572 | if given, fitting results will NOT be output to
|
---|
| 4573 | scantable (insitu=True) or None will be
|
---|
| 4574 | returned (insitu=False).
|
---|
| 4575 | (default is "": no table output)
|
---|
[1846] | 4576 |
|
---|
[2012] | 4577 | Example:
|
---|
| 4578 | bscan = scan.auto_poly_baseline(order=7, insitu=False)
|
---|
| 4579 | """
|
---|
[880] | 4580 |
|
---|
[2186] | 4581 | try:
|
---|
| 4582 | varlist = vars()
|
---|
[1846] | 4583 |
|
---|
[2269] | 4584 | if insitu is None:
|
---|
| 4585 | insitu = rcParams['insitu']
|
---|
[2186] | 4586 | if insitu:
|
---|
| 4587 | workscan = self
|
---|
| 4588 | else:
|
---|
| 4589 | workscan = self.copy()
|
---|
[1846] | 4590 |
|
---|
[2410] | 4591 | if mask is None: mask = []
|
---|
[2186] | 4592 | if order is None: order = 0
|
---|
[2767] | 4593 | if clipthresh is None: clipthresh = 3.0
|
---|
| 4594 | if clipniter is None: clipniter = 0
|
---|
[2186] | 4595 | if edge is None: edge = (0, 0)
|
---|
| 4596 | if threshold is None: threshold = 3
|
---|
| 4597 | if chan_avg_limit is None: chan_avg_limit = 1
|
---|
| 4598 | if plot is None: plot = False
|
---|
| 4599 | if getresidual is None: getresidual = True
|
---|
[2189] | 4600 | if showprogress is None: showprogress = True
|
---|
| 4601 | if minnrow is None: minnrow = 1000
|
---|
[2186] | 4602 | if outlog is None: outlog = False
|
---|
| 4603 | if blfile is None: blfile = ''
|
---|
[2641] | 4604 | if csvformat is None: csvformat = False
|
---|
[2767] | 4605 | if bltable is None: bltable = ''
|
---|
[1846] | 4606 |
|
---|
[2767] | 4607 | scsvformat = 'T' if csvformat else 'F'
|
---|
[2641] | 4608 |
|
---|
[2186] | 4609 | edge = normalise_edge_param(edge)
|
---|
[880] | 4610 |
|
---|
[2012] | 4611 | if plot:
|
---|
[2269] | 4612 | outblfile = (blfile != "") and \
|
---|
| 4613 | os.path.exists(os.path.expanduser(os.path.expandvars(blfile)))
|
---|
[2012] | 4614 | if outblfile: blf = open(blfile, "a")
|
---|
| 4615 |
|
---|
[2186] | 4616 | from asap.asaplinefind import linefinder
|
---|
[2012] | 4617 | fl = linefinder()
|
---|
[2269] | 4618 | fl.set_options(threshold=threshold, avg_limit=chan_avg_limit)
|
---|
[2012] | 4619 | fl.set_scan(workscan)
|
---|
[2186] | 4620 |
|
---|
[2012] | 4621 | f = fitter()
|
---|
| 4622 | f.set_function(lpoly=order)
|
---|
[880] | 4623 |
|
---|
[2186] | 4624 | rows = xrange(workscan.nrow())
|
---|
| 4625 | #if len(rows) > 0: workscan._init_blinfo()
|
---|
[2610] | 4626 |
|
---|
| 4627 | action = "H"
|
---|
[2012] | 4628 | for r in rows:
|
---|
[2186] | 4629 | idx = 2*workscan.getif(r)
|
---|
[2541] | 4630 | if mask:
|
---|
| 4631 | msk = mask_and(mask, workscan._getmask(r)) # (CAS-1434)
|
---|
| 4632 | else: # mask=None
|
---|
| 4633 | msk = workscan._getmask(r)
|
---|
| 4634 | fl.find_lines(r, msk, edge[idx:idx+2])
|
---|
[907] | 4635 |
|
---|
[2012] | 4636 | f.x = workscan._getabcissa(r)
|
---|
| 4637 | f.y = workscan._getspectrum(r)
|
---|
| 4638 | f.mask = fl.get_mask()
|
---|
| 4639 | f.data = None
|
---|
| 4640 | f.fit()
|
---|
| 4641 |
|
---|
[2610] | 4642 | if action != "Y": # skip plotting when accepting all
|
---|
| 4643 | f.plot(residual=True)
|
---|
| 4644 | #accept_fit = raw_input("Accept fit ( [y]/n ): ")
|
---|
| 4645 | accept_fit = self._get_verify_action("Accept fit?",action)
|
---|
| 4646 | if r == 0: action = None
|
---|
[2012] | 4647 | if accept_fit.upper() == "N":
|
---|
| 4648 | #workscan._append_blinfo(None, None, None)
|
---|
| 4649 | continue
|
---|
[2610] | 4650 | elif accept_fit.upper() == "R":
|
---|
| 4651 | break
|
---|
| 4652 | elif accept_fit.upper() == "A":
|
---|
| 4653 | action = "Y"
|
---|
[2012] | 4654 |
|
---|
| 4655 | blpars = f.get_parameters()
|
---|
| 4656 | masklist = workscan.get_masklist(f.mask, row=r, silent=True)
|
---|
| 4657 | #workscan._append_blinfo(blpars, masklist, f.mask)
|
---|
[2349] | 4658 | workscan._setspectrum(
|
---|
| 4659 | (f.fitter.getresidual() if getresidual
|
---|
| 4660 | else f.fitter.getfit()), r
|
---|
| 4661 | )
|
---|
[2012] | 4662 |
|
---|
| 4663 | if outblfile:
|
---|
| 4664 | rms = workscan.get_rms(f.mask, r)
|
---|
[2269] | 4665 | dataout = \
|
---|
| 4666 | workscan.format_blparams_row(blpars["params"],
|
---|
| 4667 | blpars["fixed"],
|
---|
| 4668 | rms, str(masklist),
|
---|
[2641] | 4669 | r, True, csvformat)
|
---|
[2012] | 4670 | blf.write(dataout)
|
---|
| 4671 |
|
---|
| 4672 | f._p.unmap()
|
---|
| 4673 | f._p = None
|
---|
| 4674 |
|
---|
| 4675 | if outblfile: blf.close()
|
---|
| 4676 | else:
|
---|
[2767] | 4677 | workscan._auto_poly_baseline(mask, order,
|
---|
| 4678 | clipthresh, clipniter,
|
---|
| 4679 | edge, threshold,
|
---|
[2269] | 4680 | chan_avg_limit, getresidual,
|
---|
| 4681 | pack_progress_params(showprogress,
|
---|
| 4682 | minnrow),
|
---|
[2767] | 4683 | outlog, scsvformat+blfile,
|
---|
| 4684 | bltable)
|
---|
| 4685 | workscan._add_history("auto_poly_baseline", varlist)
|
---|
[2012] | 4686 |
|
---|
[2767] | 4687 | if bltable == '':
|
---|
| 4688 | if insitu:
|
---|
| 4689 | self._assign(workscan)
|
---|
| 4690 | else:
|
---|
| 4691 | return workscan
|
---|
[2012] | 4692 | else:
|
---|
[2767] | 4693 | if not insitu:
|
---|
| 4694 | return None
|
---|
[2012] | 4695 |
|
---|
| 4696 | except RuntimeError, e:
|
---|
[2186] | 4697 | raise_fitting_failure_exception(e)
|
---|
[2012] | 4698 |
|
---|
| 4699 | def _init_blinfo(self):
|
---|
| 4700 | """\
|
---|
| 4701 | Initialise the following three auxiliary members:
|
---|
| 4702 | blpars : parameters of the best-fit baseline,
|
---|
| 4703 | masklists : mask data (edge positions of masked channels) and
|
---|
| 4704 | actualmask : mask data (in boolean list),
|
---|
| 4705 | to keep for use later (including output to logger/text files).
|
---|
| 4706 | Used by poly_baseline() and auto_poly_baseline() in case of
|
---|
| 4707 | 'plot=True'.
|
---|
| 4708 | """
|
---|
| 4709 | self.blpars = []
|
---|
| 4710 | self.masklists = []
|
---|
| 4711 | self.actualmask = []
|
---|
| 4712 | return
|
---|
[880] | 4713 |
|
---|
[2012] | 4714 | def _append_blinfo(self, data_blpars, data_masklists, data_actualmask):
|
---|
| 4715 | """\
|
---|
| 4716 | Append baseline-fitting related info to blpars, masklist and
|
---|
| 4717 | actualmask.
|
---|
| 4718 | """
|
---|
| 4719 | self.blpars.append(data_blpars)
|
---|
| 4720 | self.masklists.append(data_masklists)
|
---|
| 4721 | self.actualmask.append(data_actualmask)
|
---|
| 4722 | return
|
---|
| 4723 |
|
---|
[1862] | 4724 | @asaplog_post_dec
|
---|
[914] | 4725 | def rotate_linpolphase(self, angle):
|
---|
[1846] | 4726 | """\
|
---|
[914] | 4727 | Rotate the phase of the complex polarization O=Q+iU correlation.
|
---|
| 4728 | This is always done in situ in the raw data. So if you call this
|
---|
| 4729 | function more than once then each call rotates the phase further.
|
---|
[1846] | 4730 |
|
---|
[914] | 4731 | Parameters:
|
---|
[1846] | 4732 |
|
---|
[914] | 4733 | angle: The angle (degrees) to rotate (add) by.
|
---|
[1846] | 4734 |
|
---|
| 4735 | Example::
|
---|
| 4736 |
|
---|
[914] | 4737 | scan.rotate_linpolphase(2.3)
|
---|
[1846] | 4738 |
|
---|
[914] | 4739 | """
|
---|
| 4740 | varlist = vars()
|
---|
[936] | 4741 | self._math._rotate_linpolphase(self, angle)
|
---|
[914] | 4742 | self._add_history("rotate_linpolphase", varlist)
|
---|
| 4743 | return
|
---|
[710] | 4744 |
|
---|
[1862] | 4745 | @asaplog_post_dec
|
---|
[914] | 4746 | def rotate_xyphase(self, angle):
|
---|
[1846] | 4747 | """\
|
---|
[914] | 4748 | Rotate the phase of the XY correlation. This is always done in situ
|
---|
| 4749 | in the data. So if you call this function more than once
|
---|
| 4750 | then each call rotates the phase further.
|
---|
[1846] | 4751 |
|
---|
[914] | 4752 | Parameters:
|
---|
[1846] | 4753 |
|
---|
[914] | 4754 | angle: The angle (degrees) to rotate (add) by.
|
---|
[1846] | 4755 |
|
---|
| 4756 | Example::
|
---|
| 4757 |
|
---|
[914] | 4758 | scan.rotate_xyphase(2.3)
|
---|
[1846] | 4759 |
|
---|
[914] | 4760 | """
|
---|
| 4761 | varlist = vars()
|
---|
[936] | 4762 | self._math._rotate_xyphase(self, angle)
|
---|
[914] | 4763 | self._add_history("rotate_xyphase", varlist)
|
---|
| 4764 | return
|
---|
| 4765 |
|
---|
[1862] | 4766 | @asaplog_post_dec
|
---|
[914] | 4767 | def swap_linears(self):
|
---|
[1846] | 4768 | """\
|
---|
[1573] | 4769 | Swap the linear polarisations XX and YY, or better the first two
|
---|
[1348] | 4770 | polarisations as this also works for ciculars.
|
---|
[914] | 4771 | """
|
---|
| 4772 | varlist = vars()
|
---|
[936] | 4773 | self._math._swap_linears(self)
|
---|
[914] | 4774 | self._add_history("swap_linears", varlist)
|
---|
| 4775 | return
|
---|
| 4776 |
|
---|
[1862] | 4777 | @asaplog_post_dec
|
---|
[914] | 4778 | def invert_phase(self):
|
---|
[1846] | 4779 | """\
|
---|
[914] | 4780 | Invert the phase of the complex polarisation
|
---|
| 4781 | """
|
---|
| 4782 | varlist = vars()
|
---|
[936] | 4783 | self._math._invert_phase(self)
|
---|
[914] | 4784 | self._add_history("invert_phase", varlist)
|
---|
| 4785 | return
|
---|
| 4786 |
|
---|
[1862] | 4787 | @asaplog_post_dec
|
---|
[876] | 4788 | def add(self, offset, insitu=None):
|
---|
[1846] | 4789 | """\
|
---|
[513] | 4790 | Return a scan where all spectra have the offset added
|
---|
[1846] | 4791 |
|
---|
[513] | 4792 | Parameters:
|
---|
[1846] | 4793 |
|
---|
[513] | 4794 | offset: the offset
|
---|
[1855] | 4795 |
|
---|
[513] | 4796 | insitu: if False a new scantable is returned.
|
---|
| 4797 | Otherwise, the scaling is done in-situ
|
---|
| 4798 | The default is taken from .asaprc (False)
|
---|
[1846] | 4799 |
|
---|
[513] | 4800 | """
|
---|
| 4801 | if insitu is None: insitu = rcParams['insitu']
|
---|
[876] | 4802 | self._math._setinsitu(insitu)
|
---|
[513] | 4803 | varlist = vars()
|
---|
[2954] | 4804 | s = scantable(self._math._unaryop(self, offset, "ADD", False, False))
|
---|
[1118] | 4805 | s._add_history("add", varlist)
|
---|
[876] | 4806 | if insitu:
|
---|
| 4807 | self._assign(s)
|
---|
| 4808 | else:
|
---|
[513] | 4809 | return s
|
---|
| 4810 |
|
---|
[1862] | 4811 | @asaplog_post_dec
|
---|
[2966] | 4812 | def scale(self, factor, tsys=True, insitu=None):
|
---|
[1846] | 4813 | """\
|
---|
| 4814 |
|
---|
[1938] | 4815 | Return a scan where all spectra are scaled by the given 'factor'
|
---|
[1846] | 4816 |
|
---|
[513] | 4817 | Parameters:
|
---|
[1846] | 4818 |
|
---|
[1819] | 4819 | factor: the scaling factor (float or 1D float list)
|
---|
[1855] | 4820 |
|
---|
[513] | 4821 | insitu: if False a new scantable is returned.
|
---|
| 4822 | Otherwise, the scaling is done in-situ
|
---|
| 4823 | The default is taken from .asaprc (False)
|
---|
[1855] | 4824 |
|
---|
[513] | 4825 | tsys: if True (default) then apply the operation to Tsys
|
---|
| 4826 | as well as the data
|
---|
| 4827 | """
|
---|
| 4828 | if insitu is None: insitu = rcParams['insitu']
|
---|
[876] | 4829 | self._math._setinsitu(insitu)
|
---|
[513] | 4830 | varlist = vars()
|
---|
[1819] | 4831 | s = None
|
---|
| 4832 | import numpy
|
---|
| 4833 | if isinstance(factor, list) or isinstance(factor, numpy.ndarray):
|
---|
[2320] | 4834 | if isinstance(factor[0], list) or isinstance(factor[0],
|
---|
| 4835 | numpy.ndarray):
|
---|
[1819] | 4836 | from asapmath import _array2dOp
|
---|
[2966] | 4837 | s = _array2dOp( self, factor, "MUL", tsys, insitu, True )
|
---|
[1819] | 4838 | else:
|
---|
[2320] | 4839 | s = scantable( self._math._arrayop( self, factor,
|
---|
[2966] | 4840 | "MUL", tsys, True ) )
|
---|
[1819] | 4841 | else:
|
---|
[2966] | 4842 | s = scantable(self._math._unaryop(self, factor, "MUL", tsys, True ))
|
---|
[1118] | 4843 | s._add_history("scale", varlist)
|
---|
[876] | 4844 | if insitu:
|
---|
| 4845 | self._assign(s)
|
---|
| 4846 | else:
|
---|
[513] | 4847 | return s
|
---|
| 4848 |
|
---|
[2349] | 4849 | @preserve_selection
|
---|
| 4850 | def set_sourcetype(self, match, matchtype="pattern",
|
---|
[1504] | 4851 | sourcetype="reference"):
|
---|
[1846] | 4852 | """\
|
---|
[1502] | 4853 | Set the type of the source to be an source or reference scan
|
---|
[1846] | 4854 | using the provided pattern.
|
---|
| 4855 |
|
---|
[1502] | 4856 | Parameters:
|
---|
[1846] | 4857 |
|
---|
[1504] | 4858 | match: a Unix style pattern, regular expression or selector
|
---|
[1855] | 4859 |
|
---|
[1504] | 4860 | matchtype: 'pattern' (default) UNIX style pattern or
|
---|
| 4861 | 'regex' regular expression
|
---|
[1855] | 4862 |
|
---|
[1502] | 4863 | sourcetype: the type of the source to use (source/reference)
|
---|
[1846] | 4864 |
|
---|
[1502] | 4865 | """
|
---|
| 4866 | varlist = vars()
|
---|
| 4867 | stype = -1
|
---|
[2480] | 4868 | if sourcetype.lower().startswith("r") or sourcetype.lower() == "off":
|
---|
[1502] | 4869 | stype = 1
|
---|
[2480] | 4870 | elif sourcetype.lower().startswith("s") or sourcetype.lower() == "on":
|
---|
[1502] | 4871 | stype = 0
|
---|
[1504] | 4872 | else:
|
---|
[2480] | 4873 | raise ValueError("Illegal sourcetype use s(ource)/on or r(eference)/off")
|
---|
[1504] | 4874 | if matchtype.lower().startswith("p"):
|
---|
| 4875 | matchtype = "pattern"
|
---|
| 4876 | elif matchtype.lower().startswith("r"):
|
---|
| 4877 | matchtype = "regex"
|
---|
| 4878 | else:
|
---|
| 4879 | raise ValueError("Illegal matchtype, use p(attern) or r(egex)")
|
---|
[1502] | 4880 | sel = selector()
|
---|
| 4881 | if isinstance(match, selector):
|
---|
| 4882 | sel = match
|
---|
| 4883 | else:
|
---|
[2480] | 4884 | sel.set_query("SRCNAME=%s('%s')" % (matchtype, match))
|
---|
| 4885 | self.set_selection(sel)
|
---|
[1502] | 4886 | self._setsourcetype(stype)
|
---|
[1573] | 4887 | self._add_history("set_sourcetype", varlist)
|
---|
[1502] | 4888 |
|
---|
[2818] | 4889 |
|
---|
| 4890 | def set_sourcename(self, name):
|
---|
| 4891 | varlist = vars()
|
---|
| 4892 | self._setsourcename(name)
|
---|
| 4893 | self._add_history("set_sourcename", varlist)
|
---|
| 4894 |
|
---|
[1862] | 4895 | @asaplog_post_dec
|
---|
[1857] | 4896 | @preserve_selection
|
---|
[1819] | 4897 | def auto_quotient(self, preserve=True, mode='paired', verify=False):
|
---|
[1846] | 4898 | """\
|
---|
[670] | 4899 | This function allows to build quotients automatically.
|
---|
[1819] | 4900 | It assumes the observation to have the same number of
|
---|
[670] | 4901 | "ons" and "offs"
|
---|
[1846] | 4902 |
|
---|
[670] | 4903 | Parameters:
|
---|
[1846] | 4904 |
|
---|
[710] | 4905 | preserve: you can preserve (default) the continuum or
|
---|
| 4906 | remove it. The equations used are
|
---|
[1857] | 4907 |
|
---|
[670] | 4908 | preserve: Output = Toff * (on/off) - Toff
|
---|
[1857] | 4909 |
|
---|
[1070] | 4910 | remove: Output = Toff * (on/off) - Ton
|
---|
[1855] | 4911 |
|
---|
[1573] | 4912 | mode: the on/off detection mode
|
---|
[1348] | 4913 | 'paired' (default)
|
---|
| 4914 | identifies 'off' scans by the
|
---|
| 4915 | trailing '_R' (Mopra/Parkes) or
|
---|
| 4916 | '_e'/'_w' (Tid) and matches
|
---|
| 4917 | on/off pairs from the observing pattern
|
---|
[1502] | 4918 | 'time'
|
---|
| 4919 | finds the closest off in time
|
---|
[1348] | 4920 |
|
---|
[1857] | 4921 | .. todo:: verify argument is not implemented
|
---|
| 4922 |
|
---|
[670] | 4923 | """
|
---|
[1857] | 4924 | varlist = vars()
|
---|
[1348] | 4925 | modes = ["time", "paired"]
|
---|
[670] | 4926 | if not mode in modes:
|
---|
[876] | 4927 | msg = "please provide valid mode. Valid modes are %s" % (modes)
|
---|
| 4928 | raise ValueError(msg)
|
---|
[1348] | 4929 | s = None
|
---|
| 4930 | if mode.lower() == "paired":
|
---|
[2840] | 4931 | from asap._asap import srctype
|
---|
[1857] | 4932 | sel = self.get_selection()
|
---|
[2840] | 4933 | #sel.set_query("SRCTYPE==psoff")
|
---|
| 4934 | sel.set_types(srctype.psoff)
|
---|
[1356] | 4935 | self.set_selection(sel)
|
---|
[1348] | 4936 | offs = self.copy()
|
---|
[2840] | 4937 | #sel.set_query("SRCTYPE==pson")
|
---|
| 4938 | sel.set_types(srctype.pson)
|
---|
[1356] | 4939 | self.set_selection(sel)
|
---|
[1348] | 4940 | ons = self.copy()
|
---|
| 4941 | s = scantable(self._math._quotient(ons, offs, preserve))
|
---|
| 4942 | elif mode.lower() == "time":
|
---|
| 4943 | s = scantable(self._math._auto_quotient(self, mode, preserve))
|
---|
[1118] | 4944 | s._add_history("auto_quotient", varlist)
|
---|
[876] | 4945 | return s
|
---|
[710] | 4946 |
|
---|
[1862] | 4947 | @asaplog_post_dec
|
---|
[1145] | 4948 | def mx_quotient(self, mask = None, weight='median', preserve=True):
|
---|
[1846] | 4949 | """\
|
---|
[1143] | 4950 | Form a quotient using "off" beams when observing in "MX" mode.
|
---|
[1846] | 4951 |
|
---|
[1143] | 4952 | Parameters:
|
---|
[1846] | 4953 |
|
---|
[1145] | 4954 | mask: an optional mask to be used when weight == 'stddev'
|
---|
[1855] | 4955 |
|
---|
[1143] | 4956 | weight: How to average the off beams. Default is 'median'.
|
---|
[1855] | 4957 |
|
---|
[1145] | 4958 | preserve: you can preserve (default) the continuum or
|
---|
[1855] | 4959 | remove it. The equations used are:
|
---|
[1846] | 4960 |
|
---|
[1855] | 4961 | preserve: Output = Toff * (on/off) - Toff
|
---|
| 4962 |
|
---|
| 4963 | remove: Output = Toff * (on/off) - Ton
|
---|
| 4964 |
|
---|
[1217] | 4965 | """
|
---|
[1593] | 4966 | mask = mask or ()
|
---|
[1141] | 4967 | varlist = vars()
|
---|
| 4968 | on = scantable(self._math._mx_extract(self, 'on'))
|
---|
[1143] | 4969 | preoff = scantable(self._math._mx_extract(self, 'off'))
|
---|
| 4970 | off = preoff.average_time(mask=mask, weight=weight, scanav=False)
|
---|
[1217] | 4971 | from asapmath import quotient
|
---|
[1145] | 4972 | q = quotient(on, off, preserve)
|
---|
[1143] | 4973 | q._add_history("mx_quotient", varlist)
|
---|
[1217] | 4974 | return q
|
---|
[513] | 4975 |
|
---|
[1862] | 4976 | @asaplog_post_dec
|
---|
[718] | 4977 | def freq_switch(self, insitu=None):
|
---|
[1846] | 4978 | """\
|
---|
[718] | 4979 | Apply frequency switching to the data.
|
---|
[1846] | 4980 |
|
---|
[718] | 4981 | Parameters:
|
---|
[1846] | 4982 |
|
---|
[718] | 4983 | insitu: if False a new scantable is returned.
|
---|
| 4984 | Otherwise, the swictching is done in-situ
|
---|
| 4985 | The default is taken from .asaprc (False)
|
---|
[1846] | 4986 |
|
---|
[718] | 4987 | """
|
---|
| 4988 | if insitu is None: insitu = rcParams['insitu']
|
---|
[876] | 4989 | self._math._setinsitu(insitu)
|
---|
[718] | 4990 | varlist = vars()
|
---|
[876] | 4991 | s = scantable(self._math._freqswitch(self))
|
---|
[1118] | 4992 | s._add_history("freq_switch", varlist)
|
---|
[1856] | 4993 | if insitu:
|
---|
| 4994 | self._assign(s)
|
---|
| 4995 | else:
|
---|
| 4996 | return s
|
---|
[718] | 4997 |
|
---|
[1862] | 4998 | @asaplog_post_dec
|
---|
[780] | 4999 | def recalc_azel(self):
|
---|
[1846] | 5000 | """Recalculate the azimuth and elevation for each position."""
|
---|
[780] | 5001 | varlist = vars()
|
---|
[876] | 5002 | self._recalcazel()
|
---|
[780] | 5003 | self._add_history("recalc_azel", varlist)
|
---|
| 5004 | return
|
---|
| 5005 |
|
---|
[1862] | 5006 | @asaplog_post_dec
|
---|
[513] | 5007 | def __add__(self, other):
|
---|
[2574] | 5008 | """
|
---|
| 5009 | implicit on all axes and on Tsys
|
---|
| 5010 | """
|
---|
[513] | 5011 | varlist = vars()
|
---|
[2574] | 5012 | s = self.__op( other, "ADD" )
|
---|
[513] | 5013 | s._add_history("operator +", varlist)
|
---|
| 5014 | return s
|
---|
| 5015 |
|
---|
[1862] | 5016 | @asaplog_post_dec
|
---|
[513] | 5017 | def __sub__(self, other):
|
---|
| 5018 | """
|
---|
| 5019 | implicit on all axes and on Tsys
|
---|
| 5020 | """
|
---|
| 5021 | varlist = vars()
|
---|
[2574] | 5022 | s = self.__op( other, "SUB" )
|
---|
[513] | 5023 | s._add_history("operator -", varlist)
|
---|
| 5024 | return s
|
---|
[710] | 5025 |
|
---|
[1862] | 5026 | @asaplog_post_dec
|
---|
[513] | 5027 | def __mul__(self, other):
|
---|
| 5028 | """
|
---|
| 5029 | implicit on all axes and on Tsys
|
---|
| 5030 | """
|
---|
| 5031 | varlist = vars()
|
---|
[2574] | 5032 | s = self.__op( other, "MUL" ) ;
|
---|
[513] | 5033 | s._add_history("operator *", varlist)
|
---|
| 5034 | return s
|
---|
| 5035 |
|
---|
[710] | 5036 |
|
---|
[1862] | 5037 | @asaplog_post_dec
|
---|
[513] | 5038 | def __div__(self, other):
|
---|
| 5039 | """
|
---|
| 5040 | implicit on all axes and on Tsys
|
---|
| 5041 | """
|
---|
| 5042 | varlist = vars()
|
---|
[2574] | 5043 | s = self.__op( other, "DIV" )
|
---|
| 5044 | s._add_history("operator /", varlist)
|
---|
| 5045 | return s
|
---|
| 5046 |
|
---|
| 5047 | @asaplog_post_dec
|
---|
| 5048 | def __op( self, other, mode ):
|
---|
[513] | 5049 | s = None
|
---|
| 5050 | if isinstance(other, scantable):
|
---|
[2574] | 5051 | s = scantable(self._math._binaryop(self, other, mode))
|
---|
[513] | 5052 | elif isinstance(other, float):
|
---|
| 5053 | if other == 0.0:
|
---|
[718] | 5054 | raise ZeroDivisionError("Dividing by zero is not recommended")
|
---|
[2954] | 5055 | s = scantable(self._math._unaryop(self, other, mode, False, False))
|
---|
[2144] | 5056 | elif isinstance(other, list) or isinstance(other, numpy.ndarray):
|
---|
[2349] | 5057 | if isinstance(other[0], list) \
|
---|
| 5058 | or isinstance(other[0], numpy.ndarray):
|
---|
[2144] | 5059 | from asapmath import _array2dOp
|
---|
[2574] | 5060 | s = _array2dOp( self, other, mode, False )
|
---|
[2144] | 5061 | else:
|
---|
[2574] | 5062 | s = scantable( self._math._arrayop( self, other,
|
---|
[2954] | 5063 | mode, False, False ) )
|
---|
[513] | 5064 | else:
|
---|
[718] | 5065 | raise TypeError("Other input is not a scantable or float value")
|
---|
[513] | 5066 | return s
|
---|
| 5067 |
|
---|
[1862] | 5068 | @asaplog_post_dec
|
---|
[530] | 5069 | def get_fit(self, row=0):
|
---|
[1846] | 5070 | """\
|
---|
[530] | 5071 | Print or return the stored fits for a row in the scantable
|
---|
[1846] | 5072 |
|
---|
[530] | 5073 | Parameters:
|
---|
[1846] | 5074 |
|
---|
[530] | 5075 | row: the row which the fit has been applied to.
|
---|
[1846] | 5076 |
|
---|
[530] | 5077 | """
|
---|
| 5078 | if row > self.nrow():
|
---|
| 5079 | return
|
---|
[976] | 5080 | from asap.asapfit import asapfit
|
---|
[530] | 5081 | fit = asapfit(self._getfit(row))
|
---|
[1859] | 5082 | asaplog.push( '%s' %(fit) )
|
---|
| 5083 | return fit.as_dict()
|
---|
[530] | 5084 |
|
---|
[2349] | 5085 | @preserve_selection
|
---|
[1483] | 5086 | def flag_nans(self):
|
---|
[1846] | 5087 | """\
|
---|
[1483] | 5088 | Utility function to flag NaN values in the scantable.
|
---|
| 5089 | """
|
---|
| 5090 | import numpy
|
---|
| 5091 | basesel = self.get_selection()
|
---|
| 5092 | for i in range(self.nrow()):
|
---|
[1589] | 5093 | sel = self.get_row_selector(i)
|
---|
| 5094 | self.set_selection(basesel+sel)
|
---|
[1483] | 5095 | nans = numpy.isnan(self._getspectrum(0))
|
---|
[2877] | 5096 | if numpy.any(nans):
|
---|
| 5097 | bnans = [ bool(v) for v in nans]
|
---|
| 5098 | self.flag(bnans)
|
---|
| 5099 |
|
---|
| 5100 | self.set_selection(basesel)
|
---|
[1483] | 5101 |
|
---|
[1588] | 5102 | def get_row_selector(self, rowno):
|
---|
[1992] | 5103 | return selector(rows=[rowno])
|
---|
[1573] | 5104 |
|
---|
[484] | 5105 | def _add_history(self, funcname, parameters):
|
---|
[1435] | 5106 | if not rcParams['scantable.history']:
|
---|
| 5107 | return
|
---|
[484] | 5108 | # create date
|
---|
| 5109 | sep = "##"
|
---|
| 5110 | from datetime import datetime
|
---|
| 5111 | dstr = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
|
---|
| 5112 | hist = dstr+sep
|
---|
| 5113 | hist += funcname+sep#cdate+sep
|
---|
[2349] | 5114 | if parameters.has_key('self'):
|
---|
| 5115 | del parameters['self']
|
---|
[1118] | 5116 | for k, v in parameters.iteritems():
|
---|
[484] | 5117 | if type(v) is dict:
|
---|
[1118] | 5118 | for k2, v2 in v.iteritems():
|
---|
[484] | 5119 | hist += k2
|
---|
| 5120 | hist += "="
|
---|
[1118] | 5121 | if isinstance(v2, scantable):
|
---|
[484] | 5122 | hist += 'scantable'
|
---|
| 5123 | elif k2 == 'mask':
|
---|
[1118] | 5124 | if isinstance(v2, list) or isinstance(v2, tuple):
|
---|
[513] | 5125 | hist += str(self._zip_mask(v2))
|
---|
| 5126 | else:
|
---|
| 5127 | hist += str(v2)
|
---|
[484] | 5128 | else:
|
---|
[513] | 5129 | hist += str(v2)
|
---|
[484] | 5130 | else:
|
---|
| 5131 | hist += k
|
---|
| 5132 | hist += "="
|
---|
[1118] | 5133 | if isinstance(v, scantable):
|
---|
[484] | 5134 | hist += 'scantable'
|
---|
| 5135 | elif k == 'mask':
|
---|
[1118] | 5136 | if isinstance(v, list) or isinstance(v, tuple):
|
---|
[513] | 5137 | hist += str(self._zip_mask(v))
|
---|
| 5138 | else:
|
---|
| 5139 | hist += str(v)
|
---|
[484] | 5140 | else:
|
---|
| 5141 | hist += str(v)
|
---|
| 5142 | hist += sep
|
---|
| 5143 | hist = hist[:-2] # remove trailing '##'
|
---|
| 5144 | self._addhistory(hist)
|
---|
| 5145 |
|
---|
[710] | 5146 |
|
---|
[484] | 5147 | def _zip_mask(self, mask):
|
---|
| 5148 | mask = list(mask)
|
---|
| 5149 | i = 0
|
---|
| 5150 | segments = []
|
---|
| 5151 | while mask[i:].count(1):
|
---|
| 5152 | i += mask[i:].index(1)
|
---|
| 5153 | if mask[i:].count(0):
|
---|
| 5154 | j = i + mask[i:].index(0)
|
---|
| 5155 | else:
|
---|
[710] | 5156 | j = len(mask)
|
---|
[1118] | 5157 | segments.append([i, j])
|
---|
[710] | 5158 | i = j
|
---|
[484] | 5159 | return segments
|
---|
[714] | 5160 |
|
---|
[626] | 5161 | def _get_ordinate_label(self):
|
---|
| 5162 | fu = "("+self.get_fluxunit()+")"
|
---|
| 5163 | import re
|
---|
| 5164 | lbl = "Intensity"
|
---|
[1118] | 5165 | if re.match(".K.", fu):
|
---|
[626] | 5166 | lbl = "Brightness Temperature "+ fu
|
---|
[1118] | 5167 | elif re.match(".Jy.", fu):
|
---|
[626] | 5168 | lbl = "Flux density "+ fu
|
---|
| 5169 | return lbl
|
---|
[710] | 5170 |
|
---|
[876] | 5171 | def _check_ifs(self):
|
---|
[2349] | 5172 | # return len(set([self.nchan(i) for i in self.getifnos()])) == 1
|
---|
[1986] | 5173 | nchans = [self.nchan(i) for i in self.getifnos()]
|
---|
[2004] | 5174 | nchans = filter(lambda t: t > 0, nchans)
|
---|
[876] | 5175 | return (sum(nchans)/len(nchans) == nchans[0])
|
---|
[976] | 5176 |
|
---|
[1862] | 5177 | @asaplog_post_dec
|
---|
[1916] | 5178 | def _fill(self, names, unit, average, opts={}):
|
---|
[976] | 5179 | first = True
|
---|
| 5180 | fullnames = []
|
---|
| 5181 | for name in names:
|
---|
| 5182 | name = os.path.expandvars(name)
|
---|
| 5183 | name = os.path.expanduser(name)
|
---|
| 5184 | if not os.path.exists(name):
|
---|
| 5185 | msg = "File '%s' does not exists" % (name)
|
---|
| 5186 | raise IOError(msg)
|
---|
| 5187 | fullnames.append(name)
|
---|
| 5188 | if average:
|
---|
| 5189 | asaplog.push('Auto averaging integrations')
|
---|
[1079] | 5190 | stype = int(rcParams['scantable.storage'].lower() == 'disk')
|
---|
[976] | 5191 | for name in fullnames:
|
---|
[1073] | 5192 | tbl = Scantable(stype)
|
---|
[2004] | 5193 | if is_ms( name ):
|
---|
| 5194 | r = msfiller( tbl )
|
---|
| 5195 | else:
|
---|
| 5196 | r = filler( tbl )
|
---|
[976] | 5197 | msg = "Importing %s..." % (name)
|
---|
[1118] | 5198 | asaplog.push(msg, False)
|
---|
[2349] | 5199 | r.open(name, opts)
|
---|
[2480] | 5200 | rx = rcParams['scantable.reference']
|
---|
| 5201 | r.setreferenceexpr(rx)
|
---|
[1843] | 5202 | r.fill()
|
---|
[976] | 5203 | if average:
|
---|
[1118] | 5204 | tbl = self._math._average((tbl, ), (), 'NONE', 'SCAN')
|
---|
[976] | 5205 | if not first:
|
---|
[2902] | 5206 | tbl = self._math._merge([self, tbl])
|
---|
[976] | 5207 | Scantable.__init__(self, tbl)
|
---|
[1843] | 5208 | r.close()
|
---|
[1118] | 5209 | del r, tbl
|
---|
[976] | 5210 | first = False
|
---|
[1861] | 5211 | #flush log
|
---|
| 5212 | asaplog.post()
|
---|
[976] | 5213 | if unit is not None:
|
---|
| 5214 | self.set_fluxunit(unit)
|
---|
[1824] | 5215 | if not is_casapy():
|
---|
| 5216 | self.set_freqframe(rcParams['scantable.freqframe'])
|
---|
[976] | 5217 |
|
---|
[2610] | 5218 | def _get_verify_action( self, msg, action=None ):
|
---|
| 5219 | valid_act = ['Y', 'N', 'A', 'R']
|
---|
| 5220 | if not action or not isinstance(action, str):
|
---|
| 5221 | action = raw_input("%s [Y/n/a/r] (h for help): " % msg)
|
---|
| 5222 | if action == '':
|
---|
| 5223 | return "Y"
|
---|
| 5224 | elif (action.upper()[0] in valid_act):
|
---|
| 5225 | return action.upper()[0]
|
---|
| 5226 | elif (action.upper()[0] in ['H','?']):
|
---|
| 5227 | print "Available actions of verification [Y|n|a|r]"
|
---|
| 5228 | print " Y : Yes for current data (default)"
|
---|
| 5229 | print " N : No for current data"
|
---|
| 5230 | print " A : Accept all in the following and exit from verification"
|
---|
| 5231 | print " R : Reject all in the following and exit from verification"
|
---|
| 5232 | print " H or ?: help (show this message)"
|
---|
| 5233 | return self._get_verify_action(msg)
|
---|
| 5234 | else:
|
---|
| 5235 | return 'Y'
|
---|
[2012] | 5236 |
|
---|
[1402] | 5237 | def __getitem__(self, key):
|
---|
| 5238 | if key < 0:
|
---|
| 5239 | key += self.nrow()
|
---|
| 5240 | if key >= self.nrow():
|
---|
| 5241 | raise IndexError("Row index out of range.")
|
---|
| 5242 | return self._getspectrum(key)
|
---|
| 5243 |
|
---|
| 5244 | def __setitem__(self, key, value):
|
---|
| 5245 | if key < 0:
|
---|
| 5246 | key += self.nrow()
|
---|
| 5247 | if key >= self.nrow():
|
---|
| 5248 | raise IndexError("Row index out of range.")
|
---|
| 5249 | if not hasattr(value, "__len__") or \
|
---|
| 5250 | len(value) > self.nchan(self.getif(key)):
|
---|
| 5251 | raise ValueError("Spectrum length doesn't match.")
|
---|
| 5252 | return self._setspectrum(value, key)
|
---|
| 5253 |
|
---|
| 5254 | def __len__(self):
|
---|
| 5255 | return self.nrow()
|
---|
| 5256 |
|
---|
| 5257 | def __iter__(self):
|
---|
| 5258 | for i in range(len(self)):
|
---|
| 5259 | yield self[i]
|
---|