示例#1
0
/* open a clone file
 * AppOpenClone tests if a clone file is specified and if it's a
 * a valid CSF file. If not an error message is printed. If all
 * tests are passed then the map is opened read-only.
 * returns 
 *  the map pointer or NULL is an error message is printed.
 */ 
MAP *AppOpenClone(
	char **cloneFileName, /* write-only the name of the resulting clone, pointer assignment only */
	const char *cmdLineClone)   /* clone on the command-line, NULL if not given. 
	                             * This one has priority over the global one
	                             */
{
	MAP *clone = NULL;
	/* open the clone map */
	if(cmdLineClone == NULL)
	{       /* check if the global clone is set and use */
		if (appClone == NULL)
		{
			Error("no clone map specified with: --clone CloneName");
	         	return NULL;
		}
		*cloneFileName = appClone;
	}
	else
		*cloneFileName = (char *)cmdLineClone;

	/* Check clone file  */
	switch(FileStat(*cloneFileName)) {
	 case 0: clone = Mopen(*cloneFileName, M_READ);
	         if (clone != NULL)
	               break;
	         if (Merrno != NOT_CSF) /* else fall through */
	         	Mperror(*cloneFileName);
	 case 1: Error("clone map '%s' is not a map",*cloneFileName);
	         return NULL;
	 case 2: Error("clone map '%s' does not exist",*cloneFileName);
	         return NULL;
	}
	return clone;
}
/*!
  \param     filename Filename of raster to open.
  \return    Pointer to CSF MAP structure.
  \exception .
  \warning   .
  \sa        .
*/
MAP* mapOpen(
         std::string const& filename,
         MOPEN_PERM mode)
{
  MAP* map = Mopen(filename.c_str(), mode);

  return map;
}
示例#3
0
文件: main.c 项目: pcraster/pcraster
static int EditOption(
       const char *name)
{
       ATTRIBUTES a;
       MAP *in = Mopen(name, M_READ_WRITE);
       if (in == NULL || ReadAttr(&a,in,FALSE))
       {
         if (in != NULL)
          Mclose(in);
         return RetError(1,"while reading '%s': %s",name,MstrError());
       }
       if (a.version != 2)
        return RetError(1,"'%s' is not a version 2 map, no edits possible");
       a.cloneCreation = FALSE;
        switch(MakeCloneMenu(&a, name))
        { 
            case 0: return 0;
            case 1: return SetAndCloseMap(in, &a);
            case 2: fprintf(stderr,"No map attributes written\n");
                    return 0;
        }
        POSTCOND(FALSE);
        return 1;
}
示例#4
0
文件: main.c 项目: pcraster/pcraster
static int PrintOption(
 const char **names,
 int nrNames)
{
       ATTRIBUTES *a = (ATTRIBUTES *)ChkMalloc(sizeof(ATTRIBUTES) * nrNames);
       int *colLen = (int *)ChkMalloc(sizeof(int) * nrNames);
       int i;
       if (a == NULL || colLen == NULL)
              return 1;
       for(i = 0 ; i < nrNames; i++)
       {
              MAP *m= Mopen(names[i],M_READ);
              if (m == NULL)
                  {
                   Free(a);
                   return RetError(1,"while reading map '%s': %s",names[i],MstrError());
                  }
                  ReadAttr(a+i,m,TRUE);
                  colLen[i] = MAX(11, strlen(names[i]));
                  Mclose(m);
       }

       if (printDataType)
       {
        for(i = 0 ; i < nrNames; i++)
        {
        int c;
         switch(a[i].valueScale) {
              case VS_LDD : c = 'L'; break;
              case VS_SCALAR : c = 'S'; break;
              case VS_BOOLEAN : c = 'B'; break;
              case VS_NOMINAL : c = 'N'; break;
              case VS_ORDINAL : c = 'O'; break;
              case VS_DIRECTION : c = 'D'; break;
              default : c = ' '; break;
         }
         printf("%c",c);
        }
        return 1;
       }

        printf("%s",HEAD);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*s", colLen[i], names[i]);
       printf("\n");

        printf("%s",printLabels[ATTR_nrRows]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*u", colLen[i], a[i].nrRows);
       printf("\n");

        printf("%s",printLabels[ATTR_nrCols]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*u", colLen[i], a[i].nrCols);
       printf("\n");

        printf("%s",printLabels[ATTR_cellSize]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*g", colLen[i], a[i].cellSize);
       printf("\n");

       
        printf("%s",printLabels[ATTR_valueScale]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*s", colLen[i], RstrValueScale(a[i].valueScale));
       printf("\n");

        printf("%s",printLabels[ATTR_cellRepr]);
       for(i = 0 ; i < nrNames; i++)
       { const char *cStr;
         switch(a[i].cellRepr)
         { case CR_UINT1 : cStr = "small"; break;
           case CR_INT4  : cStr = "large"; break;
           case CR_REAL4 : cStr = "single"; break;
           case CR_REAL8 : cStr = "double"; break;
           default : cStr = RstrCellRepr(a[i].cellRepr);
          }
         printf(" %-*s", colLen[i], cStr);
        }
       printf("\n");

       printf("%s",printLabels[ATTR_projection]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*s", colLen[i], a[i].projection ? "yb2t" : "yt2b" );
       printf("\n");

        printf("%s",printLabels[ATTR_angle]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*g", colLen[i], a[i].angle);
       printf("\n");

       printf("%s",printLabels[ATTR_xUL]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*g", colLen[i], a[i].xUL);
       printf("\n");

       printf("%s",printLabels[ATTR_yUL]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*g", colLen[i], a[i].yUL);
       printf("\n");

       printf("%s",printLabels[ATTR_minVal]);
       for(i = 0 ; i < nrNames; i++)
        if (IS_MV_REAL8(&(a[i].minVal)))
         printf(" %-*s", colLen[i], "mv");
        else
        { 
         if ((a[i].cellRepr) & CSF_FLOAT_MASK)
          printf(" %-*g", colLen[i], a[i].minVal);
         else
          printf(" %-*d", colLen[i], (int)a[i].minVal);
        }
       printf("\n");

       printf("%s",printLabels[ATTR_maxVal]);
       for(i = 0 ; i < nrNames; i++)
        if (IS_MV_REAL8(&(a[i].maxVal)))
         printf(" %-*s", colLen[i], "mv");
        else
        {
         if ((a[i].cellRepr) & CSF_FLOAT_MASK)
          printf(" %-*g", colLen[i], a[i].maxVal);
         else
          printf(" %-*d", colLen[i], (int)a[i].maxVal);
        }
       printf("\n");

       printf("%s",printLabels[ATTR_version]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*u", colLen[i], a[i].version);
       printf("\n");

       printf("%s",printLabels[ATTR_gisFileId]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*u", colLen[i], a[i].gisFileId);
       printf("\n");

       printf("%s",printLabels[ATTR_byteOrder]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*s", colLen[i], a[i].byteOrder == 1 ? "y" : "n");
       printf("\n");

       printf("%s",printLabels[ATTR_attrTable]);
       for(i = 0 ; i < nrNames; i++)
         printf(" %-*s", colLen[i], a[i].attrTable == 0 ? "n" : "y");
       printf("\n");

       return 0;
}
示例#5
0
/*
 * Recycle vsn.
 */
static void
recycle(
	VSN_TABLE *vsn)
{
	char *host;
	ROBOT_TABLE *robot = NULL;
	struct DiskVolsDictionary *diskvols;

	/*
	 * Set archive set.
	 */
	robot = vsn->robot;

	if (robot == NULL) {
		Trace(TR_ERR,
		    "Could not find disk archive set for volume '%s'",
		    vsn->vsn);
		return;
	}

	Trace(TR_MISC,
	    "[%s] Recycling disk volume minobs: %d%% dataquantity: %s (%d)",
	    vsn->vsn, robot->obs,
	    StrFromFsize(robot->dataquantity, 3, NULL, 0),
	    robot->limit_quantity);

	Trace(TR_MISC, "[%s] Needs recycling: %d ignore: %d candidate: %d",
	    vsn->vsn, vsn->needs_recycling, ignoreRecycling, vsn->candidate);

	/* Recycling disk archive set */
	cemit(TO_FILE, 0, 20308, robot->name, vsn->vsn);

	if (IS_DISK_HONEYCOMB(vsn->media)) {
		/*
		 * Metadata records for the following list of OIDs
		 * will be deleted.
		 */
		cemit(TO_FILE, 0, 20354);
	} else {
		/*
		 * The following list of files and directories will be removed.
		 */
		cemit(TO_FILE, 0, 20355);
	}

	if (ignoreRecycling) {
		cemit(TO_FILE, 0, 20309);
	}

	diskVolume = NULL;
	diskvols = DiskVolsGetHandle(DISKVOLS_VSN_DICT);
	if (diskvols != NULL) {
		(void) diskvols->Get(diskvols,
		    (char *)&vsn->vsn[0], &diskVolume);
	}
	if (diskVolume == NULL) {
		Trace(TR_ERR, "Could not find disk volume '%s'",
		    (char *)&vsn->vsn[0]);
		goto out;
	}

	host = DiskVolsGetHostname(diskVolume);
	rft = SamrftConnect(host);
	if (rft == NULL) {
		if (host == NULL) {
			host = "";
		}
		Trace(TR_ERR, "Sam rft connection to '%s' failed", host);
		goto out;
	}

	numRecycledFiles = 0;

	/*
	 * Recycle sequence numbers.  Any sequence number not
	 * being used are mapped to files which can be removed from the
	 * disk archive volume.
	 */
	recycleSeqNumbers(robot, vsn);

	if (rft != NULL) {
		SamrftDisconnect(rft);
		rft = NULL;
	}

	if (diskVolume->DvFlags & DV_remote) {
		cemit(TO_FILE, 0, 20302, numRecycledFiles, vsn->vsn,
		    diskVolume->DvHost, diskVolume->DvPath);
	} else {
		cemit(TO_FILE, 0, 20303, numRecycledFiles, vsn->vsn,
		    diskVolume->DvPath);
	}

	if (robot->mail && ignoreRecycling == B_FALSE) {
		char *fmt;
		char *subject;
		char *mailaddr;
		FILE *mailfile;

		subject = catgets(catfd, SET, 20257, "Message number 20257");
		mailaddr = robot->mailaddress;

		mailfile = Mopen(mailaddr);
		if (mailfile != NULL) {
			if (diskVolume->DvFlags & DV_remote) {
				fmt = catgets(catfd, SET, 20302,
				    "Message number 20302");
				fprintf(mailfile, fmt, numRecycledFiles,
				    vsn->vsn, diskVolume->DvHost,
				    diskVolume->DvPath);
				fprintf(mailfile, "\n");
			} else {
				fmt = catgets(catfd, SET, 20303,
				    "Message number 20303");
				fprintf(mailfile, fmt, numRecycledFiles,
				    vsn->vsn, diskVolume->DvPath);
				fprintf(mailfile, "\n");
			}
			Msend(&mailfile, mailaddr, subject);
		}
	}
out:
	DiskVolsRelHandle(DISKVOLS_VSN_DICT);
}
示例#6
0
文件: main.c 项目: pcraster/pcraster
/* Function for resampling  N input maps into 1 output map.
 * Assumes a map "clone.map" and N "input.map"s present. Checks on
 * options for percentage and maximum value. 
 * Determines type and characteristics of output map.
 * Returns nothing, exits with 1 in case of error.
 */
int main(int argc,		/* number of arguments */
	char *argv[])		/* list of arguments */
{
     	MAP 	*clone, *out, *tmp, **in;
     	char 	*outputName, *cloneName;	
     	int 	c, borderval;	
     	size_t  nrMaps,i;
	REAL8 	X0, Y0, cellSize, angleIn, angleOut;
	size_t 	nrRows, nrCols;
	CSF_PT 	projection;
	CSF_CR  cellRepr;
	CSF_VS  valueScale;
	double 	percent = 0, errFactor = 2.5, resampleN = 0.0;
	BOOL	aligned = TRUE;
	BOOL    keepInputMinMax = FALSE;
	REAL8	minAllInput=0, maxAllInput=0;
	BOOL	onlyReal4 = TRUE, contract = FALSE;
	BOOL	onlyUint1 = TRUE;

	if(InstallArgs(argc, argv,"axmp$r$c#b#e$RBCk", "resample", __DATE__))
		exit(1);

     	while((c = GetOpt()) != 0)
     	{
     	    switch(c)
     	    {
     	    	case 'b': opB = TRUE;
     	    		borderval = *((int *) OptArg);
     	    		break;
     	    	case 'B': opB = TRUE;
     	    		borderval = 0;
     	    		break;
     	    	case 'C': opMV = TRUE;
     	    		borderval = 0;
     	    		break;
     	    	case 'c': opMV = TRUE;
     	    		borderval = *((int *) OptArg);
     	    		break;
     	    	case 'a':contract = TRUE;
     	    		break;
     	    	case 'x':contract = FALSE;
     	    		break;
     	    	case 'm':opMax = 1;
     	    		break;
     	    	case 'p':opPer = 1;
     	    		percent = *((double*) OptArg);
     	    		if(percent < 0 || 100 < percent)
     	    		{
     	    			Error("illegal percentage");
     	    			exit(1);
     	    		}
     	    		break;
     	    	case 'R':opR = 1;
     	    		resampleN = 1;
     	    		break;
     	    	case 'r':opR = 1;
     	    		resampleN = *((double*) OptArg);
     	    		break;
     	    	case 'e':optionAcc = 1;
     	    		errFactor = *((double*) OptArg);
     	    		break;
     	    	case 'k': keepInputMinMax = TRUE;
     	    		break;
     	    }
     	}

	argv = ArgArguments(&argc);
	if (AppArgCountCheck(argc,3,-1,USAGE))
		exit(1);

	outputName = argv[argc-1];
  	nrMaps  = argc-2;

	/* Read the desired specifics out of the clone map 
	 * or use first input as clone map
	 */
	cloneName = NO_CLONE_NEEDED ? argv[1] : NULL;
	if ( (clone = AppOpenClone(&cloneName,cloneName)) == NULL)
		exit(1);

	/* Determine the valueScale out of 1st input map */
	tmp = Mopen(argv[1], M_READ);
	if(tmp == NULL)
		MperrorExit(argv[1], 1);

	/* all input maps have same value scale */
	valueScale = RgetValueScale(tmp);
	if(valueScale == VS_LDD && !opMV)
	{
		Error("can not do this type of resampling on map '%s' with type ldd", argv[1]);
		exit(1);
	}
	/* adjust old ones */
	if(valueScale == VS_CLASSIFIED)
		valueScale = VS_ORDINAL;
	if(valueScale == VS_CONTINUOUS)
		valueScale = VS_SCALAR;

	/* get location attributes of clone or of 1st input map */
	projection = MgetProjection(clone);
	nrRows = RgetNrRows(clone);
	nrCols = RgetNrCols(clone);
	X0 = RgetX0(clone);
	Y0 = RgetY0(clone);
	cellRepr = RgetCellRepr(clone);
	angleOut = RgetAngle(clone); 

	/* resample option -> cell size(inputmap) * factor 
	 * Number of rows and columns are divided by resample
	 * factor.
	 */
	if(opR == 1)
	{
		/* setting for unit */
		if(!appUnitTrue)
		{
			cellSize = resampleN;
			resampleN /= (double) RgetCellSize(tmp);
		}
		else
			cellSize = RgetCellSize(tmp) * resampleN;
		if(contract)
		{
			nrRows = floor((double) nrRows / 
					(double) resampleN);
			nrCols = floor((double) nrCols / 
					(double) resampleN);

			/* Prevent an illegal map */
			if(nrRows == 0)
				nrRows = 1;
			if(nrCols == 0)
				nrCols = 1;
		}
		else
		{
			nrRows = ceil((double) nrRows / 
					(double) resampleN);
			nrCols = ceil((double) nrCols / 
					(double) resampleN);
		}
	}
	else
		cellSize = RgetCellSize(clone);

	/* Allocate memory for the input map pointers */
	in = (MAP **)ChkMalloc(sizeof(MAP *) * nrMaps);
	if(in == NULL)
	{
		AppEnd();
		exit(1);
	}

	/* Read all input maps with desired cell representation */
	for(i = 0; i < nrMaps; i++)
	{
		REAL8	tmpMin, tmpMax;

		tmp = Mopen(argv[1 + i], M_READ);
		angleIn = RgetAngle(tmp);
		if(angleIn != 0)
			aligned = FALSE;
		if(tmp == NULL)
			MperrorExit(argv[1 + i], 1);

		if(!RvalueScaleIs(tmp, valueScale))
		{
			Error("%s has illegal data type: '%s'\n",
				argv[1 + i], RstrValueScale(valueScale));
			exit(1);
		}

		in[i] = tmp;

		/* Determine which cell representation should be used */
		onlyReal4 = RgetCellRepr(in[i]) == CR_REAL4;
		onlyUint1 = RgetCellRepr(in[i]) == CR_UINT1;


		RuseAs(in[i], CR_REAL8);
		RgetMinVal(tmp, &tmpMin);
		RgetMaxVal(tmp, &tmpMax);
		if (i==0)
		 {minAllInput = tmpMin; maxAllInput = tmpMax; }
		minAllInput = MIN(minAllInput,tmpMin);
		maxAllInput = MAX(maxAllInput,tmpMax);

		if(AppIsClassified(valueScale))
			RuseAs(in[i], CR_INT4);
		else
			RuseAs(in[i], CR_REAL8);
	}

	if(opB == 1 || opMV == 1)
	{
		if(CheckInputMaps(in, nrMaps, projection, angleIn, cellSize))
		{
			Error("");
			FreeMaps(in, nrMaps);
			exit(1);
		}

		if(opB == 1)
		{
			if(SmallestFittingRectangle(&X0, &Y0, &nrRows,
			  &nrCols, in, borderval, nrMaps,
			  cellSize, angleIn, projection, contract))
			{
				FreeMaps(in, nrMaps);
				AppEnd();
				exit(1);
			}
		}
		else
		{
		  	if(SmallestNonMVRect(&X0, &Y0, &nrRows, &nrCols, in,
		  	borderval, nrMaps, valueScale, cellSize,
		  	angleIn, projection, contract))
			{	
				FreeMaps(in, nrMaps);
				AppEnd();
				exit(1);
			}
		}
	}

	/* Create output map with suitable cell representation */ 
	/* NOTE ! Create map with smallest representation possible */
	out = Rcreate(outputName, nrRows, nrCols, 
	               AppIsClassified(valueScale) ?
		               (onlyUint1 ? CR_UINT1 : CR_INT4) :
		               (onlyReal4 ? CR_REAL4 : CR_REAL8),
		               valueScale, projection, X0, Y0, 
		               angleOut, cellSize);
	if(out == NULL)
	{
		FreeMaps(in, nrMaps);
		Error("can not create output map '%s': %s", 
		      argv[1], MstrError());
		exit(1);
	}
	RuseAs(out, AppIsClassified(valueScale) ? CR_INT4 : CR_REAL8);

	if(angleOut != 0)
		aligned = FALSE;


	/* determine raster size according wanted accuracy */
	if(opB != 1 && opMV != 1)
	{
		if(DetRasterSize(out, in, nrMaps, errFactor))
		{
			Error("Illegal cell size\n");
			exit(1);
		}
	}
	else
		rasterSize = 1;

	if(nrMaps > 1 && percent > 0)
		AppProgress("rasterSize: %d\n", rasterSize);
	else
		AppProgress("No raster used\n");

	/* Call function */
	if(AppIsClassified(valueScale))
	{	/* Call resample function for classified maps */

		if(SampleClass(out, in, percent, nrMaps, nrRows, nrCols,
		   aligned, angleOut))
		{	
			EndResample(in, nrMaps, out);
			exit(1);	/* Memory allocation failed */
		}	
	}		
	else
	{	/* Call resample function for continuous maps */
		if(SampleCont(out, in, percent, nrMaps, nrRows, nrCols,
		   aligned, angleOut))
		{	
			EndResample(in, nrMaps, out);
			exit(1);	/* Memory allocation failed */
		}	
	}		

	/* End of call */
	if (keepInputMinMax) {
		RuseAs(out, CR_REAL8);
		RputMinVal(out, &minAllInput);
		RputMaxVal(out, &maxAllInput);
	}
	EndResample(in, nrMaps, out);
	
	exit(0);  			/* Successful exit */
	return 0; 			/* Never reached */
} /* main */