制作nc文件流程——以站点降水数据为例

2023-10-13 20:10

本文主要是介绍制作nc文件流程——以站点降水数据为例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

netcdf文件制作流程

      • 1. define dimensions
      • 2. define variables
      • 3. put variables into nc
      • 4. put attribute into nc
      • 5. define global attribution
      • 6.附上代码全文

【提示】 事先想好需要存储变量的结构及维度,之后需要注意的是创建 nc 文件的 类 型 \color{#FF0000}{类型} ,以下供参考:

Value of cmodeDescription
‘NOCLOBBER’Prevent overwriting of existing file with the same name.(不覆盖已经有的同名文件)
‘CLOBBER’Overwrite any existing file with the same name.(覆盖任何已有的同名文件)
‘SHARE’Allow synchronous file updates.(允许同步文件更新)
‘64BIT_OFFSET’Allow easier creation of files and variables which are larger than two gigabytes.(可创建>2G的文件)
‘NETCDF4’Create a NetCDF-4/HDF5 file(普通)
‘CLASSIC_MODEL’Enforce the classic model; has no effect unless used in a bitwise-or with NETCDF4
outDir     = 'H:\TempData\SationPrecp';
outFile    = 'Precip_Daily_2481stations_China.nc';
outDirFile = [outDir, '\', outFile];
outid      = netcdf.create(outDirFile, '64BIT_OFFSET');

1. define dimensions

numStatdimID = netcdf.defDim(outid,'numStat', 2481);
yeardimID     = netcdf.defDim(outid,'year', 68);
mondimID     = netcdf.defDim(outid,'mon', 12);
daydimID    = netcdf.defDim(outid,'day', 31);

2. define variables

lon_id  = netcdf.defVar(outid,'lon','NC_FLOAT', numStatdimID);
lat_id  = netcdf.defVar(outid,'lat','NC_FLOAT', numStatdimID);
alt_id  = netcdf.defVar(outid,'alt','NC_FLOAT', numStatdimID);
Prec_id = netcdf.defVar(outid,'Prec','NC_FLOAT',[daydimID, mondimID, yeardimID, numStatdimID]);netcdf.endDef(outid);

3. put variables into nc

【提醒】 注意放置的数据时,变量一定要和自己在 2.定义维度 时的矩阵形状完全一致才行!这个形状也就是matllab 中用 n c d i s p ( n c n a m e ) \color{FF0000}{ncdisp(ncname)} ncdisp(ncname) 函数显示出来的矩阵形状。

netcdf.putVar(outid, lon_id, UnqLon/100);
netcdf.putVar(outid, lat_id, UnqLat/100);
netcdf.putVar(outid, alt_id, UnqAlt/100);
netcdf.putVar(outid, Prec_id, Precmmd);netcdf.reDef(outid);

4. put attribute into nc

netcdf.putAtt(outid, lon_id, 'longname', 'longitude');
netcdf.putAtt(outid, lon_id, 'units', 'degrees_east');netcdf.putAtt(outid, lat_id, 'longname', 'latitude');
netcdf.putAtt(outid, lat_id, 'bounds', 'degrees_north');netcdf.putAtt(outid, alt_id, 'longname', 'height above sea level');
netcdf.putAtt(outid, alt_id, 'units', 'm');netcdf.putAtt(outid, Prec_id, 'longname', 'daily rainfall');
netcdf.putAtt(outid, Prec_id, 'units', 'mm/day');
netcdf.putAtt(outid, Prec_id, '_FillValue', single(-999));
netcdf.putAtt(outid, Prec_id, 'missing_value', single(-999));

5. define global attribution

netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'title', 'station daily rainfall data');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Data_Period', '1951-2018');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Author', 'Dayang Wang, SYSU, Email : wangdy58@mail2.sysu.edu.cn');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'History', ['created on: ',datestr(now)]);netcdf.close(outid);

