Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

envisat_dump_VV.c

Go to the documentation of this file.
00001 /* BK: simple program to dump binary data for ENVISAT 
00002    for alternating pol, HH channel.
00003    #%// BK 24-Nov-2003
00004    $Id: envisat_dump_VV.c,v 1.2 2004/05/13 18:13:39 kampes Exp $
00005 THIS PROGRAM IS NOT VERY GOOD, BUT A FAST HACK FOR Z. PERSKI
00006 */
00007 #include <assert.h>
00008 #include <errno.h>
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <math.h>
00013 #include <time.h>
00014 #include "epr_api-2.0.1/src/epr_api.h"
00015 #if defined(WIN32) && defined(_DEBUG)
00016 #include <crtdbg.h>
00017 #endif /* if defined(WIN32) && defined(_DEBUG) */
00018 
00019 
00020 int main(int argc, char** argv) 
00021   {
00022   const char*     product_file_path;
00023   const char*     outfile;
00024   const char*     record_name;
00025   FILE*           outstream;
00026   EPR_SProductId* product_id;
00027   EPR_SDatasetId* MAIN_PROC_PM_ID;
00028   EPR_SDatasetId* MDS1;
00029   EPR_SRecord*    rec1;
00030   EPR_SRecord*    rec5;
00031   EPR_SField*     numlines_field;
00032   EPR_SField*     numpixels_field;
00033   EPR_SField*     line_field;
00034   EPR_SField*     line_num_field;
00035   int             status;
00036   ulong           line_num;
00037   ulong           numlines;
00038   ulong           numberoflines;
00039   ulong           numberoffields;
00040   ulong           numpixels;
00041   ulong           l0;
00042   ulong           lN;
00043   ulong           p0;
00044   ulong           pN;
00045   ulong           cnt;
00046   ulong           x,y;  /* loop counter go upto 25.000 or so */
00047   short           realpart_short;/* written to output file */
00048   short           imagpart_short;/* written to output file */
00049 
00050   /* handle input */
00051   printf("argc: %f\n", (float)argc);
00052   if (argc != 3 && argc !=7 && argc !=8)
00053     {
00054     printf("Usage: envisat_dump_VV envisat-product outputfile [l0 lN p0 pN]\n");
00055     printf("  where envisat-product is the input filename of the alternating pol product\n");
00056     printf("        outputfile      is the output filename\n");
00057     printf("        l0              is the first azimuth line (starting at 1)\n");
00058     printf("        lN              is the last azimuth line\n");
00059     printf("        p0              is the first range pixel (starting at 1)\n");
00060     printf("        pN              is the last range pixel\n");
00061     printf("Example:\n");
00062     printf("  envisat_dump_VV ASA_IMS_1PNDPA20021025_175208_000000162010_00356_03416_0005.N1 crop.out 0 10 0 100\n\n");
00063     exit(1);
00064     }
00065   product_file_path = argv[1];
00066   outfile           = argv[2];
00067   printf("infile:  %s\n",product_file_path);
00068   printf("outfile: %s\n",outfile);
00069   if (argc>=7)
00070     {
00071     status=sscanf(argv[3],"%u",&l0);
00072     status=sscanf(argv[4],"%u",&lN);
00073     status=sscanf(argv[5],"%u",&p0);
00074     status=sscanf(argv[6],"%u",&pN);
00075     printf("sscanf l0: %f\n", (float)l0);
00076     printf("sscanf lN: %f\n", (float)lN);
00077     printf("sscanf p0: %f\n", (float)p0);
00078     printf("sscanf pN: %f\n", (float)pN);
00079     }
00080   /*
00081   record_name = (argc==8) ? argv[8] : "MDS1";
00082   printf("record name: %s\n",record_name);
00083   */
00084 
00085   /* Initialize the API. Set log-level to DEBUG and use default log-output (stdout) */
00086   epr_init_api(e_log_debug, epr_log_message, NULL);
00087 
00088   /* Open the product; an argument is a path to product data file */
00089   /* PRODUCT dataset record field element */
00090   product_id      = epr_open_product(product_file_path);
00091   /* product DATASET record field element */
00092   MAIN_PROC_PM_ID = epr_get_dataset_id(product_id, "MAIN_PROCESSING_PARAMS_ADS");
00093   /* see product specs, record is called MDS2! */
00094   MDS1            = epr_get_dataset_id(product_id, "MDS2");
00095   /* product dataset RECORD field element */
00096   rec1 = epr_read_record(MAIN_PROC_PM_ID,         0, NULL);
00097   /* product dataset record FIELD element */
00098   numlines_field  = epr_get_field(rec1, "num_output_lines");
00099   numpixels_field = epr_get_field(rec1, "num_samples_per_line");
00100   /*
00101   epr_free_record(rec1);
00102   */
00103   epr_print_field(numlines_field, stdout);
00104   epr_print_field(numpixels_field, stdout);
00105   numlines        = epr_get_field_elem_as_ulong(numlines_field, 0);
00106   numpixels       = epr_get_field_elem_as_ulong(numpixels_field, 0);
00107   if (argc == 3)
00108     {
00109     l0 = 1;
00110     lN = numlines;
00111     p0 = 1;
00112     pN = numpixels;
00113     }
00114   /* loop over data record to get data and dump it to file */
00115   numberoflines = epr_get_num_records(MDS1);
00116   printf("numberoflines: %f\n", (float)numberoflines);
00117   printf("numlines: %f\n", (float)numlines);
00118   printf("numpixels: %f\n", (float)numpixels);
00119   printf("l0: %f\n", (float)l0);
00120   printf("lN: %f\n", (float)lN);
00121   printf("p0: %f\n", (float)p0);
00122   printf("pN: %f\n", (float)pN);
00123   /* check if number of records indeed equals the number of lines */
00124   if (numberoflines != numlines)
00125     {
00126     printf("numlines not equal in check, ASAR format error?.");
00127     exit(1);
00128     }
00129   /* read in whole line of cpx data */
00130   outstream = fopen(outfile,"wb");
00131   for (y=l0;y<=lN;y++)
00132     {
00133     rec5       = epr_read_record(MDS1, y-1, NULL);
00134     /*
00135     numberoffields = epr_get_num_fields(rec5);
00136     line_num_field = epr_get_field(rec5, "line_num");
00137     line_num       = epr_get_field_elem_as_ulong(line_num_field,0);
00138     printf("numberoffields: %i\n", numberoffields);
00139     printf("line number: %i\n", line_num);
00140     */
00141 
00142     line_field = epr_get_field(rec5, "proc_data");
00143     cnt = 2*(p0-1);/* p0 starts at 1 for first element */
00144     /* write out selected pixels */
00145     for (x=p0;x<=pN;x++)
00146       {
00147       realpart_short = epr_get_field_elem_as_short(line_field,cnt);
00148       cnt = cnt + 1;
00149       imagpart_short = epr_get_field_elem_as_short(line_field,cnt);
00150       cnt = cnt + 1;
00151 /*
00152 printf("%i,%i: realpart: %f  %f\n", x,y,(float)realpart_short,(float)imagpart_short);
00153 */
00154       status = fwrite(&realpart_short,2,1,outstream);
00155       if (status != 1) fprintf(stderr,"fwrite could not write to disk?");
00156       status = fwrite(&imagpart_short,2,1,outstream);
00157       if (status != 1) fprintf(stderr,"fwrite could not write to disk?");
00158       }
00159     /* this program need memory for some reason?  try to free it
00160     */
00161     epr_free_record(rec5);
00162     }
00163   fclose(outstream);
00164   epr_close_product(product_id);
00165   /* Closes product reader API, release all allocated resources */
00166   epr_close_api();
00167 
00168   return 0;
00169   }
00170 
00171 

Generated on Fri Apr 22 15:57:52 2005 for Doris by doxygen 1.3.6