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

4
from gcis_clients import WebformClient, GcisClient, gcis_dev_auth, gcis_stage_auth, webform_token
5
from gcis_clients.sync_utils import move_images_to_gcis, sync_dataset_metadata, realize_contributors
abuddenberg's avatar
abuddenberg committed
6
from collections import OrderedDict
7

8
webform = WebformClient('http://resources.assessment.globalchange.gov', webform_token)
9

10
11
# gcis = GcisClient('http://data.gcis-dev-front.joss.ucar.edu', *gcis_dev_auth)
gcis = GcisClient('http://data-stage.globalchange.gov', *gcis_stage_auth)
12

13
14
sync_metadata_tree = {
    #Reports
abuddenberg's avatar
abuddenberg committed
15
    'nca3': OrderedDict([
16
        #Chapter 2
abuddenberg's avatar
abuddenberg committed
17
18
19
20
21
        ('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
22
            ('/metadata/figures/2677', 'observed-us-precipitation-change'),
abuddenberg's avatar
abuddenberg committed
23
            ('/metadata/figures/3298', 'observed-us-trend-in-heavy-precipitation'),
24
25
26
27
28
29
30
31
            ('/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
32
33
34
35
36
37
38
39
40

        ]),
        #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'),
41
            ('/metadata/figures/2952', 'role-of-adaptive-strategies-and-tactics-in-reducing-impacts-and-consequences'),
abuddenberg's avatar
abuddenberg committed
42
43
44
45
            ('/metadata/figures/3169', 'tropical-storm-impact-on-vermont-road'),
        ]),
        #Chapter 6
        ('agriculture', [
46
            ('/metadata/figures/2691', 'projected-changes-in-key-climate-variables-affecting-agricultural-productivity'),
abuddenberg's avatar
abuddenberg committed
47
48
49
50
51
            ('/metadata/figures/2872', 'drainage-tiles'),
        ]),
        #Chapter 7
        ('forests', [
            ('/metadata/figures/2977', 'effectiveness-of-forest-management-in-reducing-wildfire-risk'),
abuddenberg's avatar
abuddenberg committed
52
            ('/metadata/figures/2978', 'forest-vulnerability-to-changing-climate'),
abuddenberg's avatar
abuddenberg committed
53
54
55
56
57
58
59
60
61
62
            ('/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', [
63
            ('/metadata/figures/2896', 'heavy-downpours-disease')
abuddenberg's avatar
abuddenberg committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        ]),
        #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', [
81
            ('/metadata/figures/3306', 'growing-season-lengthens')
abuddenberg's avatar
abuddenberg committed
82
83
84
85
86
87
88
89
90
        ]),
        # 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'),
91
            ('/metadata/figures/2846', 'storm-surge-barrier')
abuddenberg's avatar
abuddenberg committed
92
93
94
95
96
97
98
99
100
        ]),
        #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', [
101
            ('/metadata/figures/2550', 'temperatures-are-rising-in-the-midwest'),
abuddenberg's avatar
abuddenberg committed
102
103
104
105
106
            ('/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
107
            ('/metadata/figures/2697', 'temperature-and-precipitation-distribution-in-the-great-plains'),
abuddenberg's avatar
abuddenberg committed
108
            ('/metadata/figures/2989', 'projected-change-in-number-of-hot-days'),
109
            ('/metadata/figures/2990', 'projected-change-in-number-of-warm-nights'),
abuddenberg's avatar
abuddenberg committed
110
111
112
113
114
115
116
            ('/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')
        ]),
117
        #Chapter 33: Climate Science Appendix
abuddenberg's avatar
abuddenberg committed
118
        ('appendix-climate-science', [
abuddenberg's avatar
abuddenberg committed
119
            ('/metadata/figures/3301', 'human-influences-apparent-in-many-aspects-of-the-changing-climate'),
120
            ('/metadata/figures/2798', 'detection-and-attribution-as-forensics'),
121
            # ('/metadata/figures/2799', 'human-influences-apparent-in-many-climate-variables'), #Dupe, not used
122
123
124
125
126
127
128
129
130
            ('/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
131
132
133
            # ('/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'),
134
            ('/metadata/figures/3134', 'temperature-trends-1900-2012'),
abuddenberg's avatar
abuddenberg committed
135
136
        ])
    ])
137
138
}

139

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

145

146
def sync(replace=False):
147
148
149
    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]:
150
                webform_url, gcis_id = figure_ids
151

152
153
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
                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!'
179

180
if __name__ == '__main__':
181
    main()