[100] | 1 | """
|
---|
| 2 | This is the ATNF Single Dish Analysis package.
|
---|
| 3 |
|
---|
| 4 | """
|
---|
[226] | 5 | import os,sys
|
---|
| 6 |
|
---|
[513] | 7 | def _validate_bool(b):
|
---|
[226] | 8 | 'Convert b to a boolean or raise'
|
---|
| 9 | bl = b.lower()
|
---|
| 10 | if bl in ('f', 'no', 'false', '0', 0): return False
|
---|
| 11 | elif bl in ('t', 'yes', 'true', '1', 1): return True
|
---|
| 12 | else:
|
---|
| 13 | raise ValueError('Could not convert "%s" to boolean' % b)
|
---|
| 14 |
|
---|
[513] | 15 | def _validate_int(s):
|
---|
[226] | 16 | 'convert s to int or raise'
|
---|
| 17 | try: return int(s)
|
---|
| 18 | except ValueError:
|
---|
| 19 | raise ValueError('Could not convert "%s" to int' % s)
|
---|
| 20 |
|
---|
[513] | 21 | def _asap_fname():
|
---|
[226] | 22 | """
|
---|
| 23 | Return the path to the rc file
|
---|
| 24 |
|
---|
| 25 | Search order:
|
---|
| 26 |
|
---|
| 27 | * current working dir
|
---|
| 28 | * environ var ASAPRC
|
---|
[274] | 29 | * HOME/.asaprc
|
---|
[690] | 30 |
|
---|
[226] | 31 | """
|
---|
| 32 |
|
---|
| 33 | fname = os.path.join( os.getcwd(), '.asaprc')
|
---|
| 34 | if os.path.exists(fname): return fname
|
---|
| 35 |
|
---|
| 36 | if os.environ.has_key('ASAPRC'):
|
---|
| 37 | path = os.environ['ASAPRC']
|
---|
| 38 | if os.path.exists(path):
|
---|
| 39 | fname = os.path.join(path, '.asaprc')
|
---|
| 40 | if os.path.exists(fname):
|
---|
| 41 | return fname
|
---|
| 42 |
|
---|
| 43 | if os.environ.has_key('HOME'):
|
---|
| 44 | home = os.environ['HOME']
|
---|
| 45 | fname = os.path.join(home, '.asaprc')
|
---|
| 46 | if os.path.exists(fname):
|
---|
| 47 | return fname
|
---|
| 48 | return None
|
---|
| 49 |
|
---|
[690] | 50 |
|
---|
[226] | 51 | defaultParams = {
|
---|
| 52 | # general
|
---|
[513] | 53 | 'verbose' : [True, _validate_bool],
|
---|
| 54 | 'useplotter' : [True, _validate_bool],
|
---|
[542] | 55 | 'insitu' : [True, _validate_bool],
|
---|
[690] | 56 |
|
---|
[226] | 57 | # plotting
|
---|
| 58 | 'plotter.stacking' : ['p', str],
|
---|
| 59 | 'plotter.panelling' : ['s', str],
|
---|
[690] | 60 |
|
---|
[226] | 61 | # scantable
|
---|
| 62 | 'scantable.save' : ['ASAP', str],
|
---|
[513] | 63 | 'scantable.autoaverage' : [True, _validate_bool],
|
---|
[226] | 64 | 'scantable.freqframe' : ['LSRK', str], #default frequency frame
|
---|
[513] | 65 | 'scantable.allaxes' : [True, _validate_bool], # apply action to all axes
|
---|
| 66 | 'scantable.plotter' : [True, _validate_bool], # use internal plotter
|
---|
| 67 | 'scantable.verbosesummary' : [False, _validate_bool]
|
---|
[226] | 68 |
|
---|
| 69 | # fitter
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[255] | 72 | def list_rcparameters():
|
---|
[690] | 73 |
|
---|
[255] | 74 | print """
|
---|
| 75 | # general
|
---|
| 76 | # print verbose output
|
---|
[466] | 77 | verbose : True
|
---|
[255] | 78 |
|
---|
| 79 | # preload a default plotter
|
---|
[466] | 80 | useplotter : True
|
---|
[255] | 81 |
|
---|
| 82 | # apply operations on the input scantable or return new one
|
---|
[542] | 83 | insitu : True
|
---|
[690] | 84 |
|
---|
[255] | 85 | # plotting
|
---|
| 86 | # default mode for colour stacking
|
---|
[552] | 87 | plotter.stacking : Pol
|
---|
[255] | 88 |
|
---|
| 89 | # default mode for panelling
|
---|
[552] | 90 | plotter.panelling : scan
|
---|
[255] | 91 |
|
---|
| 92 | # scantable
|
---|
| 93 | # default ouput format when saving
|
---|
[552] | 94 | scantable.save : ASAP
|
---|
[255] | 95 | # auto averaging on read
|
---|
[466] | 96 | scantable.autoaverage : True
|
---|
[255] | 97 |
|
---|
| 98 | # default frequency frame to set when function
|
---|
| 99 | # scantable.set_freqfrmae is called
|
---|
[552] | 100 | scantable.freqframe : LSRK
|
---|
[255] | 101 |
|
---|
| 102 | # apply action to all axes not just the cursor location
|
---|
[690] | 103 | scantable.allaxes : True
|
---|
[255] | 104 |
|
---|
| 105 | # use internal plotter
|
---|
[466] | 106 | scantable.plotter : True
|
---|
[255] | 107 |
|
---|
[381] | 108 | # Control the level of information printed by summary
|
---|
[466] | 109 | scantable.verbosesummary : False
|
---|
[690] | 110 |
|
---|
| 111 | # Fitter
|
---|
[255] | 112 | """
|
---|
[690] | 113 |
|
---|
[226] | 114 | def rc_params():
|
---|
| 115 | 'Return the default params updated from the values in the rc file'
|
---|
[690] | 116 |
|
---|
[513] | 117 | fname = _asap_fname()
|
---|
[690] | 118 |
|
---|
[226] | 119 | if fname is None or not os.path.exists(fname):
|
---|
| 120 | message = 'could not find rc file; returning defaults'
|
---|
| 121 | ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
|
---|
| 122 | #print message
|
---|
| 123 | return ret
|
---|
[690] | 124 |
|
---|
[226] | 125 | cnt = 0
|
---|
| 126 | for line in file(fname):
|
---|
| 127 | cnt +=1
|
---|
| 128 | line = line.strip()
|
---|
| 129 | if not len(line): continue
|
---|
| 130 | if line.startswith('#'): continue
|
---|
| 131 | tup = line.split(':',1)
|
---|
| 132 | if len(tup) !=2:
|
---|
| 133 | print ('Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname))
|
---|
| 134 | continue
|
---|
[690] | 135 |
|
---|
[226] | 136 | key, val = tup
|
---|
| 137 | key = key.strip()
|
---|
| 138 | if not defaultParams.has_key(key):
|
---|
| 139 | print ('Bad key "%s" on line %d in %s' % (key, cnt, fname))
|
---|
| 140 | continue
|
---|
[690] | 141 |
|
---|
[226] | 142 | default, converter = defaultParams[key]
|
---|
| 143 |
|
---|
| 144 | ind = val.find('#')
|
---|
| 145 | if ind>=0: val = val[:ind] # ignore trailing comments
|
---|
| 146 | val = val.strip()
|
---|
| 147 | try: cval = converter(val) # try to convert to proper type or raise
|
---|
| 148 | except Exception, msg:
|
---|
| 149 | print ('Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % (val, cnt, line, fname, msg))
|
---|
| 150 | continue
|
---|
| 151 | else:
|
---|
| 152 | # Alles Klar, update dict
|
---|
| 153 | defaultParams[key][0] = cval
|
---|
| 154 |
|
---|
| 155 | # strip the conveter funcs and return
|
---|
| 156 | ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
|
---|
[466] | 157 | print ('loaded rc file %s'%fname)
|
---|
[226] | 158 |
|
---|
| 159 | return ret
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | # this is the instance used by the asap classes
|
---|
[690] | 163 | rcParams = rc_params()
|
---|
[226] | 164 |
|
---|
| 165 | rcParamsDefault = dict(rcParams.items()) # a copy
|
---|
| 166 |
|
---|
| 167 | def rc(group, **kwargs):
|
---|
| 168 | """
|
---|
| 169 | Set the current rc params. Group is the grouping for the rc, eg
|
---|
[379] | 170 | for scantable.save the group is 'scantable', for plotter.stacking, the
|
---|
| 171 | group is 'plotter', and so on. kwargs is a list of attribute
|
---|
[226] | 172 | name/value pairs, eg
|
---|
| 173 |
|
---|
[379] | 174 | rc('scantable', save='SDFITS')
|
---|
[226] | 175 |
|
---|
| 176 | sets the current rc params and is equivalent to
|
---|
[690] | 177 |
|
---|
[379] | 178 | rcParams['scantable.save'] = 'SDFITS'
|
---|
[226] | 179 |
|
---|
| 180 | Use rcdefaults to restore the default rc params after changes.
|
---|
| 181 | """
|
---|
| 182 |
|
---|
[379] | 183 | aliases = {}
|
---|
[690] | 184 |
|
---|
[226] | 185 | for k,v in kwargs.items():
|
---|
| 186 | name = aliases.get(k) or k
|
---|
| 187 | key = '%s.%s' % (group, name)
|
---|
| 188 | if not rcParams.has_key(key):
|
---|
| 189 | raise KeyError('Unrecognized key "%s" for group "%s" and name "%s"' % (key, group, name))
|
---|
[690] | 190 |
|
---|
[226] | 191 | rcParams[key] = v
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | def rcdefaults():
|
---|
| 195 | """
|
---|
| 196 | Restore the default rc params - the ones that were created at
|
---|
| 197 | asap load time
|
---|
| 198 | """
|
---|
| 199 | rcParams.update(rcParamsDefault)
|
---|
| 200 |
|
---|
[513] | 201 |
|
---|
| 202 | def _is_sequence_or_number(param, ptype=int):
|
---|
| 203 | if isinstance(param,tuple) or isinstance(param,list):
|
---|
| 204 | out = True
|
---|
| 205 | for p in param:
|
---|
| 206 | out &= isinstance(p,ptype)
|
---|
| 207 | return out
|
---|
| 208 | elif isinstance(param, ptype):
|
---|
| 209 | return True
|
---|
| 210 | return False
|
---|
| 211 |
|
---|
[113] | 212 | from asapfitter import *
|
---|
[100] | 213 | from asapreader import reader
|
---|
| 214 | from asapmath import *
|
---|
| 215 | from scantable import *
|
---|
[297] | 216 | from asaplinefind import *
|
---|
[530] | 217 | from asapfit import *
|
---|
[285] | 218 |
|
---|
[466] | 219 | from numarray import logical_and as mask_and
|
---|
| 220 | from numarray import logical_or as mask_or
|
---|
| 221 | from numarray import logical_not as mask_not
|
---|
| 222 |
|
---|
[226] | 223 | if rcParams['useplotter']:
|
---|
[285] | 224 | if os.environ.has_key('DISPLAY'):
|
---|
[378] | 225 | print "Initialising asapplotter with the name 'plotter' ..."
|
---|
[690] | 226 | import asapplotter
|
---|
[285] | 227 | plotter = asapplotter.asapplotter()
|
---|
| 228 | else:
|
---|
[378] | 229 | print "No $DISPLAY set. Disabling plotter.\n"
|
---|
[285] | 230 |
|
---|
[574] | 231 | __date__ = '$Date: 2005-09-26 00:19:05 +0000 (Mon, 26 Sep 2005) $'.split()[1]
|
---|
[690] | 232 | __version__ = '1.2'
|
---|
[100] | 233 |
|
---|
| 234 | def list_scans(t = scantable):
|
---|
| 235 | import sys, types
|
---|
| 236 | #meta_t = type(t)
|
---|
| 237 | #if meta_t == types.InstanceType:
|
---|
| 238 | # t = t.__class__
|
---|
| 239 | #elif meta_t not in [types.ClassType, types.TypeType]:
|
---|
| 240 | # t = meta_t
|
---|
| 241 | globs = sys.modules['__main__'].__dict__.iteritems()
|
---|
[113] | 242 | print "The user created scantables are:"
|
---|
[585] | 243 | sts = map(lambda x: x[0], filter(lambda x: isinstance(x[1], t), globs))
|
---|
| 244 | print filter(lambda x: not x.startswith('_'), sts)
|
---|
[100] | 245 |
|
---|
[113] | 246 | def commands():
|
---|
[690] | 247 | x = """
|
---|
[113] | 248 | [The scan container]
|
---|
| 249 | scantable - a container for integrations/scans
|
---|
[182] | 250 | (can open asap/rpfits/sdfits and ms files)
|
---|
[113] | 251 | copy - returns a copy of a scan
|
---|
| 252 | get_scan - gets a specific scan out of a scantable
|
---|
| 253 | summary - print info about the scantable contents
|
---|
[255] | 254 | set_cursor - set a specific Beam/IF/Pol 'cursor' for
|
---|
| 255 | further use
|
---|
| 256 | get_cursor - print out the current cursor position
|
---|
[182] | 257 | stats - get specified statistic of the spectra in
|
---|
| 258 | the scantable
|
---|
| 259 | stddev - get the standard deviation of the spectra
|
---|
| 260 | in the scantable
|
---|
[113] | 261 | get_tsys - get the TSys
|
---|
| 262 | get_time - get the timestamps of the integrations
|
---|
[255] | 263 | get_unit - get the currnt unit
|
---|
[513] | 264 | set_unit - set the abcissa unit to be used from this
|
---|
| 265 | point on
|
---|
[255] | 266 | get_abcissa - get the abcissa values and name for a given
|
---|
| 267 | row (time)
|
---|
[113] | 268 | set_freqframe - set the frame info for the Spectral Axis
|
---|
| 269 | (e.g. 'LSRK')
|
---|
[276] | 270 | set_doppler - set the doppler to be used from this point on
|
---|
[240] | 271 | set_instrument - set the instrument name
|
---|
[255] | 272 | get_fluxunit - get the brightness flux unit
|
---|
[240] | 273 | set_fluxunit - set the brightness flux unit
|
---|
[188] | 274 | create_mask - return an mask in the current unit
|
---|
| 275 | for the given region. The specified regions
|
---|
| 276 | are NOT masked
|
---|
[255] | 277 | get_restfreqs - get the current list of rest frequencies
|
---|
| 278 | set_restfreqs - set a list of rest frequencies
|
---|
[403] | 279 | lines - print list of known spectral lines
|
---|
[113] | 280 | flag_spectrum - flag a whole Beam/IF/Pol
|
---|
[116] | 281 | save - save the scantable to disk as either 'ASAP'
|
---|
| 282 | or 'SDFITS'
|
---|
[486] | 283 | nbeam,nif,nchan,npol - the number of beams/IFs/Pols/Chans
|
---|
| 284 | history - print the history of the scantable
|
---|
[530] | 285 | get_fit - get a fit which has been stored witnh the data
|
---|
[690] | 286 | average_time - return the (weighted) time average of a scan
|
---|
[513] | 287 | or a list of scans
|
---|
| 288 | average_pol - average the polarisations together.
|
---|
[113] | 289 | The dimension won't be reduced and
|
---|
| 290 | all polarisations will contain the
|
---|
| 291 | averaged spectrum.
|
---|
[690] | 292 | auto_quotient - return the on/off quotient with
|
---|
| 293 | automatic detection of the on/off scans
|
---|
[513] | 294 | quotient - return the on/off quotient
|
---|
| 295 | scale - return a scan scaled by a given factor
|
---|
[690] | 296 | add - return a scan with given value added
|
---|
[513] | 297 | bin - return a scan with binned channels
|
---|
| 298 | resample - return a scan with resampled channels
|
---|
| 299 | smooth - return the spectrally smoothed scan
|
---|
| 300 | poly_baseline - fit a polynomial baseline to all Beams/IFs/Pols
|
---|
[690] | 301 | auto_poly_baseline - automatically fit a polynomial baseline
|
---|
[513] | 302 | gain_el - apply gain-elevation correction
|
---|
| 303 | opacity - apply opacity correction
|
---|
| 304 | convert_flux - convert to and from Jy and Kelvin brightness
|
---|
[255] | 305 | units
|
---|
[513] | 306 | freq_align - align spectra in frequency frame
|
---|
| 307 | rotate_xyphase - rotate XY phase of cross correlation
|
---|
| 308 | rotate_linpolphase - rotate the phase of the complex
|
---|
| 309 | polarization O=Q+iU correlation
|
---|
| 310 | [Math] Mainly functions which operate on more than one scantable
|
---|
[100] | 311 |
|
---|
[690] | 312 | average_time - return the (weighted) time average
|
---|
[513] | 313 | of a list of scans
|
---|
| 314 | quotient - return the on/off quotient
|
---|
| 315 | simple_math - simple mathematical operations on two scantables,
|
---|
| 316 | 'add', 'sub', 'mul', 'div'
|
---|
| 317 | [Fitting]
|
---|
[113] | 318 | fitter
|
---|
| 319 | auto_fit - return a scan where the function is
|
---|
| 320 | applied to all Beams/IFs/Pols.
|
---|
| 321 | commit - return a new scan where the fits have been
|
---|
| 322 | commited.
|
---|
| 323 | fit - execute the actual fitting process
|
---|
[530] | 324 | store_fit - store the fit paramaters in the data (scantable)
|
---|
[113] | 325 | get_chi2 - get the Chi^2
|
---|
| 326 | set_scan - set the scantable to be fit
|
---|
| 327 | set_function - set the fitting function
|
---|
| 328 | set_parameters - set the parameters for the function(s), and
|
---|
| 329 | set if they should be held fixed during fitting
|
---|
[513] | 330 | set_gauss_parameters - same as above but specialised for individual
|
---|
| 331 | gaussian components
|
---|
[113] | 332 | get_parameters - get the fitted parameters
|
---|
[513] | 333 | plot - plot the resulting fit and/or components and
|
---|
| 334 | residual
|
---|
[210] | 335 | [Plotter]
|
---|
| 336 | asapplotter - a plotter for asap, default plotter is
|
---|
| 337 | called 'plotter'
|
---|
| 338 | plot - plot a (list of) scantable
|
---|
[378] | 339 | save - save the plot to a file ('png' ,'ps' or 'eps')
|
---|
[210] | 340 | set_mode - set the state of the plotter, i.e.
|
---|
| 341 | what is to be plotted 'colour stacked'
|
---|
| 342 | and what 'panelled'
|
---|
[530] | 343 | set_cursor - only plot a selected part of the data
|
---|
| 344 | set_range - set a 'zoom' window
|
---|
[255] | 345 | set_legend - specify user labels for the legend indeces
|
---|
| 346 | set_title - specify user labels for the panel indeces
|
---|
| 347 | set_ordinate - specify a user label for the ordinate
|
---|
| 348 | set_abcissa - specify a user label for the abcissa
|
---|
[378] | 349 | set_layout - specify the multi-panel layout (rows,cols)
|
---|
[690] | 350 |
|
---|
[182] | 351 | [Reading files]
|
---|
| 352 | reader - access rpfits/sdfits files
|
---|
| 353 | read - read in integrations
|
---|
| 354 | summary - list info about all integrations
|
---|
| 355 |
|
---|
[113] | 356 | [General]
|
---|
| 357 | commands - this command
|
---|
| 358 | print - print details about a variable
|
---|
| 359 | list_scans - list all scantables created bt the user
|
---|
| 360 | del - delete the given variable from memory
|
---|
| 361 | range - create a list of values, e.g.
|
---|
| 362 | range(3) = [0,1,2], range(2,5) = [2,3,4]
|
---|
| 363 | help - print help for one of the listed functions
|
---|
| 364 | execfile - execute an asap script, e.g. execfile('myscript')
|
---|
[255] | 365 | list_rcparameters - print out a list of possible values to be
|
---|
[274] | 366 | put into $HOME/.asaprc
|
---|
[466] | 367 | mask_and,mask_or,
|
---|
| 368 | mask_not - boolean operations on masks created with
|
---|
| 369 | scantable.create_mask
|
---|
[690] | 370 |
|
---|
[210] | 371 | Note:
|
---|
| 372 | How to use this with help:
|
---|
| 373 | # function 'summary'
|
---|
| 374 | [xxx] is just a category
|
---|
| 375 | Every 'sub-level' in this list should be replaces by a '.' Period when
|
---|
[690] | 376 | using help
|
---|
[210] | 377 | Example:
|
---|
| 378 | ASAP> help scantable # to get info on ths scantable
|
---|
| 379 | ASAP> help scantable.summary # to get help on the scantable's
|
---|
| 380 | ASAP> help average_time
|
---|
| 381 |
|
---|
[113] | 382 | """
|
---|
| 383 | print x
|
---|
| 384 | return
|
---|
| 385 |
|
---|
[573] | 386 | print """Welcome to ASAP v%s (%s) - the ATNF Spectral Analysis Package
|
---|
[100] | 387 |
|
---|
| 388 | Please report any bugs to:
|
---|
[555] | 389 | asap@atnf.csiro.au
|
---|
[100] | 390 |
|
---|
[378] | 391 | [IMPORTANT: ASAP is 0-based]
|
---|
[113] | 392 | Type commands() to get a list of all available ASAP commands.
|
---|
[573] | 393 | """ % (__version__, __date__)
|
---|