text:
#==============================================================================#
# ElasticTranscoder.jl
#
# This file is generated from:
# https://github.com/aws/aws-sdk-js/blob/master/apis/elastictranscoder-2012-09-25.normal.json
#==============================================================================#

__precompile__()

module ElasticTranscoder

using AWSCore

"""
    using AWSSDK.ElasticTranscoder.cancel_job
    cancel_job([::AWSConfig], arguments::Dict)
    cancel_job([::AWSConfig]; Id=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "DELETE", "/2012-09-25/jobs/{Id}", arguments::Dict)
    elastictranscoder([::AWSConfig], "DELETE", "/2012-09-25/jobs/{Id}", Id=)

# CancelJob Operation

The CancelJob operation cancels an unfinished job.

**Note**
> You can only cancel a job that has a status of `Submitted`. To prevent a pipeline from starting to process a job while you're getting the job identifier, use [UpdatePipelineStatus](@ref) to temporarily pause the pipeline.

# Arguments

## `Id = ::String` -- *Required*
The identifier of the job that you want to cancel.

To get a list of the jobs (including their `jobId`) that have a status of `Submitted`, use the [ListJobsByStatus](@ref) API action.

# Returns

`CancelJobResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `ResourceInUseException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/CancelJob)
"""
@inline cancel_job(aws::AWSConfig=default_aws_config(); args...) = cancel_job(aws, args)

@inline cancel_job(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "DELETE", "/2012-09-25/jobs/{Id}", args)

@inline cancel_job(args) = cancel_job(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.create_job
    create_job([::AWSConfig], arguments::Dict)
    create_job([::AWSConfig]; PipelineId=, <keyword arguments>)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/jobs", arguments::Dict)
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/jobs", PipelineId=, <keyword arguments>)

# CreateJob Operation

When you create a job, Elastic Transcoder returns JSON data that includes the values that you specified plus information about the job that is created.

If you have specified more than one output for your jobs (for example, one output for the Kindle Fire and another output for the Apple iPhone 4s), you currently must use the Elastic Transcoder API to list the jobs (as opposed to the AWS Console).

# Arguments

## `PipelineId = ::String` -- *Required*
The `Id` of the pipeline that you want Elastic Transcoder to use for transcoding. The pipeline determines several settings, including the Amazon S3 bucket from which Elastic Transcoder gets the files to transcode and the bucket into which Elastic Transcoder puts the transcoded files.

## `Input = [ ... ]`
A section of the request body that provides information about the file that is being transcoded.
```
 Input = [
        "Key" =>  ::String,
        "FrameRate" =>  ::String,
        "Resolution" =>  ::String,
        "AspectRatio" =>  ::String,
        "Interlaced" =>  ::String,
        "Container" =>  ::String,
        "Encryption" =>  [
            "Mode" =>  ::String,
            "Key" =>  ::String,
            "KeyMd5" =>  ::String,
            "InitializationVector" =>  ::String
        ],
        "TimeSpan" =>  [
            "StartTime" =>  ::String,
            "Duration" =>  ::String
        ],
        "InputCaptions" =>  [
            "MergePolicy" =>  ::String,
            "CaptionSources" =>  [[
                "Key" =>  ::String,
                "Language" =>  ::String,
                "TimeOffset" =>  ::String,
                "Label" =>  ::String,
                "Encryption" =>  [
                    "Mode" =>  ::String,
                    "Key" =>  ::String,
                    "KeyMd5" =>  ::String,
                    "InitializationVector" =>  ::String
                ]
            ], ...]
        ],
        "DetectedProperties" =>  [
            "Width" =>  ::Int,
            "Height" =>  ::Int,
            "FrameRate" =>  ::String,
            "FileSize" =>  ::Int,
            "DurationMillis" =>  ::Int
        ]
    ]
```

## `Inputs = [[ ... ], ...]`
A section of the request body that provides information about the files that are being transcoded.
```
 Inputs = [[
        "Key" =>  ::String,
        "FrameRate" =>  ::String,
        "Resolution" =>  ::String,
        "AspectRatio" =>  ::String,
        "Interlaced" =>  ::String,
        "Container" =>  ::String,
        "Encryption" =>  [
            "Mode" =>  ::String,
            "Key" =>  ::String,
            "KeyMd5" =>  ::String,
            "InitializationVector" =>  ::String
        ],
        "TimeSpan" =>  [
            "StartTime" =>  ::String,
            "Duration" =>  ::String
        ],
        "InputCaptions" =>  [
            "MergePolicy" =>  ::String,
            "CaptionSources" =>  [[
                "Key" =>  ::String,
                "Language" =>  ::String,
                "TimeOffset" =>  ::String,
                "Label" =>  ::String,
                "Encryption" =>  [
                    "Mode" =>  ::String,
                    "Key" =>  ::String,
                    "KeyMd5" =>  ::String,
                    "InitializationVector" =>  ::String
                ]
            ], ...]
        ],
        "DetectedProperties" =>  [
            "Width" =>  ::Int,
            "Height" =>  ::Int,
            "FrameRate" =>  ::String,
            "FileSize" =>  ::Int,
            "DurationMillis" =>  ::Int
        ]
    ], ...]
```

## `Output = [ ... ]`
A section of the request body that provides information about the transcoded (target) file. We strongly recommend that you use the `Outputs` syntax instead of the `Output` syntax.
```
 Output = [
        "Key" =>  ::String,
        "ThumbnailPattern" =>  ::String,
        "ThumbnailEncryption" =>  [
            "Mode" =>  ::String,
            "Key" =>  ::String,
            "KeyMd5" =>  ::String,
            "InitializationVector" =>  ::String
        ],
        "Rotate" =>  ::String,
        "PresetId" =>  ::String,
        "SegmentDuration" =>  ::String,
        "Watermarks" =>  [[
            "PresetWatermarkId" =>  ::String,
            "InputKey" =>  ::String,
            "Encryption" =>  [
                "Mode" =>  ::String,
                "Key" =>  ::String,
                "KeyMd5" =>  ::String,
                "InitializationVector" =>  ::String
            ]
        ], ...],
        "AlbumArt" =>  [
            "MergePolicy" =>  ::String,
            "Artwork" =>  [[
                "InputKey" =>  ::String,
                "MaxWidth" =>  ::String,
                "MaxHeight" =>  ::String,
                "SizingPolicy" =>  ::String,
                "PaddingPolicy" =>  ::String,
                "AlbumArtFormat" =>  ::String,
                "Encryption" =>  [
                    "Mode" =>  ::String,
                    "Key" =>  ::String,
                    "KeyMd5" =>  ::String,
                    "InitializationVector" =>  ::String
                ]
            ], ...]
        ],
        "Composition" =>  [["TimeSpan" =>  [
                "StartTime" =>  ::String,
                "Duration" =>  ::String
            ]], ...],
        "Captions" =>  [
            "MergePolicy" =>  ::String,
            "CaptionSources" =>  [[
                "Key" =>  ::String,
                "Language" =>  ::String,
                "TimeOffset" =>  ::String,
                "Label" =>  ::String,
                "Encryption" =>  [
                    "Mode" =>  ::String,
                    "Key" =>  ::String,
                    "KeyMd5" =>  ::String,
                    "InitializationVector" =>  ::String
                ]
            ], ...],
            "CaptionFormats" =>  [[
                "Format" =>  ::String,
                "Pattern" =>  ::String,
                "Encryption" =>  [
                    "Mode" =>  ::String,
                    "Key" =>  ::String,
                    "KeyMd5" =>  ::String,
                    "InitializationVector" =>  ::String
                ]
            ], ...]
        ],
        "Encryption" =>  [
            "Mode" =>  ::String,
            "Key" =>  ::String,
            "KeyMd5" =>  ::String,
            "InitializationVector" =>  ::String
        ]
    ]
```

## `Outputs = [[ ... ], ...]`
A section of the request body that provides information about the transcoded (target) files. We recommend that you use the `Outputs` syntax instead of the `Output` syntax.
```
 Outputs = [[
        "Key" =>  ::String,
        "ThumbnailPattern" =>  ::String,
        "ThumbnailEncryption" =>  [
            "Mode" =>  ::String,
            "Key" =>  ::String,
            "KeyMd5" =>  ::String,
            "InitializationVector" =>  ::String
        ],
        "Rotate" =>  ::String,
        "PresetId" =>  ::String,
        "SegmentDuration" =>  ::String,
        "Watermarks" =>  [[
            "PresetWatermarkId" =>  ::String,
            "InputKey" =>  ::String,
            "Encryption" =>  [
                "Mode" =>  ::String,
                "Key" =>  ::String,
                "KeyMd5" =>  ::String,
                "InitializationVector" =>  ::String
            ]
        ], ...],
        "AlbumArt" =>  [
            "MergePolicy" =>  ::String,
            "Artwork" =>  [[
                "InputKey" =>  ::String,
                "MaxWidth" =>  ::String,
                "MaxHeight" =>  ::String,
                "SizingPolicy" =>  ::String,
                "PaddingPolicy" =>  ::String,
                "AlbumArtFormat" =>  ::String,
                "Encryption" =>  [
                    "Mode" =>  ::String,
                    "Key" =>  ::String,
                    "KeyMd5" =>  ::String,
                    "InitializationVector" =>  ::String
                ]
            ], ...]
        ],
        "Composition" =>  [["TimeSpan" =>  [
                "StartTime" =>  ::String,
                "Duration" =>  ::String
            ]], ...],
        "Captions" =>  [
            "MergePolicy" =>  ::String,
            "CaptionSources" =>  [[
                "Key" =>  ::String,
                "Language" =>  ::String,
                "TimeOffset" =>  ::String,
                "Label" =>  ::String,
                "Encryption" =>  [
                    "Mode" =>  ::String,
                    "Key" =>  ::String,
                    "KeyMd5" =>  ::String,
                    "InitializationVector" =>  ::String
                ]
            ], ...],
            "CaptionFormats" =>  [[
                "Format" =>  ::String,
                "Pattern" =>  ::String,
                "Encryption" =>  [
                    "Mode" =>  ::String,
                    "Key" =>  ::String,
                    "KeyMd5" =>  ::String,
                    "InitializationVector" =>  ::String
                ]
            ], ...]
        ],
        "Encryption" =>  [
            "Mode" =>  ::String,
            "Key" =>  ::String,
            "KeyMd5" =>  ::String,
            "InitializationVector" =>  ::String
        ]
    ], ...]
```

## `OutputKeyPrefix = ::String`
The value, if any, that you want Elastic Transcoder to prepend to the names of all files that this job creates, including output files, thumbnails, and playlists.

## `Playlists = [[ ... ], ...]`
If you specify a preset in `PresetId` for which the value of `Container` is fmp4 (Fragmented MP4) or ts (MPEG-TS), Playlists contains information about the master playlists that you want Elastic Transcoder to create.

The maximum number of master playlists in a job is 30.
```
 Playlists = [[
        "Name" =>  ::String,
        "Format" =>  ::String,
        "OutputKeys" =>  [::String, ...],
        "HlsContentProtection" =>  [
            "Method" =>  ::String,
            "Key" =>  ::String,
            "KeyMd5" =>  ::String,
            "InitializationVector" =>  ::String,
            "LicenseAcquisitionUrl" =>  ::String,
            "KeyStoragePolicy" =>  ::String
        ],
        "PlayReadyDrm" =>  [
            "Format" =>  ::String,
            "Key" =>  ::String,
            "KeyMd5" =>  ::String,
            "KeyId" =>  ::String,
            "InitializationVector" =>  ::String,
            "LicenseAcquisitionUrl" =>  ::String
        ]
    ], ...]
```

## `UserMetadata = ::Dict{String,String}`
User-defined metadata that you want to associate with an Elastic Transcoder job. You specify metadata in `key/value` pairs, and you can add up to 10 `key/value` pairs per job. Elastic Transcoder does not guarantee that `key/value` pairs are returned in the same order in which you specify them.

# Returns

`CreateJobResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `AccessDeniedException`, `LimitExceededException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/CreateJob)
"""
@inline create_job(aws::AWSConfig=default_aws_config(); args...) = create_job(aws, args)

@inline create_job(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "POST", "/2012-09-25/jobs", args)

@inline create_job(args) = create_job(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.create_pipeline
    create_pipeline([::AWSConfig], arguments::Dict)
    create_pipeline([::AWSConfig]; Name=, InputBucket=, Role=, <keyword arguments>)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/pipelines", arguments::Dict)
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/pipelines", Name=, InputBucket=, Role=, <keyword arguments>)

# CreatePipeline Operation

The CreatePipeline operation creates a pipeline with settings that you specify.

# Arguments

## `Name = ::String` -- *Required*
The name of the pipeline. We recommend that the name be unique within the AWS account, but uniqueness is not enforced.

Constraints: Maximum 40 characters.

## `InputBucket = ::String` -- *Required*
The Amazon S3 bucket in which you saved the media files that you want to transcode.

## `OutputBucket = ::String`
The Amazon S3 bucket in which you want Elastic Transcoder to save the transcoded files. (Use this, or use ContentConfig:Bucket plus ThumbnailConfig:Bucket.)

Specify this value when all of the following are true:

*   You want to save transcoded files, thumbnails (if any), and playlists (if any) together in one bucket.

*   You do not want to specify the users or groups who have access to the transcoded files, thumbnails, and playlists.

*   You do not want to specify the permissions that Elastic Transcoder grants to the files.

    **Important**
    > When Elastic Transcoder saves files in `OutputBucket`, it grants full control over the files only to the AWS account that owns the role that is specified by `Role`.

*   You want to associate the transcoded files and thumbnails with the Amazon S3 Standard storage class.

If you want to save transcoded files and playlists in one bucket and thumbnails in another bucket, specify which users can access the transcoded files or the permissions the users have, or change the Amazon S3 storage class, omit `OutputBucket` and specify values for `ContentConfig` and `ThumbnailConfig` instead.

## `Role = ::String` -- *Required*
The IAM Amazon Resource Name (ARN) for the role that you want Elastic Transcoder to use to create the pipeline.

## `AwsKmsKeyArn = ::String`
The AWS Key Management Service (AWS KMS) key that you want to use with this pipeline.

If you use either `S3` or `S3-AWS-KMS` as your `Encryption:Mode`, you don't need to provide a key with your job because a default key, known as an AWS-KMS key, is created for you automatically. You need to provide an AWS-KMS key only if you want to use a non-default AWS-KMS key, or if you are using an `Encryption:Mode` of `AES-PKCS7`, `AES-CTR`, or `AES-GCM`.

## `Notifications = [ ... ]`
The Amazon Simple Notification Service (Amazon SNS) topic that you want to notify to report job status.

**Important**
> To receive notifications, you must also subscribe to the new topic in the Amazon SNS console.

*   **Progressing**: The topic ARN for the Amazon Simple Notification Service (Amazon SNS) topic that you want to notify when Elastic Transcoder has started to process a job in this pipeline. This is the ARN that Amazon SNS returned when you created the topic. For more information, see Create a Topic in the Amazon Simple Notification Service Developer Guide.

*   **Completed**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder has finished processing a job in this pipeline. This is the ARN that Amazon SNS returned when you created the topic.

*   **Warning**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder encounters a warning condition while processing a job in this pipeline. This is the ARN that Amazon SNS returned when you created the topic.

*   **Error**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder encounters an error condition while processing a job in this pipeline. This is the ARN that Amazon SNS returned when you created the topic.
```
 Notifications = [
        "Progressing" =>  ::String,
        "Completed" =>  ::String,
        "Warning" =>  ::String,
        "Error" =>  ::String
    ]
```

## `ContentConfig = [ ... ]`
The optional `ContentConfig` object specifies information about the Amazon S3 bucket in which you want Elastic Transcoder to save transcoded files and playlists: which bucket to use, which users you want to have access to the files, the type of access you want users to have, and the storage class that you want to assign to the files.

If you specify values for `ContentConfig`, you must also specify values for `ThumbnailConfig`.

If you specify values for `ContentConfig` and `ThumbnailConfig`, omit the `OutputBucket` object.

*   **Bucket**: The Amazon S3 bucket in which you want Elastic Transcoder to save transcoded files and playlists.

*   **Permissions** (Optional): The Permissions object specifies which users you want to have access to transcoded files and the type of access you want them to have. You can grant permissions to a maximum of 30 users and/or predefined Amazon S3 groups.

*   **Grantee Type**: Specify the type of value that appears in the `Grantee` object:

    *   **Canonical**: The value in the `Grantee` object is either the canonical user ID for an AWS account or an origin access identity for an Amazon CloudFront distribution. For more information about canonical user IDs, see Access Control List (ACL) Overview in the Amazon Simple Storage Service Developer Guide. For more information about using CloudFront origin access identities to require that users use CloudFront URLs instead of Amazon S3 URLs, see Using an Origin Access Identity to Restrict Access to Your Amazon S3 Content.

        **Important**
        > A canonical user ID is not the same as an AWS account number.

    *   **Email**: The value in the `Grantee` object is the registered email address of an AWS account.

    *   **Group**: The value in the `Grantee` object is one of the following predefined Amazon S3 groups: `AllUsers`, `AuthenticatedUsers`, or `LogDelivery`.

*   **Grantee**: The AWS user or group that you want to have access to transcoded files and playlists. To identify the user or group, you can specify the canonical user ID for an AWS account, an origin access identity for a CloudFront distribution, the registered email address of an AWS account, or a predefined Amazon S3 group

*   **Access**: The permission that you want to give to the AWS user that you specified in `Grantee`. Permissions are granted on the files that Elastic Transcoder adds to the bucket, including playlists and video files. Valid values include:

    *   `READ`: The grantee can read the objects and metadata for objects that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `READ_ACP`: The grantee can read the object ACL for objects that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `WRITE_ACP`: The grantee can write the ACL for the objects that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `FULL_CONTROL`: The grantee has `READ`, `READ_ACP`, and `WRITE_ACP` permissions for the objects that Elastic Transcoder adds to the Amazon S3 bucket.

*   **StorageClass**: The Amazon S3 storage class, `Standard` or `ReducedRedundancy`, that you want Elastic Transcoder to assign to the video files and playlists that it stores in your Amazon S3 bucket.
```
 ContentConfig = [
        "Bucket" =>  ::String,
        "StorageClass" =>  ::String,
        "Permissions" =>  [[
            "GranteeType" =>  ::String,
            "Grantee" =>  ::String,
            "Access" =>  [::String, ...]
        ], ...]
    ]
```

## `ThumbnailConfig = [ ... ]`
The `ThumbnailConfig` object specifies several values, including the Amazon S3 bucket in which you want Elastic Transcoder to save thumbnail files, which users you want to have access to the files, the type of access you want users to have, and the storage class that you want to assign to the files.

If you specify values for `ContentConfig`, you must also specify values for `ThumbnailConfig` even if you don't want to create thumbnails.

If you specify values for `ContentConfig` and `ThumbnailConfig`, omit the `OutputBucket` object.

*   **Bucket**: The Amazon S3 bucket in which you want Elastic Transcoder to save thumbnail files.

*   **Permissions** (Optional): The `Permissions` object specifies which users and/or predefined Amazon S3 groups you want to have access to thumbnail files, and the type of access you want them to have. You can grant permissions to a maximum of 30 users and/or predefined Amazon S3 groups.

*   **GranteeType**: Specify the type of value that appears in the Grantee object:

    *   **Canonical**: The value in the `Grantee` object is either the canonical user ID for an AWS account or an origin access identity for an Amazon CloudFront distribution.

        **Important**
        > A canonical user ID is not the same as an AWS account number.

    *   **Email**: The value in the `Grantee` object is the registered email address of an AWS account.

    *   **Group**: The value in the `Grantee` object is one of the following predefined Amazon S3 groups: `AllUsers`, `AuthenticatedUsers`, or `LogDelivery`.

*   **Grantee**: The AWS user or group that you want to have access to thumbnail files. To identify the user or group, you can specify the canonical user ID for an AWS account, an origin access identity for a CloudFront distribution, the registered email address of an AWS account, or a predefined Amazon S3 group.

*   **Access**: The permission that you want to give to the AWS user that you specified in `Grantee`. Permissions are granted on the thumbnail files that Elastic Transcoder adds to the bucket. Valid values include:

    *   `READ`: The grantee can read the thumbnails and metadata for objects that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `READ_ACP`: The grantee can read the object ACL for thumbnails that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `WRITE_ACP`: The grantee can write the ACL for the thumbnails that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `FULL_CONTROL`: The grantee has `READ`, `READ_ACP`, and `WRITE_ACP` permissions for the thumbnails that Elastic Transcoder adds to the Amazon S3 bucket.

*   **StorageClass**: The Amazon S3 storage class, `Standard` or `ReducedRedundancy`, that you want Elastic Transcoder to assign to the thumbnails that it stores in your Amazon S3 bucket.
```
 ThumbnailConfig = [
        "Bucket" =>  ::String,
        "StorageClass" =>  ::String,
        "Permissions" =>  [[
            "GranteeType" =>  ::String,
            "Grantee" =>  ::String,
            "Access" =>  [::String, ...]
        ], ...]
    ]
```

# Returns

`CreatePipelineResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `AccessDeniedException`, `ResourceNotFoundException`, `LimitExceededException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/CreatePipeline)
"""
@inline create_pipeline(aws::AWSConfig=default_aws_config(); args...) = create_pipeline(aws, args)

@inline create_pipeline(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "POST", "/2012-09-25/pipelines", args)

@inline create_pipeline(args) = create_pipeline(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.create_preset
    create_preset([::AWSConfig], arguments::Dict)
    create_preset([::AWSConfig]; Name=, Container=, <keyword arguments>)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/presets", arguments::Dict)
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/presets", Name=, Container=, <keyword arguments>)

# CreatePreset Operation

The CreatePreset operation creates a preset with settings that you specify.

**Important**
> Elastic Transcoder checks the CreatePreset settings to ensure that they meet Elastic Transcoder requirements and to determine whether they comply with H.264 standards. If your settings are not valid for Elastic Transcoder, Elastic Transcoder returns an HTTP 400 response (`ValidationException`) and does not create the preset. If the settings are valid for Elastic Transcoder but aren't strictly compliant with the H.264 standard, Elastic Transcoder creates the preset and returns a warning message in the response. This helps you determine whether your settings comply with the H.264 standard while giving you greater flexibility with respect to the video that Elastic Transcoder produces.

Elastic Transcoder uses the H.264 video-compression format. For more information, see the International Telecommunication Union publication *Recommendation ITU-T H.264: Advanced video coding for generic audiovisual services*.

# Arguments

## `Name = ::String` -- *Required*
The name of the preset. We recommend that the name be unique within the AWS account, but uniqueness is not enforced.

## `Description = ::String`
A description of the preset.

## `Container = ::String` -- *Required*
The container type for the output file. Valid values include `flac`, `flv`, `fmp4`, `gif`, `mp3`, `mp4`, `mpg`, `mxf`, `oga`, `ogg`, `ts`, and `webm`.

## `Video = [ ... ]`
A section of the request body that specifies the video parameters.
```
 Video = [
        "Codec" =>  ::String,
        "CodecOptions" =>  ::Dict{String,String},
        "KeyframesMaxDist" =>  ::String,
        "FixedGOP" =>  ::String,
        "BitRate" =>  ::String,
        "FrameRate" =>  ::String,
        "MaxFrameRate" =>  ::String,
        "Resolution" =>  ::String,
        "AspectRatio" =>  ::String,
        "MaxWidth" =>  ::String,
        "MaxHeight" =>  ::String,
        "DisplayAspectRatio" =>  ::String,
        "SizingPolicy" =>  ::String,
        "PaddingPolicy" =>  ::String,
        "Watermarks" =>  [[
            "Id" =>  ::String,
            "MaxWidth" =>  ::String,
            "MaxHeight" =>  ::String,
            "SizingPolicy" =>  ::String,
            "HorizontalAlign" =>  ::String,
            "HorizontalOffset" =>  ::String,
            "VerticalAlign" =>  ::String,
            "VerticalOffset" =>  ::String,
            "Opacity" =>  ::String,
            "Target" =>  ::String
        ], ...]
    ]
```

## `Audio = [ ... ]`
A section of the request body that specifies the audio parameters.
```
 Audio = [
        "Codec" =>  ::String,
        "SampleRate" =>  ::String,
        "BitRate" =>  ::String,
        "Channels" =>  ::String,
        "AudioPackingMode" =>  ::String,
        "CodecOptions" =>  [
            "Profile" =>  ::String,
            "BitDepth" =>  ::String,
            "BitOrder" =>  ::String,
            "Signed" =>  ::String
        ]
    ]
```

## `Thumbnails = [ ... ]`
A section of the request body that specifies the thumbnail parameters, if any.
```
 Thumbnails = [
        "Format" =>  ::String,
        "Interval" =>  ::String,
        "Resolution" =>  ::String,
        "AspectRatio" =>  ::String,
        "MaxWidth" =>  ::String,
        "MaxHeight" =>  ::String,
        "SizingPolicy" =>  ::String,
        "PaddingPolicy" =>  ::String
    ]
```

# Returns

`CreatePresetResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `AccessDeniedException`, `LimitExceededException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/CreatePreset)
"""
@inline create_preset(aws::AWSConfig=default_aws_config(); args...) = create_preset(aws, args)

@inline create_preset(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "POST", "/2012-09-25/presets", args)

@inline create_preset(args) = create_preset(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.delete_pipeline
    delete_pipeline([::AWSConfig], arguments::Dict)
    delete_pipeline([::AWSConfig]; Id=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "DELETE", "/2012-09-25/pipelines/{Id}", arguments::Dict)
    elastictranscoder([::AWSConfig], "DELETE", "/2012-09-25/pipelines/{Id}", Id=)

# DeletePipeline Operation

The DeletePipeline operation removes a pipeline.

You can only delete a pipeline that has never been used or that is not currently in use (doesn't contain any active jobs). If the pipeline is currently in use, `DeletePipeline` returns an error.

# Arguments

## `Id = ::String` -- *Required*
The identifier of the pipeline that you want to delete.

# Returns

`DeletePipelineResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `ResourceInUseException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/DeletePipeline)
"""
@inline delete_pipeline(aws::AWSConfig=default_aws_config(); args...) = delete_pipeline(aws, args)

@inline delete_pipeline(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "DELETE", "/2012-09-25/pipelines/{Id}", args)

@inline delete_pipeline(args) = delete_pipeline(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.delete_preset
    delete_preset([::AWSConfig], arguments::Dict)
    delete_preset([::AWSConfig]; Id=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "DELETE", "/2012-09-25/presets/{Id}", arguments::Dict)
    elastictranscoder([::AWSConfig], "DELETE", "/2012-09-25/presets/{Id}", Id=)

# DeletePreset Operation

The DeletePreset operation removes a preset that you've added in an AWS region.

**Note**
> You can't delete the default presets that are included with Elastic Transcoder.

# Arguments

## `Id = ::String` -- *Required*
The identifier of the preset for which you want to get detailed information.

# Returns

`DeletePresetResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/DeletePreset)
"""
@inline delete_preset(aws::AWSConfig=default_aws_config(); args...) = delete_preset(aws, args)

@inline delete_preset(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "DELETE", "/2012-09-25/presets/{Id}", args)

@inline delete_preset(args) = delete_preset(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.list_jobs_by_pipeline
    list_jobs_by_pipeline([::AWSConfig], arguments::Dict)
    list_jobs_by_pipeline([::AWSConfig]; PipelineId=, <keyword arguments>)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/jobsByPipeline/{PipelineId}", arguments::Dict)
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/jobsByPipeline/{PipelineId}", PipelineId=, <keyword arguments>)

# ListJobsByPipeline Operation

The ListJobsByPipeline operation gets a list of the jobs currently in a pipeline.

Elastic Transcoder returns all of the jobs currently in the specified pipeline. The response body contains one element for each job that satisfies the search criteria.

# Arguments

## `PipelineId = ::String` -- *Required*
The ID of the pipeline for which you want to get job information.

## `Ascending = ::String`
To list jobs in chronological order by the date and time that they were submitted, enter `true`. To list jobs in reverse chronological order, enter `false`.

## `PageToken = ::String`
When Elastic Transcoder returns more than one page of results, use `pageToken` in subsequent `GET` requests to get each successive page of results.

# Returns

`ListJobsByPipelineResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/ListJobsByPipeline)
"""
@inline list_jobs_by_pipeline(aws::AWSConfig=default_aws_config(); args...) = list_jobs_by_pipeline(aws, args)

@inline list_jobs_by_pipeline(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "GET", "/2012-09-25/jobsByPipeline/{PipelineId}", args)

@inline list_jobs_by_pipeline(args) = list_jobs_by_pipeline(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.list_jobs_by_status
    list_jobs_by_status([::AWSConfig], arguments::Dict)
    list_jobs_by_status([::AWSConfig]; Status=, <keyword arguments>)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/jobsByStatus/{Status}", arguments::Dict)
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/jobsByStatus/{Status}", Status=, <keyword arguments>)

# ListJobsByStatus Operation

The ListJobsByStatus operation gets a list of jobs that have a specified status. The response body contains one element for each job that satisfies the search criteria.

# Arguments

## `Status = ::String` -- *Required*
To get information about all of the jobs associated with the current AWS account that have a given status, specify the following status: `Submitted`, `Progressing`, `Complete`, `Canceled`, or `Error`.

## `Ascending = ::String`
To list jobs in chronological order by the date and time that they were submitted, enter `true`. To list jobs in reverse chronological order, enter `false`.

## `PageToken = ::String`
When Elastic Transcoder returns more than one page of results, use `pageToken` in subsequent `GET` requests to get each successive page of results.

# Returns

`ListJobsByStatusResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/ListJobsByStatus)
"""
@inline list_jobs_by_status(aws::AWSConfig=default_aws_config(); args...) = list_jobs_by_status(aws, args)

@inline list_jobs_by_status(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "GET", "/2012-09-25/jobsByStatus/{Status}", args)

@inline list_jobs_by_status(args) = list_jobs_by_status(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.list_pipelines
    list_pipelines([::AWSConfig], arguments::Dict)
    list_pipelines([::AWSConfig]; <keyword arguments>)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/pipelines", arguments::Dict)
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/pipelines", <keyword arguments>)

# ListPipelines Operation

The ListPipelines operation gets a list of the pipelines associated with the current AWS account.

# Arguments

## `Ascending = ::String`
To list pipelines in chronological order by the date and time that they were created, enter `true`. To list pipelines in reverse chronological order, enter `false`.

## `PageToken = ::String`
When Elastic Transcoder returns more than one page of results, use `pageToken` in subsequent `GET` requests to get each successive page of results.

# Returns

`ListPipelinesResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/ListPipelines)
"""
@inline list_pipelines(aws::AWSConfig=default_aws_config(); args...) = list_pipelines(aws, args)

@inline list_pipelines(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "GET", "/2012-09-25/pipelines", args)

@inline list_pipelines(args) = list_pipelines(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.list_presets
    list_presets([::AWSConfig], arguments::Dict)
    list_presets([::AWSConfig]; <keyword arguments>)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/presets", arguments::Dict)
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/presets", <keyword arguments>)

# ListPresets Operation

The ListPresets operation gets a list of the default presets included with Elastic Transcoder and the presets that you've added in an AWS region.

# Arguments

## `Ascending = ::String`
To list presets in chronological order by the date and time that they were created, enter `true`. To list presets in reverse chronological order, enter `false`.

## `PageToken = ::String`
When Elastic Transcoder returns more than one page of results, use `pageToken` in subsequent `GET` requests to get each successive page of results.

# Returns

`ListPresetsResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/ListPresets)
"""
@inline list_presets(aws::AWSConfig=default_aws_config(); args...) = list_presets(aws, args)

@inline list_presets(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "GET", "/2012-09-25/presets", args)

@inline list_presets(args) = list_presets(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.read_job
    read_job([::AWSConfig], arguments::Dict)
    read_job([::AWSConfig]; Id=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/jobs/{Id}", arguments::Dict)
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/jobs/{Id}", Id=)

# ReadJob Operation

The ReadJob operation returns detailed information about a job.

# Arguments

## `Id = ::String` -- *Required*
The identifier of the job for which you want to get detailed information.

# Returns

`ReadJobResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/ReadJob)
"""
@inline read_job(aws::AWSConfig=default_aws_config(); args...) = read_job(aws, args)

@inline read_job(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "GET", "/2012-09-25/jobs/{Id}", args)

