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

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

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

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

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

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

143

144
def main():
145
#    print gcis.test_login()
abuddenberg's avatar
abuddenberg committed
146
    # sync_dataset_metadata(aggregate_webform_datasets())
abuddenberg's avatar
abuddenberg committed
147
    sync(replace=True)
abuddenberg's avatar
abuddenberg committed
148

149
150
151
152
153
154
155
156
157

#    ids = ['2799']
#    chapter = 'appendix-climate-science'
#
#    for id in ids:
#        print id
#        f = webform.get_webform('/metadata/figures/' + id, download_images=True)
#        print 'Dev: ', gcis.create_figure('nca3', chapter, f)
#        print 'Stage: ', stage.create_figure('nca3', chapter, f)
158
159


160
def sync(replace=False):
161
162
163
    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]:
164
                webform_url, gcis_id = figure_ids
165

166
167
168
169
170
171
172
173
174
175
176
177
178
179
                print 'Attempting to sync: {id}'.format(id=gcis_id)

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

                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)
abuddenberg's avatar
abuddenberg committed
180
                    move_images_to_gcis(webform, gcis, webform_url, gcis_id, report_id)
181

182
183
184
185
                #...then send it.
                gcis.update_figure(report_id, chapter_id, figure_obj)

                print 'Success!'
186

187
if __name__ == '__main__':
188
    main()