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

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

import numpy as np 

from lsst.sims.featureScheduler import features 

from lsst.sims.featureScheduler import utils 

import healpy as hp 

from lsst.sims.skybrightness_pre import M5percentiles 

import matplotlib.pylab as plt 

import warnings 

from lsst.sims.utils import _hpid2RaDec 

 

 

__all__ = ['Base_basis_function', 'Constant_basis_function', 'Target_map_basis_function', 

'Avoid_Fast_Revists', 'Visit_repeat_basis_function', 'M5_diff_basis_function', 

'Strict_filter_basis_function', 'Goal_Strict_filter_basis_function', 

'Filter_change_basis_function', 'Slewtime_basis_function', 

'Aggressive_Slewtime_basis_function', 'Skybrightness_limit_basis_function', 

'CableWrap_unwrap_basis_function', 'Cadence_enhance_basis_function', 'Azimuth_basis_function', 

'Az_modulo_basis_function', 'Dec_modulo_basis_function', 'Map_modulo_basis_function', 

'Template_generate_basis_function', 

'Footprint_nvis_basis_function', 'Third_observation_basis_function', 'Season_coverage_basis_function', 

'N_obs_per_year_basis_function', 'Cadence_in_season_basis_function', 'Near_sun_twilight_basis_function', 

'N_obs_high_am_basis_function'] 

 

 

class Base_basis_function(object): 

"""Class that takes features and computes a reward function when called. 

""" 

 

def __init__(self, nside=None, filtername=None, **kwargs): 

 

# Set if basis function needs to be recalculated if there is a new observation 

self.update_on_newobs = True 

# Set if basis function needs to be recalculated if conditions change 

self.update_on_mjd = True 

# Dict to hold all the features we want to track 

self.survey_features = {} 

# Keep track of the last time the basis function was called. If mjd doesn't change, use cached value 

self.mjd_last = None 

self.value = 0 

# list the attributes to compare to check if basis functions are equal. 

self.attrs_to_compare = [] 

# Do we need to recalculate the basis function 

self.recalc = True 

# Basis functions don't technically all need an nside, but so many do might as well set it here 

if nside is None: 

self.nside = utils.set_default_nside() 

else: 

self.nside = nside 

 

self.filtername = filtername 

 

def add_observation(self, observation, indx=None): 

""" 

Parameters 

---------- 

observation : np.array 

An array with information about the input observation 

indx : np.array 

The indices of the healpix map that the observation overlaps with 

""" 

for feature in self.survey_features: 

self.survey_features[feature].add_observation(observation, indx=indx) 

if self.update_on_newobs: 

self.recalc = True 

 

def check_feasibility(self, conditions): 

"""If there is logic to decide if something is feasible (e.g., only if moon is down), 

it can be calculated here. Helps prevent full __call__ from being called more than needed. 

""" 

return True 

 

def _calc_value(self, conditions, **kwarge): 

self.value = 0 

# Update the last time we had an mjd 

self.mjd_last = conditions.mjd + 0 

self.recalc = False 

return self.value 

 

def __eq__(self): 

# XXX--to work on if we need to make a registry of basis functions. 

pass 

 

def __ne__(self): 

pass 

 

def __call__(self, conditions, **kwargs): 

""" 

Parameters 

---------- 

conditions : lsst.sims.featureScheduler.features.conditions object 

Object that has attributes for all the current conditions. 

 

Return a reward healpix map or a reward scalar. 

""" 

# If we are not feasible, return -inf 

if not self.check_feasibility(conditions): 

return -np.inf 

if self.recalc: 

self.value = self._calc_value(conditions, **kwargs) 

if self.update_on_mjd: 

if conditions.mjd != self.mjd_last: 

self.value = self._calc_value(conditions, **kwargs) 

return self.value 

 

 

class Constant_basis_function(Base_basis_function): 

"""Just add a constant 

""" 

def __call__(self, conditions, **kwargs): 

return 1 

 

 

class Target_map_basis_function(Base_basis_function): 

"""Basis function that tracks number of observations and tries to match a specified spatial distribution 

 

Parameters 

---------- 

filtername: (string 'r') 

The name of the filter for this target map. 

nside: int (default_nside) 

The healpix resolution. 

target_map : numpy array (None) 

A healpix map showing the ratio of observations desired for all points on the sky 

norm_factor : float (0.00010519) 

for converting target map to number of observations. Should be the area of the camera 

divided by the area of a healpixel divided by the sum of all your goal maps. Default 

value assumes LSST foV has 1.75 degree radius and the standard goal maps. If using 

mulitple filters, see lsst.sims.featureScheduler.utils.calc_norm_factor for a utility 

that computes norm_factor. 

out_of_bounds_val : float (-10.) 

Reward value to give regions where there are no observations requested (unitless). 

""" 

def __init__(self, filtername='r', nside=None, target_map=None, 

norm_factor=None, 

out_of_bounds_val=-10.): 

 

super(Target_map_basis_function, self).__init__(nside=nside, filtername=filtername) 

 

if norm_factor is None: 

warnings.warn('No norm_factor set, use utils.calc_norm_factor if using multiple filters.') 

self.norm_factor = 0.00010519 

else: 

self.norm_factor = norm_factor 

 

self.survey_features = {} 

# Map of the number of observations in filter 

self.survey_features['N_obs'] = features.N_observations(filtername=filtername, nside=self.nside) 

# Count of all the observations 

self.survey_features['N_obs_count_all'] = features.N_obs_count(filtername=None) 

if target_map is None: 

self.target_map = utils.generate_goal_map(filtername=filtername, nside=self.nside) 

else: 

self.target_map = target_map 

self.out_of_bounds_area = np.where(self.target_map == 0)[0] 

self.out_of_bounds_val = out_of_bounds_val 

self.result = np.zeros(hp.nside2npix(self.nside), dtype=float) 

self.all_indx = np.arange(self.result.size) 

 

def _calc_value(self, conditions, indx=None): 

""" 

Parameters 

---------- 

indx : list (None) 

Index values to compute, if None, full map is computed 

Returns 

------- 

Healpix reward map 

""" 

result = self.result.copy() 

if indx is None: 

indx = self.all_indx 

 

# Find out how many observations we want now at those points 

goal_N = self.target_map[indx] * self.survey_features['N_obs_count_all'].feature * self.norm_factor 

 

result[indx] = goal_N - self.survey_features['N_obs'].feature[indx] 

result[self.out_of_bounds_area] = self.out_of_bounds_val 

 

return result 

 

 

def azRelPoint(azs, pointAz): 

azRelMoon = (azs - pointAz) % (2.0*np.pi) 

if isinstance(azs, np.ndarray): 

over = np.where(azRelMoon > np.pi) 

azRelMoon[over] = 2. * np.pi - azRelMoon[over] 

else: 

if azRelMoon > np.pi: 

azRelMoon = 2.0 * np.pi - azRelMoon 

return azRelMoon 

 

 

class N_obs_high_am_basis_function(Base_basis_function): 

"""Reward only reward/count observations at high airmass 

""" 

 

def __init__(self, nside=None, filtername='r', footprint=None, n_obs=3, season=300., 

am_limits=[1.5, 2.2], out_of_bounds_val=np.nan): 

super(N_obs_high_am_basis_function, self).__init__(nside=nside, filtername=filtername) 

self.footprint = footprint 

self.out_footprint = np.where((footprint == 0) | np.isnan(footprint)) 

self.am_limits = am_limits 

self.season = season 

self.survey_features['last_n_mjds'] = features.Last_N_obs_times(nside=nside, filtername=filtername, 

n_obs=n_obs) 

 

self.result = np.zeros(hp.nside2npix(self.nside), dtype=float) + out_of_bounds_val 

self.out_of_bounds_val = out_of_bounds_val 

 

def add_observation(self, observation, indx=None): 

""" 

Parameters 

---------- 

observation : np.array 

An array with information about the input observation 

indx : np.array 

The indices of the healpix map that the observation overlaps with 

""" 

 

# Only count the observations if they are at the airmass limits 

if (observation['airmass'] > np.min(self.am_limits)) & (observation['airmass'] < np.max(self.am_limits)): 

for feature in self.survey_features: 

self.survey_features[feature].add_observation(observation, indx=indx) 

if self.update_on_newobs: 

self.recalc = True 

 

def check_feasibility(self, conditions): 

"""If there is logic to decide if something is feasible (e.g., only if moon is down), 

it can be calculated here. Helps prevent full __call__ from being called more than needed. 

""" 

result = True 

reward = self._calc_value(conditions) 

# If there are no non-NaN values, we're not feasible now 

if True not in np.isfinite(reward): 

result = False 

 

return result 

 

def _calc_value(self, conditions, indx=None): 

result = self.result.copy() 

behind_pix = np.where(((conditions.mjd-self.survey_features['last_n_mjds'].feature[0]) > self.season) & 

(conditions.airmass > np.min(self.am_limits)) & 

(conditions.airmass < np.max(self.am_limits))) 

result[behind_pix] = 1 

result[self.out_footprint] = self.out_of_bounds_val 

 

# Update the last time we had an mjd 

self.mjd_last = conditions.mjd + 0 

self.recalc = False 

self.value = result 

 

return result 

 

 

class N_obs_per_year_basis_function(Base_basis_function): 

"""Reward areas that have not been observed N-times in the last year 

 

Parameters 

---------- 

filtername : str ('r') 

The filter to track 

footprint : np.array 

Should be a HEALpix map. Values of 0 or np.nan will be ignored. 

n_obs : int (3) 

The number of observations to demand 

season : float (300) 

The amount of time to allow pass before marking a region as "behind". Default 365.25 (days). 

season_start_hour : float (-2) 

When to start the season relative to RA 180 degrees away from the sun (hours) 

season_end_hour : float (2) 

When to consider a season ending, the RA relative to the sun + 180 degrees. (hours) 

""" 

def __init__(self, filtername='r', nside=None, footprint=None, n_obs=3, season=300, 

season_start_hour=-4., season_end_hour=2.): 

super(N_obs_per_year_basis_function, self).__init__(nside=nside, filtername=filtername) 

self.footprint = footprint 

self.n_obs = n_obs 

self.season = season 

self.season_start_hour = (season_start_hour)*np.pi/12. # To radians 

self.season_end_hour = season_end_hour*np.pi/12. # To radians 

 

self.survey_features['last_n_mjds'] = features.Last_N_obs_times(nside=nside, filtername=filtername, 

n_obs=n_obs) 

self.result = np.zeros(hp.nside2npix(self.nside), dtype=float) 

self.out_footprint = np.where((footprint == 0) | np.isnan(footprint)) 

 

def _calc_value(self, conditions, indx=None): 

 

result = self.result.copy() 

behind_pix = np.where((conditions.mjd-self.survey_features['last_n_mjds'].feature[0]) > self.season) 

result[behind_pix] = 1 

 

# let's ramp up the weight depending on how far into the observing season the healpix is 

mid_season_ra = (conditions.sunRA + np.pi) % (2.*np.pi) 

# relative RA 

relative_ra = (conditions.ra - mid_season_ra) % (2.*np.pi) 

relative_ra = (self.season_end_hour - relative_ra) % (2.*np.pi) 

# ok, now  

relative_ra[np.where(relative_ra > (self.season_end_hour-self.season_start_hour))] = 0 

 

weight = relative_ra/(self.season_end_hour - self.season_start_hour) 

result *= weight 

 

# mask off anything outside the footprint 

result[self.out_footprint] = 0 

 

return result 

 

 

class Cadence_in_season_basis_function(Base_basis_function): 

