Blame view

src/core/pyros_django/scheduling/A_Scheduler.py 27.9 KB
6cd48351   Etienne Pallier   AgentScheduler is...
1
#!/usr/bin/env python3
df477bca   Alain Klotz   New scheduler
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#
# To launch this agent from the root of Pyros:
#
# Linux console:
# cd /srv/develop/pyros/docker
# ./PYROS_DOCKER_START.sh
#
# Launch from Power Shell:
# To go from docker to Powershell: pyros_user@ORION:~/app$ exit (or Ctrl+d)
# Prompt is now PS ...>
# cd \srv\develop\pyros
# .\PYROS -t new-start -o tnc -fg -a A_Scheduler
#
# Launch from docker:
# To go from Powershell to docker: PS ...> .\PYROS_DOCKER_SHELL
# Prompt is now pyros_user@ORION:~/app$
# ./PYROS -t new-start -o tnc -fg -a A_Scheduler
# ./PYROS -d -t new-start -o tnc -fg -a A_Scheduler
#
# ---------------------------------------------------
6cd48351   Etienne Pallier   AgentScheduler is...
22
23

import sys
6cd48351   Etienne Pallier   AgentScheduler is...
24
import time
df477bca   Alain Klotz   New scheduler
25
26
27
28
29
30
31
import argparse
import os
import pickle
import socket
pwd = os.environ['PROJECT_ROOT_PATH']
if pwd not in sys.path:
    sys.path.append(pwd)
6cd48351   Etienne Pallier   AgentScheduler is...
32

df477bca   Alain Klotz   New scheduler
33
34
35
36
37
short_paths = ['src', 'src/core/pyros_django']
for short_path in short_paths:
    path = os.path.join(pwd, short_path)
    if path not in sys.path:
        sys.path.insert(0, path)
6cd48351   Etienne Pallier   AgentScheduler is...
38

df477bca   Alain Klotz   New scheduler
39
from src.core.pyros_django.majordome.agent.Agent import Agent, build_agent, log, parse_args
b95a693f   Alexis Koralewski   restructuration d...
40
from seq_submit.models import Sequence
df477bca   Alain Klotz   New scheduler
41
42
43
44
45
46
47
48
49
from user_mgmt.models import Period, ScientificProgram, SP_Period
# = Specials
import glob
import shutil
import guitastro
import datetime
from decimal import Decimal
import zoneinfo
import numpy as np
6cd48351   Etienne Pallier   AgentScheduler is...
50

df477bca   Alain Klotz   New scheduler
51
class AgentScheduler(Agent):
6cd48351   Etienne Pallier   AgentScheduler is...
52

df477bca   Alain Klotz   New scheduler
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    DPRINT = False
    
    # - status of the sequence after schedule computation
    SEQ_NOT_PROCESSED = 0
    SEQ_SCHEDULED = 1
    SEQ_SCHEDULED_OVER_QUOTA = 2
    SEQ_REJECTED_NO_QUOTA_ENOUGH = -1
    SEQ_REJECTED_NO_SLOT_AVAILABLE = -2
    
    # - enum of the matrix line
    SEQ_K = 0
    SEQ_SEQ_ID = 1
    SEQ_KOBS0 = 2
    SEQ_SP_ID = 3
    SEQ_PRIORITY = 4
    SEQ_DURATION = 5
    SEQ_STATUS = 6
    NB_SEQ = 7
    
    # - All possible running states
    RUNNING_NOTHING = 0
    RUNNING_SCHEDULE_PROCESSING = 1
6cd48351   Etienne Pallier   AgentScheduler is...
75

df477bca   Alain Klotz   New scheduler
76
77
78
79
80
    _AGENT_SPECIFIC_COMMANDS = {
        # Format : “cmd_name” : (timeout, exec_mode)
        "do_compute_schedule_1" : (60, Agent.EXEC_MODE.SEQUENTIAL, ''),
        "do_create_seq_1" : (60, Agent.EXEC_MODE.SEQUENTIAL, ''),
    }
