Netcdf 文件多属性,按照时间段导出代码示例

由于个人即是java初学者,又是netcdf文件小白,所以为了实现最终效果,可以对一个初始nc文件进行筛选指定变量,并且可以设定时间区间,花得时间比想象的要多,最终实现了效果,代码示例如下:

    @GetMapping("/export")
    public String varExport(@ModelAttribute FileSaveVars params ) throws IOException, InvalidRangeException {
//      0° 设置时间
        String timeStart = "2018-01-01 00:30:00";
        String timeEnd = "2018-01-01 00:35:00";
//      1° 设置原始文件路径+转存文件路径
        String filePath = "/Users/caowei/workspace/jszone/大气系统/ncdemo/src/main/resources/static/MERRA2_400.tavg1_2d_flx_Nx.20180101.SUB.nc";
        String savePath = "/Users/caowei/workspace/test.nc";
//      2° 读取文件
        NetcdfFile ncFile = NetcdfFile.open(filePath, null);
//      3° 设置时间变量
        Variable timeObject = ncFile.findVariable("time");
        String timeAt = timeObject.getUnitsString();
        String regex = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}";
        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(timeAt);
        if (matcher.find()) {
            timeAt = matcher.group(0);
        }
//      3-1° 获取时间差
        int timeDiff1 = DateHelp.dateDifference(timeAt,timeStart);
        int timeDiff2 = DateHelp.dateDifference(timeAt,timeEnd);
//      3-2° 获取时间步长
        Array timeArray = timeObject.read();
        int timeStep = (int)(timeArray.getFloat(1) - timeArray.getFloat(0)) ;
        int startIndex = timeDiff1/timeStep;
        int endIndex = timeDiff2/timeStep;
 //     4° 构造输出保存文件
        NetcdfFileWriter ncWrite = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, savePath);
//      4-1° 文件维度
        for(Dimension dimension : ncFile.getDimensions()){
            // 对时间特殊处理 为Unlimited
            if(dimension.getName().equals("time")){
                ncWrite.addUnlimitedDimension("time");
            } else {
                ncWrite.addDimension(null, dimension.getName(), dimension.getLength());
            }
        }
//      5° 设置需要转存的变量
        List<String> varNames = Arrays.asList("time","lon","lat","QLML");
        List<Variable> varArray = new ArrayList<>();
        for(String varName : varNames){
            Variable varObject = ncFile.findVariable(varName);
            Variable varVar = ncWrite.addVariable(null, varObject.getName().toString(), varObject.getDataType(),varObject.getDimensionsString());
            if(varName.equals("time")){
                List<Attribute> AttrList = varObject.getAttributes();
                for (Attribute att : AttrList) {
//                对单位参数特殊处理
                    if( att.getName().equals("units")) {
                        varVar.addAttribute(new Attribute("units","minutes since "+timeStart));
                    } else {
                        varVar.addAttribute(att);
                    }
                }
            }
            else {
                varVar.addAll(ncFile.findVariable(varName).getAttributes());
            }
            varArray.add(varVar);
        }
//      6° 创建
        ncWrite.create();
//      7° 变量赋值
        for(Variable varItem : varArray){
            int rank= varItem.getRank();
            String varName = varItem.getName();
            if(rank == 1){
                if(varName.equals("time") ) {
                    List<Range> tyxRanges = new ArrayList<>();
                    tyxRanges.add(new Range(null,startIndex,endIndex));
                    Array varData = ncFile.findVariable(varName).read(tyxRanges);
                    ncWrite.write(varItem, varData);
                } else {
                    Array varData = ncFile.findVariable(varName).read();
                    ncWrite.write(varItem, varData);
                }
            }
            if(rank == 3){
                int[] shapes =  varItem.getShape();
                int  lonSize = shapes[1];
                int  latSize = shapes[2];
                List<Range> tyxRanges = new ArrayList<>();
                tyxRanges.add(new Range(null,startIndex,endIndex));
                tyxRanges.add(new Range(null,0,lonSize-1));
                tyxRanges.add(new Range(null,0,latSize-1));
                ncWrite.write(varItem, ncFile.findVariable(varName).read(tyxRanges));
            }
        }
//      8°关闭保存
        ncWrite.close();
        return  "OK";
    }

你可能感兴趣的:(netcdf)