Example #1
0
def auvdeployment_import(auvdeployment, files):
    """Import an AUV deployment from disk.

    This uses the track file and hydro netcdf files (as per RELEASE_DATA).
    If the deployment comes in the CATAMI deployment format use that importer
    instead.

    Certain parameters of the deployment should be prefilled - namely short_name,
    campaign, license, descriptive_keywords and owner. The rest are obtained from the
    on disk information.

    Information obtained within the function includes start and end time stamps,
    start and end positions, min and max depths and mission aim. Additionally the
    region column, and other AUV specific fields are filled.
    """

    print "Entered!"
    logger.debug("Entering auvdeployment import")

    netcdf = NetCDFParser(open(files['netcdf'], "r"))
    track_parser = TrackParser(open(files['track'], "r"))
    image_subfolder = files['image']

    # now start going through and creating the data
    auvdeployment.mission_aim = "Generic Description."
    auvdeployment.min_depth = 14000
    auvdeployment.max_depth = 0

    auvdeployment.start_position = "POINT(0.0 0.0)"
    auvdeployment.end_position = "POINT(0.0 0.0)"
    auvdeployment.start_time_stamp = datetime.datetime.now()
    auvdeployment.end_time_stamp = datetime.datetime.now()

    auvdeployment.transect_shape = "POLYGON((0.0 0.0, 0.0 0.0, 0.0 0.0, 0.0 0.0, 0.0 0.0))"

    # we have to save the deployment so that everything else can link to it
    logger.debug("Initial save of auvdeployment.")
    auvdeployment.save()

    # create the left-colour camera object
    # we don't normally give out the right mono
    # images...
    leftcamera = Camera()

    leftcamera.deployment = auvdeployment
    leftcamera.name = "Left Colour"
    leftcamera.angle = Camera.DOWN_ANGLE

    leftcamera.save()

    # get the sm types that we are going to use
    logger.debug("Get handle on required ScientificMeasurementTypes")
    temperature = ScientificMeasurementType.objects.get(
        normalised_name='temperature')
    salinity = ScientificMeasurementType.objects.get(
        normalised_name='salinity')
    pitch = ScientificMeasurementType.objects.get(normalised_name='pitch')
    roll = ScientificMeasurementType.objects.get(normalised_name='roll')
    yaw = ScientificMeasurementType.objects.get(normalised_name='yaw')
    altitude = ScientificMeasurementType.objects.get(
        normalised_name='altitude')
    logger.debug("Got all required ScientificMeasurementTypes")

    first_pose = None
    last_pose = None

    lat_lim = LimitTracker('latitude')
    lon_lim = LimitTracker('longitude')

    logger.debug("First readings from netcdf file.")
    earlier_seabird = netcdf.next()
    later_seabird = netcdf.next()

    # now we get to the images... (and related data)
    logger.debug("Begin parsing images.")
    for row in track_parser:

        pose = Pose()
        image_name = os.path.splitext(row['leftimage'])[0] + ".tif"

        pose.deployment = auvdeployment

        pose_datetime = datetime.datetime.strptime(
            os.path.splitext(image_name)[0], "PR_%Y%m%d_%H%M%S_%f_LC16")
        pose.date_time = pose_datetime.replace(tzinfo=tzutc())
        pose.position = "POINT ({0} {1})".format(row['longitude'],
                                                 row['latitude'])

        pose.depth = float(row['depth'])

        # save now as it is complete and so image
        # can refer to it
        pose.save()

        # quickly calculate limit info

        if pose.depth > auvdeployment.max_depth:
            auvdeployment.max_depth = pose.depth

        if pose.depth < auvdeployment.min_depth:
            auvdeployment.min_depth = pose.depth

        lat_lim.check(row)
        lon_lim.check(row)

        # calculate image locations and create thumbnail
        campaign_name = auvdeployment.campaign.short_name
        deployment_name = auvdeployment.short_name
        image_path = os.path.join(image_subfolder, image_name)

        archive_path, webimage_path = image_import(campaign_name,
                                                   deployment_name, image_name,
                                                   image_path)

        image = Image()

        image.camera = leftcamera
        image.pose = pose
        image.archive_location = archive_path
        image.web_location = webimage_path

        image.save()

        # get the extra measurements from the seabird data
        while pose.date_time > later_seabird['date_time']:
            later_seabird, earlier_seabird = earlier_seabird, netcdf.next()

        # find which is closer - could use interpolation instead
        if (later_seabird['date_time'] - pose.date_time) > (
            pose.date_time - earlier_seabird['date_time']):
            closer_seabird = earlier_seabird
        else:
            closer_seabird = later_seabird

        # add those extra scientific measurements
        temp_m = ScientificPoseMeasurement()
        temp_m.measurement_type = temperature
        temp_m.value = closer_seabird['temperature']
        temp_m.pose = pose
        temp_m.save()

        sal_m = ScientificPoseMeasurement()
        sal_m.measurement_type = salinity
        sal_m.value = closer_seabird['salinity']
        sal_m.pose = pose
        sal_m.save()

        roll_m = ScientificPoseMeasurement()
        roll_m.measurement_type = roll
        roll_m.value = row['roll']
        roll_m.pose = pose
        roll_m.save()

        pitch_m = ScientificPoseMeasurement()
        pitch_m.measurement_type = pitch
        pitch_m.value = row['pitch']
        pitch_m.pose = pose
        pitch_m.save()

        yaw_m = ScientificPoseMeasurement()
        yaw_m.measurement_type = yaw
        yaw_m.value = row['heading']
        yaw_m.pose = pose
        yaw_m.save()

        alt_m = ScientificPoseMeasurement()
        alt_m.measurement_type = altitude
        alt_m.value = row['altitude']
        alt_m.pose = pose
        alt_m.save()

        # we need first and last to get start/end points and times
        last_pose = pose
        if first_pose is None:
            first_pose = pose

    auvdeployment.start_time_stamp = first_pose.date_time
    auvdeployment.end_time_stamp = last_pose.date_time

    auvdeployment.start_position = first_pose.position
    auvdeployment.end_position = last_pose.position

    auvdeployment.transect_shape = "POLYGON(({0} {2}, {0} {3}, {1} {3}, {1} {2}, {0} {2} ))".format(
        lon_lim.minimum,
        lon_lim.maximum,
        lat_lim.minimum,
        lat_lim.maximum)

    # now save the actual min/max depth as well as start/end times and
    # start position and end position
    auvdeployment.save()

    return auvdeployment