FTP服务器获取文件,并解析GRB2文件获取数据

1.连接FTP服务器

package Grib2Test;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.util.Map;

public class FTPConnect {
    private static FTPClient ftpClient;
    public static FTPClient getFTPClients(Map<String,Object> firstMap, Map<String,Object> secondMap) {
        try {
            ftpClient = new FTPClient();
            ftpClient.connect((String) firstMap.get("ftpHost"),Integer.parseInt(String.valueOf(firstMap.get("ftpPort"))));// 连接FTP服务器
            ftpClient.login((String)firstMap.get("ftpUserName"), (String)firstMap.get("ftpPassword"));// 登陆FTP服务器
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.out.println("未连接到FTP,用户名或密码错误。 ");
                ftpClient.disconnect();
            } else {
                ftpClient.enterLocalPassiveMode();
                System.out.println("FTP连接成功。");
            }
        } catch (Exception e) {
            System.out.println("FTP的IP地址可能错误,请正确配置。      正在尝试连接另一个ftp");
            try {
                ftpClient.connect((String) secondMap.get("ftpHost"),Integer.parseInt(String.valueOf(secondMap.get("ftpPort"))));// 连接FTP服务器
                ftpClient.login((String)secondMap.get("ftpUserName"), (String)secondMap.get("ftpPassword"));
            } catch (IOException ioException) {
                System.out.println("FTP连接失败");
            }
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.out.println("未连接到FTP,用户名或密码错误。 ");
                try {
                    ftpClient.disconnect();
                } catch (IOException ioException) {
                    System.out.println("断开连接失败");
                }
            } else {

                ftpClient.enterLocalPassiveMode();
                if(ftpClient!=null)
                    System.out.println("FTP连接成功。");
                else
                    System.out.println("FTP连接失败");
            }
        }
        return ftpClient;
    }


    public static void closeFTPConnection() {
        if (ftpClient != null) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                System.out.println("FTP连接关闭失败!");
            }
        }
    }
}

2.通过时间读取指定文件,并下载到本地

package Grib2Test;


import Grib2Test.FTPConnect;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;


import java.io.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ReadFTPFileByTime {
    public FTPClient ftp;
    String storePath="C:\\sunsheen\\grib\\";
    public static void main(String[] args) throws IOException {
        ReadFTPFileByTime readFTPFileByTime = new ReadFTPFileByTime();
        readFTPFileByTime.readByTime("2022-01-04 16:30:02","TAIR");
    }

    /**
     * 根据传入的日期,读取文件内容
     *
     * @throws ParseException
     */
    public String readByTime(String Time,String element) throws IOException {
        Map<String, Object> firstMap = new HashMap<>();
        firstMap.put("ftpHost", "10.155.96.30");
        firstMap.put("ftpPort", 21);
        firstMap.put("ftpUserName", "ftp_nafp");
        firstMap.put("ftpPassword", "ftp_nafp123");

        Map<String, Object> secondMap = new HashMap<>();
        secondMap.put("ftpHost", "10.155.96.30");
        secondMap.put("ftpPort", 22);
        secondMap.put("ftpUserName", "ftp_nafp");
        secondMap.put("ftpPassword", "ftp_nafp123");

//        FTPClient ftpClient;
        ftp = FTPConnect.getFTPClients(firstMap, secondMap);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = null;
        try {
            now = sdf.parse(Time);
        } catch (ParseException e) {
            System.out.println("时间格式错误");
        }
        String worldTime = sdf.format(now);
        String timeFile = worldTime.split(" ")[0];
        timeFile=timeFile.replaceAll("-","");
        String hour = worldTime.split(" ")[1].split(":")[0];

        StringBuffer filenames = new StringBuffer();
//        List  fileInfo = new ArrayList<>();
        String directory =timeFile;//下载目录20220104
        //更换目录到当前目录
        this.ftp.changeWorkingDirectory(directory);
        ftp.enterLocalPassiveMode();
        FTPFile[] files = this.ftp.listFiles();
        if (files != null) {
        }
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                String n = new String(files[i].getName().getBytes("gbk"), "utf-8");
                if (i == files.length - 1) {
                    filenames.append(n + "," + storePath);
                } else {
                    filenames.append(n + ",");
                }
            }
        }
        String fileNames = filenames.toString();
        List<Object> fileInfo=Arrays.asList(fileNames.split(","));
        System.out.println(fileInfo.size());
        String timeString=timeFile+hour+".GRB2";//2022010416.GRB2
        String file = null;
        for (int i = 0; i < fileInfo.size(); i++) {
            file= (String) fileInfo.get(i);
            if (file.contains(timeString)&&file.contains(element)){
                System.out.println(file);
                File f = new File(storePath+file);
                if(!f.exists()){
                    System.out.println("文件不存在下载");
                    downFile(ftp,file, file, storePath);
                }else{
                    System.out.println("文件存在,返回路径");
                }
                break;
            }
        }
        FTPConnect.closeFTPConnection();
