WRF土地利用数据替换

WRF更换土地利用数据



前言

近期利用WRF进行辐照度模拟,苦于精度提升慢,所以想利用中国土地利用数据(1980-2015)的土地利用数据替换wrf内老旧的土地利用数据,以提高WRF模拟精度。一做记录、二做共享。如有不妥,欢迎大家批评指正!

一、数据下载

打开中国土地利用数据(1980-2015),点下载按钮即可下载1980-2015年土地利用数据。

在这里插入图片描述

二、GRID转tiff

下载的lucc2015数据如下图所示

在这里插入图片描述

这种格式的数据我不知道怎么用python读取,但是在ArcGIS中可以打开。通过属性表可以看到,坐标为投影坐标系Krasovsky_1940_Albers,数据格式为GRID。为了方便后续操作我们先将数据格式另存为tiff:

右击数据–>Data–>Export Data将数据另存为tiff格式。

在这里插入图片描述

在这里插入图片描述

三、重投影

这一步的目的是将数据的投影坐标系Krasovsky_1940_Albers转为地理坐标系WGS84。
打开ArcToolbox–>Projections and Transformations–>Raster–>Project Raster。选中我们刚刚另存的lucc2015栅格数据,并将坐标系统重新定义为地理坐标系WGS 84。转换成功后在属性表中可以看到坐标系统变为WGS 84。

在这里插入图片描述

四、重分类

目前WRF只能输入USGS-24分类、IGBP-20分类或IGBP-21分类体系,因此需要对土地利用数据进行重新分类,分类原则参考GISer凌,如下图所示:

在这里插入图片描述

重分类python代码如下:

import os
from osgeo import gdal
import numpy as np
def read_tif(tifFile):
    tif = gdal.Open(tifFile)
    XSize = tif.RasterXSize  # 影像列数
    YSize = tif.RasterYSize
    projection = tif.GetProjection()  # 投影信息
    geotransform = tif.GetGeoTransform()  # 仿射矩阵
    bandnum=tif.RasterCount
    data = tif.GetRasterBand(1)
    nodata = data.GetNoDataValue()
    tifdata = data.ReadAsArray(0, 0, XSize, YSize).astype(float)
    return XSize,YSize,projection,geotransform,bandnum,nodata,tifdata
def reclass(data):
    # data = np.where(data == nodata, np.nan, data)  # 仿射矩阵
    data = np.where((data == 51) | (data == 53) | (data == 54), 1, data)
    data = np.where((data == 11) | (data == 12) | (data == 52), 3, data)
    data = np.where((data == 31) | (data == 32) | (data == 33) | (data == 34), 7, data)
    data = np.where((data == 23) | (data == 22), 8, data)
    data = np.where((data == 24) | (data == 25), 9, data)
    data = np.where((data == 21), 14, data)
    data = np.where((data == 41) | (data == 42) | (data == 43) | (data == 46) | (data == 99), 16, data)
    data = np.where((data == 45) | (data == 64), 17, data)
    data = np.where((data == 61) | (data == 62) | (data == 63) | (data == 65) | (data == 67), 19, data)
    data = np.where((data == 66), 23, data)
    data = np.where((data == 44), 24, data)
    return data
# 输出tif
def creattif(DriverName, out_np, XSize, YSize, Bandnum, datatype, geotransform, projection, nodata, data):
    driver = gdal.GetDriverByName(DriverName)
    # dst_ds = driver.Create( dst_filename, 512, 512, 1, gdal.GDT_Byte )这句就创建了一个图像。宽度是512*512,单波段,数据类型是Byte这里要注意,它少了源数据,因为这里用不到源数据。它创建了一个空的数据集。要使它有东西,需要用其他步骤往里头塞东西。
    new_dataset = driver.Create(out_np, XSize, YSize, Bandnum, datatype)  # band[str(i)]
    new_dataset.SetGeoTransform(geotransform)  # 写入仿射变换参数
    new_dataset.SetProjection(projection)
    band_out = new_dataset.GetRasterBand(1)
    # if nodata is None or nodata is np.nan:
    #     nodata = -9999
    band_out.WriteArray(data)  # 写入数组数据
    band_out.SetNoDataValue(nodata)
    if DriverName == "GTiff":
        band_out.ComputeStatistics(True)
    del new_dataset