6.附上代码全文

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% Aim: this script aim at transfering Precp data of Station to nc type file
% Date: 2021-03-03
% Author: Dayang Wang, Sun Yat-sen University
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % tic
clc; clear;
% read data from station 
inDir = ('H:\TempData\SationPrecp');
inFile = ('PREC_Stn2481.mat');
inDirFile = [inDir, '\', inFile];
load (inDirFile);stationId = PREC(:, 1);
N = length(stationId);
Lat  = PREC(:, 2);
Lon  = PREC(:, 3);
Alt  = PREC(:, 4);
Year = PREC(:, 5);
Month = PREC(:, 6);
Day   = PREC(:, 7);
ValPrecp = PREC(:, 8);[Unqid, ind, ~] = unique(stationId, 'stable');
UnqLat = Lat(ind);
UnqLon = Lon(ind);
UnqAlt = Alt(ind);numStat = length(Unqid);
for i = 1 : numStat stationId(stationId == Unqid(i)) = i;
endYRbeg = min(Year);
YRend = max(Year);
numYR = YRend - YRbeg + 1;
for j = 1 : numYRYear(Year == YRbeg + j - 1 ) = j;
endPrecmmd = -999 * ones(31, 12, numYR, numStat);for k = 1 : NiDay  = Day(k);iMon  = Month(k);iYear = Year(k);iStat = stationId(k);Precmmd(iDay, iMon, iYear, iStat) = ValPrecp(k);% check scheduleif mod(k, 10^6) == 0disp(['----- ',num2str(k),' -----'])end
endPrecmmd = single(Precmmd);% PREC = [Day, Month, Year, stationId];%----------------------------------------------------------------------
% make nc
%----------------------------------------------------------------------
outDir     = 'H:\TempData\SationPrecp';
outFile    = 'Precip_Daily_2481stations_China.nc';
outDirFile = [outDir, '\', outFile];
outid      = netcdf.create(outDirFile, '64BIT_OFFSET');% define dimensions
numStatdimID = netcdf.defDim(outid,'numStat', 2481);
yeardimID     = netcdf.defDim(outid,'year', 68);
mondimID     = netcdf.defDim(outid,'mon', 12);
daydimID    = netcdf.defDim(outid,'day', 31);% define variables 
station_id  = netcdf.defVar(outid,'stid','NC_FLOAT', numStatdimID);
lon_id  = netcdf.defVar(outid,'lon','NC_FLOAT', numStatdimID);
lat_id  = netcdf.defVar(outid,'lat','NC_FLOAT', numStatdimID);
alt_id  = netcdf.defVar(outid,'alt','NC_FLOAT', numStatdimID);
Prec_id = netcdf.defVar(outid,'Prec','NC_FLOAT',[daydimID, mondimID, yeardimID, numStatdimID]);netcdf.endDef(outid);% put variables into nc
netcdf.putVar(outid, station_id, Unqid);
netcdf.putVar(outid, lon_id, UnqLon/100);
netcdf.putVar(outid, lat_id, UnqLat/100);
netcdf.putVar(outid, alt_id, UnqAlt/100);
netcdf.putVar(outid, Prec_id, Precmmd);netcdf.reDef(outid);% put attribute into nc
netcdf.putAtt(outid, lon_id, 'longname', 'original station id');netcdf.putAtt(outid, lon_id, 'longname', 'longitude');
netcdf.putAtt(outid, lon_id, 'units', 'degrees_east');netcdf.putAtt(outid, lat_id, 'longname', 'latitude');
netcdf.putAtt(outid, lat_id, 'bounds', 'degrees_north');netcdf.putAtt(outid, alt_id, 'longname', 'height above sea level');
netcdf.putAtt(outid, alt_id, 'units', 'm');netcdf.putAtt(outid, Prec_id, 'longname', 'daily rainfall');
netcdf.putAtt(outid, Prec_id, 'units', 'mm/day');
netcdf.putAtt(outid, Prec_id, '_FillValue', single(-999));
netcdf.putAtt(outid, Prec_id, 'missing_value', single(-999));% define global attribution
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Title', 'Station daily rainfall data');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Data Period', '1951-2018');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Author', 'Dayang Wang, SYSU, Email : wangdy58@mail2.sysu.edu.cn');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'History', ['created on: ',datestr(now)]);netcdf.close(outid);toc

文章同步推送 水文取经人 公众号,欢迎对水文、气象、陆面过程模拟研究方向感兴趣的大佬一起交流讨论!更多干货,敬请期待!
在这里插入图片描述

这篇关于制作nc文件流程——以站点降水数据为例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/205574

相关文章

Linux镜像文件制作方式

《Linux镜像文件制作方式》本文介绍了Linux镜像文件制作的过程,包括确定磁盘空间布局、制作空白镜像文件、分区与格式化、复制引导分区和其他分区... 目录1.确定磁盘空间布局2.制作空白镜像文件3.分区与格式化1) 分区2) 格式化4.复制引导分区5.复制其它分区1) 挂载2) 复制bootfs分区3)

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

