-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplottimebylat.py
832 lines (701 loc) · 28.4 KB
/
plottimebylat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
""" plottimebylat.py
4/16/2014: plot time (or N) by latitude, showing when significance emergences
Used canam4sims_stats2.py as a template
"""
#import sys as sys
#sys.path.insert(0,'./classes/')
import copy
#import numpy as np
import numpy.ma as ma
#import scipy as sp # scientific python
import scipy.stats
import matplotlib.font_manager as fm
#import matplotlib.pyplot as plt
#import platform as platform
#import cccmaplots as cplt
import constants as con
import cccmautils as cutl
#import cccmaNC as cnc
import cccmacmaps as ccm
#cplt = reload(cplt)
#con = reload(con)
cutl = reload(cutl)
#ccm = reload(ccm)
#cnc = reload(cnc)
# commented out all the things I supposedly fixed in config
# and startup scripts: 4/28/2014
plt.close("all")
plt.ion()
printtofile=True
# if seasonal is the only true, will do anomaly plus signif in time.
seasonal = True # plot all 4 seasons in a subplot
seacycle = False
pattcorr=0 # do pattern correlation with time instead
pattcorryr=0# yearly pattern corr with mean instead of time-integrated patt corr
dostd=0 # plot standard dev with time (just the pert run, not anomaly for now@@)
addsicn=False # add sicn contours on top (probably just for when field=='st')
smclim=True # only set up for 'st' and v2 at the moment.
# version 2 uses control climo as baseline (rather than individual times),
# and full timeseries for ttest
v2=1 # only for seasonal=1
sigtype = 'cont'
seasons = 'SON','DJF','MAM','JJA'
model = 'CanAM4'
threed = 0
############ set simulations #############
casename = 'kemctl1'
casenameh = 'kemhadctl'
casenamen = 'kemnsidcctl'
timstr = '001-121'
timstr1 = '001-061' # for 3d vars
timstr2 = '062-121' # "
timesel = '0002-01-01,0121-12-31'
# Pert run
casenamep1 = 'kem1pert1' # 2002-2012 sic and sit
casenamep2 = 'kem1pert2' # 2002-2012 sic, sit, adjusted sst
casenamep3 = 'kem1pert3' # 2002-2012 sic, adjusted sst. control sit
casenameph = 'kemhadpert'
casenamepn = 'kemnsidcpert'
casenamep21 = 'kem1pert2r1';
casenamep22 = 'kem1pert2r2'
casenamep23 = 'kem1pert2r3'
casenamep24 = 'kem1pert2r4'
casenamep25 = 'kem1pert2r5'
ensruns = casenamep21, casenamep22, casenamep23, casenamep24, casenamep25
casenamep2e = 'kem1pert2ens'
casenamep2e1 = 'kem1pert2e1';
casenamep2e2 = 'kem1pert2e2'
casenamep2e3 = 'kem1pert2e3'
casenamep2e4 = 'kem1pert2e4'
casenamep2e5 = 'kem1pert2e5'
enseruns = casenamep2e1, casenamep2e2, casenamep2e3, casenamep2e4, casenamep2e5
casenamep2ee = 'kem1pert2ense'
casenamep2es = 'kem1pert2ensspr' # super ensemble
# SET SIMULATION
casenamep = casenamep2es
rnum=5 # @@ set if casenamep is one of the ens members
timstrp = '001-121'
timstrp1 = '001-061' # for 3d vars
timstrp2 = '062-121' # "
# # # ######## set Field info ###################
# st, sicn, sic, gt, pmsl, pcp, hfl, hfs, turb, flg, fsg, fn, pcpn, zn, su, sv (@@later ufs,vfs)
# OR threed: 'gz','t','u'
field = 'pmsl'
level=30000
#level = 100000 # only for threed vars
thickness=0 # do thickness instead: just for gz
level2=70000 # for thickness calc: typically 1000-700hPa thickness
cmap = 'blue2red_w20' # default cmap
cmapclimo = 'Spectral_r'
if casenamep == casenameph:
casename = casenameh
## timstr = '001-121'
## timstr2 = '062-121'
## timstrp = '001-121'
## timstrp2 = '062-121'
## timesel = '0002-01-01,0121-12-31'
elif casenamep in ensruns:
casename = casename + 'r' + str(rnum)
## timstr = '001-121'
## timstr2 = '062-121'
## timstrp = '001-121'
## timstrp2 = '062-121'
## timesel = '0002-01-01,0121-12-31'
elif casenamep in enseruns:
casename = casename + 'e' + str(rnum)
elif casenamep == casenamep2e:
casename = 'kemctl1ens'
## timstr = '001-121'
## timstr2 = '062-121'
## timstrp = '001-121'
## timstrp2 = '062-121'
## timesel = '0002-01-01,0121-12-31'
elif casenamep == casenamep2ee:
casename = 'kemctl1ense'
elif casenamep == casenamep2es:
casename = 'kemctl1ensspr'
elif casenamep == casenamepn:
casename = casenamen
print 'CONTROL IS ' + casename
print 'PERT IS ' + casenamep
# # # ###########################################
# Shouldn't have to mod below....
if field == 'st':
units = 'K'
conv = 1 # no conversion
cmin = -2; cmax = 2 # for anomaly plots
cminp=-.5; cmaxp=.5 # for when pert is 'ctl'
cminm = -2; cmaxm = 2 # monthly
if smclim:
print 'small clim!'
cmin = -1; cmax = 1 #for anomaly plots
cminm = -.5; cmaxm = .5 # monthly
cminmp = -1; cmaxmp = 1 # for when pert is 'ctl'
if dostd==1 and casenamep in casenamep2e:
cminm=-1.5; cmaxm=1.5
cmap = 'blue2red_w20'
#cmap = 'blue2red_20'
elif field == 'gt':
units = 'K'
conv = 1 # no conversion
cmin = -2; cmax = 2 # for anomaly plots
cminp=-.5; cmaxp=.5 # for when pert is 'ctl'
cminm = -3; cmaxm = 3 # monthly
## print 'small clim!'
## cmin = -1; cmax = 1 # for anomaly plots
cminm = -2; cmaxm = 2 # monthly
cminmp = -1; cmaxmp = 1 # for when pert is 'ctl'
cmap = 'blue2red_w20'
elif field == 'sic':
units='m'
conv=1/913.
cmin=-.5
cmax=.5
cminm=-.2
cmaxm=.2
cmap = 'red2blue_w20'
elif field == 'sicn':
units='frac'
conv = 1
cmin=-.15
cmax=.15
cminm=-.1
cmaxm=.1
cmap = 'red2blue_w20'
elif field == 'gt':
units='K'
conv=1
cmin=-2; cmax=2
cminm=-3; cmaxm=3
cminp=-.5; cmaxp=.5 # for when pert is 'ctl'
cminmp = -1; cmaxmp = 1 # for when pert is 'ctl'
cmap = 'blue2red_w20'
elif field == 'pmsl':
units = 'hPa' # pretty sure hpa @@double check
conv = 1
cmin = -1; cmax = 1 # for anomaly plots
cminm=-1; cmaxm=1 # for monthly maps
cminp=cmin; cmaxp=cmax # for when pert is 'ctl'
cminmp=cminm; cmaxmp=cmaxm
cmap = 'blue2red_20'
if dostd==1 and casenamep!=casenamep2e:
cminm=-5; cmaxm=5
elif field == 'pcp':
# pct=1; units = '%'
units = 'mm/day' # original: kg m-2 s-1
conv = 86400 # convert from kg m-2 s-1 to mm/day
cmin = -.2; cmax = .2 # for anomaly plots
cminp=-.15; cmaxp=.15
cminm = -.2; cmaxm = .2
#cmap = 'PuOr'
cmap = 'brown2blue_16w'
cminpct=-12; cmaxpct=12
cminmpct=-20; cmaxmpct=20
cminmp =-.25; cmaxmp=.25
cminpctp=-8; cmaxpctp=8
cminpctmp=-12; cmaxpctmp=12
elif field == 'hfl': # sfc upward LH flux
units = 'W/m2'
conv = 1
cmin = -5
cmax = 5
cminm = -8
cmaxm = 8
elif field == 'hfs': # sfc upward SH flux
units = 'W/m2'
conv = 1
cmin = -5
cmax = 5
cminm = -8
cmaxm = 8
elif field == 'turb': # combine hfl and hfs
units = 'W/m2'
conv=1
cmin=-10
cmax=10
cminm = -20
cmaxm = 20
cmap='blue2red_20'
elif field == 'net': # net of all sfc fluxes
print " 'net ' not yet implemented! @@"
elif field == 'flg': # net downward LW at the sfc. Positive down?
units = 'W/m2'
conv = 1
cmin = -5
cmax = 5
cminm = -8
cmaxm = 8
elif field == 'fsg': # net (absorbed) solar downard at sfc
units = 'W/m2'
conv = 1
cmin = -5
cmax = 5
cminm = -8
cmaxm = 8
elif field == 'fn': # snow fraction
units = '%'
conv=100
cmin = -5
cmax = 5
cminm = -5
cmaxm = 5
cmap = 'red2blue_w20'
elif field == 'pcpn': # snowfall rate (water equivalent, kg/m2/s)
#pct = 1; units='%'
units = 'mm/day'
conv = 86400 # convert from kg m-2 s-1 to mm/day (I think it's same as pcp) @@
cmap = 'brown2blue_16w'
cmin = -.1 # for anomaly plots
cmax = .1 # for anomaly plots
cminm = -.1
cmaxm = .1
cminpct=-12
cmaxpct=12
cminmpct=-25
cmaxmpct=25
elif field == 'zn': # snow depth (m)
# pct=1; units='%'
units = 'cm'
conv = 100; # convert to cm
cmap = 'brown2blue_16w'
cmin = -2
cmax = 2
cminm = -1
cmaxm = 1
cminpct=-10
cmaxpct=10
cminmpct=-10
cmaxmpct=10
elif field == 'su':
units = 'm/s'
conv = 1;
cmap = 'blue2red_20'
cmin = -1; cmax = 1
cminm = -1; cmaxm = 1
cminp = -.5; cmaxp=.5
cminmp = -.5; cmaxmp=.5
elif field == 'sv':
units = 'm/s'
conv = 1;
cmap = 'blue2red_20'
cmin = -.5
cmax = .5
cminm = -.5
cmaxm = .5
elif field == 'gz':
threed = 1
ncfield = 'PHI'
units = 'm' # @@
conv = 1/con.get_g()
if level==50000:
cminc = 5200; cmaxc = 5900 # climo 500hPa
elif level==70000:
cminc=2800; cmaxc = 3200
elif level==30000:
cminc=8600; cmaxc = 9800
cmin = -8 # annual mean
cmax = 8 # annual mean
if level==30000:
cmin = -15; cmax = 15
#cminsea=-20; cmaxsea = 20
cminm = -15; cmaxm = 15 # for monthly
if dostd==1 and casenamep!=casenamep2e:
cminm=-60; cmaxm=60
elif dostd==1 and casenamep==casenamep2e:
cminm=-40; cmaxm=40
else:
#cminsea = -15; cmaxsea = 15
cminm = -10; cmaxm = 10 # for monthly
if thickness:
cminm = -15; cmaxm = 15
elif field == 't':
threed=1
ncfield = 'TEMP'
units = 'K' # @@
if level == 30000:
cminc = 215; cmaxc = 245
elif level == 70000:
cminc = 245; cmaxc = 285
if level == 30000:
cmin = -.3; cmax = .3
cminm = -.1; cmaxm = .1 # for monthly
#cminsea = -.5; cmaxsea = .5
elif level == 70000:
cmin = -.3; cmax = .3
cminm = -.1; cmaxm = .1 # for monthly
#cminsea = -.5; cmaxsea = .5
elif field == 'u':
threed = 1
ncfield = 'U'
units = 'm/s' #@@
if level==50000:
cminc=-25; cmaxc=25
elif level==70000:
cminc=-15; cmaxc=15
elif level == 30000:
cminc=-40; cmaxc=40
if level == 30000:
cmin = -2; cmax = 2
cminm = -.25; cmaxm = .25
#cminsea = -3; cmaxsea = 3
elif level == 85000:
cminm = -.15; cmaxm = .15
else:
cmin = -1; cmax = 1
cminm = -.25; cmaxm = .25
#cminsea = -1; cmaxsea = 1
cmapclimo='blue2red_20'
else:
print 'No settings for ' + field
## fontP = fm.FontProperties()
## fontP.set_size('small')
# # # ########## Read NC data ###############
plat = platform.system()
if plat == 'Darwin': # means I'm on my mac
basepath = '/Users/kelly/CCCma/CanSISE/RUNS/'
subdir = '/'
else: # on linux workstation in Vic
basepath = '/home/rkm/work/DATA/' + model + '/'
subdir = '/ts/'
if threed: # @@ try to put 3d vars in the same scripts
if field == 'turb':
print '3d turb not ready yet!'
else:
fnamec = basepath + casename + subdir + casename + '_' + field + '_' + timstr1 + '_ts.nc'
fnamep = basepath + casenamep + subdir + casenamep + '_' + field + '_' + timstrp1 + '_ts.nc'
fnamec2 = basepath + casename + subdir + casename + '_' + field + '_' + timstr2 + '_ts.nc'
fnamep2 = basepath + casenamep + subdir + casenamep + '_' + field + '_' + timstrp2 + '_ts.nc'
else:
if field=='turb':
field='hfl'; fieldb='hfs'
fnamec = basepath + casename + subdir + casename + '_' + field + '_' + timstr + '_ts.nc'
fnamep = basepath + casenamep + subdir + casenamep + '_' + field + '_' + timstrp + '_ts.nc'
fnamecb = basepath + casename + subdir + casename + '_' + fieldb + '_' + timstr + '_ts.nc'
fnamepb = basepath + casenamep + subdir + casenamep + '_' + fieldb + '_' + timstrp + '_ts.nc'
## fldc = cnc.getNCvar(fnamec,field.upper(),timesel=timesel)*conv + \
## cnc.getNCvar(fnamecb,fieldb.upper(),timesel=timesel)*conv
## fldp = cnc.getNCvar(fnamep,field.upper(),timesel=timesel)*conv+ \
## cnc.getNCvar(fnamepb,fieldb.upper(),timesel=timesel)*conv
field='turb'
else:
fnamec = basepath + casename + subdir + casename + '_' + field + '_' + timstr + '_ts.nc'
fnamep = basepath + casenamep + subdir + casenamep + '_' + field + '_' + timstrp + '_ts.nc'
sicnfnamec = basepath + casename + subdir + casename + '_sicn_' + timstr + '_ts.nc'
sicnfnamep = basepath + casenamep + subdir + casenamep + '_sicn_' + timstrp + '_ts.nc'
## fldc = cnc.getNCvar(fnamec,field.upper(),timesel=timesel)*conv
## fldp = cnc.getNCvar(fnamep,field.upper(),timesel=timesel)*conv
lat = cnc.getNCvar(fnamec,'lat')
lon = cnc.getNCvar(fnamec,'lon')
## nt,nlat,nlon = fldc.shape
## nyr = nt/12.
if sigtype == 'hatch':
suff = 'png'
else:
suff = 'pdf'
if seasonal: # plot all 4 seasons in a subplot
cmlen=float( plt.cm.get_cmap(cmap).N) # or: from __future__ import division
incr = (cmaxm-cminm) / (cmlen)
conts = np.arange(cminm,cmaxm+incr,incr)
midx=0
fig,axs = plt.subplots(4,1)
fig.set_size_inches(6,8)
fig.subplots_adjust(hspace=.15,wspace=.05)
if addsicn:
print 'addsicn not implemented yet! @@'
for ax in axs.flat:
if threed:
fldcsea = np.append(cnc.getNCvar(fnamec,ncfield,timesel=timesel,levsel=level,seas=seasons[midx])*conv,
cnc.getNCvar(fnamec2,ncfield,levsel=level,seas=seasons[midx])*conv,
axis=0)
fldpsea = np.append(cnc.getNCvar(fnamep,ncfield,timesel=timesel,levsel=level,seas=seasons[midx])*conv,
cnc.getNCvar(fnamep2,ncfield,levsel=level,seas=seasons[midx])*conv,
axis=0)
if field == 'gz' and thickness == 1:
# get level2, then thickness is level2-level
fldcsea2 = np.append(cnc.getNCvar(fnamec,ncfield,timesel=timesel,levsel=level2,seas=seasons[midx])*conv,
cnc.getNCvar(fnamec2,ncfield,levsel=level2,seas=seasons[midx])*conv,
axis=0)
fldpsea2 = np.append(cnc.getNCvar(fnamep,ncfield,timesel=timesel,levsel=level2,seas=seasons[midx])*conv,
cnc.getNCvar(fnamep2,ncfield,levsel=level2,seas=seasons[midx])*conv,
axis=0)
fldcsea = fldcsea2 - fldcsea
fldpsea = fldpsea2 - fldpsea
else:
if field=='turb':
field='hfl'; fieldb='hfs'
fldcsea = cnc.getNCvar(fnamec,field.upper(),timesel=timesel,
seas=seasons[midx])*conv + cnc.getNCvar(fnamecb,fieldb.upper(),
timesel=timesel,seas=seasons[midx])*conv
fldpsea = cnc.getNCvar(fnamep,field.upper(),timesel=timesel,
seas=seasons[midx])*conv + cnc.getNCvar(fnamepb,fieldb.upper(),
timesel=timesel,seas=seasons[midx])*conv
field='turb'
else:
fldcsea = cnc.getNCvar(fnamec,field.upper(),timesel=timesel,
seas=seasons[midx])*conv # @@note winter returns 2 fewer elements
fldpsea = cnc.getNCvar(fnamep,field.upper(),timesel=timesel,
seas=seasons[midx])*conv
nyr,nlat,nlon = fldcsea.shape
years = np.arange(0,nyr)
if pattcorr==1:
fldcseazm = fldcsea # do not take zonal mean
fldpseazm = fldpsea
fldcseazmtm = np.mean(fldcseazm,axis=0) # time mean now
fldpseazmtm = np.mean(fldpseazm,axis=0)
plotd = np.zeros(nyr)
tstat = np.zeros(nyr)
pval = np.zeros(nyr)
testd = np.zeros(nyr) # @@
else:
# take a zonal mean for each time
fldcseazm = np.mean(fldcsea[...,:-1],axis=2) # time x lat
fldpseazm = np.mean(fldpsea[...,:-1],axis=2)
fldcseazmtm = np.mean(fldcseazm,axis=0) # time mean now
fldpseazmtm = np.mean(fldpseazm,axis=0)
plotd = np.zeros((nyr,nlat))
tstat = np.zeros((nyr,nlat))
pval = np.zeros((nyr,nlat))
for yr in years:
if pattcorr==1:
# do a pattern correlation with time
corrlim=45 # @@
areas = cutl.calc_cellareas(lat,lon)
areas = areas[lat>corrlim,:]
#areas = ma.masked_where(lmask[lat>corrlim,:]==-1,areas)
weights = areas / np.sum(np.sum(areas,axis=1),axis=0)
# Could also do each year's pattern corr rather than cumulative mean DONE
# OR, use the ens mean pattern as the pattern to compare against -- not useful I think,
# for what we want to know: is the pattern of response for each ensemble member random
# or dependent on the boundary condition.
# @@ Also, would be nice to have multiple variables in one plot and/or multiple simulations
if pattcorryr:
# yearly anomaly pattern corr w/ the time mean pattern
tmp = fldpseazm[yr,lat>corrlim,...] - fldcseazmtm[lat>corrlim,...]
else:
# time-integrated anomaly pattern corr w/ the end anomaly pattern
tmp = np.mean(fldpseazm[0:yr,lat>corrlim,...],axis=0)-fldcseazmtm[lat>corrlim,...]
tmpmean = fldpseazmtm[lat>corrlim,...]-fldcseazmtm[lat>corrlim,...] # the end pattern
tmpcorr = ma.corrcoef(tmp.flatten()*weights.flatten(),
tmpmean.flatten()*weights.flatten())
plotd[yr] = tmpcorr[0,1]
testd[yr] = cutl.pattcorr(tmp.flatten()*weights.flatten(),
tmpmean.flatten()*weights.flatten()) # @@ same result as built-in method
""" from canam4sims_analens.py. modify for here
ensmem = fldpdict[sim][moidx,lat>corrlim,...] - fldcdict[sim][moidx,lat>corrlim,...]
obsbc = fldp2[moidx,lat>corrlim,...] - fldc2[moidx,lat>corrlim,...]
# weight the fields by area
areas = cutl.calc_cellareas(lat,lon)
areas = areas[lat>corrlim,:]
areas = ma.masked_where(lmask[lat>corrlim,:]==-1,areas)
weights = areas / np.sum(np.sum(areas,axis=1),axis=0)
ensmem = ma.masked_where(lmask[lat>corrlim,:]==-1,ensmem) # mask out land
obsbc = ma.masked_where(lmask[lat>corrlim,:]==-1,obsbc) # mask out land
# @@@ note have to use masked corrcoef!
# @@ This is probably doing a centered pearson where I really
# want uncentered (no central mean removed). Could calc myself from
# http://www.stanford.edu/~maureenh/quals/html/ml/node53.html
tmpcorr = ma.corrcoef(ensmem.flatten()*weights.flatten(), obsbc.flatten()*weights.flatten())
pcorr[moidx] = tmpcorr[0,1]
@@ see above: doesn't seem to remove mean, gives same result as calc'ing the cov, etc.
"""
elif dostd==1:
if yr>=5:
plotd[yr,:] = np.std(fldpseazm[0:yr,:],axis=0)#@@-np.std(fldcseazmtm,axis=0)
#tstat[yr,:],pval[yr,:] = sp.stats.ttest_ind(fldpseazm[0:yr,:],fldcseazm,axis=0)
else:
#pval[yr,:] = np.ones((1,nlat))
plotd[yr,:] = np.NaN
elif v2==1:
# here, use the control climo as the baseline (rather than 1 year), and give
# the full control timeseries to the ttest every time
plotd[yr,:] = np.mean(fldpseazm[0:yr,:]-fldcseazmtm,axis=0)
if yr>5:
tstat[yr,:],pval[yr,:] = sp.stats.ttest_ind(fldpseazm[0:yr,:],fldcseazm,axis=0)
else:
pval[yr,:] = np.ones((1,nlat))
else:
plotd[yr,:] = np.mean(fldpseazm[0:yr,:]-fldcseazm[0:yr,:],axis=0)
if yr>5: # start doing stats after 5 years
tstat[yr,:],pval[yr,:] = sp.stats.ttest_ind(fldpseazm[0:yr,:],fldcseazm[0:yr,:],axis=0)
else:
pval[yr,:] = np.ones((1,nlat))
if pattcorr: # pattern corr by time
if pattcorryr:
plotd.sort()
ax.plot(years,plotd,marker='*')
ax.set_ylim(-1,1)
else:
ax.plot(years,plotd)
#ax.plot(years,testd,color='r',marker='.') @@ this gives same result!
ax.set_ylim(0,1)
else: # lat by time
lats,times = np.meshgrid(lat,years)
plotfld = plotd #@@these years could be in any order...how take this into account??
cf = ax.contourf(times,lats,plotfld,cmap=plt.cm.get_cmap(cmap),
levels=conts,vmin=cminm,vmax=cmaxm,extend='both')
#cplt.addtsig(ax,pval,lat,years,type=sigtype) # @@ I don't think the stats are right.
if dostd!=1:
if sigtype == 'cont':
ax.contour(times,lats,pval,levels=[0.05,0.05],colors='k')
elif sigtype == 'hatch':
ax.contourf(times,lats,pval,levels=[0,0.05],colors='none',hatches='.')
ax.set_ylim(0,90)
ax.set_ylabel(seasons[midx])
if midx == 3:
ax.set_xlabel('Time')
ax.set_xlim(0,nyr)
midx=midx+1
# ending the loop through seasons
if pattcorr!=1:
cbar_ax = fig.add_axes([.91,.15, .02,.7])
fig.colorbar(cf,cax=cbar_ax)
if threed:
if field == 'gz' and thickness==1:
plt.suptitle(field + ' ' + str(level2/100) + '-' + str(level/100) + ' (' + units + ')')
else:
plt.suptitle(field + ' ' + str(level/100) + ' (' + units + ')')
else:
plt.suptitle(field + ' (' + units + ')')
#plt.tight_layout() # makes it worse
if printtofile:
prfield = field # print field
if threed:
if field == 'gz' and thickness==1:
prfield = field + str(level2/100) + '-' + str(level/100)
else:
prfield = field + str(level/100)
if pattcorr==1:
if pattcorryr==1: # yearly
fig.savefig(prfield + 'diffpattcorryrly' + '_' + casenamep +\
'_v_' + casename + '_timexlat_seas_nh' + str(corrlim) + 'N.' + suff)
else: # time-integrated
fig.savefig(prfield + 'diffpattcorr' + '_' + casenamep +\
'_v_' + casename + '_timexlat_seas_nh' + str(corrlim) + 'N.' + suff)
elif dostd==1:
fig.savefig(prfield + 'stddev' + '_' + casenamep +\
'_timexlat_seas_nh.' + suff)
elif v2:
if smclim:
fig.savefig(prfield + 'diffsig' + sigtype + '_' + casenamep +\
'_v_' + casename + '_timexlat_seas_nh_v2smclim.' + suff)
else:
fig.savefig(prfield + 'diffsig' + sigtype + '_' + casenamep +\
'_v_' + casename + '_timexlat_seas_nh_v2.' + suff)
else:
fig.savefig(prfield + 'diffsig' + sigtype + '_' + casenamep +\
'_v_' + casename + '_timexlat_seas_nh.' + suff)
if seacycle: # want month x lat (or height)
cmlen=float( plt.cm.get_cmap(cmap).N) # or: from __future__ import division
incr = (cmaxm-cminm) / (cmlen)
conts = np.arange(cminm,cmaxm+incr,incr)
months = con.get_mon()
if threed:
print 'fix 3D vars to use premade files @@'
fldc = np.append(cnc.getNCvar(fnamec,ncfield,timesel=timesel,levsel=level)*conv,
cnc.getNCvar(fnamec2,ncfield,levsel=level)*conv,
axis=0)
fldp = np.append(cnc.getNCvar(fnamep,ncfield,timesel=timesel,levsel=level)*conv,
cnc.getNCvar(fnamep2,ncfield,levsel=level)*conv,
axis=0)
else:
if field=='turb':
field='hfl'; fieldb='hfs'
fldc = cnc.getNCvar(fnamec,field.upper(),timesel=timesel)*conv + \
cnc.getNCvar(fnamecb,fieldb.upper(),timesel=timesel)*conv
fldp = cnc.getNCvar(fnamep,field.upper(),timesel=timesel)*conv + \
cnc.getNCvar(fnamepb,fieldb.upper(),timesel=timesel)*conv
field='turb'
else:
fldc = cnc.getNCvar(fnamec,field.upper(),timesel=timesel)*conv
fldp = cnc.getNCvar(fnamep,field.upper(),timesel=timesel)*conv
if addsicn:
sicnc = cnc.getNCvar(sicnfnamec,'SICN',timesel=timesel)
sicnp = cnc.getNCvar(sicnfnamep,'SICN',timesel=timesel)
monsicnczmclimo = np.zeros((12,sicnc.shape[1]))
monsicnpzmclimo = np.zeros((12,sicnc.shape[1]))
tstat = np.zeros((12,fldc.shape[1]))
pval = np.zeros((12,fldc.shape[1]))
monfldczmclimo = np.zeros((12,fldc.shape[1]))
monfldpzmclimo = np.zeros((12,fldc.shape[1]))
# loop through months, calcing mean pert and stat sig
for midx in np.arange(0,12):
if pattcorr:
print 'do pattern corr @@'
elif dostd:
# taken zonal mean
monfldczm = np.mean(fldc[midx::12,:,:-1],axis=2)
monfldpzm = np.mean(fldp[midx::12,:,:-1],axis=2)
# std dev
monfldczmclimo[midx,:] = np.std(monfldczm,axis=0)
monfldpzmclimo[midx,:] = np.std(monfldpzm,axis=0)
else:
# taken zonal mean
monfldczm = np.mean(fldc[midx::12,:,:-1],axis=2)
monfldpzm = np.mean(fldp[midx::12,:,:-1],axis=2)
# time mean
monfldczmclimo[midx,:] = np.mean(monfldczm,axis=0)
monfldpzmclimo[midx,:] = np.mean(monfldpzm,axis=0)
tstat[midx,:],pval[midx,:] = sp.stats.ttest_ind(monfldpzm,monfldczm,axis=0)
if addsicn:
# taken zonal mean
monsicnczm = np.mean(sicnc[midx::12,:,:-1],axis=2)
monsicnpzm = np.mean(sicnp[midx::12,:,:-1],axis=2)
# time mean
monsicnczmclimo[midx,:] = np.mean(monsicnczm,axis=0)*100 # percent for ease of reading
monsicnpzmclimo[midx,:] = np.mean(monsicnpzm,axis=0)*100
lats,mos = np.meshgrid(lat,np.arange(0,12))
fig2,ax = plt.subplots(1,1)
fig2.set_size_inches(6, 5)
if dostd==1:
plotfld = monfldpzmclimo
else:
plotfld = monfldpzmclimo - monfldczmclimo
cf = ax.contourf(mos,lats,plotfld,cmap=plt.cm.get_cmap(cmap),
levels=conts,vmin=cminm,vmax=cmaxm,extend='both')
if dostd!=1:
if sigtype == 'cont':
ax.contour(mos,lats,pval,levels=[0.05,0.05],colors='k')
elif sigtype == 'hatch':
ax.contourf(mos,lats,pval,levels=[0,0.05],colors='none',hatches='.')
if addsicn:
CS = ax.contour(mos,lats,-1*(monsicnpzmclimo - monsicnczmclimo),
levels=[-10,-5,-1, 1,5,10,15],
colors='0.7',linewidths=2)# reverse sign to get solid lines for ice loss
ax.clabel(CS,fmt = '%2.0f',inline=True,
inline_spacing=3,fontsize=12,fontweight='bold') # bold doesn't seem to work
ax.set_xlim(0,11)
ax.set_xticks(range(0,12))
ax.set_xticklabels(months)
ax.set_ylim(0,88)
ax.set_ylabel('Latitude')
ax.set_xlabel('Month')
cbar_ax = fig2.add_axes([.91,.15, .02,.7])
fig2.colorbar(cf,cax=cbar_ax)
if threed:
if field == 'gz' and thickness==1:
plt.suptitle(field + ' ' + str(level2/100) + '-' + str(level/100) + ' (' + units + ')')
else:
plt.suptitle(field + ' ' + str(level/100) + ' (' + units + ')')
else:
plt.suptitle(field + ' (' + units + ')')
#plt.tight_layout() makes it worse
if printtofile:
prfield = field
if threed:
if field == 'gz' and thickness==1:
prfield = field + str(level2/100) + '-' + str(level/100)
else:
prfield = field + str(level/100)
if pattcorr==1:
fig2.savefig(prfield + 'diffpattcorr' + sigtype + '_' + casenamep +\
'_v_' + casename + '_monxlat_nh.' + suff)
elif dostd==1:
fig2.savefig(prfield + 'stddev' + '_' + casenamep +\
'_monxlat_nh.' + suff)
else:
if addsicn:
fig2.savefig(prfield + 'diffsig' + sigtype + '_SICNcont_' + casenamep +\
'_v_' + casename + '_monxlat_nh.' + suff)
else:
fig2.savefig(prfield + 'diffsig' + sigtype + '_' + casenamep +\
'_v_' + casename + '_monxlat_nh.' + suff)