def main(intifFile,outpath,outname):
    XSize,YSize,projection,geotransform,bandnum,nodata,tifdata=read_tif(intifFile)
    newdata=reclass(tifdata)
    datatype=gdal.GDT_Int32
    DriverName = "GTiff"
    outpn=os.path.join(outpath, outname)
    creattif(DriverName, outpn, XSize, YSize, bandnum, datatype, geotransform, projection, nodata, newdata)
if '__main__' in __name__:
    intifFile = r'E:\lucc2015\lucc2015_wgs84.tif'  #输入tif数据
    outpath=r'E:\lucc2015'  #输出路径
	outname='lucc2015_wgs84_reclass.tif'
    main(intifFile,outpath,outname)

五、tiff格式转二进制

将我们分类好的lucc2015栅格数据转为二进制格式才能在WRF中使用,将分类好的lucc2015拷贝到Ubuntu中的WPS_GEOG文件夹下的lucc2015文件夹下,如下图所示。

在这里插入图片描述

使用命令:gdal_translate -of ENVI -co INTERLEAVE=BSQ lucc2015_wgs84_reclass.tif data.bil将tif文件转为二进制文件。其中lucc2015_wgs84_reclass.tif为需要转换的文件,data.bil为转换的目标文件。(需要安装GDAL,如果没有安装,按报错提示安装即可sudo apt install gdal-bin)

在这里插入图片描述

运行成功后会在文件夹下生成data.bil、data.bil.aux.xml和data.hdr三个文件:

在这里插入图片描述

查看data.hdr头文件提供的一些基本信息,其中samples为数据的列数,lines为数据的行数,map info中的0.0117836266203959,0.0117836266203959分别为数据的横向和纵向网格分辨率。

ENVI
description = {
data.bil}
samples = 6157
lines   = 3393
bands   = 1
header offset = 0
file type = ENVI Standard
data type = 3
interleave = bsq
byte order = 0
map info = {Geographic Lat/Lon, 1, 1, 66.2922997247769, 54.9785484716575, 0.0117836266203959, 0.0117836266203959,WGS-84}
coordinate system string = {GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]}
band names = {
Band 1}

在lucc2015中新建index文件。根据data.hdr头文件和数据内容,对index文件进行如下设置:

type=categorical
category_min=1
category_max=24
projection=regular_ll
dx=0.0117836266203959
dy=0.0117836266203959
known_x=1.0
known_y=1.0
known_lat=14.9967033487
known_lon=66.2922997248
wordsize=1
tile_x=6157
tile_y=3393
tile_z=1
units="category"
description="USGS 24-category land use categories"
mminlu="USGS"
missing_value=128
iswater=16
islake=-1
isice=24
isurban=1
row_order=top_bottom

注意:
type:为文件描述类型
category_min:分类代码的最小值
category_max:分类代码的最大值
projection:投影类型
dx:横向格点间的间隔,即栅格影像的横向分辨率 #与data.hdr文件中横向网格分辨率保持一致
dy:纵向格点间的间隔,即栅格影像的纵向分辨率 #与data.hdr文件中纵向网格分辨率保持一致
known_x:指定一个标记点横向坐标
known_y:指定一个标记点纵向坐标
known_lat:标记点横向坐标的纬度 #tiff文件左下角的纬度,在ArcGIS中打开数据的属性查看
known_lon:标记点纵向坐标的经度 #tiff文件左下角的经度,在ArcGIS中打开数据的属性查看
tile_x:横向格点数 #参考ArcGIS中的列数
tile_y:纵向格点数 #参考ArcGIS中的行数
units:格点值的单位
description:文件描述
iswater:水体类别的编号 #查看自己重分类后的水体类别编号
islake:湖泊类别的编号 #查看自己重分类后的湖泊类别编号
isice:冰川类别的编号 #查看自己重分类后的冰川类别编号
isurban:城市类别的编号 #查看自己重分类后的城市类别编号
missing_value:缺省值是128。
在编辑完index文件后,将data.bil重命名为00001-06157.00001-03393,其中6157为lucc2015的列数,3393为lucc2015的行数,这样tiff转换二进制就成功了。如下图:
在这里插入图片描述

六、修改GEOGRID.TBL

在WPS安装目录中的geogrid文件夹下,找到GEOGRID.TBL文件,打开后找到name=LANDUSEF项,分别在每一项的后面面加上

landmask_water = lucc2015   # Calculate a landmask from this field
interp_option = lucc2015:nearest_neighbor
rel_path = lucc2015:lucc2015/

然后保存即可。如下图:

在这里插入图片描述

七、修改namelist.wps

土地利用数据制作完成之后,需要在namelist.wps中设置geog_data_res=‘usgs_30s+default’,‘lucc2015+default’,‘lucc2015+default’,我的namelist.wps设置如下所示:

在这里插入图片描述

注:
(1) 如果您希望使用 USGS 静态地面数据,而不是默认的 MODIS数据,则需要将geog_data_res指定为:
geog_data_res = ‘usgs_30s+default’, ‘usgs_30s+default’,、
(2) 在 V3.8 之前,USGS 是默认的。如果您使用的是 V3.8 之前的 WPS 版本,并且希望使用默认的 USGS 数据,则只需指定:
geog_data_res = ‘10m’, ‘2m’, ‘30s’
(3) 在 V3.8 之前,如果您希望使用 MODIS数据,则需要指定:
geog_data_res = ‘modis_30s+10m’, ‘modis_30s+2m’, ‘modis_30s+30s’
(4) 可用分辨率为 10 m(~19 公里)、5m(~9 公里)、2 m(~4 公里)和 30 s(~0.9 公里)。

八、运行geogrid.exe

运行完后,在WPS中生成以下三个文件(因为我是三层嵌套)。

在这里插入图片描述

后面就可以用新的土地利用数据去做测试啦!

