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;

}

你可能感兴趣的:(数据,文件读取,structure)