Skip to content

Instantly share code, notes, and snippets.

@geogradient
Last active August 29, 2015 14:04
Show Gist options
  • Save geogradient/3b3b231b7fad09011093 to your computer and use it in GitHub Desktop.
Save geogradient/3b3b231b7fad09011093 to your computer and use it in GitHub Desktop.
get the number of images that per week per year loading from a json file in order to tabulate per week and year later on
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "[José M. Beltrán](<beltran.data@gmail.com>)"
__credits__ = ["José M. Beltrán"]
__license__ = "GPL-3.0"
"""
The json file has the following scheme:
{"20020614":{"KEY1":{"KEY1.1":"path_to_file_01", ...},
:{"KEY2":{"KEY2.1":"path_to_file_01", ...}}}
...,
"20120414": {"KEY1":{"KEY1.1":"path_to_file_01", ...},
:{"KEY2":{"KEY2.1":"path_to_file_01", ...}}}
}
Basically I have a time series satellite images.
Only one dataset per given date. But I want to tabulate
the number of images that I have per week per year.
So I created a json file that holds the filepaths of
each image available.
"""
import json
json_file = "filepathlist_per_date.json"
# Loading data
with open(json_file, 'rb') as fp:
filepaths_dict = json.load(fp)
# Create a dictonary that holds the number of images per week per year
# My time series spans from 2002 till 2012 inclusive, so I need to add
# 2012 + 1 to include the year 2012 in my dictionary.
week_per_year_counter = {k: {} for k in range(2002, 2012 + 1)}
for date_string in l2p_filepaths_dict.keys():
# Get the week number using the function date_to_week_number (available here:
# https://gist.github.com/geogradient/6210d5e85d7867b5f9e6#file-date_to_week_number-py
week_number = date_to_week_number(date_string)
current_year = int(date_string[:4])
if week_number not in week_per_year_counter[current_year].keys():
# datasets_per_week_per_year_counter
week_per_year_counter[cyear][week_number] = 1
else:
week_per_year_counter[cyear][week_number] = week_per_year_counter[cyear][week_number] + 1
# Can also be used to group the files by week and year based on the date keys
year_dict = {k: {} for k in range(2002, 2012 + 1)}
for date_string in l2p_filepaths_dict.keys():
week_number = date_to_week_number(date_string)
current_year = int(date_string[:4])
if week_number not in year_dict[current_year].keys():
# Create a list to hold the date_string keys for the given keys in the year_dict
year_dict[current_year][week_number] = [date_string]
else:
# Note that the list must be extended not appended!
year_dict[current_year][week_number].extend([date_string])
year_dict[current_year][week_number].sort()
# using len(year_dict[current_year][week_number]) is possible to get the number of files
# per year and week_number, and then tabulate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment