sync_figures 8.77 KB
Newer Older
1
#!/usr/bin/env python
2
3
__author__ = 'abuddenberg'

4
5
from gcis_clients import WebformClient
from gcis_clients import GcisClient
6
from gcis_clients.sync_utils import move_images_to_gcis, sync_dataset_metadata, realize_contributors
abuddenberg's avatar
abuddenberg committed
7
from collections import OrderedDict
8
import json
abuddenberg's avatar
abuddenberg committed
9
import pickle
10
11
12

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

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

16
17
sync_metadata_tree = {
    #Reports
abuddenberg's avatar
abuddenberg committed
18
    'nca3': OrderedDict([
19
        #Chapter 2
abuddenberg's avatar
abuddenberg committed
20
21
22
23
24
        ('our-changing-climate', [
            # (webform_url, gcis_id)
            ('/metadata/figures/3074', 'ten-indicators-of-a-warming-world'),
            ('/metadata/figures/3293', 'observed-increase-in-frostfree-season-length'),
            ('/metadata/figures/3294', 'projected-changes-in-frostfree-season-length'), #Good
25
            ('/metadata/figures/2677', 'observed-us-precipitation-change'),
abuddenberg's avatar
abuddenberg committed
26
            ('/metadata/figures/3298', 'observed-us-trend-in-heavy-precipitation'),
27
28
29
30
31
32
33
34
            ('/metadata/figures/2506', 'observed-change-in-very-heavy-precipitation'),
            ('/metadata/figures/2997', 'observed-change-in-very-heavy-precipitation-2'),
            ('/metadata/figures/3170', 'global-temperature-and-carbon-dioxide'),
            ('/metadata/figures/2939', 'projected-changes-in-soil-moisture-for-the-western-us'),
            ('/metadata/figures/3305', 'variation-of-storm-frequency-and-intensity-during-the-cold-season-november--march'),
            ('/metadata/figures/3067', 'ice-cover-in-the-great-lakes'),
            ('/metadata/figures/2523', 'shells-dissolve-in-acidified-ocean-water'),
            ('/metadata/figures/3175', 'observed-us-temperature-change'),
abuddenberg's avatar
abuddenberg committed
35
36
37
38
39
40
41
42
43

        ]),
        #Chapter 4
        ('energy-supply-and-use', [
            ('/metadata/figures/3292', 'cooling-degree-days')
        ]),
        #Chapter 5
        ('transportation', [
            ('/metadata/figures/3568', 'hurricane-sandy-causes-flooding-in-new-york-city-subway-stations'),
44
            ('/metadata/figures/2952', 'role-of-adaptive-strategies-and-tactics-in-reducing-impacts-and-consequences'),
abuddenberg's avatar
abuddenberg committed
45
46
47
48
            ('/metadata/figures/3169', 'tropical-storm-impact-on-vermont-road'),
        ]),
        #Chapter 6
        ('agriculture', [
49
            ('/metadata/figures/2691', 'projected-changes-in-key-climate-variables-affecting-agricultural-productivity'),
abuddenberg's avatar
abuddenberg committed
50
51
52
53
54
            ('/metadata/figures/2872', 'drainage-tiles'),
        ]),
        #Chapter 7
        ('forests', [
            ('/metadata/figures/2977', 'effectiveness-of-forest-management-in-reducing-wildfire-risk'),
abuddenberg's avatar
abuddenberg committed
55
            ('/metadata/figures/2978', 'forest-vulnerability-to-changing-climate'),
abuddenberg's avatar
abuddenberg committed
56
57
58
59
60
61
62
63
64
65
            ('/metadata/figures/2985', 'forests-can-be-a-source--or-a-sink--for-carbon')

        ]),
        #Chapter 8
        ('ecosystems', [
            ('/metadata/figures/2456', 'adaptation-planning-and-implementation-framework'),
            ('/metadata/figures/3574', 'biological-responses-to-climate-change')
        ]),
        #Chapter 9
        ('human-health', [
66
            ('/metadata/figures/2896', 'heavy-downpours-disease')
abuddenberg's avatar
abuddenberg committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        ]),
        #Chapter 10
        ('water-energy-land-use', [
            ('/metadata/figures/2410', 'coasttocoast-100degree-days-in-2011'),
            ('/metadata/figures/2986', 'hydraulic-fracturing-and-water-use'),
            ('/metadata/figures/2916', 'renewable-energy-and-land-use')
        ]),
        #Chapter 11
        ('urban-systems-infrastructure-vulnerability', [
            ('/metadata/figures/3569', 'urban-support-systems-are-interconnected')
        ]),
        #Chapter 12
        ('tribal-indigenous-native-lands-resources', [
            ('/metadata/figures/2911', 'arctic-marine-food-web')
        ]),
        #Chapter 14
        ('rural', [
84
            ('/metadata/figures/3306', 'growing-season-lengthens')
abuddenberg's avatar
abuddenberg committed
85
86
87
88
89
90
91
92
93
        ]),
        # Chapter 15
        ('biogeochemical-cycles', [
            ('/metadata/figures/2575', 'many-factors-combine-to-affect-biogeochemical-cycles')
        ]),
        #Chapter 16
        ('northeast', [
            ('/metadata/figures/2995', 'projected-increases-in-the-number-of-days-over-90f'),
            ('/metadata/figures/2844', 'coney-island-after-hurricane-irene'),
94
            ('/metadata/figures/2846', 'storm-surge-barrier')
abuddenberg's avatar
abuddenberg committed
95
96
97
98
99
100
101
102
103
        ]),
        #Chapter 17
        ('southeast', [
            ('/metadata/figures/2998', 'projected-change-in-number-of-days-over-95-f'),
            ('/metadata/figures/2999', 'projected-change-in-number-of-nights-below-32f'),
            ('/metadata/figures/2857', 'local-planning')
        ]),
        #Chapter 18
        ('midwest', [
104
            ('/metadata/figures/2550', 'temperatures-are-rising-in-the-midwest'),
abuddenberg's avatar
abuddenberg committed
105
106
107
108
109
            ('/metadata/figures/2992', 'projected-midcentury-temperature-changes-in-the-midwest'),
            ('/metadata/figures/2994', 'when-it-rains-it-pours'),
        ]),
        #Chapter 19
        ('great-plains', [
abuddenberg's avatar
abuddenberg committed
110
            ('/metadata/figures/2697', 'temperature-and-precipitation-distribution-in-the-great-plains'),
abuddenberg's avatar
abuddenberg committed
111
            ('/metadata/figures/2989', 'projected-change-in-number-of-hot-days'),
112
            ('/metadata/figures/2990', 'projected-change-in-number-of-warm-nights'),
abuddenberg's avatar
abuddenberg committed
113
114
115
116
117
118
119
            ('/metadata/figures/2991', 'projected-change-in-number-of-heavy-precipitation-days'),
            ('/metadata/figures/3002', 'projected-change-in-number-of-consecutive-dry-days'),
        ]),
        #Chapter 25
        ('coastal-zone', [
            ('/metadata/figures/2543', 'coastal-ecosystem-services')
        ]),
120
        #Chapter 33: Climate Science Appendix
abuddenberg's avatar
abuddenberg committed
121
        ('appendix-climate-science', [
abuddenberg's avatar
abuddenberg committed
122
            ('/metadata/figures/3301', 'human-influences-apparent-in-many-aspects-of-the-changing-climate'),
123
            ('/metadata/figures/2798', 'detection-and-attribution-as-forensics'),
124
            # ('/metadata/figures/2799', 'human-influences-apparent-in-many-climate-variables'), #Dupe, not used
125
126
127
128
129
130
131
132
133
            ('/metadata/figures/2802', 'modeling-the-climate-system'),
            ('/metadata/figures/2803', 'increasing-model-resolution'),
            ('/metadata/figures/2533', 'us-seasonal-temperatures'),
            ('/metadata/figures/2534', 'app-extreme-precipitation'),
            ('/metadata/figures/3147', 'ice-loss-from-greenland-and-antarctica'),
        ]),
        #Chapter 34: FAQ/CAQ
        ('appendix-climate-science-faqs', [
            ('/metadata/figures/2663', 'us-annual-temperature'),
abuddenberg's avatar
abuddenberg committed
134
135
136
            # ('/metadata/figures/3300', 'human-influences-apparent-in-many-aspects-of-the-changing-climate'),#Manually associate existing figure
            # ('/metadata/figures/3075', 'ten-indicators-of-a-warming-world'), #Manually associate existing figure
            ('/metadata/figures/2520', 'ocean-acidification-and-the-food-web'),
137
            ('/metadata/figures/3134', 'temperature-trends-1900-2012'),
abuddenberg's avatar
abuddenberg committed
138
139
        ])
    ])
140
141
}

142

143
def main():
144
145
146
    # print gcis.test_login()
    # sync_dataset_metadata(gcis, webform.get_aggregated_datasets(), skip=['Proxy Data', 'Projected Sea Level Rise', 'Tide Gauge Data'])
    sync(replace=False)
abuddenberg's avatar
abuddenberg committed
147

148

149
def sync(replace=False):
150
151
152
    for report_id in sync_metadata_tree:
        for chapter_id in sync_metadata_tree[report_id]:
            for figure_ids in sync_metadata_tree[report_id][chapter_id]:
153
                webform_url, gcis_id = figure_ids
154

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                print 'Attempting to sync: {id}'.format(id=gcis_id), webform_url

                #Merge data from both systems into one object...
                gcis_fig = gcis.get_figure(report_id, gcis_id, chapter_id=chapter_id)
                figure_obj = webform.get_webform(webform_url).merge(gcis_fig)

                #Lookup and populate contributor information for figures and images
                realize_contributors(gcis, figure_obj.contributors)
                for i in figure_obj.images:
                    realize_contributors(gcis, i.contributors)

                #Hack: override webform's title in favor of gcis
                figure_obj.title = gcis_fig.title if gcis_fig.title not in (None, '') else figure_obj.title

                if replace:
                    for image in figure_obj.images:
                        #TODO: There are better ways to do this. Build File support.
                        print 'Deleting {img}'.format(img=image.identifier)
                        gcis.delete_image(image)

                    print 'Attempting to upload: {id}'.format(id=gcis_id)
                    move_images_to_gcis(webform, gcis, webform_url, gcis_id, report_id)

                #...then send it.
                gcis.update_figure(report_id, chapter_id, figure_obj)

                print 'Success!'
182

183
if __name__ == '__main__':
184
    main()