6cd48351   Etienne Pallier   AgentScheduler is...
81

df477bca   Alain Klotz   New scheduler
82
83
84
85
86
87
88
    # Scenario to be executed
    # "self do_stop_current_processing"
    # AgentCmd.CMD_STATUS_CODE.CMD_EXECUTED
    _TEST_COMMANDS_LIST = [
        # Format : ("self cmd_name cmd_args", timeout, "expected_result", expected_status),
        (True, "self do_create_seq_1 6", 200, '', Agent.CMD_STATUS.CMD_EXECUTED),
        (True, "self do_stop asap", 500, "STOPPING", Agent.CMD_STATUS.CMD_EXECUTED),
bd4b900a   Etienne Pallier   update replan
89
    ]
6cd48351   Etienne Pallier   AgentScheduler is...
90

6cd48351   Etienne Pallier   AgentScheduler is...
91
92
    """
    =================================================================
df477bca   Alain Klotz   New scheduler
93
        Methods running inside main thread
6cd48351   Etienne Pallier   AgentScheduler is...
94
95
    =================================================================
    """
df477bca   Alain Klotz   New scheduler
96
    def __init__(self, name:str=None,simulated_computer=None):
6cd48351   Etienne Pallier   AgentScheduler is...
97
98
        if name is None:
            name = self.__class__.__name__
df477bca   Alain Klotz   New scheduler
99
        super().__init__(simulated_computer=simulated_computer)
0bcd6dd5   Etienne Pallier   renamed Agent met...
100
    
0bcd6dd5   Etienne Pallier   renamed Agent met...
101
102
    def _init(self):
        super()._init()
df477bca   Alain Klotz   New scheduler
103
104
        log.debug("end super init()")
        log.info(f"self.TEST_MODE = {self.TEST_MODE}")
6cd48351   Etienne Pallier   AgentScheduler is...
105

df477bca   Alain Klotz   New scheduler
106
107
108
109
110
111
112
113
114
        # === Get the config object
        self.config = self._oc['config']
        self.pconfig = self._oc['pyros_config']
        # === Get agent_alias
        hostname = socket.gethostname()
        log.info(f"{hostname=}")
        log.info(f"{self.name=}")
        agent_alias = self.config.get_agent_real_name(self.name, hostname)
        log.info(f"{agent_alias=}")
6cd48351   Etienne Pallier   AgentScheduler is...
115

df477bca   Alain Klotz   New scheduler
116
117
118
        # === Get self._home of current unit
        self._home = self.config.getHome()        
        home = guitastro.Home(self._home)
6cd48351   Etienne Pallier   AgentScheduler is...
119

df477bca   Alain Klotz   New scheduler
120
121
        self._fn = self.pconfig.fn
        self._fn.pathnaming("PyROS.seq.1")
6cd48351   Etienne Pallier   AgentScheduler is...
122

df477bca   Alain Klotz   New scheduler
123
124
125
126
127
128
129
130
        # === Set longitude to ima object to generate the night yyyymmdd and subdirectories yyyy/mm/dd
        longitude = home.longitude
        log.info(f"{longitude=}")
        self._fn.longitude(longitude)

        # === Status of routine processing
        self._routine_running = self.RUNNING_NOTHING
        log.debug("end init()")
6cd48351   Etienne Pallier   AgentScheduler is...
131
132
133

    # Note : called by _routine_process() in Agent
    # @override
547df0ef   Etienne Pallier   routine_process_b...
134
    def _routine_process_iter_start_body(self):
739c899f   Etienne Pallier   Ajout routine_pro...
135
        log.debug("in routine_process_before_body()")
739c899f   Etienne Pallier   Ajout routine_pro...
136
137
138

    # Note : called by _routine_process() in Agent
    # @override
547df0ef   Etienne Pallier   routine_process_b...
139
    def _routine_process_iter_end_body(self):
739c899f   Etienne Pallier   Ajout routine_pro...
140
        log.debug("in routine_process_after_body()")