MySQL中的DELETE删除数据及注意事项

《MySQL中的DELETE删除数据及注意事项》MySQL的DELETE语句是数据库操作中不可或缺的一部分,通过合理使用索引、批量删除、避免全表删除、使用TRUNCATE、使用ORDERBY和LIMI... 目录1. 基本语法单表删除2. 高级用法使用子查询删除删除多表3. 性能优化策略使用索引批量删除避免

MySQL 数据库进阶之SQL 数据操作与子查询操作大全

《MySQL数据库进阶之SQL数据操作与子查询操作大全》本文详细介绍了SQL中的子查询、数据添加(INSERT)、数据修改(UPDATE)和数据删除(DELETE、TRUNCATE、DROP)操作... 目录一、子查询:嵌套在查询中的查询1.1 子查询的基本语法1.2 子查询的实战示例二、数据添加:INSE

Linux服务器数据盘移除并重新挂载的全过程

《Linux服务器数据盘移除并重新挂载的全过程》:本文主要介绍在Linux服务器上移除并重新挂载数据盘的整个过程,分为三大步:卸载文件系统、分离磁盘和重新挂载,每一步都有详细的步骤和注意事项,确保... 目录引言第一步:卸载文件系统第二步:分离磁盘第三步:重新挂载引言在 linux 服务器上移除并重新挂p

使用MyBatis TypeHandler实现数据加密与解密的具体方案

《使用MyBatisTypeHandler实现数据加密与解密的具体方案》在我们日常的开发工作中,经常会遇到一些敏感数据需要存储,比如用户的手机号、身份证号、银行卡号等,为了保障数据安全,我们通常会对... 目录1. 核心概念:什么是 TypeHandler?2. 实战场景3. 代码实现步骤步骤 1:定义 E

使用C#导出Excel数据并保存多种格式的完整示例

《使用C#导出Excel数据并保存多种格式的完整示例》在现代企业信息化管理中,Excel已经成为最常用的数据存储和分析工具,从员工信息表、销售数据报表到财务分析表,几乎所有部门都离不开Excel,本文... 目录引言1. 安装 Spire.XLS2. 创建工作簿和填充数据3. 保存为不同格式4. 效果展示5

Python多任务爬虫实现爬取图片和GDP数据

《Python多任务爬虫实现爬取图片和GDP数据》本文主要介绍了基于FastAPI开发Web站点的方法,包括搭建Web服务器、处理图片资源、实现多任务爬虫和数据可视化,同时,还简要介绍了Python爬... 目录一. 基于FastAPI之Web站点开发1. 基于FastAPI搭建Web服务器2. Web服务

MySQL 批量插入的原理和实战方法(快速提升大数据导入效率)

《MySQL批量插入的原理和实战方法(快速提升大数据导入效率)》在日常开发中,我们经常需要将大量数据批量插入到MySQL数据库中,本文将介绍批量插入的原理、实现方法,并结合Python和PyMySQ... 目录一、批量插入的优势二、mysql 表的创建示例三、python 实现批量插入1. 安装 PyMyS