这个错误提示表明在训练YOLO模型时,找不到训练数据集中的标签文件。解决这个问题的方法如下: 1. 确认标签文件是否存在,标签文件应该与图像文件在同一目录下,且文件名相同,只是扩展名不同。标签文件的扩展名通常为.txt,每个文件应包含与其对应的图像文件中所有对象的标签信息。 2. 确认标签文件的格式是否正确。YOLO模型要求标签文件的格式为每行一个对象,每行包含对象的类别和位置信息。位置信息应该是相对于图像宽度和高度的归一化坐标,即左上角和右下角的坐标值应该在0到1之间。 3. 确认训练脚本中的数据集路径和标签文件路径是否正确。如果数据集路径或标签文件路径不正确,就会导致找不到标签文件的错误。 4. 修改datasets.py文件。在该文件中,需要将标签文件的路径替换为正确的路径。具体来说,需要将datasets.py文件中的JPEGImages替换为标签文件所在的目录。 以下是修改后的datasets.py文件的示例代码: ```python import glob import os import numpy as np import torch from PIL import Image from torch.utils.data import Dataset class LoadImagesAndLabels(Dataset): # for training/testing def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False): path = str(Path(path)) # os-agnostic assert os.path.isfile(path), f'File not found {path}' with open(path, 'r') as f: self.img_files = [x.replace('\n', '') for x in f.readlines() if os.path.isfile(x.replace('\n', ''))] assert self.img_files, f'No images found in {path}' self.label_files = [x.replace('images', 'labels').replace('.png', '.txt').replace('.jpg', '.txt') .replace('.jpeg', '.txt') for x in self.img_files] self.img_size = img_size self.batch_size = batch_size self.augment = augment self.hyp = hyp self.rect = rect self.image_weights = image_weights self.cache_images = cache_images self.single_cls = single_cls def __len__(self): return len(self.img_files) def __getitem__(self, index): img_path = self.img_files[index % len(self.img_files)].rstrip() label_path = self.label_files[index % len(self.img_files)].rstrip() # Load image img = None if self.cache_images: # option 1 - caches small/medium images img = self.imgs[index % len(self.imgs)] if img is None: # option 2 - loads large images on-the-fly img = Image.open(img_path).convert('RGB') if self.cache_images: if img.size[0] < 640 or img.size[1] < 640: # if one side is < 640 img = img.resize((640, 640)) # resize self.imgs[index % len(self.imgs)] = img # save assert img.size[0] > 9, f'Width must be >9 pixels {img_path}' assert img.size[1] > 9, f'Height must be >9 pixels {img_path}' # Load labels targets = None if os.path.isfile(label_path): with open(label_path, 'r') as f: x = np.array([x.split() for x in f.read().splitlines()], dtype=np.float32) # Normalized xywh to pixel xyxy format labels = x.copy() if x.size > 0: labels[:, 1] = x[:, 1] * img.width # xmin labels[:, 2] = x[:, 2] * img.height # ymin labels[:, 3] = x[:, 3] * img.width # xmax labels[:, 4] = x[:, 4] * img.height # ymax labels[:, 1:5] = xywh2xyxy(labels[:, 1:5]) # xywh to xyxy targets = torch.zeros((len(labels), 6)) targets[:, 1:] = torch.from_numpy(labels) # Apply augmentations if self.augment: img, targets = random_affine(img, targets, degrees=self.hyp['degrees'], translate=self.hyp['translate'], scale=self.hyp['scale'], shear=self.hyp['shear'], border=self.img_size // 2) # border to remove # Letterbox img, ratio, pad = letterbox(img, new_shape=self.img_size, auto=self.rect, scaleup=self.augment, stride=self.hyp['stride']) targets[:, 2:6] = xyxy2xywh(targets[:, 2:6]) / self.img_size / ratio # normalized xywh (to grid cell) # Load into tensor img = np.array(img).transpose(2, 0, 1) # HWC to CHW img = torch.from_numpy(img).to(torch.float32) # uint8 to fp16/32 targets = targets[torch.where(targets[:, 0] == index % len(self.img_files))] # filter by image index return img, targets, index, img_path def coco_index(self, index): """Map dataset index to COCO index (minus 1)""" return int(Path(self.img_files[index]).stem) - 1 @staticmethod def collate_fn(batch): img, label, _, path = zip(*batch) # transposed for i, l in enumerate(label): l[:, 0] = i # add target image index for build_targets() return torch.stack(img, 0), torch.cat(label, 0), path class LoadImages(Dataset): # for inference def __init__(self, path, img_size=640, stride=32, auto=True): path = str(Path(path)) # os-agnostic if os.path.isdir(path): files = sorted(glob.glob('%s/*.*' % path)) elif os.path.isfile(path): files = [path] else: raise Exception(f'Error: {path} does not exist') images = [x for x in files if os.path.splitext(x)[-1].lower() in img_formats] videos = [x for x in files if os.path.splitext(x)[-1].lower() in vid_formats] ni, nv = len(images), len(videos) self.img_size = img_size self.stride = stride self.auto = auto self.video_flag = [False] * ni + [True] * nv self.img_files = images + videos self.cap = [cv2.VideoCapture(x) for x in videos] self.frame = [None] * nv self.ret = [False] * nv self.path = path def __len__(self): return len(self.img_files) def __getitem__(self, index): if self.video_flag[index]: return self.load_video(index) else: return self.load_image(index) def load_image(self, index): img_path = self.img_files[index] img = cv2.imread(img_path) # BGR assert img is not None, 'Image Not Found ' + img_path h0, w0 = img.shape[:2] # orig hw img = letterbox(img, new_shape=self.img_size, auto=self.auto)[0] img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) return torch.from_numpy(img), index, img_path, (h0, w0) def load_video(self, index): cap = self.cap[index] while True: self.ret[index], frame = cap.read() if not self.ret[index]: break if self.frame[index] is None: self.frame[index] = letterbox(frame, new_shape=self.img_size, auto=self.auto)[0] self.frame[index] = self.frame[index][:, :, ::-1].transpose(2, 0, 1) self.frame[index] = np.ascontiguousarray(self.frame[index]) else: self.frame[index] = torch.cat((self.frame[index][self.stride:], letterbox(frame, new_shape=self.img_size, auto=self.auto)[0]), 0) if self.ret[index]: return self.frame[index], index, self.img_files[index], frame.shape[:2] def __del__(self): if hasattr(self, 'cap'): for c in self.cap: c.release() def letterbox(img, new_shape=640, color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32): # Resize and pad image while meeting stride-multiple constraints shape = img.shape[:2] # current shape [height, width] if isinstance(new_shape, int): ratio = float(new_shape) / max(shape) else: ratio = min(float(new_shape[0]) / shape[0], float(new_shape[1]) / shape[1]) if ratio != 1: # always resize down, only resize up if shape < new_shape * 1.5 if scaleup or (ratio < 1 and max(shape) * ratio > stride * 1.5): interp = cv2.INTER_LINEAR if ratio < 1: img = cv2.resize(img, (int(round(shape[1] * ratio)), int(round(shape[0] * ratio))), interpolation=interp) else: img = cv2.resize(img, (int(round(shape[1] * ratio)), int(round(shape[0] * ratio))), interpolation=interp) else: interp = cv2.INTER_AREA img = cv2.resize(img, (int(round(shape[1] * ratio)), int(round(shape[0] * ratio))), interpolation=interp) new_shape = [round(shape[1] * ratio), round(shape[0] * ratio)] # Compute stride-aligned boxes if auto: stride = int(np.ceil(new_shape[0] / stride) * stride) top_pad = (stride - new_shape[0]) % stride # add top-padding (integer pixels only) left_pad = (stride - new_shape[1]) % stride # add left-padding (integer pixels only) if top_pad or left_pad: img = cv2.copyMakeBorder(img, top_pad // 2, top_pad - top_pad // 2, left_pad // 2, left_pad - left_pad // 2, cv2.BORDER_CONSTANT, value=color) # add border else: stride = 32 top_pad, left_pad = 0, 0 # Pad to rectangular shape divisible by stride h, w = img.shape[:2] if scaleFill or new_shape == (w, h): # scale-up width and height new_img = np.zeros((new_shape[1], new_shape[0], 3), dtype=np.uint8) + color # whole image nh, nw = h, w else: # scale width OR height nh = new_shape[1] - top_pad nw = new_shape[0] - left_pad assert nh > 0 and nw > 0, 'image size < new_size' new_img = np.zeros((new_shape[1], new_shape[0], 3), dtype=np.uint8) + color # whole image if nw / w <= nh / h: # resize by width, then pad height new_w = new_shape[0] new_h = int(nh * new_w / nw) assert new_h > 0, 'image size < new_size' img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_LINEAR) top = top_pad // 2 bottom = top_pad - top left = left_pad // 2 right = left_pad - left new_img[top:top + new_h, left:left + new_w] = img else: # resize by height, then pad width new_h = new_shape[1] new_w = int(nw * new_h / nh) assert new_w > 0, 'image size < new_size' img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_LINEAR) top = top_pad // 2 bottom = top_pad - top left = left_pad // 2 right = left_pad - left new_img[top:top + new_h, left:left + new_w] = img return new_img, ratio, (top_pad, left_pad) def xywh2xyxy(x): # Convert bounding box format from [x, y, w, h] to [x1, y1, x2, y2] y = x.copy() if isinstance(x, np.ndarray) else np.array(x) y[..., 0] = x[..., 0] - x[..., 2] / 2 y[..., 1] = x[..., 1] - x[..., 3] / 2 y[..., 2] = x[..., 0] + x[..., 2] / 2 y[..., 3] = x[..., 1] + x[..., 3] / 2 return y def xyxy2xywh(x): # Convert bounding
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值