df477bca   Alain Klotz   New scheduler
141
142
143
144
145
146
        # TODO EP est-ce utile ?
        if self._routine_running == self.RUNNING_NOTHING:
            # Get files to process
            # - Thread TODO
            self._routine_running = self.RUNNING_SCHEDULE_PROCESSING
            self.do_compute_schedule_1()
6cd48351   Etienne Pallier   AgentScheduler is...
147

df477bca   Alain Klotz   New scheduler
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
    """
    =================================================================
        Methods of specific commands
    =================================================================
    """

    def do_ccreate_seq_1(self, nb_seq:int):
        """Create sequences to debug
        """
        self._create_seq_1(nb_seq)

    def do_compute_schedule_1(self):
        """Compute a schedule
        
        According the current time, select the night directory.
        List the *.p file list (.p for sequences)
        Read the *.p, *.f file contents (.f for ephemeris)
        Compute the schedule
        
        Output is a matrix to unpack in the database.
        Each line of the matrix is a sequence
        Columns are defined by the enum SEQ_* (see the python code itself).
        
        """
        self._compute_schedule_1()

    """
    =================================================================
        Methods called by commands or routine. Overload these methods
    =================================================================
    # ---
    # osp = ScientificProgram.objects.get(id=scientific_program_id)
    # --- ospperiod is the SP object
    # ospperiod = SP_Period.objects.get(period = period_id, scientific_program = osp)
    # print(f"dir(ospperiod)={dir(ospperiod)}")
    # dir(spperiod)=['DoesNotExist', 
    # 'IS_VALID', 'IS_VALID_ACCEPTED', 'IS_VALID_REJECTED', 
    # 'MultipleObjectsReturned', 'SP_Period_Guests', 'SP_Period_Users', 
    # 'STATUSES', 'STATUSES_ACCEPTED', 'STATUSES_DRAFT', 
    # 'STATUSES_EVALUATED', 'STATUSES_REJECTED', 'STATUSES_SUBMITTED', 
    # 'VISIBILITY_CHOICES', 'VISIBILITY_NO', 'VISIBILITY_YES', 
    # 'VOTES', 'VOTES_NO', 'VOTES_TO_DISCUSS', 'VOTES_YES', 
    # 'can_submit_sequence', 'check', 'clean', 'clean_fields', 
    # 'date_error_message', 'delete', 'from_db', 'full_clean', 
    # 'get_constraints', 'get_deferred_fields', 'get_is_valid_display', 
    # 'get_public_visibility_display', 'get_status_display', 
    # 'get_vote_referee1_display', 'get_vote_referee2_display', 
    # 'id', 'is_currently_active', 'is_valid', 'objects', 
    # 'over_quota_duration', 'over_quota_duration_allocated', 
    # 'over_quota_duration_remaining', 'period', 'period_id', 
    # 'pk', 'prepare_database_save', 'priority', 'public_visibility', 
    # 'quota_allocated', 'quota_minimal', 'quota_nominal', 
    # 'quota_remaining', 'reason_referee1', 'reason_referee2', 
    # 'referee1', 'referee1_id', 'referee2', 'referee2_id', 
    # 'refresh_from_db', 'save', 'save_base', 'scientific_program', 
    # 'scientific_program_id', 'serializable_value', 'status', 'token', 
    # 'token_allocated', 'token_remaining', 'unique_error_message', 
    # 'validate_constraints', 'validate_unique', 'vote_referee1', 
    # 'vote_referee2'
    """
    
    def _compute_schedule_1(self):
        """Simple scheduler based on selection-insertion one state algorithm.
        
        Quotas are available only fo the night.
        No token.
        """
        t0 = time.time()
        #self.DPRINT = True
        # --- Get the incoming directory of the night
        info = self.get_infos()
        rootdir = info['rootdir']
        subdir = info['subdir']
        # --- Build the wildcard to list the sequences
        wildcard = os.path.join(rootdir, subdir, "*.p")
        self.dprint(f"{wildcard=}")
        # --- List the sequences from the incoming directory
        seqfiles = glob.glob(wildcard)
        log.info(f"{len(seqfiles)} file sequences to process")
        # --- Initialize the schedule
        schedule = np.zeros(86400, dtype=int) -1
        schedule_binary = np.ones(86400, dtype=int)
        # ===================================================================
        # --- Loop over the sequences of the night to extract useful infos
        # ===================================================================
        self.dprint("\n" + "="*70 + f"\n=== Read {len(seqfiles)} sequence files of the night {info['night']}\n" + "="*70 + "\n")
        sequence_infos = []
        # --- Initialize the list of scientific_program_ids
        scientific_program_ids = []
        kseq  = 0
        for seqfile in seqfiles:
            # --- seqfile = sequence file name
            kseq += 1
            sequence_info = {}
            sequence_info['id'] = -1 # TBD replace by idseq of the database
            sequence_info['seqfile'] = seqfile
            sequence_info['error'] = ""
            sequence_info['kobs0'] = -1
            # --- ephfile = ephemeris file name
            ephfile = os.path.splitext(seqfile)[0] + ".f"
            # --- If ephemeris file exists, read files
            if os.path.exists(ephfile):
                self.dprint(f"Read file {seqfile}")
                # --- seq_info = sequence dictionary
                # --- eph_info = ephemeris dictionary
                seq_info = pickle.load(open(seqfile,"rb"))
                #print("="*20 + "\n" + f"{seq_info=}")
                eph_info = pickle.load(open(ephfile,"rb"))
                #print("="*20 + "\n" + f"{eph_info=}")
                # ---
                param = self._fn.naming_get(seqfile)
                sequence_info['id'] = int(param['id_seq'])
                # --- scientific_program_id is an integer
                scientific_program_id = seq_info['sequence']['scientific_program']
                # --- Dictionary of informations about the sequence
                sequence_info['seq_dico'] = seq_info # useful for duration
                # --- Search the last time when the start of the sequence is observable (visibility > 0)
                visibility_duration = eph_info['visibility_duration']
                kobss = np.where(visibility_duration > 0)
                kobss = list(kobss[0])
                if len(kobss) == 0:
                    self.dprint("  Sequence has no visibility")
                    sequence_info['error'] = f"Sequence has no visibility_duration"
                    sequence_infos.append(sequence_info)
                    continue
                kobs0 = kobss[0]
                sequence_info['kobs0'] = kobs0    
                sequence_info['visibility'] = eph_info['visibility'] # total slots
                sequence_info['visibility_duration'] = visibility_duration # total slots - duration
                sequence_info['duration'] = seq_info['sequence']['duration']
                sequence_info['scientific_program_id'] = scientific_program_id
                self.dprint(f"  {scientific_program_id=} range to start={len(kobss)}")
                if scientific_program_id not in scientific_program_ids:
                    scientific_program_ids.append(scientific_program_id)
            else:
                sequence_info['error'] = f"File {ephfile} not exists"
            sequence_infos.append(sequence_info)
            
        # ===================================================================
        # --- Get informations of priority and quota from scientific programs
        # ===================================================================
        self.dprint("\n" + "="*70 + f"\n=== Get information from {len(scientific_program_ids)} scientific programs of the night\n" + "="*70 + "\n")
        scientific_program_infos = {}
        period_id = info['operiod'].id
        self.dprint(f"{scientific_program_ids=}")
        for scientific_program_id in scientific_program_ids:
            scientific_program_info = {}
            try:
                osp = ScientificProgram.objects.get(id=scientific_program_id)
                # --- ospperiod is the SP object
                ospperiod = SP_Period.objects.get(period = period_id, scientific_program = osp)
                scientific_program_info['priority'] = ospperiod.priority
                scientific_program_info['over_quota_duration'] = ospperiod.over_quota_duration
                scientific_program_info['over_quota_duration_allocated'] = ospperiod.over_quota_duration_allocated
                scientific_program_info['over_quota_duration_remaining'] = ospperiod.over_quota_duration_remaining             
                scientific_program_info['quota_allocated'] = ospperiod.quota_allocated
                scientific_program_info['quota_minimal'] = ospperiod.quota_minimal
                scientific_program_info['quota_nominal'] = ospperiod.quota_nominal
                scientific_program_info['quota_remaining'] = ospperiod.quota_remaining           
                scientific_program_info['token_allocated'] = ospperiod.token_allocated
                scientific_program_info['token_remaining'] = ospperiod.token_allocated
            except:
                # --- simulation
                scientific_program_info['priority'] = 0
            if scientific_program_info['priority'] == 0:
                # --- simulation
                priority = 50 + scientific_program_id*5
                scientific_program_info['priority'] = priority
                scientific_program_info['quota_allocated'] = 12000
                scientific_program_info['quota_remaining'] = 12000  
            scientific_program_infos[str(scientific_program_id)] = scientific_program_info
            self.dprint(f"{scientific_program_id=} priority={scientific_program_info['priority']} quota={scientific_program_info['quota_remaining']}")
            
        # ===================================================================
        # --- Build the numpy matrix seqs to make rapid computations
        # ===================================================================
        self.dprint("\n" + "="*70 + f"\n=== Build the matrix for scheduling {len(sequence_infos)} sequences\n" + "="*70 + "\n")
        self.dprint("Order ID_seq K_start ID_sp Priority Duration Status\n")
        nseq = len(sequence_infos)
        if nseq == 0:
            self._routine_running = self.RUNNING_NOTHING
            return
        seqs = np.zeros((nseq, self.NB_SEQ), dtype=int)
        k = 0
        for sequence_info in sequence_infos:
            if 'scientific_program_id' not in sequence_info.keys():
                self.dprint(f"No scientific program for ID sequence {sequence_info['id']}")
                continue
            scientific_program_id = sequence_info['scientific_program_id']
            scientific_program_info = scientific_program_infos[str(scientific_program_id)]
            priority = scientific_program_info['priority']
            # Order of the following list refers to the enum
            seq = [ k, sequence_info['id'], sequence_info['kobs0'], scientific_program_id, priority, int(np.ceil(sequence_info['duration'])), self.SEQ_NOT_PROCESSED ] 
            self.dprint(f"{seq=}")
            seqs[k] = seq
            k += 1
        seqs = seqs[:k]
        # --- Save the matrix sequence
        #print(f"{seqs=}")
        fpathname = os.path.join(rootdir, subdir, "scheduler_seq_matrix1.txt")
        np.savetxt(fpathname, seqs)
        
        # ===================================================================
        # --- Compute the matrix seq_sorteds (priority and chronology)
        # ===================================================================        
        self.dprint("\n" + "="*70 + "\n=== Sort the matrix for scheduling by priority and chronology\n" + "="*70 + "\n")
        # --- Sort the matrix sequence: priority=SEQ_PRIORITY (decreasing -1) and then chronology=SEQ_KOBS0 (increasing +1)
        seq_sorteds = seqs[np.lexsort(([1,-1]*seqs[:,[self.SEQ_KOBS0, self.SEQ_PRIORITY]]).T)]
        # --- Save the matrix sequence
        self.dprint("Order ID_seq K_start ID_sp Priority Duration Status\n")
        self.dprint(f"{seq_sorteds=}")
        fpathname = os.path.join(rootdir, subdir, "scheduler_seq_matrix2.txt")
        np.savetxt(fpathname, seq_sorteds)

        # ===================================================================
        # --- Insert sequences in the schedule. Respecting priority and quota
        # ===================================================================
        self.dprint("\n" + "="*70 + "\n=== Insertion of the sequences in the schedule respecting priority and quota\n" + "="*70 + "\n")
        for seq in seq_sorteds:
            # --- Unpack the sequence
            k, sequence_id, kobs0, scientific_program_id, priority, duration, seq_status = seq
            
            # --- Get the quota remaining of the scientific program
            quota_remaining = scientific_program_infos[str(scientific_program_id)]['quota_remaining']
            self.dprint('-'*70 + "\n" + f"Process {sequence_id=} {kobs0=} {duration=} sp_id={scientific_program_id} {quota_remaining=}")
            
            # --- Verify if duration < quota_remaining
            if duration > quota_remaining: 
                # --- No remaining quota to insert this sequence
                self.dprint(f"{sequence_id=} cannot be inserted because no quota enough")
                seqs[k][self.SEQ_STATUS] = self.SEQ_REJECTED_NO_QUOTA_ENOUGH
                continue
                
            # --- Compute the remaining visibility and list (k1s) of the best observation start
            # =0 if not possible to start observation
            # =value with the highest value for the best observation start
            
            # --- Visibility*schedule_binary are transformed into binary
            sequence_info = sequence_infos[k]
            vis_binarys = sequence_info['visibility'].copy() * schedule_binary
            vis_binarys[vis_binarys > 0] = 1
            
            # --- Cumulative sum + offset by -duration to prepare the start_binary computation
            obs_starts = np.cumsum(vis_binarys)
            obs_ends = obs_starts.copy()
            obs_ends[0:-duration] = obs_ends[duration:]
            obs_ends[-duration:] = 0

            # --- Difference and binarisation to get starts with duration
            start_binary = obs_ends - obs_starts
            start_binary[start_binary < duration] = 0
            start_binary[start_binary == duration] = 1
            
            # --- Compute the remaining visibility (float)
            remaining_visibility = sequence_info['visibility'] * start_binary
            
            # --- Check the remaining visibility
            if np.sum(remaining_visibility) == 0:
                # --- No remaining slot to insert this sequence
                self.dprint(f"{sequence_id=} cannot inserted because no more slots available")
                seqs[k][self.SEQ_STATUS] = self.SEQ_REJECTED_NO_SLOT_AVAILABLE
                continue
                
            # --- From the index of the highest value of remaining visibility to the index of the lowest value of remaining visibility
            k1s = np.flip(np.argsort(remaining_visibility))
            self.dprint(f"{k1s=} => Start elevation {sequence_info['visibility'][k1s[0]]:+.2f}")
            
            # --- Get k1 as the highest value of remaining visibility
            k1 = k1s[0]
            k2 = k1 + duration
            self.dprint(f"{k} : {sequence_id=} {scientific_program_id=} {priority=} inserted in the slot {k1=} {k2=} (remaining {quota_remaining - duration} s)")
            
            # --- Update the seqs matrix
            seqs[k][self.SEQ_STATUS] = self.SEQ_SCHEDULED
            
            # --- Update the schedule arrays
            schedule[k1:k2] = sequence_id
            schedule_binary[k1:k2] = 0
            
            # --- Update the scientific program dict
            quota_remaining -= duration
            scientific_program_infos[str(scientific_program_id)]['quota_remaining'] = quota_remaining

        # ===================================================================
        # --- Insert sequences in the schedule. Respecting priority but over quota
        # ===================================================================
        # self.dprint("\n" + "="*70 + "\n=== Insertion of the sequences in the schedule respecting priority but over quota\n" + "="*70 + "\n")
        # TBD
        # where are remaining free slots
        # scan sequences to insert in these free slots
            
        # ===================================================================
        # --- Save the schedule
        # ===================================================================
        self.dprint("\n" + "="*70 + "\n=== Save the schedule\n" + "="*70 + "\n")
        self.dprint("Order ID_seq K_start ID_sp Priority Duration Status\n")
        self.dprint(f"{seqs=}")
        fpathname = os.path.join(rootdir, subdir, "scheduler_schedule.txt")
        np.savetxt(fpathname, np.array([schedule, schedule_binary]).T)
        # --- Update the running state
        self._routine_running = self.RUNNING_NOTHING
        print(f"_compute_schedule_1 finished in {time.time() - t0:.2f} seconds")