//        System.out.println("downLoad:"+storePath+file);
        //C:\sunsheen\gribZ_NAFP_C_BABJ_20220104080816_P_HRCLDAS_RT_CHN-BEHK_0P01_HOR-TAIR-2022010416.GRB2
        return  storePath+file;
    }

    /**
     * 下载方法
     *
     * @param ftpClient   FTPClient对象
     * @param newFileName 新文件名
     * @param fileName    原文件名
     * @param downUrl     下载路径
     * @return
     * @throws IOException
     */
    public static boolean downFile(FTPClient ftpClient, String newFileName, String fileName, String downUrl) throws IOException {
        boolean isTrue = false;
        OutputStream os = null;

        File localFile = new File(downUrl + "/" + newFileName);
        System.out.println("本地下载路径:"+localFile);
        os = new FileOutputStream(localFile);
        isTrue = ftpClient.retrieveFile(new String(fileName.getBytes(), "ISO-8859-1"), os);
        os.close();
        return isTrue;
    }


}
3.通过NetcdfFile读取解决GRB2文件,获取数据,然后筛选经纬度获取指定区域数据

package Grib2Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import ucar.nc2.NetcdfFile;

public class ReadGribDataByNetcdfFile {
    public ReadGribDataByNetcdfFile() {
    }

    public Map<String, Object> ReadFileQAIR(String path) {
        NetcdfFile ncf = null;
        HashMap dataMap = new HashMap();

        try {
            ncf = NetcdfFile.open(path);
            float[][] qAIR = (float[][]) ncf
                    .findVariable("Specific_humidity_height_above_ground")
                    .read().reduce(0).reduce(0).copyToNDJavaArray();
//            System.out.println("qAIR"+ Arrays.deepToString(qAIR));
            dataMap.put("lon", ncf.findVariable("lon").read()
                    .copyTo1DJavaArray());
            dataMap.put("lat", ncf.findVariable("lat").read()
                    .copyTo1DJavaArray());
            dataMap.put("qAIR", qAIR);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dataMap;
    }


    public Map<String, Object> ReadFileTAIR(String path) {
        NetcdfFile ncf = null;
        HashMap dataMap = new HashMap();

        try {
            ncf = NetcdfFile.open(path);
            float[][] tAIR = (float[][]) ncf
                    .findVariable("Temperature_height_above_ground")
                    .read().reduce(0).reduce(0).copyToNDJavaArray();
//            System.out.println("qAIR"+ Arrays.deepToString(tAIR));
            dataMap.put("lon", ncf.findVariable("lon").read()
                    .copyTo1DJavaArray());
            dataMap.put("lat", ncf.findVariable("lat").read()
                    .copyTo1DJavaArray());
            dataMap.put("tAIR", tAIR);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMap;
    }

    public Map<String, Object> ReadFileUWIN(String path) {
        NetcdfFile ncf = null;
        HashMap dataMap = new HashMap();

        try {
            ncf = NetcdfFile.open(path);
            float[][] uWIN = (float[][]) ncf
                    .findVariable("u-component_of_wind_height_above_ground")
                    .read().reduce(0).reduce(0).copyToNDJavaArray();
//            System.out.println("qAIR"+ Arrays.deepToString(uWIN));
            dataMap.put("lon", ncf.findVariable("lon").read()
                    .copyTo1DJavaArray());
            dataMap.put("lat", ncf.findVariable("lat").read()
                    .copyTo1DJavaArray());
            dataMap.put("uWIN", uWIN);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMap;
    }

    public Map<String, Object> ReadFileVWIN(String path) {
        NetcdfFile ncf = null;
        HashMap dataMap = new HashMap();

        try {
            ncf = NetcdfFile.open(path);
            float[][] vWIN = (float[][]) ncf
                    .findVariable("v-component_of_wind_height_above_ground")
                    .read().reduce(0).reduce(0).copyToNDJavaArray();
//            System.out.println("qAIR"+ Arrays.deepToString(vWIN));//9999
            dataMap.put("lon", ncf.findVariable("lon").read()
                    .copyTo1DJavaArray());
            dataMap.put("lat", ncf.findVariable("lat").read()
                    .copyTo1DJavaArray());
            dataMap.put("vWIN", vWIN);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMap;
    }

    public Map<String, Object> ReadFileWIND(String path) {
        NetcdfFile ncf = null;
        HashMap dataMap = new HashMap();

        try {
            ncf = NetcdfFile.open(path);
            float[][] winD = (float[][]) ncf
                    .findVariable("Wind_speed_height_above_ground")
                    .read().reduce(0).reduce(0).copyToNDJavaArray();
//            System.out.println("qAIR"+ Arrays.deepToString(winD));//9999
            dataMap.put("lon", ncf.findVariable("lon").read()
                    .copyTo1DJavaArray());
            dataMap.put("lat", ncf.findVariable("lat").read()
                    .copyTo1DJavaArray());
            dataMap.put("winD", winD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMap;
    }

    public Map<String, Object> ReadFilePRE(String path) {
        NetcdfFile ncf = null;
        HashMap dataMap = new HashMap();

        try {
            ncf = NetcdfFile.open(path);
            dataMap.put("lon", ncf.findVariable("lon").read()
                    .copyTo1DJavaArray());
            dataMap.put("lat", ncf.findVariable("lat").read()
                    .copyTo1DJavaArray());
            float[][] pre = (float[][]) ncf
                    .findVariable("Total_precipitation_surface")
                    .read().reduce(0).copyToNDJavaArray();
            System.out.println("qAIR"+ Arrays.deepToString(pre));
//            dataMap.put("pre", pre);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMap;
    }

    public ArrayList<ArrayList<Integer>> lonAndLatFilter(float[] lonArray,
                                                         float[] latArray) {
        ArrayList<ArrayList<Integer>> lonAndLatFilterArray = new ArrayList();
        ArrayList<Integer> lonFilterArray = new ArrayList();
        ArrayList<Integer> latFilterArray = new ArrayList();

        int j;
        for (j = 0; j < lonArray.length; ++j) {
            if (108.55D <= (double) lonArray[j]
                    && (double) lonArray[j] <= 111.15D) {
                lonFilterArray.add(j);
            }
        }

        for (j = 0; j < latArray.length; ++j) {
            if (18.1D <= (double) latArray[j] && (double) latArray[j] <= 20.25D) {
                latFilterArray.add(j);
            }
        }

        lonAndLatFilterArray.add(0, lonFilterArray);
        lonAndLatFilterArray.add(1, latFilterArray);
        return lonAndLatFilterArray;
    }
}
 

注:下载到本地读取GRB2文件时,需要注意读取格式

 float[][] winD = (float[][]) ncf .findVariable("Wind_speed_height_above_ground") .read().reduce(0).reduce(0).copyToNDJavaArray();
dataMap.put("lon", ncf.findVariable("lon").read().copyTo1DJavaArray());
dataMap.put("lat", ncf.findVariable("lat").read().copyTo1DJavaArray());

需要用特殊的软件打开查看GRB2文件

猜你喜欢

转载自blog.csdn.net/Temp_1998_H/article/details/123007937
今日推荐