Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

import warnings 

import numpy as np 

from scipy.spatial import cKDTree as kdtree 

import palpy 

from lsst.sims.utils import Site, m5_flat_sed, xyz_from_ra_dec, xyz_angular_radius, _buildTree, _xyz_from_ra_dec 

from lsst.sims.survey.fields import FieldsDatabase 

from .baseStacker import BaseStacker 

 

__all__ = ['NormAirmassStacker', 'ParallaxFactorStacker', 'HourAngleStacker', 

'FilterColorStacker', 'ZenithDistStacker', 'ParallacticAngleStacker', 

'SeasonStacker', 'DcrStacker', 'FiveSigmaStacker', 'OpSimFieldStacker'] 

 

# Original stackers by Peter Yoachim (yoachim@uw.edu) 

# Filter color stacker by Lynne Jones (lynnej@uw.edu) 

# Season stacker by Phil Marshall (dr.phil.marshall@gmail.com), 

# modified by Humna Awan (humna.awan@rutgers.edu) 

 

 

class FiveSigmaStacker(BaseStacker): 

""" 

Calculate the 5-sigma limiting depth for a point source in the given conditions. 

 

This is generally not needed, unless the m5 parameters have been updated 

or m5 was not previously calculated. 

""" 

def __init__(self, airmassCol='airmass', seeingCol='seeingFwhmEff', skybrightnessCol='skyBrightness', 

filterCol='filter', exptimeCol='visitExposureTime'): 

self.units = ['mag'] 

self.colsAdded = ['m5_simsUtils'] 

self.colsReq = [airmassCol, seeingCol, skybrightnessCol, filterCol, exptimeCol] 

self.airmassCol = airmassCol 

self.seeingCol = seeingCol 

self.skybrightnessCol = skybrightnessCol 

self.filterCol = filterCol 

self.exptimeCol = exptimeCol 

 

def _run(self, simData, cols_present=False): 

if cols_present: 

# Column already present in data; assume it needs updating and recalculate. 

pass 

filts = np.unique(simData[self.filterCol]) 

for filtername in filts: 

infilt = np.where(simData[self.filterCol] == filtername) 

simData['m5_simsUtils'][infilt] = m5_flat_sed(filtername, 

simData[infilt][self.skybrightnessCol], 

simData[infilt][self.seeingCol], 

simData[infilt][self.exptimeCol], 

simData[infilt][self.airmassCol]) 

return simData 

 

 

class NormAirmassStacker(BaseStacker): 

"""Calculate the normalized airmass for each opsim pointing. 

""" 

def __init__(self, airmassCol='airmass', decCol='fieldDec', 

degrees=True, telescope_lat = -30.2446388): 

self.units = ['X / Xmin'] 

self.colsAdded = ['normairmass'] 

self.colsReq = [airmassCol, decCol] 

self.airmassCol = airmassCol 

self.decCol = decCol 

self.telescope_lat = telescope_lat 

self.degrees = degrees 

 

def _run(self, simData, cols_present=False): 

"""Calculate new column for normalized airmass.""" 

# Run method is required to calculate column. 

# Driver runs getColInfo to know what columns are needed from db & which are calculated, 

# then gets data from db and then calculates additional columns (via run methods here). 

70 ↛ 72line 70 didn't jump to line 72, because the condition on line 70 was never true if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

dec = simData[self.decCol] 

74 ↛ 76line 74 didn't jump to line 76, because the condition on line 74 was never false if self.degrees: 

dec = np.radians(dec) 

min_z_possible = np.abs(dec - np.radians(self.telescope_lat)) 

min_airmass_possible = 1./np.cos(min_z_possible) 

simData['normairmass'] = simData[self.airmassCol] / min_airmass_possible 

return simData 

 

 

class ZenithDistStacker(BaseStacker): 

"""Calculate the zenith distance for each pointing. 

If 'degrees' is True, then assumes altCol is in degrees and returns degrees. 

If 'degrees' is False, assumes altCol is in radians and returns radians. 

""" 

def __init__(self, altCol='altitude', degrees=True): 

self.altCol = altCol 

self.degrees = degrees 

90 ↛ 93line 90 didn't jump to line 93, because the condition on line 90 was never false if self.degrees: 

self.units = ['degrees'] 

else: 

self.unit = ['radians'] 

self.colsAdded = ['zenithDistance'] 

self.colsReq = [self.altCol] 

 

def _run(self, simData, cols_present=False): 

"""Calculate new column for zenith distance.""" 

if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

if self.degrees: 

simData['zenithDistance'] = 90.0 - simData[self.altCol] 

else: 

simData['zenithDistance'] = np.pi/2.0 - simData[self.altCol] 

return simData 

 

 

class ParallaxFactorStacker(BaseStacker): 

"""Calculate the parallax factors for each opsim pointing. Output parallax factor in arcseconds. 

""" 

def __init__(self, raCol='fieldRA', decCol='fieldDec', dateCol='observationStartMJD', degrees=True): 

self.raCol = raCol 

self.decCol = decCol 

self.dateCol = dateCol 

self.units = ['arcsec', 'arcsec'] 

self.colsAdded = ['ra_pi_amp', 'dec_pi_amp'] 

self.colsReq = [raCol, decCol, dateCol] 

self.degrees = degrees 

 

def _gnomonic_project_toxy(self, RA1, Dec1, RAcen, Deccen): 

"""Calculate x/y projection of RA1/Dec1 in system with center at RAcen, Deccenp. 

Input radians. 

""" 

# also used in Global Telescope Network website 

cosc = np.sin(Deccen) * np.sin(Dec1) + np.cos(Deccen) * np.cos(Dec1) * np.cos(RA1-RAcen) 

x = np.cos(Dec1) * np.sin(RA1-RAcen) / cosc 

y = (np.cos(Deccen)*np.sin(Dec1) - np.sin(Deccen)*np.cos(Dec1)*np.cos(RA1-RAcen)) / cosc 

return x, y 

 

def _run(self, simData, cols_present=False): 

132 ↛ 134line 132 didn't jump to line 134, because the condition on line 132 was never true if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

ra_pi_amp = np.zeros(np.size(simData), dtype=[('ra_pi_amp', 'float')]) 

dec_pi_amp = np.zeros(np.size(simData), dtype=[('dec_pi_amp', 'float')]) 

ra_geo1 = np.zeros(np.size(simData), dtype='float') 

dec_geo1 = np.zeros(np.size(simData), dtype='float') 

ra_geo = np.zeros(np.size(simData), dtype='float') 

dec_geo = np.zeros(np.size(simData), dtype='float') 

ra = simData[self.raCol] 

dec = simData[self.decCol] 

143 ↛ 147line 143 didn't jump to line 147, because the condition on line 143 was never false if self.degrees: 

ra = np.radians(ra) 

dec = np.radians(dec) 

 

for i, ack in enumerate(simData): 

mtoa_params = palpy.mappa(2000., simData[self.dateCol][i]) 

# Object with a 1 arcsec parallax 

ra_geo1[i], dec_geo1[i] = palpy.mapqk(ra[i], dec[i], 

0., 0., 1., 0., mtoa_params) 

# Object with no parallax 

ra_geo[i], dec_geo[i] = palpy.mapqk(ra[i], dec[i], 

0., 0., 0., 0., mtoa_params) 

x_geo1, y_geo1 = self._gnomonic_project_toxy(ra_geo1, dec_geo1, 

ra, dec) 

x_geo, y_geo = self._gnomonic_project_toxy(ra_geo, dec_geo, ra, dec) 

# Return ra_pi_amp and dec_pi_amp in arcseconds. 

ra_pi_amp[:] = np.degrees(x_geo1-x_geo)*3600. 

dec_pi_amp[:] = np.degrees(y_geo1-y_geo)*3600. 

simData['ra_pi_amp'] = ra_pi_amp 

simData['dec_pi_amp'] = dec_pi_amp 

return simData 

 

 

class DcrStacker(BaseStacker): 

"""Calculate the RA,Dec offset expected for an object due to differential chromatic refraction. 

 

Parameters 

---------- 

filterCol : str 

The name of the column with filter names. Default 'fitler'. 

altCol : str 

Name of the column with altitude info. Default 'altitude'. 

raCol : str 

Name of the column with RA. Default 'ra_rad'. 

decCol : str 

Name of the column with Dec. Default 'dec_rad'. 

lstCol : str 

Name of the column with local sidereal time. Default 'lst'. 

site : str or lsst.sims.utils.Site 

Name of the observory or a lsst.sims.utils.Site object. Default 'LSST'. 

mjdCol : str 

Name of column with modified julian date. Default 'observationStartMJD' 

dcr_magnitudes : dict 

Magitude of the DCR offset for each filter at altitude/zenith distance of 45 degrees. 

Defaults u=0.07, g=0.07, r=0.50, i=0.045, z=0.042, y=0.04 (all arcseconds). 

 

Returns 

------- 

numpy.array 

Returns array with additional columns 'ra_dcr_amp' and 'dec_dcr_amp' with the DCR offsets 

for each observation. Also runs ZenithDistStacker and ParallacticAngleStacker. 

""" 

 

def __init__(self, filterCol='filter', altCol='altitude', degrees=True, 

raCol='fieldRA', decCol='fieldDec', lstCol='observationStartLST', 

site='LSST', mjdCol='observationStartMJD', 

dcr_magnitudes=None): 

 

201 ↛ 205line 201 didn't jump to line 205, because the condition on line 201 was never false if dcr_magnitudes is None: 

# DCR amplitudes are in arcseconds. 

self.dcr_magnitudes = {'u': 0.07, 'g': 0.07, 'r': 0.050, 'i': 0.045, 'z': 0.042, 'y': 0.04} 

else: 

self.dcr_magnitudes = dcr_magnitudes 

self.zdCol = 'zenithDistance' 

self.paCol = 'PA' 

self.filterCol = filterCol 

self.raCol = raCol 

self.decCol = decCol 

self.colsAdded = ['ra_dcr_amp', 'dec_dcr_amp', 'zenithDistance', 'PA', 'HA'] 

self.colsReq = [filterCol, raCol, decCol, altCol, lstCol] 

self.units = ['arcsec', 'arcsec'] 

self.degrees = degrees 

self.zstacker = ZenithDistStacker(altCol=altCol, degrees=self.degrees) 

self.pastacker = ParallacticAngleStacker(raCol=raCol, decCol=decCol, mjdCol=mjdCol, 

degrees=self.degrees, 

lstCol=lstCol, site=site) 

 

def _run(self, simData, cols_present=False): 

if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

# Need to make sure the Zenith stacker gets run first 

# Call _run method because already added these columns due to 'colsAdded' line. 

simData = self.zstacker._run(simData, cols_present=False) 

simData = self.pastacker._run(simData, cols_present=False) 

if self.degrees: 

zenithTan = np.tan(np.radians(simData[self.zdCol])) 

parallacticAngle = np.radians(simData[self.paCol]) 

else: 

zenithTan = np.tan(simData[self.zdCol]) 

parallacticAngle = simData[self.paCol] 

dcr_in_ra = zenithTan * np.sin(parallacticAngle) 

dcr_in_dec = zenithTan * np.cos(parallacticAngle) 

for filtername in np.unique(simData[self.filterCol]): 

fmatch = np.where(simData[self.filterCol] == filtername) 

dcr_in_ra[fmatch] = self.dcr_magnitudes[filtername] * dcr_in_ra[fmatch] 

dcr_in_dec[fmatch] = self.dcr_magnitudes[filtername] * dcr_in_dec[fmatch] 

simData['ra_dcr_amp'] = dcr_in_ra 

simData['dec_dcr_amp'] = dcr_in_dec 

return simData 

 

 

class HourAngleStacker(BaseStacker): 

"""Add the Hour Angle for each observation. 

Always in HOURS. 

""" 

def __init__(self, lstCol='observationStartLST', raCol='fieldRA', degrees=True): 

self.units = ['Hours'] 

self.colsAdded = ['HA'] 

self.colsReq = [lstCol, raCol] 

self.lstCol = lstCol 

self.raCol = raCol 

self.degrees = degrees 

 

def _run(self, simData, cols_present=False): 

"""HA = LST - RA """ 

259 ↛ 261line 259 didn't jump to line 261, because the condition on line 259 was never true if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

262 ↛ 263line 262 didn't jump to line 263, because the condition on line 262 was never true if len(simData) == 0: 

return simData 

264 ↛ 268line 264 didn't jump to line 268, because the condition on line 264 was never false if self.degrees: 

ra = np.radians(simData[self.raCol]) 

lst = np.radians(simData[self.lstCol]) 

else: 

ra = simData[self.raCol] 

lst = simData[self.lstCol] 

# Check that LST is reasonable 

271 ↛ 272line 271 didn't jump to line 272, because the condition on line 271 was never true if (np.min(lst) < 0) | (np.max(lst) > 2.*np.pi): 

warnings.warn('LST values are not between 0 and 2 pi') 

# Check that RA is reasonable 

274 ↛ 275line 274 didn't jump to line 275, because the condition on line 274 was never true if (np.min(ra) < 0) | (np.max(ra) > 2.*np.pi): 

warnings.warn('RA values are not between 0 and 2 pi') 

ha = lst - ra 

# Wrap the results so HA between -pi and pi 

ha = np.where(ha < -np.pi, ha + 2. * np.pi, ha) 

ha = np.where(ha > np.pi, ha - 2. * np.pi, ha) 

# Convert radians to hours 

simData['HA'] = ha*12/np.pi 

return simData 

 

 

class ParallacticAngleStacker(BaseStacker): 

"""Add the parallactic angle to each visit. 

If 'degrees' is True, this will be in degrees (as are all other angles). If False, then in radians. 

""" 

def __init__(self, raCol='fieldRA', decCol='fieldDec', degrees=True, mjdCol='observationStartMJD', 

lstCol='observationStartLST', site='LSST'): 

 

self.lstCol = lstCol 

self.raCol = raCol 

self.decCol = decCol 

self.degrees = degrees 

self.mjdCol = mjdCol 

self.site = Site(name=site) 

self.units = ['radians'] 

self.colsAdded = ['PA', 'HA'] 

self.colsReq = [self.raCol, self.decCol, self.mjdCol, self.lstCol] 

self.haStacker = HourAngleStacker(lstCol=lstCol, raCol=raCol, degrees=self.degrees) 

 

def _run(self, simData, cols_present=False): 

# Equation from: 

# http://www.gb.nrao.edu/~rcreager/GBTMetrology/140ft/l0058/gbtmemo52/memo52.html 

# or 

# http://www.gb.nrao.edu/GBT/DA/gbtidl/release2pt9/contrib/contrib/parangle.pro 

308 ↛ 310line 308 didn't jump to line 310, because the condition on line 308 was never true if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

simData = self.haStacker._run(simData) 

312 ↛ 315line 312 didn't jump to line 315, because the condition on line 312 was never false if self.degrees: 

dec = np.radians(simData[self.decCol]) 

else: 

dec = simData[self.decCol] 

simData['PA'] = np.arctan2(np.sin(simData['HA']*np.pi/12.), (np.cos(dec) * 

np.tan(self.site.latitude_rad) - np.sin(dec) * 

np.cos(simData['HA']*np.pi/12.))) 

319 ↛ 321line 319 didn't jump to line 321, because the condition on line 319 was never false if self.degrees: 

simData['PA'] = np.degrees(simData['PA']) 

return simData 

 

 

class FilterColorStacker(BaseStacker): 

"""Translate filters ('u', 'g', 'r' ..) into RGB tuples. 

""" 

def __init__(self, filterCol='filter'): 

self.filter_rgb_map = {'u': (0, 0, 1), # dark blue 

'g': (0, 1, 1), # cyan 

'r': (0, 1, 0), # green 

'i': (1, 0.5, 0.3), # orange 

'z': (1, 0, 0), # red 

'y': (1, 0, 1)} # magenta 

self.filterCol = filterCol 

# self.units used for plot labels 

self.units = ['rChan', 'gChan', 'bChan'] 

# Values required for framework operation: this specifies the names of the new columns. 

self.colsAdded = ['rRGB', 'gRGB', 'bRGB'] 

# Values required for framework operation: this specifies the data columns required from the database. 

self.colsReq = [self.filterCol] 

 

def _run(self, simData, cols_present=False): 

# Translate filter names into numbers. 

if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

filtersUsed = np.unique(simData[self.filterCol]) 

for f in filtersUsed: 

if f not in self.filter_rgb_map: 

raise IndexError('Filter %s not in filter_rgb_map' % (f)) 

match = np.where(simData[self.filterCol] == f)[0] 

simData['rRGB'][match] = self.filter_rgb_map[f][0] 

simData['gRGB'][match] = self.filter_rgb_map[f][1] 

simData['bRGB'][match] = self.filter_rgb_map[f][2] 

return simData 

 

 

class SeasonStacker(BaseStacker): 

"""Add an integer label to show which season a given visit is in. 

 

The season only depends on the RA of the object: we compute the MJD 

when each object is on the meridian at midnight, and subtract 6 

months to get the start date of each season. 

The season index range is 0-10. 

Must wrap 0th and 10th to get a total of 10 seasons. 

""" 

def __init__(self, mjdCol='observationStartMJD', RACol='fieldRA', degrees=True): 

# Names of columns we want to add. 

self.colsAdded = ['year', 'season'] 

# Names of columns we need from database. 

self.colsReq = [mjdCol, RACol] 

# List of units for our new columns. 

self.units = ['', ''] 

# And save the column names. 

self.mjdCol = mjdCol 

self.RACol = RACol 

self.degrees = degrees 

 

def _run(self, simData, cols_present=False): 

if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

# Define year number: (note that opsim defines "years" in flat 365 days). 

year = np.floor((simData[self.mjdCol] - simData[self.mjdCol][0]) / 365) 

# Convert RA to Hours 

if self.degrees: 

objRA = simData[self.RACol]/15.0 

else: 

objRA = np.degrees(simData[self.RACol])/15.0 

# objRA=0 on autumnal equinox. 

# autumnal equinox 2014 happened on Sept 23 --> Equinox MJD 

Equinox = 2456923.5 - 2400000.5 

# Use 365.25 for the length of a year here, because we're dealing with real seasons. 

daysSinceEquinox = 0.5*objRA*(365.25/12.0) # 0.5 to go from RA to month; 365.25/12.0 months to days 

firstSeasonBegan = Equinox + daysSinceEquinox - 0.5*365.25 # in MJD 

# Now we can compute the number of years since the first season 

# began, and so assign a global integer season number: 

globalSeason = np.floor((simData[self.mjdCol] - firstSeasonBegan)/365.25) 

# Subtract off season number of first observation: 

season = globalSeason - np.min(globalSeason) 

simData['year'] = year 

simData['season'] = season 

return simData 

 

 

class OpSimFieldStacker(BaseStacker): 

"""Add the fieldId of the closest OpSim field for each RA/Dec pointing. 

 

Parameters 

---------- 

raCol : str, opt 

Name of the RA column. Default fieldRA. 

decCol : str, opt 

Name of the Dec column. Default fieldDec. 

 

""" 

def __init__(self, raCol='fieldRA', decCol='fieldDec', degrees=True): 

self.colsReq = [raCol, decCol] 

self.colsAdded = ['fieldId'] 

self.units = ['#'] 

self.raCol = raCol 

self.decCol = decCol 

self.degrees = degrees 

fields_db = FieldsDatabase() 

# Returned RA/Dec coordinates in degrees 

fieldid, ra, dec = fields_db.get_id_ra_dec_arrays("select * from Field;") 

asort = np.argsort(fieldid) 

self.tree = _buildTree(np.radians(ra[asort]), 

np.radians(dec[asort])) 

 

def _run(self, simData, cols_present=False): 

432 ↛ 434line 432 didn't jump to line 434, because the condition on line 432 was never true if cols_present: 

# Column already present in data; assume it is correct and does not need recalculating. 

return simData 

 

436 ↛ 438line 436 didn't jump to line 438, because the condition on line 436 was never true if self.degrees: 

# use public method (degrees) 

coord_x, coord_y, coord_z = xyz_from_ra_dec(simData[self.raCol], 

simData[self.decCol]) 

field_ids = self.tree.query_ball_point(list(zip(coord_x, coord_y, coord_z)), xyz_angular_radius()) 

 

else: 

# use private method (radians) 

coord_x, coord_y, coord_z = _xyz_from_ra_dec(simData[self.raCol], 

simData[self.decCol]) 

field_ids = self.tree.query_ball_point(list(zip(coord_x, coord_y, coord_z)), xyz_angular_radius()) 

 

simData['fieldId'] = np.array([ids[0] for ids in field_ids]) + 1 

return simData