"""Drive observations at least every N days in a given area 

 

Parameters 

---------- 

drive_map : np.array 

A HEALpix map with values of 1 where the cadence should be driven. 

filtername : str 

The filters that can count 

season_span : float (2.5) 

How long to consider a spot "in_season" (hours) 

cadence : float (2.5) 

How long to wait before activating the basis function (days) 

""" 

 

def __init__(self, drive_map, filtername='griz', season_span=2.5, cadence=2.5, nside=None): 

super(Cadence_in_season_basis_function, self).__init__(nside=nside, filtername=filtername) 

self.drive_map = drive_map 

self.season_span = season_span/12.*np.pi # To radians 

self.cadence = cadence 

self.survey_features['last_observed'] = features.Last_observed(nside=nside, filtername=filtername) 

self.result = np.zeros(hp.nside2npix(self.nside), dtype=float) 

 

def _calc_value(self, conditions, indx=None): 

result = self.result.copy() 

ra_mid_season = (conditions.sunRA + np.pi) % (2.*np.pi) 

 

angle_to_mid_season = np.abs(conditions.ra - ra_mid_season) 

over = np.where(angle_to_mid_season > np.pi) 

angle_to_mid_season[over] = 2.*np.pi - angle_to_mid_season[over] 

 

days_lag = conditions.mjd - self.survey_features['last_observed'].feature 

 

active_pix = np.where((days_lag >= self.cadence) & (self.drive_map == 1) & (angle_to_mid_season < self.season_span)) 

result[active_pix] = 1. 

 

return result 

 

 

class Season_coverage_basis_function(Base_basis_function): 

"""Basis function to encourage N observations per observing season 

 

Parameters 

---------- 

footprint : healpix map (None) 

The footprint where one should demand coverage every season 

n_per_season : int (3) 

The number of observations to attempt to gather every season 

offset : healpix map 

The offset to apply when computing the current season over the sky. utils.create_season_offset 

is helpful for making this 

season_frac_start : float (0.5) 

Only start trying to gather observations after a season is fractionally this far over. 

""" 

def __init__(self, filtername='r', nside=None, footprint=None, n_per_season=3, offset=None, 

season_frac_start=0.5): 

super(Season_coverage_basis_function, self).__init__(nside=nside, filtername=filtername) 

 

self.n_per_season = n_per_season 

self.footprint = footprint 

self.survey_features['n_obs_season'] = features.N_observations_current_season(filtername=filtername, 

nside=nside, offset=offset) 

self.result = np.zeros(hp.nside2npix(self.nside), dtype=float) 

self.season_frac_start = season_frac_start 

self.offset = offset 

 

def _calc_value(self, conditions, indx=None): 

result = self.result.copy() 

season = utils.season_calc(conditions.night, offset=self.offset, floor=False) 

# Find the area that still needs observation 

feature = self.survey_features['n_obs_season'].feature 

not_enough = np.where((self.footprint > 0) & (feature < self.n_per_season) & 

((season-np.floor(season) > self.season_frac_start)) & 

(season >= 0)) 

result[not_enough] = 1 

return result 

 

 

class Footprint_nvis_basis_function(Base_basis_function): 

"""Basis function to drive observations of a given footprint. Good to target of opportunity targets 

where one might want to observe a region 3 times. 

 

Parameters 

---------- 

footprint : np.array 

A healpix array (1 for desired, 0 for not desired) of the target footprint. 

nvis : int (1) 

The number of visits to try and gather 

""" 

def __init__(self, filtername='r', nside=None, footprint=None, 

nvis=1, out_of_bounds_val=np.nan): 

super(Footprint_nvis_basis_function, self).__init__(nside=nside, filtername=filtername) 

self.footprint = footprint 

self.nvis = nvis 

 

# Have a feature that tracks how many observations we have 

self.survey_features = {} 

# Map of the number of observations in filter 

self.survey_features['N_obs'] = features.N_observations(filtername=filtername, nside=self.nside) 

self.result = np.zeros(hp.nside2npix(nside)) 

self.result.fill(out_of_bounds_val) 

self.out_of_bounds_val = out_of_bounds_val 

 

def _calc_value(self, conditions, indx=None): 

result = self.result.copy() 

diff = self.footprint*self.nvis - self.survey_features['N_obs'].feature 

 

result[np.where(diff > 0)] = 1 

 

# Any spot where we have enough visits is out of bounds now. 

result[np.where(diff <= 0)] = self.out_of_bounds_val 

return result 

 

 

class Third_observation_basis_function(Base_basis_function): 

"""If there have been observations in two filters long enough ago, go for a third 

 

Parameters 

---------- 

gap_min : float (40.) 

The minimum time gap to consider a pixel good (minutes) 

gap_max : float (120) 

The maximum time to consider going for a pair (minutes) 

""" 

 

def __init__(self, nside=32, filtername1='r', filtername2='z', gap_min=40., gap_max=120.): 

super(Third_observation_basis_function, self).__init__(nside=nside) 

self.filtername1 = filtername1 

self.filtername2 = filtername2 

self.gap_min = gap_min/60./24. 

self.gap_max = gap_max/60./24. 

 

self.survey_features = {} 

self.survey_features['last_obs_f1'] = features.Last_observed(filtername=filtername1, nside=nside) 

self.survey_features['last_obs_f2'] = features.Last_observed(filtername=filtername2, nside=nside) 

self.result = np.empty(hp.nside2npix(self.nside)) 

self.result.fill(np.nan) 

 

def _calc_value(self, conditions, indx=None): 

result = self.result.copy() 

d1 = conditions.mjd - self.survey_features['last_obs_f1'].feature 

d2 = conditions.mjd - self.survey_features['last_obs_f2'].feature 

good = np.where((d1 > self.gap_min) & (d1 < self.gap_max) & 

(d2 > self.gap_min) & (d2 < self.gap_max)) 

