mdv to binary

将mdv数据转换为binary数据,以结构体的形式存放在文件中

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
xiu
2015.4.8 10:00
use the body structure, put the mdv format data radar and vdras , in a binary file.
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

///////////////////////////////////////////////////////////////////////////////////
//
//_MdvToBinary
//
//
// Description:
//
//      change the file type from mdv to binary
//
//
// -------------
///////////////////////////////////////////////////////////////////////////////////

void  _MdvToBinary()
{

    // Read Vdras field data  write to file one field by one field

    time_t vdrasTime = mdvxVdras.getValidTime();
    struct tm* cur = gmtime(&vdrasTime);

    // get "year", "month","day" from vdrasTime

    // set the binary file name
    //file_name_vdras restore the Vdras data
    //file_name_radar restore the radar data

    char file_name_vdras[1024];
    char file_name_radar[1024]; 

    sprintf(file_name_vdras, "%02d%02d%02d_Vdras_bin_xiu",
        cur->tm_hour,cur->tm_min,cur->tm_sec); 

    sprintf(file_name_radar, "%02d%02d%02d_Radar_bin_xiu",
        cur->tm_hour,cur->tm_min,cur->tm_sec);  

    // cout<<"xiu...test..."<<file_name<<endl;

    //set the data to restore

    readSourceVdras ptemp_vdras_data;
    readSourceRadar ptemp_radar_data;

    for(int field = 0 ;field < fieldNum ; field++)
    {
        const MdvxField *fld_Vdras  = mdvxVdras.getField(field);

        if (fld_Vdras == NULL)
        {
            cerr << "Cannot find rh field, Vdras file" << endl;
            return;
        }

        // Get the pointer of the Field data of Vdras
        const Mdvx::field_header_t &fld_hdr_Vdras = fld_Vdras->getFieldHeader();
        fl32 *fld_data_Vdras  = (fl32 *)fld_Vdras->getVol();

        // Read Radar field datadestructor,
        const MdvxField *fld_Radar = mdvxRadar.getField(0);
        if (fld_Radar == NULL)
        {
            cerr << "Cannot find DBZ Composite field, Radar file" << endl;
            return;
        }

        // Get the pointer of the Field data of Radar
        const Mdvx::field_header_t &fld_hdr_Radar = fld_Radar->getFieldHeader();
        fl32 *fld_data_Radar  = (fl32 *)fld_Radar->getVol();

        // set Vdras params
        int nx = fld_hdr_Vdras.nx;
        int ny = fld_hdr_Vdras.ny;
        int nz = fld_hdr_Vdras.nz;
        int npoints   = nx*ny;
        int npoints3D = nx*ny*nz;

        // set Radar params
        int nx2 = fld_hdr_Radar.nx;
        int ny2 = fld_hdr_Radar.ny;

        // make a check here. Vdras and Radar data should have the same grid params.
        if(nx != nx2 || ny != ny2)
        {
            cerr << "ERROR - VdrasMdv::readSourceFile()" << endl;
            cerr << "  nx or ny of Vdras and Radar data is different." << endl;
            return;
        }

        // cerr << "nx = " << nx << "; ny = " << ny << "; nz = " << nz<<  endl << endl;
        //cerr << "npoints = " << npoints << "; npoints3D = " << npoints3D <<  endl << endl;

        //set the vdras data
        ptemp_vdras_data.nx = nx;
        ptemp_vdras_data.ny = ny;
        ptemp_vdras_data.nz = nz;
        ptemp_vdras_data.field=field;

        sprintf(ptemp_vdras_data.time, "%04d%02d%02d_%02d%02d%02d",
            cur->tm_year+1900,cur->tm_mon+1,cur->tm_mday,cur->tm_hour,cur->tm_min,cur->tm_sec); 

        //cout<<"field="<<field<<endl;
        struct Level vdras_level_data[nz] ;

        ptemp_radar_data.nx=nx2;
        ptemp_radar_data.ny=ny2;

        sprintf(ptemp_radar_data.time, "%04d%02d%02d_%02d%02d%02d",
            cur->tm_year+1900,cur->tm_mon+1,cur->tm_mday,cur->tm_hour,cur->tm_min,cur->tm_sec);

        //set  all levels‘ vdras data and radar data

        for(int tt_iz = 0 ; tt_iz < nz ; tt_iz++)
        {
            vdras_level_data[tt_iz].level_z = tt_iz;

            for(int tt_iy = 0 ;tt_iy < ny ; tt_iy++)

                for(int tt_ix = 0 ; tt_ix < nz ;tt_ix++)
                {
                    int index = ( tt_ix )+( tt_iy )*nx + tt_iz*nx*ny;
                    int index1 = ( tt_ix )+( tt_iy )*nx ;

                    vdras_level_data[tt_iz].vdras_data[ index1 ] = fld_data_Vdras[index];                           

                }

        }

        int radarnum=0;
        for ( int ix = 0; ix < nx; ix++)
            for(int iy =0; iy < ny; iy++)
            {
                int ind = ix + iy * nx;
                ptemp_radar_data.radar_data[ind] = fld_data_Radar[ind];
                if(ptemp_radar_data.radar_data[ind]>35)
                 {
                    //cout<< fld_data_Radar[ind]<<"   ";
                    radarnum++;
                }

            }
        cout<<endl;
        cout<<"\nradarnum2 = "<<radarnum <<endl;

        //write to vdras file
        //cout<<"xiu.....write..."<<file_name<<endl; 

        /*ofstream os( file_name_vdras , ios_base::out | ios_base::binary |ios_base::app);
        os.write( reinterpret_cast<char *>(&ptemp_vdras_data),sizeof( ptemp_vdras_data ) );
        for(int temp = 0 ;temp < nz ;temp++)
        {
            os.write( reinterpret_cast<char *>(&vdras_level_data[temp]),sizeof( Level ) );
        }
        os.close();
        */

        //////////////////////////////////////////////////////////////
        // test file
        /*
        ifstream is(file_name1, ios_base::in |ios_base::binary );

        if (is)
        {
            readSourceRadar e;

            is.read(reinterpret_cast<char *>(&e), sizeof(e));

            cout << e.nx << " "<<e.ny<<" " <<endl;
            cout<<e.time<<endl;

            //cout<<e.vdras_data<<"  ";
            for(int iz=0;iz<=0;iz++)
            {
                for(int iy=0;iy<=0;iy++)
                {
                    for(int ix=0;ix<10;ix++)
                    {
                        int indexq=ix+iy*e.nx+iz*e.nx*e.ny;
                        cout<<e.radar_data<<" ";
                        //cout<<e.vdras_data[indexq]<<"  ";
                        //  cout<<index<<"  ";
                    }
                    cout<<endl;
                }
            }

        }

        else
        {
            cout << "ERROR: Cannot open file " << << file_name1 << endl;
        }

        is.close();

        */

        //cout<<"write file....."<<endl<<endl;
    }

    //write to radar file
    /*ofstream os1( file_name_radar , ios_base::out | ios_base::binary |ios_base::app);
    os1.write( reinterpret_cast<char *>(&ptemp_radar_data),sizeof( ptemp_radar_data ) );
    os1.close();
    */

    // cout<<"xiu ... readSourceFile............................end"<<endl<<endl;

}

时间: 2024-11-08 18:56:25

mdv to binary的相关文章

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

[leetcode] 104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode 50 Pow(x, n)(Math、Binary Search)(*)

翻译 实现pow(x, n). 原文 Implement pow(x, n). 分析 首先给大家推荐维基百科: zh.wikipedia.org/wiki/二元搜尋樹 en.wikipedia.org/wiki/Binary_search_tree 其次,大家也可以看看类似的一道题: LeetCode 69 Sqrt(x)(Math.Binary Search)(*) 然而这题我还是没有解出来,看看别人的解法-- class Solution { private: double myPowHel