void CreateStringCode(string InputFilename, string OutputFilename) {
	Data* NewData = new Data(0);
	string TempFilename;
	Species* NewSpecies = new Species(TempFilename,NewData);
	
	if (InputFilename.length() > 3 && InputFilename.substr(InputFilename.length()-3,3).compare("mol") == 0) {
		NewSpecies->ReadFromMol(InputFilename);
	} else if (InputFilename.length() > 3 && InputFilename.substr(InputFilename.length()-3,3).compare("dat") == 0) {
		NewSpecies->ReadFromDat(InputFilename);
	} else {
		NewSpecies->ReadFromSmiles(InputFilename);
	}

	NewSpecies->MakeNeutral();
	NewSpecies->PerformAllCalculations(false,true,true,false,false);

	ofstream Output;
	if (!OpenOutput(Output,OutputFilename)) {
		return;
	}

	Output << NewSpecies->FCode();

	Output.close();
	delete NewData;
}
void ProcessMolfiles() {
	string fullfile = FOutputFilepath()+"MolfileInput.txt";
	ifstream Input;
	if (!OpenInput(Input,fullfile)) {
		return;	
	}
	ofstream Output;
	string outfile = FOutputFilepath()+"MolfileOutput.txt";
	if (OpenOutput(Output,outfile)) {
		Data* NewData = new Data(0);
		Output << "id\tmolfile\tgroups\tcharge\tformula\tstringcode\tmass\tdeltaG\tdeltaGerr" << endl;
		GetStringsFileline(Input,"\t",false);
		while(!Input.eof()) {
			vector<string>* strings = GetStringsFileline(Input,"\t",false);
			if (strings->size() >= 2) {
				Species* NewSpecies = new Species("", NewData, false);
				NewSpecies->ReadFromMol(FOutputFilepath()+"molfiles/"+(*strings)[1]);
				NewSpecies->PerformAllCalculations(true,true,true,true,true);
				string cues = NewSpecies->CreateStructuralCueList();
				findandreplace(cues,"\t","|");
				Output << (*strings)[0] << "\t" << (*strings)[1] << "\t" << cues << "\t" << NewSpecies->FCharge() << "\t" << NewSpecies->FFormula() << "\t";
				Output << NewSpecies->FCode() << "\t" << NewSpecies->FMW() << "\t" << NewSpecies->FEstDeltaG() << "\t" << NewSpecies->FEstDeltaGUncertainty() << endl;
				delete NewSpecies;
			}
			delete strings;
		}
		Output.close();
	}
	Input.close();
}
Пример #3
0
int main(int argc, char *argv[])
{
    char c;

    //Make sure we didn't get a weird cp call
    if(argc > 3 && argc < 3)
    {
        printf("usage: cp <filename> <filename2>");
    }
    //If we got a valid cp call, open files for copying
    if(argc == 3)
    {
        OpenInput(argv[1]);
        OpenOutput(argv[2]);
    }

    //Make sure the fd's are valid
    if(out_fd == -1)
    {
        printf("cp: could not open file for writing!");
        exit(-1);
    }

    //Read byte by byte into the new file from the old file
    while(read(in_fd, &c, 1))
    {
        write(out_fd, &c, 1);
    }

    exit(0);
}
Пример #4
0
//Fileoutput
int Gene::SaveGene(string InFilename) {
	if (InFilename.length() == 0) {
		InFilename = GetData("FILENAME",STRING);
		if (InFilename.length() == 0) {
			InFilename = GetData("DATABASE",STRING);
			if (InFilename.length() == 0) {
				return FAIL;
			}
		}
	}
	
	if (InFilename.substr(1,1).compare(":") != 0 && InFilename.substr(0,1).compare("/") != 0) {
		InFilename = GetDatabaseDirectory(GetParameter("database"),"new gene directory")+InFilename;
	}

	ofstream Output;
	if (!OpenOutput(Output,InFilename)) {
		return FAIL;
	}

	//First I check to see if the user specified that the input headers be printed in the output file
	vector<string>* FileHeader = StringToStrings(GetParameter("gene data to print"),";");
	vector<string> InputHeaders;
	for (int i=0; i < int(FileHeader->size()); i++) {
		if ((*FileHeader)[i].compare("INPUT_HEADER") == 0) {
			InputHeaders = GetAllData("INPUT_HEADER",STRING);
			break;
		}
	}

	for (int i=0; i < int(InputHeaders.size()); i++) {
		string Data;
		Interpreter(InputHeaders[i],Data,false);
		Output << InputHeaders[i] << "\t" << Data << endl;
	}

	for (int i=0; i < int(FileHeader->size()); i++) {
		//I check to see if the current file header has already been printed to file
		if ((*FileHeader)[i].compare("INPUT_HEADER") != 0) {
			int j =0;
			for (j=0; j < int(InputHeaders.size()); j++) {
				if (InputHeaders[j].compare((*FileHeader)[i]) == 0) {
					break;
				}
			}
			if (j == int(InputHeaders.size())) {
				//If the current file header has not already been printed to file, it is printed now
				string Data;
				Interpreter((*FileHeader)[i],Data,false);
				if (Data.length() > 0) {
					Output << (*FileHeader)[i] << "\t" << Data << endl;
				}
			}
		}
	}

	Output.close();
	return SUCCESS;
}
Пример #5
0
status_t
QuaJoystickManager::connect(Output *s)
{
	s->dst.joy = OpenOutput(s->channel->uberQua, (QuaJoystickPort *)s->device);
	if (s->dst.midi != nullptr) {
		s->enabled = true;
	} else {
		s->enabled = false;
		return B_ERROR;
	}
	return B_OK;
}
Пример #6
0
int main(int argc, char* argv[])
{
      char *Intents[] = {"perceptual",
                         "relative colorimetric",
                         "saturation",
                         "absolute colorimetric" };

      HandleSwitches(argc, argv);

      cmsSetErrorHandler(MyErrorHandler);

      if ((argc - xoptind) != 2) {

              Help(0);              
              }

     

      if (Verbose) {

          if (lIsDeviceLink)
            fprintf(stdout, "%s(device link) -> %s [%s]", 
                                                argv[xoptind], 
                                                argv[xoptind+1], 
                                                Intents[Intent]);

        else
            fprintf(stdout, "%s(%s) -> %s(%s) [%s]", argv[xoptind],
                                                (cInpProf == NULL ? "sRGB": cInpProf), 
                                                argv[xoptind+1],
                                                (cOutProf == NULL ? "sRGB" : cOutProf), 
                                                Intents[Intent]);
      }

      OpenInput(argv[xoptind]);
      OpenOutput(argv[xoptind+1]);
      TransformImage(cInpProf, cOutProf);


      if (Verbose) fprintf(stdout, "\n");

      Done();

      return 0;
}
Пример #7
0
// OpenFiles():
//  Tests to make sure the paths are valid and opens the files.
//   This also generates an output and ubernull name if none are
//   specified.  Returns an error if something went wrong.
//  You should call GenerateNames() before calling this function.
//   Overload me if you need to open more files.
int CLIFileManipBase::OpenFiles() {
  // See if the source and output names are the same
  if( stricmp( source_name, output_name ) == 0 )
    return CLIFILEMANIPBASE_SAME_SOURCE_AND_OUTPUT;

  // Open the Source File
  int error = OpenSource();
  if( error != CLIFILEMANIPBASE_OK )
    return error;

  // Open the Output File
  error = OpenOutput();
  if( error != CLIFILEMANIPBASE_OK ) {
    CloseFiles();
    return error;
  }

  return CLIFILEMANIPBASE_OK;
}
Пример #8
0
int main(int argc, char* argv[])
{
	InitUtils("jpgicc");

	HandleSwitches(argc, argv);

	if ((argc - xoptind) != 2) {
		Help(0);
	}

	OpenInput(argv[xoptind]);
	OpenOutput(argv[xoptind+1]);

	TransformImage(cInpProf, cOutProf);


	if (Verbose) { fprintf(stdout, "\n"); fflush(stdout); }

	Done();

	return 0;
}
void ProcessMolfileDirectory(string Directory,string OutputDirectory) {
	vector<string> DirectoryList = GetDirectoryFileList(Directory);

	Data* NewData = new Data(0);
	for (int i=0; i < int(DirectoryList.size()); i++) {
		cout << DirectoryList[i] << endl;
		Species* NewSpecies = new Species("", NewData, false);
		if (DirectoryList[i].length() > 0) {
			NewSpecies->ReadFromMol(Directory+DirectoryList[i]);
			NewSpecies->LabelAtoms();
			string FileRoot = RemovePath(RemoveExtension(DirectoryList[i]));
			ofstream Output;
			if (OpenOutput(Output,OutputDirectory+FileRoot+".txt")) {
				Output << "Atom index;Group;GroupIndex" << endl;
				for (int j=0; j < NewSpecies->FNumAtoms(); j++) {
					Output << j << ";" << NewSpecies->GetAtom(j)->FGroupString() << ";" << NewSpecies->GetAtom(j)->FGroupIndex() << endl;
				}
				Output.close();
			}
		}
		delete NewSpecies;
	}
}
Пример #10
0
/*----------------------------------------------------------------------
|   WriteSamples
+---------------------------------------------------------------------*/
static AP4_Result
WriteSamples(AP4_Mpeg2TsWriter&               writer,
             AP4_Track*                       audio_track,
             SampleReader*                    audio_reader, 
             AP4_Mpeg2TsWriter::SampleStream* audio_stream,
             AP4_Track*                       video_track,
             SampleReader*                    video_reader, 
             AP4_Mpeg2TsWriter::SampleStream* video_stream,
             unsigned int                     segment_duration_threshold)
{
    AP4_Sample        audio_sample;
    AP4_DataBuffer    audio_sample_data;
    unsigned int      audio_sample_count = 0;
    double            audio_ts = 0.0;
    bool              audio_eos = false;
    AP4_Sample        video_sample;
    AP4_DataBuffer    video_sample_data;
    unsigned int      video_sample_count = 0;
    double            video_ts = 0.0;
    bool              video_eos = false;
    double            last_ts = 0.0;
    unsigned int      segment_number = 0;
    double            segment_duration = 0.0;
    AP4_ByteStream*   output = NULL;
    AP4_ByteStream*   playlist = NULL;
    char              string_buffer[32];
    AP4_Result        result = AP4_SUCCESS;
    AP4_Array<double> segment_durations;
    
    // prime the samples
    if (audio_reader) {
        result = ReadSample(*audio_reader, *audio_track, audio_sample, audio_sample_data, audio_ts, audio_eos);
        if (AP4_FAILED(result)) goto end;
    }
    if (video_reader) {
        result = ReadSample(*video_reader, *video_track, video_sample, video_sample_data, video_ts, video_eos);
        if (AP4_FAILED(result)) goto end;
    }
    
    for (;;) {
        bool sync_sample = false;
        AP4_Track* chosen_track= NULL;
        if (audio_track && !audio_eos) {
            chosen_track = audio_track;
            if (video_track == NULL) sync_sample = true;
        }
        if (video_track && !video_eos) {
            if (audio_track) {
                if (video_ts <= audio_ts) {
                    chosen_track = video_track;
                }
            } else {
                chosen_track = video_track;
            }
            if (chosen_track == video_track && video_sample.IsSync()) {
                sync_sample = true;
            }
        }
        if (chosen_track == NULL) break;
        
        // check if we need to start a new segment
        if (Options.segment_duration && sync_sample) {
            if (video_track) {
                segment_duration = video_ts - last_ts;
            } else {
                segment_duration = audio_ts - last_ts;
            }
            if (segment_duration >= (double)Options.segment_duration - (double)segment_duration_threshold/1000.0) {
                if (video_track) {
                    last_ts = video_ts;
                } else {
                    last_ts = audio_ts;
                }
                if (output) {
                    segment_durations.Append(segment_duration);
                    if (Options.verbose) {
                        printf("Segment %d, duration=%.2f, %d audio samples, %d video samples\n",
                               segment_number, 
                               segment_duration,
                               audio_sample_count, 
                               video_sample_count);
                    }
                    output->Release();
                    output = NULL;
                    ++segment_number;
                    audio_sample_count = 0;
                    video_sample_count = 0;
                }
            }
        }
        if (output == NULL) {
            output = OpenOutput(Options.output, segment_number);
            if (output == NULL) return AP4_ERROR_CANNOT_OPEN_FILE;
            writer.WritePAT(*output);
            writer.WritePMT(*output);
        }
        
        // write the samples out and advance to the next sample
        if (chosen_track == audio_track) {
            result = audio_stream->WriteSample(audio_sample, 
                                               audio_sample_data,
                                               audio_track->GetSampleDescription(audio_sample.GetDescriptionIndex()), 
                                               video_track==NULL, 
                                               *output);
            if (AP4_FAILED(result)) return result;
            
            result = ReadSample(*audio_reader, *audio_track, audio_sample, audio_sample_data, audio_ts, audio_eos);
            if (AP4_FAILED(result)) return result;
            ++audio_sample_count;
        } else if (chosen_track == video_track) {
            result = video_stream->WriteSample(video_sample,
                                               video_sample_data, 
                                               video_track->GetSampleDescription(video_sample.GetDescriptionIndex()),
                                               true, 
                                               *output);
            if (AP4_FAILED(result)) return result;

            result = ReadSample(*video_reader, *video_track, video_sample, video_sample_data, video_ts, video_eos);
            if (AP4_FAILED(result)) return result;
            ++video_sample_count;
        } else {
            break;
        }        
    }
    
    // finish the last segment
    if (output) {
        if (video_track) {
            segment_duration = video_ts - last_ts;
        } else {
            segment_duration = audio_ts - last_ts;
        }
        segment_durations.Append(segment_duration);
        if (Options.verbose) {
            printf("Segment %d, duration=%.2f, %d audio samples, %d video samples\n",
                   segment_number, 
                   segment_duration,
                   audio_sample_count, 
                   video_sample_count);
        }
        output->Release();
        output = NULL;
        ++segment_number;
        audio_sample_count = 0;
        video_sample_count = 0;
    }

    // create the playlist file if needed 
    if (Options.playlist) {
        playlist = OpenOutput(Options.playlist, 0);
        if (playlist == NULL) return AP4_ERROR_CANNOT_OPEN_FILE;

        unsigned int target_duration = 0;
        for (unsigned int i=0; i<segment_durations.ItemCount(); i++) {
            if ((unsigned int)(segment_durations[i]+0.5) > target_duration) {
                target_duration = segment_durations[i];
            }
        }

        playlist->WriteString("#EXTM3U\r\n");
        if (Options.playlist_hls_version > 1) {
            sprintf(string_buffer, "#EXT-X-VERSION:%d\r\n", Options.playlist_hls_version);
            playlist->WriteString(string_buffer);
        }
        playlist->WriteString("#EXT-X-MEDIA-SEQUENCE:0\r\n");
        playlist->WriteString("#EXT-X-TARGETDURATION:");
        sprintf(string_buffer, "%d\r\n\r\n", target_duration);
        playlist->WriteString(string_buffer);

        for (unsigned int i=0; i<segment_durations.ItemCount(); i++) {
            if (Options.playlist_hls_version >= 3) {
                sprintf(string_buffer, "#EXTINF:%f,\r\n", segment_durations[i]);
            } else {
                sprintf(string_buffer, "#EXTINF:%u,\r\n", (unsigned int)(segment_durations[i]+0.5));
            }
            playlist->WriteString(string_buffer);
            sprintf(string_buffer, Options.output, i);
            playlist->WriteString(string_buffer);
            playlist->WriteString("\r\n");
        }
                        
        playlist->WriteString("\r\n#EXT-X-ENDLIST\r\n");
        playlist->Release();
    }    

    if (Options.verbose) {
        if (video_track) {
            segment_duration = video_ts - last_ts;
        } else {
            segment_duration = audio_ts - last_ts;
        }
        printf("Conversion complete, duration=%.2f secs\n",
               segment_duration);
    }
    
end:
    if (output) output->Release();
    
    return result;
}
Пример #11
0
/******************************************************************************
METHOD:  cfmask

PURPOSE:  the main routine for fmask in C

RETURN VALUE:
Type = int
Value           Description
-----           -----------
ERROR           An error occurred during processing of the cfmask
SUCCESS         Processing was successful

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
at the USGS EROS

HISTORY:
Date        Programmer       Reason
--------    ---------------  -------------------------------------
3/15/2013   Song Guo         Original Development
5/14/2013   Song Guo         Added in Polar Stereographic support
7/17/2013   Song Guo         Added in Map info 
8/15/2013   Song Guo         Modified to use TOA reflectance file 
                             as input instead of metadata file

NOTES: type ./cfmask --help for information to run the code
******************************************************************************/
int main (int argc, char *argv[])
{
    char errstr[MAX_STR_LEN];           /* error string */
    char lndth_name[MAX_STR_LEN];       /* ledaps Brightness Temperature file */
    char fmask_name[MAX_STR_LEN];       /* output fmask binary file name */
    char fmask_header[MAX_STR_LEN];     /* output fmask binary file header */
    char fmask_hdf_name[MAX_STR_LEN];   /* output fmask HDF file name */
    char fmask_hdf_hdr[MAX_STR_LEN];    /* output fmask HDF file header */
    char *lndcal_name = NULL;           /* input lndcal data filename */
    char directory[MAX_STR_LEN];        /* input/output data directory */
    char extension[MAX_STR_LEN];        /* input TOA file extension */
    int ib;                             /* band counters */
    char sds_names[NBAND_REFL_MAX][MAX_STR_LEN]; /* array of image SDS names */
    Input_t *input = NULL;              /* input data and meta data */
    char  scene_name[MAX_STR_LEN];      /* input data scene name */
    char *hdf_grid_name = "Grid";  /* name of the grid for HDF-EOS */
    unsigned char **cloud_mask;    /* cloud pixel mask */
    unsigned char **shadow_mask;   /* shadow pixel mask */
    unsigned char **snow_mask;     /* snow pixel mask */
    unsigned char **water_mask;    /* water pixel mask */
    int status;                    /* return value from function call */
    FILE *fd = NULL;               /* file pointer */
    float ptm;                     /* percent of clear-sky pixels */
    float t_templ = 0.0;     /* percentile of low background temperature */
    float t_temph = 0.0;     /* percentile of high background temperature */
    int out_sds_types[NUM_OUT_SDS];     /* array of image SDS types */
    char *sds_name="fmask_band";        /* Fmask hdf SDS name */
    Output_t *output = NULL;            /* output structure and metadata */
    bool verbose;            /* verbose flag for printing messages */
    bool write_binary;       /* should we write raw binary output? */
    bool no_hdf_output;      /* should we don't write HDF4 output file? */
    int cldpix = 2;          /* Default buffer for cloud pixel dilate */
    int sdpix = 2;           /* Default buffer for shadow pixel dilate */
    float cloud_prob;        /* Default cloud probability */
    Space_def_t space_def;   /* spatial definition information */
    float sun_azi_temp = 0.0;/* Keep the original sun azimuth angle */
  
    time_t now;
    time(&now);
    printf("CFmask start_time=%s\n",ctime(&now));

    /* Read the command-line arguments, including the name of the input
       Landsat TOA reflectance product and the DEM */
    status = get_args (argc, argv, &lndcal_name, &cloud_prob, &cldpix,
                       &sdpix, &write_binary, &no_hdf_output, &verbose);
    if (status != SUCCESS)
    { 
        sprintf (errstr, "calling get_args");
        ERROR (errstr, "main");
    }

    split_filename(lndcal_name, directory, scene_name, extension);
    if (verbose)
        printf("directory, scene_name, extension=%s,%s,%s\n", 
            directory, scene_name, extension);
    sprintf(lndcal_name, "%slndcal.%s.hdf", directory, scene_name);
    sprintf(lndth_name, "%slndth.%s.hdf", directory, scene_name);
    sprintf(fmask_name, "%sfmask.%s.img", directory, scene_name);
    sprintf(fmask_header, "%sfmask.%s.img.hdr", directory, scene_name);
    sprintf(fmask_hdf_name, "%sfmask.%s.hdf", directory, scene_name);
    if (verbose)
    {
        printf("lndcal_name, lndth_name = %s, %s\n", lndcal_name, lndth_name); 
        printf("fmask_name, fmask_header, fmask_hdf_name = %s, %s, %s\n", 
                fmask_name, fmask_header, fmask_hdf_name); 
    }

    /* Open input file, read metadata, and set up buffers */
    input = OpenInput(lndth_name, lndcal_name);
    if (input == NULL)
    {
        sprintf (errstr, "opening the input files: %s & %s", lndth_name,
                 lndcal_name);
        ERROR (errstr, "main");
    }

    /* Get the projection and spatial information from the input TOA
       reflectance product */
    status = get_space_def_hdf(&space_def, lndcal_name, hdf_grid_name);
    if (status != SUCCESS)
    {
        sprintf(errstr, "Reading spatial metadata from the HDF file: %s", 
               lndcal_name);
        ERROR (errstr, "main");
    }
    input->meta.zone = space_def.zone;

    if (verbose)
    {
        /* Print some info to show how the input metadata works */
        printf ("DEBUG: Number of input TOA bands: %d\n", input->nband);
        printf ("DEBUG: Number of input thermal bands: %d\n", 1);
        printf ("DEBUG: Number of input lines: %d\n", input->size.l);
        printf ("DEBUG: Number of input samples: %d\n", input->size.s);
        printf ("DEBUG: Number of input TOA lines: %d\n", input->toa_size.l);
        printf ("DEBUG: Number of input TOA samples: %d\n", input->toa_size.s);
        printf ("DEBUG: Provider is %s\n", input->meta.provider);
        printf ("DEBUG: Satellite is %s\n", input->meta.sat);
        printf ("DEBUG: Instrument is %s\n", input->meta.inst);
        printf ("DEBUG: ACQUISITION_DATE.DOY is %d\n", input->meta.acq_date.doy);
        printf ("DEBUG: WRS system is %s\n", input->meta.wrs_sys);
        printf ("DEBUG: Path is %d\n", input->meta.path);
        printf ("DEBUG: Row is %d\n", input->meta.row);
        printf ("DEBUG: Fill value is %d\n", input->meta.fill);
        for (ib = 0; ib < input->nband; ib++)
        {
            printf ("DEBUG: Band %d-->\n", ib);
            printf ("DEBUG:   SDS name is %s\n", input->sds[ib].name);
            printf ("DEBUG:   SDS rank: %d\n", input->sds[ib].rank);
            printf ("DEBUG:   band satu_value_ref: %d\n", 
                   input->meta.satu_value_ref[ib]);
            printf ("DEBUG:   band satu_value_max: %d\n", 
                   input->meta.satu_value_max[ib]);
            printf ("DEBUG:   band gains: %f, band biases: %f\n", 
                    input->meta.gain[ib], input->meta.bias[ib]);      
        }
        printf ("DEBUG: Thermal Band -->\n");
        printf ("DEBUG:   SDS name is %s\n", input->therm_sds.name);
        printf ("DEBUG:   SDS rank: %d\n", input->therm_sds.rank);
        printf ("DEBUG:   therm_satu_value_ref: %d\n", 
                   input->meta.therm_satu_value_ref);
        printf ("DEBUG:   therm_satu_value_max: %d\n", 
                   input->meta.therm_satu_value_max);
        printf ("DEBUG:   therm_gain: %f, therm_bias: %f\n", 
                input->meta.gain_th, input->meta.bias_th);

        printf("DEBUG: ROW is %d\n", input->meta.row);
        printf("DEBUG: PATH is %d\n", input->meta.path);
        printf("DEBUG: SUN AZIMUTH is %f\n", input->meta.sun_az);
        printf("DEBUG: SUN ZENITH is %f\n", input->meta.sun_zen);
        printf("DEBUG: Projection Zone is %d\n", input->meta.zone);

        printf("DEBUG: unit_ref is %s\n", input->meta.unit_ref);
        printf("DEBUG: valid_range_ref is %f & %f\n", 
           input->meta.valid_range_ref[0], 
           input->meta.valid_range_ref[1]);
    }

    /* If the scene is an ascending polar scene (flipped upside down), then
       the solar azimuth needs to be adjusted by 180 degrees.  The scene in
       this case would be north down and the solar azimuth is based on north
       being up clock-wise direction. Flip the south to be up will not change 
       the actual sun location, with the below relations, the solar azimuth
       angle will need add in 180.0 for correct sun location */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
    {
        /* Keep the original solar azimuth angle */
        sun_azi_temp = input->meta.sun_az;
        input->meta.sun_az += 180.0;
        if ((input->meta.sun_az - 360.0) > MINSIGMA)
            input->meta.sun_az -= 360.0;
        if (verbose)
            printf ("  Polar or ascending scene.  Readjusting solar azimuth by "
                "180 degrees.\n  New value: %f degrees\n", input->meta.sun_az);
    }

    /* Copy the SDS names and QA SDS names from the input structure for the
       output structure, since we are simply duplicating the input */
    for (ib = 0; ib < input->nband; ib++)
        strcpy (&sds_names[ib][0], input->sds[ib].name);

    /* Dynamic allocate the 2d mask memory */
    cloud_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    shadow_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    snow_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    water_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    if (cloud_mask == NULL  || shadow_mask == NULL || snow_mask == NULL
        || water_mask == NULL)
    {
        sprintf (errstr, "Allocating mask memory");
        ERROR (errstr, "main");
    }

    /* Build the potential cloud, shadow, snow, water mask */
    status = potential_cloud_shadow_snow_mask(input, cloud_prob, &ptm,
             &t_templ, &t_temph, cloud_mask, shadow_mask, snow_mask, 
             water_mask, verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing potential_cloud_shadow_snow_mask");
        ERROR (errstr, "main");
    }

    printf("Pcloud done, starting cloud/shadow match\n");


    /* Build the final cloud shadow based on geometry matching and
       combine the final cloud, shadow, snow, water masks into fmask */
    status = object_cloud_shadow_match(input, ptm, t_templ, t_temph,
             cldpix, sdpix, cloud_mask, shadow_mask, snow_mask, water_mask,
             verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing object_cloud_and_shadow_match");
        ERROR (errstr, "main");
    }    

    status = free_2d_array((void **)shadow_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }
    status = free_2d_array((void **)snow_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }
    status = free_2d_array((void **)water_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }

    /* Reassign solar azimuth angle for output purpose if south up north 
       down scene is involved */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
        input->meta.sun_az = sun_azi_temp;
   
    if (write_binary)
    {
        /* Create an ENVI header file for the binary fmask */
        status = write_envi_hdr(fmask_header, BINARY_FILE, input, &space_def);
        if (status != SUCCESS)
        {
            sprintf(errstr, "Creating ENVI header for binary fmask");
            ERROR (errstr, "main");
        }

        /* Open the mask file for writing */
        fd = fopen(fmask_name, "w"); 
        if (fd == NULL)
        {
            sprintf(errstr, "Opening report file: %s", fmask_name);
            ERROR (errstr, "main");
        }

        /* Write out the mask file */
        status = fwrite(&cloud_mask[0][0], sizeof(unsigned char),
            input->size.l * input->size.s, fd);
        if (status != input->size.l * input->size.s)
        {
            sprintf(errstr, "Writing to %s", fmask_name);
            ERROR (errstr, "main");
        }

        /* Close the mask file */
        status = fclose(fd);
        if (status)
        {
            sprintf(errstr, "Closing file %s", fmask_name);
            ERROR (errstr, "main");
        }
    }

    if (!no_hdf_output)
    {
        /* Create and open fmask HDF output file */
        if (!CreateOutput(fmask_hdf_name))
        {
            sprintf(errstr, "Creating HDF fmask output file");
            ERROR (errstr, "main");
        }
        output = OpenOutput (fmask_hdf_name, sds_name, &input->size);
        if (output == NULL)
        {
            sprintf (errstr, "opening output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }

        if (!PutOutput(output, cloud_mask))
        {
            sprintf (errstr, "Writing output fmask in HDF files\n");
            ERROR (errstr, "main");
        }

        if (!PutMetadata(output, input))
        {
            sprintf (errstr, "Writing output fmask metadata in HDF files\n");
            ERROR (errstr, "main");
        }

        /* Close the output file and free the structure */
        if (!CloseOutput (output))
        {
            sprintf (errstr, "closing output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }
        if (!FreeOutput (output))
        {
            sprintf (errstr, "freeing output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }

        /* Write the spatial information, after the file has been closed */
        out_sds_types[0] = DFNT_UINT8;
        status = put_space_def_hdf (&space_def, fmask_hdf_name, NUM_OUT_SDS, 
            out_sds_names, out_sds_types, hdf_grid_name);
        if (status != SUCCESS)
        {
            sprintf("Putting spatial metadata to the HDF file: "
            "%s", lndcal_name);
            ERROR (errstr, "main");
        }

        /* Write CFmask HDF header to add in envi map info */
        sprintf (fmask_hdf_hdr, "%s.hdr", fmask_hdf_name);
        status = write_envi_hdr (fmask_hdf_hdr, HDF_FILE, input, &space_def);
        if (status != SUCCESS)
        {
            sprintf(errstr, "Error writing the ENVI header for CFmask HDF hdr");
            ERROR (errstr, "main");
        }
    }

    /* Free the final output cloud_mask */
    status = free_2d_array((void **)cloud_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }

    /* Close the input file and free the structure */
    CloseInput (input);
    FreeInput (input);

    free(lndcal_name);
    printf ("Processing complete.\n");
    time(&now);
    printf("CFmask end_time=%s\n",ctime(&now));
    return (SUCCESS);
}
Пример #12
0
/******************************************************************************
METHOD:  l8cfmask

PURPOSE:  the main routine for fmask in C

RETURN VALUE:
Type = int
Value           Description
-----           -----------
ERROR           An error occurred during processing of the l8cfmask
SUCCESS         Processing was successful

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
at the USGS EROS

HISTORY:
Date        Programmer       Reason
--------    ---------------  -------------------------------------
3/15/2013   Song Guo         Original Development
5/14/2013   Song Guo         Added in Polar Stereographic support
7/17/2013   Song Guo         Added in Map info 
8/15/2013   Song Guo         Modified to use TOA reflectance file 
                             as input instead of metadata file
Oct/2014    Ron Dilley       Modified to utilize the ESPA internal raw binary
                             file format

NOTES: type ./l8cfmask --help for information to run the code
******************************************************************************/
int
main (int argc, char *argv[])
{
    char errstr[MAX_STR_LEN];     /* error string */
    char *cptr = NULL;            /* pointer to the file extension */
    char *xml_name = NULL;        /* input XML filename */
    char directory[MAX_STR_LEN];  /* input/output data directory */
    char extension[MAX_STR_LEN];  /* input TOA file extension */
    int ib;                       /* band counters */
    Input_t *input = NULL;        /* input data and meta data */
    char envi_file[MAX_STR_LEN];  /* output ENVI file name */
    char scene_name[MAX_STR_LEN]; /* input data scene name */
    unsigned char **pixel_mask;   /* pixel mask */
    unsigned char **conf_mask;    /* confidence mask */
    int status;               /* return value from function call */
    float clear_ptm;          /* percent of clear-sky pixels */
    float t_templ = 0.0;      /* percentile of low background temperature */
    float t_temph = 0.0;      /* percentile of high background temperature */
    Output_t *output = NULL;  /* output structure and metadata */
    bool verbose;             /* verbose flag for printing messages */
    bool use_l8_cirrus;       /* should we use L8 cirrus cloud bit results? */
    int cldpix = 2;           /* Default buffer for cloud pixel dilate */
    int sdpix = 2;            /* Default buffer for shadow pixel dilate */
    float cloud_prob;         /* Default cloud probability */
    float sun_azi_temp = 0.0; /* Keep the original sun azimuth angle */
    int max_cloud_pixels; /* Maximum cloud pixel number in cloud division */
    Espa_internal_meta_t xml_metadata; /* XML metadata structure */
    Envi_header_t envi_hdr;            /* output ENVI header information */

    time_t now;
    time (&now);
    printf ("CFmask start_time=%s\n", ctime (&now));

    /* Read the command-line arguments, including the name of the input
       Landsat TOA reflectance product and the DEM */
    status = get_args (argc, argv, &xml_name, &cloud_prob, &cldpix,
                       &sdpix, &max_cloud_pixels, &use_l8_cirrus, &verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "calling get_args");
        CFMASK_ERROR (errstr, "main");
    }

    /* Validate the input metadata file */
    if (validate_xml_file (xml_name) != SUCCESS)
    {                           /* Error messages already written */
        CFMASK_ERROR (errstr, "main");
    }

    /* Initialize the metadata structure */
    init_metadata_struct (&xml_metadata);

    /* Parse the metadata file into our internal metadata structure; also
       allocates space as needed for various pointers in the global and band
       metadata */
    if (parse_metadata (xml_name, &xml_metadata) != SUCCESS)
    {                           /* Error messages already written */
        CFMASK_ERROR (errstr, "main");
    }

    /* Verify supported satellites */
    if (strcmp (xml_metadata.global.satellite, "LANDSAT_8") != 0)
    {
        sprintf (errstr, "Unsupported satellite sensor");
        CFMASK_ERROR (errstr, "main");
    }

    /* Split the filename to obtain the directory, scene name, and extension */
    split_filename (xml_name, directory, scene_name, extension);
    if (verbose)
        printf ("directory, scene_name, extension=%s,%s,%s\n",
                directory, scene_name, extension);

    /* Open input file, read metadata, and set up buffers */
    input = OpenInput (&xml_metadata);
    if (input == NULL)
    {
        sprintf (errstr, "opening the TOA and brightness temp files in: %s",
                 xml_name);
        CFMASK_ERROR (errstr, "main");
    }

    if (verbose)
    {
        /* Print some info to show how the input metadata works */
        printf ("DEBUG: Number of input TOA bands: %d\n", input->nband);
        printf ("DEBUG: Number of input thermal bands: %d\n", 1);
        printf ("DEBUG: Number of input TOA lines: %d\n", input->size.l);
        printf ("DEBUG: Number of input TOA samples: %d\n", input->size.s);
        printf ("DEBUG: ACQUISITION_DATE.DOY is %d\n",
                input->meta.acq_date.doy);
        printf ("DEBUG: Fill value is %d\n", input->meta.fill);
        for (ib = 0; ib < input->nband; ib++)
        {
            printf ("DEBUG: Band %d-->\n", ib);
            printf ("DEBUG:   band satu_value_ref: %d\n",
                    input->meta.satu_value_ref[ib]);
            printf ("DEBUG:   band satu_value_max: %d\n",
                    input->meta.satu_value_max[ib]);
            printf ("DEBUG:   band gains: %f, band biases: %f\n",
                    input->meta.gain[ib], input->meta.bias[ib]);
        }
        printf ("DEBUG: Thermal Band (Band 10) -->\n");
        printf ("DEBUG:   therm_satu_value_ref: %d\n",
                input->meta.therm_satu_value_ref);
        printf ("DEBUG:   therm_satu_value_max: %d\n",
                input->meta.therm_satu_value_max);
        printf ("DEBUG:   therm_gain: %f, therm_bias: %f\n",
                input->meta.gain_th, input->meta.bias_th);

        printf ("DEBUG: SUN AZIMUTH is %f\n", input->meta.sun_az);
        printf ("DEBUG: SUN ZENITH is %f\n", input->meta.sun_zen);
    }

    /* If the scene is an ascending polar scene (flipped upside down), then
       the solar azimuth needs to be adjusted by 180 degrees.  The scene in
       this case would be north down and the solar azimuth is based on north
       being up clock-wise direction. Flip the south to be up will not change
       the actual sun location, with the below relations, the solar azimuth
       angle will need add in 180.0 for correct sun location */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
    {
        /* Keep the original solar azimuth angle */
        sun_azi_temp = input->meta.sun_az;
        input->meta.sun_az += 180.0;
        if ((input->meta.sun_az - 360.0) > MINSIGMA)
            input->meta.sun_az -= 360.0;
        if (verbose)
            printf ("  Polar or ascending scene."
                    "  Readjusting solar azimuth by 180 degrees.\n"
                    "  New value: %f degrees\n", input->meta.sun_az);
    }

    /* Dynamic allocate the 2d mask memory */
    pixel_mask = (unsigned char **) allocate_2d_array (input->size.l,
                                                       input->size.s,
                                                       sizeof (unsigned char));
    if (pixel_mask == NULL)
    {
        sprintf (errstr, "Allocating pixel mask memory");
        CFMASK_ERROR (errstr, "main");
    }

    conf_mask = (unsigned char **) allocate_2d_array (input->size.l,
                                                      input->size.s,
                                                      sizeof (unsigned char));
    if (conf_mask == NULL)
    {
        sprintf (errstr, "Allocating confidence mask memory");
        CFMASK_ERROR (errstr, "main");
    }

    /* Initialize the mask to clear data */
    int row, col;
    for (row = 0; row < input->size.l; row++)
        for (col = 0; col < input->size.s; col++)
        {
            pixel_mask[row][col] = MASK_CLEAR_LAND;
            conf_mask[row][col] = CLOUD_CONFIDENCE_NONE;
        }

    /* Build the potential cloud, shadow, snow, water mask */
    status = potential_cloud_shadow_snow_mask (input, cloud_prob, &clear_ptm,
                                               &t_templ, &t_temph, pixel_mask,
                                               conf_mask, use_l8_cirrus,
                                               verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing potential_cloud_shadow_snow_mask");
        CFMASK_ERROR (errstr, "main");
    }

    printf ("Pcloud done, starting cloud/shadow match\n");

    /* Build the final cloud shadow based on geometry matching and
       combine the final cloud, shadow, snow, water masks into fmask
       the pixel_mask is a bit mask as input and a value mask as output */
    status = object_cloud_shadow_match (input, clear_ptm, t_templ, t_temph,
                                        cldpix, sdpix, max_cloud_pixels,
                                        pixel_mask, verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing object_cloud_and_shadow_match");
        CFMASK_ERROR (errstr, "main");
    }

    /* Reassign solar azimuth angle for output purpose if south up north
       down scene is involved */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
        input->meta.sun_az = sun_azi_temp;

    /* Open the output file */
    output = OpenOutput (&xml_metadata, input);
    if (output == NULL)
    {                           /* error message already printed */
        sprintf (errstr, "Opening output file");
        CFMASK_ERROR (errstr, "main");
    }

    if (!PutOutput (output, pixel_mask))
    {
        sprintf (errstr, "Writing output fmask files\n");
        CFMASK_ERROR (errstr, "main");
    }

    /* Close the output file */
    if (!CloseOutput (output))
    {
        sprintf (errstr, "closing output file");
        CFMASK_ERROR (errstr, "main");
    }

    /* Create the ENVI header file this band */
    if (create_envi_struct (&output->metadata.band[0], &xml_metadata.global,
                            &envi_hdr) != SUCCESS)
    {
        sprintf (errstr, "Creating ENVI header structure.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Write the ENVI header */
    strcpy (envi_file, output->metadata.band[0].file_name);
    cptr = strchr (envi_file, '.');
    if (cptr == NULL)
    {
        sprintf (errstr, "error in ENVI header filename");
        CFMASK_ERROR (errstr, "main");
    }

    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
    {
        sprintf (errstr, "Writing ENVI header file.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Append the cfmask band to the XML file */
    if (append_metadata (output->nband, output->metadata.band, xml_name)
        != SUCCESS)
    {
        sprintf (errstr, "Appending spectral index bands to XML file.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Free the structure */
    if (!FreeOutput (output))
    {
        sprintf (errstr, "freeing output file structure");
        CFMASK_ERROR (errstr, "main");
    }

    output = OpenOutputConfidence (&xml_metadata, input);
    if (output == NULL)
    {                           /* error message already printed */
        sprintf (errstr, "Opening output file");
        CFMASK_ERROR (errstr, "main");
    }

    if (!PutOutput (output, conf_mask))
    {
        sprintf (errstr, "Writing output fmask files\n");
        CFMASK_ERROR (errstr, "main");
    }

    /* Close the output file */
    if (!CloseOutput (output))
    {
        sprintf (errstr, "closing output file");
        CFMASK_ERROR (errstr, "main");
    }

    /* Create the ENVI header file this band */
    if (create_envi_struct (&output->metadata.band[0], &xml_metadata.global,
                            &envi_hdr) != SUCCESS)
    {
        sprintf (errstr, "Creating ENVI header structure.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Write the ENVI header */
    strcpy (envi_file, output->metadata.band[0].file_name);
    cptr = strchr (envi_file, '.');
    if (cptr == NULL)
    {
        sprintf (errstr, "error in ENVI header filename");
        CFMASK_ERROR (errstr, "main");
    }

    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
    {
        sprintf (errstr, "Writing ENVI header file.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Append the cfmask band to the XML file */
    if (append_metadata (output->nband, output->metadata.band, xml_name)
        != SUCCESS)
    {
        sprintf (errstr, "Appending spectral index bands to XML file.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Free the structure */
    if (!FreeOutput (output))
    {
        sprintf (errstr, "freeing output file structure");
        CFMASK_ERROR (errstr, "main");
    }

    /* Free the metadata structure */
    free_metadata (&xml_metadata);

    /* Free the pixel mask */
    status = free_2d_array ((void **) pixel_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing pixel mask memory");
        CFMASK_ERROR (errstr, "main");
    }

    status = free_2d_array ((void **) conf_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing confidence mask memory");
        CFMASK_ERROR (errstr, "main");
    }

    /* Close the input file and free the structure */
    CloseInput (input);
    FreeInput (input);

    free (xml_name);

    printf ("Processing complete.\n");
    time (&now);
    printf ("CFmask end_time=%s\n", ctime (&now));

    return SUCCESS;
}
Пример #13
0
int main (int argc, const char **argv) {
  Param_t *param;
  Input_t *input;
  Input_t *input_th;
  Lut_t *lut;
  Output_t *output;
  int i,odometer_flag=0;

  /* Space info */
  Space_def_t space_def;
  Space_t *space;
  char *grid_name = "Grid";
  char *sds_names[NSDS];
  int sds_types[NSDS];

  printf ("\nRunning lndcsm ...\n");
  for (i=1; i<argc; i++)if ( !strcmp(argv[i],"-o") )odometer_flag=1;
  param = GetParam(argc, argv);
  if (param == (Param_t *)NULL) ERROR("getting runtime parameters", "main");

  /* Open input file */

  input = OpenInput(param->input_file_name);
  if (input == (Input_t *)NULL) ERROR("bad input file", "main");

  /* Get space definition from HDF */

  if (!GetSpaceDefHDF(&space_def, param->input_file_name, grid_name))
    ERROR("getting definition from file", "main");

  /* Setup Space */

  space = SetupSpace(&space_def);
  if (space == (Space_t *)NULL) ERROR("setting up space", "main");

  /* Open input thermal file */
  if ( param->therm_flag )
    {
    input_th = OpenInput(param->input_therm_file_name);
    if (input_th == (Input_t *)NULL) ERROR("bad thermal input file", "main");

  if ( input_th->size.s != input->size.s || input_th->size.l != input->size.l )
     ERROR("mismatch size on inputs (reflective vs thermal)", "main");
    }

  /* Get Lookup table */
  lut = GetLut(param->lut_file_name, input->nband, &input->meta);
  if (lut == (Lut_t *)NULL) ERROR("bad lut file", "main");
  
  /* Create and open output file */

  if (!CreateOutput(param->output_file_name))
    ERROR("creating output file", "main");

  output = OpenOutput( param->output_file_name, &input->size );
  if (output == (Output_t *)NULL) ERROR("opening output file", "main");

  /******************************************************************/
  /*                         call snow mask                         */
  /******************************************************************/

  if ( !CloudMask( 
                  input 
                , input_th
                , output
                , lut
                , param
                , odometer_flag
		  ) )
     ERROR("In Cloud/Snow Mask Computation","main");

  if (!PutMetadata(output, &input->meta,lut, param) )
    ERROR("writing the metadata", "main");


  if (!CloseInput(input)) ERROR("closing input file", "main");

  if (!CloseOutput(output)) ERROR("closing output file", "main");

  sds_names[0] = output->sds_csm[0].name;
  sds_types[0] = output->sds_csm[0].type;

  if (!PutSpaceDefHDF(&space_def, param->output_file_name, 1, 
                      sds_names, sds_types, grid_name))
    ERROR("putting space metadata in HDF file", "main");

  if (!FreeInput(input)) 
    ERROR("freeing input file stucture", "main");

  if (!FreeLut(lut)) 
    ERROR("freeing lut file stucture", "main");

  if (!FreeOutput(output)) 
    ERROR("freeing output file stucture", "main");

  /* All done */

  printf ("lndcsm complete.\n");

  return (EXIT_SUCCESS);
}
Пример #14
0
/******************************************************************************
METHOD:  lst

PURPOSE:  The main routine for scene based LST (Land Surface Temperature).

RETURN VALUE:
Type = int
Value           Description
-----           -----------
ERROR           An error occurred during processing of the scene_based_lst
SUCCESS         Processing was successful

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
          at the USGS EROS
******************************************************************************/
int
main (int argc, char *argv[])
{
    char FUNC_NAME[] = "main";

    Espa_internal_meta_t xml_metadata;  /* XML metadata structure */

    char msg_str[MAX_STR_LEN];
    char xml_filename[PATH_MAX];        /* input XML filename */
    char dem_filename[PATH_MAX];        /* input DEM filename */
    char emissivity_filename[PATH_MAX]; /* input Emissivity filename */
    char command[PATH_MAX];

    Input_t *input = NULL;          /* input data and meta data */
    //    Output_t *output = NULL; /* output structure and metadata */

    bool use_tape6;             /* Use the tape6 output */
    bool verbose;               /* verbose flag for printing messages */
    bool debug;                 /* debug flag for debug output */

    int modtran_run;

    double alb = 0.1;
    double **modtran_results = NULL;

    char *tmp_env = NULL;

    REANALYSIS_POINTS points;

    time_t now;

    /* Display the starting time of the application */
    time (&now);
    snprintf (msg_str, sizeof(msg_str),
              "LST start_time [%s]", ctime (&now));
    LOG_MESSAGE (msg_str, FUNC_NAME);

    /* Read the command-line arguments, including the name of the input
       Landsat TOA reflectance product and the DEM */
    if (get_args (argc, argv, xml_filename, dem_filename, emissivity_filename,
                  &use_tape6, &verbose, &debug) != SUCCESS)
    {
        RETURN_ERROR ("calling get_args", FUNC_NAME, EXIT_FAILURE);
    }

    /* Verify the existence of required environment variables */
    /* Grab the environment path to the LST_DATA_DIR */
    tmp_env = getenv ("LST_DATA_DIR");
    if (tmp_env == NULL)
    {
        RETURN_ERROR ("LST_DATA_DIR environment variable is not set",
                      FUNC_NAME, EXIT_FAILURE);
    }

    /* Validate the input metadata file */
    if (validate_xml_file (xml_filename) != SUCCESS)
    {
        /* Error messages already written */
        return EXIT_FAILURE;
    }

    /* Initialize the metadata structure */
    init_metadata_struct (&xml_metadata);

    /* Parse the metadata file into our internal metadata structure; also
       allocates space as needed for various pointers in the global and band
       metadata */
    if (parse_metadata (xml_filename, &xml_metadata) != SUCCESS)
    {
        /* Error messages already written */
        return EXIT_FAILURE;
    }

    /* Open input file, read metadata, and set up buffers */
    input = OpenInput (&xml_metadata);
    if (input == NULL)
    {
        RETURN_ERROR ("opening input files", FUNC_NAME, EXIT_FAILURE);
    }

    if (verbose)
    {
        /* Print some info to show how the input metadata works */
        printf ("Satellite: %d\n", input->meta.satellite);
        printf ("Instrument: %d\n", input->meta.instrument);

        printf ("Number of input lines: %d\n", input->thermal.size.l);
        printf ("Number of input samples: %d\n", input->thermal.size.s);

        printf ("Fill value is %d\n", input->thermal.fill_value);

        printf ("Thermal Band -->\n");
        printf ("  therm_gain: %f\n  therm_bias: %f\n",
                input->thermal.rad_gain, input->thermal.rad_bias);

        printf ("Year, Month, Day, Hour, Minute, Second:"
                " %d, %d, %d, %d, %d, %f\n",
                input->meta.acq_date.year, input->meta.acq_date.month,
                input->meta.acq_date.day, input->meta.acq_date.hour,
                input->meta.acq_date.minute, input->meta.acq_date.second);
        printf ("ACQUISITION_DATE.DOY is %d\n",
                input->meta.acq_date.doy);

        printf ("UL_MAP_CORNER: %f, %f\n", input->meta.ul_map_corner.x,
                input->meta.ul_map_corner.y);
        printf ("LR_MAP_CORNER: %f, %f\n", input->meta.lr_map_corner.x,
                input->meta.lr_map_corner.y);
        printf ("UL_GEO_CORNER: %f, %f\n",
                input->meta.ul_geo_corner.lat, input->meta.ul_geo_corner.lon);
        printf ("LR_GEO_CORNER: %f, %f\n",
                input->meta.lr_geo_corner.lat, input->meta.lr_geo_corner.lon);
    }

    /* Build the points that will be used */
    if (build_points (input, &points) != SUCCESS)
    {
        RETURN_ERROR ("Building POINTS input\n", FUNC_NAME, EXIT_FAILURE);
    }

    if (verbose)
    {
        printf ("Number of Points: %d\n", points.num_points);
    }

    /* Call build_modtran_input to generate the tape5 file input and
       the MODTRAN commands for each point and height */
    if (build_modtran_input (input, &points, verbose, debug)
        != SUCCESS)
    {
        RETURN_ERROR ("Building MODTRAN input\n", FUNC_NAME, EXIT_FAILURE);
    }

    /* Perform MODTRAN runs by calling each command */
    for (modtran_run = 0; modtran_run < points.num_modtran_runs; modtran_run++)
    {
        snprintf (msg_str, sizeof(msg_str),
                  "Executing MODTRAN [%s]",
                   points.modtran_runs[modtran_run].command);
        LOG_MESSAGE (msg_str, FUNC_NAME);

#if RUN_MODTRAN
        if (system (points.modtran_runs[modtran_run].command) != SUCCESS)
        {
            RETURN_ERROR ("Error executing MODTRAN", FUNC_NAME,
                          EXIT_FAILURE);
        }
#endif
    }

    /* PARSING MODTRAN RESULTS:
       for each case in caseList (for each modtran run),
       parse wavelength and total radiance from tape6 file into parsed */
    for (modtran_run = 0; modtran_run < points.num_modtran_runs; modtran_run++)
    {
        if (use_tape6)
        {
            /* Use modtran generated tape6 output */
            snprintf (command, sizeof (command),
                      "lst_extract_modtran_results.py"
                      " --tape6"
                      " --input-path %s"
                      " --output-path %s",
                      points.modtran_runs[modtran_run].path,
                      points.modtran_runs[modtran_run].path);
        }
        else
        {
            /* Use modtran generated pltout.asc output */
            snprintf (command, sizeof (command),
                      "lst_extract_modtran_results.py"
                      " --pltout"
                      " --input-path %s"
                      " --output-path %s",
                      points.modtran_runs[modtran_run].path,
                      points.modtran_runs[modtran_run].path);
        }

        snprintf (msg_str, sizeof(msg_str), "Executing [%s]", command);
        LOG_MESSAGE (msg_str, FUNC_NAME);

#if EXTRACT_TAPE6_RESULTS
        if (system (command) != SUCCESS)
        {
            RETURN_ERROR ("Failed executing lst_extract_tape6_results.py",
                          FUNC_NAME, EXIT_FAILURE);
        }
#endif
    }

    /* Allocate memory for MODTRAN results */
    modtran_results =
        (double **) allocate_2d_array (points.num_points * NUM_ELEVATIONS,
                                       MGPE_NUM_ELEMENTS, sizeof (double));
    if (modtran_results == NULL)
    {
        RETURN_ERROR ("Allocating MODTRAN results memory", FUNC_NAME,
                      EXIT_FAILURE);
    }

    /* Generate parameters for each height and NARR point */
    if (calculate_point_atmospheric_parameters (input, &points, alb,
                                                modtran_results, verbose)
        != SUCCESS)
    {
        RETURN_ERROR ("Calculating point atmospheric parameters\n",
                      FUNC_NAME, EXIT_FAILURE);
    }

    /* Generate parameters for each Landsat pixel */
    if (calculate_pixel_atmospheric_parameters (input, &points,
                                                xml_filename,
                                                dem_filename,
                                                emissivity_filename,
                                                modtran_results, verbose)
        != SUCCESS)
    {
        RETURN_ERROR ("Calculating per/pixel atmospheric parameters\n",
                      FUNC_NAME, EXIT_FAILURE);
    }

    /* Free memory allocation */
    free_points_memory (&points);

#if NOT_TESTED
    /* Open the output file */
    output = OpenOutput (&xml_metadata, input);
    if (output == NULL)
    {                           /* error message already printed */
        RETURN_ERROR ("Opening output file", FUNC_NAME, EXIT_FAILURE);
    }

    if (!PutOutput (output, pixel_mask))
    {
        RETURN_ERROR ("Writing output LST in HDF files\n", FUNC_NAME,
                      EXIT_FAILURE);
    }

    /* Close the output file */
    if (!CloseOutput (output))
    {
        RETURN_ERROR ("closing output file", FUNC_NAME, EXIT_FAILURE);
    }

    /* Create the ENVI header file this band */
    if (create_envi_struct (&output->metadata.band[0], &xml_metadata.global,
                            &envi_hdr) != SUCCESS)
    {
        RETURN_ERROR ("Creating ENVI header structure.", FUNC_NAME,
                      EXIT_FAILURE);
    }

    /* Write the ENVI header */
    strcpy (envi_file, output->metadata.band[0].file_name);
    cptr = strchr (envi_file, '.');
    if (cptr == NULL)
    {
        RETURN_ERROR ("error in ENVI header filename", FUNC_NAME,
                      EXIT_FAILURE);
    }

    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
    {
        RETURN_ERROR ("Writing ENVI header file.", FUNC_NAME, EXIT_FAILURE);
    }

    /* Append the LST band to the XML file */
    if (append_metadata (output->nband, output->metadata.band, xml_filename)
        != SUCCESS)
    {
        RETURN_ERROR ("Appending spectral index bands to XML file.",
                      FUNC_NAME, EXIT_FAILURE);
    }

    /* Free the structure */
    if (!FreeOutput (output))
    {
        RETURN_ERROR ("freeing output file structure", FUNC_NAME,
                      EXIT_FAILURE);
    }
#endif

    /* Free the metadata structure */
    free_metadata (&xml_metadata);

    /* Close the input file and free the structure */
    CloseInput (input);
    FreeInput (input);

    /* Free memory allocations */
    if (free_2d_array ((void **) modtran_results) != SUCCESS)
    {
        RETURN_ERROR ("Freeing memory: MODTRAN results\n", FUNC_NAME,
                      EXIT_FAILURE);
    }

    if (!debug)
    {
        /* Delete temporary file */
        if (unlink ("atmospheric_parameters.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting atmospheric_parameters.txt files\n",
                          FUNC_NAME, EXIT_FAILURE);
        }

        if (unlink ("base_head.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting baseHead.txt files\n", FUNC_NAME,
                          EXIT_FAILURE);
        }

        if (unlink ("new_tail.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting newTail.txt files\n", FUNC_NAME,
                          EXIT_FAILURE);
        }

        if (unlink ("temp_layers.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting tempLayers.txt file\n", FUNC_NAME,
                          EXIT_FAILURE);
        }

        if (unlink ("used_points.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting used_points.txt file\n", FUNC_NAME,
                          EXIT_FAILURE);
        }
    }

    time (&now);
    snprintf (msg_str, sizeof(msg_str),
              "scene_based_lst end_time=%s\n", ctime (&now));
    LOG_MESSAGE (msg_str, FUNC_NAME);

    return EXIT_SUCCESS;
}
Пример #15
0
int main(int argc, char** argv) {
    Opt2 opt(argc, argv, "rxa:z:q", IntRange(1, 999999), "hexdump=x,recursive=r,quiet=q,prepend=z,append=a");
    bool hexdump         = opt.Has('x', "- produce hexdump");
    const bool recursive = opt.Has('r', "- read all files under each directory, recursively");
    Quiet                = opt.Has('q', "- do not output progress on stderr.");
    const Stroka prepend = opt.Arg('z', "<PREFIX> - prepend string to output verbatim", 0);
    const Stroka append  = opt.Arg('a', "<SUFFIX> - append string to output verbatim", 0);
    opt.AutoUsageErr("<file>..."); // "Files or directories to archive."

    yvector<TRec> recs;
    for (size_t i = 0; i != opt.Pos.size(); ++i) {
        Stroka path = opt.Pos[i];
        size_t off = 0;
#ifdef _win_
        if (path[0] > 0 && isalpha(path[0]) && path[1] == ':')
            off = 2; // skip drive letter ("d:")
#endif // _win_
        const size_t pos = path.find(':', off);
        TRec cur;
        cur.Path = path.substr(0, pos);
        if (pos != Stroka::npos)
            cur.Prefix = path.substr(pos + 1);
        cur.Recursive = recursive;
        cur.Fix();
        recs.push_back(cur);
    }

    try {
        TAutoPtr<TOutputStream> outf(OpenOutput(""));
        TOutputStream* out = outf.Get();
        THolder<TOutputStream> hexout;

        if (hexdump) {
            hexout.Reset(new THexOutput(out));
            out = hexout.Get();
        }

        outf->Write(~prepend, +prepend);

        TArchiveWriter w(out);

        for (yvector<TRec>::const_iterator it = recs.begin(); it != recs.end(); ++it) {
            const TRec& rec = *it;

            if (IsDir(rec.Path)) {
                it->Recurse(w);
            } else {
                Append(w, it->Path, it->Prefix + "/" + GetFile(it->Path));
            }
        }

        w.Finish();
        outf->Write(~append, +append);
        out->Finish();
    } catch (...) {
        Cerr << CurrentExceptionMessage() << Endl;

        return 1;
    }

    return 0;
}
void ProcessWebInterfaceModels() {
	string Filename(GetParameter("web interface model directory"));
	Filename.append("ModelList.txt");
	vector<string> Lines = ReadStringsFromFile(Filename,false);

	//this database will hold a combination of all of the model compounds and reactions
	Data* CompleteDatabase = new Data(0);

	//the following structures will hold the data for the various models as they are read in
	vector<Data*> Models;
	vector< map<string, Identity*, std::less<string> > > ModelGenes;
	map<string, vector<string>, std::less<string> > ReactionGenes;
	map<string, vector<string>, std::less<string> > ReactionModels;
	map<string, vector<string>, std::less<string> > CompoundModels;
	//Reading in the models and storing the data in the above structures
	for (int i=1; i < int(Lines.size()); i++) {
		cout << i << endl;
		//Parsing the model list line
		vector<string>* Strings = StringToStrings(Lines[i], "\t");
		
		if (Strings->size() >= 4) {
			Filename.assign(GetParameter("web interface model directory"));
			Filename.append((*Strings)[3]);
		}

		//Loading each model from the combined model file
		if (FileExists(Filename)) {
			map<string, Identity*, std::less<string> > CurrentGeneData;
			ModelGenes.push_back(CurrentGeneData);
			Data* NewData = new Data(0);
			Models.push_back(NewData);
			NewData->AddData("NAME",(*Strings)[0].data(),STRING);
			NewData->AddData("AUTHORS",(*Strings)[1].data(),STRING);
			NewData->AddData("ORGANISMS NAME",(*Strings)[2].data(),STRING);
			NewData->AddData("FILENAME",(*Strings)[3].data(),STRING);
			NewData->LoadSystem(Filename);
			//Loading additional data on compounds and reactions from the centralized database
			int TotalTransported = 0;
			for (int j=0; j < NewData->FNumSpecies(); j++) {
				if (NewData->GetSpecies(j)->FExtracellular()) {
					TotalTransported++;
				}
				NewData->GetSpecies(j)->LoadSpecies(NewData->GetSpecies(j)->GetData("DATABASE",STRING));
				CompoundModels[NewData->GetSpecies(j)->Query("CGI_ID")].push_back((*Strings)[0]);
				if (CompleteDatabase->FindSpecies("DATABASE;CGI_ID",NewData->GetSpecies(j)->Query("CGI_ID").data()) == NULL && CompleteDatabase->FindSpecies("DATABASE;CGI_ID",NewData->GetSpecies(j)->GetData("DATABASE",STRING).data()) == NULL) {
					CompleteDatabase->AddSpecies(NewData->GetSpecies(j));
				}
			}
			//Note that the reaction equation that was read in was cleared in favor of the database equation... this could lead to problems
			int TotalReactionsWithGenes = 0;
			for (int j=0; j < NewData->FNumReactions(); j++) {
				NewData->GetReaction(j)->LoadReaction(NewData->GetReaction(j)->GetData("DATABASE",STRING));
				ReactionModels[NewData->GetReaction(j)->Query("CGI_ID")].push_back((*Strings)[0]);
				//Capturing the gene data for entry into the gene hash
				vector<string> GeneData = NewData->GetReaction(j)->GetAllData("ASSOCIATED PEG",DATABASE_LINK);
				if (GeneData.size() > 0) {
					TotalReactionsWithGenes++;
				}
				for (int k=0; k < int(GeneData.size()); k++) {
					//Adding the gene to the reaction gene hash
					string GeneName((*Strings)[2]);
					GeneName.append(":");
					GeneName.append(GeneData[k]);
					ReactionGenes[NewData->GetReaction(j)->Query("CGI_ID")].push_back(GeneName);
					//Finding the gene and creating a new gene if the gene does not currently exist
					Identity* CurrentGene = ModelGenes[ModelGenes.size()-1][GeneData[k]];
					if (CurrentGene == NULL) {
						CurrentGene = new Identity;
						ModelGenes[ModelGenes.size()-1][GeneData[k]] = CurrentGene;
					}
					//Adding the reaction data to the gene data
					CurrentGene->AddData("REACTIONS",NewData->GetReaction(j)->Query("CGI_ID").data(),STRING);
					vector<string> EnzymeData = NewData->GetReaction(j)->GetAllData("ENZYME",DATABASE_LINK);
					for (int l=0; l < int(EnzymeData.size()); l++) {
						CurrentGene->AddData("ENZYME",EnzymeData[l].data(),STRING);
					}
					EnzymeData = NewData->GetReaction(j)->GetAllData("PATHWAYS",STRING);
					for (int l=0; l < int(EnzymeData.size()); l++) {
						CurrentGene->AddData("PATHWAY",EnzymeData[l].data(),STRING);
					}

				}
				//Adding all unique reactions to the complete database
				if (CompleteDatabase->FindReaction("DATABASE;CGI_ID",NewData->GetReaction(j)->Query("CGI_ID").data()) == NULL && CompleteDatabase->FindReaction("DATABASE;CGI_ID",NewData->GetReaction(j)->GetData("DATABASE",STRING).data()) == NULL) {
					CompleteDatabase->AddReaction(NewData->GetReaction(j));
				}
			}
			//Searching the model for dead compounds and reactions
			NewData->FindDeadEnds();
			NewData->AddData("TRANSPORTED COMPOUNDS",double(TotalTransported));
			NewData->AddData("REACTION WITH GENES",double(TotalReactionsWithGenes));
		}

		delete Strings;
	}

	//Printing the combined compounds table
	Filename.assign(GetParameter("web interface model directory"));
	Filename.append("AllCompoundsTable.txt");
	ofstream CompleteOutput;
	if (!OpenOutput(CompleteOutput,Filename)) {
		return;
	}
	CompleteOutput << "ID	SHORTNAME	NAMES	STATUS IN MODEL	MODELS	FORMULA	MASS	CHARGE(pH7)	DELTA G(kcal/mol)	PATHWAY	DATABASE LINKS	REACTIONS" << endl;
	for (int i=0; i < CompleteDatabase->FNumSpecies(); i++) {
		CompleteOutput << CompleteDatabase->GetSpecies(i)->Query("CGI_ID") << "\t";
		CompleteOutput << CompleteDatabase->GetSpecies(i)->Query("SHORTNAME") << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetSpecies(i)->GetAllDataString("NAME",STRING).data(),"\t",", ") << "\t";
		CompleteOutput << "Not in model" << "\t";
		vector<string> ModelList = CompoundModels[CompleteDatabase->GetSpecies(i)->Query("CGI_ID")];
		for (int j=0; j < int(ModelList.size()-1); j++) {
			CompleteOutput << ModelList[j] << ", ";
		}
		CompleteOutput << ModelList[ModelList.size()-1] << "\t";		CompleteOutput << CompleteDatabase->GetSpecies(i)->FFormula() << "\t";
		CompleteOutput << CompleteDatabase->GetSpecies(i)->FMW() << "\t";
		CompleteOutput << CompleteDatabase->GetSpecies(i)->FCharge() << "\t";
		CompleteOutput << CompleteDatabase->GetSpecies(i)->FEstDeltaG() << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetSpecies(i)->GetAllDataString("PATHWAYS",STRING).data(),"\t",", ") << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetSpecies(i)->GetCombinedData(DATABASE_LINK).data(),"\t",", ") << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetSpecies(i)->Query("REACTIONS").data(),"\t",", ") << endl;
	}
	CompleteOutput.close();

	//Printing the combined reactions table
	Filename.assign(GetParameter("web interface model directory"));
	Filename.append("AllReactionsTable.txt");
	if (!OpenOutput(CompleteOutput,Filename)) {
		return;
	}
	CompleteOutput << "ID	SHORTNAME	NAMES	STATUS IN MODEL	MODELS	DEFINITION	EQUATION	EC NUMBER	ASSIGNED FUNCTIONS	ASSIGNED GENES	DELTA G(kcal/mol)	PATHWAY	PREVIOUS REACTIONS	NEXT REACTIONS" << endl;
	for (int i=0; i < CompleteDatabase->FNumReactions(); i++) {
		CompleteOutput << CompleteDatabase->GetReaction(i)->Query("CGI_ID") << "\t";
		CompleteOutput << CompleteDatabase->GetReaction(i)->Query("SHORTNAME") << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetReaction(i)->GetAllDataString("NAME",STRING).data(),"\t",", ") << "\t";
		CompleteOutput << "Not in model" << "\t";
		vector<string> ModelList = ReactionModels[CompleteDatabase->GetReaction(i)->Query("CGI_ID")];
		for (int j=0; j < int(ModelList.size()-1); j++) {
			CompleteOutput << ModelList[j] << ", ";
		}
		CompleteOutput << ModelList[ModelList.size()-1] << "\t";
		CompleteOutput << CompleteDatabase->GetReaction(i)->Query("DEFINITION") << "\t";
		CompleteOutput << CompleteDatabase->GetReaction(i)->CreateReactionEquation("CGI_ID") << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetReaction(i)->GetAllDataString("ENZYME",DATABASE_LINK).data(),"\t",", ") << "\t";
		CompleteOutput << "No data" << "\t";
		vector<string> AssignedGenes = ReactionGenes[CompleteDatabase->GetReaction(i)->Query("CGI_ID")];
		if (AssignedGenes.size() >= 1) {
			for (int j=0; j < int(AssignedGenes.size()-1); j++) {
				CompleteOutput << AssignedGenes[j] << ", ";
			}
			CompleteOutput << AssignedGenes[AssignedGenes.size()-1] << "\t";
		} else {
			CompleteOutput << "None" << "\t";
		}
		CompleteOutput << CompleteDatabase->GetReaction(i)->FEstDeltaG() << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetReaction(i)->Query("PATHWAYS").data(),"\t",", ") << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetReaction(i)->Query("PREVIOUS").data(),"\t",", ") << "\t";
		CompleteOutput << StringReplace(CompleteDatabase->GetReaction(i)->Query("NEXT").data(),"\t",", ") << endl;
	}
	CompleteOutput.close();

	//Printing the compounds, reactions, and genes tables for each model and the model table
	Filename.assign(GetParameter("web interface model directory"));
	Filename.append("ModelTable.txt");
	if (!OpenOutput(CompleteOutput,Filename)) {
		return;
	}
	CompleteOutput << "ID	MODEL AUTHORS	ORGANISMS NAME	TOTAL COMPOUNDS	TRANSPORTED COMPOUNDS	DEAD COMPOUNDS	TOTAL REACTIONS	REACTIONS WITH GENES	DEAD REACTIONS	NUMBER OF GENES" << endl;
	for (int j=0; j < int(Models.size()); j++) {
		//Printing the model data into the model table
		CompleteOutput << Models[j]->GetData("NAME",STRING) << "\t";
		CompleteOutput << Models[j]->GetData("AUTHORS",STRING) << "\t";
		CompleteOutput << Models[j]->GetData("ORGANISMS NAME",STRING) << "\t";
		CompleteOutput << Models[j]->FNumSpecies() << "\t";
		CompleteOutput << Models[j]->GetDoubleData("TRANSPORTED COMPOUNDS") << "\t";
		CompleteOutput << Models[j]->GetDoubleData("DEAD COMPOUNDS") << "\t";
		CompleteOutput << Models[j]->FNumReactions() << "\t";
		CompleteOutput << Models[j]->GetDoubleData("REACTION WITH GENES") << "\t";
		CompleteOutput << Models[j]->GetDoubleData("DEAD REACTIONS") << "\t";
		CompleteOutput << ModelGenes[j].size() << endl;
		
		ofstream ModelOutput;
		Filename.assign(GetParameter("web interface model directory"));
		Filename.append(Models[j]->GetData("NAME",STRING));
		Filename.append("CompoundsTable.txt");
		if (!OpenOutput(ModelOutput,Filename)) {
			return;
		}
		ModelOutput << "ID	SHORTNAME	NAMES	STATUS IN MODEL	MODELS	FORMULA	MASS	CHARGE(pH7)	DELTA G(kcal/mol)	PATHWAY	DATABASE LINKS	REACTIONS" << endl;
		for (int i=0; i < Models[j]->FNumSpecies(); i++) {
			ModelOutput << Models[j]->GetSpecies(i)->Query("CGI_ID") << "\t";
			ModelOutput << Models[j]->GetSpecies(i)->Query("SHORTNAME") << "\t";
			ModelOutput << StringReplace(Models[j]->GetSpecies(i)->GetAllDataString("NAME",STRING).data(),"\t",", ") << "\t";
			ModelOutput << Models[j]->GetSpecies(i)->GetData("CLASS",STRING) << "\t";
			vector<string> ModelList = ReactionModels[Models[j]->GetSpecies(i)->Query("CGI_ID")];
			if (ModelList.size() > 0) {
				for (int k=0; k < int(ModelList.size()-1); k++) {
					ModelOutput << ModelList[k] << ", ";
				}
				ModelOutput << ModelList[ModelList.size()-1] << "\t";
			} else {
				ModelOutput << "No data" << "\t";
			}
			ModelOutput << Models[j]->GetSpecies(i)->FFormula() << "\t";
			ModelOutput << Models[j]->GetSpecies(i)->FMW() << "\t";
			ModelOutput << Models[j]->GetSpecies(i)->FCharge() << "\t";
			ModelOutput << Models[j]->GetSpecies(i)->FEstDeltaG() << "\t";
			ModelOutput << StringReplace(Models[j]->GetSpecies(i)->GetAllDataString("PATHWAYS",STRING).data(),"\t",", ") << "\t";
			ModelOutput << StringReplace(Models[j]->GetSpecies(i)->GetCombinedData(DATABASE_LINK).data(),"\t",", ") << "\t";
			ModelOutput << StringReplace(Models[j]->GetSpecies(i)->Query("REACTIONS").data(),"\t",", ") << endl;
		}
		ModelOutput.close();

		Filename.assign(GetParameter("web interface model directory"));
		Filename.append(Models[j]->GetData("NAME",STRING));
		Filename.append("ReactionsTable.txt");
		if (!OpenOutput(ModelOutput,Filename)) {
			return;
		}
		ModelOutput << "ID	SHORTNAME	NAMES	STATUS IN MODEL	MODELS	DEFINITION	EQUATION	EC NUMBER	ASSIGNED FUNCTIONS	ASSIGNED GENES	DELTA G(kcal/mol)	PATHWAY	PREVIOUS REACTIONS	NEXT REACTIONS" << endl;
		for (int i=0; i < Models[j]->FNumReactions(); i++) {
			ModelOutput << Models[j]->GetReaction(i)->Query("CGI_ID") << "\t";
			ModelOutput << Models[j]->GetReaction(i)->Query("SHORTNAME") << "\t";
			ModelOutput << StringReplace(Models[j]->GetReaction(i)->GetAllDataString("NAME",STRING).data(),"\t",", ") << "\t";
			ModelOutput << Models[j]->GetReaction(i)->GetData("CLASS",STRING) << "\t";
			vector<string> ModelList = ReactionModels[Models[j]->GetReaction(i)->Query("CGI_ID")];
			if (ModelList.size() > 0) {
				for (int k=0; k < int(ModelList.size()-1); k++) {
					ModelOutput << ModelList[k] << ", ";
				}
				ModelOutput << ModelList[ModelList.size()-1] << "\t";
			}else {
				ModelOutput << "No data" << "\t";
			}
			ModelOutput << Models[j]->GetReaction(i)->Query("DEFINITION") << "\t";
			ModelOutput << Models[j]->GetReaction(i)->CreateReactionEquation("CGI_ID") << "\t";
			ModelOutput << StringReplace(Models[j]->GetReaction(i)->GetAllDataString("ENZYME",DATABASE_LINK).data(),"\t",", ") << "\t";
			ModelOutput << "No data" << "\t";
			vector<string> AssignedGenes = ReactionGenes[Models[j]->GetReaction(i)->Query("CGI_ID")];
			if (AssignedGenes.size() >= 1) {
				bool First = true;
				for (int k=0; k < int(AssignedGenes.size()); k++) {
					if (AssignedGenes[k].length() > Models[j]->GetData("ORGANISMS NAME",STRING).length() && AssignedGenes[k].substr(0,Models[j]->GetData("ORGANISMS NAME",STRING).length()).compare(Models[j]->GetData("ORGANISMS NAME",STRING)) == 0) {
						if (!First) {
							ModelOutput << ", ";
						}
						First = false;
						ModelOutput << AssignedGenes[k].substr(Models[j]->GetData("ORGANISMS NAME",STRING).length()+1,AssignedGenes[k].length()-Models[j]->GetData("ORGANISMS NAME",STRING).length()-1);
					}
				}
				ModelOutput << "\t";
			} else {
				ModelOutput << "None" << "\t";
			}
			ModelOutput << Models[j]->GetReaction(i)->FEstDeltaG() << "\t";
			ModelOutput << StringReplace(Models[j]->GetReaction(i)->Query("PATHWAYS").data(),"\t",", ") << "\t";
			ModelOutput << StringReplace(Models[j]->GetReaction(i)->Query("PREVIOUS").data(),"\t",", ") << "\t";
			ModelOutput << StringReplace(Models[j]->GetReaction(i)->Query("NEXT").data(),"\t",", ") << endl;
		}
		ModelOutput.close();

		Filename.assign(GetParameter("web interface model directory"));
		Filename.append(Models[j]->GetData("NAME",STRING));
		Filename.append("GenesTable.txt");
		if (!OpenOutput(ModelOutput,Filename)) {
			return;
		}
		ModelOutput << "ID	SHORTNAME	NAMES	ASSIGNED FUNCTIONS	STATUS IN ORGANISM	PARALOGS	COORDINATES	DIRECTIONALITY	EC NUMBER	PATHWAY	REACTIONS" << endl;
		for (map<string, Identity*, std::less<string> >::iterator MapIT = ModelGenes[j].begin(); MapIT != ModelGenes[j].end(); MapIT++) {
			ModelOutput << MapIT->first << "\t";
			ModelOutput << MapIT->first << "\t";
			ModelOutput << MapIT->first << "\t";
			ModelOutput << "No data" << "\t";
			ModelOutput << "No data" << "\t";
			ModelOutput << "No data" << "\t";
			ModelOutput << "No data" << "\t";
			ModelOutput << "No data" << "\t";
			ModelOutput << StringReplace(MapIT->second->GetAllDataString("ENZYME",STRING).data(),"\t",", ") << "\t";
			ModelOutput << StringReplace(MapIT->second->GetAllDataString("PATHWAY",STRING).data(),"\t",", ") << "\t";
			ModelOutput << StringReplace(MapIT->second->GetAllDataString("REACTIONS",STRING).data(),"\t",", ") << endl;
		}
		ModelOutput.close();
	}
	CompleteOutput.close();
}
Пример #17
0
int main(int argc, char** argv) {
    Opt2 opt(argc, argv, "rxcpulma:z:qo:", IntRange(1, 999999), "hexdump=x,cat=c,plain=p,unpack=u,list=l,md5=m,recursive=r,quiet=q,prepend=z,append=a,output=o");
    const bool hexdump   = opt.Has('x', "- produce hexdump");
    const bool cat       = opt.Has('c', "- do not store keys (file names), just cat uncompressed files");
    const bool doNotZip  = opt.Has('p', "- do not use compression");
    const bool unpack    = opt.Has('u', "- unpack archive in current dir");
    const bool list      = opt.Has('l', "- list files in archive");
    const bool listMd5   = opt.Has('m', "- list files in archive with md5");
    const bool recursive = opt.Has('r', "- read all files under each directory, recursively");
    Quiet                = opt.Has('q', "- do not output progress on stderr.");
    Stroka prepend = opt.Arg('z', "<PREFIX> - prepend string to output", 0);
    Stroka append  = opt.Arg('a', "<SUFFIX> - append string to output", 0);
    const Stroka outputf = opt.Arg('o', "<FILE> - output to file instead stdout", 0);
    opt.AutoUsageErr("<file>..."); // "Files or directories to archive."

    SubstGlobal(append, "\\n", "\n");
    SubstGlobal(prepend, "\\n", "\n");

    yvector<TRec> recs;
    for (size_t i = 0; i != opt.Pos.size(); ++i) {
        Stroka path = opt.Pos[i];
        size_t off = 0;
#ifdef _win_
        if (path[0] > 0 && isalpha(path[0]) && path[1] == ':')
            off = 2; // skip drive letter ("d:")
#endif // _win_
        const size_t pos = path.find(':', off);
        TRec cur;
        cur.Path = path.substr(0, pos);
        if (pos != Stroka::npos)
            cur.Prefix = path.substr(pos + 1);
        cur.Recursive = recursive;
        cur.Fix();
        recs.push_back(cur);
    }

    try {
        if (listMd5) {
            for (yvector<TRec>::const_iterator it = recs.begin(); it != recs.end(); ++it) {
                ListArchiveMd5(it->Path);
            }
        } else if (list) {
            for (yvector<TRec>::const_iterator it = recs.begin(); it != recs.end(); ++it) {
                ListArchive(it->Path);
            }
        } else if (unpack) {
            for (yvector<TRec>::const_iterator it = recs.begin(); it != recs.end(); ++it) {
                UnpackArchive(it->Path);
            }
        } else {
            TAutoPtr<TOutputStream> outf(OpenOutput(outputf));
            TOutputStream* out = outf.Get();
            THolder<TOutputStream> hexout;

            if (hexdump) {
                hexout.Reset(new THexOutput(out));
                out = hexout.Get();
            }

            outf->Write(~prepend, +prepend);

            if (cat) {
                for (yvector<TRec>::const_iterator it = recs.begin(); it != recs.end(); ++it) {
                    it->Recurse(*out);
                }
            } else {
                TArchiveWriter w(out, !doNotZip);
                for (yvector<TRec>::const_iterator it = recs.begin(); it != recs.end(); ++it) {
                    it->Recurse(w);
                }
                w.Finish();
            }

            outf->Write(~append, +append);

            try {
                out->Finish();
            } catch (...) {
            }
        }
    } catch (...) {
        Cerr << CurrentExceptionMessage() << Endl;
        return 1;
    }

    return 0;
}
Пример #18
0
int main (int argc, const char **argv) {
  Param_t *param = NULL;
  Input_t *input = NULL;
  Lut_t *lut = NULL;
  Output_t *output = NULL;
  Output_t *output_th = NULL;
  int iline, isamp,oline, ib, jb, iz, val;
  unsigned char *line_in = NULL;
  unsigned char *line_in_thz = NULL;
  unsigned char *line_out_qa = NULL;
  int16 *line_out = NULL;
  int16 *line_out_th = NULL;
  int16 *line_out_thz = NULL;
  Cal_stats_t cal_stats;
  Cal_stats6_t cal_stats6;
  int nps,nls, nps6, nls6;
  int zoomx, zoomy;
  int i,odometer_flag=0;
  char msgbuf[1024];
  char envi_file[STR_SIZE]; /* name of the output ENVI header file */
  char *cptr=NULL;          /* pointer to the file extension */
  size_t input_psize;
  int qa_band = QA_BAND_NUM;
  int nband_refl = NBAND_REFL_MAX;
  int ifill, num_zero;
  int maxth=0;
  int mss_flag=0;
  Espa_internal_meta_t xml_metadata;  /* XML metadata structure */
  Envi_header_t envi_hdr;   /* output ENVI header information */

  printf ("\nRunning lndcal ...\n");
  for (i=1; i<argc; i++)if ( !strcmp(argv[i],"-o") )odometer_flag=1;

  /* Read the parameters from the input parameter file */
  param = GetParam(argc, argv);
  if (param == (Param_t *)NULL) EXIT_ERROR("getting runtime parameters",
    "main");

  /* Validate the input metadata file */
  if (validate_xml_file (param->input_xml_file_name) != SUCCESS)
  {  /* Error messages already written */
      EXIT_ERROR("Failure validating XML file", "main");
  }

  /* Initialize the metadata structure */
  init_metadata_struct (&xml_metadata);

  /* Parse the metadata file into our internal metadata structure; also
     allocates space as needed for various pointers in the global and band
     metadata */
  if (parse_metadata (param->input_xml_file_name, &xml_metadata) != SUCCESS)
  {  /* Error messages already written */
    EXIT_ERROR("parsing XML file", "main");
  }

  /* Check to see if the gain and bias values were specified */
  if (!existRadGB (&xml_metadata))
    EXIT_ERROR("Gains and biases don't exist in XML file (TOA radiance gain "
      "and bias fields) for each band.  Make sure to utilize the latest LPGS "
      "MTL file for conversion to the ESPA internal raw binary format as the "
      "gains and biases should be in that file.", "main");
  
  /* Open input file */
  input = OpenInput (&xml_metadata);
  if (input == (Input_t *)NULL)
    EXIT_ERROR("setting up input from XML structure", "main");

  /* Get Lookup table */
  lut = GetLut(param, input->nband, input);
  if (lut == (Lut_t *)NULL) EXIT_ERROR("bad lut file", "main");

  nps6=  input->size_th.s;
  nls6=  input->size_th.l;
  nps =  input->size.s;
  nls =  input->size.l;
  zoomx= nint( (float)nps / (float)nps6 );
  zoomy= nint( (float)nls / (float)nls6 );

  for (ib = 0; ib < input->nband; ib++) 
    cal_stats.first[ib] = true;
  cal_stats6.first = true;
  if (input->meta.inst == INST_MSS)mss_flag=1; 

  /* Open the output files.  Raw binary band files will be be opened. */
  output = OpenOutput(&xml_metadata, input, param, lut, false /*not thermal*/,
    mss_flag);
  if (output == NULL) EXIT_ERROR("opening output file", "main");

  /* Allocate memory for the input buffer, enough for all reflectance bands */
  input_psize = sizeof(unsigned char);
  line_in = calloc (input->size.s * nband_refl, input_psize);
   if (line_in == NULL) 
     EXIT_ERROR("allocating input line buffer", "main");

  /* Create and open output thermal band, if one exists */
  if ( input->nband_th > 0 ) {
    output_th = OpenOutput (&xml_metadata, input, param, lut, true /*thermal*/,
      mss_flag);
    if (output_th == NULL)
      EXIT_ERROR("opening output therm file", "main");

    /* Allocate memory for the thermal input and output buffer, only holds
       one band */
    line_out_th = calloc(input->size_th.s, sizeof(int16));
    if (line_out_th == NULL) 
      EXIT_ERROR("allocating thermal output line buffer", "main");

    if (zoomx == 1) {
      line_out_thz = line_out_th;
      line_in_thz = line_in;
    }
    else {
      line_out_thz = calloc (input->size.s, sizeof(int16));
      if (line_out_thz == NULL) 
        EXIT_ERROR("allocating thermal zoom output line buffer", "main");
      line_in_thz = calloc (input->size.s, input_psize);
      if (line_in_thz == NULL) 
        EXIT_ERROR("allocating thermal zoom input line buffer", "main");
    }
  } else {
    printf("*** no output thermal file ***\n"); 
  }

  /* Allocate memory for output lines for both the image and QA data */
  line_out = calloc (input->size.s, sizeof (int16));
  if (line_out == NULL) 
    EXIT_ERROR("allocating output line buffer", "main");

  line_out_qa = calloc (input->size.s, sizeof(unsigned char));
  if (line_out_qa == NULL) 
    EXIT_ERROR("allocating qa output line buffer", "main");
  memset (line_out_qa, 0, input->size.s * sizeof(unsigned char));    

  /* Do for each THERMAL line */
  oline= 0;
  if (input->nband_th > 0) {
    ifill= (int)lut->in_fill;
    for (iline = 0; iline < input->size_th.l; iline++) {
      ib=0;
      if (!GetInputLineTh(input, iline, line_in))
        EXIT_ERROR("reading input data for a line", "main");

      if ( odometer_flag && ( iline==0 || iline ==(nls-1) || iline%100==0  ) ){ 
        if ( zoomy == 1 )
          printf("--- main loop BAND6 Line %d --- \r",iline); 
        else
          printf("--- main loop BAND6 Line in=%d out=%d --- \r",iline,oline); 
        fflush(stdout); 
      }

      memset(line_out_qa, 0, input->size.s*sizeof(unsigned char));    
      if (!Cal6(lut, input, line_in, line_out_th, line_out_qa, &cal_stats6,
        iline))
        EXIT_ERROR("doing calibration for a line", "main");

      if ( zoomx>1 ) {
        zoomIt(line_out_thz, line_out_th, nps/zoomx, zoomx );
        zoomIt8(line_in_thz, line_in, nps/zoomx, zoomx );
      }

      for ( iz=0; iz<zoomy; iz++ ) {
        for (isamp = 0; isamp < input->size.s; isamp++) {
          val= getValue(line_in_thz, isamp);
          if ( val> maxth) maxth=val;
          if ( val==ifill) line_out_qa[isamp] = lut->qa_fill; 
          else if ( val>=SATU_VAL6 ) line_out_qa[isamp] = ( 0x000001 << 6 ); 
        }

        if ( oline<nls ) {
          if (!PutOutputLine(output_th, ib, oline, line_out_thz)) {
            sprintf(msgbuf,"write thermal error ib=%d oline=%d iline=%d",ib,
              oline,iline);
            EXIT_ERROR(msgbuf, "main");
          }

          if (input->meta.inst != INST_MSS) 
            if (!PutOutputLine(output_th, ib+1, oline, line_out_qa)) {
	          sprintf(msgbuf,"write thermal QA error ib=%d oline=%d iline=%d",
                ib+1,oline,iline);
              EXIT_ERROR(msgbuf, "main");
            }
        }
        oline++;
      }
    } /* end loop for each thermal line */
  }
  if (odometer_flag) printf("\n");

  if (input->nband_th > 0)
    if (!CloseOutput(output_th))
      EXIT_ERROR("closing output thermal file", "main");

  /* Do for each REFLECTIVE line */
  ifill= (int)lut->in_fill;
  for (iline = 0; iline < input->size.l; iline++){
    /* Do for each band */

    if ( odometer_flag && ( iline==0 || iline ==(nls-1) || iline%100==0  ) )
     {printf("--- main reflective loop Line %d ---\r",iline); fflush(stdout);}

    memset(line_out_qa, 0, input->size.s*sizeof(unsigned char));
    
    for (ib = 0; ib < input->nband; ib++) {
      if (!GetInputLine(input, ib, iline, &line_in[ib*nps]))
        EXIT_ERROR("reading input data for a line", "main");
    }
    
    for (isamp = 0; isamp < input->size.s; isamp++){
      num_zero=0;
      for (ib = 0; ib < input->nband; ib++) {
        jb= (ib != 5 ) ? ib+1 : ib+2;
        val= getValue((unsigned char *)&line_in[ib*nps], isamp);
	    if ( val==ifill   )num_zero++;
        if ( val==SATU_VAL[ib] ) line_out_qa[isamp]|= ( 0x000001 <<jb ); 
      }
      /* Feng fixed bug by changing "|=" to "=" below (4/17/09) */
      if ( num_zero >  0 )line_out_qa[isamp] = lut->qa_fill; 
    }

    for (ib = 0; ib < input->nband; ib++) {
      if (!Cal(lut, ib, input, &line_in[ib*nps], line_out, line_out_qa,
        &cal_stats,iline))
        EXIT_ERROR("doing calibraton for a line", "main");

      if (!PutOutputLine(output, ib, iline, line_out))
        EXIT_ERROR("reading input data for a line", "main");
    } /* End loop for each band */
        
    if (input->meta.inst != INST_MSS) 
      if (!PutOutputLine(output, qa_band, iline, line_out_qa))
        EXIT_ERROR("writing qa data for a line", "main");
  } /* End loop for each line */

  if ( odometer_flag )printf("\n");

  for (ib = 0; ib < input->nband; ib++) {
    printf(
      " band %d rad min %8.5g max %8.4f  |  ref min  %8.5f max  %8.4f\n", 
      input->meta.iband[ib], cal_stats.rad_min[ib], cal_stats.rad_max[ib],
      cal_stats.ref_min[ib], cal_stats.ref_max[ib]);
  }

  if ( input->nband_th > 0 )
    printf(
      " band %d rad min %8.5g max %8.4f  |  tmp min  %8.5f max  %8.4f\n", 6,
      cal_stats6.rad_min,  cal_stats6.rad_max,
      cal_stats6.temp_min, cal_stats6.temp_max);

  /* Close input and output files */
  if (!CloseInput(input)) EXIT_ERROR("closing input file", "main");
  if (!CloseOutput(output)) EXIT_ERROR("closing input file", "main");

  /* Write the ENVI header for reflectance files */
  for (ib = 0; ib < output->nband; ib++) {
    /* Create the ENVI header file this band */
    if (create_envi_struct (&output->metadata.band[ib], &xml_metadata.global,
      &envi_hdr) != SUCCESS)
        EXIT_ERROR("Creating the ENVI header structure for this file.", "main");

    /* Write the ENVI header */
    strcpy (envi_file, output->metadata.band[ib].file_name);
    cptr = strchr (envi_file, '.');
    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
        EXIT_ERROR("Writing the ENVI header file.", "main");
  }

  /* Write the ENVI header for thermal files */
  for (ib = 0; ib < output_th->nband; ib++) {
    /* Create the ENVI header file this band */
    if (create_envi_struct (&output_th->metadata.band[ib], &xml_metadata.global,
      &envi_hdr) != SUCCESS)
        EXIT_ERROR("Creating the ENVI header structure for this file.", "main");

    /* Write the ENVI header */
    strcpy (envi_file, output_th->metadata.band[ib].file_name);
    cptr = strchr (envi_file, '.');
    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
        EXIT_ERROR("Writing the ENVI header file.", "main");
  }

  /* Append the reflective and thermal bands to the XML file */
  if (append_metadata (output->nband, output->metadata.band,
    param->input_xml_file_name) != SUCCESS)
    EXIT_ERROR("appending reflectance and QA bands", "main");
  if (input->nband_th > 0) {
    if (append_metadata (output_th->nband, output_th->metadata.band,
      param->input_xml_file_name) != SUCCESS)
      EXIT_ERROR("appending thermal and QA bands", "main");
  }

  /* Free the metadata structure */
  free_metadata (&xml_metadata);

  /* Free memory */
  if (!FreeParam(param)) 
    EXIT_ERROR("freeing parameter stucture", "main");

  if (!FreeInput(input)) 
    EXIT_ERROR("freeing input file stucture", "main");

  if (!FreeLut(lut)) 
    EXIT_ERROR("freeing lut file stucture", "main");

  if (!FreeOutput(output)) 
    EXIT_ERROR("freeing output file stucture", "main");

  free(line_out);
  line_out = NULL;
  free(line_in);
  line_in = NULL;
  free(line_out_qa);
  line_out_qa = NULL;
  free(line_out_th);
  line_out_th = NULL;
  if (zoomx != 1) {
    free(line_in_thz);
    free(line_out_thz);
  }
  line_in_thz = NULL;
  line_out_thz = NULL;

  /* All done */
  printf ("lndcal complete.\n");
  return (EXIT_SUCCESS);
}