result[good] = 1 

return result 

 

 

class Avoid_Fast_Revists(Base_basis_function): 

"""Marks targets as unseen if they are in a specified time window in order to avoid fast revisits. 

 

Parameters 

---------- 

filtername: (string 'r') 

The name of the filter for this target map. 

gap_min : float (25.) 

Minimum time for the gap (minutes). 

nside: int (default_nside) 

The healpix resolution. 

penalty_val : float (np.nan) 

The reward value to use for regions to penalize. Will be masked if set to np.nan (default). 

""" 

def __init__(self, filtername='r', nside=None, gap_min=25., 

penalty_val=np.nan): 

super(Avoid_Fast_Revists, self).__init__(nside=nside, filtername=filtername) 

 

self.filtername = filtername 

self.penalty_val = penalty_val 

 

self.gap_min = gap_min/60./24. 

self.nside = nside 

 

self.survey_features = dict() 

self.survey_features['Last_observed'] = features.Last_observed(filtername=filtername, nside=nside) 

 

def _calc_value(self, conditions, indx=None): 

result = np.ones(hp.nside2npix(self.nside), dtype=float) 

if indx is None: 

indx = np.arange(result.size) 

diff = conditions.mjd - self.survey_features['Last_observed'].feature[indx] 

bad = np.where(diff < self.gap_min)[0] 

result[indx[bad]] = self.penalty_val 

return result 

 

 

class Near_sun_twilight_basis_function(Base_basis_function): 

"""Reward looking into the twilight for NEOs at high airmass 

 

Parameters 

---------- 

max_airmass : float (2.5) 

The maximum airmass to try and observe (unitless) 

""" 

 

def __init__(self, nside=None, max_airmass=2.5): 

super(Near_sun_twilight_basis_function, self).__init__(nside=nside) 

self.max_airmass = max_airmass 

self.result = np.zeros(hp.nside2npix(self.nside)) 

 

def _calc_value(self, conditions, indx=None): 

result = self.result.copy() 

good_pix = np.where((conditions.airmass < self.max_airmass) & (conditions.az_to_sun < np.pi/2.)) 

result[good_pix] = conditions.airmass[good_pix] / self.max_airmass 

return result 

 

 

class Visit_repeat_basis_function(Base_basis_function): 

""" 

Basis function to reward re-visiting an area on the sky. Looking for Solar System objects. 

 

Parameters 

---------- 

gap_min : float (15.) 

Minimum time for the gap (minutes) 

gap_max : float (45.) 

Maximum time for a gap 

filtername : str ('r') 

The filter(s) to count with pairs 

npairs : int (1) 

The number of pairs of observations to attempt to gather 

""" 

def __init__(self, gap_min=25., gap_max=45., 

filtername='r', nside=None, npairs=1): 

 

super(Visit_repeat_basis_function, self).__init__(nside=nside, filtername=filtername) 

 

self.gap_min = gap_min/60./24. 

self.gap_max = gap_max/60./24. 

self.npairs = npairs 

 

self.survey_features = {} 

# Track the number of pairs that have been taken in a night 

self.survey_features['Pair_in_night'] = features.Pair_in_night(filtername=filtername, 

gap_min=gap_min, gap_max=gap_max, 

nside=nside) 

# When was it last observed 

# XXX--since this feature is also in Pair_in_night, I should just access that one! 

self.survey_features['Last_observed'] = features.Last_observed(filtername=filtername, 

nside=nside) 

 

def _calc_value(self, conditions, indx=None): 

result = np.zeros(hp.nside2npix(self.nside), dtype=float) 

if indx is None: 

indx = np.arange(result.size) 

diff = conditions.mjd - self.survey_features['Last_observed'].feature[indx] 

good = np.where((diff >= self.gap_min) & (diff <= self.gap_max) & 

(self.survey_features['Pair_in_night'].feature[indx] < self.npairs))[0] 

result[indx[good]] += 1. 

return result 

 

 

class M5_diff_basis_function(Base_basis_function): 

"""Basis function based on the 5-sigma depth. 

Look up the best depth a healpixel achieves, and compute 

the limiting depth difference given current conditions 

""" 

def __init__(self, filtername='r', nside=None): 

 

super(M5_diff_basis_function, self).__init__(nside=nside, filtername=filtername) 

# Need to look up the deepest m5 values for all the healpixels 

m5p = M5percentiles() 

self.dark_map = m5p.dark_map(filtername=filtername, nside_out=self.nside) 

 

def _calc_value(self, conditions, indx=None): 

# No way to get the sign on this right the first time. 

result = conditions.M5Depth[self.filtername] - self.dark_map 

return result 

 

 

class Strict_filter_basis_function(Base_basis_function): 

"""Remove the bonus for staying in the same filter if certain conditions are met. 

 

If the moon rises/sets or twilight starts/ends, it makes a lot of sense to consider 

a filter change. This basis function rewards if it matches the current filter, the moon rises or sets, 

twilight starts or stops, or there has been a large gap since the last observation. 

 

Paramters 

--------- 

time_lag : float (10.) 

If there is a gap between observations longer than this, let the filter change (minutes) 

twi_change : float (-18.) 

The sun altitude to consider twilight starting/ending (degrees) 

note_free : str ('DD') 

No penalty for changing filters if the last observation note field includes string.  

Useful for giving a free filter change after deep drilling sequence 

""" 

def __init__(self, time_lag=10., filtername='r', twi_change=-18., note_free='DD'): 

 

super(Strict_filter_basis_function, self).__init__(filtername=filtername) 

 

self.time_lag = time_lag/60./24. # Convert to days 

self.twi_change = np.radians(twi_change) 

 

self.survey_features = {} 

self.survey_features['Last_observation'] = features.Last_observation() 

self.note_free = note_free 

 

def _calc_value(self, conditions, **kwargs): 

# Did the moon set or rise since last observation? 

moon_changed = conditions.moonAlt * self.survey_features['Last_observation'].feature['moonAlt'] < 0 

 

# Are we already in the filter (or at start of night)? 

in_filter = (conditions.current_filter == self.filtername) | (conditions.current_filter is None) 

 

# Has enough time past? 

time_past = (conditions.mjd - self.survey_features['Last_observation'].feature['mjd']) > self.time_lag 

 

# Did twilight start/end? 

twi_changed = (conditions.sunAlt - self.twi_change) * (self.survey_features['Last_observation'].feature['sunAlt']- self.twi_change) < 0 

 

# Did we just finish a DD sequence 

wasDD = self.note_free in self.survey_features['Last_observation'].feature['note'] 

 

# Is the filter mounted? 

mounted = self.filtername in conditions.mounted_filters 

 

if (moon_changed | in_filter | time_past | twi_changed | wasDD) & mounted: 

result = 1. 

else: 

result = 0. 

 

return result 

 

 

class Goal_Strict_filter_basis_function(Base_basis_function): 

"""Remove the bonus for staying in the same filter if certain conditions are met. 

 

If the moon rises/sets or twilight starts/ends, it makes a lot of sense to consider 

a filter change. This basis function rewards if it matches the current filter, the moon rises or sets, 

twilight starts or stops, or there has been a large gap since the last observation. 

 

Parameters 

--------- 

time_lag_min: Minimum time after a filter change for which a new filter change will receive zero reward, or 

be denied at all (see unseen_before_lag). 

time_lag_max: Time after a filter change where the reward for changing filters achieve its maximum. 

time_lag_boost: Time after a filter change to apply a boost on the reward. 

boost_gain: A multiplier factor for the reward after time_lag_boost. 

unseen_before_lag: If True will make it impossible to switch filter before time_lag has passed. 

filtername: The filter for which this basis function will be used. 

tag: When using filter proportion use only regions with this tag to count for observations. 

twi_change: Switch reward on when twilight changes. 

proportion: The expected filter proportion distribution. 

aways_available: If this is true the basis function will aways be computed regardless of the feasibility. If 

False a more detailed feasibility check is performed. When set to False, it may speed up the computation 

process by avoiding to compute the reward functions paired with this bf, when observation is not feasible. 

 

""" 

 

def __init__(self, time_lag_min=10., time_lag_max=30., 

time_lag_boost=60., boost_gain=2.0, unseen_before_lag=False, 

filtername='r', tag=None, twi_change=-18., proportion=1.0, aways_available=False): 

 

super(Goal_Strict_filter_basis_function, self).__init__(filtername=filtername) 

 

self.time_lag_min = time_lag_min / 60. / 24. # Convert to days 

self.time_lag_max = time_lag_max / 60. / 24. # Convert to days 

self.time_lag_boost = time_lag_boost / 60. / 24. 

self.boost_gain = boost_gain 

self.unseen_before_lag = unseen_before_lag 

 

self.twi_change = np.radians(twi_change) 

self.proportion = proportion 

self.aways_available = aways_available 

 

self.survey_features = {} 

self.survey_features['Last_observation'] = features.Last_observation() 

self.survey_features['Last_filter_change'] = features.LastFilterChange() 

self.survey_features['N_obs_all'] = features.N_obs_count(filtername=None) 

self.survey_features['N_obs'] = features.N_obs_count(filtername=filtername, 

tag=tag) 

 

def filter_change_bonus(self, time): 

 

lag_min = self.time_lag_min 

lag_max = self.time_lag_max 

 

a = 1. / (lag_max - lag_min) 

b = -a * lag_min 

 

bonus = a * time + b 

# How far behind we are with respect to proportion? 

nobs = self.survey_features['N_obs'].feature 

nobs_all = self.survey_features['N_obs_all'].feature 

goal = self.proportion 

# need = 1. - nobs / nobs_all + goal if nobs_all > 0 else 1. + goal 

need = goal / nobs * nobs_all if nobs > 0 else 1. 

# need /= goal 

if hasattr(time, '__iter__'): 

before_lag = np.where(time <= lag_min) 

bonus[before_lag] = -np.inf if self.unseen_before_lag else 0. 

after_lag = np.where(time >= lag_max) 

bonus[after_lag] = 1. if time < self.time_lag_boost else self.boost_gain 

elif time <= lag_min: 

return -np.inf if self.unseen_before_lag else 0. 

elif time >= lag_max: 

return 1. if time < self.time_lag_boost else self.boost_gain 

 

return bonus * need 

 

def check_feasibility(self, conditions): 

""" 

This method makes a pre-check of the feasibility of this basis function. If a basis function return False 

on the feasibility check, it won't computed at all. 

 

:return: 

""" 

 

# Make a quick check about the feasibility of this basis function. If current filter is none, telescope 

# is parked and we could, in principle, switch to any filter. If this basis function computes reward for 

# the current filter, then it is also feasible. At last we check for an "aways_available" flag. Meaning, we 

# force this basis function to be aways be computed. 

if conditions.current_filter is None or conditions.current_filter == self.filtername or self.aways_available: 

return True 

 

# If we arrive here, we make some extra checks to make sure this bf is feasible and should be computed. 

 

# Did the moon set or rise since last observation? 

moon_changed = conditions.moonAlt * self.survey_features['Last_observation'].feature['moonAlt'] < 0 

 

# Are we already in the filter (or at start of night)? 

not_in_filter = (conditions.current_filter != self.filtername) 

 

# Has enough time past? 

lag = conditions.mjd - self.survey_features['Last_filter_change'].feature['mjd'] 

time_past = lag > self.time_lag_min 

 

# Did twilight start/end? 

twi_changed = (conditions.sunAlt - self.twi_change) * \ 

(self.survey_features['Last_observation'].feature['sunAlt'] - self.twi_change) < 0 

 