@inline read_job(args) = read_job(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.read_pipeline
    read_pipeline([::AWSConfig], arguments::Dict)
    read_pipeline([::AWSConfig]; Id=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/pipelines/{Id}", arguments::Dict)
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/pipelines/{Id}", Id=)

# ReadPipeline Operation

The ReadPipeline operation gets detailed information about a pipeline.

# Arguments

## `Id = ::String` -- *Required*
The identifier of the pipeline to read.

# Returns

`ReadPipelineResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/ReadPipeline)
"""
@inline read_pipeline(aws::AWSConfig=default_aws_config(); args...) = read_pipeline(aws, args)

@inline read_pipeline(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "GET", "/2012-09-25/pipelines/{Id}", args)

@inline read_pipeline(args) = read_pipeline(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.read_preset
    read_preset([::AWSConfig], arguments::Dict)
    read_preset([::AWSConfig]; Id=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/presets/{Id}", arguments::Dict)
    elastictranscoder([::AWSConfig], "GET", "/2012-09-25/presets/{Id}", Id=)

# ReadPreset Operation

The ReadPreset operation gets detailed information about a preset.

# Arguments

## `Id = ::String` -- *Required*
The identifier of the preset for which you want to get detailed information.

# Returns

`ReadPresetResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/ReadPreset)
"""
@inline read_preset(aws::AWSConfig=default_aws_config(); args...) = read_preset(aws, args)

@inline read_preset(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "GET", "/2012-09-25/presets/{Id}", args)

@inline read_preset(args) = read_preset(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.test_role
    test_role([::AWSConfig], arguments::Dict)
    test_role([::AWSConfig]; Role=, InputBucket=, OutputBucket=, Topics=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/roleTests", arguments::Dict)
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/roleTests", Role=, InputBucket=, OutputBucket=, Topics=)

# TestRole Operation

The TestRole operation tests the IAM role used to create the pipeline.

The `TestRole` action lets you determine whether the IAM role you are using has sufficient permissions to let Elastic Transcoder perform tasks associated with the transcoding process. The action attempts to assume the specified IAM role, checks read access to the input and output buckets, and tries to send a test notification to Amazon SNS topics that you specify.

# Arguments

## `Role = ::String` -- *Required*
The IAM Amazon Resource Name (ARN) for the role that you want Elastic Transcoder to test.

## `InputBucket = ::String` -- *Required*
The Amazon S3 bucket that contains media files to be transcoded. The action attempts to read from this bucket.

## `OutputBucket = ::String` -- *Required*
The Amazon S3 bucket that Elastic Transcoder writes transcoded media files to. The action attempts to read from this bucket.

## `Topics = [::String, ...]` -- *Required*
The ARNs of one or more Amazon Simple Notification Service (Amazon SNS) topics that you want the action to send a test notification to.

# Returns

`TestRoleResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/TestRole)
"""
@inline test_role(aws::AWSConfig=default_aws_config(); args...) = test_role(aws, args)

@inline test_role(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "POST", "/2012-09-25/roleTests", args)

@inline test_role(args) = test_role(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.update_pipeline
    update_pipeline([::AWSConfig], arguments::Dict)
    update_pipeline([::AWSConfig]; Id=, <keyword arguments>)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "PUT", "/2012-09-25/pipelines/{Id}", arguments::Dict)
    elastictranscoder([::AWSConfig], "PUT", "/2012-09-25/pipelines/{Id}", Id=, <keyword arguments>)

# UpdatePipeline Operation

Use the `UpdatePipeline` operation to update settings for a pipeline.

**Important**
> When you change pipeline settings, your changes take effect immediately. Jobs that you have already submitted and that Elastic Transcoder has not started to process are affected in addition to jobs that you submit after you change settings.

# Arguments

## `Id = ::String` -- *Required*
The ID of the pipeline that you want to update.

## `Name = ::String`
The name of the pipeline. We recommend that the name be unique within the AWS account, but uniqueness is not enforced.

Constraints: Maximum 40 characters

## `InputBucket = ::String`
The Amazon S3 bucket in which you saved the media files that you want to transcode and the graphics that you want to use as watermarks.

## `Role = ::String`
The IAM Amazon Resource Name (ARN) for the role that you want Elastic Transcoder to use to transcode jobs for this pipeline.

## `AwsKmsKeyArn = ::String`
The AWS Key Management Service (AWS KMS) key that you want to use with this pipeline.

If you use either `S3` or `S3-AWS-KMS` as your `Encryption:Mode`, you don't need to provide a key with your job because a default key, known as an AWS-KMS key, is created for you automatically. You need to provide an AWS-KMS key only if you want to use a non-default AWS-KMS key, or if you are using an `Encryption:Mode` of `AES-PKCS7`, `AES-CTR`, or `AES-GCM`.

## `Notifications = [ ... ]`
The topic ARN for the Amazon Simple Notification Service (Amazon SNS) topic that you want to notify to report job status.

**Important**
> To receive notifications, you must also subscribe to the new topic in the Amazon SNS console.

*   **Progressing**: The topic ARN for the Amazon Simple Notification Service (Amazon SNS) topic that you want to notify when Elastic Transcoder has started to process jobs that are added to this pipeline. This is the ARN that Amazon SNS returned when you created the topic.

*   **Completed**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder has finished processing a job. This is the ARN that Amazon SNS returned when you created the topic.

*   **Warning**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder encounters a warning condition. This is the ARN that Amazon SNS returned when you created the topic.

*   **Error**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder encounters an error condition. This is the ARN that Amazon SNS returned when you created the topic.
```
 Notifications = [
        "Progressing" =>  ::String,
        "Completed" =>  ::String,
        "Warning" =>  ::String,
        "Error" =>  ::String
    ]
```

## `ContentConfig = [ ... ]`
The optional `ContentConfig` object specifies information about the Amazon S3 bucket in which you want Elastic Transcoder to save transcoded files and playlists: which bucket to use, which users you want to have access to the files, the type of access you want users to have, and the storage class that you want to assign to the files.

If you specify values for `ContentConfig`, you must also specify values for `ThumbnailConfig`.

If you specify values for `ContentConfig` and `ThumbnailConfig`, omit the `OutputBucket` object.

*   **Bucket**: The Amazon S3 bucket in which you want Elastic Transcoder to save transcoded files and playlists.

*   **Permissions** (Optional): The Permissions object specifies which users you want to have access to transcoded files and the type of access you want them to have. You can grant permissions to a maximum of 30 users and/or predefined Amazon S3 groups.

*   **Grantee Type**: Specify the type of value that appears in the `Grantee` object:

    *   **Canonical**: The value in the `Grantee` object is either the canonical user ID for an AWS account or an origin access identity for an Amazon CloudFront distribution. For more information about canonical user IDs, see Access Control List (ACL) Overview in the Amazon Simple Storage Service Developer Guide. For more information about using CloudFront origin access identities to require that users use CloudFront URLs instead of Amazon S3 URLs, see Using an Origin Access Identity to Restrict Access to Your Amazon S3 Content.

        **Important**
        > A canonical user ID is not the same as an AWS account number.

    *   **Email**: The value in the `Grantee` object is the registered email address of an AWS account.

    *   **Group**: The value in the `Grantee` object is one of the following predefined Amazon S3 groups: `AllUsers`, `AuthenticatedUsers`, or `LogDelivery`.

*   **Grantee**: The AWS user or group that you want to have access to transcoded files and playlists. To identify the user or group, you can specify the canonical user ID for an AWS account, an origin access identity for a CloudFront distribution, the registered email address of an AWS account, or a predefined Amazon S3 group

*   **Access**: The permission that you want to give to the AWS user that you specified in `Grantee`. Permissions are granted on the files that Elastic Transcoder adds to the bucket, including playlists and video files. Valid values include:

    *   `READ`: The grantee can read the objects and metadata for objects that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `READ_ACP`: The grantee can read the object ACL for objects that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `WRITE_ACP`: The grantee can write the ACL for the objects that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `FULL_CONTROL`: The grantee has `READ`, `READ_ACP`, and `WRITE_ACP` permissions for the objects that Elastic Transcoder adds to the Amazon S3 bucket.

*   **StorageClass**: The Amazon S3 storage class, `Standard` or `ReducedRedundancy`, that you want Elastic Transcoder to assign to the video files and playlists that it stores in your Amazon S3 bucket.
```
 ContentConfig = [
        "Bucket" =>  ::String,
        "StorageClass" =>  ::String,
        "Permissions" =>  [[
            "GranteeType" =>  ::String,
            "Grantee" =>  ::String,
            "Access" =>  [::String, ...]
        ], ...]
    ]
```

## `ThumbnailConfig = [ ... ]`
The `ThumbnailConfig` object specifies several values, including the Amazon S3 bucket in which you want Elastic Transcoder to save thumbnail files, which users you want to have access to the files, the type of access you want users to have, and the storage class that you want to assign to the files.

If you specify values for `ContentConfig`, you must also specify values for `ThumbnailConfig` even if you don't want to create thumbnails.

If you specify values for `ContentConfig` and `ThumbnailConfig`, omit the `OutputBucket` object.

*   **Bucket**: The Amazon S3 bucket in which you want Elastic Transcoder to save thumbnail files.

*   **Permissions** (Optional): The `Permissions` object specifies which users and/or predefined Amazon S3 groups you want to have access to thumbnail files, and the type of access you want them to have. You can grant permissions to a maximum of 30 users and/or predefined Amazon S3 groups.

*   **GranteeType**: Specify the type of value that appears in the Grantee object:

    *   **Canonical**: The value in the `Grantee` object is either the canonical user ID for an AWS account or an origin access identity for an Amazon CloudFront distribution.

        **Important**
        > A canonical user ID is not the same as an AWS account number.

    *   **Email**: The value in the `Grantee` object is the registered email address of an AWS account.

    *   **Group**: The value in the `Grantee` object is one of the following predefined Amazon S3 groups: `AllUsers`, `AuthenticatedUsers`, or `LogDelivery`.

*   **Grantee**: The AWS user or group that you want to have access to thumbnail files. To identify the user or group, you can specify the canonical user ID for an AWS account, an origin access identity for a CloudFront distribution, the registered email address of an AWS account, or a predefined Amazon S3 group.

*   **Access**: The permission that you want to give to the AWS user that you specified in `Grantee`. Permissions are granted on the thumbnail files that Elastic Transcoder adds to the bucket. Valid values include:

    *   `READ`: The grantee can read the thumbnails and metadata for objects that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `READ_ACP`: The grantee can read the object ACL for thumbnails that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `WRITE_ACP`: The grantee can write the ACL for the thumbnails that Elastic Transcoder adds to the Amazon S3 bucket.

    *   `FULL_CONTROL`: The grantee has `READ`, `READ_ACP`, and `WRITE_ACP` permissions for the thumbnails that Elastic Transcoder adds to the Amazon S3 bucket.

*   **StorageClass**: The Amazon S3 storage class, `Standard` or `ReducedRedundancy`, that you want Elastic Transcoder to assign to the thumbnails that it stores in your Amazon S3 bucket.
```
 ThumbnailConfig = [
        "Bucket" =>  ::String,
        "StorageClass" =>  ::String,
        "Permissions" =>  [[
            "GranteeType" =>  ::String,
            "Grantee" =>  ::String,
            "Access" =>  [::String, ...]
        ], ...]
    ]
```

# Returns

`UpdatePipelineResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `AccessDeniedException`, `ResourceInUseException`, `ResourceNotFoundException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/UpdatePipeline)
"""
@inline update_pipeline(aws::AWSConfig=default_aws_config(); args...) = update_pipeline(aws, args)

@inline update_pipeline(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "PUT", "/2012-09-25/pipelines/{Id}", args)

@inline update_pipeline(args) = update_pipeline(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.update_pipeline_notifications
    update_pipeline_notifications([::AWSConfig], arguments::Dict)
    update_pipeline_notifications([::AWSConfig]; Id=, Notifications=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/pipelines/{Id}/notifications", arguments::Dict)
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/pipelines/{Id}/notifications", Id=, Notifications=)

# UpdatePipelineNotifications Operation

With the UpdatePipelineNotifications operation, you can update Amazon Simple Notification Service (Amazon SNS) notifications for a pipeline.

When you update notifications for a pipeline, Elastic Transcoder returns the values that you specified in the request.

# Arguments

## `Id = ::String` -- *Required*
The identifier of the pipeline for which you want to change notification settings.

## `Notifications = [ ... ]` -- *Required*
The topic ARN for the Amazon Simple Notification Service (Amazon SNS) topic that you want to notify to report job status.

**Important**
> To receive notifications, you must also subscribe to the new topic in the Amazon SNS console.

*   **Progressing**: The topic ARN for the Amazon Simple Notification Service (Amazon SNS) topic that you want to notify when Elastic Transcoder has started to process jobs that are added to this pipeline. This is the ARN that Amazon SNS returned when you created the topic.

*   **Completed**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder has finished processing a job. This is the ARN that Amazon SNS returned when you created the topic.

*   **Warning**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder encounters a warning condition. This is the ARN that Amazon SNS returned when you created the topic.

*   **Error**: The topic ARN for the Amazon SNS topic that you want to notify when Elastic Transcoder encounters an error condition. This is the ARN that Amazon SNS returned when you created the topic.
```
 Notifications = [
        "Progressing" =>  ::String,
        "Completed" =>  ::String,
        "Warning" =>  ::String,
        "Error" =>  ::String
    ]
```

# Returns

`UpdatePipelineNotificationsResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `ResourceInUseException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/UpdatePipelineNotifications)
"""
@inline update_pipeline_notifications(aws::AWSConfig=default_aws_config(); args...) = update_pipeline_notifications(aws, args)

@inline update_pipeline_notifications(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "POST", "/2012-09-25/pipelines/{Id}/notifications", args)

@inline update_pipeline_notifications(args) = update_pipeline_notifications(default_aws_config(), args)

"""
    using AWSSDK.ElasticTranscoder.update_pipeline_status
    update_pipeline_status([::AWSConfig], arguments::Dict)
    update_pipeline_status([::AWSConfig]; Id=, Status=)

    using AWSCore.Services.elastictranscoder
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/pipelines/{Id}/status", arguments::Dict)
    elastictranscoder([::AWSConfig], "POST", "/2012-09-25/pipelines/{Id}/status", Id=, Status=)

# UpdatePipelineStatus Operation

The UpdatePipelineStatus operation pauses or reactivates a pipeline, so that the pipeline stops or restarts the processing of jobs.

Changing the pipeline status is useful if you want to cancel one or more jobs. You can't cancel jobs after Elastic Transcoder has started processing them; if you pause the pipeline to which you submitted the jobs, you have more time to get the job IDs for the jobs that you want to cancel, and to send a [CancelJob](@ref) request.

# Arguments

## `Id = ::String` -- *Required*
The identifier of the pipeline to update.

## `Status = ::String` -- *Required*
The desired status of the pipeline:

*   `Active`: The pipeline is processing jobs.

*   `Paused`: The pipeline is not currently processing jobs.

# Returns

`UpdatePipelineStatusResponse`

# Exceptions

`ValidationException`, `IncompatibleVersionException`, `ResourceNotFoundException`, `ResourceInUseException`, `AccessDeniedException` or `InternalServiceException`.

See also: [AWS API Documentation](https://docs.aws.amazon.com/goto/WebAPI/elastictranscoder-2012-09-25/UpdatePipelineStatus)
"""
@inline update_pipeline_status(aws::AWSConfig=default_aws_config(); args...) = update_pipeline_status(aws, args)

@inline update_pipeline_status(aws::AWSConfig, args) = AWSCore.Services.elastictranscoder(aws, "POST", "/2012-09-25/pipelines/{Id}/status", args)

@inline update_pipeline_status(args) = update_pipeline_status(default_aws_config(), args)

end # module ElasticTranscoder

#==============================================================================#
# End of file
#==============================================================================#


text:
using HemirealNumbers
using Base.Test

const mult_safe = VERSION >= v"0.5.0-dev"   # julia #14293

p1 = @inferred(PureHemi(1,2))
p2 = PureHemi(1,2)
p3 = @inferred(PureHemi(1.0,2))
@test p1 == p2
@test p1 == p3

@test @inferred(convert(PureHemi{Float64}, p1)) === PureHemi(1.0,2.0)
@test @inferred(convert(PureHemi{Float64}, 0)) == PureHemi(0.0,0.0)
@test_throws DomainError convert(PureHemi{Float64}, 1)

@test @inferred(-p1) == PureHemi(-1,-2)

