Changeset 343 in lmdz_wrf
- Timestamp:
- Mar 9, 2015, 5:32:39 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/validation_sim.py
r341 r343 11 11 from optparse import OptionParser 12 12 from netCDF4 import Dataset as NetCDFFile 13 from scipy import stats as sts 14 import numpy.ma as ma 13 15 14 16 main = 'validarion_sim.py' … … 294 296 295 297 return valpos 298 299 def datetimeStr_datetime(StringDT): 300 """ Function to transform a string date ([YYYY]-[MM]-[DD]_[HH]:[MI]:[SS] format) to a date object 301 >>> datetimeStr_datetime('1976-02-17_00:00:00') 302 1976-02-17 00:00:00 303 """ 304 import datetime as dt 305 306 fname = 'datetimeStr_datetime' 307 308 dateD = np.zeros((3), dtype=int) 309 timeT = np.zeros((3), dtype=int) 310 311 dateD[0] = int(StringDT[0:4]) 312 dateD[1] = int(StringDT[5:7]) 313 dateD[2] = int(StringDT[8:10]) 314 315 trefT = StringDT.find(':') 316 if not trefT == -1: 317 # print ' ' + fname + ': refdate with time!' 318 timeT[0] = int(StringDT[11:13]) 319 timeT[1] = int(StringDT[14:16]) 320 timeT[2] = int(StringDT[17:19]) 321 322 if int(dateD[0]) == 0: 323 print warnmsg 324 print ' ' + fname + ': 0 reference year!! changing to 1' 325 dateD[0] = 1 326 327 newdatetime = dt.datetime(dateD[0], dateD[1], dateD[2], timeT[0], timeT[1], timeT[2]) 328 329 return newdatetime 330 331 def datetimeStr_conversion(StringDT,typeSi,typeSo): 332 """ Function to transform a string date to an another date object 333 StringDT= string with the date and time 334 typeSi= type of datetime string input 335 typeSo= type of datetime string output 336 [typeSi/o] 337 'cfTime': [time],[units]; ]time in CF-convention format [units] = [tunits] since [refdate] 338 'matYmdHMS': numerical vector with [[YYYY], [MM], [DD], [HH], [MI], [SS]] 339 'YmdHMS': [YYYY][MM][DD][HH][MI][SS] format 340 'Y-m-d_H:M:S': [YYYY]-[MM]-[DD]_[HH]:[MI]:[SS] format 341 'Y-m-d H:M:S': [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] format 342 'Y/m/d H-M-S': [YYYY]/[MM]/[DD] [HH]-[MI]-[SS] format 343 'WRFdatetime': [Y], [Y], [Y], [Y], '-', [M], [M], '-', [D], [D], '_', [H], 344 [H], ':', [M], [M], ':', [S], [S] 345 >>> datetimeStr_conversion('1976-02-17_08:32:05','Y-m-d_H:M:S','matYmdHMS') 346 [1976 2 17 8 32 5] 347 >>> datetimeStr_conversion(str(137880)+',minutes since 1979-12-01_00:00:00','cfTime','Y/m/d H-M-S') 348 1980/03/05 18-00-00 349 """ 350 import datetime as dt 351 352 fname = 'datetimeStr_conversion' 353 354 if StringDT[0:1] == 'h': 355 print fname + '_____________________________________________________________' 356 print datetimeStr_conversion.__doc__ 357 quit() 358 359 if typeSi == 'cfTime': 360 timeval = np.float(StringDT.split(',')[0]) 361 tunits = StringDT.split(',')[1].split(' ')[0] 362 Srefdate = StringDT.split(',')[1].split(' ')[2] 363 364 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 365 ## 366 yrref=Srefdate[0:4] 367 monref=Srefdate[5:7] 368 dayref=Srefdate[8:10] 369 370 trefT = Srefdate.find(':') 371 if not trefT == -1: 372 # print ' ' + fname + ': refdate with time!' 373 horref=Srefdate[11:13] 374 minref=Srefdate[14:16] 375 secref=Srefdate[17:19] 376 refdate = datetimeStr_datetime( yrref + '-' + monref + '-' + dayref + \ 377 '_' + horref + ':' + minref + ':' + secref) 378 else: 379 refdate = datetimeStr_datetime( yrref + '-' + monref + '-' + dayref + \ 380 + '_00:00:00') 381 382 if tunits == 'weeks': 383 newdate = refdate + dt.timedelta(weeks=float(timeval)) 384 elif tunits == 'days': 385 newdate = refdate + dt.timedelta(days=float(timeval)) 386 elif tunits == 'hours': 387 newdate = refdate + dt.timedelta(hours=float(timeval)) 388 elif tunits == 'minutes': 389 newdate = refdate + dt.timedelta(minutes=float(timeval)) 390 elif tunits == 'seconds': 391 newdate = refdate + dt.timedelta(seconds=float(timeval)) 392 elif tunits == 'milliseconds': 393 newdate = refdate + dt.timedelta(milliseconds=float(timeval)) 394 else: 395 print errormsg 396 print ' timeref_datetime: time units "' + tunits + '" not ready!!!!' 397 quit(-1) 398 399 yr = newdate.year 400 mo = newdate.month 401 da = newdate.day 402 ho = newdate.hour 403 mi = newdate.minute 404 se = newdate.second 405 elif typeSi == 'matYmdHMS': 406 yr = StringDT[0] 407 mo = StringDT[1] 408 da = StringDT[2] 409 ho = StringDT[3] 410 mi = StringDT[4] 411 se = StringDT[5] 412 elif typeSi == 'YmdHMS': 413 yr = int(StringDT[0:4]) 414 mo = int(StringDT[4:6]) 415 da = int(StringDT[6:8]) 416 ho = int(StringDT[8:10]) 417 mi = int(StringDT[10:12]) 418 se = int(StringDT[12:14]) 419 elif typeSi == 'Y-m-d_H:M:S': 420 dateDT = StringDT.split('_') 421 dateD = dateDT[0].split('-') 422 timeT = dateDT[1].split(':') 423 yr = int(dateD[0]) 424 mo = int(dateD[1]) 425 da = int(dateD[2]) 426 ho = int(timeT[0]) 427 mi = int(timeT[1]) 428 se = int(timeT[2]) 429 elif typeSi == 'Y-m-d H:M:S': 430 dateDT = StringDT.split(' ') 431 dateD = dateDT[0].split('-') 432 timeT = dateDT[1].split(':') 433 yr = int(dateD[0]) 434 mo = int(dateD[1]) 435 da = int(dateD[2]) 436 ho = int(timeT[0]) 437 mi = int(timeT[1]) 438 se = int(timeT[2]) 439 elif typeSi == 'Y/m/d H-M-S': 440 dateDT = StringDT.split(' ') 441 dateD = dateDT[0].split('/') 442 timeT = dateDT[1].split('-') 443 yr = int(dateD[0]) 444 mo = int(dateD[1]) 445 da = int(dateD[2]) 446 ho = int(timeT[0]) 447 mi = int(timeT[1]) 448 se = int(timeT[2]) 449 elif typeSi == 'WRFdatetime': 450 yr = int(StringDT[0])*1000 + int(StringDT[1])*100 + int(StringDT[2])*10 + \ 451 int(StringDT[3]) 452 mo = int(StringDT[5])*10 + int(StringDT[6]) 453 da = int(StringDT[8])*10 + int(StringDT[9]) 454 ho = int(StringDT[11])*10 + int(StringDT[12]) 455 mi = int(StringDT[14])*10 + int(StringDT[15]) 456 se = int(StringDT[17])*10 + int(StringDT[18]) 457 else: 458 print errormsg 459 print ' ' + fname + ': type of String input date "' + typeSi + \ 460 '" not ready !!!!' 461 quit(-1) 462 463 if typeSo == 'matYmdHMS': 464 dateYmdHMS = np.zeros((6), dtype=int) 465 dateYmdHMS[0] = yr 466 dateYmdHMS[1] = mo 467 dateYmdHMS[2] = da 468 dateYmdHMS[3] = ho 469 dateYmdHMS[4] = mi 470 dateYmdHMS[5] = se 471 elif typeSo == 'YmdHMS': 472 dateYmdHMS = str(yr).zfill(4) + str(mo).zfill(2) + str(da).zfill(2) + \ 473 str(ho).zfill(2) + str(mi).zfill(2) + str(se).zfill(2) 474 elif typeSo == 'Y-m-d_H:M:S': 475 dateYmdHMS = str(yr).zfill(4) + '-' + str(mo).zfill(2) + '-' + \ 476 str(da).zfill(2) + '_' + str(ho).zfill(2) + ':' + str(mi).zfill(2) + ':' + \ 477 str(se).zfill(2) 478 elif typeSo == 'Y-m-d H:M:S': 479 dateYmdHMS = str(yr).zfill(4) + '-' + str(mo).zfill(2) + '-' + \ 480 str(da).zfill(2) + ' ' + str(ho).zfill(2) + ':' + str(mi).zfill(2) + ':' + \ 481 str(se).zfill(2) 482 elif typeSo == 'Y/m/d H-M-S': 483 dateYmdHMS = str(yr).zfill(4) + '/' + str(mo).zfill(2) + '/' + \ 484 str(da).zfill(2) + ' ' + str(ho).zfill(2) + '-' + str(mi).zfill(2) + '-' + \ 485 str(se).zfill(2) 486 elif typeSo == 'WRFdatetime': 487 dateYmdHMS = [] 488 yM = yr/1000 489 yC = (yr-yM*1000)/100 490 yD = (yr-yM*1000-yC*100)/10 491 yU = yr-yM*1000-yC*100-yD*10 492 493 mD = mo/10 494 mU = mo-mD*10 495 496 dD = da/10 497 dU = da-dD*10 498 499 hD = ho/10 500 hU = ho-hD*10 501 502 miD = mi/10 503 miU = mi-miD*10 504 505 sD = se/10 506 sU = se-sD*10 507 508 dateYmdHMS.append(str(yM)) 509 dateYmdHMS.append(str(yC)) 510 dateYmdHMS.append(str(yD)) 511 dateYmdHMS.append(str(yU)) 512 dateYmdHMS.append('-') 513 dateYmdHMS.append(str(mD)) 514 dateYmdHMS.append(str(mU)) 515 dateYmdHMS.append('-') 516 dateYmdHMS.append(str(dD)) 517 dateYmdHMS.append(str(dU)) 518 dateYmdHMS.append('_') 519 dateYmdHMS.append(str(hD)) 520 dateYmdHMS.append(str(hU)) 521 dateYmdHMS.append(':') 522 dateYmdHMS.append(str(miD)) 523 dateYmdHMS.append(str(miU)) 524 dateYmdHMS.append(':') 525 dateYmdHMS.append(str(sD)) 526 dateYmdHMS.append(str(sU)) 527 else: 528 print errormsg 529 print ' ' + fname + ': type of output date "' + typeSo + '" not ready !!!!' 530 quit(-1) 531 532 return dateYmdHMS 296 533 297 534 def coincident_CFtimes(tvalB, tunitA, tunitB): … … 576 813 'WRFp': pressure from WRF variables 577 814 'WRFrh': relative humidty fom WRF variables 815 'WRFT': CF-time from WRF variables 578 816 'WRFt': temperature from WRF variables 579 817 'WRFtd': dew-point temperature from WRF variables … … 645 883 shape = ncobj.variables['P'].shape 646 884 885 elif varn == 'WRFT': 886 # To compute CF-times from WRF kind 887 # 888 import datetime as dt 889 890 times = ncobj.variables['Times'] 891 dimt = times.shape[0] 892 varNOcheckv = np.zeros((dimt), dtype=np.float64) 893 self.unitsval = 'seconds since 1949-12-01 00:00:00' 894 refdate = datetimeStr_datetime('1949-12-01_00:00:00') 895 896 dimensions = tuple([ncobj.variables['Times'].dimensions[0]]) 897 shape = tuple([dimt]) 898 899 for it in range(dimt): 900 datevalS = datetimeStr_conversion(times[it,:], 'WRFdatetime', \ 901 'YmdHMS') 902 dateval = dt.datetime.strptime(datevalS, '%Y%m%d%H%M%S') 903 difft = dateval - refdate 904 varNOcheckv[it] = difft.days*3600.*24. + difft.seconds + \ 905 np.float(int(difft.microseconds/10.e6)) 906 647 907 elif varn == 'WRFt': 648 908 # print ' ' + main + ': computing temperature from WRF as inv_potT(T + 300) ...' … … 740 1000 '[constant] to variables values; divc,[constant]: divide by [constant] to ' + \ 741 1001 'variables values' 742 varNOcheck = ['WRFdens', 'WRFght', 'WRFp', 'WRFrh', 'WRF t', 'WRFtd', 'WRFws',\1002 varNOcheck = ['WRFdens', 'WRFght', 'WRFp', 'WRFrh', 'WRFT', 'WRFt', 'WRFtd', 'WRFws',\ 743 1003 'WRFwd', 'WRFz'] 744 1004 varNOcheckinf = "'WRFdens': air density from WRF variables; 'WRFght': geopotential"+ \ 745 1005 " height from WRF variables; 'WRFp': pressure from WRF variables; 'WRFrh': " + \ 746 "relative humidty fom WRF variables; 'WRF t': temperature from WRF variables; " +\747 " 'WRFtd': dew-point temperature from WRF variables; 'WRFws': wind speed from " +\748 " WRF variables; 'WRFwd': wind speed direction from WRF variables; 'WRFz': " +\749 " height from WRF variables"1006 "relative humidty fom WRF variables; 'WRFT': CF-time from WRF variables'WRFt'; " + \ 1007 " temperature from WRF variables; 'WRFtd': dew-point temperature from WRF " + \ 1008 "variables; 'WRFws': wind speed from WRF variables; 'WRFwd': wind speed " + \ 1009 "direction from WRF variables; 'WRFz': height from WRF variables" 750 1010 751 1011 dimshelp = "[DIM]@[simdim]@[obsdim] ',' list of couples of dimensions names from " + \ … … 925 1185 if not osim.dimensions.has_key(dims[dn][0]): 926 1186 print errormsg 927 print ' ' + main + ": simulation file does not have dimension '" + \ 928 dims[dn][0] + "' !!" 1187 print ' ' + main + ": simulation file '" + opts.fsim + "' does not have " + \ 1188 "dimension '" + dims[dn][0] + "' !!" 1189 print ' it has: ',osim.dimensions 929 1190 quit(-1) 1191 930 1192 if not osim.variables.has_key(vardims[dn][0]) and not \ 931 1193 searchInlist(varNOcheck,vardims[dn][0]): 932 1194 print errormsg 933 print ' ' + main + ": simulation file does not have varibale dimension '" + \ 934 vardims[dn][0] + "' !!" 1195 print ' ' + main + ": simulation file '" + opts.fsim + "' does not have " + \ 1196 "varibale dimension '" + vardims[dn][0] + "' !!" 1197 print ' it has variables:',osim.variables 935 1198 quit(-1) 936 1199 if searchInlist(varNOcheck,vardims[dn][0]): … … 940 1203 941 1204 # General characteristics 942 dimtobs = len(valdimobs['T'])943 dimtsim = len(valdimsim['T'])1205 dimtobs = valdimobs['T'].shape[0] 1206 dimtsim = valdimsim['T'].shape[0] 944 1207 945 1208 print main +': observational time-steps:',dimtobs,'simulation:',dimtsim … … 1026 1289 tobj = oobs.variables[vardims['T'][1]] 1027 1290 obstunits = tobj.getncattr('units') 1028 tobj = osim.variables[vardims['T'][0]] 1029 simtunits = tobj.getncattr('units') 1030 1031 simobstimes = coincident_CFtimes(valdimsim['T'], obstunits, simtunits) 1291 if vardims['T'][0] == 'WRFT': 1292 tsim = valdimsim['T'][:] 1293 simtunits = 'seconds since 1949-12-01 00:00:00' 1294 else: 1295 tsim = osim.variables[vardims['T'][0]] 1296 simtunits = tobj.getncattr('units') 1297 1298 simobstimes = coincident_CFtimes(valdimsim['T'][:], obstunits, simtunits) 1032 1299 1033 1300 # Concident times … … 1157 1424 elif obskind == 'single-station': 1158 1425 for it in range(Ncoindt): 1159 slicev = dims['X'][0]+':'+str(stationpos[1]) + '|' + \ 1160 dims['Y'][0]+':'+str(stationpos[0]) + '|' + \ 1161 dims['T'][0]+':'+str(coindtvalues[it][0]) 1426 ito = int(coindtvalues[it,1]) 1427 slicev = dims['X'][0] + ':' + str(stationpos[1]) + '|' + \ 1428 dims['Y'][0] + ':' + str(stationpos[0]) + '|' + \ 1429 dims['T'][0] + ':' + str(int(coindtvalues[it][0])) 1162 1430 slicevar, dimslice = slice_variable(ovsim, slicev) 1163 slicev = dims['X'][0] + ':' + str(trajpos[2,it]-Ngrid) + '@' + \ 1164 str(trajpos[2,it]+Ngrid) + '|' + dims['Y'][0] + ':' + \ 1165 str(trajpos[1,it]-Ngrid) + '@' + str(trajpos[1,it]+Ngrid) + '|' + \ 1166 dims['T'][0] + ':' + str(coindtvalues[it,0]) 1431 if ovobs[int(ito)] != ovobs[int(ito)]: 1432 simobsvalues.append([ slicevar, fillValueF]) 1433 else: 1434 simobsvalues.append([ slicevar, ovobs[int(ito)]]) 1435 slicev = dims['X'][0] + ':' + str(stationpos[1]-Ngrid) + '@' + \ 1436 str(stationpos[1]+Ngrid+1) + '|' + dims['Y'][0] + ':' + \ 1437 str(stationpos[0]-Ngrid) + '@' + str(stationpos[0]+Ngrid+1) + '|' + \ 1438 dims['T'][0] + ':' + str(int(coindtvalues[it,0])) 1167 1439 slicevar, dimslice = slice_variable(ovsim, slicev) 1168 1440 simobsSvalues[it,:,:] = slicevar 1441 1169 1442 elif obskind == 'trajectory': 1170 1443 if dims.has_key('Z'): … … 1250 1523 quit(-1) 1251 1524 1525 # statisics sim 1526 simstats = np.zeros((5), dtype=np.float) 1527 simstats[0] = np.min(arrayvals[:,0]) 1528 simstats[1] = np.max(arrayvals[:,0]) 1529 simstats[2] = np.mean(arrayvals[:,0]) 1530 simstats[3] = np.mean(arrayvals[:,0]*arrayvals[:,0]) 1531 simstats[4] = np.sqrt(simstats[3] - simstats[2]*simstats[2]) 1532 1533 # statisics obs 1534 obsmask = ma.masked_equal(arrayvals, fillValueF) 1535 obsmask2 = obsmask*obsmask 1536 1537 print 'Lluis',obsmask 1538 1539 obsstats = np.zeros((5), dtype=np.float) 1540 obsstats[0] = obsmask.min() 1541 obsstats[1] = obsmask.max() 1542 obsstats[2] = obsmask.mean() 1543 obsstats[3] = obsmask2.mean() 1544 obsstats[4] = np.sqrt(obsstats[3] - obsstats[2]*obsstats[2]) 1545 1546 # Statistics sim-obs 1547 simobsstats = np.zeros((5), dtype=np.float) 1548 diffvals = np.zeros((Ncoindt), dtype=np.float) 1549 1550 diffvals = arrayvals[:,0] - arrayvals[:,1] 1551 simobsstats[0] = simstats[0] - obsstats[0] 1552 simobsstats[1] = np.mean(np.abs(diffvals)) 1553 simobsstats[2] = np.sqrt(simstats[3] + obsstats[3] - 2.*simstats[2]*obsstats[2]) 1554 simobsstats[3], simobsstats[4] = sts.pearsonr(arrayvals[:,0], arrayvals[:,1]) 1555 1556 print 'Lluis means sim:',simstats[0],'obs:',obsstats[0] 1557 print 'Lluis tests rmse:',simobsstats[2], np.sqrt(np.mean(diffvals*diffvals)) 1558 1252 1559 # Statistics around values 1253 1560 aroundstats = np.zeros((5,Ncoindt), dtype=np.float)
Note: See TracChangeset
for help on using the changeset viewer.