# Did we just finish a DD sequence 

wasDD = self.survey_features['Last_observation'].feature['note'] == 'DD' 

 

# Is the filter mounted? 

mounted = self.filtername in conditions.mounted_filters 

 

if (moon_changed | time_past | twi_changed | wasDD) & mounted & not_in_filter: 

return True 

else: 

return False 

 

def _calc_value(self, conditions, **kwargs): 

 

if conditions.current_filter is None: 

return 0. # no bonus if no filter is mounted 

# elif self.condition_features['Current_filter'].feature == self.filtername: 

# return 0. # no bonus if on the filter already 

 

# Did the moon set or rise since last observation? 

moon_changed = conditions.moonAlt * \ 

self.survey_features['Last_observation'].feature['moonAlt'] < 0 

 

# Are we already in the filter (or at start of night)? 

# not_in_filter = (self.condition_features['Current_filter'].feature != self.filtername) 

 

# Has enough time past? 

lag = conditions.mjd - self.survey_features['Last_filter_change'].feature['mjd'] 

time_past = lag > self.time_lag_min 

 

# Did twilight start/end? 

twi_changed = (conditions.sunAlt - self.twi_change) * ( 

self.survey_features['Last_observation'].feature['sunAlt'] - self.twi_change) < 0 

 

# Did we just finish a DD sequence 

wasDD = self.survey_features['Last_observation'].feature['note'] == 'DD' 

 

# Is the filter mounted? 

mounted = self.filtername in conditions.mounted_filters 

 

if (moon_changed | time_past | twi_changed | wasDD) & mounted: 

result = self.filter_change_bonus(lag) if time_past else 0. 

else: 

result = -100. if self.unseen_before_lag else 0. 

 

return result 

 

 

class Filter_change_basis_function(Base_basis_function): 

"""Reward staying in the current filter. 

""" 

def __init__(self, filtername='r'): 

super(Filter_change_basis_function, self).__init__(filtername=filtername) 

 

def _calc_value(self, conditions, **kwargs): 

 

if (conditions.current_filter == self.filtername) | (conditions.current_filter is None): 

result = 1. 

else: 

result = 0. 

return result 

 

 

class Slewtime_basis_function(Base_basis_function): 

"""Reward slews that take little time 

 

Parameters 

---------- 

max_time : float (135) 

The estimated maximum slewtime (seconds). Used to normalize so the basis function 

spans ~ -1-0 in reward units. 

""" 

def __init__(self, max_time=135., filtername='r', nside=None): 

super(Slewtime_basis_function, self).__init__(nside=nside, filtername=filtername) 

 

self.maxtime = max_time 

self.nside = nside 

self.filtername = filtername 

self.result = np.zeros(hp.nside2npix(nside), dtype=float) 

 

def add_observation(self, observation, indx=None): 

# No tracking of observations in this basis function. Purely based on conditions. 

pass 

 

def _calc_value(self, conditions, indx=None): 

# If we are in a different filter, the Filter_change_basis_function will take it 

if conditions.current_filter != self.filtername: 

result = 0 

else: 

# Need to make sure smaller slewtime is larger reward. 

if np.size(conditions.slewtime) > 1: 

result = self.result.copy() 

good = ~np.isnan(conditions.slewtime) 

result[good] = -conditions.slewtime[good]/self.maxtime 

else: 

result = -conditions.slewtime/self.maxtime 

return result 

 

 

class Aggressive_Slewtime_basis_function(Base_basis_function): 

"""Reward slews that take little time 

 

XXX--not sure how this is different from Slewtime_basis_function? 

Looks like it's checking the slewtime to the field position rather than the healpix maybe? 

""" 

 

def __init__(self, max_time=135., order=1., hard_max=None, filtername='r', nside=None): 

super(Aggressive_Slewtime_basis_function, self).__init__(nside=nside, filtername=filtername) 

 

self.maxtime = max_time 

self.hard_max = hard_max 

self.order = order 

self.result = np.zeros(hp.nside2npix(nside), dtype=float) 

 

def _calc_value(self, conditions, indx=None): 

# If we are in a different filter, the Filter_change_basis_function will take it 

if conditions.current_filter != self.filtername: 

result = 0. 

else: 

# Need to make sure smaller slewtime is larger reward. 

if np.size(self.condition_features['slewtime'].feature) > 1: 

result = self.result.copy() 

result.fill(np.nan) 

 

good = np.where(np.bitwise_and(conditions.slewtime > 0., 

conditions.slewtime < self.maxtime)) 

result[good] = ((self.maxtime - conditions.slewtime[good]) / 

self.maxtime) ** self.order 

if self.hard_max is not None: 

not_so_good = np.where(conditions.slewtime > self.hard_max) 

result[not_so_good] -= 10. 

fields = np.unique(conditions.hp2fields[good]) 

for field in fields: 

hp_indx = np.where(conditions.hp2fields == field) 

result[hp_indx] = np.min(result[hp_indx]) 

else: 

result = (self.maxtime - conditions.slewtime) / self.maxtime 

return result 

 

 

class Skybrightness_limit_basis_function(Base_basis_function): 

"""Mask regions that are outside a sky brightness limit 

 

XXX--TODO: This should probably go to the mask basis functions. 

 

Parameters 

---------- 

min : float (20.) 

The minimum sky brightness (mags). 

max : float (30.) 

The maximum sky brightness (mags). 

 

""" 

def __init__(self, nside=None, filtername='r', min=20., max=30.): 

super(Skybrightness_limit_basis_function, self).__init__(nside=nside, filtername=filtername) 

 

self.min = min 

self.max = max 

self.result = np.empty(hp.nside2npix(self.nside), dtype=float) 

self.result.fill(np.nan) 

 

def _calc_value(self, conditions, indx=None): 

result = self.result.copy() 

 

