Beispiel #1
0
 def handle(self, *args, **options):
     user = DimUser.objects.get_or_create(username="******")[0]
     sim_group = SimulationGroup(submitted_by=user)
     sim_group.save()
     print "Simulation group %s has been created successfully" % sim_group.id
     with open(os.path.join("ts_om","management", "commands", "default.xml")) as fp:
         xml = fp.read()
     simulation1 = add_simulation(sim_group, xml)
     print "Simulation %s has been created successfully" % simulation1.id
     simulation2 = add_simulation(sim_group, xml)
     print "Simulation %s has been created successfully" % simulation2.id
     dispatcher.submit(sim_group)
     print "Simulation group has been submitted successfully. Waiting for the results."
     seconds = 0
     print sim_status.SCRIPT_DONE
     while simulation1.status != sim_status.SCRIPT_DONE and \
           simulation1.status != sim_status.SCRIPT_ERROR and \
           simulation1.status != sim_status.OUTPUT_ERROR:
         time.sleep(1)
         seconds += 1
         if divmod(seconds, 60):
             print "Waiting %s seconds, status %s/%s" % (seconds, simulation1.status, simulation2.status)
         # reload model from the database
         simulation1 = Simulation.objects.get(id = simulation1.id)
         simulation2 = Simulation.objects.get(id = simulation2.id)
     print "Simulation result: %s/%s" % (simulation1.status, simulation2.status)
Beispiel #2
0
def submit_group(user, xml_scenarios, version=None):
    # Create Simulation and SimulationGroup
    if isinstance(user, User):
        user = DimUser.objects.get_or_create(username=user.username)[0]
    if isinstance(user, (str, unicode)):
        user = DimUser.objects.get_or_create(username=user)[0]

    sim_group = SimulationGroup(submitted_by=user)
    sim_group.save()
    for scenario in xml_scenarios:
        add_simulation(sim_group, scenario, version=version)
    try:
        dispatcher.submit(sim_group)
        return sim_group
    except RuntimeError:
        return None
Beispiel #3
0
def send_calibration_request(request, targets, scenario_id):
    dim_scenario = DimBaseline.objects.get(id=scenario_id)

    # Get user
    user = DimUser.objects.get_or_create(username=request.user.username)[0]

    # Check user
    #if dim_scenario.user != user:
    #    raise PermissionDenied

    # Create simulation group
    simulation_group = SimulationGroup(submitted_by=user)
    simulation_group.save()

    # Create emod_scenario for helper functions to get files
    emod_scenario = EMODBaseline.from_dw(id=scenario_id)

    # Create input files and put them into a list
    simulation_input_files = create_and_return_input_files(user, emod_scenario)

    # Get version
    try:
        # Slices off the excess and leaves just the version number
        version = emod_scenario.template.model_version.split('v')[1]
    except:
        version = 'unknown'

    # Create simulation
    simulation = Simulation.objects.create(
        group=simulation_group,
        model=sim_model.EMOD_CALIBRATION,
        version=version,
        status=sim_status.READY_TO_RUN
    )

    file_urls = {}

    # Add simulation input files to simulation
    for i in range(len(simulation_input_files)):
        simulation.input_files.add(simulation_input_files[i])
        file_urls[simulation_input_files[i].name] = SITE_ROOT_URL[:-1] + reverse(
            'input_file_download',
            kwargs={
                'resource_name': 'input_files',
                'pk': simulation_input_files[i].id,
                'api_name': 'v1'
            }
        )

    print "targets = " + json.dumps(targets)
    print file_urls

    data = {}
    data['targets'] = json.dumps(targets)
    data['files'] = json.dumps(file_urls)
    data['url'] = SITE_ROOT_URL[:-1] + reverse('data_services.update_emod_config', kwargs={'scenario_id': scenario_id})
    data['version'] = version

    print data

    url = CONFIG_CALIBRATION_URL

    try:
        send_request = requests.post(url, data=data)
    except ConnectionError:
        set_notification('alert-error', '<strong>Error!</strong> Connection error with calibration service.', request.session)
        return

    print send_request.status_code

    try:
        current_metadata = json.loads(dim_scenario.metadata)
    except:
        current_metadata = {}

    if str(send_request.status_code) == "200":
        current_metadata['calibration_status'] = 'sent'
    else:
        current_metadata['calibration_status'] = 'failed'

    dim_scenario.metadata = json.dumps(current_metadata)
    dim_scenario.save()

    if current_metadata['calibration_status'] == 'failed':
        set_notification('alert-error', '<strong>Error!</strong> Request error with calibration service.', request.session)
        return

    set_notification('alert-success', '<strong>Success!</strong> Calibration request sent.', request.session)
Beispiel #4
0
def convert_baseline_to_scenario(request, baseline_id):
    dim_user = DimUser.objects.get(username=request.user.username)
    emod_scenario = EMODBaseline.from_dw(id=baseline_id)
    dim_scenario = DimBaseline.objects.get(id=baseline_id)

    extra_metadata = {}

    # This doesn't all need to be here, but it is simpler just to leave it
    if dim_scenario.metadata:
        metadata = json.loads(dim_scenario.metadata)

        if 'representative' in metadata:
            emod_scenario.is_representative = True
            extra_metadata = metadata
            if 'is_complete' in metadata['representative']:
                emod_scenario.representative_is_completed = True
            else:
                emod_scenario.representative_is_completed = False
            emod_scenario.name = derive_autoname(dim_scenario)
        else:
            emod_scenario.is_representative = False

    # Create simulation group if one is not already made
    if not dim_scenario.simulation_group:
        simulation_group = SimulationGroup(submitted_by=dim_user)
        simulation_group.save()

        simulation = add_simulation(dim_user, sim_model.EMOD, dim_scenario.template.model_version.split('v')[1], simulation_group, baseline_id)

        scenario = Scenario.objects.create(
            name=dim_scenario.name,
            description=dim_scenario.description,
            user=dim_user,
            simulation=simulation,
            metadata={},
            extra_metadata=extra_metadata
        )

        # Modify config
        config_json = json.loads(scenario.config_file.get_contents())
        config_json['parameters']['Land_Temperature_Filename'] = 'temperature.bin'
        config_json['parameters']['Rainfall_Filename'] = 'rainfall.bin'
        config_json['parameters']['Relative_Humidity_Filename'] = 'humidity.bin'
        config_json['parameters']['Air_Temperature_Filename'] = 'temperature.bin'
        config_json['parameters']['Campaign_Filename'] = 'campaign.json'
        config_json['parameters']['Demographics_Filename'] = 'demographics.compiled.json'
        contents = json.dumps(config_json)
        scenario.set_file_by_type('config', contents)

        set_notification('alert-success', '<strong>Success!</strong> Converted baseline to scenario.', request.session)

        # This makes sure we don't try to convert the same baseline twice
        dim_scenario.simulation_group = simulation_group
        dim_scenario.save()
    else:
        simulation_group = dim_scenario.simulation_group
        try:
            simulation = simulation_group.simulations.all()[0]
            scenario = simulation.emod_scenario_set.all()[0]
        except Exception as exception:
            set_notification(
                'alert-error',
                '<strong>Error!</strong> Simulation group exists but there is no matching scenario. ' + str(exception),
                request.session
            )
            return redirect("ts.index")
        set_notification('alert-success', '<strong>Success!</strong> This baseline has already been converted.', request.session)

    return redirect("ts_emod2.details", scenario_id=scenario.id)