sync_figures.py 3.38 KB
Newer Older
1
2
3
4
__author__ = 'abuddenberg'

from webform_client import WebformClient
from gcis_client import GcisClient
5
from os.path import exists
6
import json
7
8
9
10
11
12

# webform_dev = ('http://dev.nemac.org/asides10/metadata/figures/all?token=A2PNYxRuG9')

webform = WebformClient('http://resources.assessment.globalchange.gov', 'mgTD63FAjG')

gcis_url = 'http://data.gcis-dev-front.joss.ucar.edu'
13
14
gcis = GcisClient(gcis_url, 'andrew.buddenberg@noaa.gov', '4cd31dc7173eb47b26f616fb07db607f25ab861552e81195')
# stage = GcisClient('http://data-stage.globalchange.gov', 'andrew.buddenberg@noaa.gov', 'ef427a895acf26d4f0b1f053ba7d922791b76f7852e7efee')
15

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
sync_metadata_tree = {
    #Reports
    'nca3draft': {
        #Chapters
        'our-changing-climate': [
            #(webform_url, gcis_id)
            # ('/metadata/figures/2506', 'observed-change-in-very-heavy-precipitation'),
            # ('/metadata/figures/2997', 'observed-change-in-very-heavy-precipitation-2'),
            # ('/metadata/figures/2677', 'observed-us-precipitation-change'),
            # ('/metadata/figures/3175', 'observed-us-temperature-change'),
            # ('/metadata/figures/3074', 'ten-indicators-of-a-warming-world'),
            # ('/metadata/figures/3170', 'global-temperature-and-carbon-dioxide'),
            ('/metadata/figures/3293', 'observed-increase-in-frostfree-season-length'),
            # ('/metadata/figures/3294', 'projected-changes-in-frostfree-season-length'),
            # ('/metadata/figures/3305', 'variation-of-storm-frequency-and-intensity-during-the-cold-season-november--march') #incomplete

        ]
    }
}

#These are artifacts from our collection efforts; largely duplicates
webform_skip_list = []
38
39


40
41
42
43
def main():
    print_webform_list()
    # sync_images()
    # sync_metadata()
44
45


46
47


48
49
50
51
52
def print_webform_list():
    for item in webform.get_list():
        webform_url = item['url']
        print webform_url
        print webform.get_webform(webform_url)
53
54


55
56
57
58
59
def sync_metadata():
    for report in ['nca3draft']:
        for chapter in ['our-changing-climate']:
            for figure_ids in sync_metadata_tree[report][chapter]:
                webform_url, gcis_id = figure_ids
60

61
62
63
64
65
66
                #Merge data from both systems into one object...
                figure_obj = webform.get_webform(webform_url).merge(
                    gcis.get_figure(report, gcis_id, chapter_id=chapter)
                )
                #...then send it.
                gcis.update_figure(report, chapter, figure_obj)
67
68


69
70
71
72
73
def sync_images():
    for report in ['nca3draft']:
        for chapter in ['our-changing-climate']:
            for figure_ids in sync_metadata_tree[report][chapter]:
                webform_url, gcis_id = figure_ids
74

75
76
77
                figure = webform.get_webform(webform_url)
                #Now identifiers don't need to be matched
                figure.identifier = gcis_id
78

79
80
                webform.download_all_images(figure)
                upload_images_to_gcis(figure)
81
82


83
84
85
86
87
88
89
90
def upload_images_to_gcis(figure, report_id='nca3draft'):
    #Make sure we have all the images required for a COMPLETE update
    for image in figure.images:
        if not exists(image.local_path):
            raise Exception('Local file missing ' + image.local_path)
    for image in figure.images:
        for resp in gcis.create_image(image, report_id=report_id, figure_id=figure.identifier):
            print resp.status_code, resp.text
91
92


93
94
if __name__ == '__main__':
    main()