DJF 的年度季节性总和

问题描述 投票:0回答:2

我想为 DJF 的气象术语创建总和,这意味着 12 月的值来自 x-1 年。

已经有一个建议,使用包 seaszoo 来解决我的问题:参考链接。我可以使用关于动物园对象的时间索引的循环来获取每年和不同列的冬季总和吗?我的样本数据中已经只有冬季月份了:

structure(c(0.335767631885527, 0.329964137686826, 0.324867678295622, 
0.346234032749876, 0.315486588076342, 0.373440783616547, 0.393108355980974, 
0.310526442402042, 0.955068399718777, 0.959654624426492, 0.293930575800507, 
0.350949140946517, 0.657761387039141, 0.53822087533681, 0.296938223280703, 
0.318325593619261, 0.827528522109129, 0.914084376992577, 0.914209302937996, 
0.913163846516007, 0.776698687524975, 0.597284692104539, 0.91488961230643, 
0.28945161773974, 0.282895617679457, 0.28492139335934, 0.928492227792593, 
0.287740157404564, 0.93011080075256, 0.32787462005944, 0.809245564874419, 
0.299095322129539, 0.302473955104931, 0.453458703894119, 0.331724139938735, 
0.314265997270211, 0.378968117507553, 0.344955599135117, 0.961200295699775, 
1.07300929383762, 0.339365254133058, 0.421999171190298, 0.351276824906379, 
0.36810350819186, 0.364237601690115, 0.425751222495895, 1.2000504740503, 
0.401585883450189, 0.393244206959102, 0.412013522316855, 1.40622761554481, 
1.43010692801434, 1.45452312391606, 1.44102848262452, 0.583854512560274, 
0.453530324821785, 0.836929179095723, 0.485649439571136, 1.45323622566975, 
1.42066532567401, 1.55192692063172, 1.69545734226667, 1.59084952877426, 
0.536277991651981, 0.878100994910164, 1.80588869793109, 0.612726668114702, 
1.49557275883036, 1.83080789724595, 0.859368961826519, 1.3537163175202, 
0.795003445956722, 1.68510799767645, 1.94219078558463, 0.678911636490617, 
1.98538116097216, 1.39431924099171, 0.716178198907659, 0.897864731079577, 
0.739754008960108, 1.32647638785145, 1.27550346512974, 1.57782298324095, 
1.17541538713537, 1.08141388070016, 2.81373485339402, 0.841584582588819, 
2.98872530454666, 1.93484656658214, 3.01625884992721, 0.902448663673698, 
0.361944635028181, 1.03795562218241, 0.961881521906292, 0.704732279822006, 
0.894256898010956, 0.307197052425753, 0.620230669033494, 0.900835004143219, 
0.336503062729966, 0.376726235662507, 0.323019953443342, 0.291097473211189, 
0.583926906347703, 0.540940525007957, 0.906358816314195, 0.372788957369332, 
0.335375002309946, 0.914209302937996, 0.328320596067713, 0.659589829678685, 
0.68859386616471, 0.91488961230643, 0.902977019532625, 0.739324647975471, 
0.603576498397486, 0.690375139214112, 0.603004583921208, 0.659868379563069, 
0.292376232645021, 0.562401086780579, 0.298131207627614, 0.299095322129539, 
0.302473955104931, 0.705840069893102, 0.993644273952054, 0.425326528868129, 
0.400345928302124, 0.361221494378293, 0.328750601711733, 0.55820945179875, 
0.748093576785292, 0.345188978576, 0.351315165819748, 0.357626992140137, 
0.517538802067647, 1.04751086637289, 0.385695811626645, 0.385612146149294, 
0.397271280188057, 0.550298801906058, 1.28131889629393, 0.82396230266283, 
1.03189532043667, 0.502923809446499, 1.13388533378536, 0.821249922028902, 
0.496130920693478, 0.491056299113018, 0.861144623672965, 0.498763665924562, 
0.912165347541201, 0.64869230436972, 1.32528603957948, 1.75339437114229, 
1.78285803283739, 1.11217610098546, 0.597795159831033, 1.00740416004752, 
0.739549658487185, 0.607139331936484, 1.35734916834937, 1.43608105985186, 
1.80042779869959, 1.18905308118327, 1.70456429994882, 0.905541925940458, 
2.22398340066076, 2.16944665030202, 2.29546486372867, 1.85605245367111, 
1.1239234690604, 2.50480944519147, 1.02954245959557, 0.975126362552554, 
2.14223132835323, 2.91282474285556, 2.66863827732602, 0.933593864631134, 
2.70815814163342, 2.87351062547491, 0.335329222971355, 0.934907402460015, 
0.57591904762801, 0.907224647738403, 0.320417497402957, 0.766767831651282, 
0.861903342837008, 0.303464733511709, 0.709698376015027, 0.308598232977547, 
0.293930575800507, 0.29130992351097, 0.28896933229556, 0.45769807141885, 
0.468340431926149, 0.830040974016766, 0.282420179745874, 0.477428977916008, 
0.733418492651481, 0.822348309121175, 0.280392410026905, 0.542239475756514, 
0.281077879631808, 0.281845318148658, 0.42849080424256, 0.295089908538224, 
0.747925637213591, 0.929814463524078, 0.310954657683433, 0.292376232645021, 
0.64500798819687, 0.690255336889303, 0.364309565584761, 0.306129346468766, 
0.311371964852598, 0.915461004824963, 0.397063771122394, 1.0404933625801, 
0.483845551843616, 0.333807374425717, 0.402255447456447, 0.453946781602374, 
0.394538152500142, 0.357626992140137, 0.364237601690115, 0.372020526598045, 
0.37823224873185, 0.389581791596903, 0.393244206959102, 0.401126173348066, 
0.563948059226945, 0.625538021242673, 0.80823517471131, 0.440809452269821, 
0.753920921570439, 0.571583127323145, 0.463092290982252, 0.576935449307388, 
0.482901053437729, 1.40965077473646, 1.25183016539419, 0.856169846501004, 
1.72377824975207, 0.536277991651981, 1.13652692119597, 1.24290457699823, 
1.64437171023011, 1.87302947654355, 0.594841647571458, 2.04410190051534, 
1.62571002130845, 1.13052139459963, 0.836130011762252, 1.85233449007414, 
2.38839794838805, 1.09920265799031, 1.94766079436355, 1.66770758466983, 
1.27453119791191, 2.57917818578189, 1.13896219096471, 2.74804359878488, 
1.69823856330245, 0.935150681359782, 1.74656095016161, 0.835168244061429, 
0.841584582588819, 0.856635868155615, 0.972724285567558, 2.42939239419398, 
0.96325679668782, 0.640892567004161, 1.03795562218241, 0.949309568900219, 
0.316910844084317, 0.311204732481577, 0.307197052425753, 0.303464733511709, 
0.779574150582344, 0.296830513889512, 0.335960010735195, 0.4390886067335, 
0.28896933229556, 0.306902835898889, 0.926150657204963, 0.388532344331494, 
0.495283643343666, 0.916064063737401, 0.281013296117892, 0.913163846516007, 
0.912928724576721, 0.438926937515807, 0.59117658733228, 0.517844090756594, 
0.704234100156676, 0.913848110190877, 0.423829975580762, 0.795497269555325, 
0.289917958593354, 0.292376232645021, 0.295114252321699, 0.345353147959634, 
0.854886103409894, 0.62965115658928, 0.776701146370991, 0.446059142229343, 
0.326457042618417, 0.568752212327844, 0.325374322793979, 0.374762702815228, 
0.333807374425717, 0.420206697512664, 0.399408381034396, 0.456977698650331, 
0.357626992140137, 0.596680957599271, 1.29550961397828, 1.24265117031916, 
0.580164026815441, 0.393244206959102, 0.401126173348066, 0.443462006528755, 
0.417630422649225, 0.426247823064678, 0.505363855323395, 0.494595916530596, 
1.12922054709106, 0.482617341273223, 0.650774092876326, 0.5452273225038, 
1.61305811763483, 1.66701808699342, 0.514281824935098, 0.525174470147384, 
1.6850349371761, 1.78354241230912, 1.83460579403794, 1.86582069105335, 
1.40279004365455, 0.594841647571458, 0.691585610303159, 0.619623644706909, 
2.06846657922012, 0.710726446010795, 0.997307890433014, 2.40064963745822, 
2.22161516025196, 1.79188547652641, 2.19553900228869, 2.1816869110449, 
2.1984531582332, 2.55364304827728, 0.918827215513173, 0.930267750935017, 
0.798812034349413, 0.830829315142733, 1.13089106389005, 1.00204606351463, 
1.07126361979325, 0.871799972892206, 1.28166129954517), .Dim = c(181L, 
2L), .Dimnames = list(NULL, NULL), index = structure(c(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, 1065, 
1066, 1067, 1068, 1069, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 
1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087, 
1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 
1099, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 
1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 
1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 
1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 1140, 1141, 1142, 
1143, 1144, 1145, 1146, 1147, 1148, 1149, 1150, 1151, 1152, 1153, 
1154), class = "Date"), class = "zoo")
r time-series zoo
2个回答
1
投票
library(hydroTSM)

