制作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

相关文章

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键