Skip to content

Commit

Permalink
Update Documentation & Fix location handling
Browse files Browse the repository at this point in the history
Adding example environment variable config.

Signed-off-by: Gnanakeethan Balasubramaniam <[email protected]>
  • Loading branch information
gnanakeethan committed Sep 24, 2023
1 parent fa55860 commit 0ec0776
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
28 changes: 19 additions & 9 deletions docs/implementations/watt-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ WattTime technology—based on real-time grid data, cutting-edge algorithms, and
WattTime Model provides a way to calculate emissions for a given time in a specific location.

The model is based on the WattTime API. The model uses the following inputs:
* latitude: Location of the software system (latitude in decimal degrees).
* longitude: Location of the software system (longitude in decimal degrees).
* location: Location of the software system (latitude in decimal degrees, longitude in decimal degrees). "latitude,longitude"
* timestamp: Timestamp of the recorded event (2021-01-01T00:00:00Z) RFC3339
* duration: Duration of the recorded event in seconds (3600)

Expand All @@ -26,13 +25,27 @@ Limitations:


WattTime API requires activation of subscription before usage. Please refer to WattTime website for more information.
https://www.watttime.org/get-the-data/data-plans/

**Required Parameters:**

```
# example environment variable config , prefix the environment variables with "ENV" to load them inside the model.
# export WATT_TIME_USERNAME=test1
# export WATT_TIME_PASSWORD=test2
```
* username: Username for the WattTime API
* ENV_WATT_TIME_USERNAME - specifying this value enables the RIMPL to load the value from the environment variable
* password: Password for the WattTime API
* ENV_WATT_TIME_PASSWORD - specifying this value enables the RIMPL to load the value from the environment variable


### Observations

**Required Parameters:**
* timestamp: Timestamp of the recorded event (2021-01-01T00:00:00Z) RFC3339
* location: Location of the software system (latitude in decimal degrees, longitude in decimal degrees). "latitude,longitude"
* duration: Duration of the recorded event in seconds (3600)

### Typescript Usage
```typescript
// environment variable configuration
Expand All @@ -46,8 +59,7 @@ const env_model = await new WattTimeGridEmissions().configure('watt-time', {
const observations = [
{
timestamp: '2021-01-01T00:00:00Z',
latitude: 43.22,
longitude: -80.22,
latitude: "43.22,-80.22",
duration: 3600,
},
];
Expand All @@ -65,8 +77,7 @@ config:
password: ENV_WATT_TIME_PASSWORD
observations:
- timestamp: 2021-01-01T00:00:00Z
latitude: 43.22
longitude: -80.22
latitude: "43.22,-80.22"
duration: 3600
```
#### Static configuration for IMPL
Expand All @@ -76,7 +87,6 @@ config:
password: password
observations:
- timestamp: 2021-01-01T00:00:00Z
latitude: 43.22
longitude: -80.22
latitude: "43.22,-80.22"
duration: 3600
```
32 changes: 28 additions & 4 deletions src/lib/watt-time/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ export class WattTimeGridEmissions implements IImpactModelInterface {

private validateObservations(observations: object[]) {
observations.forEach((observation: KeyValuePair) => {
if (!('latitude' in observation) || !('longitude' in observation)) {
throw new Error('latitude or longitude is missing');
if (!('location' in observation)) {
const {latitude, longitude} =
this.getLatitudeLongitudeFromObservation(observation);
if (isNaN(latitude) || isNaN(longitude)) {
throw new Error('latitude or longitude is not a number');
}
}
if (!('timestamp' in observation)) {
throw new Error('timestamp is missing');
Expand All @@ -148,6 +152,24 @@ export class WattTimeGridEmissions implements IImpactModelInterface {
});
}

private getLatitudeLongitudeFromObservation(observation: KeyValuePair) {
const location = observation['location'].split(','); //split location into latitude and longitude
if (location.length !== 2) {
throw new Error(
'location should be a comma separated string of latitude and longitude'
);
}
if (location[0] === '' || location[1] === '') {
throw new Error('latitude or longitude is missing');
}
if (location[0] === '0' || location[1] === '0') {
throw new Error('latitude or longitude is missing');
}
const latitude = parseFloat(location[0]); //convert latitude to float
const longitude = parseFloat(location[1]); //convert longitude to float
return {latitude, longitude};
}

private determineObservationStartEnd(observations: object[]) {
// largest possible start time
let starttime = dayjs('9999-12-31');
Expand Down Expand Up @@ -185,10 +207,12 @@ export class WattTimeGridEmissions implements IImpactModelInterface {
if (duration > 32 * 24 * 60 * 60) {
throw new Error('duration is too long');
}
const {latitude, longitude} =
this.getLatitudeLongitudeFromObservation(observation);

const params = {
latitude: observation.latitude,
longitude: observation.longitude,
latitude: latitude,
longitude: longitude,
starttime: dayjs(observation.timestamp).format('YYYY-MM-DDTHH:mm:ssZ'),
endtime: dayjs(observation.timestamp).add(duration, 'seconds'),
};
Expand Down

0 comments on commit 0ec0776

Please sign in to comment.