6cd48351   Etienne Pallier   AgentScheduler is...
450

df477bca   Alain Klotz   New scheduler
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
    def _create_seq_1(self, nb_seq):
        t0 = time.time()
        self.dprint("Debut _create_seq_1")
        seq_template = {'sequence': {'id': 4, 'start_expo_pref': 'IMMEDIATE', 'pyros_user': 2, 'scientific_program': 1, 'name': 'seq_20230628T102140', 'desc': None, 'last_modified_by': 2, 'is_alert': False, 'status': 'TBP', 'with_drift': False, 'priority': None, 'analysis_method': None, 'moon_min': None, 'alt_min': None, 'type': None, 'img_current': None, 'img_total': None, 'not_obs': False, 'obsolete': False, 'processing': False, 'flag': None, 'period': 1, 'start_date': datetime.datetime(2023, 6, 28, 10, 21, 40, tzinfo=zoneinfo.ZoneInfo(key='UTC')), 'end_date': datetime.datetime(2023, 6, 28, 10, 21, 40, 999640, tzinfo=datetime.timezone.utc), 'jd1': Decimal('0E-8'), 'jd2': Decimal('0E-8'), 'tolerance_before': '1s', 'tolerance_after': '1min', 'duration': -1.0, 'overhead': Decimal('0E-8'), 'submitted': False, 'config_attributes': {'tolerance_before': '1s', 'tolerance_after': '1min', 'target': 'RADEC 0H10M -15D', 'conformation': 'WIDE', 'layout': 'Altogether'}, 'ra': None, 'dec': None, 'complete': True, 'night_id': '20230627'}, 'albums': {'Altogether': {'plans': [{'id': 4, 'album': 4, 'duration': 0.0, 'nb_images': 1, 'config_attributes': {'binnings': {'binxy': [1, 1], 'readouttime': 6}, 'exposuretime': 1.0}, 'complete': True}]}}}
        info = self.get_infos()
        rootdir = info['rootdir']
        subdir = info['subdir']
        # --- Prepare ephemeris object
        # TBD duskelev a parametrer from obsconfig (yml)
        duskelev = -7
        eph = guitastro.Ephemeris()
        eph.set_home(self.config.getHome())
        #print("Debut _create_seq_1 SUN")
        ephem_sun = eph.target2night("sun", info['night'], None, None)
        #print("Debut _create_seq_1 MOON")
        ephem_moon = eph.target2night("moon", info['night'], None, None)
        # --- Horizon (TBD get from config)
        #print("Debut _create_seq_1 Horizon")
        hor = guitastro.Horizon(eph.home)
        hor.horizon_altaz = [ [0,0], [360,0] ]
        # --- Delete all existing *.p and *.f files in the night directory
        fn_param = {
            "period" : f"{info['period_id']}",
            "version": "1",
            "unit": self.config.unit_name,
            "date": info['night'],
            "id_seq": 0
        }
        fname = self._fn.naming_set(fn_param)
        self.dprint(f":: {fname=}")
        seq_file = self._fn.join(fname)
        path_night = os.path.dirname(seq_file)
        cards = ['*.p', '*.f']
        for card in cards:
            wildcard = os.path.join(path_night, card)
            seq_dfiles = glob.glob(wildcard)
            #print(f"::: {seq_dfiles=}")
            for seq_dfile in seq_dfiles:
                #print(f":::.1 : os.remove {seq_dfile=}")
                os.remove(seq_dfile)
        # ---
        for k in range(nb_seq):
            #print("B"*20 + f" {info['operiod'].id} {info['night']} {k}")
            time.sleep(1)
            seq = seq_template.copy()
            seq['sequence']['period'] = info['operiod'].id # int
            seq['sequence']['night_id'] = info['night'] # str
            seq['sequence']['config_attributes']['target'] = k # int
            # ---
            start_expo_pref = "BESTELEV" #"IMMEDIATE"
            scientific_program = int(k/2)
            start_date = datetime.datetime(2023, 6, 28, 10, 21, 40)
            end_date = datetime.datetime(2023, 6, 28, 10, 21, 40, 999640, tzinfo=datetime.timezone.utc)
            jd1 = Decimal('0E-8')
            jd2 = Decimal('0E-8')
            tolerance_before = '1s'
            tolerance_after = '1min'
            duration =  3000.0
            target = f"RADEC {k}h {10+2*k}d"
            # ---
            seq['sequence']['start_expo_pref'] = start_expo_pref
            seq['sequence']['scientific_program'] = scientific_program
            seq['sequence']['start_date'] = start_date
            seq['sequence']['end_date'] = end_date
            seq['sequence']['jd1'] = jd1
            seq['sequence']['jd2'] = jd2
            seq['sequence']['tolerance_before'] = tolerance_before
            seq['sequence']['tolerance_after'] = tolerance_after
            seq['sequence']['duration'] = duration
            seq['sequence']['config_attributes']['target'] = target
            # --- Build the path and file name of the sequence file
            fn_param["id_seq"] = k
            #print(f"{k} : {self._fn.fcontext=}")
            self._fn.fname = self._fn.naming_set(fn_param)
            #print(f"{k} : {self._fn.fname=}")
            seq_file = self._fn.join(self._fn.fname)
            #print(f"{k} : {seq_file=}")
            # --- Build the path and file name of the ephemeris file
            eph_file = f"{seq_file[:-2]}.f"
            # --- Create directory if it doesn't exist
            #print(f"{k} : {seq_file=}")
            os.makedirs(os.path.dirname(seq_file), exist_ok=True)
            # --- Compute the ephemeris of the sequence and manage errors
            #print(f"{k} : TRY")
            errors = []
            try:
                # TODO remplacer les none par les fichiers pickle de ephem_sun & ephem_moon
                ephem = eph.target2night(seq["sequence"]["config_attributes"]["target"], info['night'], ephem_sun, ephem_moon, preference=seq['sequence']['start_expo_pref'], duskelev=duskelev, horizon=hor, duration=duration)
            except ValueError:
                errors.append("Target value is not valid")
            except guitastro.ephemeris.EphemerisException as ephemException:
                errors.append(str(ephemException))
            if len(errors) == 0 and np.sum(ephem["visibility"]) == 0 :
                errors.append("Target is not visible.")
            if len(errors) == 0:
                pickle.dump(ephem, open(eph_file,"wb"))
                pickle.dump(seq, open(seq_file,"wb"))
            #print(f"{errors=}")
            #print("C"*20)
        print(f"_create_seq_1 finished in {time.time() - t0:.2f} seconds")
        
    def load_sequence(self):
        sequence = ""
        return sequence
6cd48351   Etienne Pallier   AgentScheduler is...
555

df477bca   Alain Klotz   New scheduler
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
    def get_infos(self):
        rootdir = self._fn.rootdir
        operiod = Period.objects.exploitation_period()
        if operiod == None:
            log.info("No period valid in the database")
            self._routine_running = self.RUNNING_NOTHING
            return
        period_id = str(operiod.id)
        if len(str(operiod.id)) < 3:
            while len(period_id) < 3:
                period_id = "0" + period_id
        period_id = "P" + period_id
        night_id = self._fn.date2night("now")
        subdir = os.path.join(period_id, night_id)
        dico = {}
        dico['rootdir'] = rootdir
        dico['subdir'] = subdir
        dico['operiod'] = operiod # object
        dico['period_id'] = period_id # str formated (P000)
        dico['night'] = night_id # str (YYYYMMDD)
        return dico
        
    def dprint(self, *args, **kwargs):
        if self.DPRINT:
            print(*args, **kwargs)
        
6cd48351   Etienne Pallier   AgentScheduler is...
582
583
if __name__ == "__main__":

df477bca   Alain Klotz   New scheduler
584
    agent = build_agent(AgentScheduler)
6cd48351   Etienne Pallier   AgentScheduler is...
585
586
    print(agent)
    agent.run()