@test @inferred(PureHemi(1,2) + PureHemi(5,3)) === PureHemi(6,5)
@test @inferred((μ+2ν) + (5μ+3ν)) === 6μ+5ν
@test PureHemi(1,2) - PureHemi(5,3) == PureHemi(-4,-1)
@test μ*μ == 0
@test ν*ν == 0
@test ν*μ == 1
@test μ*ν == 1
@test 2*PureHemi(3,4) == PureHemi(6,8)
@test PureHemi(3,4)*(-1) == PureHemi(-3,-4)
@test false*PureHemi(3,4) == 0
@test PureHemi(3,4)*true == 3μ+4ν
@test @inferred((3μ+4ν)/2) === 1.5μ+2ν
@test 2\(3μ+4ν) === 1.5μ+2ν
@test 18/PureHemi(3,3) == PureHemi(3,3)
@test PureHemi(3,3)\7 === PureHemi(7/6,7/6)
@test @inferred((4μ+2ν)/(2μ+ν)) === 2.0
@test_throws AssertionError (4μ+2ν)/(2μ+3ν)
@test @inferred((4μ+2ν)\(2μ+ν)) === 0.5
@test PureHemi(3,4)^2 == 24
@test PureHemi(3,4)^(1//4) == 24^(1//8)
@test PureHemi(3,4)^2.0 == 24.0
@test real(PureHemi(0.2,0.3)) == 0.0
@test real(PureHemi{Bool}) == Bool
@test mu(PureHemi(0.2,0.3)) == 0.2
@test nu(PureHemi(0.2,0.3)) == 0.3
@test zero(PureHemi(0.2,0.3)) == PureHemi(0,0)
@test zero(PureHemi{Bool}) == PureHemi(false,false)
@test conj(3μ+4ν) == 3μ+4ν
@test isfinite(3μ+4ν)
@test !isfinite(Inf*μ + 3.2*ν)
@test_approx_eq PureHemi(1,2) PureHemi(nextfloat(1.0), nextfloat(2.0))

@test @inferred(Hemireal(1,2,3)) === 1+2μ+3ν
@test @inferred(Hemireal(1.0,2,3)) === 1.0+2.0μ+3.0ν
@test @inferred(Hemireal(2μ+3ν)) === Hemireal(0,2,3)
@test @inferred(Hemireal(7.0,2μ+3ν)) === Hemireal(7.0,2.0,3.0)
@test @inferred(Hemireal(7)) === Hemireal(7,0,0)

@test convert(Hemireal{Float64}, 1) === Hemireal(1.0,0.0,0.0)
@test convert(Hemireal{Float64}, 2μ+3ν) === Hemireal(0.0,2.0,3.0)
@test convert(Hemireal{Float64}, Hemireal(1,2,3)) === Hemireal(1.0,2.0,3.0)

@test @inferred(-(7+2μ)) === Hemireal(-7,-2,0)
@test @inferred((1+2μ+3ν) + (5.5+2.1μ+3.2ν)) === Hemireal(6.5,4.1,6.2)
@test_approx_eq @inferred((1+2μ+3ν) - (5.5+2.1μ+3.2ν)) Hemireal(-4.5,-0.1,-0.2)
@test_approx_eq @inferred((1+2μ+3ν) * (5.5+2.1μ+3.2ν)) Hemireal(5.5+6.4+6.3, 13.1, 19.7)

@test @inferred((1+2μ+3ν) + PureHemi(2.1,3.2)) === Hemireal(1,4.1,6.2)
@test @inferred(PureHemi(7,9) + (2+μ-6ν)) === Hemireal(2,8,3)
@test_approx_eq @inferred((1+2μ+3ν) - PureHemi(2.1,3.2)) Hemireal(1.0,-0.1,-0.2)
@test @inferred(PureHemi(7,9) - (2+μ-6ν)) === Hemireal(-2,6,15)
@test_approx_eq @inferred((1+2μ+3ν) * PureHemi(2.1,3.2)) Hemireal(6.4+6.3, 2.1, 3.2)
@test @inferred(PureHemi(7,9) * (2+μ-6ν)) === Hemireal(-33,14,18)

@test @inferred((1+2μ+3ν) + 5.5) === Hemireal(6.5,2.0,3.0)
@test @inferred(3 + (2+μ-6ν)) === Hemireal(5,1,-6)
@test @inferred((1+2μ+3ν) - 5.5) === Hemireal(-4.5,2.0,3.0)
@test @inferred(3 - (2+μ-6ν)) === Hemireal(1,-1,6)
@test @inferred(false*(2+μ-6ν)) === Hemireal(0,0,0)
@test @inferred((2+μ-6ν)*true) === Hemireal(2,1,-6)
@test @inferred(1.5*(2+μ-6ν)) === Hemireal(3.0,1.5,-9.0)
@test @inferred((2+μ-6ν)*1.5) === Hemireal(3.0,1.5,-9.0)
@test @inferred((2+μ-6ν)/1.5) === Hemireal(2/1.5,1/1.5,-6/1.5)
@test @inferred(1.5\(2+μ-6ν)) === Hemireal(2/1.5,1/1.5,-6/1.5)

x = 3.2+μ-ν
@test real(x) === 3.2
@test real(Hemireal{Float16}) == Float16
@test mu(x) == 1
@test nu(x) == -1
@test mu([x]) == [1]
@test nu([x]) == [-1]
@test conj(x) === x
@test zero(x) === Hemireal(0.0,0.0,0.0)
@test zero(Hemireal{Float16}) === Hemireal{Float16}(0,0,0)

a = [μ+2ν, 5μ-ν]
@test mu(a) == [1,5]
@test nu(a) == [2,-1]
if mult_safe
    @test @inferred(a*a') == [4 9; 9 -10]
    @test isa(a*a', Matrix{Int})
    @test @inferred(a'*a) == [-6]
    @test isa(a'*a, Vector{Int})
end
A = [μ 0; 2μ+3ν 5μ-4ν]
@test @inferred(A*a) == [2,-18]
@test isa(A*a, Vector{Hemireal{Int}})
z = zero(PureHemi{Int})
A = [μ z; 2μ+3ν 5μ-4ν]
@test @inferred(A*a) == [2,-18]
@test isa(A*a, Vector{Int})
@test @inferred(A*A)   == [0 0; 10 -40]
@test isa(A*A,   Matrix{Int})
@test @inferred(A*A')  == [0 3;  3 -28]
@test isa(A*A',  Matrix{Int})
@test @inferred(A*A.') == [0 3;  3 -28]
@test isa(A*A.', Matrix{Int})
@test @inferred(A'*A)  == [12 7; 7 -40]
@test isa(A'*A,  Matrix{Int})
@test @inferred(A.'*A) == [12 7; 7 -40]
@test isa(A.'*A, Matrix{Int})
@test @inferred(A'*A') == [0 10; 0 -40]
@test isa(A'*A', Matrix{Int})
@test @inferred(A.'*A.') == [0 10; 0 -40]
@test isa(A.'*A.', Matrix{Int})

@test @inferred(A*[1,2]) == [μ,12μ-5ν]
if mult_safe
    @test isa(A*[1,2], Vector{PureHemi{Int}})
end

@test μ*a == [2,-1]
@test isa(μ*a, Vector{Int})
@test 3*a == [3μ+6ν, 15μ-3ν]
@test isa(3*a, Vector{PureHemi{Int}})

# Promotion
@test isa([μ, false], Vector{Hemireal{Bool}})
@test isa([2μ, false], Vector{Hemireal{Int}})
@test isa([μ, π], Vector{Hemireal{Float64}})
@test isa([1+μ, false], Vector{Hemireal{Int}})
@test isa([1+μ, π], Vector{Hemireal{Float64}})
@test isa([1+μ,2.0+ν], Vector{Hemireal{Float64}})

io = IOBuffer()
show(io, 1μ+2ν)
@test takebuf_string(io) == "1μ + 2ν"
show(io, 1μ-2ν)
@test takebuf_string(io) == "1μ - 2ν"
show(io, 3-4.0μ+2ν)
@test takebuf_string(io) == "3.0 - 4.0μ + 2.0ν"
show(io, 3-4.0μ-2ν)
@test takebuf_string(io) == "3.0 - 4.0μ - 2.0ν"

ext: jl


text:
module EMSx

using ProgressMeter
using Distributed
using Dates, TimeZones, CSV, DataFrames, CodecZlib
using JLD2
using HTTP
using Statistics

const DIR = dirname(@__DIR__)

include("struct.jl")

include("database_interface/download_data.jl")
include("database_interface/download_data_schneider.jl")
include("database_interface/download_data_zenodo.jl")
include("database_interface/split_data.jl")
include("database_interface/simulation_interface.jl")

include("parallelization/parallel_progress.jl")
include("parallelization/parallel.jl")

include("function.jl")
include("simulate.jl")
include("evaluate.jl")

end


text:
"""
    inpolygon(points, poly)

Check if a point or points are inside a polygon.

This algorithm is very efficient for many points, less so a single point.

# Arguments

- `points`: an `AbstractVector` or a `Tuple` or `Real`, an `AbstractVector` of these,
    or a `GeoInterface.AbstractGeometry`.
- `poly`: an `AbstractVector` or nested `AbstractVector` with an inner
    `AbstractVector` or `Tuple` of `Real`, or a `GeoInterface.AbstractPolygon`.

Returns a `Bool` or `AbstractVector{Bool}`.
"""
function inpolygon end
function inpolygon(points, geom::GI.AbstractGeometry; kw...)
    throw(ArgumentError("`inpolygon` works with GeoInterface polygons, but not for $(typeof(geom))"))
end
function inpolygon(points, poly::Union{GI.AbstractPolygon,GI.AbstractMultiPolygon}; kw...)
    inpolygon(points, GI.coordinates(poly); kw...)
end
function inpolygon(point::Union{Tuple,AbstractVector{<:Real}}, poly::AbstractVector; kw...)
    inpolygon([point], poly; kw...)[1]
end
function inpolygon(points::GI.AbstractGeometry, poly::AbstractVector; kw...)
    inpolygon(GI.coordinates(points), poly, kw...)
end
function inpolygon(points::AbstractVector, poly::AbstractVector; kw...)
    inpolygon(collect(_flat_nodes(points)), poly; kw...)
end
function inpolygon(points::Union{Base.Generator,AbstractVector{<:Union{<:Tuple,<:AbstractVector{<:Real}}}}, poly::AbstractVector; kw...)
    edges = to_edges(poly)
    nodes = collect(_flat_nodes(poly))
    inpoly_matrix = inpoly2(points, nodes, edges; kw...)
    return view(inpoly_matrix, :, 1)
end


text:
"""
    check_neighbor_order(cells, cellNeighbors)

check if cellNeighbors are regularized, e.g., in a triangle,

- face 1 -> neighbor cell 1
- face 2 -> neighbor cell 2
- face 3 -> neighbor cell 3
"""
function check_cell_neighbor(cells, cellNeighbors)
    nCells = size(cells, 1)
    nNodesPerCell = size(cells, 2)

    for i = 1:nCells
        cids = cellNeighbors[i, :]

        fpids = [cells[i, j:j+1] for j = 1:nNodesPerCell-1]
        push!(fpids, [cells[i, nNodesPerCell], cells[i, 1]])

        for j = 1:nNodesPerCell
            nid = cids[j]
            if nid != -1
                @assert length(intersect(fpids[j], cells[nid, :])) == 2 "neighbor is not regularized at ($i, $j)"
            end
        end
    end

    return true
end

"""
    check_neighbor_order(cells, cellNeighbors)

check if cellFaces are regularized, e.g., in a triangle,

- face 1 -> points 1 & 2
- face 2 -> points 2 & 3
- face 3 -> points 3 & 1
"""
function check_cell_face(cells, cellFaces, facePoints)
    nCells = size(cells, 1)
    nNodesPerCell = size(cells, 2)

    for i = 1:nCells
        fpids = [cells[i, j:j+1] for j = 1:nNodesPerCell-1]
        push!(fpids, [cells[i, nNodesPerCell], cells[i, 1]])

        for j = 1:nNodesPerCell
            @assert sort(facePoints[cellFaces[i, j], :]) == sort(fpids[j]) "face is not regularized at ($i, $j)"
        end
    end

    return true
end


text:
using Documenter, Dolo

makedocs(
    modules=[Dolo],
    authors="Spencer Lyon, Pablo Winant, and contributors",
    clean=false,
    format=:html,
    sitename="Dolo.jl",
    doctest=true,
    pages=[
        "Guide" => [
            "index.md",
            "modeling_language.md",
            "model_specification.md",
            "algos.md",
            "simulate.md"
        ],
        # "Developpers" => [
        #     "model_api.md"
        # ]
    ],
)

deploydocs(
    repo   = "github.com/EconForge/Dolo.jl.git",
    julia  = "0.6",
    target = "build",
    deps   = nothing,
    make   = nothing,
)


text:
# small scrip to compute the solution of Lippmann Schwinger equation
# We test that the density sigma can be properly factorized as a
# mu_slow*e^{i \omega \phi(x)} where $\phi$ accounts for the travel
# time within the geometrical optics solution

using PyPlot
using IterativeSolvers
using SpecialFunctions

include("../src/FastConvolutionDual.jl")
include("../src/FastConvolution.jl")
include("../src/Preconditioner.jl")
include("../src/FastConvolutionSlowDual.jl")

FFTW.set_num_threads(4);
BLAS.set_num_threads(4)

#Defining Omega
h = 0.0015
k = 1/h

# size of box
a = 1;
b = 0.125;
x = collect(-a/2:h:a/2)
y = collect(-b/2:h:b/2)
(n,m) = length(x), length(y)
N = n*m
X = repmat(x, 1, m)[:];
Y = repmat(y', n,1)[:];
# we solve \triangle u + k^2(1 + nu(x))u = 0

# We use the modified quadrature in Duan and Rohklin
(ppw,D) = referenceValsTrapRule();
D0 = D[1];

window(y,alpha) = 1*(abs.(y).<=alpha/2) + (abs.(y).>alpha/2).*(abs.(y).<alpha).*
                     exp.(2*exp.(-0.5*alpha./(abs.(y)-alpha/2))./
                         ((abs.(y)-alpha/2)./(0.5*alpha)-1) )

window(y,alpha, beta) = 1*(abs.(y).<=beta) + (abs.(y).>beta).*(abs.(y).<alpha).*
                           exp.(2*exp.(-(alpha- beta)./(abs.(y)-beta))./
                               ((abs.(y)-beta)./(alpha- beta)-1) )

# Defining the smooth perturbation of the slowness
nu(x,y) = -0.05*(sin.(4*pi*x/(0.96))).*
          window(y,0.96*b/2, 0.48*b/2).*
          window(x,0.96*0.5, 0.3);

figure(1); clf();
imshow(real(reshape( (1 + nu(X,Y)),n,m)),
       extent=[y[1], y[end], x[1], x[end]]); cb =  colorbar();

###############################################################
###############################################################

# comparison with the true solution
fastconvDual = buildFastConvolutionDual(x,y,h,k,nu,quadRule ="Greengard_Vico" );
#fastconvDual = buildFastConvolutionDual(x,y,h,k,nu);

u_inc = exp.(k*im*X);

rhsDual = -k^2*nu(X,Y).*u_inc ;

# allocating the solution
sigma = zeros(Complex128,N);

# solving the system using GMRES
@time info =  gmres!(sigma, fastconvDual, rhsDual, maxiter = 10 )
# println(info[2].residuals[:])

figure(4)
clf()
imshow(real(reshape(sigma,n,m))); colorbar();
title("density \sigma")

uDual = FFTconvolution(fastconvDual,sigma);

figure(5)
clf()
imshow(real(reshape(uDual,n,m))); colorbar();
title("scattered wavefield")

figure(7)
clf()
imshow(real(reshape(uDual+u_inc,n,m))); colorbar();
title("total wavefield")

# computing the geometric optic phi
phiGO = cumsum(reshape(sqrt(1+nu(X,Y)), n,m))*h-0.5;
u_gem = exp(k*im*phiGO);

# plotting the solution
figure(8)
clf()
imshow(real(reshape(sigma./u_gem[:],n,m)))
title("sigma/ u_geom ")

figure(9)
clf()
imshow(real(reshape(sigma./u_inc[:],n,m))); colorbar();
title("sigma/ u_inc ")

figure(10)
clf()
imshow(real(reshape(uDual./u_gem[:],n,m)))
title("u / u_geom ")

fastconvSlowDual = buildFastConvolutionSlowDual(x,y,h,k,nu, [1.0, 0.0],
                                                quadRule ="Greengard_Vico");

rhsSlowDual = -k^2*nu(X,Y) + zeros(Complex128,N);

# allocating the solution
sigmaSlow = zeros(Complex128,N);

# solving the system using GMRES
@time info =  gmres!(sigmaSlow, fastconvSlowDual, rhsSlowDual, maxiter = 10 )
# println(info[2].residuals[:])

figure(11)
clf()
imshow(real(reshape(sigmaSlow,n,m)))
title("sigma Slow")

norm2 = maximum(abs(sigma./u_inc[:]))
figure(21)
clf()
imshow(abs(reshape(sigmaSlow - sigma./u_inc[:],n,m)/norm2)); colorbar();
title("error")

max_stars_repo_name: Forgotten/Lippmann-Schingwer

max_issues_repo_name: Forgotten/Lippmann-Schingwer

max_forks_repo_name: Forgotten/Lippmann-Schingwer


text:
@doc doc"""
    Rotations{N} <: Manifold

Special orthogonal manifold $\mathrm{SO}(n)$ represented by $n\times n$
real-valued orthogonal matrices with determinant $+1$.

# Constructor

    Rotations(n)

generates the $\mathrm{SO}(n) \subset \mathbb R^{n\times n}$
"""
struct Rotations{N} <: Manifold end
Rotations(n::Int) = Rotations{n}()

@doc doc"""
    manifold_dimension(S::Rotations)

returns the dimension of the manifold $\mathrm{SO}(n)$, i.e. $\frac{n(n-1)}{2}$.
"""
manifold_dimension(S::Rotations{N}) where {N} = div(N*(N-1), 2)

@doc doc"""
    inner(S, x, w, v)

compute the inner product of the two tangent vectors `w, v` from the tangent
plane at `x` on the special orthogonal space `S=`$\mathrm{SO}(n)$ using the
restriction of the metric from the embedding, i.e.

$(v, w)_x = \operatorname{tr}(v^T w)$.

Tangent vectors are represented by matrices.
"""
inner(S::Rotations, x, w, v) = dot(w, v)

project_tangent!(S::Rotations, w, x, v) = w .= (transpose(v).-v)./2

function exp!(S::Rotations, y, x, v)
    y .= x * exp(v)
    return y
end

@doc doc"""
    log!(M, v, x, y)

compute the logarithmic map on the [`Rotations`](@ref) manifold
`M`$=\mathrm{SO}(n)$, which is given by
```math
\operatorname{log}_{x} y =
  \frac{1}{2} \bigl(\operatorname{Log}(x^{\mathrm{T}}y)
  - (\operatorname{Log} x^{\mathrm{T}}y)^{\mathrm{T}}),
```
where $\operatorname{Log}$ denotes the matrix logarithm
and save the result to `v`.

For antipodal rotations the function returns one of the tangent vectors that
point at `y`.
"""
function log!(S::Rotations, v::TV, x, y) where TV
    U = transpose(x) * y
    # MMatrix doesn't have `log` defined
    U1 = TV(real(log(Array(U))))
    v .= (U1 .- transpose(U1))./2
    return v
end

injectivity_radius(M::Rotations, x) = π*sqrt(2.0)

@doc doc"""
    PolarRetraction

Retraction on the rotations manifold using the polar method.
This SVD-based retraction is a second-order approximation of the
exponential map. Let

$USV = x + xv$

be the singular value decomposition, then the formula reads

$\operatorname{retr}_x v = UV^\mathrm{T}$

Retraction is performed by the function [`retract!(::Rotations, y, x, v, ::PolarRetraction)`](@ref)
"""
struct PolarRetraction <: AbstractRetractionMethod end

@doc doc"""
    retract_polar!(M::Rotations, y, x, v, method::PolarRetraction)

Compute the SVD-based retraction [`PolarRetraction`](@ref), a second-order
approximation of the exponential map.
"""
function retract!(M::Rotations, y, x, v, method::PolarRetraction)
    A = x + x*v
    S = svd(A)
    y .= S.U * transpose(S.V)
    return y
end

"""
    QRRetraction

Retraction on the rotations manifold using the QR method, a first order
approximation of the exponential map.
"""
struct QRRetraction <: AbstractRetractionMethod end

@doc doc"""
    retract!(M, y, x, v, method::QRRetraction)

This QR-based retraction is a first-order approximation of the exponential map.
"""
function retract!(M::Rotations, y::AbstractArray{T}, x, v, method::QRRetraction) where T
    A = x + x*v
    qr_decomp = qr(A)
    d = diag(qr_decomp.R)
    D = Diagonal( sign.(d .+ convert(T, 0.5)) )
    y .= qr_decomp.Q * D
    return y
end

retract!(M::Rotations, y, x, v) = retract!(M, y, x, v, QRRetraction())

"""
    PolarInverseRetraction

Inverse retraction on the rotations manifold using the polar method.
"""
struct PolarInverseRetraction <: AbstractInverseRetractionMethod end

injectivity_radius(::Rotations, x, ::PolarRetraction) = π*sqrt(2.0)/2

@doc doc"""
    inverse_retract!(M, v, x, y, ::PolarInverseRetraction)

Compute a vector from the tagent space $T_x\mathrm{SO}(n)$
of the point `x` on the [`Rotations`](@ref) manifold `M`
with which the point `y` can be reached by the
[`PolarRetraction`](@ref) from the point `x` after time 1.

The formula reads
$v = -\frac{1}{2}(x^{\mathrm{T}}ys - (x^{\mathrm{T}}ys)^{\mathrm{T}})$

where $s$ is the solution to the Sylvester equation

$x^{\mathrm{T}}ys + s(x^{\mathrm{T}}y)^{\mathrm{T}} + 2\mathrm{I}_n = 0.$
"""
function inverse_retract!(M::Rotations, v, x, y, method::PolarInverseRetraction)
    A = transpose(x) * y
    H = 2 * one(x)
    try
        B = sylvester(collect(A), collect(transpose(A)), collect(H))
        C = A * B
        v .= (transpose(C) .- C)./2
    catch e
        if isa(e, LinearAlgebra.LAPACKException)
            throw(OutOfInjectivityRadiusError())
        else
            rethrow()
        end
    end
    return v
end

"""
    QRInverseRetraction

Inverse retraction on the rotations manifold using the QR method.
"""
struct QRInverseRetraction <: AbstractInverseRetractionMethod end

@doc doc"""
    inverse_retract!(M::Rotations, x, y, ::QRInverseRetraction)

Compute a vector from the tagent space $T_x\mathrm{SO}(n)$
of the point `x` on the [`Rotations`](@ref) manifold `M`
with which the point `y` can be reached by the
[`QRRetraction`](@ref) from the point `x` after time 1.
"""
function inverse_retract!(M::Rotations{N}, v, x, y, ::QRInverseRetraction) where N
    A = transpose(x) * y
    R = zero(v)
    for i = 1:N
        b = zeros(i)
        b[end] = 1
        b[1:(end-1)] = - transpose(R[1:(i-1), 1:(i-1)]) * A[i, 1:(i-1)]
        R[1:i, i] = A[1:i, 1:i] \ b
    end
    C =  A * R
    v .= (C .- transpose(C))./2
    return v
end

zero_tangent_vector(S::Rotations, x) = zero(x)
zero_tangent_vector!(S::Rotations, v, x) = (v .= zero(x))

"""
    is_manifold_point(S,x; kwargs...)

checks, whether `x` is a valid point on the [`Rotations`](@ref) `S`,
i.e. is an array of size [`manifold_dimension`](@ref)`(S)` and represents a
valid rotation.
The tolerance for the last test can be set using the ´kwargs...`.
"""
function is_manifold_point(S::Rotations{N},x; kwargs...) where {N}
    if size(x) != (N, N)
        throw(DomainError(size(x),
            "The point $(x) does not lie on $S, since its size is not $((N, N))."))
    end
    if !isapprox(det(x), 1; kwargs...)
        throw(DomainError(norm(x), "The determinant of $x has to be +1 but it is $(det(x))"))
    end
    if !isapprox(transpose(x)*x, one(x); kwargs...)
        throw(DomainError(norm(x), "$x has to be orthogonal but it's not"))
    end
    return true
end

"""
    is_tangent_vector(S,x,v; kwargs... )

checks whether `v` is a tangent vector to `x` on the [`Rotations`](@ref)
space `S`, i.e. after [`is_manifold_point`](@ref)`(S,x)`, `v` has to be of same
dimension as `x` and orthogonal to `x`.
The tolerance for the last test can be set using the ´kwargs...`.
"""
function is_tangent_vector(S::Rotations{N},x,v; kwargs...) where N
    is_manifold_point(S,x)
    if size(v) != (N, N)
        throw(DomainError(size(v),
            "The array $(v) is not a tangent to a point on $S since its size does not match $((N, N))."))
    end
    if !isapprox(transpose(v)+v, zero(v); kwargs...)
        throw(DomainError(size(v),
            "The array $(v) is not a tangent to a point on $S since it is not skew-symmetric."))
    end
    return true
end

"""
    gaussian_tvector_distribution(S::Rotations, x, σ)

Normal distribution in ambient space with standard deviation `σ`
projected to tangent space at `x`.
"""
function normal_tvector_distribution(S::Rotations, x, σ)
    d = Distributions.MvNormal(reshape(zero(x), :), σ)
    return ProjectedTVectorDistribution(S, x, d, project_tangent!, x)
end

"""
    NormalRotationDistribution(M::Rotations, d::Distribution, x::TResult)

Distribution that returns a random point on the manifold [`Rotations`](@ref)
`M`. Random point is generated using base distribution `d` and the type
of the result is adjusted to `TResult`.

See [`normal_rotation_distribution`](@ref) for details.
"""
struct NormalRotationDistribution{TResult, TM<:Rotations, TD<:Distribution} <: MPointDistribution{TM}
    manifold::TM
    distr::TD
end

function NormalRotationDistribution(M::Rotations, d::Distribution, x::TResult) where TResult
    return NormalRotationDistribution{TResult, typeof(M), typeof(d)}(M, d)
end

function _fix_random_rotation(A::AbstractMatrix)
    s=diag(sign.(qr(A).R))
    D=Diagonal(s)
    C = qr(A).Q*D
    if det(C) < 0
        C[:,[1,2]] = C[:,[2,1]]
    end
    return C
end

function rand(rng::AbstractRNG, d::NormalRotationDistribution{TResult,Rotations{N}}) where {TResult,N}
    if N==1
        return convert(TResult, ones(1,1))
    else
        A = reshape(rand(rng, d.distr), (N, N))
        return convert(TResult, _fix_random_rotation(A))
    end
end

function _rand!(rng::AbstractRNG, d::NormalRotationDistribution{TResult,Rotations{N}}, x::AbstractArray{<:Real}) where {TResult,N}
    if N==1
        x .= ones(1,1)
    else
        rand!(rng, d.distr, x)
        x .= _fix_random_rotation(x)
    end
    return x
end

@doc doc"""
    normal_rotation_distribution(M::Rotations, x, σ::Real)

return a random point on the manifold [`Rotations`](@ref) `M`
by generating a (Gaussian) random orthogonal matrix with determinant $+1$. Let

$QR = A$

be the QR decomposition of a random matrix $A$, then the formula reads

$x = QD$

where $D$ is a diagonal matrix with the signs of the diagonal entries of $R$,
i.e.

$D_{ij}=\begin{cases} \operatorname{sgn}(R_{ij}) & \text{if} \; i=j \\ 0 & \, \text{otherwise} \end{cases}.$

It can happen that the matrix gets -1 as a determinant. In this case, the first
and second columns are swapped.

The argument `x` is used to determine the type of returned points.
"""
function normal_rotation_distribution(M::Rotations{N}, x, σ::Real) where N
    d = Distributions.MvNormal(zeros(N*N), σ)
    return NormalRotationDistribution(M, d, x)
end


text:
using D91PVInversion
using IterativeSolvers
using Random
using Printf
using JLD2
import PyPlot; const plt = PyPlot

# Define functional form of PV anomaly
function q′fun(x, y, z, A, L, H, p)
    πc = p.π0 - 0.5
    return A*exp(-(x^2 + y^2)/L^2)*exp(-(z-πc)^2/H^2)
end

# Set up inversion domain
params = Params(Float64)
domain = Domain(params, size = (48, 48, 8), x = (-3, 3), y = (-3, 3))

# Load background fields
B = 0.1
fname = @sprintf("output/invert_A%.1f.jld2", B)
ψ0 = load(fname, "ψ")
ϕ0 = load(fname, "ϕ")

# Create inversion problem
inv = LinearizedInversion(
    ψ0 = ψ0, ϕ0 = ϕ0,
    domain = domain, params = params
)

# Initialize problem
A = 1.0
L = 0.1
H = 0.1
initialize!(inv, q′fun, A, L, H, params)

# Solve
Random.seed!(1234)
solve!(inv; verbose = true)

# Compute diagnostics
ψ′ = inv.ψ′
ϕ′ = inv.ϕ′
q′ = inv.q′
u′ = diagnose_umag(ψ′, domain)
v′ = diagnose_v(ψ′, domain)
ζ′ = diagnose_ζ(ψ′, domain)
θ′ = diagnose_θ(ϕ′, domain)
nx, ny, nz = size(domain)
x = domain.xc[1:nx]
z = domain.zc[1:nz]
v′mid = 0.5*(v′[1:nx,ny÷2,1:nz] + v′[1:nx,ny÷2+1,1:nz])
θ′mid = 0.5*(θ′[1:nx,ny÷2,1:nz] + θ′[1:nx,ny÷2+1,1:nz])
ζ′mid = 0.5*(ζ′[1:nx,ny÷2,1:nz] + ζ′[1:nx,ny÷2+1,1:nz])
q′mid = 0.5*(q′[1:nx,ny÷2,1:nz] + q′[1:nx,ny÷2+1,1:nz])

# Calculate vertical penetration
u′bot = u′[1:nx,1:ny,1]
u′max = 0.5*(u′[1:nx,1:ny,nz÷2] + u′[1:nx,1:ny,nz÷2+1])
vpen = maximum(u′bot)/maximum(u′max)
print("Vertical penetration: ", vpen, "\n")

# Plot solution
fig, axes = plt.subplots(
    figsize = (9.5, 3), nrows = 1, ncols = 4,
    sharey = true, sharex = true, constrained_layout = true
)
c = axes[1].contour(x, z, q′mid', colors = "black")
Δc = length(c.levels) > 1 ? c.levels[2] - c.levels[1] : NaN
axes[1].set_title(@sprintf("q′\nmax %.1e\ncontour interval %.1e", maximum(q′mid), Δc))
c = axes[2].contour(x, z, v′mid', colors = "black")
Δc = length(c.levels) > 1 ? c.levels[2] - c.levels[1] : NaN
axes[2].set_title(@sprintf("v′\nmax %.1e\ncontour interval %.1e", maximum(v′mid), Δc))
c = axes[3].contour(x, z, ζ′mid', colors = "black")
Δc = length(c.levels) > 1 ? c.levels[2] - c.levels[1] : NaN
axes[3].set_title(@sprintf("ζ′\nmax %.1e\ncontour interval %.1e", maximum(ζ′mid), Δc))
c = axes[4].contour(x, z, θ′mid', colors = "black")
Δc = length(c.levels) > 1 ? c.levels[2] - c.levels[1] : NaN
axes[4].set_title(@sprintf("θ′\nmax %.1e\ncontour interval %.1e", maximum(θ′mid), Δc))
axes[1].set_xlim([-1, 1])
axes[1].invert_yaxis()
plt.show()

# Save output
save_inversion_results(@sprintf("output/invert_linear_A%.1f_B%.1f.jld2", A, B), inv)


text:
"Predicate Relaxation"
module SoftPredicates

using Spec
# import Omega
# import ForwardDiff
import Cassette
using LinearAlgebra: norm
using DocStringExtensions

export  d,
        SoftBool,
        recursofteq,
        softeq,
        softlt,
        softgt,
        >ₛ,
        >=ₛ,
        <=ₛ,
        <ₛ,
        ==ₛ,
        err,
        logerr,
        anyₛ,
        allₛ,

        # Kernels
        kse,
        kseα,
        kf1,
        kf1β,
        withkernel,
        atα,
        @atα

        # indomain,
        # indomainₛ,
        # applynotrackerr,
        # applytrackerr

abstract type AbstractSoftBool <: Real end
const AbstractBool = Union{AbstractSoftBool, Bool}

include("inf.jl")            # Infinity
include("kernels.jl")        # Kernels
include("kernelctx.jl")      # Kernels context
include("softbool.jl")       # Soft Boolean
include("dualsoftbool.jl")   # Dual Soft Boolean
include("distances.jl")      # Standard Distance Functions
include("recur.jl")          # Recursive
include("any.jl")            # any, all

# Using Dual Soft Bools
softeq(x, y) = dsofteq(x, y)
softgt(x, y) = dsoftgt(x, y)
softlt(x, y) = dsoftlt(x, y)

const softtrue = dsofttrue
const softfalse = dsoftfalse

# Using Not Dual Soft Bools
# const softeq = ssofteq
# const softgt = ssoftgt
# const softlt = ssoftlt

# const softtrue = ssofttrue
# const softfalse = ssoftfalse

const >ₛ = softgt
const >=ₛ = softgt
const <=ₛ = softlt
const <ₛ = softlt
const ==ₛ = softeq

end


text:
export fast_mul, fast_mul2, optimize, optimize_coeffs#, solve_nonlinear

# Function to speed-up ψ_diag*Q1'*Q1
# fast_mul(ψbis::Array{Float64,2}, Q, N::Int64, nx::Int64) = ([([ψbis zeros(size(ψbis,1), nx)] * Q)[axes(ψbis,1), 1:nx] zeros(size(ψbis,1), N)] * Q')[axes(ψbis,1), 1:N]

# Function to speed-up Q1*Q1'*ψ_diag
fast_mul(ψ_diag::Array{Float64,2}, Q, N::Int64, n_off::Int64) = (Q*[(Q'*[ψ_diag; zeros(n_off, size(ψ_diag,2))])[1:n_off, axes(ψ_diag,2)]; zeros(N, size(ψ_diag,2))])[1:N, axes(ψ_diag,2)]

# Version without the L2 regularization term, we assuem that N (number of samples) > n_off (number of off-diagonal features)
fast_mul2(ψ_diag::Array{Float64,2}, Q, N::Int64, n_off::Int64) = Q*[(Q'*ψ_diag)[1:n_off, axes(ψ_diag,2)]; zeros(N-n_off, size(ψ_diag,2))]

# Code to identify the coefficients
function optimize(C::RadialMapComponent, W::Weights, λ, δ)
	# C and W can have different dimension
	@assert C.p == W.p "Mismatch order p of the map"
	Nx = C.Nx
	@get W (p, Ne)
	# Compute weights
    ψ_off, ψ_diag, dψ_diag = rearrange(W,Nx)

    no = size(ψ_off,2)
	nd = size(ψ_diag,2)
    nx = size(ψ_off,2)+size(ψ_diag,2)+1
    nlog = size(dψ_diag,2)

	@assert nd==nlog "Error size of the diag and ∂Nx weights"
	@assert nx==no+nlog+1 "Error size of the weights"

	# Cache for the solution
	A = zeros(nd,nd)
	x = zeros(nx)

    # Normalize diagtone basis functions
    μψ = copy(mean(ψ_diag, dims=1)[1,:])
    σψ = copy(std(ψ_diag, dims=1, corrected=false)[1,:])

    ψ_diag .-= μψ'
    ψ_diag ./= σψ'
    dψ_diag ./= σψ'

    if Nx==1
		BLAS.gemm!('T', 'N', 1/Ne, ψ_diag, ψ_diag, 1.0, A)
		# A .= BLAS.gemm('N', 'T', 1/Ne, ψ_diag, ψ_diag)
    else
        #Normalize off-diagonal covariates
        μψ_off = copy(mean(ψ_off, dims = 1)[1,:])
        σψ_off = copy(std(ψ_off, dims = 1, corrected = false)[1,:])
        ψ_off .-= μψ_off'
        ψ_off ./= σψ_off'

		#Assemble reduced QR to solve least square problem
		ψ_aug = zeros(Ne+no,no)
		ψ_X = view(ψ_aug,1:Ne,1:no)
		ψ_X .= ψ_off

		# ψ_λ = view(ψ_aug,Ne+1:Ne+no,1:no)
		@inbounds for i=1:no
			ψ_aug[Ne+i,i] = √λ
		end

	    F = qr(ψ_aug)
		# Speed-up proposed in https://discourse.julialang.org/t/extract-submatrix-from-qr-factor-is-slow-julia-1-4/36582/4
		# Q1 = Matrix(F.Q)[1:Ne,:]
		# Asqrt .= ψ_diag - Q1*(Q1'*ψ_diag)
		Asqrt = zero(ψ_diag)
		Asqrt .= ψ_diag - fast_mul(ψ_diag, F.Q, Ne, no)
		A .= (1/Ne)*Asqrt'*Asqrt
	end

    #Assemble reduced QR to solve least square problem
    #Approximate diagonal component
	# for linear S_{Nx}^{2} (i.e., order 0 function), use closed
	# form solution for linear diagtone component
	if p == 0
        @assert size(A)==(1,1) "Quadratic matrix should be a scalar."
		tmp_diag = zeros(1)
		# This equation is equation (A.9) Couplings for nonlinear ensemble filtering
		# uNx(z) = c + α z so α = 1/√κ*
        tmp_diag[1] = sqrt(1/A[1,1])

	# for nonlinear diagonal, use Newton solver for coefficients
	else
		tmp_diag = ones(nd)
		# Construct loss function, the convention is flipped inside the solver
		lhd = LHD(A, dψ_diag, λ, δ)
		tmp_diag,_,_,_ = projected_newton(tmp_diag, lhd, "TrueHessian")
		tmp_diag .+= δ
	end

	if Nx==1
		tmp_diag ./= σψ
		x[no+1] = -dot(μψ,tmp_diag)
	else
		tmp_off = zeros(no)
		padded_diag = zeros(Ne+no)
		view(padded_diag,1:Ne) .= ψ_diag*tmp_diag
		tmp_off.= -F.R\((F.Q'*(padded_diag))[1:no])

		# Rescale coefficients
		tmp_diag ./= σψ

		tmp_off ./= σψ_off
		# Compute and store the constant term (expectation of selected terms)
		x[no+1] = -dot(μψ, tmp_diag)-dot(μψ_off, tmp_off)
	end

	# Coefficients of the diagonal except the constant
	x[no+2:nx] .= tmp_diag

	if Nx>1
		x[1:no] .= tmp_off
	end
	modifycoeff!(C, x)
	return C

end

function optimize(S::RadialMap, X; apply_rescaling::Bool=true, start::Int64=1, P::Parallel=serial)
	NxX, Ne = size(X)
	@get S (Nx, p, γ, λ, δ, κ)
	@assert NxX == Nx "Wrong dimension of the ensemble matrix X"

	# We can apply the rescaling to all the components once
	if apply_rescaling == true
		transform!(S.L, X)
	end

	# Compute centers and widths
	center_std!(S, X)

	# Create weights
	W = create_weights(S, X)

	# Compute weights
	compute_weights(S, X, W)

	# Optimize coefficients with multi-threading
	if typeof(P)==Serial
		@inbounds for i=start:Nx
				S.C[i] = optimize(S.C[i], W, λ, δ)
		end
	else
		@inbounds Threads.@threads for i=start:Nx
				S.C[i] = optimize(S.C[i], W, λ, δ)
		end
	end

	# We can apply the rescaling to all the components once
	if apply_rescaling == true
		itransform!(S.L, X)
	end
end

## Optimize for the weights with SparseRadialMapComponentMap

# Code to identify the coefficients

function optimize(C::SparseRadialMapComponent, X, maxfamilies::Nothing, λ, δ)
	NxX, Ne = size(X)
	@get C (Nx,p)

	@assert NxX == Nx "Wrong dimension of the ensemble matrix X"

	# Compute weights
    ψ_off, ψ_diag, dψ_diag = compute_weights(C, X)

    no = size(ψ_off,2)
	nd = size(ψ_diag,2)
    nx = size(ψ_off,2)+size(ψ_diag,2)+1
    nlog = size(dψ_diag,2)

	@assert nd==nlog "Error size of the diag and ∂Nx weights"
	@assert nx==no+nlog+1 "Error size of the weights"

	# Cache for the solution
	A = zeros(nd,nd)
	x = zeros(nx)

    # Normalize diagtone basis functions
	μψ = copy(mean(ψ_diag, dims=1)[1,:])
    σψ = copy(std(ψ_diag, dims=1, corrected=false)[1,:])

	ψ_diag .-= μψ'
    ψ_diag ./= σψ'
    dψ_diag ./= σψ'

	# @show ψ_diag[2,:]
	# @show dψ_diag[2,:]

    if Nx==1 || no == 0
		BLAS.gemm!('T', 'N', 1/Ne, ψ_diag, ψ_diag, 1.0, A)
		# A .= BLAS.gemm('N', 'T', 1/Ne, ψ_diag, ψ_diag)
    else
        #Normalize off-diagonal covariates
        μψ_off = copy(mean(ψ_off, dims = 1)[1,:])
        σψ_off = copy(std(ψ_off, dims = 1, corrected = false)[1,:])
        ψ_off .-= μψ_off'
        ψ_off ./= σψ_off'

		#Assemble reduced QR to solve least square problem

		ψ_aug = zeros(Ne+no,no)
		ψ_X = view(ψ_aug,1:Ne,1:no)
		ψ_X .= ψ_off

		@inbounds for i=1:no
			ψ_aug[Ne+i,i] = √λ
		end

		F = qr(ψ_aug)
		# Speed-up proposed in https://discourse.julialang.org/t/extract-submatrix-from-qr-factor-is-slow-julia-1-4/36582/4
		# Q1 = Matrix(F.Q)[1:Ne,:]
		# Asqrt .= ψ_diag - Q1*(Q1'*ψ_diag)
		Asqrt = zero(ψ_diag)
		Asqrt .= ψ_diag - fast_mul(ψ_diag, F.Q, Ne, no)
		A .= (1/Ne)*Asqrt'*Asqrt
	end

    #Assemble reduced QR to solve least square problem
    #Approximate diagonal component
	# for linear S_{Nx}^{2} (i.e., order 0 function), use closed
	# form solution for linear diagtone component
	if p[Nx] == 0
        @assert size(A)==(1,1) "Quadratic matrix should be a scalar."
		tmp_diag = zeros(1)
		# This equation is equation (A.9) Couplings for nonlinear ensemble filtering
		# uNx(z) = c + α z so α = 1/√κ*
        tmp_diag[1] = sqrt(1/A[1,1])
	# for nonlinear diagonal, use Newton solver for coefficients
	else
		tmp_diag = ones(nd)
		# Construct loss function, the convention is flipped inside the solver
		lhd = LHD(A, dψ_diag, λ, δ)

		tmp_diag,_,_,_ = projected_newton(tmp_diag, lhd, "TrueHessian")
		tmp_diag .+= δ
	end

	if Nx == 1 || no == 0
		tmp_diag ./= σψ
		x[no+1] = -dot(μψ,tmp_diag)
	else
		tmp_off = zeros(no)
		padded_diag = zeros(Ne+no)
		view(padded_diag,1:Ne) .= ψ_diag*tmp_diag
		tmp_off.= -F.R\((F.Q'*(padded_diag))[1:no])
	    # Rescale coefficients
	    tmp_diag ./= σψ

	    tmp_off ./= σψ_off
	    # Compute and store the constant term (expectation of selected terms)
		x[no+1] = -dot(μψ, tmp_diag)-dot(μψ_off, tmp_off)
	end

	# Coefficients of the diagonal except the constant
	x[no+2:nx] .= tmp_diag

	if Nx>1 && no != 0
		x[1:no] .= tmp_off
	end

	modifycoeff!(C, x)
	error = negative_likelihood(C, X, λ, δ)
	return C, error
end

function optimize(S::SparseRadialMap, X, maxfamilies::Nothing; apply_rescaling::Bool=true, start::Int64=1, P::Parallel=serial)
	NxX, Ne = size(X)
	@get S (Nx, p, γ, λ, δ, κ)

	@assert NxX == Nx "Wrong dimension of the ensemble matrix X"

	# We can apply the rescaling to all the components once
	if apply_rescaling == true
		transform!(S.L, X)
	end

	# Compute centers and widths
	center_std!(S, X)

	# Optimize coefficients
	# Skip the identity components of the map
	if typeof(P)==Serial
		@inbounds for i=start:Nx
			if !allequal(p[i], -1)
					S.C[i], _ = optimize(S.C[i], X[1:i,:], maxfamilies, λ, δ)
			end
		end
	else
		@inbounds Threads.@threads for i=start:Nx
			if !allequal(p[i], -1)
					S.C[i], _ = optimize(S.C[i], X[1:i,:], maxfamilies, λ, δ)
			end
		end
	end

	# We can apply the rescaling to all the components once
	if apply_rescaling == true
		itransform!(S.L, X)
	end

end

# function solve_nonlinear(dψ::Array{Float64,2}, A::Array{Float64,2}, λ, δ)
#
# 	n∂, Ne = size(dψ)
#
# 	# Create the model
# 	model = Model(Ipopt.Optimizer)
#
# 	# Create variable x
# 	@variable(model, x[1:n∂])#, start = 0.0)
#
# 	@NLparameter(model, b[i = 1:n∂] == δ*sum(A[i,j] for j=1:n∂))
#
# 	@NLparameter(model, Ã[i = 1:n∂, j = 1:n∂] == A[i,j]+(λ/Ne)*I[i,j])
#
# 	@NLparameter(model, dψopt[i = 1:n∂, j = 1:Ne] == dψ[i,j] + δ*sum(dψ[i,j] for j=1:Ne))
#
#
# 	# Ã is a symmetric matrix
# 	# Set lower bound on x
# 	@inbounds for i in 1:n∂
# 	    set_lower_bound(x[i], 0.0)
# 	end
#
# 	@NLobjective(model, Min, 0.5*sum(sum(x[i]*Ã[i,j]*x[j] for i=1:n∂) for j=1:n∂)-
# 	                  sum(log(sum(dψopt[i,j]*x[i] for i=1:n∂)) for j=1:Ne)*(1/Ne)+
# 	                  sum(x[i]*b[i] for i=1:n∂))
#
# 	optimize!(model)
#
# 	return value.(x)
# end
#
#
#
#
# # Code to identify the coefficients
# function optimize_coeffs(C::RadialMapComponent{Nx,p}, W::Weights{n, p, Ne}) where {Nx,p,n,Ne}
#     # Compute weights
#     wq, w∂ = rearrange_weights(W, Nx)
#
#     nx = size(wq,1)
#     nlog = size(w∂,1)
#
#
#     # Create the model
#     model = Model(Ipopt.Optimizer)
#
#     # Create variable x
#     @variable(model, x[1:nx])#, start = 0.001)
#
#     @NLparameter(model, z[i = 1:nx, j = 1:Ne] == wq[i,j])
#     @NLparameter(model, b[i = 1:nlog, j = 1:Ne] == w∂[i,j])
#
#
#     # Set lower bound on the coefficients of the derivatives
#     @inbounds for i in nx-nlog+1:nx
#         set_lower_bound(x[i], 0.0)
#     end
#
#     if p==0
#     @NLobjective(model, Min, (1/Ne)*sum(0.5*sum(z[j,i]*x[j] for j=1:nx)^2 -(w∂[1,i]*x[end]) for i=1:Ne))
#     else
#     @NLobjective(model, Min, (1/Ne)*sum(0.5*sum(z[j,i]*x[j] for j=1:nx)^2 -
#                 log(sum(w∂[j,i]*x[end-(p+2)+j] for j=1:p+2)) for i=1:Ne))
#     end
#     optimize!(model)
#
#     # Now replace identifed coefficients into a
#     modifycoeff!(C, xopt)
# end
#
#
#
#
#

# function _cost(x, wquad, w∂,p)
#     J = 0.0
#     nx = length(x)
#     Ne = size(wquad,2)
#     @inbounds for i=1:Ne
#         J += 0.5*sum(wquad[j,i]*x[j] for j=1:nx)^2
#
#         if p==0
#         J += -log(w∂[1,i]*x[end])
#         else
#         J += -log(sum(w∂[j,i]*x[end-(p+2)+j] for j=1:p+2))
#         end
#     end
#     J *=  (1/Ne)
#     return J
# end

# This code is written for Nx>1
# function run_optimization_parallel(S::RadialMap, X; start::Int64=1)
# 	Nx, Ne = size(X)
# 	@get S (Nx, p, γ, λ, δ, κ)
# 	@assert Nx>1 "This code is not written for Nx=1"
# 	# Compute centers and widths
# 	center_std!(S, X)
# 	# Create weights
# 	W = create_weights(S, X)
# 	# Compute weights
# 	weights(S, X, W)
# 	# Optimize coefficients with Distributed
# 	# X = SharedArray{Float64}(Nx*(p+1)+2,Nx)
# 	# scoeffs = SharedArray{Int64}(Nx)
# 	X = SharedArray{Float64}(Nx*(p+1)+2,Nx-start+1)
# 	scoeffs = SharedArray{Int64}(Nx-start+1)
# 	@sync @distributed for i=start:Nx
# 		@inbounds begin
# 		xopt = optimize(S.C[i], W, λ, δ)
# 		scoeffs[i-start+1] = deepcopy(size(xopt,1))
# 	    X[1:size(xopt,1),i-start+1] .= deepcopy(xopt)
# 		end
# 	end
#
# 	# Run this part in serial
# 	@inbounds for i=start:Nx
# 		modifycoeff!(S.C[i], X[1:scoeffs[i-start+1],i-start+1])
# 	# @inbounds modifycoeff!(S.C[i], X[1:scoeffs[i],i])
#     end
# end


text:
# function wrappers for ../c/mine.h

function compute_score(prob::Ptr{Problem}, param::Ptr{Parameter})
    @fncall(:mine_compute_score, Ptr{Score}, (Ptr{Problem}, Ptr{Parameter}), prob, param)
end

function check_parameter(param::Ptr{Parameter})
    @fncall(:mine_check_parameter, Ptr{Cchar}, (Ptr{Parameter},), param)
end

function mch(pscore::Ptr{Score}, eps::Cdouble)
    @fncall(:mine_mch, Cdouble, (Ptr{Score}, Cdouble), pscore, eps)
end

function mic(score::Ptr{Score})
    ccall((:mine_mic, "/haplox/users/guo/Github/MINE/src/c/libmine.so"), Cdouble, (Ptr{Score},), score)
end

#=
for :func in (:mic,:mas,:mev,:mch_general)
    @eval quote
        function func(score::Ptr{Score})
            @fncall(Symbol("mine_", $func), Cdouble, (Ptr{Score},), score)
        end
    end
end
=#

#=
for func in (:mic, :mas, :mev, :mch_general)
    @eval begin
        function $func(score::Ptr{Score})
            ccall((Symbol("mine_", $func), $libmine), Cdouble, (Ptr{Score},), score)
        end
    end
end
=#

function free_score(score::Ptr{Score})
    @fncall(:mine_free_score, Cdouble, (Ptr{Score},), score)
end