good = np.where(np.bitwise_and(conditions.skybrightness[self.filtername] > self.min, 

conditions.skybrightness[self.filtername] < self.max)) 

result[good] = 1.0 

 

return result 

 

 

class CableWrap_unwrap_basis_function(Base_basis_function): 

""" 

Parameters 

---------- 

minAz : float (20.) 

The minimum azimuth to activate bf (degrees) 

maxAz : float (82.) 

The maximum azimuth to activate bf (degrees) 

unwrap_until: float (90.) 

The window in which the bf is activated (degrees) 

""" 

def __init__(self, nside=None, minAz=-270., maxAz=270., minAlt=20., maxAlt=82., 

activate_tol=20., delta_unwrap=1.2, unwrap_until=70., max_duration=30.): 

super(CableWrap_unwrap_basis_function, self).__init__(nside=nside) 

 

self.minAz = np.radians(minAz) 

self.maxAz = np.radians(maxAz) 

 

self.activate_tol = np.radians(activate_tol) 

self.delta_unwrap = np.radians(delta_unwrap) 

self.unwrap_until = np.radians(unwrap_until) 

 

self.minAlt = np.radians(minAlt) 

self.maxAlt = np.radians(maxAlt) 

# Convert to half-width for convienence 

self.nside = nside 

self.active = False 

self.unwrap_direction = 0. # either -1., 0., 1. 

self.max_duration = max_duration/60./24. # Convert to days 

self.activation_time = None 

self.result = np.zeros(hp.nside2npix(self.nside), dtype=float) 

 

def _calc_value(self, conditions, indx=None): 

 

result = self.result.copy() 

 

current_abs_rad = np.radians(conditions.az) 

unseen = np.where(np.bitwise_or(conditions.alt < self.minAlt, 

conditions.alt > self.maxAlt)) 

result[unseen] = np.nan 

 

if (self.minAz + self.activate_tol < current_abs_rad < self.maxAz - self.activate_tol) and not self.active: 

return result 

elif self.active and self.unwrap_direction == 1 and current_abs_rad > self.minAz+self.unwrap_until: 

self.active = False 

self.unwrap_direction = 0. 

self.activation_time = None 

return result 

elif self.active and self.unwrap_direction == -1 and current_abs_rad < self.maxAz-self.unwrap_until: 

self.active = False 

self.unwrap_direction = 0. 

self.activation_time = None 

return result 

elif (self.activation_time is not None and 

conditions.mjd - self.activation_time > self.max_duration): 

self.active = False 

self.unwrap_direction = 0. 

self.activation_time = None 

return result 

 

if not self.active: 

self.activation_time = conditions.mjd 

if current_abs_rad < 0.: 

self.unwrap_direction = 1 # clock-wise unwrap 

else: 

self.unwrap_direction = -1 # counter-clock-wise unwrap 

 

self.active = True 

 

max_abs_rad = self.maxAz 

min_abs_rad = self.minAz 

 

TWOPI = 2.*np.pi 

 

# Compute distance and accumulated az. 

norm_az_rad = np.divmod(conditions.az - min_abs_rad, TWOPI)[1] + min_abs_rad 

distance_rad = divmod(norm_az_rad - current_abs_rad, TWOPI)[1] 

get_shorter = np.where(distance_rad > np.pi) 

distance_rad[get_shorter] -= TWOPI 

accum_abs_rad = current_abs_rad + distance_rad 

 

# Compute wrap regions and fix distances 

mask_max = np.where(accum_abs_rad > max_abs_rad) 

distance_rad[mask_max] -= TWOPI 

mask_min = np.where(accum_abs_rad < min_abs_rad) 

distance_rad[mask_min] += TWOPI 

 

# Step-2: Repeat but now with compute reward to unwrap using specified delta_unwrap 

unwrap_current_abs_rad = current_abs_rad - (np.abs(self.delta_unwrap) if self.unwrap_direction > 0 

else -np.abs(self.delta_unwrap)) 

unwrap_distance_rad = divmod(norm_az_rad - unwrap_current_abs_rad, TWOPI)[1] 

unwrap_get_shorter = np.where(unwrap_distance_rad > np.pi) 

unwrap_distance_rad[unwrap_get_shorter] -= TWOPI 

unwrap_distance_rad = np.abs(unwrap_distance_rad) 

 

if self.unwrap_direction < 0: 

mask = np.where(accum_abs_rad > unwrap_current_abs_rad) 

else: 

mask = np.where(accum_abs_rad < unwrap_current_abs_rad) 

 

# Finally build reward map 

result = (1. - unwrap_distance_rad/np.max(unwrap_distance_rad))**2. 

result[mask] = 0. 

result[unseen] = np.nan 

 

return result 

 

 

class Cadence_enhance_basis_function(Base_basis_function): 

"""Drive a certain cadence 

Parameters 

---------- 

filtername : str ('gri') 

The filter(s) that should be grouped together 

supress_window : list of float 

The start and stop window for when observations should be repressed (days) 

apply_area : healpix map 

The area over which to try and drive the cadence. Good values as 1, no candece drive 0. 

Probably works as a bool array too.""" 

def __init__(self, filtername='gri', nside=None, 

supress_window=[0, 1.8], supress_val=-0.5, 

enhance_window=[2.1, 3.2], enhance_val=1., 

apply_area=None): 

super(Cadence_enhance_basis_function, self).__init__(nside=nside, filtername=filtername) 

 

self.supress_window = np.sort(supress_window) 

self.supress_val = supress_val 

self.enhance_window = np.sort(enhance_window) 

self.enhance_val = enhance_val 

 

self.survey_features = {} 

self.survey_features['last_observed'] = features.Last_observed(filtername=filtername) 

 

self.empty = np.zeros(hp.nside2npix(self.nside), dtype=float) 

# No map, try to drive the whole area 

if apply_area is None: 

self.apply_indx = np.arange(self.empty.size) 

else: 

self.apply_indx = np.where(apply_area != 0)[0] 

 

def _calc_value(self, conditions, indx=None): 

# copy an empty array 

result = self.empty.copy() 

if indx is not None: 

ind = np.intersect1d(indx, self.apply_indx) 

else: 

ind = self.apply_indx 

if np.size(ind) == 0: 

result = 0 

else: 

mjd_diff = conditions.mjd - self.survey_features['last_observed'].feature[ind] 

to_supress = np.where((mjd_diff > self.supress_window[0]) & (mjd_diff < self.supress_window[1])) 

result[ind[to_supress]] = self.supress_val 

to_enhance = np.where((mjd_diff > self.enhance_window[0]) & (mjd_diff < self.enhance_window[1])) 

result[ind[to_enhance]] = self.enhance_val 

return result 

 

 

class Azimuth_basis_function(Base_basis_function): 

"""Reward staying in the same azimuth range. Possibly better than using slewtime, especially when selecting a large area of sky. 

 

Parameters 

---------- 

 

""" 

 

def __init__(self, nside=None): 

super(Azimuth_basis_function, self).__init__(nside=nside) 

 

def _calc_value(self, conditions, indx=None): 

az_dist = conditions.az - conditions.telAz 

az_dist = az_dist % (2.*np.pi) 

over = np.where(az_dist > np.pi) 

az_dist[over] = 2. * np.pi - az_dist[over] 

# Normalize sp between 0 and 1 

result = az_dist/np.pi 

return result 

 

 

class Az_modulo_basis_function(Base_basis_function): 

"""Try to replicate the Rothchild et al cadence forcing by only observing on limited az ranges per night. 

 

Parameters 

---------- 

az_limits : list of float pairs (None) 

The azimuth limits (degrees) to use. 

""" 

def __init__(self, nside=None, az_limits=None, out_of_bounds_val=-1.): 

super(Az_modulo_basis_function, self).__init__(nside=nside) 

self.result = np.ones(hp.nside2npix(self.nside)) 

if az_limits is None: 

spread = 100./2. 

self.az_limits = np.radians([[360-spread, spread], 

[90.-spread, 90.+spread], 

[180.-spread, 180.+spread]]) 

else: 

self.az_limits = np.radians(az_limits) 

self.mod_val = len(self.az_limits) 

self.out_of_bounds_val = out_of_bounds_val 

 

def _calc_value(self, conditions, indx=None): 

result = self.result.copy() 

az_lim = self.az_limits[np.max(conditions.night) % self.mod_val] 

 

if az_lim[0] < az_lim[1]: 

out_pix = np.where((conditions.az < az_lim[0]) | (conditions.az > az_lim[1])) 

else: 

out_pix = np.where((conditions.az < az_lim[0]) | (conditions.az > az_lim[1]))[0] 

result[out_pix] = self.out_of_bounds_val 

return result 

 

 

class Dec_modulo_basis_function(Base_basis_function): 

"""Emphasize dec bands on a nightly varying basis 

 

Parameters 

---------- 

dec_limits : list of float pairs (None) 

The azimuth limits (degrees) to use. 

""" 

def __init__(self, nside=None, dec_limits=None, out_of_bounds_val=-1.): 

super(Dec_modulo_basis_function, self).__init__(nside=nside) 

 

npix = hp.nside2npix(nside) 

hpids = np.arange(npix) 

ra, dec = _hpid2RaDec(nside, hpids) 

 

self.results = [] 

 

if dec_limits is None: 

self.dec_limits = np.radians([[-90., -32.8], 

[-32.8, -12.], 

[-12., 35.]]) 

else: 

self.dec_limits = np.radians(dec_limits) 

self.mod_val = len(self.dec_limits) 

self.out_of_bounds_val = out_of_bounds_val 

 

for limits in self.dec_limits: 

good = np.where((dec >= limits[0]) & (dec < limits[1]))[0] 

tmp = np.zeros(npix) 

tmp[good] = 1 

self.results.append(tmp) 

 

def _calc_value(self, conditions, indx=None): 

night_index = np.max(conditions.night % self.mod_val) 

result = self.results[night_index] 

 

return result 

 

 

class Map_modulo_basis_function(Base_basis_function): 

"""Similar to Dec_modulo, but now use input masks 

 

Parameters 

---------- 

inmaps : list of hp arrays 

""" 

def __init__(self, inmaps): 

nside = hp.npix2nside(np.size(inmaps[0])) 

super(Map_modulo_basis_function, self).__init__(nside=nside) 

self.maps = inmaps 

self.mod_val = len(inmaps) 

 

def _calc_value(self, conditions, indx=None): 

indx = np.max(conditions.night % self.mod_val) 

result = self.maps[indx] 

return result 

 

 

class Template_generate_basis_function(Base_basis_function): 

"""Emphasize areas that have not been observed in a long time 

 

Parameters 

---------- 

day_gap : float (250.) 

How long to wait before boosting the reward (days) 

footprint : np.array (None) 

The indices of the healpixels to apply the boost to. Uses the default footprint if None 

""" 

def __init__(self, nside=None, day_gap=250., filtername='r', footprint=None): 

super(Template_generate_basis_function, self).__init__(nside=nside) 

self.day_gap = day_gap 

self.filtername = filtername 

self.survey_features = {} 

self.survey_features['Last_observed'] = features.Last_observed(filtername=filtername) 

self.result = np.zeros(hp.nside2npix(self.nside)) 

if footprint is None: 

fp = utils.standard_goals(nside=nside)[filtername] 

else: 

fp = footprint 

self.out_of_bounds = np.where(fp == 0) 

 

def _calc_value(self, conditions, **kwargs): 

result = self.result.copy() 

overdue = np.where((conditions.mjd - self.survey_features['Last_observed'].feature) > self.day_gap) 

result[overdue] = 1 

result[self.out_of_bounds] = 0 

 

return result