简体   繁体   English

如何交换 x 轴和 y 轴上的纬度和经度?

[英]How to swap latitude and longitude on x and y axes?

I've uploaded my data onto github here: https://github.com/dschmidt42/Benguela_Chla_Danielle_Schmidt .我已将我的数据上传到 github: https://github.com/dschmidt42/Benguela_Chla_Danielle_Schmidt

It's concerning chl a data off the eastern coast of Africa and spans 26 years.它涉及非洲东海岸的 chl a 数据,时间跨度为 26 年。 I am trying to split the data into each month and then compare the variance within months.我试图将数据分成每个月,然后比较几个月内的差异。 This is my first time working with a.netCDF file and I'm having a lot of issues.这是我第一次使用 .netCDF 文件,我遇到了很多问题。 The main one at the moment is that my longitude is on the x axis and latitude is on the y axis.目前最主要的是我的经度在 x 轴上,纬度在 y 轴上。 How do I swap these?我如何交换这些?

What I've done我做了什么

nc_chla_file <- "cmems_mod_glo_bgc_my_0.25_P1M-m_1673881119174.nc"
nc <- nc_open(nc_chla_file)

Here are the dimensions for longitude, latitude, depth, and months respectively.这里分别是经度、纬度、深度和月份的维度。 I only need depth 1 as that is the closest to the surface.我只需要深度 1,因为它离表面最近。

dim(ncvar_get(nc,varid="chl"))

Then I've separated chl data for January and turned it into a raster.然后我分离了 1 月份的 chl 数据并将其转换为栅格。

chl_jan <- ncvar_get(nc,varid="chl")[,,1,seq(1,312,12)]
chl_jan_rast <- rast(chl_jan)

Lastly I've taken the variance and made a plot.最后,我采用了差异并制作了 plot。

variance_chl_jan <- terra::app(chl_jan_rast, fun = "var")
plot(variance_chl_jan, main = "Variance in chl a in January")

在此处输入图像描述

As you can see the latitude and longitude are the wrong way round.如您所见,纬度和经度是错误的。 How do I fix this?我该如何解决?

You can open the file like this:您可以像这样打开文件:

library(terra)
#terra 1.6.47
f <- "cmems_mod_glo_bgc_my_0.25_P1M-m_1673881119174.nc"
r <- rast(f)

Inspect检查

r
#class       : SpatRaster 
#dimensions  : 82, 48, 936  (nrow, ncol, nlyr)
#resolution  : 0.25, 0.25  (x, y)
#extent      : 8.875, 20.875, -35.875, -15.375  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : cmems_mod_glo_bgc_my_0.25_P1M-m_1673881119174.nc 
#varname     : chl (Total Chlorophyll) 
#names       : chl_d~001_1, chl_d~855_1, chl_d~819_1, chl_d~001_2, chl_d~855_2, chl_d~819_2, ... 
#unit        :      mg m-3,      mg m-3,      mg m-3,      mg m-3,      mg m-3,      mg m-3, ... 
#time        : 1994-01-16 12:00:00 to 2019-12-16 12:00:00 UTC 

names(r)[1:3]
#[1] "chl_depth=0.50576001_1" "chl_depth=1.555855_1"   "chl_depth=2.6676819_1" 

You can get the layer numbers for the depth you want like this您可以像这样获得所需深度的层号

i <- grep("chl_depth=0.5", names(r))
#or like this 
i <- seq(1, nlyr(r), 3) 

And subset SpatRaster r和子集 SpatRaster r

r1 <- r[[i]]

To get the variance within each month, you can now do要获得每个月内的差异,您现在可以这样做

vm <- tapp(r1, "month", var)

vm
#class       : SpatRaster 
#dimensions  : 82, 48, 12  (nrow, ncol, nlyr)
#resolution  : 0.25, 0.25  (x, y)
#extent      : 8.875, 20.875, -35.875, -15.375  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source(s)   : memory
#names       :          m_1,          m_2,          m_3,          m_4,          m_5,          m_6, ... 
#min values  : 0.0001622432, 2.360285e-05, 1.136451e-05, 4.821852e-05, 0.0002333084, 0.0009578676, ... 
#max values  : 3.0582257258, 3.222419e+00, 3.880621e+00, 4.295268e+00, 4.2899640566, 3.3797369950, ... 
#time (mnts) : Jan to Dec 

plot(vm)

在此处输入图像描述

Doing things "by hand" like you do is more error-prone, but you could fix your result by doing像你一样“手工”做事更容易出错,但你可以通过做来修复你的结果

vcj <- flip(t(variance_chl_jan), "v")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 X 和 Y 坐标转换为纬度和经度? - How to convert X and Y coordinates into Latitude and longitude? 如何将经度和纬度设置为 map 的 ggplot 的轴? - How do I set longitude and latitude as the axes for this ggplot of a map? 纬度和经度,x和y,以及在绘制时的差异:地球层和ggmap - Latitude and Longitude, x and y, and differences when plotting them: geosphere and ggmap 如何在 ggraph R 中翻转 x 和 y 轴 - How to flip x and y axes in ggraph R 将 GPS 经纬度跟踪数据转换为足球场上的位置 x 和位置 y? - Convert GPS latitude and longitude tracking data to a position x and position y on a football pitch? 如何将R中的纬度(行)x经度(列)数据框更改为纬度(第1列),经度(第2列),值(第3列)数据框? - How to change a latitude (rows) x longitude (columns) data frame into a latitude (column 1), longitude (column 2), value (column 3) data frame in R? 将经纬度坐标(WGS)转换为等距轴的网格(在给定区域中) - Convert longitude latitude coordinates (WGS) to grid with equidistant axes (in given area) 如何在ggplot图中绘制y和x轴的刻度和数量? - How to plot ticks and numbers of y and x axes in a ggplot graph? 如何删除R中的第二个x和y轴? - How do I remove the second x and y axes in R? 如何在ggiNEXT图形中编辑X和Y轴的标签? - How can I edit the labels of the X and Y axes in a figure of ggiNEXT?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM