Skip to content

Commit

Permalink
clean vegetation_time_series
Browse files Browse the repository at this point in the history
  • Loading branch information
luisageo6 committed Apr 10, 2024
1 parent a661c67 commit 647b83a
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions geosyspy/services/vegetation_time_series_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pandas as pd
from datetime import datetime
from urllib.parse import urljoin
from geosyspy.utils.constants import *
from geosyspy.utils.http_client import *
from geosyspy.utils.constants import GeosysApiEndpoints
from geosyspy.utils.http_client import HttpClient


class VegetationTimeSeriesService:
Expand Down Expand Up @@ -61,9 +61,9 @@ def get_modis_time_series(self, season_field_id:str,


def get_time_series_by_pixel(self, season_field_id: str,
start_date: datetime,
end_date: datetime,
indicator: str) -> pd.DataFrame:
start_date: datetime,
end_date: datetime,
indicator: str) -> pd.DataFrame:
"""Returns a pandas DataFrame.
This method returns a time series of 'indicator' by pixel within the range 'start_date' -> 'end_date'
Expand Down Expand Up @@ -97,16 +97,27 @@ def get_time_series_by_pixel(self, season_field_id: str,
start_date: str = start_date.strftime("%Y-%m-%d")
end_date: str = end_date.strftime("%Y-%m-%d")
parameters: str = f"/values?$offset=0&$limit=None&$count=false&SeasonField.Id={season_field_id}&index={indicator}&$filter=Date >= '{start_date}' and Date <= '{end_date}'"
vts_url: str = urljoin(self.base_url, GeosysApiEndpoints.VTS_BY_PIXEL_ENDPOINT.value + parameters)
vts_url: str = urljoin(
self.base_url,
GeosysApiEndpoints.VTS_BY_PIXEL_ENDPOINT.value + parameters
)
response = self.http_client.get(vts_url)

if response.status_code == 200:
return self._extracted_from_get_time_series_by_pixel_50(response)
return self.extract_pixel(response)
else:
self.logger.info(response.status_code)

# TODO Rename this here and in `get_time_series_by_pixel`
def _extracted_from_get_time_series_by_pixel_50(self, response):
def extract_pixel(self, response):
"""
Extracts h, v, i, and j coordinates from the pixel dataframe.
Args:
response: The response object containing pixel data.
Returns:
DataFrame: A DataFrame with index, value, pixel.id, X, and Y columns.
"""
df = pd.json_normalize(response.json())
df.set_index("date", inplace=True)

Expand All @@ -132,4 +143,4 @@ def _extracted_from_get_time_series_by_pixel_50(self, response):
df["X"] = df["i"] * PSX + df["XUL"]
df["Y"] = df["j"] * PSY + df["YUL"]
self.logger.info("Done ! ")
return df[["index", "value", "pixel.id", "X", "Y"]]
return df[["index", "value", "pixel.id", "X", "Y"]]

0 comments on commit 647b83a

Please sign in to comment.