dm2seasonal(df, FUN=sum, season="DJF")

我使用了包HydroTSM。该套件还可用于其他季节(MAM、JJA、SON)和其他功能(例如mean)。您可以计算矩阵中每一列的年度季节性总和 (df)。 seas 对每一列执行相同的操作,但我猜你必须编写自己的循环才能获得年度季节性总和。来自 seasmkseas() 将计算时间序列中所有冬季月份的总和。


0
投票

您显示的

dim_names()
表明这是遵循 CF 元数据约定的数据(这是气候变化模型数据吗?):使用特定单位(例如小时或天)与基础数据的数值偏移。在这种情况下,您可以使用 CFtime 包,您可以轻松地将原始偏移量转换为遵循 CF 日历的时间戳,并为气象季节创建一个因素。

install.packages("CFtime")
library(CFtime)
library(ncdf4)

# Open the data file and build the time series from the CF calendar
nc <- nc_open("./my_data.nc")
cf <- CFtime(nc$dim$time$units, nc$dim$time$calendar, nc$dim$time$vals)

# Create a factor for the meteorological seasons
# The factor will have levels like "2021-DJF", "2021-MAM", ...
fac <- CFfactor(cf, "season")

# Read the data from the file and process with the factor
data <- ncvar_get(nc, "var")                  # whatever your variable is
nc_close(nc)
result <- apply(data, 1:2, tapply, fac, mean) # pick your own function
result <- aperm(result, c(2, 3, 1))           # rearrange dimensions

使用

CFfactor()
函数,您可以获取时间序列中每年的气象季节,但您也可以定义一个特定时期(例如 2041 - 2060 年),然后获取一个因子以对季节数据应用函数在那段时期。在后一种情况下,您可以获得该期间的平均值,然后可能在基线期间进行比率来计算异常。

© www.soinside.com 2019 - 2024. All rights reserved.