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