Changeset 344 in lmdz_wrf


Ignore:
Timestamp:
Mar 9, 2015, 7:02:22 PM (10 years ago)
Author:
lfita
Message:

Adding statistics for sim, obs, simobs and around temporally from observations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/validation_sim.py

    r343 r344  
    10181018  "(WRFdiagnosted variables also available: " + varNOcheckinf + ")"
    10191019statsn = ['minimum', 'maximum', 'mean', 'mean2', 'standard deviation']
     1020gstatsn = ['bias', 'simobs_mean', 'sim_obsmin', 'sim_obsmax', 'sim_obsmean', 'mae',  \
     1021  'rmse', 'r_pearsoncorr', 'p_pearsoncorr']
     1022ostatsn = ['number of points', 'start', 'end', 'minimum', 'maximum', 'mean',         \
     1023  'mean2', 'standard deviation']
    10201024
    10211025parser = OptionParser()
     
    13341338newdim = onewnc.createDimension('xaround',Ngrid*2+1)
    13351339newdim = onewnc.createDimension('yaround',Ngrid*2+1)
     1340newdim = onewnc.createDimension('gstats',9)
    13361341newdim = onewnc.createDimension('stats',5)
     1342newdim = onewnc.createDimension('tstats',8)
    13371343
    13381344# Variable dimensions
     
    13501356basicvardef(newvar, 'statistics', 'statitics from values', '-')
    13511357writing_str_nc(newvar, statsn, StringLength)
     1358
     1359newvar = onewnc.createVariable('gstatistics', 'c', ('gstats','StrLength'))
     1360basicvardef(newvar, 'gstatistics', 'global statitics from values', '-')
     1361writing_str_nc(newvar, gstatsn, StringLength)
     1362
     1363newvar = onewnc.createVariable('tstatistics', 'c', ('tstats','StrLength'))
     1364basicvardef(newvar, 'tstatistics', 'statitics from values along time', '-')
     1365writing_str_nc(newvar, ostatsn, StringLength)
    13521366
    13531367if obskind == 'trajectory':
     
    14021416
    14031417    ovobs = oobs.variables[valvars[ivar][1]]
     1418
     1419# Simulated values spatially around coincident times
    14041420    if dims.has_key('Z'):
    14051421        simobsSvalues = np.zeros((Ncoindt, Ngrid*2+1, Ngrid*2+1, Ngrid*2+1),         \
     
    14071423    else:
    14081424        simobsSvalues = np.zeros((Ncoindt, Ngrid*2+1, Ngrid*2+1), dtype = np.float)
     1425
     1426# Observed values temporally around coincident times
     1427    simobsTvalues = {}
     1428    simobsTtvalues = np.zeros((Ncoindt,2), dtype=np.float)
    14091429
    14101430    if obskind == 'multi-points':
     
    14391459            slicevar, dimslice = slice_variable(ovsim, slicev)
    14401460            simobsSvalues[it,:,:] = slicevar
     1461
     1462            if it == 0:
     1463                itoi = 0
     1464                itof = int(coindtvalues[it,1]) / 2
     1465            elif it == Ncoindt-1:
     1466                itoi = int( (ito + int(coindtvalues[it-1,1])) / 2)
     1467                itof = int(coindtvalues[it,1])
     1468            else:
     1469                itod = int( (ito - int(coindtvalues[it-1,1])) / 2 )
     1470                itoi = ito - itod
     1471                itod = int( (int(coindtvalues[it+1,1]) - ito) / 2 )
     1472                itof = ito + itod
     1473
     1474            slicev = dims['T'][1] + ':' + str(itoi) + '@' + str(itof + 1)
     1475
     1476            slicevar, dimslice = slice_variable(ovobs, slicev)
     1477            simobsTvalues[str(it)] = slicevar
     1478
     1479            simobsTtvalues[it,0] = valdimobs['T'][itoi]
     1480            simobsTtvalues[it,1] = valdimobs['T'][itof]
    14411481
    14421482    elif obskind == 'trajectory':
     
    15321572
    15331573# statisics obs
    1534     obsmask = ma.masked_equal(arrayvals, fillValueF)
     1574    obsmask = ma.masked_equal(arrayvals[:,1], fillValueF)
    15351575    obsmask2 = obsmask*obsmask
    1536 
    1537     print 'Lluis',obsmask
    15381576
    15391577    obsstats = np.zeros((5), dtype=np.float)
     
    15451583
    15461584# Statistics sim-obs
    1547     simobsstats = np.zeros((5), dtype=np.float)
     1585    simobsstats = np.zeros((9), dtype=np.float)
    15481586    diffvals = np.zeros((Ncoindt), dtype=np.float)
    15491587
    1550     diffvals = arrayvals[:,0] - arrayvals[:,1]
     1588    diffvals = arrayvals[:,0] - obsmask
     1589
    15511590    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 
    1559 # Statistics around values
     1591    simobsstats[1] = np.mean(arrayvals[:,0]*obsmask)
     1592    simobsstats[2] = np.min(diffvals)
     1593    simobsstats[3] = np.max(diffvals)
     1594    simobsstats[4] = np.mean(diffvals)
     1595    simobsstats[5] = np.mean(np.abs(diffvals))
     1596    simobsstats[6] = np.sqrt(np.mean(diffvals*diffvals))
     1597    simobsstats[7], simobsstats[8] = sts.pearsonr(arrayvals[:,0], arrayvals[:,1])
     1598
     1599# Statistics around sim values
    15601600    aroundstats = np.zeros((5,Ncoindt), dtype=np.float)
    15611601    for it in range(Ncoindt):
     
    15661606        aroundstats[4,it] = np.sqrt(aroundstats[3,it] - aroundstats[2,it]*           \
    15671607          aroundstats[2,it])
     1608
     1609# Statistics around obs values
     1610    aroundostats = np.zeros((8,Ncoindt), dtype=np.float)
     1611
     1612    for it in range(Ncoindt):
     1613        obsmask = ma.masked_equal(simobsTvalues[str(it)], fillValueF)
     1614        obsmask2 = obsmask*obsmask
     1615
     1616        aroundostats[0,it] = len(obsmask.flatten())
     1617        aroundostats[1,it] = simobsTtvalues[it,0]
     1618        aroundostats[2,it] = simobsTtvalues[it,1]
     1619        aroundostats[3,it] = obsmask.min()
     1620        aroundostats[4,it] = obsmask.max()
     1621        aroundostats[5,it] = obsmask.mean()
     1622        aroundostats[6,it] = obsmask2.mean()
     1623        aroundostats[7,it] = np.sqrt(aroundostats[6,it] - aroundostats[5,it]*        \
     1624          aroundostats[5,it])
    15681625
    15691626# Values to netCDF
     
    15901647        newvar[:] = simobsSvalues
    15911648
    1592 # Statistics
    1593     if not searchInlist(onewnc.variables,valvars[ivar][0]):
     1649# sim Statistics
     1650    if not searchInlist(onewnc.variables,valvars[ivar][0] + 'stsim'):
     1651        newvar = onewnc.createVariable(valvars[ivar][0] + 'stsim', 'f', ('stats'),   \
     1652          fill_value=fillValueF)
     1653        descvar = 'simulated statisitcs: ' + valvars[ivar][0]
     1654        basicvardef(newvar, valvars[ivar][0] + 'stsim', descvar, ovobs.getncattr('units'))
     1655        newvar[:] = simstats
     1656
     1657# obs Statistics
     1658    if not searchInlist(onewnc.variables,valvars[ivar][1] + 'stobs'):
     1659        newvar = onewnc.createVariable(valvars[ivar][1] + 'stobs', 'f', ('stats'),   \
     1660          fill_value=fillValueF)
     1661        descvar = 'observed statisitcs: ' + valvars[ivar][1]
     1662        basicvardef(newvar, valvars[ivar][1] + 'stobs', descvar,                     \
     1663          ovobs.getncattr('units'))
     1664        newvar[:] = obsstats
     1665
     1666# sim-obs Statistics
     1667    if not searchInlist(onewnc.variables,varsimobs + 'st'):
     1668        newvar = onewnc.createVariable(varsimobs + 'st', 'f', ('gstats'),            \
     1669          fill_value=fillValueF)
     1670        descvar = 'simulated-observed statisitcs: ' + varsimobs
     1671        basicvardef(newvar, varsimobs + 'st', descvar, ovobs.getncattr('units'))
     1672        newvar[:] = simobsstats
     1673
     1674# around sim Statistics
     1675    if not searchInlist(onewnc.variables,valvars[ivar][0] + 'staround'):
    15941676        newvar = onewnc.createVariable(valvars[ivar][0] + 'staround', 'f',           \
    15951677          ('time','stats'), fill_value=fillValueF)
    1596         descvar = 'around simulated statisitcs: ' + valvars[ivar][0]
    1597         basicvardef(newvar, varsimobs + 'staround', descvar, ovobs.getncattr('units'))
     1678        descvar = 'around (' +  str(Ngrid) + ', ' + str(Ngrid) +                     \
     1679          ') simulated statisitcs: ' + valvars[ivar][0]
     1680        basicvardef(newvar, valvars[ivar][0] + 'staround', descvar,                  \
     1681          ovobs.getncattr('units'))
    15981682        newvar[:] = aroundstats.transpose()
     1683
     1684# around obs Statistics
     1685    if not searchInlist(onewnc.variables,valvars[ivar][1] + 'staround'):
     1686        newvar = onewnc.createVariable(valvars[ivar][1] + 'staround', 'f',           \
     1687          ('time','tstats'), fill_value=fillValueF)
     1688        descvar = 'around temporal observed statisitcs: ' + valvars[ivar][1]
     1689        basicvardef(newvar, valvars[ivar][1] + 'staround', descvar,                  \
     1690          ovobs.getncattr('units'))
     1691        newvar[:] = aroundostats.transpose()
    15991692
    16001693        onewnc.sync()
Note: See TracChangeset for help on using the changeset viewer.